moveList.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/gameBase/moveList.h"
  23. #include "T3D/gameBase/gameConnection.h"
  24. #include "core/stream/bitStream.h"
  25. MoveList::MoveList()
  26. {
  27. mConnection = NULL;
  28. mControlMismatch = false;
  29. reset();
  30. }
  31. void MoveList::reset()
  32. {
  33. mLastMoveAck = 0;
  34. mLastClientMove = 0;
  35. mFirstMoveIndex = 0;
  36. mMoveVec.clear();
  37. }
  38. bool MoveList::getNextMove(Move &curMove)
  39. {
  40. if ( mMoveVec.size() > MaxMoveQueueSize )
  41. return false;
  42. F32 pitchAdd = MoveManager::mPitchUpSpeed - MoveManager::mPitchDownSpeed;
  43. F32 yawAdd = MoveManager::mYawLeftSpeed - MoveManager::mYawRightSpeed;
  44. F32 rollAdd = MoveManager::mRollRightSpeed - MoveManager::mRollLeftSpeed;
  45. curMove.pitch = MoveManager::mPitch + pitchAdd;
  46. curMove.yaw = MoveManager::mYaw + yawAdd;
  47. curMove.roll = MoveManager::mRoll + rollAdd;
  48. MoveManager::mPitch = 0;
  49. MoveManager::mYaw = 0;
  50. MoveManager::mRoll = 0;
  51. curMove.x = MoveManager::mRightAction - MoveManager::mLeftAction + MoveManager::mXAxis_L;
  52. curMove.y = MoveManager::mForwardAction - MoveManager::mBackwardAction + MoveManager::mYAxis_L;
  53. curMove.z = MoveManager::mUpAction - MoveManager::mDownAction;
  54. curMove.freeLook = MoveManager::mFreeLook;
  55. curMove.deviceIsKeyboardMouse = MoveManager::mDeviceIsKeyboardMouse;
  56. for(U32 i = 0; i < MaxTriggerKeys; i++)
  57. {
  58. curMove.trigger[i] = false;
  59. if(MoveManager::mTriggerCount[i] & 1)
  60. curMove.trigger[i] = true;
  61. else if(!(MoveManager::mPrevTriggerCount[i] & 1) && MoveManager::mPrevTriggerCount[i] != MoveManager::mTriggerCount[i])
  62. curMove.trigger[i] = true;
  63. MoveManager::mPrevTriggerCount[i] = MoveManager::mTriggerCount[i];
  64. }
  65. if (mConnection->getControlObject())
  66. mConnection->getControlObject()->preprocessMove(&curMove);
  67. curMove.clamp(); // clamp for net traffic
  68. return true;
  69. }
  70. void MoveList::pushMove(const Move &mv)
  71. {
  72. U32 id = mFirstMoveIndex + mMoveVec.size();
  73. U32 sz = mMoveVec.size();
  74. mMoveVec.push_back(mv);
  75. mMoveVec[sz].id = id;
  76. mMoveVec[sz].sendCount = 0;
  77. }
  78. U32 MoveList::getMoves(Move** movePtr,U32* numMoves)
  79. {
  80. if (mConnection->isConnectionToServer())
  81. {
  82. // give back moves starting at the last client move...
  83. AssertFatal(mLastClientMove >= mFirstMoveIndex, "Bad move request");
  84. AssertFatal(mLastClientMove - mFirstMoveIndex <= mMoveVec.size(), "Desynched first and last move.");
  85. *numMoves = mMoveVec.size() - mLastClientMove + mFirstMoveIndex;
  86. *movePtr = mMoveVec.address() + mLastClientMove - mFirstMoveIndex;
  87. }
  88. else
  89. {
  90. // return the full list
  91. *numMoves = mMoveVec.size();
  92. *movePtr = mMoveVec.begin();
  93. }
  94. return *numMoves;
  95. }
  96. void MoveList::collectMove()
  97. {
  98. Move mv;
  99. if (mConnection)
  100. {
  101. if(!mConnection->isPlayingBack() && getNextMove(mv))
  102. {
  103. mv.checksum=Move::ChecksumMismatch;
  104. pushMove(mv);
  105. mConnection->recordBlock(GameConnection::BlockTypeMove, sizeof(Move), &mv);
  106. }
  107. }
  108. else
  109. {
  110. if(getNextMove(mv))
  111. {
  112. mv.checksum=Move::ChecksumMismatch;
  113. pushMove(mv);
  114. }
  115. }
  116. }
  117. void MoveList::clearMoves(U32 count)
  118. {
  119. if (mConnection->isConnectionToServer())
  120. {
  121. mLastClientMove += count;
  122. AssertFatal(mLastClientMove >= mFirstMoveIndex, "Bad move request");
  123. AssertFatal(mLastClientMove - mFirstMoveIndex <= mMoveVec.size(), "Desynched first and last move.");
  124. if (!mConnection)
  125. // drop right away if no connection
  126. ackMoves(count);
  127. }
  128. else
  129. {
  130. AssertFatal(count <= mMoveVec.size(),"GameConnection: Clearing too many moves");
  131. for (S32 i=0; i<count; i++)
  132. if (mMoveVec[i].checksum == Move::ChecksumMismatch)
  133. mControlMismatch = true;
  134. else
  135. mControlMismatch = false;
  136. if (count == mMoveVec.size())
  137. mMoveVec.clear();
  138. else
  139. while (count--)
  140. mMoveVec.pop_front();
  141. }
  142. }
  143. bool MoveList::areMovesPending()
  144. {
  145. return mConnection->isConnectionToServer() ?
  146. mMoveVec.size() - mLastClientMove + mFirstMoveIndex :
  147. mMoveVec.size();
  148. }
  149. bool MoveList::isBacklogged()
  150. {
  151. if ( !mConnection->isConnectionToServer() )
  152. return false;
  153. return mLastClientMove - mFirstMoveIndex == mMoveVec.size() &&
  154. mMoveVec.size() >= MaxMoveCount;
  155. }
  156. void MoveList::ackMoves(U32 count)
  157. {
  158. mLastMoveAck += count;
  159. if(mLastMoveAck > mLastClientMove)
  160. mLastClientMove = mLastMoveAck;
  161. while(mFirstMoveIndex < mLastMoveAck)
  162. {
  163. if (mMoveVec.size())
  164. {
  165. mMoveVec.pop_front();
  166. mFirstMoveIndex++;
  167. }
  168. else
  169. {
  170. AssertWarn(1, "Popping off too many moves!");
  171. mFirstMoveIndex = mLastMoveAck;
  172. }
  173. }
  174. }
  175. void MoveList::writeDemoStartBlock(ResizeBitStream *stream)
  176. {
  177. stream->write(mLastMoveAck);
  178. stream->write(mLastClientMove);
  179. stream->write(mFirstMoveIndex);
  180. stream->write(U32(mMoveVec.size()));
  181. for(U32 j = 0; j < mMoveVec.size(); j++)
  182. mMoveVec[j].pack(stream);
  183. }
  184. void MoveList::readDemoStartBlock(BitStream *stream)
  185. {
  186. stream->read(&mLastMoveAck);
  187. stream->read(&mLastClientMove);
  188. stream->read(&mFirstMoveIndex);
  189. U32 size;
  190. Move mv;
  191. stream->read(&size);
  192. mMoveVec.clear();
  193. while(size--)
  194. {
  195. mv.unpack(stream);
  196. pushMove(mv);
  197. }
  198. }