stdMoveList.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "platform/platform.h"
  23. #include "T3D/gameBase/std/stdMoveList.h"
  24. #include "T3D/gameBase/gameConnection.h"
  25. #include "core/stream/bitStream.h"
  26. #define MAX_MOVE_PACKET_SENDS 4
  27. StdMoveList::StdMoveList()
  28. {
  29. mMoveCredit = MaxMoveCount;
  30. }
  31. U32 StdMoveList::getMoves(Move** movePtr,U32* numMoves)
  32. {
  33. if (!mConnection->isConnectionToServer())
  34. {
  35. if (mMoveVec.size() > mMoveCredit)
  36. mMoveVec.setSize(mMoveCredit);
  37. }
  38. return Parent::getMoves(movePtr,numMoves);
  39. }
  40. void StdMoveList::clearMoves(U32 count)
  41. {
  42. if (!mConnection->isConnectionToServer())
  43. {
  44. count = mClamp(count,0,mMoveCredit);
  45. mMoveCredit -= count;
  46. }
  47. Parent::clearMoves(count);
  48. }
  49. void StdMoveList::advanceMove()
  50. {
  51. AssertFatal(!mConnection->isConnectionToServer(), "Cannot inc move credit on the client.");
  52. // Game tick increment
  53. mMoveCredit++;
  54. if (mMoveCredit > MaxMoveCount)
  55. mMoveCredit = MaxMoveCount;
  56. // Clear pending moves for the elapsed time if there
  57. // is no control object.
  58. if ( mConnection->getControlObject() == NULL )
  59. mMoveVec.clear();
  60. }
  61. void StdMoveList::clientWriteMovePacket(BitStream *bstream)
  62. {
  63. AssertFatal(mLastMoveAck == mFirstMoveIndex, "Invalid move index.");
  64. U32 count = mMoveVec.size();
  65. Move * move = mMoveVec.address();
  66. U32 start = mLastMoveAck;
  67. U32 offset;
  68. for(offset = 0; offset < count; offset++)
  69. if(move[offset].sendCount < MAX_MOVE_PACKET_SENDS)
  70. break;
  71. if(offset == count && count != 0)
  72. offset--;
  73. start += offset;
  74. count -= offset;
  75. if (count > MaxMoveCount)
  76. count = MaxMoveCount;
  77. bstream->writeInt(start,32);
  78. bstream->writeInt(count,MoveCountBits);
  79. Move * prevMove = NULL;
  80. for (S32 i = 0; i < count; i++)
  81. {
  82. move[offset + i].sendCount++;
  83. move[offset + i].pack(bstream,prevMove);
  84. bstream->writeInt(move[offset + i].checksum & (~(0xFFFFFFFF << Move::ChecksumBits)),Move::ChecksumBits);
  85. prevMove = &move[offset+i];
  86. }
  87. }
  88. void StdMoveList::serverReadMovePacket(BitStream *bstream)
  89. {
  90. // Server side packet read.
  91. U32 start = bstream->readInt(32);
  92. U32 count = bstream->readInt(MoveCountBits);
  93. Move * prevMove = NULL;
  94. Move prevMoveHolder;
  95. // Skip forward (must be starting up), or over the moves
  96. // we already have.
  97. S32 skip = mLastMoveAck - start;
  98. if (skip < 0)
  99. {
  100. mLastMoveAck = start;
  101. }
  102. else
  103. {
  104. if (skip > count)
  105. skip = count;
  106. for (S32 i = 0; i < skip; i++)
  107. {
  108. prevMoveHolder.unpack(bstream,prevMove);
  109. prevMoveHolder.checksum = bstream->readInt(Move::ChecksumBits);
  110. prevMove = &prevMoveHolder;
  111. S32 idx = mMoveVec.size()-skip+i;
  112. if (idx>=0)
  113. {
  114. #ifdef TORQUE_DEBUG_NET_MOVES
  115. if (mMoveVec[idx].checksum != prevMoveHolder.checksum)
  116. Con::printf("updated checksum on move %i from %i to %i",mMoveVec[idx].id,mMoveVec[idx].checksum,prevMoveHolder.checksum);
  117. #endif
  118. mMoveVec[idx].checksum = prevMoveHolder.checksum;
  119. }
  120. }
  121. start += skip;
  122. count = count - skip;
  123. }
  124. // Put the rest on the move list.
  125. S32 index = mMoveVec.size();
  126. mMoveVec.increment(count);
  127. while (index < mMoveVec.size())
  128. {
  129. mMoveVec[index].unpack(bstream,prevMove);
  130. mMoveVec[index].checksum = bstream->readInt(Move::ChecksumBits);
  131. prevMove = &mMoveVec[index];
  132. mMoveVec[index].id = start++;
  133. index ++;
  134. }
  135. mLastMoveAck += count;
  136. }
  137. void StdMoveList::serverWriteMovePacket(BitStream * bstream)
  138. {
  139. #ifdef TORQUE_DEBUG_NET_MOVES
  140. Con::printf("ack %i minus %i",mLastMoveAck,mMoveVec.size());
  141. #endif
  142. // acknowledge only those moves that have been ticked
  143. bstream->writeInt(mLastMoveAck - mMoveVec.size(),32);
  144. }
  145. void StdMoveList::clientReadMovePacket(BitStream * bstream)
  146. {
  147. #ifdef TORQUE_DEBUG_NET_MOVES
  148. Con::printf("pre move ack: %i", mLastMoveAck);
  149. #endif
  150. mLastMoveAck = bstream->readInt(32);
  151. #ifdef TORQUE_DEBUG_NET_MOVES
  152. Con::printf("post move ack %i, first move %i, last move %i", mLastMoveAck, mFirstMoveIndex, mLastClientMove);
  153. #endif
  154. if (mLastMoveAck < mFirstMoveIndex)
  155. mLastMoveAck = mFirstMoveIndex;
  156. if(mLastMoveAck > mLastClientMove)
  157. mLastClientMove = mLastMoveAck;
  158. while(mFirstMoveIndex < mLastMoveAck)
  159. {
  160. if (mMoveVec.size())
  161. {
  162. mMoveVec.pop_front();
  163. mFirstMoveIndex++;
  164. }
  165. else
  166. {
  167. AssertWarn(1, "Popping off too many moves!");
  168. mFirstMoveIndex = mLastMoveAck;
  169. }
  170. }
  171. }