AIGuardRetaliate.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: AIGuardRetaliate.h
  24. /*---------------------------------------------------------------------------*/
  25. /* Electronic Arts Los Angeles */
  26. /* Confidential Information */
  27. /* Copyright (C) 2003 - All Rights Reserved */
  28. /* DO NOT DISTRIBUTE */
  29. /*---------------------------------------------------------------------------*/
  30. /* Project: RTS3 */
  31. /* File name: AIGuardRetaliate.h */
  32. /* Created: Kris Morness July 2003 */
  33. /* Desc: // Define Guard Retaliation states for AI */
  34. /*---------------------------------------------------------------------------*/
  35. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  36. #include "Common/PerfTimer.h"
  37. #include "Common/Team.h"
  38. #include "Common/Xfer.h"
  39. #include "Common/ThingTemplate.h"
  40. #include "GameLogic/AI.h"
  41. #include "GameLogic/AIPathfind.h"
  42. #include "GameLogic/AIGuardRetaliate.h"
  43. #include "GameLogic/Module/AIUpdate.h"
  44. #include "GameLogic/Module/BodyModule.h"
  45. #include "GameLogic/Module/CollideModule.h"
  46. #include "GameLogic/Object.h"
  47. #include "GameLogic/PartitionManager.h"
  48. #include "GameLogic/PolygonTrigger.h"
  49. const Real CLOSE_ENOUGH = (25.0f);
  50. #ifdef _INTERNAL
  51. // for occasional debugging...
  52. //#pragma optimize("", off)
  53. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  54. #endif
  55. static Bool hasAttackedMeAndICanReturnFire( State *thisState, void* /*userData*/ )
  56. {
  57. Object *obj = thisState->getMachineOwner();
  58. BodyModuleInterface *bmi = obj ? obj->getBodyModule() : NULL;
  59. if (!(obj && bmi)) {
  60. return FALSE;
  61. }
  62. if (bmi->getClearableLastAttacker() == INVALID_ID) {
  63. return FALSE;
  64. }
  65. // K. It appears we have a valid aggressor. Find it, and determine if we can attack it, etc.
  66. Object *target = TheGameLogic->findObjectByID(bmi->getClearableLastAttacker());
  67. bmi->clearLastAttacker();
  68. // We use the clearable last attacker because we should continue attacking the guy. But if he
  69. // stops attacking us, then we want our timer to kick us off of him and make us go attack
  70. // other units instead.
  71. if (!target) {
  72. return FALSE;
  73. }
  74. if (obj->getRelationship(target) != ENEMIES) {
  75. return FALSE;
  76. }
  77. // This is a quick test on the target. It will be duplicated in getAbleToAttackSpecificObject,
  78. // but the payoff is worth the duplication.
  79. if (target->isEffectivelyDead()) {
  80. return FALSE;
  81. }
  82. //@todo: Get this out of here. Move it into the declaration of calling this function, or figure
  83. // out some way to call it less often.
  84. if (!obj->isAbleToAttack()) {
  85. return FALSE;
  86. }
  87. CanAttackResult result = obj->getAbleToAttackSpecificObject(ATTACK_NEW_TARGET, target, CMD_FROM_AI);
  88. if( result == ATTACKRESULT_POSSIBLE || result == ATTACKRESULT_POSSIBLE_AFTER_MOVING )
  89. {
  90. return TRUE;
  91. }
  92. return FALSE;
  93. }
  94. //-- ExitConditions -------------------------------------------------------------------------------
  95. /**
  96. * This returns true if the conditions specified have been met, false otherwise.
  97. */
  98. Bool GuardRetaliateExitConditions::shouldExit(const StateMachine* machine) const
  99. {
  100. if (!machine->getGoalObject())
  101. {
  102. if (m_conditionsToConsider & ATTACK_ExitIfNoUnitFound)
  103. {
  104. return true;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110. }
  111. if (m_conditionsToConsider & ATTACK_ExitIfExpiredDuration)
  112. {
  113. if (TheGameLogic->getFrame() >= m_attackGiveUpFrame)
  114. {
  115. return true;
  116. }
  117. }
  118. if (m_conditionsToConsider & ATTACK_ExitIfOutsideRadius)
  119. {
  120. Coord3D deltaAggressor, myRange;
  121. Coord3D objPos = *machine->getGoalObject()->getPosition();
  122. Coord3D myPos = *machine->getOwner()->getPosition();
  123. deltaAggressor.x = objPos.x - m_center.x;
  124. deltaAggressor.y = objPos.y - m_center.y;
  125. deltaAggressor.z = 0; // BGC - when we search for a target we don't account for Z, so why should we here?
  126. // changing this fixed a crash where a GLARebelInfantry would be in GuardReturnState, find
  127. // a target that is within range, then not be able to attack because its actually out of range.
  128. // then it would look for a new target, get the same one, and proceed in an infinite recursive
  129. // loop that eventually blew the stack.
  130. Real guardRangeSqr = sqr( AIGuardRetaliateMachine::getStdGuardRange( machine->getOwner() ) );
  131. myRange.x = myPos.x - m_center.x;
  132. myRange.y = myPos.y - m_center.y;
  133. myRange.z = 0;
  134. if( deltaAggressor.lengthSqr() > m_radiusSqr )
  135. {
  136. //The aggressor is too far away now... give up retaliation.
  137. return TRUE;
  138. }
  139. if( myRange.lengthSqr() > guardRangeSqr )
  140. {
  141. //Never go beyond standard guard radius.
  142. return TRUE;
  143. }
  144. }
  145. return FALSE;
  146. }
  147. //-- AIGuardRetaliateMachine -------------------------------------------------------------------------------
  148. //--------------------------------------------------------------------------------------
  149. AIGuardRetaliateMachine::AIGuardRetaliateMachine( Object *owner ) :
  150. StateMachine(owner, "AIGuardRetaliateMachine"),
  151. m_nemesisToAttack(INVALID_ID)
  152. {
  153. m_positionToGuard.zero();
  154. static const StateConditionInfo attackAggressors[] =
  155. {
  156. StateConditionInfo(hasAttackedMeAndICanReturnFire, AI_GUARD_RETALIATE_ATTACK_AGGRESSOR, NULL),
  157. StateConditionInfo(NULL, NULL, NULL) // keep last
  158. };
  159. // order matters: first state is the default state.
  160. // srj sez: I made "return" the start state, so that if ordered to guard a position
  161. // that isn't the unit's current position, it moves to that position first.
  162. defineState( AI_GUARD_RETALIATE_ATTACK_AGGRESSOR, newInstance(AIGuardRetaliateAttackAggressorState)( this ), AI_GUARD_RETALIATE_RETURN, AI_GUARD_RETALIATE_RETURN );
  163. defineState( AI_GUARD_RETALIATE_RETURN, newInstance(AIGuardRetaliateReturnState)( this ), AI_GUARD_RETALIATE_IDLE, AI_GUARD_RETALIATE_INNER, attackAggressors );
  164. defineState( AI_GUARD_RETALIATE_IDLE, newInstance(AIGuardRetaliateIdleState)( this ), AI_GUARD_RETALIATE_INNER, EXIT_MACHINE_WITH_SUCCESS, attackAggressors );
  165. defineState( AI_GUARD_RETALIATE_INNER, newInstance(AIGuardRetaliateInnerState)( this ), AI_GUARD_RETALIATE_OUTER, AI_GUARD_RETALIATE_OUTER );
  166. defineState( AI_GUARD_RETALIATE_OUTER, newInstance(AIGuardRetaliateOuterState)( this ), AI_GUARD_RETALIATE_GET_CRATE, AI_GUARD_RETALIATE_GET_CRATE );
  167. defineState( AI_GUARD_RETALIATE_GET_CRATE, newInstance(AIGuardRetaliatePickUpCrateState)( this ), AI_GUARD_RETALIATE_RETURN, AI_GUARD_RETALIATE_RETURN );
  168. }
  169. //--------------------------------------------------------------------------------------
  170. AIGuardRetaliateMachine::~AIGuardRetaliateMachine()
  171. {
  172. }
  173. Bool AIGuardRetaliateMachine::isIdle() const
  174. {
  175. if( getCurrentStateID() == AI_IDLE )
  176. {
  177. return TRUE;
  178. }
  179. return FALSE;
  180. }
  181. //--------------------------------------------------------------------------------------
  182. /*static*/ Real AIGuardRetaliateMachine::getStdGuardRange(const Object* obj)
  183. {
  184. Real visionRange = TheAI->getAdjustedVisionRangeForObject(obj,
  185. AI_VISIONFACTOR_OWNERTYPE | AI_VISIONFACTOR_MOOD | AI_VISIONFACTOR_GUARDINNER);
  186. return visionRange;
  187. }
  188. //--------------------------------------------------------------------------------------
  189. Bool AIGuardRetaliateMachine::lookForInnerTarget(void)
  190. {
  191. Object* owner = getOwner();
  192. if (!owner->isAbleToAttack())
  193. {
  194. return false; // my, that was easy
  195. }
  196. // Check if team auto targets same victim.
  197. Object *teamVictim = NULL;
  198. if (owner->getTeam()->getPrototype()->getTemplateInfo()->m_attackCommonTarget)
  199. {
  200. teamVictim = owner->getTeam()->getTeamTargetObject();
  201. if (teamVictim)
  202. {
  203. setNemesisID(teamVictim->getID());
  204. return true; // Transitions to AIGuardRetaliateInnerState.
  205. }
  206. }
  207. PartitionFilterRelationship f1(owner, PartitionFilterRelationship::ALLOW_ENEMIES);
  208. PartitionFilterPossibleToAttack f2(ATTACK_NEW_TARGET, owner, CMD_FROM_AI);
  209. PartitionFilterSameMapStatus filterMapStatus(owner);
  210. PartitionFilterRelationship f5(owner, PartitionFilterRelationship::ALLOW_NEUTRAL);
  211. PartitionFilterPossibleToEnter f6(owner, CMD_FROM_AI);
  212. PartitionFilterPossibleToHijack f7(owner, CMD_FROM_AI);
  213. PartitionFilterRejectBuildings f8( owner );
  214. PartitionFilter *filters[16];
  215. Int count = 0;
  216. // Enter Guard state
  217. if (owner->getTemplate()->isEnterGuard())
  218. {
  219. filters[count++] = &f6;
  220. // Hijack Guard state
  221. if (owner->getTemplate()->isHijackGuard())
  222. {
  223. filters[count++] = &f1;
  224. filters[count++] = &f7;
  225. }
  226. else
  227. {
  228. filters[count++] = &f5;
  229. }
  230. }
  231. // Attack Guard state
  232. else
  233. {
  234. filters[count++] = &f1;
  235. filters[count++] = &f2;
  236. filters[count++] = &f8; //Different than guard... we won't allow acquiring of structures (unless base defenses)
  237. }
  238. filters[count++] = &filterMapStatus;
  239. Real visionRange = AIGuardRetaliateMachine::getStdGuardRange(owner);
  240. filters[count++] = NULL;
  241. // SimpleObjectIterator* iter = ThePartitionManager->iterateObjectsInRange(
  242. // &pos, visionRange, FROM_CENTER_2D, filters, ITER_SORTED_NEAR_TO_FAR);
  243. // MemoryPoolObjectHolder hold(iter);
  244. // Object* target = iter->first();
  245. //
  246. // srj sez: the above code is stupid and slow. since we only want the closest object,
  247. // just ask for that; the above has to find ALL objects in range, but we ignore all
  248. // but the first (closest).
  249. //
  250. const Coord3D *pos = getPositionToGuard();
  251. Object* target = ThePartitionManager->getClosestObject(pos, visionRange, FROM_CENTER_2D, filters);
  252. if (target)
  253. {
  254. setNemesisID(target->getID());
  255. return true; // Transitions to AIGuardRetaliateInnerState.
  256. }
  257. else
  258. {
  259. return false;
  260. }
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. /** CRC */
  264. // ------------------------------------------------------------------------------------------------
  265. void AIGuardRetaliateMachine::crc( Xfer *xfer )
  266. {
  267. } // end crc
  268. // ------------------------------------------------------------------------------------------------
  269. /** Xfer Method */
  270. // ------------------------------------------------------------------------------------------------
  271. void AIGuardRetaliateMachine::xfer( Xfer *xfer )
  272. {
  273. // version
  274. XferVersion currentVersion = 2;
  275. XferVersion version = currentVersion;
  276. xfer->xferVersion( &version, currentVersion );
  277. if (version>=2) {
  278. StateMachine::xfer(xfer); // Forgot this in initial implementation. jba.
  279. }
  280. xfer->xferObjectID(&m_nemesisToAttack);
  281. xfer->xferCoord3D(&m_positionToGuard);
  282. } // end xfer
  283. // ------------------------------------------------------------------------------------------------
  284. /** Load post process */
  285. // ------------------------------------------------------------------------------------------------
  286. void AIGuardRetaliateMachine::loadPostProcess( void )
  287. {
  288. } // end loadPostProcess
  289. //-- AIGuardRetaliateInnerState ----------------------------------------------------------------------------
  290. // ------------------------------------------------------------------------------------------------
  291. /** CRC */
  292. // ------------------------------------------------------------------------------------------------
  293. void AIGuardRetaliateInnerState::crc( Xfer *xfer )
  294. {
  295. } // end crc
  296. // ------------------------------------------------------------------------------------------------
  297. /** Xfer Method */
  298. // ------------------------------------------------------------------------------------------------
  299. void AIGuardRetaliateInnerState::xfer( Xfer *xfer )
  300. {
  301. // version
  302. XferVersion currentVersion = 1;
  303. XferVersion version = currentVersion;
  304. xfer->xferVersion( &version, currentVersion );
  305. } // end xfer
  306. // ------------------------------------------------------------------------------------------------
  307. /** Load post process */
  308. // ------------------------------------------------------------------------------------------------
  309. void AIGuardRetaliateInnerState::loadPostProcess( void )
  310. {
  311. onEnter();
  312. } // end loadPostProcess
  313. //--------------------------------------------------------------------------------------
  314. StateReturnType AIGuardRetaliateInnerState::onEnter( void )
  315. {
  316. // See if we try to enter the target
  317. if (getMachineOwner()->getTemplate()->isEnterGuard())
  318. {
  319. Object* nemesis = TheGameLogic->findObjectByID(getGuardMachine()->getNemesisID()) ;
  320. if (nemesis == NULL)
  321. {
  322. DEBUG_LOG(("Unexpected NULL nemesis in AIGuardRetaliateInnerState.\n"));
  323. return STATE_SUCCESS;
  324. }
  325. m_enterState = newInstance(AIEnterState)(getMachine());
  326. m_enterState->getMachine()->setGoalObject(nemesis);
  327. StateReturnType returnVal = m_enterState->onEnter();
  328. if (returnVal == STATE_CONTINUE) {
  329. return STATE_CONTINUE;
  330. }
  331. }
  332. // Or try to destroy the target
  333. else
  334. {
  335. Coord3D pos = *getGuardMachine()->getPositionToGuard();
  336. Object* nemesis = TheGameLogic->findObjectByID(getGuardMachine()->getNemesisID()) ;
  337. if (nemesis == NULL)
  338. {
  339. DEBUG_LOG(("Unexpected NULL nemesis in AIGuardRetaliateInnerState.\n"));
  340. return STATE_SUCCESS;
  341. }
  342. m_exitConditions.m_center = pos;
  343. m_exitConditions.m_radiusSqr = sqr( 1.5f * AIGuardRetaliateMachine::getStdGuardRange( getMachineOwner() ) );
  344. m_exitConditions.m_conditionsToConsider = (GuardRetaliateExitConditions::ATTACK_ExitIfOutsideRadius |
  345. GuardRetaliateExitConditions::ATTACK_ExitIfNoUnitFound);
  346. m_attackState = newInstance(AIAttackState)(getMachine(), false, true, false, &m_exitConditions);
  347. m_attackState->getMachine()->setGoalObject(nemesis);
  348. StateReturnType returnVal = m_attackState->onEnter();
  349. if (returnVal == STATE_CONTINUE) {
  350. return STATE_CONTINUE;
  351. }
  352. }
  353. // if we had no one to attack, we were successful, so go to the next state.
  354. return STATE_SUCCESS;
  355. }
  356. //--------------------------------------------------------------------------------------
  357. StateReturnType AIGuardRetaliateInnerState::update( void )
  358. {
  359. if (m_attackState)
  360. {
  361. return m_attackState->update();
  362. }
  363. else if (m_enterState)
  364. {
  365. return m_enterState->update();
  366. }
  367. return STATE_SUCCESS;
  368. }
  369. //--------------------------------------------------------------------------------------
  370. void AIGuardRetaliateInnerState::onExit( StateExitType status )
  371. {
  372. Object *obj = getMachineOwner();
  373. if (m_attackState)
  374. {
  375. m_attackState->onExit(status);
  376. m_attackState->deleteInstance();
  377. m_attackState = NULL;
  378. }
  379. else if (m_enterState)
  380. {
  381. m_enterState->onExit(status);
  382. m_enterState->deleteInstance();
  383. m_enterState = NULL;
  384. }
  385. if (obj->getTeam())
  386. {
  387. obj->getTeam()->setTeamTargetObject(NULL); // clear the target.
  388. }
  389. }
  390. //-- AIGuardRetaliateOuterState ----------------------------------------------------------------------------
  391. // ------------------------------------------------------------------------------------------------
  392. /** CRC */
  393. // ------------------------------------------------------------------------------------------------
  394. void AIGuardRetaliateOuterState::crc( Xfer *xfer )
  395. {
  396. } // end crc
  397. // ------------------------------------------------------------------------------------------------
  398. /** Xfer Method */
  399. // ------------------------------------------------------------------------------------------------
  400. void AIGuardRetaliateOuterState::xfer( Xfer *xfer )
  401. {
  402. // version
  403. XferVersion currentVersion = 1;
  404. XferVersion version = currentVersion;
  405. xfer->xferVersion( &version, currentVersion );
  406. } // end xfer
  407. // ------------------------------------------------------------------------------------------------
  408. /** Load post process */
  409. // ------------------------------------------------------------------------------------------------
  410. void AIGuardRetaliateOuterState::loadPostProcess( void )
  411. { AIGuardRetaliateOuterState
  412. onEnter();
  413. } // end loadPostProcess
  414. //--------------------------------------------------------------------------------------
  415. StateReturnType AIGuardRetaliateOuterState::onEnter( void )
  416. {
  417. //if (getGuardMachine()->getGuardMode() == GUARDMODE_GUARD_WITHOUT_PURSUIT)
  418. //{
  419. // // "patrol" mode does not follow targets outside the guard area.
  420. // return STATE_SUCCESS;
  421. //}
  422. Coord3D pos = *getGuardMachine()->getPositionToGuard();
  423. Object* nemesis = TheGameLogic->findObjectByID(getGuardMachine()->getNemesisID()) ;
  424. if (nemesis == NULL)
  425. {
  426. DEBUG_LOG(("Unexpected NULL nemesis in AIGuardRetaliateOuterState.\n"));
  427. return STATE_SUCCESS;
  428. }
  429. Object *obj = getMachineOwner();
  430. Real range = TheAI->getAdjustedVisionRangeForObject(obj, AI_VISIONFACTOR_OWNERTYPE | AI_VISIONFACTOR_MOOD);
  431. m_exitConditions.m_center = pos;
  432. m_exitConditions.m_radiusSqr = sqr( 0.67f * (range + AIGuardRetaliateMachine::getStdGuardRange( getMachineOwner() )) );
  433. m_exitConditions.m_attackGiveUpFrame = TheGameLogic->getFrame() + TheAI->getAiData()->m_guardChaseUnitFrames;
  434. m_exitConditions.m_conditionsToConsider = (GuardRetaliateExitConditions::ATTACK_ExitIfExpiredDuration |
  435. GuardRetaliateExitConditions::ATTACK_ExitIfOutsideRadius |
  436. GuardRetaliateExitConditions::ATTACK_ExitIfNoUnitFound);
  437. m_attackState = newInstance(AIAttackState)(getMachine(), false, true, false, &m_exitConditions);
  438. m_attackState->getMachine()->setGoalObject(nemesis);
  439. StateReturnType returnVal = m_attackState->onEnter();
  440. if (returnVal == STATE_CONTINUE) {
  441. return STATE_CONTINUE;
  442. }
  443. // if we had no one to attack, we were successful, so go to the next state.
  444. return STATE_SUCCESS;
  445. }
  446. //--------------------------------------------------------------------------------------
  447. StateReturnType AIGuardRetaliateOuterState::update( void )
  448. {
  449. if (m_attackState==NULL) return STATE_SUCCESS;
  450. Object* goalObj = m_attackState->getMachineGoalObject();
  451. if (goalObj)
  452. {
  453. Coord3D deltaAggr;
  454. deltaAggr.x = m_exitConditions.m_center.x - goalObj->getPosition()->x;
  455. deltaAggr.y = m_exitConditions.m_center.y - goalObj->getPosition()->y;
  456. deltaAggr.z = m_exitConditions.m_center.z - goalObj->getPosition()->z;
  457. Real visionSqr = sqr(AIGuardRetaliateMachine::getStdGuardRange(getMachineOwner()));
  458. if (deltaAggr.lengthSqr() <= visionSqr)
  459. {
  460. // reset the counter
  461. m_exitConditions.m_attackGiveUpFrame = TheGameLogic->getFrame() + TheAI->getAiData()->m_guardChaseUnitFrames;
  462. }
  463. }
  464. return m_attackState->update();
  465. }
  466. //--------------------------------------------------------------------------------------
  467. void AIGuardRetaliateOuterState::onExit( StateExitType status )
  468. {
  469. if (m_attackState)
  470. {
  471. m_attackState->onExit(status);
  472. m_attackState->deleteInstance();
  473. m_attackState = NULL;
  474. }
  475. }
  476. //-- AIGuardRetaliateReturnState ----------------------------------------------------------------------------
  477. // ------------------------------------------------------------------------------------------------
  478. /** CRC */
  479. // ------------------------------------------------------------------------------------------------
  480. void AIGuardRetaliateReturnState::crc( Xfer *xfer )
  481. {
  482. } // end crc
  483. // ------------------------------------------------------------------------------------------------
  484. /** Xfer Method */
  485. // ------------------------------------------------------------------------------------------------
  486. void AIGuardRetaliateReturnState::xfer( Xfer *xfer )
  487. {
  488. // version
  489. XferVersion currentVersion = 1;
  490. XferVersion version = currentVersion;
  491. xfer->xferVersion( &version, currentVersion );
  492. xfer->xferUnsignedInt(&m_nextReturnScanTime);
  493. } // end xfer
  494. // ------------------------------------------------------------------------------------------------
  495. /** Load post process */
  496. // ------------------------------------------------------------------------------------------------
  497. void AIGuardRetaliateReturnState::loadPostProcess( void )
  498. {
  499. } // end loadPostProcess
  500. //--------------------------------------------------------------------------------------
  501. StateReturnType AIGuardRetaliateReturnState::onEnter( void )
  502. {
  503. UnsignedInt now = TheGameLogic->getFrame();
  504. m_nextReturnScanTime = now + GameLogicRandomValue(0, TheAI->getAiData()->m_guardEnemyReturnScanRate);
  505. // no, no, no, don't do this in onEnter, unless you like really slow maps. (srj)
  506. // if (getGuardMachine()->lookForInnerTarget())
  507. // return STATE_FAILURE; // early termination because we found a target.
  508. m_goalPosition = *getGuardMachine()->getPositionToGuard();
  509. AIUpdateInterface *ai = getMachineOwner()->getAIUpdateInterface();
  510. if (ai && ai->isDoingGroundMovement())
  511. {
  512. TheAI->pathfinder()->adjustDestination(getMachineOwner(), ai->getLocomotorSet(), &m_goalPosition);
  513. }
  514. setAdjustsDestination(true);
  515. return AIInternalMoveToState::onEnter();
  516. }
  517. //--------------------------------------------------------------------------------------
  518. StateReturnType AIGuardRetaliateReturnState::update( void )
  519. {
  520. UnsignedInt now = TheGameLogic->getFrame();
  521. if (now >= m_nextReturnScanTime)
  522. {
  523. m_nextReturnScanTime = now + TheAI->getAiData()->m_guardEnemyReturnScanRate;
  524. if (getGuardMachine()->lookForInnerTarget())
  525. return STATE_FAILURE; // early termination because we found a target.
  526. }
  527. // Just let the return movement finish.
  528. return AIInternalMoveToState::update();
  529. }
  530. //--------------------------------------------------------------------------------------
  531. void AIGuardRetaliateReturnState::onExit( StateExitType status )
  532. {
  533. AIInternalMoveToState::onExit( status );
  534. }
  535. //-- AIGuardRetaliateIdleState ----------------------------------------------------------------------------
  536. // ------------------------------------------------------------------------------------------------
  537. /** CRC */
  538. // ------------------------------------------------------------------------------------------------
  539. void AIGuardRetaliateIdleState::crc( Xfer *xfer )
  540. {
  541. } // end crc
  542. // ------------------------------------------------------------------------------------------------
  543. /** Xfer Method */
  544. // ------------------------------------------------------------------------------------------------
  545. void AIGuardRetaliateIdleState::xfer( Xfer *xfer )
  546. {
  547. // version
  548. XferVersion currentVersion = 1;
  549. XferVersion version = currentVersion;
  550. xfer->xferVersion( &version, currentVersion );
  551. xfer->xferUnsignedInt(&m_nextEnemyScanTime);
  552. } // end xfer
  553. // ------------------------------------------------------------------------------------------------
  554. /** Load post process */
  555. // ------------------------------------------------------------------------------------------------
  556. void AIGuardRetaliateIdleState::loadPostProcess( void )
  557. {
  558. } // end loadPostProcess
  559. //--------------------------------------------------------------------------------------
  560. StateReturnType AIGuardRetaliateIdleState::onEnter( void )
  561. {
  562. // first time thru, use a random amount so that everyone doesn't scan on the same frame,
  563. // to avoid "spikes".
  564. UnsignedInt now = TheGameLogic->getFrame();
  565. m_nextEnemyScanTime = now + GameLogicRandomValue(0, TheAI->getAiData()->m_guardEnemyScanRate);
  566. return STATE_CONTINUE;
  567. }
  568. //--------------------------------------------------------------------------------------
  569. StateReturnType AIGuardRetaliateIdleState::update( void )
  570. {
  571. //DEBUG_LOG(("AIGuardRetaliateIdleState frame %d: %08lx\n",TheGameLogic->getFrame(),getMachineOwner()));
  572. UnsignedInt now = TheGameLogic->getFrame();
  573. if (now < m_nextEnemyScanTime)
  574. return STATE_SLEEP(m_nextEnemyScanTime - now);
  575. m_nextEnemyScanTime = now + TheAI->getAiData()->m_guardEnemyScanRate;
  576. #ifdef STATE_MACHINE_DEBUG
  577. //getMachine()->setDebugOutput(true);
  578. #endif
  579. Object *owner = getMachineOwner();
  580. AIUpdateInterface *ai = owner->getAIUpdateInterface();
  581. // Check to see if we have created a crate we need to pick up.
  582. if (ai->getCrateID() != INVALID_ID)
  583. {
  584. getMachine()->setState(AI_GUARD_RETALIATE_GET_CRATE);
  585. return STATE_SLEEP(m_nextEnemyScanTime - now);
  586. }
  587. // if anyone is in the inner area, return success.
  588. if (getGuardMachine()->lookForInnerTarget())
  589. {
  590. return STATE_SUCCESS; // Transitions to AIGuardRetaliateInnerState.
  591. }
  592. else
  593. {
  594. return STATE_FAILURE;
  595. }
  596. return STATE_SLEEP(m_nextEnemyScanTime - now);
  597. }
  598. //--------------------------------------------------------------------------------------
  599. void AIGuardRetaliateIdleState::onExit( StateExitType status )
  600. {
  601. }
  602. //-- AIGuardRetaliatePickUpCrateState ----------------------------------------------------------------------
  603. //-------------------------------------------------------------------------------------------------
  604. AIGuardRetaliatePickUpCrateState::AIGuardRetaliatePickUpCrateState( StateMachine *machine ) : AIPickUpCrateState(machine)
  605. {
  606. #ifdef STATE_MACHINE_DEBUG
  607. setName("AIGuardRetaliatePickUpCrateState");
  608. #endif
  609. }
  610. //--------------------------------------------------------------------------------------
  611. StateReturnType AIGuardRetaliatePickUpCrateState::onEnter( void )
  612. {
  613. Object *owner = getMachineOwner();
  614. AIUpdateInterface *ai = owner->getAIUpdateInterface();
  615. // Check to see if we have created a crate we need to pick up.
  616. Object* crate = ai->checkForCrateToPickup();
  617. if (crate)
  618. {
  619. getMachine()->setGoalObject(crate);
  620. return AIPickUpCrateState::onEnter();
  621. }
  622. return STATE_SUCCESS; // no crate, so we're done.
  623. }
  624. //--------------------------------------------------------------------------------------
  625. StateReturnType AIGuardRetaliatePickUpCrateState::update( void )
  626. {
  627. return AIPickUpCrateState::update();
  628. }
  629. //--------------------------------------------------------------------------------------
  630. void AIGuardRetaliatePickUpCrateState::onExit( StateExitType status )
  631. {
  632. }
  633. //-- AIGuardRetaliateAttackAggressorState ------------------------------------------------------------------
  634. //-------------------------------------------------------------------------------------------------
  635. AIGuardRetaliateAttackAggressorState::AIGuardRetaliateAttackAggressorState( StateMachine *machine ) :
  636. State( machine, "AIGuardRetaliateAttackAggressorState" )
  637. {
  638. m_attackState = NULL;
  639. }
  640. #ifdef STATE_MACHINE_DEBUG
  641. //----------------------------------------------------------------------------------------------------------
  642. AsciiString AIGuardRetaliateAttackAggressorState::getName( ) const
  643. {
  644. AsciiString name = m_name;
  645. name.concat("/ ");
  646. if (m_attackState) name.concat(m_attackState->getName());
  647. else name.concat("*NULL m_attackState");
  648. return name;
  649. }
  650. #endif
  651. //-------------------------------------------------------------------------------------------------
  652. StateReturnType AIGuardRetaliateAttackAggressorState::onEnter( void )
  653. {
  654. Object *obj = getMachineOwner();
  655. ObjectID nemID = INVALID_ID;
  656. //Get the specified nemesis. If he doesn't exist... look at the object that damaged
  657. //us last.
  658. Object *nemesis = TheGameLogic->findObjectByID( getGuardMachine()->getNemesisID() );
  659. BodyModuleInterface *body = obj->getBodyModule();
  660. if( !nemesis && body && body->getLastDamageInfo()->in.m_sourceID && body->getLastDamageInfo()->in.m_damageType != DAMAGE_HEALING )
  661. {
  662. nemID = obj->getBodyModule()->getLastDamageInfo()->in.m_sourceID;
  663. nemesis = TheGameLogic->findObjectByID( nemID );
  664. if( nemesis && obj->getRelationship( nemesis ) == ENEMIES )
  665. {
  666. getGuardMachine()->setNemesisID(nemID);
  667. }
  668. }
  669. if( !nemesis )
  670. {
  671. DEBUG_LOG(("Unexpected NULL nemesis in AIGuardRetaliateAttackAggressorState.\n"));
  672. return STATE_SUCCESS;
  673. }
  674. Coord3D pos = *getGuardMachine()->getPositionToGuard();
  675. Real range = TheAI->getAdjustedVisionRangeForObject(obj, AI_VISIONFACTOR_OWNERTYPE | AI_VISIONFACTOR_MOOD);
  676. m_exitConditions.m_center = pos;
  677. m_exitConditions.m_attackGiveUpFrame = TheGameLogic->getFrame() + TheAI->getAiData()->m_guardChaseUnitFrames;
  678. m_exitConditions.m_radiusSqr = sqr( range + AIGuardRetaliateMachine::getStdGuardRange( obj ) );
  679. m_exitConditions.m_conditionsToConsider = ( GuardRetaliateExitConditions::ATTACK_ExitIfExpiredDuration |
  680. GuardRetaliateExitConditions::ATTACK_ExitIfOutsideRadius |
  681. GuardRetaliateExitConditions::ATTACK_ExitIfNoUnitFound );
  682. m_attackState = newInstance(AIAttackState)(getMachine(), false, true, false, &m_exitConditions);
  683. m_attackState->getMachine()->setGoalObject(nemesis);
  684. StateReturnType returnVal = m_attackState->onEnter();
  685. if (returnVal == STATE_CONTINUE) {
  686. return STATE_CONTINUE;
  687. }
  688. // if we had no one to attack, we were successful, so go to the next state.
  689. return STATE_SUCCESS;
  690. }
  691. //-------------------------------------------------------------------------------------------------
  692. StateReturnType AIGuardRetaliateAttackAggressorState::update( void )
  693. {
  694. if (m_attackState==NULL) return STATE_SUCCESS;
  695. return m_attackState->update();
  696. }
  697. //-------------------------------------------------------------------------------------------------
  698. void AIGuardRetaliateAttackAggressorState::onExit( StateExitType status )
  699. {
  700. Object *obj = getMachineOwner();
  701. if (m_attackState)
  702. {
  703. m_attackState->onExit(status);
  704. m_attackState->deleteInstance();
  705. m_attackState = NULL;
  706. }
  707. if (obj->getTeam())
  708. {
  709. obj->getTeam()->setTeamTargetObject(NULL); // clear the target.
  710. }
  711. }
  712. //-------------------------------------------------------------------------------------------------
  713. void AIGuardRetaliateAttackAggressorState::crc( Xfer *xfer )
  714. {
  715. }
  716. //-------------------------------------------------------------------------------------------------
  717. void AIGuardRetaliateAttackAggressorState::xfer( Xfer *xfer )
  718. {
  719. // version
  720. XferVersion currentVersion = 1;
  721. XferVersion version = currentVersion;
  722. xfer->xferVersion( &version, currentVersion );
  723. }
  724. //-------------------------------------------------------------------------------------------------
  725. void AIGuardRetaliateAttackAggressorState::loadPostProcess()
  726. {
  727. onEnter();
  728. }