FrameDataManager.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  24. #include "GameNetwork/FrameDataManager.h"
  25. #include "GameNetwork/NetworkUtil.h"
  26. /**
  27. * Constructor. isLocal tells it whether its the frame data manager for the local player or not.
  28. */
  29. FrameDataManager::FrameDataManager(Bool isLocal) {
  30. m_isLocal = isLocal;
  31. m_frameData = NEW FrameData[FRAME_DATA_LENGTH];
  32. m_isQuitting = FALSE;
  33. m_quitFrame = 0;
  34. }
  35. /**
  36. * destructor.
  37. */
  38. FrameDataManager::~FrameDataManager() {
  39. for (Int i = 0; i < FRAME_DATA_LENGTH; ++i) {
  40. m_frameData[i].reset();
  41. }
  42. if (m_frameData)
  43. {
  44. delete[] m_frameData;
  45. m_frameData = NULL;
  46. }
  47. }
  48. /**
  49. * Initialize all of the frame datas associated with this manager.
  50. */
  51. void FrameDataManager::init() {
  52. for (Int i = 0; i < FRAME_DATA_LENGTH; ++i) {
  53. m_frameData[i].init();
  54. if (m_isLocal) {
  55. // If this is the local connection, adjust the frame command count.
  56. m_frameData[i].setFrameCommandCount(m_frameData[i].getCommandCount());
  57. }
  58. }
  59. m_isQuitting = FALSE;
  60. m_quitFrame = 0;
  61. }
  62. /**
  63. * Reset the state of all the frames.
  64. */
  65. void FrameDataManager::reset() {
  66. init();
  67. }
  68. /**
  69. * update function. Does nothing at this time.
  70. */
  71. void FrameDataManager::update() {
  72. }
  73. /**
  74. * Add a network command to the appropriate frame.
  75. */
  76. void FrameDataManager::addNetCommandMsg(NetCommandMsg *msg) {
  77. UnsignedInt frame = msg->getExecutionFrame();
  78. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  79. DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("FrameDataManager::addNetCommandMsg - about to add a command of type %s for frame %d, frame index %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), frame, frameindex));
  80. m_frameData[frameindex].addCommand(msg);
  81. if (m_isLocal) {
  82. // If this is the local connection, adjust the frame command count.
  83. m_frameData[frameindex].setFrameCommandCount(m_frameData[frameindex].getCommandCount());
  84. }
  85. }
  86. /**
  87. * Returns true if all the commands for the given frame are ready.
  88. */
  89. FrameDataReturnType FrameDataManager::allCommandsReady(UnsignedInt frame, Bool debugSpewage) {
  90. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  91. //DEBUG_ASSERTCRASH(m_frameData[frameindex].getFrame() == frame || frame == 256, ("Looking at old commands!"));
  92. return m_frameData[frameindex].allCommandsReady(debugSpewage);
  93. }
  94. /**
  95. * Returns the command list for the given frame.
  96. */
  97. NetCommandList * FrameDataManager::getFrameCommandList(UnsignedInt frame) {
  98. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  99. return m_frameData[frameindex].getCommandList();
  100. }
  101. /**
  102. * Reset the contents of the given frame.
  103. */
  104. void FrameDataManager::resetFrame(UnsignedInt frame, Bool isAdvancing) {
  105. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  106. m_frameData[frameindex].reset();
  107. if (isAdvancing) {
  108. m_frameData[frameindex].setFrame(frame + MAX_FRAMES_AHEAD);
  109. }
  110. if (m_isLocal) {
  111. m_frameData[frameindex].setFrameCommandCount(m_frameData[frameindex].getCommandCount());
  112. }
  113. DEBUG_ASSERTCRASH(m_frameData[frameindex].getCommandCount() == 0, ("we just reset the frame data and the command count is not zero, huh?"));
  114. }
  115. /**
  116. * Returns the command count for the given frame.
  117. */
  118. UnsignedInt FrameDataManager::getCommandCount(UnsignedInt frame) {
  119. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  120. return m_frameData[frameindex].getCommandCount();
  121. }
  122. /**
  123. * Set the frame command count for the given frame.
  124. */
  125. void FrameDataManager::setFrameCommandCount(UnsignedInt frame, UnsignedInt commandCount) {
  126. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  127. m_frameData[frameindex].setFrameCommandCount(commandCount);
  128. }
  129. /**
  130. *
  131. */
  132. UnsignedInt FrameDataManager::getFrameCommandCount(UnsignedInt frame) {
  133. UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;
  134. return m_frameData[frameindex].getFrameCommandCount();
  135. }
  136. /**
  137. * Set both the command count and the frame command count to 0 for the given frames.
  138. */
  139. void FrameDataManager::zeroFrames(UnsignedInt startingFrame, UnsignedInt numFrames) {
  140. UnsignedInt frameIndex = startingFrame % FRAME_DATA_LENGTH;
  141. for (UnsignedInt i = 0; i < numFrames; ++i) {
  142. //DEBUG_LOG(("Calling zeroFrame for frame index %d\n", frameIndex));
  143. m_frameData[frameIndex].zeroFrame();
  144. ++frameIndex;
  145. frameIndex = frameIndex % FRAME_DATA_LENGTH;
  146. }
  147. }
  148. /**
  149. * Destroy all the commands held by this object.
  150. */
  151. void FrameDataManager::destroyGameMessages() {
  152. for (Int i = 0; i < FRAME_DATA_LENGTH; ++i) {
  153. m_frameData[i].destroyGameMessages();
  154. }
  155. }
  156. /**
  157. * Sets the quit frame, also sets the isQuitting flag.
  158. */
  159. void FrameDataManager::setQuitFrame(UnsignedInt frame) {
  160. m_isQuitting = TRUE;
  161. m_quitFrame = frame;
  162. }
  163. /**
  164. * returns the quit frame.
  165. */
  166. UnsignedInt FrameDataManager::getQuitFrame() {
  167. return m_quitFrame;
  168. }
  169. /**
  170. * returns true if this frame data manager is quitting.
  171. */
  172. Bool FrameDataManager::getIsQuitting() {
  173. return m_isQuitting;
  174. }