ChinookAIUpdate.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. // ChinookAIUpdate.cpp //////////
  24. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  25. #define DEFINE_VETERANCY_NAMES // for TheVeterancyNames[]
  26. #include "Common/ActionManager.h"
  27. #include "Common/DrawModule.h"
  28. #include "Common/GameState.h"
  29. #include "Common/GlobalData.h"
  30. #include "Common/RandomValue.h"
  31. #include "Common/Team.h"
  32. #include "Common/ThingFactory.h"
  33. #include "Common/ThingTemplate.h"
  34. #include "Common/Xfer.h"
  35. #include "GameClient/Drawable.h"
  36. #include "GameClient/GameClient.h"
  37. #include "GameLogic/AIPathfind.h"
  38. #include "GameLogic/Locomotor.h"
  39. #include "GameLogic/Module/ContainModule.h"
  40. #include "GameLogic/Module/ChinookAIUpdate.h"
  41. #include "GameLogic/Module/PhysicsUpdate.h"
  42. #include "GameLogic/PartitionManager.h"
  43. const Real BIGNUM = 99999.0f;
  44. #ifdef _INTERNAL
  45. // for occasional debugging...
  46. //#pragma optimize("", off)
  47. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  48. #endif
  49. //-------------------------------------------------------------------------------------------------
  50. enum ChinookAIStateType
  51. {
  52. // note that these must be distinct (numerically) from AIStateType. ick.
  53. ChinookAIStateType_FIRST = 1000,
  54. TAKING_OFF,
  55. LANDING,
  56. MOVE_TO_AND_LAND,
  57. MOVE_TO_AND_EVAC,
  58. LAND_AND_EVAC,
  59. EVAC_AND_TAKEOFF,
  60. MOVE_TO_AND_EVAC_AND_EXIT,
  61. LAND_AND_EVAC_AND_EXIT,
  62. EVAC_AND_EXIT,
  63. TAKEOFF_AND_EXIT,
  64. HEAD_OFF_MAP,
  65. MOVE_TO_COMBAT_DROP,
  66. DO_COMBAT_DROP
  67. };
  68. //-------------------------------------------------------------------------------------------------
  69. //-------------------------------------------------------------------------------------------------
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. static Real calcDistSqr(const Coord3D& a, const Coord3D& b)
  73. {
  74. return sqr(a.x-b.x) + sqr(a.y-b.y) + sqr(a.z-b.z);
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. static Object* getPotentialRappeller(Object* obj)
  78. {
  79. const ContainedItemsList* items = obj->getContain() ? obj->getContain()->getContainedItemsList() : NULL;
  80. if (items)
  81. {
  82. for (ContainedItemsList::const_iterator it = items->begin(); it != items->end(); ++it )
  83. {
  84. Object* rider = *it;
  85. if (rider->isKindOf(KINDOF_CAN_RAPPEL))
  86. {
  87. return rider;
  88. }
  89. }
  90. }
  91. return NULL;
  92. }
  93. //----------------------------------------------------------------------------------------------------------
  94. class ChinookEvacuateState : public State
  95. {
  96. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookEvacuateState, "ChinookEvacuateState")
  97. protected:
  98. // snapshot interface STUBBED - no member vars to save. jba.
  99. virtual void crc( Xfer *xfer ){};
  100. virtual void xfer( Xfer *xfer ){};
  101. virtual void loadPostProcess(){};
  102. public:
  103. ChinookEvacuateState( StateMachine *machine ) : State( machine, "ChinookEvacuateState" ) { }
  104. StateReturnType onEnter()
  105. {
  106. Object* obj = getMachineOwner();
  107. if( obj->getContain() )
  108. {
  109. obj->getContain()->removeAllContained(FALSE);
  110. }
  111. obj->getTeam()->setActive(); // why? I don't know.
  112. return STATE_SUCCESS;
  113. }
  114. virtual StateReturnType update()
  115. {
  116. return STATE_SUCCESS;
  117. }
  118. };
  119. EMPTY_DTOR(ChinookEvacuateState)
  120. //-------------------------------------------------------------------------------------------------
  121. class ChinookHeadOffMapState : public State
  122. {
  123. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookHeadOffMapState, "ChinookHeadOffMapState")
  124. //I'm outta here
  125. protected:
  126. // snapshot interface STUBBED - no member vars to save. jba.
  127. virtual void crc( Xfer *xfer ){};
  128. virtual void xfer( Xfer *xfer ){};
  129. virtual void loadPostProcess(){};
  130. public:
  131. ChinookHeadOffMapState( StateMachine *machine ) : State( machine, "ChinookHeadOffMapState" ) {}
  132. StateReturnType onEnter() // Give move order out of town
  133. {
  134. Object *owner = getMachineOwner();
  135. AIUpdateInterface *ai = owner->getAIUpdateInterface();
  136. // just keep moving straight ahead till we exit the map.
  137. Coord3D exitCoord = *owner->getPosition();
  138. const Coord3D* dir = owner->getUnitDirectionVector2D();
  139. Region3D terrainExtent;
  140. TheTerrainLogic->getExtent( &terrainExtent );
  141. const Real FUDGE = 1.2f;
  142. Real HUGE_DIST = FUDGE * sqrt(sqr(terrainExtent.hi.x - terrainExtent.lo.x) + sqr(terrainExtent.hi.y - terrainExtent.lo.y));
  143. exitCoord.x += dir->x * HUGE_DIST;
  144. exitCoord.y += dir->y * HUGE_DIST;
  145. ai->aiMoveToPosition( &exitCoord, CMD_FROM_AI );
  146. ai->getCurLocomotor()->setAllowInvalidPosition(true);
  147. return STATE_CONTINUE;
  148. }
  149. StateReturnType update()
  150. {
  151. Object *owner = getMachineOwner();
  152. Region3D mapRegion;
  153. TheTerrainLogic->getExtentIncludingBorder( &mapRegion );
  154. if (!mapRegion.isInRegionNoZ( owner->getPosition() ))
  155. {
  156. TheGameLogic->destroyObject(owner);
  157. return STATE_SUCCESS;
  158. }
  159. return STATE_CONTINUE;
  160. }
  161. };
  162. EMPTY_DTOR(ChinookHeadOffMapState)
  163. //-------------------------------------------------------------------------------------------------
  164. class ChinookTakeoffOrLandingState : public State
  165. {
  166. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookTakeoffOrLandingState, "ChinookTakeoffOrLandingState")
  167. private:
  168. Coord3D m_destLoc;
  169. Bool m_landing;
  170. protected:
  171. // snapshot interface
  172. virtual void crc( Xfer *xfer )
  173. {
  174. // empty
  175. }
  176. virtual void xfer( Xfer *xfer )
  177. {
  178. // version
  179. XferVersion currentVersion = 1;
  180. XferVersion version = currentVersion;
  181. xfer->xferVersion( &version, currentVersion );
  182. xfer->xferCoord3D(&m_destLoc);
  183. xfer->xferBool(&m_landing);
  184. }
  185. virtual void loadPostProcess()
  186. {
  187. // empty
  188. }
  189. public:
  190. ChinookTakeoffOrLandingState( StateMachine *machine, Bool landing ) : m_landing(landing), State( machine, "ChinookTakeoffOrLandingState" )
  191. {
  192. m_destLoc.zero();
  193. }
  194. virtual StateReturnType onEnter()
  195. {
  196. Object* obj = getMachineOwner();
  197. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  198. ai->friend_setFlightStatus(m_landing ? CHINOOK_LANDING : CHINOOK_TAKING_OFF);
  199. if( m_landing )
  200. {
  201. // A chinook given transport duty loses his supplies.
  202. while( ai->loseOneBox() );
  203. }
  204. // kill any drift...
  205. obj->getPhysics()->scrubVelocity2D(0);
  206. ai->chooseLocomotorSet(LOCOMOTORSET_NORMAL);
  207. Locomotor* loco = ai->getCurLocomotor();
  208. loco->setUsePreciseZPos(true);
  209. loco->setUltraAccurate(true);
  210. m_destLoc = *obj->getPosition();
  211. const Bool onlyHealthyBridges = true; // ignore dead bridges.
  212. PathfindLayerEnum layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&m_destLoc, onlyHealthyBridges);
  213. m_destLoc.z = TheTerrainLogic->getLayerHeight(m_destLoc.x, m_destLoc.y, layerAtDest);
  214. if (m_landing)
  215. {
  216. Coord3D tmp;
  217. FindPositionOptions options;
  218. options.maxRadius = obj->getGeometryInfo().getBoundingCircleRadius() * 100.0f;
  219. if (ThePartitionManager->findPositionAround(&m_destLoc, &options, &tmp))
  220. {
  221. m_destLoc = tmp;
  222. TheAI->pathfinder()->adjustToLandingDestination(obj, &m_destLoc);
  223. }
  224. // recalc, since it may have changed. note that findPositionAround() will ALWAYS
  225. // return a position on the ground proper, so if our initial search start pos was
  226. // above a bridge, this will put us below the bridge, which would be unfortunate.
  227. // so recheck to be sure. (note that the partitionmgr is 2d-only, so if it sez that
  228. // the position on the ground at that xy is clear, it will be clear for both the
  229. // ground proper and the bridge itself.) also note: don't call objectInteractsWithBridgeLayer(),
  230. // since it assumes that things that aren't close in z shouldn't interact.
  231. tmp = m_destLoc;
  232. tmp.z = obj->getPosition()->z;
  233. layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&tmp, onlyHealthyBridges);
  234. m_destLoc.z = TheTerrainLogic->getLayerHeight(m_destLoc.x, m_destLoc.y, layerAtDest);
  235. obj->setLayer(layerAtDest);
  236. }
  237. else
  238. {
  239. m_destLoc.z += loco->getPreferredHeight();
  240. obj->setLayer(LAYER_GROUND);
  241. }
  242. return STATE_CONTINUE;
  243. }
  244. virtual StateReturnType update()
  245. {
  246. Object* obj = getMachineOwner();
  247. if (obj->isEffectivelyDead())
  248. return STATE_FAILURE;
  249. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  250. ai->setLocomotorGoalPositionExplicit(m_destLoc);
  251. const Real THRESH = 3.0f;
  252. const Real THRESH_SQR = THRESH*THRESH;
  253. if (calcDistSqr(*obj->getPosition(), m_destLoc) <= THRESH_SQR)
  254. return STATE_SUCCESS;
  255. return STATE_CONTINUE;
  256. }
  257. virtual void onExit( StateExitType status )
  258. {
  259. Object* obj = getMachineOwner();
  260. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  261. ai->friend_setFlightStatus(m_landing ? CHINOOK_LANDED : CHINOOK_FLYING);
  262. // Paranoia checks - sometimes onExit is called when we are
  263. // shutting down, and not all pieces are valid. CurLocomotor
  264. // is definitely null in some cases. jba.
  265. Locomotor* loco = ai->getCurLocomotor();
  266. if (loco)
  267. {
  268. loco->setUsePreciseZPos(false);
  269. loco->setUltraAccurate(false);
  270. // don't restore lift if dead -- this may fight with JetSlowDeathBehavior!
  271. if (!obj->isEffectivelyDead())
  272. loco->setMaxLift(BIGNUM);
  273. }
  274. if (m_landing)
  275. {
  276. ai->chooseLocomotorSet(LOCOMOTORSET_TAXIING);
  277. }
  278. else
  279. {
  280. // when takeoff is complete, always go back to layer-ground, rather than
  281. // some bridge layer.
  282. obj->setLayer(LAYER_GROUND);
  283. }
  284. }
  285. };
  286. EMPTY_DTOR(ChinookTakeoffOrLandingState)
  287. //-------------------------------------------------------------------------------------------------
  288. class ChinookCombatDropState : public State
  289. {
  290. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookCombatDropState, "ChinookCombatDropState")
  291. private:
  292. struct RopeInfo
  293. {
  294. Drawable* ropeDrawable;
  295. DrawableID ropeID; // used only during save-load process
  296. Matrix3D dropStartMtx;
  297. Real ropeSpeed;
  298. Real ropeLen;
  299. Real ropeLenMax;
  300. UnsignedInt nextDropTime;
  301. std::list<ObjectID> rappellerIDs;
  302. };
  303. std::vector<RopeInfo> m_ropes;
  304. void removeDoneRappellers()
  305. {
  306. for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
  307. {
  308. for (std::list<ObjectID>::iterator oit = it->rappellerIDs.begin(); oit != it->rappellerIDs.end(); )
  309. {
  310. Object* rappeller = TheGameLogic->findObjectByID(*oit);
  311. if (rappeller == NULL || rappeller->isEffectivelyDead() || !rappeller->isAboveTerrain())
  312. {
  313. oit = it->rappellerIDs.erase(oit);
  314. }
  315. else
  316. {
  317. ++oit;
  318. }
  319. }
  320. }
  321. }
  322. static void initRopeParms(Drawable* rope, Real length, Real width, const RGBColor& color, Real wobbleLen, Real wobbleAmp, Real wobbleRate)
  323. {
  324. RopeDrawInterface* tdi = NULL;
  325. for (DrawModule** d = rope->getDrawModules(); *d; ++d)
  326. {
  327. if ((tdi = (*d)->getRopeDrawInterface()) != NULL)
  328. {
  329. tdi->initRopeParms(length, width, color, wobbleLen, wobbleAmp, wobbleRate);
  330. }
  331. }
  332. }
  333. static void setRopeCurLen(Drawable* rope, Real length)
  334. {
  335. RopeDrawInterface* tdi = NULL;
  336. for (DrawModule** d = rope->getDrawModules(); *d; ++d)
  337. {
  338. if ((tdi = (*d)->getRopeDrawInterface()) != NULL)
  339. {
  340. tdi->setRopeCurLen(length);
  341. }
  342. }
  343. }
  344. static void setRopeSpeed(Drawable* rope, Real curSpeed, Real maxSpeed, Real accel)
  345. {
  346. RopeDrawInterface* tdi = NULL;
  347. for (DrawModule** d = rope->getDrawModules(); *d; ++d)
  348. {
  349. if ((tdi = (*d)->getRopeDrawInterface()) != NULL)
  350. {
  351. tdi->setRopeSpeed(curSpeed, maxSpeed, accel);
  352. }
  353. }
  354. }
  355. protected:
  356. // snapshot interface
  357. virtual void crc( Xfer *xfer )
  358. {
  359. // empty
  360. }
  361. virtual void xfer( Xfer *xfer )
  362. {
  363. // version
  364. const XferVersion currentVersion = 2;
  365. XferVersion version = currentVersion;
  366. xfer->xferVersion( &version, currentVersion );
  367. Int numRopes = m_ropes.size();
  368. xfer->xferInt(&numRopes);
  369. if (version >= 2)
  370. {
  371. if (xfer->getXferMode() == XFER_LOAD)
  372. {
  373. if (!m_ropes.empty())
  374. {
  375. DEBUG_CRASH(( "ChinookCombatDropState - ropes should be empty\n" ));
  376. throw SC_INVALID_DATA;
  377. }
  378. m_ropes.resize(numRopes);
  379. }
  380. for (Int i = 0; i < numRopes; ++i)
  381. {
  382. RopeInfo info;
  383. if (xfer->getXferMode() == XFER_SAVE)
  384. {
  385. info = m_ropes[i];
  386. // always overwrite this, since it's probably stale
  387. info.ropeID = info.ropeDrawable ? info.ropeDrawable->getID() : INVALID_DRAWABLE_ID;
  388. }
  389. xfer->xferDrawableID(&info.ropeID);
  390. xfer->xferMatrix3D(&info.dropStartMtx);
  391. xfer->xferReal(&info.ropeSpeed);
  392. xfer->xferReal(&info.ropeLen);
  393. xfer->xferReal(&info.ropeLenMax);
  394. xfer->xferUnsignedInt(&info.nextDropTime);
  395. xfer->xferSTLObjectIDList(&info.rappellerIDs);
  396. if (xfer->getXferMode() == XFER_LOAD)
  397. {
  398. info.ropeDrawable = NULL; // filled in via loadPostProcess
  399. m_ropes[i] = info;
  400. }
  401. }
  402. }
  403. }
  404. virtual void loadPostProcess()
  405. {
  406. for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
  407. {
  408. it->ropeDrawable = TheGameClient->findDrawableByID(it->ropeID);
  409. // always nuke this, since we're done with it till we save/load again
  410. it->ropeID = INVALID_DRAWABLE_ID;
  411. }
  412. }
  413. public:
  414. ChinookCombatDropState( StateMachine *machine ): State( machine, "ChinookCombatDropState" ) { }
  415. // --------------
  416. virtual StateReturnType onEnter()
  417. {
  418. Object* obj = getMachineOwner();
  419. Drawable* draw = obj->getDrawable();
  420. if (draw == NULL)
  421. return STATE_FAILURE;
  422. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  423. const ChinookAIUpdateModuleData* d = ai->friend_getData();
  424. obj->setDisabled( DISABLED_HELD );
  425. ai->friend_setFlightStatus(CHINOOK_DOING_COMBAT_DROP);
  426. // A chinook given combat drop duty also loses his supplies.
  427. while( ai->loseOneBox() );
  428. UnsignedInt now = TheGameLogic->getFrame();
  429. const ThingTemplate* ropeTmpl = TheThingFactory->findTemplate(d->m_ropeName);
  430. const Int MAX_BONES = 32;
  431. Coord3D ropePos[MAX_BONES];
  432. Matrix3D dropMtx[MAX_BONES];
  433. Int ropeCount = draw->getPristineBonePositions("RopeStart", 1, ropePos, NULL, MAX_BONES);
  434. Int dropCount = draw->getPristineBonePositions("RopeEnd", 1, NULL, dropMtx, MAX_BONES);
  435. Int numRopes = d->m_numRopes;
  436. if (numRopes > ropeCount) numRopes = ropeCount;
  437. if (numRopes > dropCount) numRopes = dropCount;
  438. if (numRopes <= 0)
  439. return STATE_FAILURE;
  440. m_ropes.clear();
  441. for (Int i = 0; i < numRopes; ++i)
  442. {
  443. RopeInfo info;
  444. obj->convertBonePosToWorldPos( NULL, &dropMtx[i], NULL, &info.dropStartMtx );
  445. info.ropeDrawable = ropeTmpl ? TheThingFactory->newDrawable(ropeTmpl) : NULL;
  446. if (info.ropeDrawable)
  447. {
  448. obj->convertBonePosToWorldPos( &ropePos[i], NULL, &ropePos[i], NULL );
  449. info.ropeDrawable->setPosition(&ropePos[i]);
  450. info.ropeSpeed = 0.0f;
  451. info.ropeLen = 1.0f;
  452. const Bool onlyHealthyBridges = true; // ignore dead bridges.
  453. PathfindLayerEnum layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&ropePos[i], onlyHealthyBridges);
  454. info.ropeLenMax = ropePos[i].z - TheTerrainLogic->getLayerHeight(ropePos[i].x, ropePos[i].y, layerAtDest) - d->m_ropeFinalHeight;
  455. initRopeParms(info.ropeDrawable, info.ropeLenMax, d->m_ropeWidth, d->m_ropeColor, d->m_ropeWobbleLen, d->m_ropeWobbleAmp, d->m_ropeWobbleRate);
  456. }
  457. info.nextDropTime = now + GameLogicRandomValue(d->m_perRopeDelayMin, d->m_perRopeDelayMax) - d->m_perRopeDelayMin;
  458. info.rappellerIDs.clear();
  459. m_ropes.push_back(info);
  460. }
  461. return STATE_CONTINUE;
  462. }
  463. // --------------
  464. virtual StateReturnType update()
  465. {
  466. Object* obj = getMachineOwner();
  467. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  468. const ChinookAIUpdateModuleData* d = ai->friend_getData();
  469. if (obj->isEffectivelyDead())
  470. {
  471. return STATE_FAILURE;
  472. }
  473. // first, eliminate "done" rappellers
  474. removeDoneRappellers();
  475. UnsignedInt now = TheGameLogic->getFrame();
  476. // ok, now check each rope: if it's empty, or we're at the next drop time, spawn a new rappeller
  477. Int numRopesInUse = 0;
  478. for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
  479. {
  480. if (it->ropeLen < it->ropeLenMax)
  481. {
  482. it->ropeSpeed += fabs(TheGlobalData->m_gravity);
  483. if (it->ropeSpeed > d->m_ropeDropSpeed)
  484. it->ropeSpeed = d->m_ropeDropSpeed;
  485. it->ropeLen += it->ropeSpeed;
  486. setRopeCurLen(it->ropeDrawable, it->ropeLen);
  487. if (d->m_waitForRopesToDrop)
  488. {
  489. // can't use this rope till it's dropped all the way
  490. ++it->nextDropTime;
  491. continue;
  492. }
  493. }
  494. if (now >= it->nextDropTime)
  495. {
  496. Object* rappeller = getPotentialRappeller(obj);
  497. if (rappeller != NULL)
  498. {
  499. ExitInterface *exitInterface = obj->getObjectExitInterface();
  500. ExitDoorType exitDoor = exitInterface ? exitInterface->reserveDoorForExit(rappeller->getTemplate(), rappeller) : DOOR_NONE_AVAILABLE;
  501. if(exitDoor != DOOR_NONE_AVAILABLE)
  502. {
  503. exitInterface->exitObjectViaDoor(rappeller, exitDoor);
  504. }
  505. else
  506. {
  507. DEBUG_CRASH(("rappeller is not free to exit... what?"));
  508. }
  509. rappeller->setTransformMatrix(&it->dropStartMtx);
  510. AIUpdateInterface* rappellerAI = rappeller ? rappeller->getAIUpdateInterface() : NULL;
  511. if (rappellerAI)
  512. {
  513. rappellerAI->setDesiredSpeed(d->m_rappelSpeed);
  514. rappellerAI->aiRappelInto(getMachineGoalObject(), *getMachineGoalPosition(), CMD_FROM_AI);
  515. }
  516. it->rappellerIDs.push_back(rappeller->getID());
  517. it->nextDropTime = now + GameLogicRandomValue(d->m_perRopeDelayMin, d->m_perRopeDelayMax);
  518. }
  519. }
  520. if (!it->rappellerIDs.empty())
  521. {
  522. ++numRopesInUse;
  523. }
  524. }
  525. if (numRopesInUse == 0 && getPotentialRappeller(obj) == NULL)
  526. {
  527. // we're done!
  528. return STATE_SUCCESS;
  529. }
  530. return STATE_CONTINUE;
  531. }
  532. // --------------
  533. virtual void onExit( StateExitType status )
  534. {
  535. Object* obj = getMachineOwner();
  536. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  537. const ChinookAIUpdateModuleData* d = ai->friend_getData();
  538. obj->clearDisabled( DISABLED_HELD );
  539. ai->friend_setFlightStatus(CHINOOK_FLYING);
  540. if (obj->isEffectivelyDead())
  541. {
  542. // oops. drop the rangers.
  543. for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
  544. {
  545. for (std::list<ObjectID>::iterator oit = it->rappellerIDs.begin(); oit != it->rappellerIDs.end(); ++oit)
  546. {
  547. Object* rappeller = TheGameLogic->findObjectByID(*oit);
  548. AIUpdateInterface* rappellerAI = rappeller ? rappeller->getAIUpdateInterface() : NULL;
  549. if (rappellerAI != NULL)
  550. {
  551. rappellerAI->aiIdle(CMD_FROM_AI);
  552. }
  553. }
  554. }
  555. }
  556. UnsignedInt now = TheGameLogic->getFrame();
  557. for (Int i = 0; i < m_ropes.size(); ++i)
  558. {
  559. if (m_ropes[i].ropeDrawable)
  560. {
  561. const UnsignedInt ROPE_EXPIRATION_TIME = LOGICFRAMES_PER_SECOND * 5;
  562. const Real initialSpeed = TheGlobalData->m_gravity * 30; // give it a little kick
  563. setRopeSpeed(m_ropes[i].ropeDrawable, initialSpeed, d->m_ropeDropSpeed, TheGlobalData->m_gravity);
  564. m_ropes[i].ropeDrawable->setExpirationDate(now + ROPE_EXPIRATION_TIME);
  565. m_ropes[i].ropeDrawable = NULL; // we're done with it, so null it so we won't save it
  566. }
  567. }
  568. m_ropes.clear();
  569. }
  570. };
  571. EMPTY_DTOR(ChinookCombatDropState)
  572. //-----------------------------------------------------------------------------------------------------------
  573. /**
  574. * Move to the GoalPosition, or GoalObject.
  575. */
  576. class ChinookMoveToBldgState : public AIMoveToState
  577. {
  578. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookMoveToBldgState, "ChinookMoveToBldgState")
  579. private:
  580. Real m_oldPreferredHeight;
  581. Real m_newPreferredHeight;
  582. Real m_destZ;
  583. protected:
  584. // snapshot interface
  585. virtual void crc( Xfer *xfer )
  586. {
  587. // empty
  588. }
  589. virtual void xfer( Xfer *xfer )
  590. {
  591. // version
  592. XferVersion currentVersion = 1;
  593. XferVersion version = currentVersion;
  594. xfer->xferVersion( &version, currentVersion );
  595. xfer->xferReal(&m_oldPreferredHeight);
  596. xfer->xferReal(&m_newPreferredHeight);
  597. xfer->xferReal(&m_destZ);
  598. }
  599. virtual void loadPostProcess()
  600. {
  601. // empty
  602. }
  603. public:
  604. ChinookMoveToBldgState( StateMachine *machine ): AIMoveToState( machine ) { }
  605. virtual StateReturnType onEnter()
  606. {
  607. Object* obj = getMachineOwner();
  608. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  609. const ChinookAIUpdateModuleData* d = ai->friend_getData();
  610. Locomotor* loco = ai->getCurLocomotor();
  611. loco->setUltraAccurate(true);
  612. m_oldPreferredHeight = loco->getPreferredHeight();
  613. m_newPreferredHeight = m_oldPreferredHeight;
  614. const Coord3D* destPos;
  615. Object* bldg = getMachineGoalObject();
  616. if (bldg != NULL && !bldg->isEffectivelyDead() && bldg->isKindOf(KINDOF_STRUCTURE))
  617. {
  618. destPos = bldg->getPosition();
  619. m_newPreferredHeight = bldg->getGeometryInfo().getMaxHeightAbovePosition() + d->m_minDropHeight;
  620. if (m_newPreferredHeight < m_oldPreferredHeight)
  621. m_newPreferredHeight = m_oldPreferredHeight;
  622. }
  623. else
  624. {
  625. destPos = getMachineGoalPosition();
  626. }
  627. loco->setPreferredHeight(m_newPreferredHeight);
  628. m_destZ = TheTerrainLogic->getGroundHeight(destPos->x, destPos->y) + m_newPreferredHeight;
  629. return AIMoveToState::onEnter();
  630. }
  631. virtual StateReturnType update()
  632. {
  633. Object* obj = getMachineOwner();
  634. // the normal moveto state will bail when 2d pos matches; we need z, too
  635. StateReturnType status = AIMoveToState::update();
  636. const Real THRESH = 3.0f;
  637. if (status != STATE_CONTINUE && fabs(obj->getPosition()->z - m_destZ) > THRESH)
  638. status = STATE_CONTINUE;
  639. return status;
  640. }
  641. virtual void onExit( StateExitType status )
  642. {
  643. Object* obj = getMachineOwner();
  644. ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
  645. Locomotor* loco = ai->getCurLocomotor();
  646. loco->setPreferredHeight(m_oldPreferredHeight);
  647. loco->setUltraAccurate(false);
  648. AIMoveToState::onExit(status);
  649. }
  650. };
  651. EMPTY_DTOR(ChinookMoveToBldgState)
  652. //-------------------------------------------------------------------------------------------------
  653. //-------------------------------------------------------------------------------------------------
  654. //-------------------------------------------------------------------------------------------------
  655. //-------------------------------------------------------------------------------------------------
  656. class ChinookAIStateMachine : public AIStateMachine
  657. {
  658. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( ChinookAIStateMachine, "ChinookAIStateMachine" );
  659. public:
  660. ChinookAIStateMachine( Object *owner, AsciiString name );
  661. };
  662. //-------------------------------------------------------------------------------------------------
  663. ChinookAIStateMachine::ChinookAIStateMachine(Object *owner, AsciiString name) : AIStateMachine(owner, name)
  664. {
  665. defineState( TAKING_OFF, newInstance(ChinookTakeoffOrLandingState)( this, false ), AI_IDLE, AI_IDLE );
  666. defineState( LANDING, newInstance(ChinookTakeoffOrLandingState)( this, true ), AI_IDLE, AI_IDLE );
  667. defineState( MOVE_TO_COMBAT_DROP, newInstance(ChinookMoveToBldgState)( this ), DO_COMBAT_DROP, AI_IDLE );
  668. defineState( DO_COMBAT_DROP, newInstance(ChinookCombatDropState)( this ), AI_IDLE, AI_IDLE );
  669. defineState( MOVE_TO_AND_LAND, newInstance(AIMoveToState)( this ), LANDING, AI_IDLE );
  670. defineState( MOVE_TO_AND_EVAC, newInstance(AIMoveToState)( this ), LAND_AND_EVAC, AI_IDLE );
  671. defineState( LAND_AND_EVAC, newInstance(ChinookTakeoffOrLandingState)( this, true ), EVAC_AND_TAKEOFF, AI_IDLE );
  672. defineState( EVAC_AND_TAKEOFF, newInstance(ChinookEvacuateState)( this ), TAKING_OFF, AI_IDLE );
  673. defineState( MOVE_TO_AND_EVAC_AND_EXIT, newInstance(AIMoveToState)( this ), LAND_AND_EVAC_AND_EXIT, AI_IDLE );
  674. defineState( LAND_AND_EVAC_AND_EXIT, newInstance(ChinookTakeoffOrLandingState)( this, true ), EVAC_AND_EXIT, AI_IDLE );
  675. defineState( EVAC_AND_EXIT, newInstance(ChinookEvacuateState)( this ), TAKEOFF_AND_EXIT, AI_IDLE );
  676. defineState( TAKEOFF_AND_EXIT, newInstance(ChinookTakeoffOrLandingState)( this, false ), HEAD_OFF_MAP, AI_IDLE );
  677. defineState( HEAD_OFF_MAP, newInstance(ChinookHeadOffMapState)( this ), AI_IDLE, AI_IDLE );
  678. }
  679. //-------------------------------------------------------------------------------------------------
  680. ChinookAIStateMachine::~ChinookAIStateMachine()
  681. {
  682. }
  683. //-------------------------------------------------------------------------------------------------
  684. //-------------------------------------------------------------------------------------------------
  685. //-------------------------------------------------------------------------------------------------
  686. //-------------------------------------------------------------------------------------------------
  687. ChinookAIUpdateModuleData::ChinookAIUpdateModuleData()
  688. {
  689. m_numRopes = 4;
  690. m_ropeWidth = 0.5f;
  691. m_ropeColor.red = 0.9f;
  692. m_ropeColor.green = 0.8f;
  693. m_ropeColor.blue = 0.7f;
  694. m_perRopeDelayMin = 0x7fffffff;
  695. m_perRopeDelayMax = 0x7fffffff;
  696. m_ropeName = "GenericRope";
  697. m_waitForRopesToDrop = true;
  698. m_minDropHeight = 30.0f;
  699. m_ropeFinalHeight = 0.0f;
  700. m_ropeDropSpeed = 1e10f; // um, fast.
  701. m_rappelSpeed = fabs(TheGlobalData->m_gravity) * LOGICFRAMES_PER_SECOND * 0.5f;
  702. m_ropeWobbleLen = 10.0f;
  703. m_ropeWobbleAmp = 1.0f;
  704. m_ropeWobbleRate = 0.1f;
  705. }
  706. //-------------------------------------------------------------------------------------------------
  707. /*static*/ void ChinookAIUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  708. {
  709. SupplyTruckAIUpdateModuleData::buildFieldParse(p);
  710. static const FieldParse dataFieldParse[] =
  711. {
  712. { "RappelSpeed", INI::parseVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_rappelSpeed) },
  713. { "RopeDropSpeed", INI::parseVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeDropSpeed) },
  714. { "RopeName", INI::parseAsciiString, 0, offsetof(ChinookAIUpdateModuleData, m_ropeName) },
  715. { "RopeFinalHeight", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeFinalHeight) },
  716. { "RopeWidth", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWidth) },
  717. { "RopeWobbleLen", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleLen) },
  718. { "RopeWobbleAmplitude", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleAmp) },
  719. { "RopeWobbleRate", INI::parseAngularVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleRate) },
  720. { "RopeColor", INI::parseRGBColor, 0, offsetof(ChinookAIUpdateModuleData, m_ropeColor) },
  721. { "NumRopes", INI::parseUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_numRopes) },
  722. { "PerRopeDelayMin", INI::parseDurationUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_perRopeDelayMin) },
  723. { "PerRopeDelayMax", INI::parseDurationUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_perRopeDelayMax) },
  724. { "MinDropHeight", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_minDropHeight) },
  725. { "WaitForRopesToDrop", INI::parseBool, 0, offsetof(ChinookAIUpdateModuleData, m_waitForRopesToDrop) },
  726. { 0, 0, 0, 0 }
  727. };
  728. p.add(dataFieldParse);
  729. }
  730. //-------------------------------------------------------------------------------------------------
  731. //-------------------------------------------------------------------------------------------------
  732. //-------------------------------------------------------------------------------------------------
  733. //-------------------------------------------------------------------------------------------------
  734. AIStateMachine* ChinookAIUpdate::makeStateMachine()
  735. {
  736. return newInstance(ChinookAIStateMachine)( getObject(), "ChinookAIStateMachine");
  737. }
  738. //-------------------------------------------------------------------------------------------------
  739. ChinookAIUpdate::ChinookAIUpdate( Thing *thing, const ModuleData* moduleData ) : SupplyTruckAIUpdate( thing, moduleData )
  740. {
  741. m_hasPendingCommand = false;
  742. m_flightStatus = CHINOOK_FLYING; // yep, that's right, even if we start "on ground"
  743. m_airfieldForHealing = INVALID_ID;
  744. }
  745. //-------------------------------------------------------------------------------------------------
  746. ChinookAIUpdate::~ChinookAIUpdate()
  747. {
  748. }
  749. //-------------------------------------------------------------------------------------------------
  750. static ParkingPlaceBehaviorInterface* getPP(ObjectID id)
  751. {
  752. Object* airfield = TheGameLogic->findObjectByID( id );
  753. if (airfield == NULL || airfield->isEffectivelyDead() || !airfield->isKindOf(KINDOF_AIRFIELD))
  754. return NULL;
  755. ParkingPlaceBehaviorInterface* pp = NULL;
  756. for (BehaviorModule** i = airfield->getBehaviorModules(); *i; ++i)
  757. {
  758. if ((pp = (*i)->getParkingPlaceBehaviorInterface()) != NULL)
  759. break;
  760. }
  761. return pp;
  762. }
  763. //-------------------------------------------------------------------------------------------------
  764. void ChinookAIUpdate::setAirfieldForHealing(ObjectID id)
  765. {
  766. // make sure we de-register with current one, if any
  767. if (m_airfieldForHealing != INVALID_ID && m_airfieldForHealing != id)
  768. {
  769. ParkingPlaceBehaviorInterface* pp = getPP(m_airfieldForHealing);
  770. if (pp != NULL)
  771. {
  772. pp->setHealee(getObject(), false);
  773. }
  774. }
  775. m_airfieldForHealing = id;
  776. }
  777. //-------------------------------------------------------------------------------------------------
  778. Bool ChinookAIUpdate::isIdle() const
  779. {
  780. // we need to do this because we enter an idle state briefly between takeoff/landing in these cases,
  781. // but scripting relies on us never claiming to be "idle"...
  782. if (m_hasPendingCommand)
  783. return false;
  784. Bool result = SupplyTruckAIUpdate::isIdle();
  785. if (result && m_flightStatus == CHINOOK_LANDED)
  786. {
  787. // ditto: if we are waiting to disgorge some folks, we aren't 'idle'
  788. ContainModuleInterface* contain = getObject()->getContain();
  789. if (contain && contain->hasObjectsWantingToEnterOrExit())
  790. result = false;
  791. }
  792. return result;
  793. }
  794. //-------------------------------------------------------------------------------------------------
  795. Bool ChinookAIUpdate::isCurrentlyFerryingSupplies() const
  796. {
  797. return SupplyTruckAIUpdate::isCurrentlyFerryingSupplies();
  798. }
  799. //-------------------------------------------------------------------------------------------------
  800. Bool ChinookAIUpdate::isAvailableForSupplying() const
  801. {
  802. if (!SupplyTruckAIUpdate::isAvailableForSupplying())
  803. return false;
  804. ContainModuleInterface* contain = getObject()->getContain();
  805. if( !contain || contain->hasObjectsWantingToEnterOrExit() || contain->getContainCount())
  806. return false;
  807. return true;
  808. }
  809. //-------------------------------------------------------------------------------------------------
  810. Bool ChinookAIUpdate::isAllowedToAdjustDestination() const
  811. {
  812. if (m_flightStatus == CHINOOK_LANDED)
  813. return false;
  814. return SupplyTruckAIUpdate::isAllowedToAdjustDestination();
  815. }
  816. //-------------------------------------------------------------------------------------------------
  817. ObjectID ChinookAIUpdate::getBuildingToNotPathAround() const
  818. {
  819. if (getAIStateType() == MOVE_TO_COMBAT_DROP || getAIStateType() == DO_COMBAT_DROP)
  820. {
  821. const Object* goalObj = getStateMachine()->getGoalObject();
  822. if (goalObj)
  823. return goalObj->getID();
  824. }
  825. return INVALID_ID;
  826. }
  827. //-------------------------------------------------------------------------------------------------
  828. AIFreeToExitType ChinookAIUpdate::getAiFreeToExit(const Object* exiter) const
  829. {
  830. if (m_flightStatus == CHINOOK_LANDED
  831. || (m_flightStatus == CHINOOK_DOING_COMBAT_DROP && exiter->isKindOf(KINDOF_CAN_RAPPEL)))
  832. return FREE_TO_EXIT;
  833. return WAIT_TO_EXIT;
  834. }
  835. //-------------------------------------------------------------------------------------------------
  836. Bool ChinookAIUpdate::chooseLocomotorSet(LocomotorSetType wst)
  837. {
  838. if (m_flightStatus == CHINOOK_LANDED)
  839. wst = LOCOMOTORSET_TAXIING;
  840. return SupplyTruckAIUpdate::chooseLocomotorSet(wst);
  841. }
  842. //-------------------------------------------------------------------------------------------------
  843. UpdateSleepTime ChinookAIUpdate::update()
  844. {
  845. ParkingPlaceBehaviorInterface* pp = getPP(m_airfieldForHealing);
  846. if (pp != NULL)
  847. {
  848. if (m_flightStatus == CHINOOK_LANDED &&
  849. !m_hasPendingCommand &&
  850. getObject()->getBodyModule()->getHealth() == getObject()->getBodyModule()->getMaxHealth())
  851. {
  852. // we're completely healed, so take off again
  853. pp->setHealee(getObject(), false);
  854. setMyState(TAKING_OFF, NULL, NULL, CMD_FROM_AI);
  855. }
  856. else
  857. {
  858. pp->setHealee(getObject(), m_flightStatus == CHINOOK_LANDED);
  859. }
  860. }
  861. else
  862. {
  863. setAirfieldForHealing(INVALID_ID);
  864. }
  865. // have to call our parent's isIdle, because we override it to never return true
  866. // when we have a pending command...
  867. if (SupplyTruckAIUpdate::isIdle())
  868. {
  869. ContainModuleInterface* contain = getObject()->getContain();
  870. if( contain )
  871. {
  872. Bool waitingToEnterOrExit = contain->hasObjectsWantingToEnterOrExit();
  873. if (m_hasPendingCommand)
  874. {
  875. AICommandParms parms(AICMD_MOVE_TO_POSITION, CMD_FROM_AI); // values don't matter, will be wiped by next line
  876. m_pendingCommand.reconstitute(parms);
  877. m_hasPendingCommand = false;
  878. aiDoCommand(&parms);
  879. }
  880. else if (waitingToEnterOrExit && m_flightStatus != CHINOOK_LANDED)
  881. {
  882. setMyState(LANDING, NULL, NULL, CMD_FROM_AI);
  883. }
  884. else if (!waitingToEnterOrExit && m_flightStatus == CHINOOK_LANDED && m_airfieldForHealing == INVALID_ID)
  885. {
  886. setMyState(TAKING_OFF, NULL, NULL, CMD_FROM_AI);
  887. }
  888. }
  889. }
  890. return SupplyTruckAIUpdate::update();
  891. }
  892. //-------------------------------------------------------------------------------------------------
  893. void ChinookAIUpdate::setMyState( StateID cmd, Object* target, const Coord3D* pos, CommandSourceType cmdSource )
  894. {
  895. getStateMachine()->clear();
  896. getStateMachine()->setGoalObject( target );
  897. setGoalPositionClipped(pos, cmdSource); // yeah, null is ok here.
  898. setLastCommandSource( cmdSource );
  899. getStateMachine()->setState( cmd );
  900. }
  901. //----------------------------------------------------------------------------------------
  902. /**
  903. * Get repaired at the repair depot
  904. */
  905. void ChinookAIUpdate::privateGetRepaired( Object *repairDepot, CommandSourceType cmdSource )
  906. {
  907. // we are already landing. just ignore it.
  908. if (m_flightStatus == CHINOOK_LANDING || m_flightStatus == CHINOOK_LANDED)
  909. return;
  910. // sanity, if we can't get repaired from here get out of here
  911. if( TheActionManager->canGetRepairedAt( getObject(), repairDepot, cmdSource ) == FALSE )
  912. return;
  913. setAirfieldForHealing(repairDepot->getID());
  914. Coord3D pos = *repairDepot->getPosition();
  915. Coord3D tmp;
  916. FindPositionOptions options;
  917. options.maxRadius = repairDepot->getGeometryInfo().getBoundingCircleRadius() * 100.0f;
  918. if (ThePartitionManager->findPositionAround(&pos, &options, &tmp))
  919. pos = tmp;
  920. setMyState(MOVE_TO_AND_LAND, NULL, &pos, cmdSource);
  921. }
  922. //-------------------------------------------------------------------------------------------------
  923. void ChinookAIUpdate::privateCombatDrop( Object* target, const Coord3D& pos, CommandSourceType cmdSource )
  924. {
  925. //
  926. // when there is a target present, we must verify that we can logically do the action when
  927. // we get commands from players (we'll assume AI knows what its doing)
  928. //
  929. if( target != NULL && cmdSource == CMD_FROM_PLAYER &&
  930. TheActionManager->canEnterObject( getObject(), target, cmdSource, COMBATDROP_INTO ) == FALSE )
  931. return;
  932. Coord3D localPos = pos;
  933. if (target == NULL)
  934. {
  935. // if target is null, we are dropping at a pos, not into a bldg.
  936. // in this case, ensure there is no structure at the pos... this can happen
  937. // if you combat-drop into a spot in the fog-of-war.
  938. Coord3D tmp;
  939. FindPositionOptions options;
  940. options.maxRadius = getObject()->getGeometryInfo().getBoundingCircleRadius() * 100.0f;
  941. if (ThePartitionManager->findPositionAround(&localPos, &options, &tmp))
  942. {
  943. localPos = tmp;
  944. }
  945. }
  946. // start the combat drop process
  947. setMyState(MOVE_TO_COMBAT_DROP, target, &localPos, cmdSource);
  948. }
  949. //-------------------------------------------------------------------------------------------------
  950. void ChinookAIUpdate::aiDoCommand(const AICommandParms* parms)
  951. {
  952. // this gets reset every time a command is issued.
  953. setAirfieldForHealing(INVALID_ID);
  954. if (!isAllowedToRespondToAiCommands(parms))
  955. return;
  956. if (m_flightStatus == CHINOOK_TAKING_OFF ||
  957. m_flightStatus == CHINOOK_LANDING ||
  958. m_flightStatus == CHINOOK_DOING_COMBAT_DROP)
  959. {
  960. // have to wait for takeoff or landing (or rappel) to complete, just store the sucker.
  961. m_pendingCommand.store(*parms);
  962. m_hasPendingCommand = true;
  963. return;
  964. }
  965. Bool passItThru = true;
  966. switch (parms->m_cmd)
  967. {
  968. case AICMD_IDLE:
  969. case AICMD_BUSY:
  970. case AICMD_FOLLOW_EXITPRODUCTION_PATH:
  971. {
  972. // don't need (or want) to take off for these...
  973. // just pass it thru.
  974. }
  975. break;
  976. case AICMD_MOVE_TO_POSITION_AND_EVACUATE:
  977. case AICMD_MOVE_TO_POSITION_AND_EVACUATE_AND_EXIT:
  978. {
  979. const Real THRESH = 3.0f;
  980. const Real THRESH_SQR = THRESH*THRESH;
  981. if (calcDistSqr(*getObject()->getPosition(), parms->m_pos) > THRESH_SQR &&
  982. m_flightStatus == CHINOOK_LANDED)
  983. {
  984. // gotta take off first!
  985. m_pendingCommand.store(*parms);
  986. m_hasPendingCommand = true;
  987. setMyState(TAKING_OFF, NULL, NULL, CMD_FROM_AI);
  988. passItThru = false;
  989. }
  990. else
  991. {
  992. // do this INSTEAD of the standard stuff
  993. setMyState(
  994. (parms->m_cmd == AICMD_MOVE_TO_POSITION_AND_EVACUATE) ? MOVE_TO_AND_EVAC : MOVE_TO_AND_EVAC_AND_EXIT,
  995. NULL, &parms->m_pos, CMD_FROM_AI);
  996. passItThru = false;
  997. }
  998. }
  999. break;
  1000. case AICMD_EXIT:
  1001. case AICMD_EVACUATE:
  1002. {
  1003. if (m_flightStatus != CHINOOK_LANDED)
  1004. {
  1005. // gotta land first!
  1006. m_pendingCommand.store(*parms);
  1007. m_hasPendingCommand = true;
  1008. setMyState(LANDING, NULL, NULL, CMD_FROM_AI);
  1009. passItThru = false;
  1010. }
  1011. }
  1012. break;
  1013. default:
  1014. {
  1015. if (m_flightStatus != CHINOOK_FLYING)
  1016. {
  1017. // gotta take off first!
  1018. m_pendingCommand.store(*parms);
  1019. m_hasPendingCommand = true;
  1020. setMyState(TAKING_OFF, NULL, NULL, CMD_FROM_AI);
  1021. passItThru = false;
  1022. }
  1023. }
  1024. break;
  1025. }
  1026. if (passItThru)
  1027. {
  1028. m_hasPendingCommand = false;
  1029. SupplyTruckAIUpdate::aiDoCommand(parms);
  1030. }
  1031. }
  1032. // ------------------------------------------------------------------------------------------------
  1033. /** CRC */
  1034. // ------------------------------------------------------------------------------------------------
  1035. void ChinookAIUpdate::crc( Xfer *xfer )
  1036. {
  1037. SupplyTruckAIUpdate::crc(xfer);
  1038. } // end crc
  1039. // ------------------------------------------------------------------------------------------------
  1040. /** Xfer method
  1041. * Version Info:
  1042. * 1: Initial version */
  1043. // ------------------------------------------------------------------------------------------------
  1044. void ChinookAIUpdate::xfer( Xfer *xfer )
  1045. {
  1046. // version
  1047. XferVersion currentVersion = 1;
  1048. XferVersion version = currentVersion;
  1049. xfer->xferVersion( &version, currentVersion );
  1050. SupplyTruckAIUpdate::xfer(xfer);
  1051. xfer->xferBool(&m_hasPendingCommand);
  1052. if (m_hasPendingCommand) {
  1053. m_pendingCommand.doXfer(xfer);
  1054. }
  1055. xfer->xferUser(&m_flightStatus, sizeof(m_flightStatus));
  1056. xfer->xferObjectID(&m_airfieldForHealing);
  1057. // extend base class
  1058. } // end xfer
  1059. // ------------------------------------------------------------------------------------------------
  1060. /** Load post process */
  1061. // ------------------------------------------------------------------------------------------------
  1062. void ChinookAIUpdate::loadPostProcess( void )
  1063. {
  1064. SupplyTruckAIUpdate::loadPostProcess();
  1065. } // end loadPostProcess