FrameData.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/FrameData.h"
  25. #include "GameNetwork/NetworkUtil.h"
  26. /**
  27. * Constructor
  28. */
  29. FrameData::FrameData()
  30. {
  31. m_frame = 0;
  32. m_commandList = NULL;
  33. m_commandCount = 0;
  34. m_frameCommandCount = -1;
  35. //Added By Sadullah Nader
  36. //Initializations missing and needed
  37. m_lastFailedCC = 0;
  38. m_lastFailedFrameCC = 0;
  39. //
  40. }
  41. /**
  42. * Destructor
  43. */
  44. FrameData::~FrameData()
  45. {
  46. if (m_commandList != NULL) {
  47. m_commandList->deleteInstance();
  48. m_commandList = NULL;
  49. }
  50. }
  51. /**
  52. * Initialize this thing.
  53. */
  54. void FrameData::init()
  55. {
  56. m_frame = 0;
  57. if (m_commandList == NULL) {
  58. m_commandList = newInstance(NetCommandList);
  59. m_commandList->init();
  60. }
  61. m_commandList->reset();
  62. m_frameCommandCount = -1;
  63. //DEBUG_LOG(("FrameData::init\n"));
  64. m_commandCount = 0;
  65. m_lastFailedCC = -2;
  66. m_lastFailedFrameCC = -2;
  67. }
  68. /**
  69. * Reset this thing.
  70. */
  71. void FrameData::reset() {
  72. init();
  73. }
  74. /**
  75. * update the thing, doesn't do anything at the moment.
  76. */
  77. void FrameData::update() {
  78. }
  79. /**
  80. * return the frame number this frame data is associated with.
  81. */
  82. UnsignedInt FrameData::getFrame() {
  83. return m_frame;
  84. }
  85. /**
  86. * Assign the frame number this frame data is associated with.
  87. */
  88. void FrameData::setFrame(UnsignedInt frame) {
  89. m_frame = frame;
  90. }
  91. /**
  92. * Returns true if all the frame command count is equal to the number of commands that have been received.
  93. */
  94. FrameDataReturnType FrameData::allCommandsReady(Bool debugSpewage) {
  95. if (m_frameCommandCount == m_commandCount) {
  96. m_lastFailedFrameCC = -2;
  97. m_lastFailedCC = -2;
  98. return FRAMEDATA_READY;
  99. }
  100. if (debugSpewage) {
  101. if ((m_lastFailedFrameCC != m_frameCommandCount) || (m_lastFailedCC != m_commandCount)) {
  102. DEBUG_LOG(("FrameData::allCommandsReady - failed, frame command count = %d, command count = %d\n", m_frameCommandCount, m_commandCount));
  103. m_lastFailedFrameCC = m_frameCommandCount;
  104. m_lastFailedCC = m_commandCount;
  105. }
  106. }
  107. if (m_commandCount > m_frameCommandCount) {
  108. DEBUG_LOG(("FrameData::allCommandsReady - There are more commands than there should be (%d, should be %d). Commands in command list are...\n", m_commandCount, m_frameCommandCount));
  109. NetCommandRef *ref = m_commandList->getFirstMessage();
  110. while (ref != NULL) {
  111. DEBUG_LOG(("%s, frame = %d, id = %d\n", GetAsciiNetCommandType(ref->getCommand()->getNetCommandType()).str(), ref->getCommand()->getExecutionFrame(), ref->getCommand()->getID()));
  112. ref = ref->getNext();
  113. }
  114. DEBUG_LOG(("FrameData::allCommandsReady - End of command list.\n"));
  115. DEBUG_LOG(("FrameData::allCommandsReady - about to clear the command list\n"));
  116. reset();
  117. DEBUG_LOG(("FrameData::allCommandsReady - command list cleared. command list length = %d, command count = %d, frame command count = %d\n", m_commandList->length(), m_commandCount, m_frameCommandCount));
  118. return FRAMEDATA_RESEND;
  119. }
  120. return FRAMEDATA_NOTREADY;
  121. }
  122. /**
  123. * Set the command count for this frame
  124. */
  125. void FrameData::setFrameCommandCount(UnsignedInt frameCommandCount) {
  126. //DEBUG_LOG(("setFrameCommandCount to %d for frame %d\n", frameCommandCount, m_frame));
  127. m_frameCommandCount = frameCommandCount;
  128. }
  129. /**
  130. * Get the command count for this frame.
  131. */
  132. UnsignedInt FrameData::getFrameCommandCount() {
  133. return m_frameCommandCount;
  134. }
  135. /**
  136. * return the number of commands received so far.
  137. */
  138. UnsignedInt FrameData::getCommandCount() {
  139. return m_commandCount;
  140. }
  141. /**
  142. * Add a command to this frame
  143. */
  144. void FrameData::addCommand(NetCommandMsg *msg) {
  145. // need to add the message in order of command ID
  146. if (m_commandList == NULL) {
  147. init();
  148. }
  149. // We don't need to worry about setting the relay since its not getting sent anywhere.
  150. if (m_commandList->findMessage(msg) != NULL) {
  151. // We don't want to add the same command twice.
  152. return;
  153. }
  154. m_commandList->addMessage(msg);
  155. ++m_commandCount;
  156. //DEBUG_LOG(("added command %d, type = %d(%s), command count = %d, frame command count = %d\n", msg->getID(), msg->getNetCommandType(), GetAsciiNetCommandType(msg->getNetCommandType()).str(), m_commandCount, m_frameCommandCount));
  157. }
  158. /**
  159. * Return the list of commands for this frame
  160. */
  161. NetCommandList * FrameData::getCommandList() {
  162. return m_commandList;
  163. }
  164. /**
  165. * Set both the command count and the frame command count to 0.
  166. */
  167. void FrameData::zeroFrame() {
  168. m_commandCount = 0;
  169. m_frameCommandCount = 0;
  170. }
  171. /**
  172. * destroy all the commands in this frame.
  173. */
  174. void FrameData::destroyGameMessages() {
  175. if (m_commandList == NULL) {
  176. return;
  177. }
  178. m_commandList->reset();
  179. m_commandCount = 0;
  180. }