DeployStyleAIUpdate.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. // DeployStyleAIUpdate.cpp ////////////
  24. // Author: Kris Morness, August 2002
  25. // Desc: A standard ai update that also handles units that must deploy to attack and pack before moving.
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "Common/Player.h"
  28. #include "Common/ThingFactory.h"
  29. #include "Common/ThingTemplate.h"
  30. #include "GameClient/Drawable.h"
  31. #include "GameClient/InGameUI.h"
  32. #include "GameLogic/ExperienceTracker.h"
  33. #include "GameLogic/Locomotor.h"
  34. #include "GameLogic/Object.h"
  35. #include "GameLogic/PartitionManager.h"
  36. #include "GameLogic/Weapon.h"
  37. #include "GameLogic/Module/BodyModule.h"
  38. #include "GameLogic/Module/ContainModule.h"
  39. #include "GameLogic/Module/DeployStyleAIUpdate.h"
  40. #include "GameLogic/Module/PhysicsUpdate.h"
  41. #ifdef _INTERNAL
  42. // for occasional debugging...
  43. //#pragma optimize("", off)
  44. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  45. #endif
  46. //-------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------
  48. //-------------------------------------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------------------
  50. DeployStyleAIUpdate::DeployStyleAIUpdate( Thing *thing, const ModuleData* moduleData ) : AIUpdateInterface( thing, moduleData )
  51. {
  52. m_hasOutsideCommand = false;
  53. m_state = READY_TO_MOVE;
  54. m_frameToWakeForDeploy = 0;
  55. reset();
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. DeployStyleAIUpdate::~DeployStyleAIUpdate( void )
  59. {
  60. }
  61. //-------------------------------------------------------------------------------------------------
  62. Bool DeployStyleAIUpdate::isIdle() const
  63. {
  64. // we need to do this because we enter an idle state during deploy states in these cases,
  65. // but scripting relies on us never claiming to be "idle"...
  66. if (m_hasOutsideCommand)
  67. return false;
  68. return AIUpdateInterface::isIdle();
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. void DeployStyleAIUpdate::reset()
  72. {
  73. m_designatedTargetID = INVALID_ID;
  74. m_isAttackMultiple = FALSE;
  75. m_overriddenAttack = FALSE;
  76. m_isGuardingPosition = FALSE;
  77. m_isAttackObject = FALSE;
  78. m_attackObjectID = INVALID_ID;
  79. m_isAttackPosition = FALSE;
  80. m_position.zero();
  81. }
  82. //-------------------------------------------------------------------------------------------------
  83. void DeployStyleAIUpdate::aiDoCommand( const AICommandParms* parms )
  84. {
  85. if (!isAllowedToRespondToAiCommands(parms))
  86. return;
  87. if( parms->m_cmd != AICMD_IDLE && parms->m_cmd != AICMD_FOLLOW_PATH_APPEND )
  88. {
  89. aiIdle( CMD_FROM_AI );
  90. }
  91. if( parms->m_cmdSource != CMD_FROM_AI )
  92. {
  93. reset();
  94. if( parms->m_cmd != AICMD_IDLE )
  95. {
  96. m_lastOutsideCommand.store( *parms );
  97. m_hasOutsideCommand = TRUE;
  98. }
  99. if( m_state != DEPLOY && m_state != UNDEPLOY )
  100. {
  101. //Only issue the command if we're not in the process of deploying/undeploying.
  102. AIUpdateInterface::aiDoCommand( parms );
  103. }
  104. switch( parms->m_cmd )
  105. {
  106. case AICMD_GUARD_POSITION:
  107. m_position.set( &parms->m_pos );
  108. m_isGuardingPosition = TRUE;
  109. //fall through (no break)
  110. case AICMD_GUARD_OBJECT:
  111. case AICMD_GUARD_AREA:
  112. case AICMD_ATTACKMOVE_TO_POSITION:
  113. case AICMD_HUNT:
  114. case AICMD_ATTACKFOLLOW_WAYPOINT_PATH:
  115. case AICMD_ATTACKFOLLOW_WAYPOINT_PATH_AS_TEAM:
  116. case AICMD_ATTACK_AREA:
  117. m_isAttackMultiple = TRUE;
  118. break;
  119. case AICMD_ATTACK_OBJECT:
  120. case AICMD_FORCE_ATTACK_OBJECT:
  121. m_isAttackObject = TRUE;
  122. m_attackObjectID = parms->m_obj ? parms->m_obj->getID() : INVALID_ID;
  123. break;
  124. case AICMD_ATTACK_POSITION:
  125. m_isAttackPosition = TRUE;
  126. m_position.set( &parms->m_pos );
  127. break;
  128. }
  129. }
  130. else
  131. {
  132. //Always process AI issued commands.
  133. AIUpdateInterface::aiDoCommand( parms );
  134. }
  135. }
  136. //-------------------------------------------------------------------------------------------------
  137. UpdateSleepTime DeployStyleAIUpdate::update( void )
  138. {
  139. // have to call our parent's isIdle, because we override it to never return true
  140. // when we have a pending command...
  141. Object *self = getObject();
  142. Weapon *weapon = self->getCurrentWeapon();
  143. Bool inRange = FALSE;
  144. Object *designatedTarget = NULL;
  145. Bool isAttacking = FALSE;
  146. if( weapon )
  147. {
  148. if( m_isAttackPosition )
  149. {
  150. //Handle force attacking attacking specific position
  151. inRange = weapon->isWithinAttackRange( self, &m_position );
  152. isAttacking = TRUE;
  153. }
  154. else if( m_isAttackObject )
  155. {
  156. //Handle attacking a specific object.
  157. designatedTarget = TheGameLogic->findObjectByID( m_attackObjectID );
  158. if( designatedTarget && designatedTarget->isEffectivelyDead() )
  159. {
  160. designatedTarget = NULL;
  161. }
  162. if( designatedTarget )
  163. {
  164. inRange = weapon->isWithinAttackRange( self, designatedTarget );
  165. isAttacking = TRUE;
  166. }
  167. }
  168. else if( m_isAttackMultiple )
  169. {
  170. Bool newTarget = FALSE;
  171. //Handle attackmove and guard.
  172. //We are attacking in a different way... so attempt to figure out how.
  173. WhichTurretType tur = getWhichTurretForCurWeapon();
  174. if( tur != TURRET_INVALID )
  175. {
  176. //Get the turret's current target.
  177. designatedTarget = getTurretTargetObject( tur );
  178. }
  179. else
  180. {
  181. //Get the current goal object (NULL if we have a turret).
  182. designatedTarget = getGoalObject();
  183. }
  184. if( !designatedTarget )
  185. {
  186. //If we still don't have a target, get the last known target.
  187. designatedTarget = TheGameLogic->findObjectByID( m_designatedTargetID );
  188. }
  189. if( designatedTarget && designatedTarget->isEffectivelyDead() )
  190. {
  191. //See if we can acquire one!
  192. designatedTarget = getNextMoodTarget( TRUE, FALSE );
  193. newTarget = TRUE;
  194. }
  195. if( !designatedTarget && m_isGuardingPosition )
  196. {
  197. //While guarding, acquire a target periodically.
  198. designatedTarget = getNextMoodTarget( FALSE, FALSE );
  199. if( designatedTarget )
  200. {
  201. inRange = weapon->isWithinAttackRange( self, designatedTarget );
  202. isAttacking = TRUE;
  203. if( inRange )
  204. {
  205. //Continue overriding it as long as there are enemies in range!
  206. aiAttackObject( designatedTarget, NO_MAX_SHOTS_LIMIT, CMD_FROM_AI );
  207. m_overriddenAttack = TRUE;
  208. m_designatedTargetID = designatedTarget->getID();
  209. }
  210. else
  211. {
  212. designatedTarget = NULL;
  213. }
  214. }
  215. else
  216. {
  217. designatedTarget = NULL;
  218. }
  219. }
  220. else if( designatedTarget )
  221. {
  222. //Finally... we have a target -- so are we in range?
  223. inRange = weapon->isWithinAttackRange( self, designatedTarget );
  224. isAttacking = TRUE;
  225. m_designatedTargetID = designatedTarget->getID();
  226. if( m_overriddenAttack && newTarget && inRange )
  227. {
  228. //Continue overriding it as long as there are enemies in range!
  229. aiAttackObject( designatedTarget, NO_MAX_SHOTS_LIMIT, CMD_FROM_AI );
  230. }
  231. }
  232. else
  233. {
  234. //No target, we must be done or moving.
  235. m_designatedTargetID = INVALID_ID;
  236. }
  237. }
  238. }
  239. Bool remainDeployed = m_isGuardingPosition && !designatedTarget && !isMoving() && !isWaitingForPath();
  240. UnsignedInt now = TheGameLogic->getFrame();
  241. switch( m_state )
  242. {
  243. case READY_TO_MOVE:
  244. if( remainDeployed || (inRange && isAttacking) )
  245. {
  246. setMyState( DEPLOY );
  247. }
  248. break;
  249. case READY_TO_ATTACK:
  250. if( !remainDeployed && (!inRange && isAttacking || !isAttacking && (isWaitingForPath() || getPath())) )
  251. {
  252. WhichTurretType tur = getWhichTurretForCurWeapon();
  253. if( tur != TURRET_INVALID )
  254. {
  255. if( doTurretsHaveToCenterBeforePacking() )
  256. {
  257. setMyState( ALIGNING_TURRETS );
  258. break;
  259. }
  260. }
  261. setMyState( UNDEPLOY );
  262. }
  263. else if( !designatedTarget && m_overriddenAttack && m_hasOutsideCommand )
  264. {
  265. //For multiple attacks we specifically order the unit to attack one object. After
  266. //object is dead, we revert to our original command.
  267. AICommandParms parms( AICMD_MOVE_TO_POSITION, CMD_FROM_AI ); // values don't matter, will be wiped by next line
  268. m_lastOutsideCommand.reconstitute( parms );
  269. aiDoCommand(&parms);
  270. }
  271. break;
  272. case DEPLOY:
  273. if( m_frameToWakeForDeploy != 0 && now >= m_frameToWakeForDeploy)
  274. {
  275. setMyState( READY_TO_ATTACK );
  276. if( m_isAttackMultiple && inRange && isAttacking && designatedTarget )
  277. {
  278. aiAttackObject( designatedTarget, NO_MAX_SHOTS_LIMIT, CMD_FROM_AI );
  279. m_overriddenAttack = TRUE;
  280. }
  281. }
  282. break;
  283. case UNDEPLOY:
  284. if( m_frameToWakeForDeploy != 0 && now >= m_frameToWakeForDeploy)
  285. {
  286. setMyState( READY_TO_MOVE );
  287. }
  288. break;
  289. case ALIGNING_TURRETS:
  290. {
  291. WhichTurretType tur = getWhichTurretForCurWeapon();
  292. if( tur != TURRET_INVALID )
  293. {
  294. if( isTurretInNaturalPosition( tur ) )
  295. {
  296. setMyState( UNDEPLOY );
  297. }
  298. }
  299. break;
  300. }
  301. }
  302. UpdateSleepTime mine = UPDATE_SLEEP_FOREVER;
  303. switch( m_state )
  304. {
  305. case READY_TO_ATTACK:
  306. case READY_TO_MOVE:
  307. mine = UPDATE_SLEEP_FOREVER; // we can sleep for, well, a while
  308. break;
  309. case DEPLOY:
  310. case UNDEPLOY:
  311. mine = m_frameToWakeForDeploy > now ? UPDATE_SLEEP(m_frameToWakeForDeploy - now) : UPDATE_SLEEP_NONE;
  312. aiIdle( CMD_FROM_AI );
  313. break;
  314. case ALIGNING_TURRETS:
  315. mine = UPDATE_SLEEP_NONE; // no sleep for us right now.
  316. aiIdle( CMD_FROM_AI );
  317. break;
  318. }
  319. UpdateSleepTime ret = AIUpdateInterface::update();
  320. return (mine < ret) ? mine : ret;
  321. }
  322. //-------------------------------------------------------------------------------------------------
  323. void DeployStyleAIUpdate::setMyState( DeployStateTypes stateID )
  324. {
  325. m_state = stateID;
  326. Object *self = getObject();
  327. switch( stateID )
  328. {
  329. case DEPLOY:
  330. {
  331. //Tell our object to deploy (so it can continue the same attack later).
  332. aiIdle( CMD_FROM_AI );
  333. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ),
  334. MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ) );
  335. m_frameToWakeForDeploy = getUnpackTime(); //In frames
  336. //Make sure the animation matches the length of unpacking
  337. self->getDrawable()->setAnimationLoopDuration( m_frameToWakeForDeploy );
  338. m_frameToWakeForDeploy += TheGameLogic->getFrame(); // convert to absolute frame
  339. //Play deploy sound
  340. const ThingTemplate *thing = self->getTemplate();
  341. const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Deploy" );
  342. if( soundToPlayPtr )
  343. {
  344. AudioEventRTS soundToPlay = *soundToPlayPtr;
  345. soundToPlay.setObjectID( self->getID() );
  346. TheAudio->addAudioEvent( &soundToPlay );
  347. }
  348. break;
  349. }
  350. case UNDEPLOY:
  351. {
  352. //Tell our object to pack up (so it can continue the same move later).
  353. aiIdle( CMD_FROM_AI );
  354. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK2( MODELCONDITION_UNPACKING, MODELCONDITION_DEPLOYED ),
  355. MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
  356. m_frameToWakeForDeploy = getPackTime(); //In frames
  357. //Make sure the animation matches the length of packing
  358. self->getDrawable()->setAnimationLoopDuration( m_frameToWakeForDeploy );
  359. m_frameToWakeForDeploy += TheGameLogic->getFrame(); // convert to absolute frame
  360. if( doTurretsFunctionOnlyWhenDeployed() )
  361. {
  362. WhichTurretType tur = getWhichTurretForCurWeapon();
  363. if( tur != TURRET_INVALID )
  364. {
  365. setTurretEnabled( tur, false );
  366. }
  367. }
  368. //Play undeploy sound
  369. const ThingTemplate *thing = self->getTemplate();
  370. const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Undeploy" );
  371. if( soundToPlayPtr )
  372. {
  373. AudioEventRTS soundToPlay = *soundToPlayPtr;
  374. soundToPlay.setObjectID( self->getID() );
  375. TheAudio->addAudioEvent( &soundToPlay );
  376. }
  377. break;
  378. }
  379. case READY_TO_MOVE:
  380. {
  381. m_frameToWakeForDeploy = 0;
  382. //We're ready to move, so restore our command!
  383. if( m_hasOutsideCommand )
  384. {
  385. AICommandParms parms( AICMD_MOVE_TO_POSITION, CMD_FROM_AI ); // values don't matter, will be wiped by next line
  386. m_lastOutsideCommand.reconstitute( parms );
  387. aiDoCommand(&parms);
  388. }
  389. self->clearModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
  390. break;
  391. }
  392. case READY_TO_ATTACK:
  393. {
  394. m_frameToWakeForDeploy = 0;
  395. //We're ready to attack, so restore our command!
  396. if( !m_isAttackMultiple && m_hasOutsideCommand )
  397. {
  398. AICommandParms parms( AICMD_MOVE_TO_POSITION, CMD_FROM_AI ); // values don't matter, will be wiped by next line
  399. m_lastOutsideCommand.reconstitute( parms );
  400. aiDoCommand(&parms);
  401. }
  402. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ),
  403. MAKE_MODELCONDITION_MASK( MODELCONDITION_DEPLOYED) );
  404. if( doTurretsFunctionOnlyWhenDeployed() )
  405. {
  406. WhichTurretType tur = getWhichTurretForCurWeapon();
  407. if( tur != TURRET_INVALID )
  408. {
  409. setTurretEnabled( tur, true );
  410. }
  411. }
  412. break;
  413. }
  414. case ALIGNING_TURRETS:
  415. {
  416. m_frameToWakeForDeploy = 0;
  417. WhichTurretType tur = getWhichTurretForCurWeapon();
  418. if( tur != TURRET_INVALID )
  419. {
  420. recenterTurret( tur );
  421. }
  422. break;
  423. }
  424. }
  425. }
  426. // ------------------------------------------------------------------------------------------------
  427. /** CRC */
  428. // ------------------------------------------------------------------------------------------------
  429. void DeployStyleAIUpdate::crc( Xfer *xfer )
  430. {
  431. // extend base class
  432. AIUpdateInterface::crc(xfer);
  433. } // end crc
  434. // ------------------------------------------------------------------------------------------------
  435. /** Xfer method
  436. * Version Info:
  437. * 1: Initial version
  438. * 2: Added support for attack move
  439. * 3: Added improved support for guard, and support for hunt AI
  440. **/
  441. // ------------------------------------------------------------------------------------------------
  442. void DeployStyleAIUpdate::xfer( Xfer *xfer )
  443. {
  444. // version
  445. XferVersion currentVersion = 3;
  446. XferVersion version = currentVersion;
  447. xfer->xferVersion( &version, currentVersion );
  448. // extend base class
  449. AIUpdateInterface::xfer(xfer);
  450. xfer->xferBool(&m_hasOutsideCommand);
  451. xfer->xferUnsignedInt(&m_frameToWakeForDeploy);
  452. xfer->xferUser(&m_state, sizeof(m_state));
  453. if( version >= 2 )
  454. {
  455. xfer->xferObjectID( &m_designatedTargetID );
  456. xfer->xferObjectID( &m_attackObjectID );
  457. xfer->xferCoord3D( &m_position );
  458. xfer->xferBool( &m_isAttackMultiple );
  459. xfer->xferBool( &m_isAttackObject );
  460. xfer->xferBool( &m_isAttackPosition );
  461. }
  462. if( version >= 3 )
  463. {
  464. xfer->xferBool( &m_isGuardingPosition );
  465. xfer->xferBool( &m_overriddenAttack );
  466. }
  467. m_lastOutsideCommand.doXfer(xfer);
  468. if( version < 2 )
  469. {
  470. AICommandParmsStorage obsolete;
  471. obsolete.doXfer(xfer);
  472. }
  473. } // end xfer
  474. // ------------------------------------------------------------------------------------------------
  475. /** Load post process */
  476. // ------------------------------------------------------------------------------------------------
  477. void DeployStyleAIUpdate::loadPostProcess( void )
  478. {
  479. // extend base class
  480. AIUpdateInterface::loadPostProcess();
  481. } // end loadPostProcess