moveList.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. mControlMismatch = false;
  28. reset();
  29. }
  30. void MoveList::reset()
  31. {
  32. mLastMoveAck = 0;
  33. mLastClientMove = 0;
  34. mFirstMoveIndex = 0;
  35. mMoveVec.clear();
  36. }
  37. bool MoveList::getNextMove(Move &curMove)
  38. {
  39. if ( mMoveVec.size() > MaxMoveQueueSize )
  40. return false;
  41. F32 pitchAdd = MoveManager::mPitchUpSpeed - MoveManager::mPitchDownSpeed;
  42. F32 yawAdd = MoveManager::mYawLeftSpeed - MoveManager::mYawRightSpeed;
  43. F32 rollAdd = MoveManager::mRollRightSpeed - MoveManager::mRollLeftSpeed;
  44. curMove.pitch = MoveManager::mPitch + pitchAdd;
  45. curMove.yaw = MoveManager::mYaw + yawAdd;
  46. curMove.roll = MoveManager::mRoll + rollAdd;
  47. MoveManager::mPitch = 0;
  48. MoveManager::mYaw = 0;
  49. MoveManager::mRoll = 0;
  50. curMove.x = MoveManager::mRightAction - MoveManager::mLeftAction + MoveManager::mXAxis_L;
  51. curMove.y = MoveManager::mForwardAction - MoveManager::mBackwardAction + MoveManager::mYAxis_L;
  52. curMove.z = MoveManager::mUpAction - MoveManager::mDownAction;
  53. curMove.freeLook = MoveManager::mFreeLook;
  54. curMove.deviceIsKeyboardMouse = MoveManager::mDeviceIsKeyboardMouse;
  55. for(U32 i = 0; i < MaxTriggerKeys; i++)
  56. {
  57. curMove.trigger[i] = false;
  58. if(MoveManager::mTriggerCount[i] & 1)
  59. curMove.trigger[i] = true;
  60. else if(!(MoveManager::mPrevTriggerCount[i] & 1) && MoveManager::mPrevTriggerCount[i] != MoveManager::mTriggerCount[i])
  61. curMove.trigger[i] = true;
  62. MoveManager::mPrevTriggerCount[i] = MoveManager::mTriggerCount[i];
  63. }
  64. if (mConnection->getControlObject())
  65. mConnection->getControlObject()->preprocessMove(&curMove);
  66. curMove.clamp(); // clamp for net traffic
  67. return true;
  68. }
  69. void MoveList::pushMove(const Move &mv)
  70. {
  71. U32 id = mFirstMoveIndex + mMoveVec.size();
  72. U32 sz = mMoveVec.size();
  73. mMoveVec.push_back(mv);
  74. mMoveVec[sz].id = id;
  75. mMoveVec[sz].sendCount = 0;
  76. }
  77. U32 MoveList::getMoves(Move** movePtr,U32* numMoves)
  78. {
  79. if (mConnection->isConnectionToServer())
  80. {
  81. // give back moves starting at the last client move...
  82. AssertFatal(mLastClientMove >= mFirstMoveIndex, "Bad move request");
  83. AssertFatal(mLastClientMove - mFirstMoveIndex <= mMoveVec.size(), "Desynched first and last move.");
  84. *numMoves = mMoveVec.size() - mLastClientMove + mFirstMoveIndex;
  85. *movePtr = mMoveVec.address() + mLastClientMove - mFirstMoveIndex;
  86. }
  87. else
  88. {
  89. // return the full list
  90. *numMoves = mMoveVec.size();
  91. *movePtr = mMoveVec.begin();
  92. }
  93. return *numMoves;
  94. }
  95. void MoveList::collectMove()
  96. {
  97. Move mv;
  98. if (mConnection)
  99. {
  100. if(!mConnection->isPlayingBack() && getNextMove(mv))
  101. {
  102. mv.checksum=Move::ChecksumMismatch;
  103. pushMove(mv);
  104. mConnection->recordBlock(GameConnection::BlockTypeMove, sizeof(Move), &mv);
  105. }
  106. }
  107. else
  108. {
  109. if(getNextMove(mv))
  110. {
  111. mv.checksum=Move::ChecksumMismatch;
  112. pushMove(mv);
  113. }
  114. }
  115. }
  116. void MoveList::clearMoves(U32 count)
  117. {
  118. if (mConnection->isConnectionToServer())
  119. {
  120. mLastClientMove += count;
  121. AssertFatal(mLastClientMove >= mFirstMoveIndex, "Bad move request");
  122. AssertFatal(mLastClientMove - mFirstMoveIndex <= mMoveVec.size(), "Desynched first and last move.");
  123. if (!mConnection)
  124. // drop right away if no connection
  125. ackMoves(count);
  126. }
  127. else
  128. {
  129. AssertFatal(count <= mMoveVec.size(),"GameConnection: Clearing too many moves");
  130. for (S32 i=0; i<count; i++)
  131. if (mMoveVec[i].checksum == Move::ChecksumMismatch)
  132. mControlMismatch = true;
  133. else
  134. mControlMismatch = false;
  135. if (count == mMoveVec.size())
  136. mMoveVec.clear();
  137. else
  138. while (count--)
  139. mMoveVec.pop_front();
  140. }
  141. }
  142. bool MoveList::areMovesPending()
  143. {
  144. return mConnection->isConnectionToServer() ?
  145. mMoveVec.size() - mLastClientMove + mFirstMoveIndex :
  146. mMoveVec.size();
  147. }
  148. bool MoveList::isBacklogged()
  149. {
  150. if ( !mConnection->isConnectionToServer() )
  151. return false;
  152. return mLastClientMove - mFirstMoveIndex == mMoveVec.size() &&
  153. mMoveVec.size() >= MaxMoveCount;
  154. }
  155. void MoveList::ackMoves(U32 count)
  156. {
  157. mLastMoveAck += count;
  158. if(mLastMoveAck > mLastClientMove)
  159. mLastClientMove = mLastMoveAck;
  160. while(mFirstMoveIndex < mLastMoveAck)
  161. {
  162. if (mMoveVec.size())
  163. {
  164. mMoveVec.pop_front();
  165. mFirstMoveIndex++;
  166. }
  167. else
  168. {
  169. AssertWarn(1, "Popping off too many moves!");
  170. mFirstMoveIndex = mLastMoveAck;
  171. }
  172. }
  173. }
  174. void MoveList::writeDemoStartBlock(ResizeBitStream *stream)
  175. {
  176. stream->write(mLastMoveAck);
  177. stream->write(mLastClientMove);
  178. stream->write(mFirstMoveIndex);
  179. stream->write(U32(mMoveVec.size()));
  180. for(U32 j = 0; j < mMoveVec.size(); j++)
  181. mMoveVec[j].pack(stream);
  182. }
  183. void MoveList::readDemoStartBlock(BitStream *stream)
  184. {
  185. stream->read(&mLastMoveAck);
  186. stream->read(&mLastClientMove);
  187. stream->read(&mFirstMoveIndex);
  188. U32 size;
  189. Move mv;
  190. stream->read(&size);
  191. mMoveVec.clear();
  192. while(size--)
  193. {
  194. mv.unpack(stream);
  195. pushMove(mv);
  196. }
  197. }