TunnelTracker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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: TunnelTracker.cpp ///////////////////////////////////////////////////////////
  24. // The part of a Player's brain that holds the communal Passenger list of all tunnels.
  25. // Author: Graham Smallwood, March, 2002
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "Common/GameState.h"
  28. #include "Common/GlobalData.h"
  29. #include "Common/KindOf.h"
  30. #include "Common/TunnelTracker.h"
  31. #include "Common/Xfer.h"
  32. #include "GameClient/ControlBar.h"
  33. #include "GameClient/Drawable.h"
  34. #include "GameLogic/AI.h"
  35. #include "GameLogic/AIPathfind.h"
  36. #include "GameLogic/GameLogic.h"
  37. #include "GameLogic/Object.h"
  38. #include "GameLogic/PartitionManager.h"
  39. #include "GameLogic/Module/BodyModule.h"
  40. #include "GameLogic/Module/TunnelContain.h"
  41. // ------------------------------------------------------------------------
  42. TunnelTracker::TunnelTracker()
  43. {
  44. m_tunnelCount = 0;
  45. m_containListSize = 0;
  46. m_curNemesisID = INVALID_ID;
  47. m_nemesisTimestamp = 0;
  48. }
  49. // ------------------------------------------------------------------------
  50. TunnelTracker::~TunnelTracker()
  51. {
  52. m_tunnelIDs.clear();
  53. }
  54. // ------------------------------------------------------------------------
  55. void TunnelTracker::iterateContained( ContainIterateFunc func, void *userData, Bool reverse )
  56. {
  57. if (reverse)
  58. {
  59. // note that this has to be smart enough to handle items in the list being deleted
  60. // via the callback function.
  61. for(ContainedItemsList::reverse_iterator it = m_containList.rbegin(); it != m_containList.rend(); )
  62. {
  63. // save the obj...
  64. Object* obj = *it;
  65. // incr the iterator BEFORE calling the func (if the func removes the obj,
  66. // the iterator becomes invalid)
  67. ++it;
  68. // call it
  69. (*func)( obj, userData );
  70. }
  71. }
  72. else
  73. {
  74. // note that this has to be smart enough to handle items in the list being deleted
  75. // via the callback function.
  76. for(ContainedItemsList::iterator it = m_containList.begin(); it != m_containList.end(); )
  77. {
  78. // save the obj...
  79. Object* obj = *it;
  80. // incr the iterator BEFORE calling the func (if the func removes the obj,
  81. // the iterator becomes invalid)
  82. ++it;
  83. // call it
  84. (*func)( obj, userData );
  85. }
  86. }
  87. }
  88. // ------------------------------------------------------------------------
  89. Int TunnelTracker::getContainMax() const
  90. {
  91. return TheGlobalData->m_maxTunnelCapacity;
  92. }
  93. // ------------------------------------------------------------------------
  94. void TunnelTracker::updateNemesis(const Object *target)
  95. {
  96. if (getCurNemesis()==NULL) {
  97. if (target) {
  98. if (target->isKindOf(KINDOF_VEHICLE) || target->isKindOf(KINDOF_STRUCTURE) ||
  99. target->isKindOf(KINDOF_INFANTRY) || target->isKindOf(KINDOF_AIRCRAFT)) {
  100. m_curNemesisID = target->getID();
  101. m_nemesisTimestamp = TheGameLogic->getFrame();
  102. }
  103. }
  104. } else if (getCurNemesis()==target) {
  105. m_nemesisTimestamp = TheGameLogic->getFrame();
  106. }
  107. }
  108. // ------------------------------------------------------------------------
  109. Object *TunnelTracker::getCurNemesis(void)
  110. {
  111. if (m_curNemesisID == INVALID_ID) {
  112. return NULL;
  113. }
  114. if (m_nemesisTimestamp + 4*LOGICFRAMES_PER_SECOND < TheGameLogic->getFrame()) {
  115. m_curNemesisID = INVALID_ID;
  116. return NULL;
  117. }
  118. Object *target = TheGameLogic->findObjectByID(m_curNemesisID);
  119. if (target) {
  120. //If the enemy unit is stealthed and not detected, then we can't attack it!
  121. if( target->testStatus( OBJECT_STATUS_STEALTHED ) &&
  122. !target->testStatus( OBJECT_STATUS_DETECTED ) &&
  123. !target->testStatus( OBJECT_STATUS_DISGUISED ) )
  124. {
  125. target = NULL;
  126. }
  127. }
  128. if (target && target->isEffectivelyDead()) {
  129. target = NULL;
  130. }
  131. if (target == NULL) {
  132. m_curNemesisID = INVALID_ID;
  133. }
  134. return target;
  135. }
  136. // ------------------------------------------------------------------------
  137. Bool TunnelTracker::isValidContainerFor(const Object* obj, Bool checkCapacity) const
  138. {
  139. //October 11, 2002 -- Kris : Dustin wants ALL units to be able to use tunnels!
  140. // srj sez: um, except aircraft.
  141. if (obj && !obj->isKindOf(KINDOF_AIRCRAFT))
  142. {
  143. if (checkCapacity)
  144. {
  145. Int containMax = getContainMax();
  146. Int containCount = getContainCount();
  147. return ( containCount < containMax );
  148. }
  149. else
  150. {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. // ------------------------------------------------------------------------
  157. void TunnelTracker::addToContainList( Object *obj )
  158. {
  159. m_containList.push_back(obj);
  160. ++m_containListSize;
  161. }
  162. // ------------------------------------------------------------------------
  163. void TunnelTracker::removeFromContain( Object *obj, Bool exposeStealthUnits )
  164. {
  165. ContainedItemsList::iterator it = std::find(m_containList.begin(), m_containList.end(), obj);
  166. if (it != m_containList.end())
  167. {
  168. // note that this invalidates the iterator!
  169. m_containList.erase(it);
  170. --m_containListSize;
  171. }
  172. }
  173. // ------------------------------------------------------------------------
  174. Bool TunnelTracker::isInContainer( Object *obj )
  175. {
  176. return (std::find(m_containList.begin(), m_containList.end(), obj) != m_containList.end()) ;
  177. }
  178. // ------------------------------------------------------------------------
  179. void TunnelTracker::onTunnelCreated( const Object *newTunnel )
  180. {
  181. m_tunnelCount++;
  182. m_tunnelIDs.push_back( newTunnel->getID() );
  183. }
  184. // ------------------------------------------------------------------------
  185. void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
  186. {
  187. m_tunnelCount--;
  188. m_tunnelIDs.remove( deadTunnel->getID() );
  189. if( m_tunnelCount == 0 )
  190. {
  191. // Kill everyone in our contain list. Cave in!
  192. iterateContained( destroyObject, NULL, FALSE );
  193. m_containList.clear();
  194. m_containListSize = 0;
  195. }
  196. else
  197. {
  198. Object *validTunnel = TheGameLogic->findObjectByID( m_tunnelIDs.front() );
  199. // Otherwise, make sure nobody inside remembers the dead tunnel as the one they entered
  200. // (scripts need to use so there must be something valid here)
  201. for(ContainedItemsList::iterator it = m_containList.begin(); it != m_containList.end(); )
  202. {
  203. Object* obj = *it;
  204. ++it;
  205. if( obj->getContainedBy() == deadTunnel )
  206. obj->onContainedBy( validTunnel );
  207. }
  208. }
  209. }
  210. // ------------------------------------------------------------------------
  211. void TunnelTracker::destroyObject( Object *obj, void * )
  212. {
  213. // Now that tunnels consider ContainedBy to be "the tunnel you entered", I need to say goodbye
  214. // llike other contain types so they don't look us up on their deletion and crash
  215. obj->onRemovedFrom( obj->getContainedBy() );
  216. TheGameLogic->destroyObject( obj );
  217. }
  218. // ------------------------------------------------------------------------
  219. // heal all the objects within the tunnel system using the iterateContained function
  220. void TunnelTracker::healObjects(Real frames)
  221. {
  222. iterateContained(healObject, &frames, FALSE);
  223. }
  224. // ------------------------------------------------------------------------
  225. // heal one object within the tunnel network system
  226. void TunnelTracker::healObject( Object *obj, void *frames)
  227. {
  228. //get the number of frames to heal
  229. Real *framesForFullHeal = (Real*)frames;
  230. // setup the healing damageInfo structure with all but the amount
  231. DamageInfo healInfo;
  232. healInfo.in.m_damageType = DAMAGE_HEALING;
  233. healInfo.in.m_deathType = DEATH_NONE;
  234. //healInfo.in.m_sourceID = getObject()->getID();
  235. // get body module of the thing to heal
  236. BodyModuleInterface *body = obj->getBodyModule();
  237. // if we've been in here long enough ... set our health to max
  238. if( TheGameLogic->getFrame() - obj->getContainedByFrame() >= *framesForFullHeal )
  239. {
  240. // set the amount to max just to be sure we're at the top
  241. healInfo.in.m_amount = body->getMaxHealth();
  242. // set max health
  243. body->attemptHealing( &healInfo );
  244. } // end if
  245. else
  246. {
  247. //
  248. // given the *whole* time it would take to heal this object, lets pretend that the
  249. // object is at zero health ... and give it a sliver of health as if it were at 0 health
  250. // and would be fully healed at 'framesForFullHeal'
  251. //
  252. healInfo.in.m_amount = body->getMaxHealth() / *framesForFullHeal;
  253. // do the healing
  254. body->attemptHealing( &healInfo );
  255. } // end else
  256. }
  257. // ------------------------------------------------------------------------------------------------
  258. /** CRC */
  259. // ------------------------------------------------------------------------------------------------
  260. void TunnelTracker::crc( Xfer *xfer )
  261. {
  262. } // end crc
  263. // ------------------------------------------------------------------------------------------------
  264. /** Xfer method
  265. * Version Info:
  266. * 1: Initial version */
  267. // ------------------------------------------------------------------------------------------------
  268. void TunnelTracker::xfer( Xfer *xfer )
  269. {
  270. // version
  271. XferVersion currentVersion = 1;
  272. XferVersion version = currentVersion;
  273. xfer->xferVersion( &version, currentVersion );
  274. // tunnel object id list
  275. xfer->xferSTLObjectIDList( &m_tunnelIDs );
  276. // contain list count
  277. xfer->xferInt( &m_containListSize );
  278. // contain list data
  279. ObjectID objectID;
  280. if( xfer->getXferMode() == XFER_SAVE )
  281. {
  282. ContainedItemsList::const_iterator it;
  283. for( it = m_containList.begin(); it != m_containList.end(); ++it )
  284. {
  285. objectID = (*it)->getID();
  286. xfer->xferObjectID( &objectID );
  287. } // end for, it
  288. } // end if, save
  289. else
  290. {
  291. for( UnsignedShort i = 0; i < m_containListSize; ++i )
  292. {
  293. xfer->xferObjectID( &objectID );
  294. m_xferContainList.push_back( objectID );
  295. } // end for, i
  296. } // end else, load
  297. // tunnel count
  298. xfer->xferUnsignedInt( &m_tunnelCount );
  299. } // end xfer
  300. // ------------------------------------------------------------------------------------------------
  301. /** Load post process */
  302. // ------------------------------------------------------------------------------------------------
  303. void TunnelTracker::loadPostProcess( void )
  304. {
  305. // sanity, the contain list should be empty until we post process the id list
  306. if( m_containList.size() != 0 )
  307. {
  308. DEBUG_CRASH(( "TunnelTracker::loadPostProcess - m_containList should be empty but is not\n" ));
  309. throw SC_INVALID_DATA;
  310. } // end if
  311. // translate each object ids on the xferContainList into real object pointers in the contain list
  312. Object *obj;
  313. std::list< ObjectID >::const_iterator it;
  314. for( it = m_xferContainList.begin(); it != m_xferContainList.end(); ++it )
  315. {
  316. obj = TheGameLogic->findObjectByID( *it );
  317. if( obj == NULL )
  318. {
  319. DEBUG_CRASH(( "TunnelTracker::loadPostProcess - Unable to find object ID '%d'\n", *it ));
  320. throw SC_INVALID_DATA;
  321. } // end if
  322. // push on the back of the contain list
  323. m_containList.push_back( obj );
  324. // Crap. This is in OpenContain as a fix, but not here.
  325. {
  326. // remove object from its group (if any)
  327. obj->leaveGroup();
  328. // remove rider from partition manager
  329. ThePartitionManager->unRegisterObject( obj );
  330. // hide the drawable associated with rider
  331. if( obj->getDrawable() )
  332. obj->getDrawable()->setDrawableHidden( true );
  333. // remove object from pathfind map
  334. if( TheAI )
  335. TheAI->pathfinder()->removeObjectFromPathfindMap( obj );
  336. }
  337. } // end for, it
  338. // we're done with the xfer contain list now
  339. m_xferContainList.clear();
  340. } // end loadPostProcess