TunnelTracker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // 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. UnsignedInt status = target->getStatusBits();
  122. if( (status & OBJECT_STATUS_STEALTHED) && !(status & OBJECT_STATUS_DETECTED) ) {
  123. target = NULL;
  124. }
  125. }
  126. if (target && target->isEffectivelyDead()) {
  127. target = NULL;
  128. }
  129. if (target == NULL) {
  130. m_curNemesisID = INVALID_ID;
  131. }
  132. return target;
  133. }
  134. // ------------------------------------------------------------------------
  135. Bool TunnelTracker::isValidContainerFor(const Object* obj, Bool checkCapacity) const
  136. {
  137. //October 11, 2002 -- Kris : Dustin wants ALL units to be able to use tunnels!
  138. // srj sez: um, except aircraft.
  139. if (obj && !obj->isKindOf(KINDOF_AIRCRAFT))
  140. {
  141. if (checkCapacity)
  142. {
  143. Int containMax = getContainMax();
  144. Int containCount = getContainCount();
  145. return ( containCount < containMax );
  146. }
  147. else
  148. {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. // ------------------------------------------------------------------------
  155. void TunnelTracker::addToContainList( Object *obj )
  156. {
  157. m_containList.push_back(obj);
  158. ++m_containListSize;
  159. }
  160. // ------------------------------------------------------------------------
  161. void TunnelTracker::removeFromContain( Object *obj, Bool exposeStealthUnits )
  162. {
  163. ContainedItemsList::iterator it = std::find(m_containList.begin(), m_containList.end(), obj);
  164. if (it != m_containList.end())
  165. {
  166. // note that this invalidates the iterator!
  167. m_containList.erase(it);
  168. --m_containListSize;
  169. }
  170. }
  171. // ------------------------------------------------------------------------
  172. Bool TunnelTracker::isInContainer( Object *obj )
  173. {
  174. return (std::find(m_containList.begin(), m_containList.end(), obj) != m_containList.end()) ;
  175. }
  176. // ------------------------------------------------------------------------
  177. void TunnelTracker::onTunnelCreated( const Object *newTunnel )
  178. {
  179. m_tunnelCount++;
  180. m_tunnelIDs.push_back( newTunnel->getID() );
  181. }
  182. // ------------------------------------------------------------------------
  183. void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
  184. {
  185. m_tunnelCount--;
  186. m_tunnelIDs.remove( deadTunnel->getID() );
  187. if( m_tunnelCount == 0 )
  188. {
  189. // Kill everyone in our contain list. Cave in!
  190. iterateContained( destroyObject, NULL, FALSE );
  191. m_containList.clear();
  192. m_containListSize = 0;
  193. }
  194. else
  195. {
  196. Object *validTunnel = TheGameLogic->findObjectByID( m_tunnelIDs.front() );
  197. // Otherwise, make sure nobody inside remembers the dead tunnel as the one they entered
  198. // (scripts need to use so there must be something valid here)
  199. for(ContainedItemsList::iterator it = m_containList.begin(); it != m_containList.end(); )
  200. {
  201. Object* obj = *it;
  202. ++it;
  203. if( obj->getContainedBy() == deadTunnel )
  204. obj->onContainedBy( validTunnel );
  205. }
  206. }
  207. }
  208. // ------------------------------------------------------------------------
  209. void TunnelTracker::destroyObject( Object *obj, void * )
  210. {
  211. // Now that tunnels consider ContainedBy to be "the tunnel you entered", I need to say goodbye
  212. // llike other contain types so they don't look us up on their deletion and crash
  213. obj->onRemovedFrom( obj->getContainedBy() );
  214. TheGameLogic->destroyObject( obj );
  215. }
  216. // ------------------------------------------------------------------------
  217. // heal all the objects within the tunnel system using the iterateContained function
  218. void TunnelTracker::healObjects(Real frames)
  219. {
  220. iterateContained(healObject, &frames, FALSE);
  221. }
  222. // ------------------------------------------------------------------------
  223. // heal one object within the tunnel network system
  224. void TunnelTracker::healObject( Object *obj, void *frames)
  225. {
  226. //get the number of frames to heal
  227. Real *framesForFullHeal = (Real*)frames;
  228. // setup the healing damageInfo structure with all but the amount
  229. DamageInfo healInfo;
  230. healInfo.in.m_damageType = DAMAGE_HEALING;
  231. healInfo.in.m_deathType = DEATH_NONE;
  232. //healInfo.in.m_sourceID = getObject()->getID();
  233. // get body module of the thing to heal
  234. BodyModuleInterface *body = obj->getBodyModule();
  235. // if we've been in here long enough ... set our health to max
  236. if( TheGameLogic->getFrame() - obj->getContainedByFrame() >= *framesForFullHeal )
  237. {
  238. // set the amount to max just to be sure we're at the top
  239. healInfo.in.m_amount = body->getMaxHealth();
  240. // set max health
  241. body->attemptHealing( &healInfo );
  242. } // end if
  243. else
  244. {
  245. //
  246. // given the *whole* time it would take to heal this object, lets pretend that the
  247. // object is at zero health ... and give it a sliver of health as if it were at 0 health
  248. // and would be fully healed at 'framesForFullHeal'
  249. //
  250. healInfo.in.m_amount = body->getMaxHealth() / *framesForFullHeal;
  251. // do the healing
  252. body->attemptHealing( &healInfo );
  253. } // end else
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. /** CRC */
  257. // ------------------------------------------------------------------------------------------------
  258. void TunnelTracker::crc( Xfer *xfer )
  259. {
  260. } // end crc
  261. // ------------------------------------------------------------------------------------------------
  262. /** Xfer method
  263. * Version Info:
  264. * 1: Initial version */
  265. // ------------------------------------------------------------------------------------------------
  266. void TunnelTracker::xfer( Xfer *xfer )
  267. {
  268. // version
  269. XferVersion currentVersion = 1;
  270. XferVersion version = currentVersion;
  271. xfer->xferVersion( &version, currentVersion );
  272. // tunnel object id list
  273. xfer->xferSTLObjectIDList( &m_tunnelIDs );
  274. // contain list count
  275. xfer->xferInt( &m_containListSize );
  276. // contain list data
  277. ObjectID objectID;
  278. if( xfer->getXferMode() == XFER_SAVE )
  279. {
  280. ContainedItemsList::const_iterator it;
  281. for( it = m_containList.begin(); it != m_containList.end(); ++it )
  282. {
  283. objectID = (*it)->getID();
  284. xfer->xferObjectID( &objectID );
  285. } // end for, it
  286. } // end if, save
  287. else
  288. {
  289. for( UnsignedShort i = 0; i < m_containListSize; ++i )
  290. {
  291. xfer->xferObjectID( &objectID );
  292. m_xferContainList.push_back( objectID );
  293. } // end for, i
  294. } // end else, load
  295. // tunnel count
  296. xfer->xferUnsignedInt( &m_tunnelCount );
  297. } // end xfer
  298. // ------------------------------------------------------------------------------------------------
  299. /** Load post process */
  300. // ------------------------------------------------------------------------------------------------
  301. void TunnelTracker::loadPostProcess( void )
  302. {
  303. // sanity, the contain list should be empty until we post process the id list
  304. if( m_containList.size() != 0 )
  305. {
  306. DEBUG_CRASH(( "TunnelTracker::loadPostProcess - m_containList should be empty but is not\n" ));
  307. throw SC_INVALID_DATA;
  308. } // end if
  309. // translate each object ids on the xferContainList into real object pointers in the contain list
  310. Object *obj;
  311. std::list< ObjectID >::const_iterator it;
  312. for( it = m_xferContainList.begin(); it != m_xferContainList.end(); ++it )
  313. {
  314. obj = TheGameLogic->findObjectByID( *it );
  315. if( obj == NULL )
  316. {
  317. DEBUG_CRASH(( "TunnelTracker::loadPostProcess - Unable to find object ID '%d'\n", *it ));
  318. throw SC_INVALID_DATA;
  319. } // end if
  320. // push on the back of the contain list
  321. m_containList.push_back( obj );
  322. // Crap. This is in OpenContain as a fix, but not here.
  323. {
  324. // remove object from its group (if any)
  325. obj->leaveGroup();
  326. // remove rider from partition manager
  327. ThePartitionManager->unRegisterObject( obj );
  328. // hide the drawable associated with rider
  329. if( obj->getDrawable() )
  330. obj->getDrawable()->setDrawableHidden( true );
  331. // remove object from pathfind map
  332. if( TheAI )
  333. TheAI->pathfinder()->removeObjectFromPathfindMap( obj );
  334. }
  335. } // end for, it
  336. // we're done with the xfer contain list now
  337. m_xferContainList.clear();
  338. } // end loadPostProcess