UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
g_ai.h File Reference

Artificial Intelligence functions. More...

#include "g_local.h"

Go to the source code of this file.

Data Structures

class  AiAreaSearch
 AiAreaSearch class, used to get an area of the map around a certain position for the AI to check possible moving positions. More...
 
class  AiAreaSearch::LQueue
 A simple queue class. More...
 
struct  AiAreaSearch::LQueue::qnode_s
 

Functions

void AI_Init (void)
 
void AI_CheckRespawn (int team)
 If the cvar g_endlessaliens is set we will endlessly respawn aliens. More...
 
void G_AddToWayPointList (Edict *ent)
 
void AI_Run (void)
 Every server frame one single actor is handled - always in the same order. More...
 
void AI_ActorRun (Player &player, Actor *actor)
 
Player * AI_CreatePlayer (int team)
 Spawn civilians and aliens. More...
 
bool AI_CheckUsingDoor (const Edict *ent, const Edict *door)
 Checks whether the AI controlled actor wants to use a door. More...
 
bool AI_HasLineOfFire (const Actor *actor, const Edict *target)
 Check if actor has a line of fire to the target given. More...
 
void AI_TurnIntoDirection (Actor *actor, const pos3_t pos)
 This function will turn the AI actor into the direction that is needed to walk to the given location. More...
 
bool AI_FindHidingLocation (int team, Actor *actor, const pos3_t from, int tuLeft)
 Tries to search a hiding spot. More...
 
bool AI_FindHerdLocation (Actor *actor, const pos3_t from, const vec3_t target, int tu, bool inverse)
 Tries to search a spot where actor will be more closer to the target and behind the target from enemy (ie hide behind target) More...
 
int AI_GetHidingTeam (const Edict *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. More...
 
const ItemAI_GetItemForShootType (shoot_types_t shootType, const Edict *ent)
 
bool AI_FighterCheckShoot (const Actor *actor, const Edict *check, const fireDef_t *fd, float dist)
 Check whether the fighter should perform the shoot. More...
 
bool AI_CheckLineOfFire (const Actor *shooter, const Edict *target, const fireDef_t *fd, int shots)
 
float AI_CalcShotDamage (Actor *actor, const Actor *target, const fireDef_t *fd, shoot_types_t shotType)
 Calculate estimated damage per single shoot. More...
 
bool AI_TryToReloadWeapon (Actor *actor, containerIndex_t containerID)
 if a weapon can be reloaded we attempt to do so if TUs permit, otherwise drop it More...
 
bool AI_IsHostile (const Actor *actor, const Edict *target)
 Check if actor perceives target as hostile. More...
 
const invDef_tAI_SearchGrenade (const Actor *actor, Item **ip)
 Search the edict's inventory for a grenade or other one-use weapon. More...
 
bool AI_FindMissionLocation (Actor *actor, const pos3_t to, int tus, int radius=0)
 Try to go close to a mission edict. More...
 
bool AI_CheckPosition (const Actor *const ent, const pos3_t pos)
 Checks if the given position is safe to stand on. More...
 
bool AI_HideNeeded (const Actor *actor)
 Checks whether the given alien should try to hide because there are enemies close enough to shoot the alien. More...
 
bool AIL_TeamThink (Player &player)
 The team think function for the ai controlled players. More...
 
void AIL_ActorThink (Player &player, Actor *actor)
 The think function for the ai controlled players. More...
 
int AIL_InitActor (Actor *actor)
 Initializes the lua AI for an actor. More...
 
void AIL_Cleanup (void)
 Closes the LUA AI. More...
 
void AIL_Init (void)
 
void AIL_Shutdown (void)
 

Variables

Edictai_waypointList
 

Detailed Description

Artificial Intelligence functions.

Definition in file g_ai.h.

Function Documentation

void AI_ActorRun ( Player &  player,
Actor actor 
)

Definition at line 1655 of file g_ai.cpp.

References AI_ActorThink(), AIL_ActorThink(), g_ailua, and cvar_s::integer.

Referenced by AI_PlayerRun(), G_MoralePanic(), and G_MoraleRage().

bool AI_CheckPosition ( const Actor *const  actor,
const pos3_t  pos 
)
void AI_CheckRespawn ( int  team)

If the cvar g_endlessaliens is set we will endlessly respawn aliens.

Note
This can be used for rescue or collect missions where it is enough to do something, and then leave the map (rescue zone)

Definition at line 1908 of file g_ai.cpp.

References G_AppearPerishEvent(), g_endlessaliens, G_EventActorAdd(), G_GetEquipmentForAISpawn(), G_GetPlayerForTeam(), G_SpawnAIPlayer(), G_VisToPM(), level_locals_s::initialAlienActorsSpawned, cvar_s::integer, level, level_locals_s::num_alive, PM_ALL, TEAM_ALIEN, and Edict::visflags.

Referenced by G_ClientEndRound().

bool AI_CheckUsingDoor ( const Edict ent,
const Edict door 
)

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

Parameters
[in]entThe AI controlled actor
[in]doorThe 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 396 of file g_ai.cpp.

References ACTOR_VIS_0, Edict::doorState, f, frand(), G_ActorVis(), G_EdictsGetNextLivingActor(), G_FrustumVis(), G_VisCheckDist(), Edict::getTeam(), gi, Edict::hiding, Edict::isSameTeamAs(), Edict::origin, STATE_OPENED, TEAM_ALIEN, TEAM_CIVILIAN, Edict::type, and VectorDist.

Referenced by Touch_DoorTrigger().

Player* AI_CreatePlayer ( int  team)

Spawn civilians and aliens.

Parameters
[in]team
See also
G_SpawnAIPlayer
Returns
Player pointer
Note
see cvars ai_singleplayeraliens, ai_numcivilians, ai_multiplayeraliens

Definition at line 1944 of file g_ai.cpp.

References ai_multiplayeraliens, ai_numcivilians, ai_singleplayeraliens, G_IsSinglePlayer, G_PlayerGetNextAI(), G_SetTeamForPlayer(), G_SpawnAIPlayers(), game, gi, level_locals_s::initialAlienActorsSpawned, cvar_s::integer, level, level_locals_s::num_spawned, game_locals_s::players, sv_ai, and TEAM_CIVILIAN.

Referenced by G_SpawnEntities(), and SVCmd_AI_Add_f().

bool AI_FighterCheckShoot ( const Actor actor,
const Edict check,
const fireDef_t fd,
float  dist 
)

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 364 of file g_ai.cpp.

References AI_CheckFF(), G_CalcEffectiveSpread(), Actor::isInsane(), Edict::origin, fireDef_s::range, and fireDef_s::splrad.

Referenced by actorL_throwgrenade(), AI_FindBestFiredef(), AI_SearchDestroyableObject(), and AIL_GetBestShot().

bool AI_FindHerdLocation ( Actor actor,
const pos3_t  from,
const vec3_t  target,
int  tu,
bool  inverse 
)

Tries to search a spot where actor will be more closer to the target and behind the target from enemy (ie hide behind target)

Parameters
[in]actorThe actor edict.
[in]fromThe grid position the actor is (theoretically) standing at and searching the nearest location from
[in]targetTries to find the nearest position to this location
[in]tuThe available TUs of the actor
[in]inverseTry to shield the target instead of using target as shield

Definition at line 659 of file g_ai.cpp.

References AI_CheckPosition(), AI_GetHidingTeam(), Edict::calcOrigin(), DotProduct, f, G_ActorMoveLength(), G_EdictsGetNextLivingActor(), G_MoveCalcLocal(), G_TagMalloc, AiAreaSearch::getNext(), Edict::getTeam(), HERD_DIST, HERD_THRESHOLD, length, Edict::origin, PATHFINDING_HEIGHT, Edict::pos, ROUTING_NOT_REACHABLE, TAG_LEVEL, TU_MOVE_STRAIGHT, VectorCopy, VectorDistSqr, VectorNormalizeFast(), and VectorSubtract.

Referenced by AIL_positionherd().

bool AI_FindHidingLocation ( int  team,
Actor actor,
const pos3_t  from,
int  tuLeft 
)

Tries to search a hiding spot.

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

Definition at line 606 of file g_ai.cpp.

References AI_ACTION_NOTHING_FOUND, AI_CheckPosition(), AI_IsExposed(), Edict::calcOrigin(), G_ActorMoveLength(), G_MoveCalcLocal(), G_TagMalloc, AiAreaSearch::getNext(), HIDE_DIST, PATHFINDING_HEIGHT, Edict::pos, ROUTING_NOT_REACHABLE, TAG_LEVEL, TU_MOVE_STRAIGHT, and VectorCopy.

Referenced by AI_FighterCalcActionScore(), and AIL_positionhide().

bool AI_FindMissionLocation ( Actor actor,
const pos3_t  to,
int  tus,
int  radius 
)

Try to go close to a mission edict.

Parameters
[in,out]actorThe actor edict.
[in]toThe target position.
[in]tusAvailable Time Units
[in]radiusRadius of the area to search
Returns
true if found a suitable position, false otherwise

Definition at line 1423 of file g_ai.cpp.

References AI_CheckPosition(), G_ActorMoveLength(), AiAreaSearch::getNext(), length, level, level_locals_s::pathingMap, Edict::pos, ROUTING_NOT_REACHABLE, VectorCompare, and VectorCopy.

Referenced by AIL_positionmission().

int AI_GetHidingTeam ( const Edict 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 570 of file g_ai.cpp.

References G_IsCivilian, Edict::getTeam(), and TEAM_ALIEN.

Referenced by AI_FighterCalcActionScore(), AI_FindHerdLocation(), AI_HideNeeded(), AIL_positionapproach(), AIL_positionflee(), and AIL_positionhide().

const Item* AI_GetItemForShootType ( shoot_types_t  shootType,
const Edict ent 
)

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

Parameters
shootTypeThe current selected shoot type
entThe ai actor
Returns
The item that was selected for the given shoot type. This might be nullptr if no item was found.

Definition at line 541 of file g_ai.cpp.

References AI_GetItemFromInventory(), Edict::getLeftHandItem(), Edict::getRightHandItem(), IS_SHOT_HEADGEAR, IS_SHOT_LEFT, IS_SHOT_REACTION, and IS_SHOT_RIGHT.

Referenced by AI_FighterCalcActionScore(), AI_HasLineOfFire(), AIL_GetBestShot(), and AIL_positionshoot().

bool AI_HasLineOfFire ( const Actor actor,
const Edict target 
)

Check if actor has a line of fire to the target given.

Definition at line 275 of file g_ai.cpp.

References AI_CheckLineOfFire(), AI_GetItemForShootType(), Item::ammoDef(), Item::getFiredefs(), objDef_s::numFiredefs, ST_NUM_SHOOT_TYPES, ST_RIGHT, and fireDef_s::weapFdsIdx.

Referenced by AI_HideNeeded(), and AI_IsExposed().

bool AI_HideNeeded ( const Actor actor)

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

Parameters
[in]actorThe 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 484 of file g_ai.cpp.

References AI_GetHidingTeam(), AI_HasLineOfFire(), AI_IsHostile(), crand(), fireDef_s::damage, G_EdictsGetNextLivingActor(), G_Vis(), Item::getFiredefs(), Edict::getLeftHandItem(), Edict::getRightHandItem(), Edict::HP, cvar_s::integer, mor_brave, Edict::morale, Edict::origin, fireDef_s::range, fireDef_s::spldmg, VectorDistSqr, and VT_NOFRUSTUM.

Referenced by AI_FighterCalcActionScore(), and AIL_hideneeded().

void AI_Init ( void  )

Definition at line 266 of file g_ai.cpp.

Referenced by G_Init().

bool AI_IsHostile ( const Actor actor,
const Edict target 
)

Check if actor perceives target as hostile.

Note
Takes lose of sanity in consideration.
Parameters
[in]actorThe actor that checks for hostiles.
[in]targetThe possible hostile actor.
Returns
true if actor perceives target as hostile

Definition at line 933 of file g_ai.cpp.

References G_IsCivilian, G_IsMultiPlayer, Actor::isInsane(), and Edict::isOpponent().

Referenced by actorL_isvalidtarget(), actorL_throwgrenade(), AI_FighterCalcActionScore(), AI_HideNeeded(), AIL_positionflee(), and AIL_positionshoot().

void AI_Run ( void  )

Every server frame one single actor is handled - always in the same order.

See also
G_RunFrame

Definition at line 1713 of file g_ai.cpp.

References AI_PlayerRun(), level_locals_s::framenum, g_aihumans, G_PlayerGetNextActiveAI(), G_PlayerGetNextActiveHuman(), cvar_s::integer, and level.

Referenced by G_RunFrame().

const invDef_t* AI_SearchGrenade ( const Actor actor,
Item **  ip 
)

Search the edict's inventory for a grenade or other one-use weapon.

Definition at line 954 of file g_ai.cpp.

References Edict::chr, Item::def(), Container::def(), Inventory::getNextCont(), Container::getNextItem(), character_s::inv, Item::isWeapon(), Item::mustReload(), invDef_s::out, and Q_streq.

Referenced by actorL_throwgrenade(), AI_FighterCalcActionScore(), and AI_PrepBestAction().

bool AI_TryToReloadWeapon ( Actor actor,
containerIndex_t  containerID 
)
void AI_TurnIntoDirection ( Actor actor,
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]actorThe actor to turn
[in]posThe position to set the direction for

Definition at line 1557 of file g_ai.cpp.

References CORE_DIRECTIONS, FLYING_DIRECTIONS, G_ActorDoTurn(), G_MoveCalc(), getDVdir, Edict::getTeam(), Actor::getUsableTUs(), gi, Actor::isCrouched(), level, level_locals_s::pathingMap, and ROUTING_UNREACHABLE.

Referenced by AI_ActorThink(), and pos3L_face().

void AIL_ActorThink ( Player &  player,
Actor actor 
)

The think function for the ai controlled players.

Parameters
[in]player
[in]actor

Definition at line 2108 of file g_ai_lua.cpp.

References Edict::AI, AIL_player, ailState, gi, and AI_s::type.

Referenced by AI_ActorRun().

void AIL_Cleanup ( void  )

Closes the LUA AI.

Definition at line 2308 of file g_ai_lua.cpp.

References ailState.

Referenced by G_MatchDoEnd().

int AIL_InitActor ( Actor actor)

Initializes the lua AI for an actor.

Parameters
[in]actorPointer to actor to initialize AI for.
Returns
0 on success.

Definition at line 2203 of file g_ai_lua.cpp.

References Edict::AI, AIL_GetAIType(), AIL_InitLua(), ailState, Edict::chr, Com_sprintf(), Edict::getTeam(), gi, teamDef_s::id, luaL_dobuffer, MAX_VAR, Q_strncpyz(), AI_s::subtype, character_s::teamDef, and AI_s::type.

Referenced by AI_InitPlayer(), and G_ClientTeamInfo().

void AIL_Shutdown ( void  )

Definition at line 2276 of file g_ai_lua.cpp.

References gi.

Referenced by G_Shutdown().

bool AIL_TeamThink ( Player &  player)

The team think function for the ai controlled players.

Parameters
[in]player

Definition at line 2156 of file g_ai_lua.cpp.

References AIL_GetAIType(), AIL_player, AIL_toTeamString(), ailState, and gi.

Referenced by AI_PlayerRun().

void G_AddToWayPointList ( Edict ent)

Definition at line 1537 of file g_ai.cpp.

References level_locals_s::ai_waypointList, Edict::groupChain, and level.

Referenced by SP_civilian_target().

Variable Documentation

Edict* ai_waypointList