2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
33 ===============================================================================
37 ===============================================================================
40 const float SQUARE_ROOT_OF_2 = 1.414213562f;
41 const float AI_TURN_PREDICTION = 0.2f;
42 const float AI_TURN_SCALE = 60.0f;
43 const float AI_SEEK_PREDICTION = 0.3f;
44 const float AI_FLY_DAMPENING = 0.15f;
45 const float AI_HEARING_RANGE = 2048.0f;
46 const int DEFAULT_FLY_OFFSET = 68;
48 #define ATTACK_IGNORE 0
49 #define ATTACK_ON_DAMAGE 1
50 #define ATTACK_ON_ACTIVATE 2
51 #define ATTACK_ON_SIGHT 4
53 // defined in script/ai_base.script. please keep them up to date.
68 // commands < NUM_NONMOVING_COMMANDS don't cause a change in position
69 NUM_NONMOVING_COMMANDS,
71 MOVE_TO_ENEMY = NUM_NONMOVING_COMMANDS,
75 MOVE_TO_ATTACK_POSITION,
78 MOVE_TO_POSITION_DIRECT,
79 MOVE_SLIDE_TO_POSITION,
93 // status results from move commands
94 // make sure to change script/doom_defs.script if you add any, or change their order
100 MOVE_STATUS_DEST_NOT_FOUND,
101 MOVE_STATUS_DEST_UNREACHABLE,
102 MOVE_STATUS_BLOCKED_BY_WALL,
103 MOVE_STATUS_BLOCKED_BY_OBJECT,
104 MOVE_STATUS_BLOCKED_BY_ENEMY,
105 MOVE_STATUS_BLOCKED_BY_MONSTER
110 // obstacle avoidance
111 typedef struct obstaclePath_s {
112 idVec3 seekPos; // seek position avoiding obstacles
113 idEntity * firstObstacle; // if != NULL the first obstacle along the path
114 idVec3 startPosOutsideObstacles; // start position outside obstacles
115 idEntity * startPosObstacle; // if != NULL the obstacle containing the start position
116 idVec3 seekPosOutsideObstacles; // seek position outside obstacles
117 idEntity * seekPosObstacle; // if != NULL the obstacle containing the seek position
123 SE_ENTER_LEDGE_AREA = BIT(1),
124 SE_ENTER_OBSTACLE = BIT(2),
129 typedef struct predictedPath_s {
130 idVec3 endPos; // final position
131 idVec3 endVelocity; // velocity at end position
132 idVec3 endNormal; // normal of blocking surface
133 int endTime; // time predicted
134 int endEvent; // event that stopped the prediction
135 const idEntity * blockingEntity; // entity that blocks the movement
141 extern const idEventDef AI_BeginAttack;
142 extern const idEventDef AI_EndAttack;
143 extern const idEventDef AI_MuzzleFlash;
144 extern const idEventDef AI_CreateMissile;
145 extern const idEventDef AI_AttackMissile;
146 extern const idEventDef AI_FireMissileAtTarget;
147 extern const idEventDef AI_AttackMelee;
148 extern const idEventDef AI_DirectDamage;
149 extern const idEventDef AI_JumpFrame;
150 extern const idEventDef AI_EnableClip;
151 extern const idEventDef AI_DisableClip;
152 extern const idEventDef AI_EnableGravity;
153 extern const idEventDef AI_DisableGravity;
154 extern const idEventDef AI_TriggerParticles;
155 extern const idEventDef AI_RandomPath;
159 typedef struct particleEmitter_s {
160 particleEmitter_s() {
163 joint = INVALID_JOINT;
165 const idDeclParticle *particle;
174 void Save( idSaveGame *savefile ) const;
175 void Restore( idRestoreGame *savefile );
178 moveCommand_t moveCommand;
179 moveStatus_t moveStatus;
181 idVec3 moveDir; // used for wandering and slide moves
182 idEntityPtr<idEntity> goalEntity;
183 idVec3 goalEntityOrigin; // move to entity uses this to avoid checking the floor position every frame
187 float speed; // only used by flying creatures
192 idEntityPtr<idEntity> obstacle;
193 idVec3 lastMoveOrigin;
198 class idAASFindCover : public idAASCallback {
200 idAASFindCover( const idVec3 &hideFromPos );
203 virtual bool TestArea( const idAAS *aas, int areaNum );
207 int PVSAreas[ idEntity::MAX_PVS_AREAS ];
210 class idAASFindAreaOutOfRange : public idAASCallback {
212 idAASFindAreaOutOfRange( const idVec3 &targetPos, float maxDist );
214 virtual bool TestArea( const idAAS *aas, int areaNum );
221 class idAASFindAttackPosition : public idAASCallback {
223 idAASFindAttackPosition( const idAI *self, const idMat3 &gravityAxis, idEntity *target, const idVec3 &targetPos, const idVec3 &fireOffset );
224 ~idAASFindAttackPosition();
226 virtual bool TestArea( const idAAS *aas, int areaNum );
231 idBounds excludeBounds;
235 pvsHandle_t targetPVS;
236 int PVSAreas[ idEntity::MAX_PVS_AREAS ];
239 class idAI : public idActor {
241 CLASS_PROTOTYPE( idAI );
246 void Save( idSaveGame *savefile ) const;
247 void Restore( idRestoreGame *savefile );
250 void HeardSound( idEntity *ent, const char *action );
251 idActor *GetEnemy( void ) const;
252 void TalkTo( idActor *actor );
253 talkState_t GetTalkState( void ) const;
255 bool GetAimDir( const idVec3 &firePos, idEntity *aimAtEnt, const idEntity *ignore, idVec3 &aimDir ) const;
257 void TouchedByFlashlight( idActor *flashlight_owner );
259 // Outputs a list of all monsters to the console.
260 static void List_f( const idCmdArgs &args );
262 // Finds a path around dynamic obstacles.
263 static bool FindPathAroundObstacles( const idPhysics *physics, const idAAS *aas, const idEntity *ignore, const idVec3 &startPos, const idVec3 &seekPos, obstaclePath_t &path );
264 // Frees any nodes used for the dynamic obstacle avoidance.
265 static void FreeObstacleAvoidanceNodes( void );
266 // Predicts movement, returns true if a stop event was triggered.
267 static bool PredictPath( const idEntity *ent, const idAAS *aas, const idVec3 &start, const idVec3 &velocity, int totalTime, int frameTime, int stopEvent, predictedPath_t &path );
268 // Return true if the trajectory of the clip model is collision free.
269 static bool TestTrajectory( const idVec3 &start, const idVec3 &end, float zVel, float gravity, float time, float max_height, const idClipModel *clip, int clipmask, const idEntity *ignore, const idEntity *targetEntity, int drawtime );
270 // Finds the best collision free trajectory for a clip model.
271 static bool PredictTrajectory( const idVec3 &firePos, const idVec3 &target, float projectileSpeed, const idVec3 &projGravity, const idClipModel *clip, int clipmask, float max_height, const idEntity *ignore, const idEntity *targetEntity, int drawtime, idVec3 &aimDir );
279 idMoveState savedMove;
282 bool ignore_obstacles;
285 int blockedAttackTime;
293 float anim_turn_amount;
294 float anim_turn_angles;
297 idPhysics_Monster physicsObj;
300 jointHandle_t flyTiltJoint;
302 float fly_bob_strength;
305 int fly_offset; // prefered offset from player's view
306 float fly_seek_scale;
307 float fly_roll_scale;
310 float fly_pitch_scale;
314 bool allowMove; // disables any animation movement
315 bool allowHiddenMovement; // allows character to still move around while hidden
316 bool disableGravity; // disables gravity and allows vertical movement by the animation
317 bool af_push_moveables; // allow the articulated figure to push moveable objects
319 // weapon/attack vars
320 bool lastHitCheckResult;
321 int lastHitCheckTime;
324 float projectile_height_to_distance_ratio; // calculates the maximum height a projectile can be thrown
325 idList<idVec3> missileLaunchOffset;
327 const idDict * projectileDef;
328 mutable idClipModel *projectileClipModel;
329 float projectileRadius;
330 float projectileSpeed;
331 idVec3 projectileVelocity;
332 idVec3 projectileGravity;
333 idEntityPtr<idProjectile> projectile;
337 const idSoundShader *chat_snd;
341 talkState_t talk_state;
342 idEntityPtr<idActor> talkTarget;
346 int current_cinematic;
349 idEntityPtr<idEntity> focusEntity;
350 idVec3 currentFocusPos;
353 int forceAlignHeadTime;
356 idAngles destLookAng;
359 idList<jointHandle_t> lookJoints;
360 idList<idAngles> lookJointAngles;
361 float eyeVerticalOffset;
362 float eyeHorizontalOffset;
371 bool restartParticles; // should smoke emissions restart
372 bool useBoneAxis; // use the bone vs the model axis
373 idList<particleEmitter_t> particles; // particle data
375 renderLight_t worldMuzzleFlash; // positioned on world weapon bone
376 int worldMuzzleFlashHandle;
377 jointHandle_t flashJointWorld;
384 jointHandle_t focusJoint;
385 jointHandle_t orientationJoint;
388 idEntityPtr<idActor> enemy;
389 idVec3 lastVisibleEnemyPos;
390 idVec3 lastVisibleEnemyEyeOffset;
391 idVec3 lastVisibleReachableEnemyPos;
392 idVec3 lastReachableEnemyPos;
393 bool wakeOnFlashlight;
396 idScriptBool AI_TALK;
397 idScriptBool AI_DAMAGE;
398 idScriptBool AI_PAIN;
399 idScriptFloat AI_SPECIAL_DAMAGE;
400 idScriptBool AI_DEAD;
401 idScriptBool AI_ENEMY_VISIBLE;
402 idScriptBool AI_ENEMY_IN_FOV;
403 idScriptBool AI_ENEMY_DEAD;
404 idScriptBool AI_MOVE_DONE;
405 idScriptBool AI_ONGROUND;
406 idScriptBool AI_ACTIVATED;
407 idScriptBool AI_FORWARD;
408 idScriptBool AI_JUMP;
409 idScriptBool AI_ENEMY_REACHABLE;
410 idScriptBool AI_BLOCKED;
411 idScriptBool AI_OBSTACLE_IN_PATH;
412 idScriptBool AI_DEST_UNREACHABLE;
413 idScriptBool AI_HIT_ENEMY;
414 idScriptBool AI_PUSHED;
420 virtual void DormantBegin( void ); // called when entity becomes dormant
421 virtual void DormantEnd( void ); // called when entity wakes from being dormant
423 void Activate( idEntity *activator );
424 int ReactionTo( const idEntity *ent );
425 bool CheckForEnemy( void );
426 void EnemyDead( void );
427 virtual bool CanPlayChatterSounds( void ) const;
428 void SetChatSound( void );
429 void PlayChatter( void );
430 virtual void Hide( void );
431 virtual void Show( void );
432 idVec3 FirstVisiblePointOnPath( const idVec3 origin, const idVec3 &target, int travelFlags ) const;
433 void CalculateAttackOffsets( void );
434 void PlayCinematic( void );
437 virtual void ApplyImpulse( idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse );
438 void GetMoveDelta( const idMat3 &oldaxis, const idMat3 &axis, idVec3 &delta );
439 void CheckObstacleAvoidance( const idVec3 &goalPos, idVec3 &newPos );
440 void DeadMove( void );
441 void AnimMove( void );
442 void SlideMove( void );
443 void AdjustFlyingAngles( void );
444 void AddFlyBob( idVec3 &vel );
445 void AdjustFlyHeight( idVec3 &vel, const idVec3 &goalPos );
446 void FlySeekGoal( idVec3 &vel, idVec3 &goalPos );
447 void AdjustFlySpeed( idVec3 &vel );
448 void FlyTurn( void );
449 void FlyMove( void );
450 void StaticMove( void );
453 virtual bool Pain( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location );
454 virtual void Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location );
457 void KickObstacles( const idVec3 &dir, float force, idEntity *alwaysKick );
458 bool ReachedPos( const idVec3 &pos, const moveCommand_t moveCommand ) const;
459 float TravelDistance( const idVec3 &start, const idVec3 &end ) const;
460 int PointReachableAreaNum( const idVec3 &pos, const float boundsScale = 2.0f ) const;
461 bool PathToGoal( aasPath_t &path, int areaNum, const idVec3 &origin, int goalAreaNum, const idVec3 &goalOrigin ) const;
462 void DrawRoute( void ) const;
463 bool GetMovePos( idVec3 &seekPos );
464 bool MoveDone( void ) const;
465 bool EntityCanSeePos( idActor *actor, const idVec3 &actorOrigin, const idVec3 &pos );
466 void BlockedFailSafe( void );
469 void StopMove( moveStatus_t status );
470 bool FaceEnemy( void );
471 bool FaceEntity( idEntity *ent );
472 bool DirectMoveToPosition( const idVec3 &pos );
473 bool MoveToEnemyHeight( void );
474 bool MoveOutOfRange( idEntity *entity, float range );
475 bool MoveToAttackPosition( idEntity *ent, int attack_anim );
476 bool MoveToEnemy( void );
477 bool MoveToEntity( idEntity *ent );
478 bool MoveToPosition( const idVec3 &pos );
479 bool MoveToCover( idEntity *entity, const idVec3 &pos );
480 bool SlideToPosition( const idVec3 &pos, float time );
481 bool WanderAround( void );
482 bool StepDirection( float dir );
483 bool NewWanderDir( const idVec3 &dest );
486 const idDeclParticle *SpawnParticlesOnJoint( particleEmitter_t &pe, const char *particleName, const char *jointName );
487 void SpawnParticles( const char *keyName );
488 bool ParticlesActive( void );
491 bool FacingIdeal( void );
493 bool TurnToward( float yaw );
494 bool TurnToward( const idVec3 &pos );
497 void ClearEnemy( void );
498 bool EnemyPositionValid( void ) const;
499 void SetEnemyPosition( void );
500 void UpdateEnemyPosition( void );
501 void SetEnemy( idActor *newEnemy );
504 void CreateProjectileClipModel( void ) const;
505 idProjectile *CreateProjectile( const idVec3 &pos, const idVec3 &dir );
506 void RemoveProjectile( void );
507 idProjectile *LaunchProjectile( const char *jointname, idEntity *target, bool clampToAttackCone );
508 virtual void DamageFeedback( idEntity *victim, idEntity *inflictor, int &damage );
509 void DirectDamage( const char *meleeDefName, idEntity *ent );
510 bool TestMelee( void ) const;
511 bool AttackMelee( const char *meleeDefName );
512 void BeginAttack( const char *name );
513 void EndAttack( void );
514 void PushWithAF( void );
517 void GetMuzzle( const char *jointname, idVec3 &muzzle, idMat3 &axis );
518 void InitMuzzleFlash( void );
519 void TriggerWeaponEffects( const idVec3 &muzzle );
520 void UpdateMuzzleFlash( void );
521 virtual bool UpdateAnimationControllers( void );
522 void UpdateParticles( void );
523 void TriggerParticles( const char *jointName );
525 // AI script state management
526 void LinkScriptVariables( void );
527 void UpdateAIScript( void );
532 void Event_Activate( idEntity *activator );
533 void Event_Touch( idEntity *other, trace_t *trace );
534 void Event_FindEnemy( int useFOV );
535 void Event_FindEnemyAI( int useFOV );
536 void Event_FindEnemyInCombatNodes( void );
537 void Event_ClosestReachableEnemyOfEntity( idEntity *team_mate );
538 void Event_HeardSound( int ignore_team );
539 void Event_SetEnemy( idEntity *ent );
540 void Event_ClearEnemy( void );
541 void Event_MuzzleFlash( const char *jointname );
542 void Event_CreateMissile( const char *jointname );
543 void Event_AttackMissile( const char *jointname );
544 void Event_FireMissileAtTarget( const char *jointname, const char *targetname );
545 void Event_LaunchMissile( const idVec3 &muzzle, const idAngles &ang );
546 void Event_AttackMelee( const char *meleeDefName );
547 void Event_DirectDamage( idEntity *damageTarget, const char *damageDefName );
548 void Event_RadiusDamageFromJoint( const char *jointname, const char *damageDefName );
549 void Event_BeginAttack( const char *name );
550 void Event_EndAttack( void );
551 void Event_MeleeAttackToJoint( const char *jointname, const char *meleeDefName );
552 void Event_RandomPath( void );
553 void Event_CanBecomeSolid( void );
554 void Event_BecomeSolid( void );
555 void Event_BecomeNonSolid( void );
556 void Event_BecomeRagdoll( void );
557 void Event_StopRagdoll( void );
558 void Event_SetHealth( float newHealth );
559 void Event_GetHealth( void );
560 void Event_AllowDamage( void );
561 void Event_IgnoreDamage( void );
562 void Event_GetCurrentYaw( void );
563 void Event_TurnTo( float angle );
564 void Event_TurnToPos( const idVec3 &pos );
565 void Event_TurnToEntity( idEntity *ent );
566 void Event_MoveStatus( void );
567 void Event_StopMove( void );
568 void Event_MoveToCover( void );
569 void Event_MoveToEnemy( void );
570 void Event_MoveToEnemyHeight( void );
571 void Event_MoveOutOfRange( idEntity *entity, float range );
572 void Event_MoveToAttackPosition( idEntity *entity, const char *attack_anim );
573 void Event_MoveToEntity( idEntity *ent );
574 void Event_MoveToPosition( const idVec3 &pos );
575 void Event_SlideTo( const idVec3 &pos, float time );
576 void Event_Wander( void );
577 void Event_FacingIdeal( void );
578 void Event_FaceEnemy( void );
579 void Event_FaceEntity( idEntity *ent );
580 void Event_WaitAction( const char *waitForState );
581 void Event_GetCombatNode( void );
582 void Event_EnemyInCombatCone( idEntity *ent, int use_current_enemy_location );
583 void Event_WaitMove( void );
584 void Event_GetJumpVelocity( const idVec3 &pos, float speed, float max_height );
585 void Event_EntityInAttackCone( idEntity *ent );
586 void Event_CanSeeEntity( idEntity *ent );
587 void Event_SetTalkTarget( idEntity *target );
588 void Event_GetTalkTarget( void );
589 void Event_SetTalkState( int state );
590 void Event_EnemyRange( void );
591 void Event_EnemyRange2D( void );
592 void Event_GetEnemy( void );
593 void Event_GetEnemyPos( void );
594 void Event_GetEnemyEyePos( void );
595 void Event_PredictEnemyPos( float time );
596 void Event_CanHitEnemy( void );
597 void Event_CanHitEnemyFromAnim( const char *animname );
598 void Event_CanHitEnemyFromJoint( const char *jointname );
599 void Event_EnemyPositionValid( void );
600 void Event_ChargeAttack( const char *damageDef );
601 void Event_TestChargeAttack( void );
602 void Event_TestAnimMoveTowardEnemy( const char *animname );
603 void Event_TestAnimMove( const char *animname );
604 void Event_TestMoveToPosition( const idVec3 &position );
605 void Event_TestMeleeAttack( void );
606 void Event_TestAnimAttack( const char *animname );
607 void Event_Shrivel( float shirvel_time );
608 void Event_Burn( void );
609 void Event_PreBurn( void );
610 void Event_ClearBurn( void );
611 void Event_SetSmokeVisibility( int num, int on );
612 void Event_NumSmokeEmitters( void );
613 void Event_StopThinking( void );
614 void Event_GetTurnDelta( void );
615 void Event_GetMoveType( void );
616 void Event_SetMoveType( int moveType );
617 void Event_SaveMove( void );
618 void Event_RestoreMove( void );
619 void Event_AllowMovement( float flag );
620 void Event_JumpFrame( void );
621 void Event_EnableClip( void );
622 void Event_DisableClip( void );
623 void Event_EnableGravity( void );
624 void Event_DisableGravity( void );
625 void Event_EnableAFPush( void );
626 void Event_DisableAFPush( void );
627 void Event_SetFlySpeed( float speed );
628 void Event_SetFlyOffset( int offset );
629 void Event_ClearFlyOffset( void );
630 void Event_GetClosestHiddenTarget( const char *type );
631 void Event_GetRandomTarget( const char *type );
632 void Event_TravelDistanceToPoint( const idVec3 &pos );
633 void Event_TravelDistanceToEntity( idEntity *ent );
634 void Event_TravelDistanceBetweenPoints( const idVec3 &source, const idVec3 &dest );
635 void Event_TravelDistanceBetweenEntities( idEntity *source, idEntity *dest );
636 void Event_LookAtEntity( idEntity *ent, float duration );
637 void Event_LookAtEnemy( float duration );
638 void Event_SetJointMod( int allowJointMod );
639 void Event_ThrowMoveable( void );
640 void Event_ThrowAF( void );
641 void Event_SetAngles( idAngles const &ang );
642 void Event_GetAngles( void );
643 void Event_RealKill( void );
644 void Event_Kill( void );
645 void Event_WakeOnFlashlight( int enable );
646 void Event_LocateEnemy( void );
647 void Event_KickObstacles( idEntity *kickEnt, float force );
648 void Event_GetObstacle( void );
649 void Event_PushPointIntoAAS( const idVec3 &pos );
650 void Event_GetTurnRate( void );
651 void Event_SetTurnRate( float rate );
652 void Event_AnimTurn( float angles );
653 void Event_AllowHiddenMovement( int enable );
654 void Event_TriggerParticles( const char *jointName );
655 void Event_FindActorsInBounds( const idVec3 &mins, const idVec3 &maxs );
656 void Event_CanReachPosition( const idVec3 &pos );
657 void Event_CanReachEntity( idEntity *ent );
658 void Event_CanReachEnemy( void );
659 void Event_GetReachableEntityPosition( idEntity *ent );
662 class idCombatNode : public idEntity {
664 CLASS_PROTOTYPE( idCombatNode );
668 void Save( idSaveGame *savefile ) const;
669 void Restore( idRestoreGame *savefile );
672 bool IsDisabled( void ) const;
673 bool EntityInView( idActor *actor, const idVec3 &pos );
674 static void DrawDebugInfo( void );
687 void Event_Activate( idEntity *activator );
688 void Event_MarkUsed( void );
691 #endif /* !__AI_H__ */