Squad.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // FILE: Squad.cpp
  24. /*---------------------------------------------------------------------------*/
  25. /* EA Pacific */
  26. /* Confidential Information */
  27. /* Copyright (C) 2001 - All Rights Reserved */
  28. /* DO NOT DISTRIBUTE */
  29. /*---------------------------------------------------------------------------*/
  30. /* Project: RTS3 */
  31. /* File name: Squad.cpp */
  32. /* Created: John K. McDonald, Jr., 4/19/2002 */
  33. /* Desc: // @todo */
  34. /* Revision History: */
  35. /* 4/19/2002 : Initial creation */
  36. /*---------------------------------------------------------------------------*/
  37. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  38. #include "GameLogic/Squad.h"
  39. #include "Common/GameState.h"
  40. #include "Common/Team.h"
  41. #include "Common/Xfer.h"
  42. #include "GameLogic/AI.h"
  43. #include "GameLogic/GameLogic.h"
  44. #include "GameLogic/Object.h"
  45. #ifdef _INTERNAL
  46. // for occasional debugging...
  47. //#pragma optimize("", off)
  48. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  49. #endif
  50. // addObject //////////////////////////////////////////////////////////////////////////////////////
  51. void Squad::addObject(Object *objectToAdd)
  52. {
  53. if (objectToAdd) {
  54. m_objectIDs.push_back(objectToAdd->getID());
  55. }
  56. }
  57. // addObjectID ////////////////////////////////////////////////////////////////////////////////////
  58. void Squad::addObjectID(ObjectID objectID) {
  59. m_objectIDs.push_back(objectID);
  60. }
  61. // removeObject ///////////////////////////////////////////////////////////////////////////////////
  62. void Squad::removeObject(Object *objectToRemove)
  63. {
  64. if (objectToRemove) {
  65. ObjectID objID;
  66. objID = objectToRemove->getID();
  67. VecObjectIDIt it = std::find(m_objectIDs.begin(), m_objectIDs.end(), objID);
  68. if (it != m_objectIDs.end()) {
  69. m_objectIDs.erase(it);
  70. }
  71. }
  72. }
  73. // clearSquad /////////////////////////////////////////////////////////////////////////////////////
  74. void Squad::clearSquad() {
  75. m_objectIDs.clear();
  76. m_objectsCached.clear();
  77. }
  78. // getAllObjects //////////////////////////////////////////////////////////////////////////////////
  79. const VecObjectPtr& Squad::getAllObjects(void) // Not a const function cause we clear away dead object here too
  80. {
  81. // prunes all NULL objects
  82. m_objectsCached.clear();
  83. for (VecObjectIDIt it = m_objectIDs.begin(); it != m_objectIDs.end(); ) {
  84. Object *obj = TheGameLogic->findObjectByID(*it);
  85. if (obj) {
  86. m_objectsCached.push_back(obj);
  87. ++it;
  88. } else {
  89. it = m_objectIDs.erase(it);
  90. }
  91. }
  92. return m_objectsCached;
  93. }
  94. // getLiveObjects /////////////////////////////////////////////////////////////////////////////////
  95. const VecObjectPtr& Squad::getLiveObjects(void)
  96. {
  97. // first get all the objects.
  98. // cheat, since we are a member function, and just use m_objectsCached
  99. getAllObjects();
  100. for (VecObjectPtrIt it = m_objectsCached.begin(); it != m_objectsCached.end(); ) {
  101. if (!(*it)->isSelectable()) {
  102. it = m_objectsCached.erase(it);
  103. } else {
  104. ++it;
  105. }
  106. }
  107. return m_objectsCached;
  108. }
  109. // getSizeOfGroup /////////////////////////////////////////////////////////////////////////////////
  110. Int Squad::getSizeOfGroup(void) const
  111. {
  112. return m_objectIDs.size();
  113. }
  114. // isOnSquad //////////////////////////////////////////////////////////////////////////////////////
  115. Bool Squad::isOnSquad(const Object *objToTest) const
  116. {
  117. // @todo need a faster way to do this. Perhaps a more efficient data structure?
  118. ObjectID objID = objToTest->getID();
  119. for (VecObjectID::const_iterator cit = m_objectIDs.begin(); cit != m_objectIDs.end(); ++cit) {
  120. if (objID == (*cit)) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * There should never be a TeamFromSqaud as Teams are entirely a construct to work with the AI.
  128. * Since things can only be on one Team at a time, creating a Team from an arbitrary Squad will
  129. * cause weird, difficult to reproduce bugs. Please don't do it.
  130. */
  131. // squadFromTeam //////////////////////////////////////////////////////////////////////////////////
  132. void Squad::squadFromTeam(const Team* fromTeam, Bool clearSquadFirst)
  133. {
  134. if (!fromTeam) {
  135. return;
  136. }
  137. if (clearSquadFirst) {
  138. m_objectIDs.clear();
  139. }
  140. for (DLINK_ITERATOR<Object> iter = fromTeam->iterate_TeamMemberList(); !iter.done(); iter.advance()) {
  141. Object *obj = iter.cur();
  142. m_objectIDs.push_back(obj->getID());
  143. }
  144. }
  145. // squadFromAIGroup ///////////////////////////////////////////////////////////////////////////////
  146. void Squad::squadFromAIGroup(const AIGroup* fromAIGroup, Bool clearSquadFirst)
  147. {
  148. if (!fromAIGroup) {
  149. return;
  150. }
  151. if (clearSquadFirst) {
  152. m_objectIDs.clear();
  153. }
  154. m_objectIDs = fromAIGroup->getAllIDs();
  155. }
  156. // aiGroupFromSquad ///////////////////////////////////////////////////////////////////////////////
  157. void Squad::aiGroupFromSquad(AIGroup* aiGroupToFill)
  158. {
  159. if (!aiGroupToFill) {
  160. return;
  161. }
  162. // cheat, since we are a member function, and just use m_objectsCached
  163. getLiveObjects();
  164. for (VecObjectPtr::iterator it = m_objectsCached.begin(); it != m_objectsCached.end(); ++it) {
  165. aiGroupToFill->add((*it));
  166. }
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. /** CRC */
  170. // ------------------------------------------------------------------------------------------------
  171. void Squad::crc( Xfer *xfer )
  172. {
  173. } // end crc
  174. // ------------------------------------------------------------------------------------------------
  175. /** Xfer method
  176. * Version Info:
  177. * 1: Initial version */
  178. // ------------------------------------------------------------------------------------------------
  179. void Squad::xfer( Xfer *xfer )
  180. {
  181. // version
  182. XferVersion currentVersion = 1;
  183. XferVersion version = currentVersion;
  184. xfer->xferVersion( &version, currentVersion );
  185. // length of object ID list
  186. UnsignedShort objectCount = m_objectIDs.size();
  187. xfer->xferUnsignedShort( &objectCount );
  188. // object id elements
  189. ObjectID objectID;
  190. if( xfer->getXferMode() == XFER_SAVE )
  191. {
  192. // save each object id
  193. VecObjectIDIt it;
  194. for( it = m_objectIDs.begin(); it != m_objectIDs.end(); ++it )
  195. {
  196. // save object ID
  197. objectID = *it;
  198. xfer->xferObjectID( &objectID );
  199. } // end for, it
  200. } // end if, save
  201. else
  202. {
  203. // the cached objects list should be empty
  204. if( m_objectsCached.size() != 0 )
  205. {
  206. DEBUG_CRASH(( "Squad::xfer - m_objectsCached should be emtpy, but is not\n" ));
  207. throw SC_INVALID_DATA;
  208. } // end of
  209. // read all items
  210. for( UnsignedShort i = 0; i < objectCount; ++i )
  211. {
  212. // read id
  213. xfer->xferObjectID( &objectID );
  214. // put on list
  215. m_objectIDs.push_back( objectID );
  216. } // end for, i
  217. } // end else, load
  218. } // end xfer
  219. // ------------------------------------------------------------------------------------------------
  220. /** Load post process */
  221. // ------------------------------------------------------------------------------------------------
  222. void Squad::loadPostProcess( void )
  223. {
  224. } // end loadPostProcess