DeployStyleAIUpdate.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. // 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_state = READY_TO_MOVE;
  53. m_frameToWaitForDeploy = 0;
  54. }
  55. //-------------------------------------------------------------------------------------------------
  56. DeployStyleAIUpdate::~DeployStyleAIUpdate( void )
  57. {
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. Bool DeployStyleAIUpdate::isIdle() const
  61. {
  62. return AIUpdateInterface::isIdle();
  63. }
  64. //-------------------------------------------------------------------------------------------------
  65. void DeployStyleAIUpdate::aiDoCommand( const AICommandParms* parms )
  66. {
  67. if (!isAllowedToRespondToAiCommands(parms))
  68. return;
  69. /*
  70. //Hack code to allow follow waypoint scripts to be converted to attack follow waypoint scripts
  71. //for simple-test-map-purposes.
  72. switch( parms->m_cmd )
  73. {
  74. case AICMD_FOLLOW_WAYPOINT_PATH:
  75. ((AICommandParms*)parms)->m_cmd = AICMD_ATTACKFOLLOW_WAYPOINT_PATH;
  76. break;
  77. case AICMD_FOLLOW_WAYPOINT_PATH_AS_TEAM:
  78. ((AICommandParms*)parms)->m_cmd = AICMD_ATTACKFOLLOW_WAYPOINT_PATH_AS_TEAM;
  79. break;
  80. }
  81. */
  82. AIUpdateInterface::aiDoCommand( parms );
  83. }
  84. //-------------------------------------------------------------------------------------------------
  85. UpdateSleepTime DeployStyleAIUpdate::update( void )
  86. {
  87. // have to call our parent's isIdle, because we override it to never return true
  88. // when we have a pending command...
  89. Object *self = getObject();
  90. Weapon *weapon = self->getCurrentWeapon();
  91. UnsignedInt now = TheGameLogic->getFrame();
  92. const DeployStyleAIUpdateModuleData *data = getDeployStyleAIUpdateModuleData();
  93. //Are we attempting to move? If so we can't do it unless we are undeployed.
  94. Bool isTryingToMove = isWaitingForPath() || getPath();
  95. //Are we trying to attack something. If so, we need to be in range before we can do so.
  96. Bool isTryingToAttack = getStateMachine()->isInAttackState();
  97. Bool isInRange = FALSE;
  98. //Are we in guard mode? If so, are we idle... idle guarders deploy for fastest response against attackers.
  99. Bool isInGuardIdleState = getStateMachine()->isInGuardIdleState();
  100. AIUpdateInterface *ai = self->getAI();
  101. if( isTryingToAttack && weapon )
  102. {
  103. Object *victim = ai->getCurrentVictim();
  104. if( victim )
  105. {
  106. isInRange = weapon->isWithinAttackRange( self, victim );
  107. }
  108. else
  109. {
  110. const Coord3D *pos = ai->getCurrentVictimPos();
  111. if( pos )
  112. {
  113. isInRange = weapon->isWithinAttackRange( self, pos );
  114. }
  115. }
  116. }
  117. if( m_frameToWaitForDeploy != 0 && now >= m_frameToWaitForDeploy)
  118. {
  119. switch( m_state )
  120. {
  121. case DEPLOY:
  122. setMyState( READY_TO_ATTACK );
  123. break;
  124. case UNDEPLOY:
  125. setMyState( READY_TO_MOVE );
  126. break;
  127. }
  128. }
  129. if( isInRange || isInGuardIdleState )
  130. {
  131. switch( m_state )
  132. {
  133. case READY_TO_MOVE:
  134. //We're need to deploy before we can attack.
  135. setMyState( DEPLOY );
  136. break;
  137. case READY_TO_ATTACK:
  138. //Let the AI handle attacking.
  139. break;
  140. case DEPLOY:
  141. //We can't start attacking until we are fully deployed.
  142. break;
  143. case UNDEPLOY:
  144. if( m_frameToWaitForDeploy != 0 )
  145. {
  146. //Reverse the undeploy at it's current frame!
  147. setMyState( DEPLOY, TRUE );
  148. }
  149. break;
  150. case ALIGNING_TURRETS:
  151. //If turrets are aligning, we are able to attack right now.
  152. setMyState( READY_TO_ATTACK );
  153. break;
  154. }
  155. }
  156. else if( isTryingToMove )
  157. {
  158. switch( m_state )
  159. {
  160. case READY_TO_MOVE:
  161. //We're ready... ai will handle moving.
  162. break;
  163. case READY_TO_ATTACK:
  164. {
  165. WhichTurretType tur = getWhichTurretForCurWeapon();
  166. if( tur != TURRET_INVALID )
  167. {
  168. if( doTurretsHaveToCenterBeforePacking() )
  169. {
  170. //Turrets need to center before we can undeploy.
  171. setMyState( ALIGNING_TURRETS );
  172. break;
  173. }
  174. }
  175. //Start undeploying.
  176. setMyState( UNDEPLOY );
  177. break;
  178. }
  179. case DEPLOY:
  180. if( m_frameToWaitForDeploy != 0 )
  181. {
  182. //Reverse the deploy at it's current frame!
  183. setMyState( UNDEPLOY, TRUE );
  184. }
  185. break;
  186. case UNDEPLOY:
  187. break;
  188. case ALIGNING_TURRETS:
  189. {
  190. WhichTurretType tur = getWhichTurretForCurWeapon();
  191. if( tur != TURRET_INVALID )
  192. {
  193. if( isTurretInNaturalPosition( tur ) )
  194. {
  195. //Turrets are centers, so now we can undeploy.
  196. setMyState( UNDEPLOY );
  197. }
  198. }
  199. break;
  200. }
  201. }
  202. }
  203. switch( m_state )
  204. {
  205. case READY_TO_MOVE:
  206. if( isTryingToMove )
  207. {
  208. self->setModelConditionState( MODELCONDITION_MOVING );
  209. }
  210. break;
  211. case READY_TO_ATTACK:
  212. break;
  213. case DEPLOY:
  214. if( data->m_manualDeployAnimations )
  215. {
  216. UnsignedInt totalFrames = getPackTime();
  217. UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
  218. Drawable *draw = self->getDrawable();
  219. if( draw )
  220. {
  221. draw->setAnimationFrame( totalFrames - framesLeft );
  222. }
  223. }
  224. getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
  225. setLocomotorGoalNone();
  226. break;
  227. case UNDEPLOY:
  228. if( data->m_manualDeployAnimations )
  229. {
  230. UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
  231. Drawable *draw = self->getDrawable();
  232. if( draw )
  233. {
  234. draw->setAnimationFrame( framesLeft );
  235. }
  236. }
  237. getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
  238. setLocomotorGoalNone();
  239. break;
  240. case ALIGNING_TURRETS:
  241. getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
  242. setLocomotorGoalNone();
  243. break;
  244. }
  245. AIUpdateInterface::update();
  246. //We can't sleep the deploy AI because any new commands need to be caught and sent
  247. //into busy state during the update.
  248. return UPDATE_SLEEP_NONE;
  249. }
  250. //-------------------------------------------------------------------------------------------------
  251. void DeployStyleAIUpdate::setMyState( DeployStateTypes stateID, Bool reverseDeploy )
  252. {
  253. m_state = stateID;
  254. Object *self = getObject();
  255. UnsignedInt now = TheGameLogic->getFrame();
  256. const DeployStyleAIUpdateModuleData *data = getDeployStyleAIUpdateModuleData();
  257. switch( stateID )
  258. {
  259. case DEPLOY:
  260. {
  261. //Tell our object to deploy (so it can continue the same attack later).
  262. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ),
  263. MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ) );
  264. if( reverseDeploy )
  265. {
  266. //This is a zero to max animation.
  267. UnsignedInt totalFrames = getUnpackTime();
  268. UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
  269. m_frameToWaitForDeploy = now + totalFrames - framesLeft;
  270. if( data->m_manualDeployAnimations )
  271. {
  272. Drawable *draw = self->getDrawable();
  273. if( draw )
  274. {
  275. draw->setAnimationFrame( totalFrames - framesLeft );
  276. }
  277. }
  278. }
  279. else
  280. {
  281. m_frameToWaitForDeploy = getUnpackTime() + now;
  282. }
  283. //Play deploy sound
  284. const ThingTemplate *thing = self->getTemplate();
  285. const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Deploy" );
  286. if( soundToPlayPtr )
  287. {
  288. AudioEventRTS soundToPlay = *soundToPlayPtr;
  289. soundToPlay.setObjectID( self->getID() );
  290. TheAudio->addAudioEvent( &soundToPlay );
  291. }
  292. break;
  293. }
  294. case UNDEPLOY:
  295. {
  296. //This status will tell the approach AI state to succeed automatically. This prevents
  297. //twitching on deploy. Make sure we clear it now so when it goes into that state, it'll
  298. //actually move.
  299. self->clearStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_DEPLOYED ) );
  300. //Tell our object to pack up (so it can continue the same move later).
  301. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK2( MODELCONDITION_UNPACKING, MODELCONDITION_DEPLOYED ),
  302. MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
  303. if( reverseDeploy )
  304. {
  305. //This is a max to zero animation.
  306. UnsignedInt totalFrames = getUnpackTime();
  307. UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
  308. m_frameToWaitForDeploy = now + totalFrames - framesLeft;
  309. if( data->m_manualDeployAnimations )
  310. {
  311. Drawable *draw = self->getDrawable();
  312. if( draw )
  313. {
  314. draw->setAnimationFrame( framesLeft );
  315. }
  316. }
  317. }
  318. else
  319. {
  320. m_frameToWaitForDeploy = getPackTime() + now;
  321. }
  322. if( doTurretsFunctionOnlyWhenDeployed() )
  323. {
  324. WhichTurretType tur = getWhichTurretForCurWeapon();
  325. if( tur != TURRET_INVALID )
  326. {
  327. setTurretEnabled( tur, false );
  328. }
  329. }
  330. //Play undeploy sound
  331. const ThingTemplate *thing = self->getTemplate();
  332. const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Undeploy" );
  333. if( soundToPlayPtr )
  334. {
  335. AudioEventRTS soundToPlay = *soundToPlayPtr;
  336. soundToPlay.setObjectID( self->getID() );
  337. TheAudio->addAudioEvent( &soundToPlay );
  338. }
  339. break;
  340. }
  341. case READY_TO_MOVE:
  342. {
  343. m_frameToWaitForDeploy = 0;
  344. self->clearModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
  345. break;
  346. }
  347. case READY_TO_ATTACK:
  348. {
  349. //This status will tell the approach AI state to succeed automatically. This prevents
  350. //twitching on deploy.
  351. self->setStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_DEPLOYED ) );
  352. m_frameToWaitForDeploy = 0;
  353. self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ),
  354. MAKE_MODELCONDITION_MASK( MODELCONDITION_DEPLOYED) );
  355. if( doTurretsFunctionOnlyWhenDeployed() )
  356. {
  357. WhichTurretType tur = getWhichTurretForCurWeapon();
  358. if( tur != TURRET_INVALID )
  359. {
  360. setTurretEnabled( tur, true );
  361. }
  362. }
  363. break;
  364. }
  365. case ALIGNING_TURRETS:
  366. {
  367. m_frameToWaitForDeploy = 0;
  368. WhichTurretType tur = getWhichTurretForCurWeapon();
  369. if( tur != TURRET_INVALID )
  370. {
  371. recenterTurret( tur );
  372. }
  373. break;
  374. }
  375. }
  376. }
  377. // ------------------------------------------------------------------------------------------------
  378. /** CRC */
  379. // ------------------------------------------------------------------------------------------------
  380. void DeployStyleAIUpdate::crc( Xfer *xfer )
  381. {
  382. // extend base class
  383. AIUpdateInterface::crc(xfer);
  384. } // end crc
  385. // ------------------------------------------------------------------------------------------------
  386. /** Xfer method
  387. * Version Info:
  388. * 1: Initial version
  389. * 2: Added support for attack move
  390. * 3: Added improved support for guard, and support for hunt AI
  391. **/
  392. // ------------------------------------------------------------------------------------------------
  393. void DeployStyleAIUpdate::xfer( Xfer *xfer )
  394. {
  395. // version
  396. XferVersion currentVersion = 4;
  397. XferVersion version = currentVersion;
  398. xfer->xferVersion( &version, currentVersion );
  399. // extend base class
  400. AIUpdateInterface::xfer(xfer);
  401. if( version >= 4 )
  402. {
  403. xfer->xferUser(&m_state, sizeof(m_state));
  404. xfer->xferUnsignedInt(&m_frameToWaitForDeploy);
  405. }
  406. else if( xfer->getXferMode() == XFER_LOAD )
  407. {
  408. //Good riddance!!!
  409. Bool obsoleteBool;
  410. UnsignedInt obsoleteUnsignedInt;
  411. ObjectID obsoleteObjectID;
  412. Coord3D obsoleteCoord3D;
  413. AICommandParmsStorage obsoleteAICommandParmsStorage;
  414. xfer->xferBool( &obsoleteBool );
  415. xfer->xferUnsignedInt( &obsoleteUnsignedInt );
  416. xfer->xferUser(&m_state, sizeof(m_state));
  417. if( version >= 2 )
  418. {
  419. xfer->xferObjectID( &obsoleteObjectID );
  420. xfer->xferObjectID( &obsoleteObjectID );
  421. xfer->xferCoord3D( &obsoleteCoord3D );
  422. xfer->xferBool( &obsoleteBool );
  423. xfer->xferBool( &obsoleteBool );
  424. xfer->xferBool( &obsoleteBool );
  425. }
  426. if( version >= 3 )
  427. {
  428. xfer->xferBool( &obsoleteBool );
  429. xfer->xferBool( &obsoleteBool );
  430. }
  431. obsoleteAICommandParmsStorage.doXfer( xfer );
  432. if( version < 2 )
  433. {
  434. obsoleteAICommandParmsStorage.doXfer(xfer);
  435. }
  436. //Initialize unit to able to move.
  437. m_state = READY_TO_MOVE;
  438. }
  439. } // end xfer
  440. // ------------------------------------------------------------------------------------------------
  441. /** Load post process */
  442. // ------------------------------------------------------------------------------------------------
  443. void DeployStyleAIUpdate::loadPostProcess( void )
  444. {
  445. // extend base class
  446. AIUpdateInterface::loadPostProcess();
  447. } // end loadPostProcess