g_ai.c File Reference

Artificial Intelligence. More...

#include "g_local.h"
#include "g_ai.h"
Include dependency graph for g_ai.c:

Go to the source code of this file.

Defines

#define AI_MAX_DIST   30

Functions

static qboolean AI_CheckFF (const edict_t *ent, const vec3_t target, float spread)
 Check whether friendly units are in the line of fire when shooting.
static qboolean AI_FighterCheckShoot (const edict_t *ent, const edict_t *check, const fireDef_t *fd, float *dist)
 Check whether the fighter should perform the shoot.
qboolean AI_CheckUsingDoor (const edict_t *ent, const edict_t *door)
 Checks whether the AI controlled actor wants to use a door.
static qboolean AI_CheckCrouch (const edict_t *ent)
 Checks whether it would be smart to change the state to STATE_CROUCHED.
static qboolean AI_HideNeeded (const edict_t *ent)
 Checks whether the given alien should try to hide because there are enemies close enough to shoot the alien.
static const item_tAI_GetItemFromInventory (const invList_t *ic)
 Returns useable item from the given inventory list. That means that the 'weapon' has ammunition left or must not be reloaded.
const item_tAI_GetItemForShootType (shoot_types_t shootType, const edict_t *ent)
int AI_GetHidingTeam (const edict_t *ent)
 Returns the value for the vis check whenever an ai actor tries to hide. For aliens this is the inverse team - see the vis check code for the inverse team rules to see how this works. For civilians we have to specify the alien team and can't use the inverse team rules. This is needed because the inverse team rules aren't working for the civilian team - see TEAM_CIVILIAN.
qboolean AI_FindHidingLocation (int team, edict_t *ent, const pos3_t from, int *tuLeft)
 Tries to search a hiding spot.
qboolean AI_FindHerdLocation (edict_t *ent, const pos3_t from, const vec3_t target, int tu)
 Tries to search a spot where actor will be more closer to the target and behind the target from enemy.
static edict_tAI_SearchDestroyableObject (const edict_t *ent, const fireDef_t *fd)
static void AI_SearchBestTarget (aiAction_t *aia, const edict_t *ent, edict_t *check, const item_t *item, shoot_types_t shootType, int tu, float *maxDmg, int *bestTime, const fireDef_t *fdArray)
static float AI_FighterCalcBestAction (edict_t *ent, pos3_t to, aiAction_t *aia)
static float AI_CivilianCalcBestAction (edict_t *ent, pos3_t to, aiAction_t *aia)
 Calculates possible actions for a civilian.
static int AI_CheckForMissionTargets (const player_t *player, edict_t *ent, aiAction_t *aia)
 Searches the map for mission edicts and try to get there.
static aiAction_t AI_PrepBestAction (const player_t *player, edict_t *ent)
 Attempts to find the best action for an alien. Moves the alien into the starting position for that action and returns the action.
void G_AddToWayPointList (edict_t *ent)
void AI_TurnIntoDirection (edict_t *ent, const pos3_t pos)
 This function will turn the AI actor into the direction that is needed to walk to the given location.
static void AI_TryToReloadWeapon (edict_t *ent, containerIndex_t containerID)
 if a weapon can be reloaded we attempt to do so if TUs permit, otherwise drop it
void AI_ActorThink (player_t *player, edict_t *ent)
 The think function for the ai controlled aliens.
void AI_Run (void)
 Every server frame one single actor is handled - always in the same order.
static void AI_SetStats (edict_t *ent)
 Initializes the actor's stats like morals, strength and so on.
static void AI_SetCharacterValues (edict_t *ent, int team)
 Sets an actor's character values.
static void AI_SetEquipment (edict_t *ent, const equipDef_t *ed)
 Sets the actor's equipment.
static void AI_InitPlayer (const player_t *player, edict_t *ent, const equipDef_t *ed)
 Initializes the actor.
static void G_SpawnAIPlayer (const player_t *player, int numSpawn)
 Spawn civilians and aliens.
player_tAI_CreatePlayer (int team)
 Spawn civilians and aliens.

Variables

edict_tai_waypointList

Detailed Description

Artificial Intelligence.

You can find the reference lua manual at http://www.lua.org/manual/5.1/
-1 and -2 are pseudo indexes, they count backwards:
  • -1 is top
  • 1 is bottom
  • -2 is under the top
  • etc...

Definition in file g_ai.c.


Define Documentation

#define AI_MAX_DIST   30

Definition at line 820 of file g_ai.c.


Function Documentation

void AI_ActorThink ( player_t player,
edict_t ent 
)
static qboolean AI_CheckCrouch ( const edict_t ent  )  [static]

Checks whether it would be smart to change the state to STATE_CROUCHED.

Parameters:
[in] ent The AI controlled actor to chech the state change for
Returns:
true if the actor should go into STATE_CROUCHED, false otherwise

Definition at line 157 of file g_ai.c.

References ACTOR_VIS_50, check, G_ActorVis(), G_EdictsGetNextLivingActor(), G_FrustumVis(), G_IsCivilian, MAX_SPOT_DIST, edict_s::origin, qfalse, qtrue, edict_s::team, and VectorDist.

Referenced by AI_ActorThink().

static qboolean AI_CheckFF ( const edict_t ent,
const vec3_t  target,
float  spread 
) [static]

Check whether friendly units are in the line of fire when shooting.

Parameters:
[in] ent AI that is trying to shoot
[in] target Shoot to this location
[in] spread 

Definition at line 35 of file g_ai.c.

References check, DotProduct, G_EdictsGetNextLivingActorOfTeam(), edict_s::origin, PLAYER_WIDTH, qfalse, qtrue, edict_s::team, torad, VectorAdd, VectorNormalize(), VectorScale, and VectorSubtract.

Referenced by AI_FighterCheckShoot().

static int AI_CheckForMissionTargets ( const player_t player,
edict_t ent,
aiAction_t aia 
) [static]
qboolean AI_CheckUsingDoor ( const edict_t ent,
const edict_t door 
)

Checks whether the AI controlled actor wants to use a door.

Parameters:
[in] ent The AI controlled actor
[in] door The door edict
Returns:
true if the AI wants to use (open/close) that door, false otherwise
Note:
Don't start any new events in here, don't change the actor state
See also:
Touch_DoorTrigger
Todo:
Finish implementation

Definition at line 99 of file g_ai.c.

References ACTOR_VIS_0, check, edict_s::doorState, frand(), G_ActorVis(), G_EdictsGetNextLivingActor(), G_FrustumVis(), gi, edict_s::hiding, MAX_SPOT_DIST, edict_s::origin, qfalse, qtrue, STATE_OPENED, edict_s::team, TEAM_ALIEN, TEAM_CIVILIAN, edict_s::type, and VectorDist.

Referenced by Touch_DoorTrigger().

static float AI_CivilianCalcBestAction ( edict_t ent,
pos3_t  to,
aiAction_t aia 
) [static]

Calculates possible actions for a civilian.

Parameters:
[in] ent Pointer to an edict being civilian.
[in] to The grid position to walk to.
[in] aia Pointer to aiAction containing informations about possible action.
See also:
AI_ActorThink
Note:
Even civilians can use weapons if the teamdef allows this

Definition at line 643 of file g_ai.c.

References AI_ACTION_NOTHING_FOUND, AI_FighterCalcBestAction(), byte, check, edict_s::chr, frand(), G_ActorVis(), G_EdictSetOrigin(), G_EdictsGetNextLivingActor(), G_IsAlien, G_IsCrouched, G_IsInsane, G_IsPaniced, G_IsVisibleForTeam, gi, GUETE_CIV_LAZINESS, GUETE_CIV_RANDOM, level, edict_s::origin, level_locals_t::pathingMap, qtrue, ROUTING_NOT_REACHABLE, RUN_AWAY_DIST, aiAction_t::stop, edict_s::team, TEAM_ALIEN, TEAM_CIVILIAN, TEAM_PHALANX, character_s::teamDef, aiAction_t::to, edict_s::TU, UNIT_SIZE, VectorCopy, VectorDist, and teamDef_s::weapons.

Referenced by AI_PrepBestAction().

player_t* AI_CreatePlayer ( int  team  ) 

Spawn civilians and aliens.

Parameters:
[in] team 
See also:
G_SpawnAIPlayer
Returns:
player_t pointer
Note:
see cvars ai_numaliens, ai_numcivilians, ai_numactors

Definition at line 1209 of file g_ai.c.

References client_persistant_t::ai, ai_numactors, ai_numaliens, ai_numcivilians, G_SetTeamForPlayer(), G_SpawnAIPlayer(), game, gi, i, cvar_s::integer, player_s::inuse, player_s::num, player_s::pers, game_locals_t::players, qtrue, sv_ai, sv_maxclients, game_locals_t::sv_maxplayersperteam, client_persistant_t::team, and TEAM_CIVILIAN.

Referenced by G_SpawnEntities(), and SVCmd_AI_Add_f().

static float AI_FighterCalcBestAction ( edict_t ent,
pos3_t  to,
aiAction_t aia 
) [static]
See also:
AI_ActorThink
Todo:

fill z_align values

optimize this

Todo:
do this decently, only penalizing the visible part of walk and penalizing much more for reaction shooters around; now it may remove some tactical options from aliens, e.g. they may now choose only the closer doors; however it's still better than going three times around soldier and only then firing at him
Todo:
Try to crouch if no hiding spot was found - randomized
Todo:
also add bonus for fleeing from reaction fire and a huge malus if more than 1 move under reaction

Definition at line 532 of file g_ai.c.

References AI_ACTION_NOTHING_FOUND, AI_FindHidingLocation(), AI_GetHidingTeam(), AI_GetItemForShootType(), AI_HideNeeded(), AI_SearchBestTarget(), check, CLOSE_IN_DIST, FIRESH_FiredefForWeapon(), G_EdictSetOrigin(), G_EdictsGetNextLivingActor(), G_IsCivilian, G_IsCrouched, G_IsInsane, G_IsRaged, G_TestVis(), gi, GUETE_CLOSE_IN, GUETE_HIDE, cvar_s::integer, level, edict_s::origin, level_locals_t::pathingMap, edict_s::pos, qtrue, ROUTING_NOT_REACHABLE, ST_NUM_SHOOT_TYPES, ST_RIGHT, aiAction_t::stop, sv_maxclients, aiAction_t::target, edict_s::team, aiAction_t::to, edict_s::TU, TU_MOVE_STRAIGHT, VectorCopy, VectorDist, VIS_YES, VT_NOFRUSTUM, and VT_PERISH.

Referenced by AI_CivilianCalcBestAction(), and AI_PrepBestAction().

static qboolean AI_FighterCheckShoot ( const edict_t ent,
const edict_t check,
const fireDef_t fd,
float *  dist 
) [static]

Check whether the fighter should perform the shoot.

Todo:

Check whether radius and power of fd are to to big for dist

Check whether the alien will die when shooting

Definition at line 73 of file g_ai.c.

References AI_CheckFF(), G_IsInsane, edict_s::origin, qfalse, qtrue, fireDef_s::range, fireDef_s::spread, and VectorDist.

Referenced by AI_SearchBestTarget(), and AI_SearchDestroyableObject().

qboolean AI_FindHerdLocation ( edict_t ent,
const pos3_t  from,
const vec3_t  target,
int  tu 
)

Tries to search a spot where actor will be more closer to the target and behind the target from enemy.

Parameters:
[in] ent The actor edict.
[in] from The grid position the actor is (theoretically) standing at and searching the nearest location from
[in] target Tries to find the nearest position to this location
[in] tu The available TUs of the actor

Definition at line 340 of file g_ai.c.

References AI_GetHidingTeam(), byte, DotProduct, G_EdictCalcOrigin(), G_EdictsGetNextLivingActorOfTeam(), G_IsCrouched, G_MoveCalcLocal(), gi, HERD_DIST, next, edict_s::origin, PATHFINDING_WIDTH, edict_s::pos, qfalse, qtrue, ROUTING_NOT_REACHABLE, VectorCompare, VectorCopy, VectorDistSqr, VectorNormalize(), and VectorSubtract.

Referenced by AIL_positionherd().

qboolean AI_FindHidingLocation ( int  team,
edict_t ent,
const pos3_t  from,
int *  tuLeft 
)

Tries to search a hiding spot.

Parameters:
[out] ent The actor edict. The position of the actor is updated here to perform visibility checks
[in] from The grid position the actor is (theoretically) standing at and searching a hiding location from
[in,out] tuLeft The amount of left TUs to find a hiding spot. The TUs needed to walk to the grid position is subtracted. May not be NULL.
[in] team The team from which actor tries to hide
Returns:
true if hiding is possible, false otherwise

Definition at line 295 of file g_ai.c.

References byte, G_EdictCalcOrigin(), G_IsCrouched, G_MoveCalcLocal(), G_TestVis(), gi, HIDE_DIST, PATHFINDING_WIDTH, edict_s::pos, qfalse, qtrue, ROUTING_NOT_REACHABLE, VIS_YES, VT_NOFRUSTUM, and VT_PERISH.

Referenced by AI_FighterCalcBestAction(), and AIL_positionhide().

int AI_GetHidingTeam ( const edict_t ent  ) 

Returns the value for the vis check whenever an ai actor tries to hide. For aliens this is the inverse team - see the vis check code for the inverse team rules to see how this works. For civilians we have to specify the alien team and can't use the inverse team rules. This is needed because the inverse team rules aren't working for the civilian team - see TEAM_CIVILIAN.

Returns:
A negative team number means "every other team" as the one from the given ent. See the vis check functions for the inverse team rules for more information.

Definition at line 279 of file g_ai.c.

References G_IsCivilian, edict_s::team, and TEAM_ALIEN.

Referenced by AI_FighterCalcBestAction(), AI_FindHerdLocation(), AI_HideNeeded(), and AIL_positionhide().

const item_t* AI_GetItemForShootType ( shoot_types_t  shootType,
const edict_t ent 
)

Returns the item of the currently chosen shoot type of the ai actor.

Parameters:
shootType The current selected shoot type
ent The ai actor
Returns:
The item that was selected for the given shoot type. This might be NULL if no item was found.

Definition at line 250 of file g_ai.c.

References AI_GetItemFromInventory(), IS_SHOT_HEADGEAR, IS_SHOT_LEFT, IS_SHOT_REACTION, IS_SHOT_RIGHT, LEFT, and RIGHT.

Referenced by actorL_shoot(), and AI_FighterCalcBestAction().

static const item_t* AI_GetItemFromInventory ( const invList_t ic  )  [inline, static]

Returns useable item from the given inventory list. That means that the 'weapon' has ammunition left or must not be reloaded.

Parameters:
ic The inventory to search a useable weapon in.
Returns:
Ready to fire weapon.

Definition at line 233 of file g_ai.c.

References item_s::a, invList_s::item, item_s::m, objDef_s::reload, item_s::t, and objDef_s::weapon.

Referenced by AI_GetItemForShootType().

static qboolean AI_HideNeeded ( const edict_t ent  )  [static]

Checks whether the given alien should try to hide because there are enemies close enough to shoot the alien.

Parameters:
[in] ent The alien edict that should (maybe) hide
Returns:
true if hide is needed or false if the alien thinks that it is not needed

Definition at line 187 of file g_ai.c.

References AI_GetHidingTeam(), crand(), fireDef_s::damage, FIRESH_FiredefForWeapon(), G_EdictsGetNextLivingActor(), G_IsCivilian, G_IsVisibleForTeam, G_Vis(), edict_s::HP, cvar_s::integer, invList_s::item, LEFT, mor_brave, edict_s::morale, edict_s::origin, qfalse, qtrue, fireDef_s::range, RIGHT, item_s::t, edict_s::team, VectorDistSqr, and VT_NOFRUSTUM.

Referenced by AI_FighterCalcBestAction().

static void AI_InitPlayer ( const player_t player,
edict_t ent,
const equipDef_t ed 
) [static]

Initializes the actor.

Parameters:
[in] player Player to which this actor belongs.
[in,out] ent Pointer to edict_t representing actor.
[in] ed Equipment definition for the new actor. Might be NULL.

Definition at line 1129 of file g_ai.c.

References AI_SetCharacterValues(), AI_SetEquipment(), AI_SetStats(), AIL_InitActor(), edict_s::body, edict_s::chr, CHRSH_CharGetBody(), CHRSH_CharGetHead(), f, frand(), G_ClientStateChange(), gi, edict_s::head, player_s::pers, qfalse, STATE_REACTION_ONCE, client_persistant_t::team, TEAM_ALIEN, and TEAM_CIVILIAN.

Referenced by G_SpawnAIPlayer().

static aiAction_t AI_PrepBestAction ( const player_t player,
edict_t ent 
) [static]

Attempts to find the best action for an alien. Moves the alien into the starting position for that action and returns the action.

Parameters:
[in] player The AI player
[in] ent The AI actor

Definition at line 827 of file g_ai.c.

References AI_ACTION_NOTHING_FOUND, AI_CheckForMissionTargets(), AI_CivilianCalcBestAction(), AI_FighterCalcBestAction(), byte, Com_DPrintf(), DEBUG_ENGINE, G_ClientMove(), G_ClientStateChange(), G_IsCivilian, G_IsCrouched, G_IsDead, G_IsPaniced, G_MoveCalc(), gi, level, oldPos, edict_s::origin, PATHFINDING_HEIGHT, PATHFINDING_WIDTH, level_locals_t::pathingMap, edict_s::pos, qtrue, ROUTING_NOT_REACHABLE, STATE_CROUCHED, aiAction_t::target, aiAction_t::to, edict_s::TU, and VectorCopy.

Referenced by AI_ActorThink().

void AI_Run ( void   ) 
static void AI_SearchBestTarget ( aiAction_t aia,
const edict_t ent,
edict_t check,
const item_t item,
shoot_types_t  shootType,
int  tu,
float *  maxDmg,
int *  bestTime,
const fireDef_t fdArray 
) [static]
static edict_t* AI_SearchDestroyableObject ( const edict_t ent,
const fireDef_t fd 
) [static]
Todo:
This feature causes the 'aliens shoot at walls'-bug. I considered adding a visibility check, but that wouldn't prevent aliens from shooting at the breakable parts of their own ship. So I disabled it for now. Duke, 23.10.09

Definition at line 411 of file g_ai.c.

References ACTOR_VIS_0, AI_FighterCheckShoot(), check, G_ActorVis(), G_EdictsGetNextInUse(), G_IsBreakable, edict_s::origin, and qtrue.

Referenced by AI_SearchBestTarget().

static void AI_SetCharacterValues ( edict_t ent,
int  team 
) [static]

Sets an actor's character values.

Parameters:
ent Actor to set the model for.
[in] team Team to which actor belongs.

Definition at line 1085 of file g_ai.c.

References csi_s::alienTeams, edict_s::chr, game_import_t::csi, game_import_t::Cvar_String(), gi, teamDef_s::id, csi_s::numAlienTeams, TEAM_CIVILIAN, and character_s::teamDef.

Referenced by AI_InitPlayer().

static void AI_SetEquipment ( edict_t ent,
const equipDef_t ed 
) [static]

Sets the actor's equipment.

Parameters:
ent Actor to give equipment to.
[in] ed Equipment definition for the new actor.

Definition at line 1110 of file g_ai.c.

References edict_s::chr, inventoryInterface_s::EquipActor, inventoryInterface_s::EquipActorMelee, game, gi, character_s::i, game_locals_t::i, character_s::teamDef, and teamDef_s::weapons.

Referenced by AI_InitPlayer().

static void AI_SetStats ( edict_t ent  )  [static]

Initializes the actor's stats like morals, strength and so on.

Parameters:
ent Actor to set the stats for.

Definition at line 1068 of file g_ai.c.

References edict_s::chr, CHRSH_CharGenAbilitySkills(), G_ActorGiveTimeUnits(), character_s::HP, edict_s::HP, cvar_s::integer, character_s::morale, edict_s::morale, edict_s::STUN, and sv_maxclients.

Referenced by AI_InitPlayer().

static void AI_TryToReloadWeapon ( edict_t ent,
containerIndex_t  containerID 
) [static]

if a weapon can be reloaded we attempt to do so if TUs permit, otherwise drop it

Definition at line 946 of file g_ai.c.

References CONTAINER, game_import_t::csi, G_ActorInvMove(), G_ActorReload(), G_ClientCanReload(), gi, csi_s::idFloor, INVDEF, NONE, and qtrue.

Referenced by AI_ActorThink().

void AI_TurnIntoDirection ( edict_t ent,
const pos3_t  pos 
)

This function will turn the AI actor into the direction that is needed to walk to the given location.

Parameters:
[in] aiActor The actor to turn
[in] pos The position to set the direction for

Definition at line 927 of file g_ai.c.

References byte, CORE_DIRECTIONS, FLYING_DIRECTIONS, G_ActorDoTurn(), G_IsCrouched, G_MoveCalc(), getDVdir, gi, level, level_locals_t::pathingMap, ROUTING_UNREACHABLE, edict_s::team, and edict_s::TU.

Referenced by actorL_face(), AI_ActorThink(), and pos3L_face().

void G_AddToWayPointList ( edict_t ent  ) 

Definition at line 904 of file g_ai.c.

References edict_s::groupChain, and i.

Referenced by SP_civilian_target().

static void G_SpawnAIPlayer ( const player_t player,
int  numSpawn 
) [static]

Variable Documentation

Definition at line 902 of file g_ai.c.

Referenced by AI_CheckForMissionTargets(), and G_SpawnEntities().


Generated by  doxygen 1.6.2