ParkingPlaceBehavior.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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: ParkingPlaceBehavior.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author: Steven Johnson, June 2002
  25. // Desc:
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/CRCDebug.h"
  30. #include "Common/Xfer.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "GameClient/Drawable.h"
  33. #include "GameLogic/AI.h"
  34. #include "GameLogic/AIPathfind.h"
  35. #include "GameLogic/Module/AIUpdate.h"
  36. #include "GameLogic/GameLogic.h"
  37. #include "GameLogic/Module/ParkingPlaceBehavior.h"
  38. #include "GameLogic/Module/ProductionUpdate.h"
  39. #include "GameLogic/Module/JetAIUpdate.h"
  40. #include "GameLogic/Object.h"
  41. #include "GameLogic/TerrainLogic.h"
  42. #include "Common/Team.h"
  43. #ifdef _INTERNAL
  44. // for occasional debugging...
  45. //#pragma optimize("", off)
  46. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  47. #endif
  48. //-------------------------------------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------------------
  50. ParkingPlaceBehavior::ParkingPlaceBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  51. {
  52. m_gotInfo = false;
  53. //Added By Sadullah Nader
  54. //Initializations
  55. m_heliRallyPoint.zero();
  56. //
  57. m_heliRallyPointExists = FALSE;
  58. m_nextHealFrame = FOREVER;
  59. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  60. }
  61. //-------------------------------------------------------------------------------------------------
  62. //-------------------------------------------------------------------------------------------------
  63. ParkingPlaceBehavior::~ParkingPlaceBehavior( void )
  64. {
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. //-------------------------------------------------------------------------------------------------
  68. void ParkingPlaceBehavior::buildInfo()
  69. {
  70. if (m_gotInfo)
  71. return;
  72. if (getObject()->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) ||
  73. getObject()->testStatus(OBJECT_STATUS_SOLD))
  74. return;
  75. const ParkingPlaceBehaviorModuleData* d = getParkingPlaceBehaviorModuleData();
  76. ProductionUpdateInterface* pu = getObject()->getProductionUpdateInterface();
  77. m_spaces.reserve(d->m_numRows * d->m_numCols);
  78. Int door = 0;
  79. {
  80. ParkingPlaceInfo info;
  81. for (Int row = 0; row < d->m_numRows; ++row)
  82. {
  83. for (Int col = 0; col < d->m_numCols; ++col)
  84. {
  85. AsciiString tmp;
  86. Matrix3D mtx;
  87. tmp.format("Runway%dPark%dHan",col+1,row+1);
  88. getObject()->getSingleLogicalBonePosition(tmp.str(), &info.m_hangarStart, &mtx);
  89. info.m_hangarStartOrient = mtx.Get_Z_Rotation();
  90. tmp.format("Runway%dParking%d",col+1,row+1);
  91. getObject()->getSingleLogicalBonePosition(tmp.str(), &info.m_location, &mtx);
  92. info.m_orientation = mtx.Get_Z_Rotation();
  93. tmp.format("Runway%dPrep%d",col+1,row+1);
  94. getObject()->getSingleLogicalBonePosition(tmp.str(), &info.m_prep, NULL);
  95. info.m_runway = col;
  96. info.m_door = (ExitDoorType)door++;
  97. info.m_objectInSpace = INVALID_ID;
  98. info.m_reservedForExit = false;
  99. if (pu)
  100. pu->setHoldDoorOpen(info.m_door, false);
  101. m_spaces.push_back(info);
  102. }
  103. }
  104. }
  105. if (d->m_hasRunways)
  106. {
  107. RunwayInfo info;
  108. m_runways.reserve(d->m_numCols);
  109. for (Int col = 0; col < d->m_numCols; ++col)
  110. {
  111. AsciiString tmp;
  112. tmp.format("RunwayStart%d",col+1);
  113. getObject()->getSingleLogicalBonePosition(tmp.str(), &info.m_start, NULL);
  114. tmp.format("RunwayEnd%d",col+1);
  115. getObject()->getSingleLogicalBonePosition(tmp.str(), &info.m_end, NULL);
  116. info.m_inUseBy = INVALID_ID;
  117. info.m_nextInLineForTakeoff = INVALID_ID;
  118. info.m_wasInLine = false;
  119. m_runways.push_back(info);
  120. }
  121. }
  122. m_gotInfo = true;
  123. }
  124. //-------------------------------------------------------------------------------------------------
  125. void ParkingPlaceBehavior::purgeDead()
  126. {
  127. buildInfo();
  128. ProductionUpdateInterface* pu = getObject()->getProductionUpdateInterface();
  129. {
  130. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  131. {
  132. if (it->m_objectInSpace != INVALID_ID)
  133. {
  134. Object* obj = TheGameLogic->findObjectByID(it->m_objectInSpace);
  135. if (obj == NULL || obj->isEffectivelyDead())
  136. {
  137. it->m_objectInSpace = INVALID_ID;
  138. it->m_reservedForExit = false;
  139. if (pu)
  140. pu->setHoldDoorOpen(it->m_door, false);
  141. }
  142. }
  143. }
  144. }
  145. {
  146. for (std::vector<RunwayInfo>::iterator it = m_runways.begin(); it != m_runways.end(); ++it)
  147. {
  148. if (it->m_inUseBy != INVALID_ID)
  149. {
  150. Object* obj = TheGameLogic->findObjectByID(it->m_inUseBy);
  151. if (obj == NULL || obj->isEffectivelyDead())
  152. {
  153. it->m_inUseBy = INVALID_ID;
  154. it->m_wasInLine = false;
  155. }
  156. }
  157. if (it->m_nextInLineForTakeoff != INVALID_ID)
  158. {
  159. Object* obj = TheGameLogic->findObjectByID(it->m_nextInLineForTakeoff);
  160. if (obj == NULL || obj->isEffectivelyDead())
  161. {
  162. it->m_nextInLineForTakeoff = INVALID_ID;
  163. }
  164. }
  165. }
  166. }
  167. {
  168. Bool anythingPurged = false;
  169. for (std::list<HealingInfo>::iterator it = m_healing.begin(); it != m_healing.end(); /*++it*/)
  170. {
  171. if (it->m_gettingHealedID != INVALID_ID)
  172. {
  173. Object* objToHeal = TheGameLogic->findObjectByID(it->m_gettingHealedID);
  174. if (objToHeal == NULL || objToHeal->isEffectivelyDead())
  175. {
  176. it = m_healing.erase(it);
  177. anythingPurged = true;
  178. }
  179. else
  180. {
  181. ++it;
  182. }
  183. }
  184. }
  185. if (anythingPurged)
  186. resetWakeFrame();
  187. }
  188. }
  189. //-------------------------------------------------------------------------------------------------
  190. // note: called from client, so MUST NOT modify self in any way, or desyncs will occur
  191. Bool ParkingPlaceBehavior::hasReservedSpace(ObjectID id) const
  192. {
  193. if (!m_gotInfo)
  194. return false;
  195. if (id == INVALID_ID) // shouldn't call this way, but Weapon mistakenly does sometimes, so check for it
  196. return false;
  197. for (std::vector<ParkingPlaceInfo>::const_iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  198. {
  199. if (it->m_objectInSpace == id)
  200. return true;
  201. }
  202. return false;
  203. }
  204. //-------------------------------------------------------------------------------------------------
  205. ParkingPlaceBehavior::ParkingPlaceInfo* ParkingPlaceBehavior::findPPI(ObjectID id)
  206. {
  207. DEBUG_ASSERTCRASH(id != INVALID_ID, ("call findEmptyPPI instead"));
  208. if (!m_gotInfo || id == INVALID_ID)
  209. return NULL;
  210. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  211. {
  212. if (it->m_objectInSpace == id)
  213. return &(*it);
  214. }
  215. return NULL;
  216. }
  217. //-------------------------------------------------------------------------------------------------
  218. ParkingPlaceBehavior::ParkingPlaceInfo* ParkingPlaceBehavior::findEmptyPPI()
  219. {
  220. if (!m_gotInfo)
  221. return NULL;
  222. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  223. {
  224. if (it->m_objectInSpace == INVALID_ID && it->m_reservedForExit == false)
  225. return &(*it);
  226. }
  227. return NULL;
  228. }
  229. //-------------------------------------------------------------------------------------------------
  230. // note: called from client, so MUST NOT modify self in any way, or desyncs will occur
  231. Bool ParkingPlaceBehavior::shouldReserveDoorWhenQueued(const ThingTemplate* thing) const
  232. {
  233. if (thing->isKindOf(KINDOF_PRODUCED_AT_HELIPAD))
  234. return false;
  235. return true;
  236. }
  237. //-------------------------------------------------------------------------------------------------
  238. // note: called from client, so MUST NOT modify self in any way, or desyncs will occur
  239. Bool ParkingPlaceBehavior::hasAvailableSpaceFor(const ThingTemplate* thing) const
  240. {
  241. if (!m_gotInfo) // degenerate case, shouldn't happen, but just in case...
  242. return false;
  243. if (thing->isKindOf(KINDOF_PRODUCED_AT_HELIPAD))
  244. return true;
  245. for (std::vector<ParkingPlaceInfo>::const_iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  246. {
  247. ObjectID id = it->m_objectInSpace;
  248. // since this is const, and we can't purge the dead safely, just peek and see if we have a dead thing.
  249. if (id != INVALID_ID)
  250. {
  251. Object* obj = TheGameLogic->findObjectByID(id);
  252. if (obj == NULL || obj->isEffectivelyDead())
  253. {
  254. id = INVALID_ID;
  255. }
  256. }
  257. if (id == INVALID_ID && it->m_reservedForExit == false)
  258. {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. //-------------------------------------------------------------------------------------------------
  265. Bool ParkingPlaceBehavior::reserveSpace(ObjectID id, Real parkingOffset, ParkingPlaceBehaviorInterface::PPInfo* info)
  266. {
  267. buildInfo();
  268. purgeDead();
  269. const ParkingPlaceBehaviorModuleData* d = getParkingPlaceBehaviorModuleData();
  270. ParkingPlaceInfo* ppi = findPPI(id);
  271. if (ppi == NULL)
  272. {
  273. ppi = findEmptyPPI();
  274. if (ppi == NULL)
  275. {
  276. DEBUG_CRASH(("No parking places!"));
  277. return false; // nothing available
  278. }
  279. }
  280. ppi->m_objectInSpace = id;
  281. ppi->m_reservedForExit = false;
  282. if (info)
  283. {
  284. const RunwayInfo& rr = m_runways[ppi->m_runway];
  285. info->parkingSpace = d->m_parkInHangars ? ppi->m_hangarStart : ppi->m_location;
  286. if (parkingOffset != 0.0f)
  287. {
  288. info->parkingSpace.x += parkingOffset * Cos(ppi->m_orientation);
  289. info->parkingSpace.y += parkingOffset * Sin(ppi->m_orientation);
  290. }
  291. info->runwayPrep = ppi->m_prep;
  292. info->parkingOrientation = d->m_parkInHangars ? ppi->m_hangarStartOrient : ppi->m_orientation;
  293. info->runwayStart = rr.m_start;
  294. info->runwayEnd = rr.m_end;
  295. info->runwayApproach = rr.m_end;
  296. //const Real APPROACH_DIST = 0.5f; // no, too short, planes may not have enough space to drop altitude
  297. const Real APPROACH_DIST = 0.75f;
  298. info->runwayApproach.x += (rr.m_end.x - rr.m_start.x) * APPROACH_DIST;
  299. info->runwayApproach.y += (rr.m_end.y - rr.m_start.y) * APPROACH_DIST;
  300. info->runwayApproach.z = rr.m_end.z + d->m_approachHeight;
  301. info->hangarInternal = ppi->m_hangarStart;
  302. info->hangarInternalOrient = ppi->m_hangarStartOrient;
  303. for (std::vector<RunwayInfo>::iterator it = m_runways.begin(); it != m_runways.end(); ++it)
  304. {
  305. if (it->m_inUseBy == id && it->m_wasInLine)
  306. {
  307. info->runwayStart = info->runwayPrep;
  308. }
  309. }
  310. }
  311. ProductionUpdateInterface* pu = getObject()->getProductionUpdateInterface();
  312. if (pu)
  313. pu->setHoldDoorOpen(ppi->m_door, true);
  314. return true;
  315. }
  316. //-------------------------------------------------------------------------------------------------
  317. void ParkingPlaceBehavior::releaseSpace(ObjectID id)
  318. {
  319. buildInfo();
  320. purgeDead();
  321. ProductionUpdateInterface* pu = getObject()->getProductionUpdateInterface();
  322. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  323. {
  324. if (it->m_objectInSpace == id)
  325. {
  326. it->m_objectInSpace = INVALID_ID;
  327. it->m_reservedForExit = false;
  328. if (pu)
  329. pu->setHoldDoorOpen(it->m_door, false);
  330. }
  331. }
  332. }
  333. //-------------------------------------------------------------------------------------------------
  334. ObjectID ParkingPlaceBehavior::getRunwayReservation(Int runway)
  335. {
  336. buildInfo();
  337. purgeDead();
  338. return m_runways[runway].m_inUseBy;
  339. }
  340. //-------------------------------------------------------------------------------------------------
  341. void ParkingPlaceBehavior::transferRunwayReservationToNextInLineForTakeoff(ObjectID id)
  342. {
  343. buildInfo();
  344. purgeDead();
  345. for (std::vector<RunwayInfo>::iterator it = m_runways.begin(); it != m_runways.end(); ++it)
  346. {
  347. if (it->m_inUseBy == id && it->m_nextInLineForTakeoff != INVALID_ID)
  348. {
  349. it->m_inUseBy = it->m_nextInLineForTakeoff;
  350. it->m_wasInLine = true;
  351. it->m_nextInLineForTakeoff = INVALID_ID;
  352. }
  353. }
  354. }
  355. //-------------------------------------------------------------------------------------------------
  356. Bool ParkingPlaceBehavior::reserveRunway(ObjectID id, Bool forLanding)
  357. {
  358. buildInfo();
  359. purgeDead();
  360. Int runway = -1;
  361. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  362. {
  363. if (it->m_objectInSpace == id)
  364. {
  365. runway = it->m_runway;
  366. break;
  367. }
  368. }
  369. if (runway == -1)
  370. {
  371. DEBUG_CRASH(("only planes with reserved spaces can reserve runways"));
  372. return false;
  373. }
  374. RunwayInfo& info = m_runways[runway];
  375. if (info.m_inUseBy == id)
  376. {
  377. return true;
  378. }
  379. else if (info.m_inUseBy == INVALID_ID)
  380. {
  381. info.m_inUseBy = id;
  382. if (info.m_nextInLineForTakeoff == id)
  383. {
  384. info.m_nextInLineForTakeoff = INVALID_ID;
  385. info.m_wasInLine = true;
  386. }
  387. else
  388. {
  389. info.m_wasInLine = false;
  390. }
  391. return true;
  392. }
  393. else if (!forLanding && info.m_nextInLineForTakeoff == INVALID_ID)
  394. {
  395. info.m_nextInLineForTakeoff = id;
  396. return false; // yes, that's right
  397. }
  398. return false;
  399. }
  400. //-------------------------------------------------------------------------------------------------
  401. void ParkingPlaceBehavior::releaseRunway(ObjectID id)
  402. {
  403. buildInfo();
  404. purgeDead();
  405. for (std::vector<RunwayInfo>::iterator it = m_runways.begin(); it != m_runways.end(); ++it)
  406. {
  407. if (it->m_inUseBy == id)
  408. {
  409. it->m_inUseBy = INVALID_ID;
  410. it->m_wasInLine = false;
  411. }
  412. if (it->m_nextInLineForTakeoff == id)
  413. {
  414. it->m_nextInLineForTakeoff = INVALID_ID;
  415. }
  416. }
  417. }
  418. //-------------------------------------------------------------------------------------------------
  419. // don't really need to autoheal every frame....
  420. const Int HEAL_RATE_FRAMES = LOGICFRAMES_PER_SECOND / 5;
  421. //-------------------------------------------------------------------------------------------------
  422. void ParkingPlaceBehavior::resetWakeFrame()
  423. {
  424. if (m_healing.empty())
  425. {
  426. m_nextHealFrame = FOREVER;
  427. }
  428. else
  429. {
  430. m_nextHealFrame = TheGameLogic->getFrame() + HEAL_RATE_FRAMES;
  431. }
  432. }
  433. //-------------------------------------------------------------------------------------------------
  434. void ParkingPlaceBehavior::setHealee(Object* healee, Bool add)
  435. {
  436. if (add)
  437. {
  438. for (std::list<HealingInfo>::const_iterator it = m_healing.begin(); it != m_healing.end(); ++it)
  439. {
  440. if (it->m_gettingHealedID == healee->getID())
  441. return;
  442. }
  443. HealingInfo info;
  444. info.m_gettingHealedID = healee->getID();
  445. info.m_healStartFrame = TheGameLogic->getFrame();
  446. m_healing.push_back(info);
  447. resetWakeFrame();
  448. }
  449. else
  450. {
  451. for (std::list<HealingInfo>::iterator it = m_healing.begin(); it != m_healing.end(); /*++it*/)
  452. {
  453. if (it->m_gettingHealedID == healee->getID())
  454. {
  455. it = m_healing.erase(it);
  456. resetWakeFrame();
  457. }
  458. else
  459. {
  460. ++it;
  461. }
  462. }
  463. }
  464. }
  465. //-------------------------------------------------------------------------------------------------
  466. void ParkingPlaceBehavior::defectAllParkedUnits(Team* newTeam, UnsignedInt detectionTime)
  467. {
  468. buildInfo();
  469. purgeDead();
  470. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  471. {
  472. if (it->m_objectInSpace != INVALID_ID)
  473. {
  474. Object* obj = TheGameLogic->findObjectByID(it->m_objectInSpace);
  475. if (obj == NULL || obj->isEffectivelyDead())
  476. continue;
  477. // srj sez: evil. fix better someday.
  478. static NameKeyType jetKey = TheNameKeyGenerator->nameToKey("JetAIUpdate");
  479. JetAIUpdate* ju = (JetAIUpdate *)obj->findUpdateModule(jetKey);
  480. Bool takeoffOrLanding = ju ? ju->friend_isTakeoffOrLandingInProgress() : false;
  481. if (obj->isAboveTerrain() && !takeoffOrLanding)
  482. {
  483. // if the new team is a different controlling player, this guys loses his space.
  484. if (newTeam->getControllingPlayer() != obj->getControllingPlayer())
  485. {
  486. releaseSpace(obj->getID());
  487. if (obj->getProducerID() == getObject()->getID())
  488. obj->setProducer(NULL);
  489. }
  490. }
  491. else
  492. {
  493. obj->defect(newTeam, detectionTime);
  494. }
  495. }
  496. }
  497. purgeDead();
  498. }
  499. //-------------------------------------------------------------------------------------------------
  500. void ParkingPlaceBehavior::killAllParkedUnits()
  501. {
  502. buildInfo();
  503. purgeDead();
  504. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  505. {
  506. if (it->m_objectInSpace != INVALID_ID)
  507. {
  508. Object* obj = TheGameLogic->findObjectByID(it->m_objectInSpace);
  509. if (obj == NULL || obj->isEffectivelyDead())
  510. continue;
  511. // srj sez: evil. fix better someday.
  512. static NameKeyType jetKey = TheNameKeyGenerator->nameToKey("JetAIUpdate");
  513. JetAIUpdate* ju = (JetAIUpdate *)obj->findUpdateModule(jetKey);
  514. Bool takeoffOrLanding = ju ? ju->friend_isTakeoffOrLandingInProgress() : false;
  515. if (obj->isAboveTerrain() && !takeoffOrLanding)
  516. continue;
  517. obj->kill();
  518. }
  519. }
  520. purgeDead();
  521. }
  522. //-------------------------------------------------------------------------------------------------
  523. void ParkingPlaceBehavior::onDie( const DamageInfo *damageInfo )
  524. {
  525. killAllParkedUnits();
  526. }
  527. //-------------------------------------------------------------------------------------------------
  528. UpdateSleepTime ParkingPlaceBehavior::update()
  529. {
  530. // alas, we need to keep the buildInfo and dead-purged stuff pretty much up to date, for
  531. // the client to be able to peek at. at this late date, the most expedient way is to ensure
  532. // our update is run every frame, and do this manually. the extra cost should be trivial, since
  533. // there are generally at most only a few airfields at any given time.
  534. buildInfo();
  535. purgeDead();
  536. UnsignedInt now = TheGameLogic->getFrame();
  537. if (now >= m_nextHealFrame)
  538. {
  539. m_nextHealFrame = now + HEAL_RATE_FRAMES;
  540. const ParkingPlaceBehaviorModuleData* d = getParkingPlaceBehaviorModuleData();
  541. for (std::list<HealingInfo>::iterator it = m_healing.begin(); it != m_healing.end(); /*++it*/)
  542. {
  543. if (it->m_gettingHealedID != INVALID_ID)
  544. {
  545. Object* objToHeal = TheGameLogic->findObjectByID(it->m_gettingHealedID);
  546. if (objToHeal == NULL || objToHeal->isEffectivelyDead())
  547. {
  548. it = m_healing.erase(it);
  549. }
  550. else
  551. {
  552. DamageInfo healInfo;
  553. healInfo.in.m_damageType = DAMAGE_HEALING;
  554. healInfo.in.m_deathType = DEATH_NONE;
  555. healInfo.in.m_sourceID = getObject()->getID();
  556. healInfo.in.m_amount = HEAL_RATE_FRAMES * d->m_healAmount * SECONDS_PER_LOGICFRAME_REAL;
  557. BodyModuleInterface *body = objToHeal->getBodyModule();
  558. body->attemptHealing( &healInfo );
  559. ++it;
  560. }
  561. }
  562. }
  563. }
  564. return UPDATE_SLEEP_NONE;
  565. }
  566. //-------------------------------------------------------------------------------------------------
  567. ExitDoorType ParkingPlaceBehavior::reserveDoorForExit( const ThingTemplate* objType, Object *specificObject )
  568. {
  569. buildInfo();
  570. purgeDead();
  571. if (objType->isKindOf(KINDOF_PRODUCED_AT_HELIPAD))
  572. {
  573. return DOOR_NONE_NEEDED;
  574. }
  575. // note that this never allocates a per-unit parking space, even if specificObject is null.
  576. // this is by design and is assumed by exitObjectViaDoor.
  577. ParkingPlaceInfo* ppi = findEmptyPPI();
  578. if (ppi)
  579. {
  580. ppi->m_objectInSpace = INVALID_ID;
  581. ppi->m_reservedForExit = true;
  582. return ppi->m_door;
  583. }
  584. return DOOR_NONE_AVAILABLE;
  585. }
  586. //-------------------------------------------------------------------------------------------------
  587. void ParkingPlaceBehavior::exitObjectViaDoor( Object *newObj, ExitDoorType exitDoor ) ///< Here is the thing I want you to exit
  588. {
  589. if (exitDoor != DOOR_NONE_NEEDED)
  590. {
  591. ParkingPlaceInfo* ppi = NULL;
  592. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  593. {
  594. if (it->m_objectInSpace == INVALID_ID && it->m_reservedForExit == TRUE && it->m_door == exitDoor)
  595. {
  596. ppi = &(*it);
  597. break;
  598. }
  599. }
  600. if (!ppi)
  601. {
  602. DEBUG_CRASH(("could not find the space. what?"));
  603. return;
  604. }
  605. ppi->m_objectInSpace = newObj->getID();
  606. ppi->m_reservedForExit = false;
  607. }
  608. /// @todo srj -- this is evil. fix.
  609. static NameKeyType jetKey = TheNameKeyGenerator->nameToKey( "JetAIUpdate" );
  610. JetAIUpdate* ju = (JetAIUpdate *)newObj->findUpdateModule( jetKey );
  611. Real parkingOffset = ju ? ju->friend_getParkingOffset() : 0.0f;
  612. Bool producedAtHelipad = newObj->isKindOf(KINDOF_PRODUCED_AT_HELIPAD);
  613. PPInfo ppinfo;
  614. DUMPMATRIX3D(getObject()->getTransformMatrix());
  615. DUMPCOORD3D(getObject()->getPosition());
  616. if (producedAtHelipad)
  617. {
  618. CRCDEBUG_LOG(("Produced at helipad (door = %d)\n", exitDoor));
  619. DEBUG_ASSERTCRASH(exitDoor == DOOR_NONE_NEEDED, ("Hmm, unlikely"));
  620. Matrix3D mtx;
  621. #ifdef DEBUG_CRASHING
  622. Bool boneOk =
  623. #endif
  624. getObject()->getSingleLogicalBonePosition("HeliPark01", &ppinfo.hangarInternal, &mtx);
  625. DEBUG_ASSERTCRASH(boneOk, ("Could not get bone!"));
  626. ppinfo.hangarInternalOrient = mtx.Get_Z_Rotation();
  627. ppinfo.parkingSpace = ppinfo.hangarInternal;
  628. ppinfo.parkingOrientation = ppinfo.hangarInternalOrient;
  629. }
  630. else
  631. {
  632. CRCDEBUG_LOG(("Produced at hangar (door = %d)\n", exitDoor));
  633. DEBUG_ASSERTCRASH(exitDoor != DOOR_NONE_NEEDED, ("Hmm, unlikely"));
  634. if (!reserveSpace(newObj->getID(), parkingOffset, &ppinfo)) //&loc, &orient, NULL, NULL, NULL, NULL, &hangarInternal, &hangOrient))
  635. {
  636. DEBUG_CRASH(("no spaces available, how did we get here?"));
  637. ppinfo.parkingSpace = *getObject()->getPosition();
  638. ppinfo.parkingOrientation = getObject()->getOrientation();
  639. }
  640. }
  641. DUMPCOORD3D(&ppinfo.hangarInternal);
  642. DUMPREAL(ppinfo.hangarInternalOrient);
  643. DUMPCOORD3D(&ppinfo.parkingSpace);
  644. DUMPREAL(ppinfo.parkingOrientation);
  645. newObj->setPosition( &ppinfo.hangarInternal );
  646. newObj->setOrientation( ppinfo.hangarInternalOrient );
  647. TheAI->pathfinder()->addObjectToPathfindMap( newObj );
  648. AIUpdateInterface *ai = newObj->getAIUpdateInterface();
  649. if( ai )
  650. {
  651. Bool movedToRallyPoint = FALSE;
  652. if( producedAtHelipad )
  653. {
  654. const Coord3D *rallyPoint = getRallyPoint();
  655. if( rallyPoint )
  656. {
  657. ai->aiMoveToPosition( rallyPoint, CMD_FROM_AI );
  658. movedToRallyPoint = TRUE;
  659. }
  660. }
  661. if( !movedToRallyPoint )
  662. {
  663. std::vector<Coord3D> exitPath;
  664. exitPath.push_back(ppinfo.parkingSpace);
  665. ai->aiFollowExitProductionPath( &exitPath, getObject(), CMD_FROM_AI );
  666. }
  667. }
  668. }
  669. //-------------------------------------------------------------------------------------------------
  670. void ParkingPlaceBehavior::unreserveDoorForExit( ExitDoorType exitDoor )
  671. {
  672. if (exitDoor != DOOR_NONE_NEEDED)
  673. {
  674. for (std::vector<ParkingPlaceInfo>::iterator it = m_spaces.begin(); it != m_spaces.end(); ++it)
  675. {
  676. if (it->m_door == exitDoor)
  677. {
  678. //DEBUG_ASSERTCRASH(it->m_reservedForExit, ("ParkingPlaceBehavior::unreserveDoorForExit: door %d was not reserved\n",exitDoor));
  679. it->m_objectInSpace = INVALID_ID;
  680. it->m_reservedForExit = false;
  681. return;
  682. }
  683. }
  684. DEBUG_CRASH(("ParkingPlaceBehavior::unreserveDoorForExit: door %d was not found\n",exitDoor));
  685. }
  686. }
  687. //-------------------------------------------------------------------------------------------------
  688. void ParkingPlaceBehavior::setRallyPoint( const Coord3D *pos )
  689. {
  690. m_heliRallyPointExists = TRUE;
  691. m_heliRallyPoint.set( pos );
  692. // nothing
  693. }
  694. //-------------------------------------------------------------------------------------------------
  695. const Coord3D* ParkingPlaceBehavior::getRallyPoint( void ) const
  696. {
  697. if( m_heliRallyPointExists )
  698. {
  699. return &m_heliRallyPoint;
  700. }
  701. return NULL;
  702. }
  703. //-------------------------------------------------------------------------------------------------
  704. //We only use this for the helipad -- therefore the exit position is the helipad creation point.
  705. //-------------------------------------------------------------------------------------------------
  706. Bool ParkingPlaceBehavior::getExitPosition( Coord3D& exitPosition ) const
  707. {
  708. Matrix3D mtx;
  709. return getObject()->getSingleLogicalBonePosition("HeliPark01", &exitPosition, &mtx );
  710. }
  711. //-------------------------------------------------------------------------------------------------
  712. Bool ParkingPlaceBehavior::getNaturalRallyPoint( Coord3D& rallyPoint, Bool offset ) const
  713. {
  714. Matrix3D mtx;
  715. return getObject()->getSingleLogicalBonePosition("HeliPark01", &rallyPoint, &mtx );
  716. }
  717. // ------------------------------------------------------------------------------------------------
  718. /** CRC */
  719. // ------------------------------------------------------------------------------------------------
  720. void ParkingPlaceBehavior::crc( Xfer *xfer )
  721. {
  722. // extend base class
  723. UpdateModule::crc( xfer );
  724. } // end crc
  725. // ------------------------------------------------------------------------------------------------
  726. /** Xfer method
  727. * Version Info:
  728. * 1: Initial version */
  729. // ------------------------------------------------------------------------------------------------
  730. void ParkingPlaceBehavior::xfer( Xfer *xfer )
  731. {
  732. Int i;
  733. // version
  734. const XferVersion currentVersion = 3;
  735. XferVersion version = currentVersion;
  736. xfer->xferVersion( &version, currentVersion );
  737. // extend base class
  738. UpdateModule::xfer( xfer );
  739. if( xfer->getXferMode() == XFER_LOAD )
  740. {
  741. // first, build our info, so it won't be overwritten later.
  742. buildInfo();
  743. }
  744. // spaces info count and data
  745. UnsignedByte spacesCount = m_spaces.size();
  746. xfer->xferUnsignedByte( &spacesCount );
  747. if( xfer->getXferMode() == XFER_SAVE )
  748. {
  749. // save all elements
  750. std::vector< ParkingPlaceInfo >::iterator it;
  751. for( it = m_spaces.begin(); it != m_spaces.end(); ++it )
  752. {
  753. // object in this space
  754. xfer->xferObjectID( &((*it).m_objectInSpace) );
  755. // reserved for exit
  756. xfer->xferBool( &((*it).m_reservedForExit) );
  757. } // end for, it
  758. } // end if, save
  759. else if( xfer->getXferMode() == XFER_LOAD )
  760. {
  761. ObjectID objectID;
  762. Bool reservedForExit;
  763. // read all elements
  764. std::vector< ParkingPlaceInfo >::iterator it;
  765. it = m_spaces.begin();
  766. for( i = 0; i < spacesCount; ++i )
  767. {
  768. // read object id
  769. xfer->xferObjectID( &objectID );
  770. // read reserved flag
  771. xfer->xferBool( &reservedForExit );
  772. // store in vector if the vector does indeed still have room for this entry
  773. if( it != m_spaces.end() )
  774. {
  775. (*it).m_objectInSpace = objectID;
  776. (*it).m_reservedForExit = reservedForExit;
  777. ++it;
  778. } // end if
  779. } // end for, i
  780. } // end else, load
  781. // runways cound and info
  782. UnsignedByte runwaysCount = m_runways.size();
  783. xfer->xferUnsignedByte( &runwaysCount );
  784. if( xfer->getXferMode() == XFER_SAVE )
  785. {
  786. // save all elements
  787. std::vector< RunwayInfo >::iterator it;
  788. for( it = m_runways.begin(); it != m_runways.end(); ++it )
  789. {
  790. // save object ID
  791. xfer->xferObjectID( &((*it).m_inUseBy) );
  792. xfer->xferObjectID( &((*it).m_nextInLineForTakeoff) );
  793. xfer->xferBool( &((*it).m_wasInLine) );
  794. } // end for, it
  795. } // end if, save
  796. else if( xfer->getXferMode() == XFER_LOAD )
  797. {
  798. // read all elements
  799. std::vector< RunwayInfo >::iterator it;
  800. it = m_runways.begin();
  801. for( i = 0; i < runwaysCount; ++i )
  802. {
  803. ObjectID inUseBy;
  804. ObjectID nextInLineForTakeoff;
  805. Bool wasInLine;
  806. // read object ID
  807. xfer->xferObjectID( &inUseBy );
  808. xfer->xferObjectID( &nextInLineForTakeoff );
  809. xfer->xferBool( &wasInLine );
  810. // store in vector if the vector does indeed still have room for this entry
  811. if( it != m_runways.end() )
  812. {
  813. (*it).m_inUseBy = inUseBy;
  814. (*it).m_nextInLineForTakeoff = nextInLineForTakeoff;
  815. (*it).m_wasInLine = wasInLine;
  816. ++it;
  817. } // end if
  818. } // end for, i
  819. } // end else, load
  820. // healees
  821. UnsignedByte healCount = m_healing.size();
  822. xfer->xferUnsignedByte( &healCount );
  823. if( xfer->getXferMode() == XFER_SAVE )
  824. {
  825. // save all elements
  826. std::list< HealingInfo >::iterator it;
  827. for( it = m_healing.begin(); it != m_healing.end(); ++it )
  828. {
  829. // save object ID
  830. xfer->xferObjectID( &((*it).m_gettingHealedID) );
  831. xfer->xferUnsignedInt( &((*it).m_healStartFrame) );
  832. } // end for, it
  833. } // end if, save
  834. else if( xfer->getXferMode() == XFER_LOAD )
  835. {
  836. // read all elements
  837. m_healing.clear();
  838. for( i = 0; i < healCount; ++i )
  839. {
  840. HealingInfo info;
  841. // read object ID
  842. xfer->xferObjectID( &info.m_gettingHealedID );
  843. xfer->xferUnsignedInt( &info.m_healStartFrame );
  844. m_healing.push_back(info);
  845. } // end for, i
  846. } // end else, load
  847. if (version >= 2)
  848. {
  849. xfer->xferCoord3D(&m_heliRallyPoint);
  850. xfer->xferBool(&m_heliRallyPointExists);
  851. }
  852. if (version >= 3)
  853. {
  854. xfer->xferUnsignedInt(&m_nextHealFrame);
  855. }
  856. else
  857. {
  858. // old file .. just wake it up right away, and it will fall into the correct pattern
  859. if (xfer->getXferMode() == XFER_LOAD)
  860. {
  861. m_nextHealFrame = 0;
  862. }
  863. }
  864. } // end xfer
  865. // ------------------------------------------------------------------------------------------------
  866. /** Load post process */
  867. // ------------------------------------------------------------------------------------------------
  868. void ParkingPlaceBehavior::loadPostProcess( void )
  869. {
  870. // extend base class
  871. UpdateModule::loadPostProcess();
  872. // no, this is bad.. it is NOT SAFE to call setWakeFrame from the xfer system. crap. (srj)
  873. // make sure we are awake... old save games let us sleep
  874. //setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  875. } // end loadPostProcess