| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // DeployStyleAIUpdate.cpp ////////////
- // Author: Kris Morness, August 2002
- // Desc: A standard ai update that also handles units that must deploy to attack and pack before moving.
- #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
- #include "Common/Player.h"
- #include "Common/ThingFactory.h"
- #include "Common/ThingTemplate.h"
- #include "GameClient/Drawable.h"
- #include "GameClient/InGameUI.h"
- #include "GameLogic/ExperienceTracker.h"
- #include "GameLogic/Locomotor.h"
- #include "GameLogic/Object.h"
- #include "GameLogic/PartitionManager.h"
- #include "GameLogic/Weapon.h"
- #include "GameLogic/Module/BodyModule.h"
- #include "GameLogic/Module/ContainModule.h"
- #include "GameLogic/Module/DeployStyleAIUpdate.h"
- #include "GameLogic/Module/PhysicsUpdate.h"
- #ifdef _INTERNAL
- // for occasional debugging...
- //#pragma optimize("", off)
- //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
- #endif
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- DeployStyleAIUpdate::DeployStyleAIUpdate( Thing *thing, const ModuleData* moduleData ) : AIUpdateInterface( thing, moduleData )
- {
- m_state = READY_TO_MOVE;
- m_frameToWaitForDeploy = 0;
- }
- //-------------------------------------------------------------------------------------------------
- DeployStyleAIUpdate::~DeployStyleAIUpdate( void )
- {
- }
- //-------------------------------------------------------------------------------------------------
- Bool DeployStyleAIUpdate::isIdle() const
- {
- return AIUpdateInterface::isIdle();
- }
- //-------------------------------------------------------------------------------------------------
- void DeployStyleAIUpdate::aiDoCommand( const AICommandParms* parms )
- {
- if (!isAllowedToRespondToAiCommands(parms))
- return;
- /*
- //Hack code to allow follow waypoint scripts to be converted to attack follow waypoint scripts
- //for simple-test-map-purposes.
- switch( parms->m_cmd )
- {
- case AICMD_FOLLOW_WAYPOINT_PATH:
- ((AICommandParms*)parms)->m_cmd = AICMD_ATTACKFOLLOW_WAYPOINT_PATH;
- break;
- case AICMD_FOLLOW_WAYPOINT_PATH_AS_TEAM:
- ((AICommandParms*)parms)->m_cmd = AICMD_ATTACKFOLLOW_WAYPOINT_PATH_AS_TEAM;
- break;
- }
- */
- AIUpdateInterface::aiDoCommand( parms );
- }
- //-------------------------------------------------------------------------------------------------
- UpdateSleepTime DeployStyleAIUpdate::update( void )
- {
- // have to call our parent's isIdle, because we override it to never return true
- // when we have a pending command...
- Object *self = getObject();
- Weapon *weapon = self->getCurrentWeapon();
- UnsignedInt now = TheGameLogic->getFrame();
- const DeployStyleAIUpdateModuleData *data = getDeployStyleAIUpdateModuleData();
- //Are we attempting to move? If so we can't do it unless we are undeployed.
- Bool isTryingToMove = isWaitingForPath() || getPath();
-
- //Are we trying to attack something. If so, we need to be in range before we can do so.
- Bool isTryingToAttack = getStateMachine()->isInAttackState();
- Bool isInRange = FALSE;
- //Are we in guard mode? If so, are we idle... idle guarders deploy for fastest response against attackers.
- Bool isInGuardIdleState = getStateMachine()->isInGuardIdleState();
- AIUpdateInterface *ai = self->getAI();
- if( isTryingToAttack && weapon )
- {
- Object *victim = ai->getCurrentVictim();
- if( victim )
- {
- isInRange = weapon->isWithinAttackRange( self, victim );
- }
- else
- {
- const Coord3D *pos = ai->getCurrentVictimPos();
- if( pos )
- {
- isInRange = weapon->isWithinAttackRange( self, pos );
- }
- }
- }
- if( m_frameToWaitForDeploy != 0 && now >= m_frameToWaitForDeploy)
- {
- switch( m_state )
- {
- case DEPLOY:
- setMyState( READY_TO_ATTACK );
- break;
- case UNDEPLOY:
- setMyState( READY_TO_MOVE );
- break;
- }
- }
-
- if( isInRange || isInGuardIdleState )
- {
- switch( m_state )
- {
- case READY_TO_MOVE:
- //We're need to deploy before we can attack.
- setMyState( DEPLOY );
- break;
- case READY_TO_ATTACK:
- //Let the AI handle attacking.
- break;
- case DEPLOY:
- //We can't start attacking until we are fully deployed.
- break;
- case UNDEPLOY:
- if( m_frameToWaitForDeploy != 0 )
- {
- //Reverse the undeploy at it's current frame!
- setMyState( DEPLOY, TRUE );
- }
- break;
- case ALIGNING_TURRETS:
- //If turrets are aligning, we are able to attack right now.
- setMyState( READY_TO_ATTACK );
- break;
- }
- }
- else if( isTryingToMove )
- {
- switch( m_state )
- {
- case READY_TO_MOVE:
- //We're ready... ai will handle moving.
- break;
- case READY_TO_ATTACK:
- {
- WhichTurretType tur = getWhichTurretForCurWeapon();
- if( tur != TURRET_INVALID )
- {
- if( doTurretsHaveToCenterBeforePacking() )
- {
- //Turrets need to center before we can undeploy.
- setMyState( ALIGNING_TURRETS );
- break;
- }
- }
- //Start undeploying.
- setMyState( UNDEPLOY );
- break;
- }
- case DEPLOY:
- if( m_frameToWaitForDeploy != 0 )
- {
- //Reverse the deploy at it's current frame!
- setMyState( UNDEPLOY, TRUE );
- }
- break;
- case UNDEPLOY:
- break;
- case ALIGNING_TURRETS:
- {
- WhichTurretType tur = getWhichTurretForCurWeapon();
- if( tur != TURRET_INVALID )
- {
- if( isTurretInNaturalPosition( tur ) )
- {
- //Turrets are centers, so now we can undeploy.
- setMyState( UNDEPLOY );
- }
- }
- break;
- }
- }
- }
- switch( m_state )
- {
- case READY_TO_MOVE:
- if( isTryingToMove )
- {
- self->setModelConditionState( MODELCONDITION_MOVING );
- }
- break;
- case READY_TO_ATTACK:
- break;
- case DEPLOY:
- if( data->m_manualDeployAnimations )
- {
- UnsignedInt totalFrames = getPackTime();
- UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
- Drawable *draw = self->getDrawable();
- if( draw )
- {
- draw->setAnimationFrame( totalFrames - framesLeft );
- }
- }
- getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
- setLocomotorGoalNone();
- break;
- case UNDEPLOY:
- if( data->m_manualDeployAnimations )
- {
- UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
- Drawable *draw = self->getDrawable();
- if( draw )
- {
- draw->setAnimationFrame( framesLeft );
- }
- }
- getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
- setLocomotorGoalNone();
- break;
- case ALIGNING_TURRETS:
- getStateMachine()->setTemporaryState( AI_BUSY, UPDATE_SLEEP_NONE );
- setLocomotorGoalNone();
- break;
- }
- AIUpdateInterface::update();
- //We can't sleep the deploy AI because any new commands need to be caught and sent
- //into busy state during the update.
- return UPDATE_SLEEP_NONE;
- }
- //-------------------------------------------------------------------------------------------------
- void DeployStyleAIUpdate::setMyState( DeployStateTypes stateID, Bool reverseDeploy )
- {
- m_state = stateID;
- Object *self = getObject();
- UnsignedInt now = TheGameLogic->getFrame();
- const DeployStyleAIUpdateModuleData *data = getDeployStyleAIUpdateModuleData();
-
- switch( stateID )
- {
- case DEPLOY:
- {
- //Tell our object to deploy (so it can continue the same attack later).
- self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ),
- MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ) );
- if( reverseDeploy )
- {
- //This is a zero to max animation.
- UnsignedInt totalFrames = getUnpackTime();
- UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
- m_frameToWaitForDeploy = now + totalFrames - framesLeft;
- if( data->m_manualDeployAnimations )
- {
- Drawable *draw = self->getDrawable();
- if( draw )
- {
- draw->setAnimationFrame( totalFrames - framesLeft );
- }
- }
- }
- else
- {
- m_frameToWaitForDeploy = getUnpackTime() + now;
- }
- //Play deploy sound
- const ThingTemplate *thing = self->getTemplate();
- const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Deploy" );
- if( soundToPlayPtr )
- {
- AudioEventRTS soundToPlay = *soundToPlayPtr;
- soundToPlay.setObjectID( self->getID() );
- TheAudio->addAudioEvent( &soundToPlay );
- }
-
- break;
- }
- case UNDEPLOY:
- {
- //This status will tell the approach AI state to succeed automatically. This prevents
- //twitching on deploy. Make sure we clear it now so when it goes into that state, it'll
- //actually move.
- self->clearStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_DEPLOYED ) );
- //Tell our object to pack up (so it can continue the same move later).
- self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK2( MODELCONDITION_UNPACKING, MODELCONDITION_DEPLOYED ),
- MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
- if( reverseDeploy )
- {
- //This is a max to zero animation.
- UnsignedInt totalFrames = getUnpackTime();
- UnsignedInt framesLeft = m_frameToWaitForDeploy - now;
- m_frameToWaitForDeploy = now + totalFrames - framesLeft;
- if( data->m_manualDeployAnimations )
- {
- Drawable *draw = self->getDrawable();
- if( draw )
- {
- draw->setAnimationFrame( framesLeft );
- }
- }
- }
- else
- {
- m_frameToWaitForDeploy = getPackTime() + now;
- }
- if( doTurretsFunctionOnlyWhenDeployed() )
- {
- WhichTurretType tur = getWhichTurretForCurWeapon();
- if( tur != TURRET_INVALID )
- {
- setTurretEnabled( tur, false );
- }
- }
-
- //Play undeploy sound
- const ThingTemplate *thing = self->getTemplate();
- const AudioEventRTS* soundToPlayPtr = thing->getPerUnitSound( "Undeploy" );
- if( soundToPlayPtr )
- {
- AudioEventRTS soundToPlay = *soundToPlayPtr;
- soundToPlay.setObjectID( self->getID() );
- TheAudio->addAudioEvent( &soundToPlay );
- }
- break;
- }
- case READY_TO_MOVE:
- {
- m_frameToWaitForDeploy = 0;
- self->clearModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_PACKING ) );
- break;
- }
- case READY_TO_ATTACK:
- {
- //This status will tell the approach AI state to succeed automatically. This prevents
- //twitching on deploy.
- self->setStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_DEPLOYED ) );
- m_frameToWaitForDeploy = 0;
- self->clearAndSetModelConditionFlags( MAKE_MODELCONDITION_MASK( MODELCONDITION_UNPACKING ),
- MAKE_MODELCONDITION_MASK( MODELCONDITION_DEPLOYED) );
- if( doTurretsFunctionOnlyWhenDeployed() )
- {
- WhichTurretType tur = getWhichTurretForCurWeapon();
- if( tur != TURRET_INVALID )
- {
- setTurretEnabled( tur, true );
- }
- }
- break;
- }
- case ALIGNING_TURRETS:
- {
- m_frameToWaitForDeploy = 0;
- WhichTurretType tur = getWhichTurretForCurWeapon();
- if( tur != TURRET_INVALID )
- {
- recenterTurret( tur );
- }
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- /** CRC */
- // ------------------------------------------------------------------------------------------------
- void DeployStyleAIUpdate::crc( Xfer *xfer )
- {
- // extend base class
- AIUpdateInterface::crc(xfer);
- } // end crc
- // ------------------------------------------------------------------------------------------------
- /** Xfer method
- * Version Info:
- * 1: Initial version
- * 2: Added support for attack move
- * 3: Added improved support for guard, and support for hunt AI
- **/
- // ------------------------------------------------------------------------------------------------
- void DeployStyleAIUpdate::xfer( Xfer *xfer )
- {
- // version
- XferVersion currentVersion = 4;
- XferVersion version = currentVersion;
- xfer->xferVersion( &version, currentVersion );
-
- // extend base class
- AIUpdateInterface::xfer(xfer);
- if( version >= 4 )
- {
- xfer->xferUser(&m_state, sizeof(m_state));
- xfer->xferUnsignedInt(&m_frameToWaitForDeploy);
- }
- else if( xfer->getXferMode() == XFER_LOAD )
- {
- //Good riddance!!!
- Bool obsoleteBool;
- UnsignedInt obsoleteUnsignedInt;
- ObjectID obsoleteObjectID;
- Coord3D obsoleteCoord3D;
- AICommandParmsStorage obsoleteAICommandParmsStorage;
- xfer->xferBool( &obsoleteBool );
- xfer->xferUnsignedInt( &obsoleteUnsignedInt );
- xfer->xferUser(&m_state, sizeof(m_state));
- if( version >= 2 )
- {
- xfer->xferObjectID( &obsoleteObjectID );
- xfer->xferObjectID( &obsoleteObjectID );
- xfer->xferCoord3D( &obsoleteCoord3D );
- xfer->xferBool( &obsoleteBool );
- xfer->xferBool( &obsoleteBool );
- xfer->xferBool( &obsoleteBool );
- }
- if( version >= 3 )
- {
- xfer->xferBool( &obsoleteBool );
- xfer->xferBool( &obsoleteBool );
- }
- obsoleteAICommandParmsStorage.doXfer( xfer );
- if( version < 2 )
- {
- obsoleteAICommandParmsStorage.doXfer(xfer);
- }
- //Initialize unit to able to move.
- m_state = READY_TO_MOVE;
- }
- } // end xfer
- // ------------------------------------------------------------------------------------------------
- /** Load post process */
- // ------------------------------------------------------------------------------------------------
- void DeployStyleAIUpdate::loadPostProcess( void )
- {
- // extend base class
- AIUpdateInterface::loadPostProcess();
- } // end loadPostProcess
|