naev 0.12.5
ai.c File Reference

Controls the Pilot AI. More...

#include "ai.h"
#include "array.h"
#include "board.h"
#include "conf.h"
#include "faction.h"
#include "gatherable.h"
#include "hook.h"
#include "log.h"
#include "ndata.h"
#include "nlua.h"
#include "nlua_asteroid.h"
#include "nlua_pilot.h"
#include "nlua_spob.h"
#include "nlua_vec2.h"
#include "nluadef.h"
#include "ntracing.h"
#include "physics.h"
#include "pilot.h"
#include "rng.h"
#include "space.h"
Include dependency graph for ai.c:

Go to the source code of this file.

Macros

#define ai_setFlag(f)
#define ai_isFlag(f)
#define AI_DISTRESS   ( 1 << 0 )
#define AI_STATUS_NORMAL   1
#define AI_STATUS_CREATE   2

Functions

static void ai_run (nlua_env env, int nargs)
 Attempts to run a function.
static int ai_loadProfile (AI_Profile *prof, const char *filename)
 Initializes an AI_Profile and adds it to the stack.
static int ai_setMemory (void)
 Sets the cur_pilot's ai.
static void ai_create (Pilot *pilot)
 Runs the create() function in the pilot.
static int ai_loadEquip (void)
 Loads the equipment selector script.
static int ai_sort (const void *p1, const void *p2)
static void ai_taskGC (Pilot *pilot)
 Runs the garbage collector on the pilot's tasks.
static Taskai_createTask (lua_State *L, int subtask)
 Creates a new task based on stack information.
static int ai_tasktarget (lua_State *L, const Task *t)
 Pushes a task target.
static int aiL_pushtask (lua_State *L)
 Pushes a task onto the pilot's task list. Lua function parameter: string func Name of function to call for task. Lua function parameter:[opt] data Data to pass to the function. Supports any lua type.
static int aiL_poptask (lua_State *L)
 Pops the current running task.
static int aiL_taskname (lua_State *L)
 Gets the current task's name. Lua return parameter: string The current task name or nil if there are no tasks.
static int aiL_taskdata (lua_State *L)
 Gets the pilot's task data. Lua return parameter: The pilot's task data or nil if there is no task data. See also: pushtask.
static int aiL_pushsubtask (lua_State *L)
 Pushes a subtask onto the pilot's task's subtask list. Lua function parameter: string func Name of function to call for task. Lua function parameter:[opt] data Data to pass to the function. Supports any lua type.
static int aiL_popsubtask (lua_State *L)
 Pops the current running task.
static int aiL_subtaskname (lua_State *L)
 Gets the current subtask's name. Lua return parameter: string The current subtask name or nil if there are no subtasks.
static int aiL_subtaskdata (lua_State *L)
 Gets the pilot's subtask target. Lua return parameter: The pilot's target ship identifier or nil if no target. See also: pushsubtask.
static int aiL_pilot (lua_State *L)
 Gets the AI's pilot. Lua return parameter: Pilot The AI's pilot.
static int aiL_getrndpilot (lua_State *L)
 Gets a random pilot in the system. Lua return parameter: Pilot|nil.
static int aiL_getnearestpilot (lua_State *L)
 gets the nearest pilot to the current pilot
static int aiL_getdistance (lua_State *L)
 Gets the distance from the pointer.
static int aiL_getdistance2 (lua_State *L)
 Gets the squared distance from the pointer.
static int aiL_getflybydistance (lua_State *L)
 Gets the distance from the pointer perpendicular to the current pilot's flight vector.
static int aiL_minbrakedist (lua_State *L)
 Gets the minimum braking distance.
static int aiL_isbribed (lua_State *L)
 Checks to see if target has bribed pilot.
static int aiL_getGatherable (lua_State *L)
 Gets the closest gatherable within a radius.
static int aiL_instantJump (lua_State *L)
 Checks to see if pilot can instant jump.
static int aiL_ismaxvel (lua_State *L)
 Checks to see if pilot is at maximum velocity.
static int aiL_isstopped (lua_State *L)
 Checks to see if pilot is stopped.
static int aiL_isenemy (lua_State *L)
 Checks to see if target is an enemy.
static int aiL_isally (lua_State *L)
 Checks to see if target is an ally.
static int aiL_haslockon (lua_State *L)
 Checks to see if pilot has a missile lockon.
static int aiL_hasprojectile (lua_State *L)
 Checks to see if pilot has a projectile after him.
static int aiL_scandone (lua_State *L)
 Checks to see if pilot has finished scanning their target.
static int aiL_accel (lua_State *L)
 Starts accelerating the pilot.
static int aiL_turn (lua_State *L)
 Starts turning the pilot.
static int aiL_careful_face (lua_State *L)
 Gives the direction to follow in order to reach the target while minimizating risk.
static int aiL_aim (lua_State *L)
 Aims at a pilot, trying to hit it rather than move to it.
static int aiL_dir (lua_State *L)
 calculates the direction that the target is relative to the current pilot facing.
static int aiL_face (lua_State *L)
 Faces the target.
static int aiL_iface (lua_State *L)
 Maintains an intercept pursuit course.
static int aiL_idir (lua_State *L)
 Calculates angle between pilot facing and intercept-course to target.
static int aiL_follow_accurate (lua_State *L)
 Computes the point to face in order to follow another pilot using a PD controller.
static int aiL_face_accurate (lua_State *L)
 Computes the point to face in order to follow a moving object.
static int aiL_drift_facing (lua_State *L)
 Calculate the offset between the pilot's current direction of travel and the pilot's current facing.
static int aiL_brake (lua_State *L)
 Brakes the pilot.
static int aiL_getnearestspob (lua_State *L)
 Get the nearest friendly spob to the pilot.
static int aiL_getspobfrompos (lua_State *L)
 Get the nearest friendly spob to a given position.
static int aiL_getrndspob (lua_State *L)
 Get a random spob.
static int aiL_getlandspob (lua_State *L)
 Get a random friendly spob.
static int aiL_land (lua_State *L)
 Lands on a spob.
static int aiL_stop (lua_State *L)
 Completely stops the pilot if it is below minimum vel error (no insta-stops).
static int aiL_relvel (lua_State *L)
 Gets the relative velocity of a pilot.
static int aiL_sethyptarget (lua_State *L)
 Sets hyperspace target.
static int aiL_nearhyptarget (lua_State *L)
 Gets the nearest hyperspace target.
static int aiL_rndhyptarget (lua_State *L)
 Gets a random hyperspace target.
static int aiL_hyperspace (lua_State *L)
 Tries to enter hyperspace.
static int aiL_canHyperspace (lua_State *L)
 Gets whether or not the pilot can hyperspace.
static int aiL_hyperspaceAbort (lua_State *L)
 Has the AI abandon hyperspace if applicable.
static int aiL_dock (lua_State *L)
 Docks the ship.
static int aiL_combat (lua_State *L)
 Sets the combat flag.
static int aiL_settarget (lua_State *L)
 Sets the pilot's target.
static int aiL_weapSet (lua_State *L)
 Sets the active weapon set, fires another weapon set or activate an outfit.
static int aiL_hascannons (lua_State *L)
 Does the pilot have cannons?
static int aiL_hasturrets (lua_State *L)
 Does the pilot have turrets?
static int aiL_hasfighterbays (lua_State *L)
 Does the pilot have fighter bays?
static int aiL_hasafterburner (lua_State *L)
 Does the pilot have afterburners?
static int aiL_getenemy (lua_State *L)
 Gets the nearest enemy.
static int aiL_hostile (lua_State *L)
 Sets the enemy hostile (basically notifies of an impending attack).
static int aiL_getweaprangemin (lua_State *L)
 Gets the minimum range of a weapon set.
static int aiL_getweaprange (lua_State *L)
 Gets the range of a weapon set.
static int aiL_getweapspeed (lua_State *L)
 Gets the speed of a weapon.
static int aiL_getweapammo (lua_State *L)
 Gets the ammo of a weapon.
static int aiL_canboard (lua_State *L)
 Checks to see if pilot can board the target.
static int aiL_relsize (lua_State *L)
 Gets the relative size (ship mass) between the current pilot and the specified target.
static int aiL_reldps (lua_State *L)
 Gets the relative damage output (total DPS) between the current pilot and the specified target.
static int aiL_relhp (lua_State *L)
 Gets the relative health (total shields and armour) between the current pilot and the specified target.
static int aiL_settimer (lua_State *L)
 Sets a timer.
static int aiL_timeup (lua_State *L)
 Checks a timer.
static int aiL_distress (lua_State *L)
 Sends a distress signal.
static int aiL_getBoss (lua_State *L)
 Picks a pilot that will command the current pilot.
static int aiL_credits (lua_State *L)
 Sets the pilots credits. Only call in create().
static int aiL_board (lua_State *L)
 Attempts to board the pilot's target.
static int aiL_refuel (lua_State *L)
 Attempts to refuel the pilot's target.
static int aiL_messages (lua_State *L)
 Returns and clears the pilots message queue.
static int aiL_setasterotarget (lua_State *L)
 Sets the pilot's asteroid target.
static int aiL_gatherablePos (lua_State *L)
 Gets the pos and vel of a given gatherable.
static int aiL_shoot_indicator (lua_State *L)
 Access the seeker shoot indicator (that is put to true each time a seeker is shot).
static int aiL_set_shoot_indicator (lua_State *L)
 Set the seeker shoot indicator.
static int aiL_stealth (lua_State *L)
 Tries to stealth or destealth the pilot.
static int aiL_outfitOffAll (lua_State *L)
 Tries to turn off all outfits.
Taskai_curTask (Pilot *pilot)
 Gets the current running task.
AIMemory ai_setPilot (Pilot *p)
 Sets the pilot for further AI calls.
void ai_unsetPilot (AIMemory oldmem)
 Finishes setting up a pilot.
void ai_thinkSetup (double dt)
 Sets up the pilot for thinking.
void ai_thinkApply (Pilot *p)
 Applies the result of thinking.
int ai_pinit (Pilot *p, const char *ai)
 Initializes the pilot in the ai.
void ai_cleartasks (Pilot *p)
 Clears the pilot's tasks.
void ai_destroy (Pilot *p)
 Destroys the ai part of the pilot.
int ai_load (void)
 Initializes the AI stuff which is basically Lua.
int nlua_loadAI (nlua_env env)
AI_Profileai_getProfile (const char *name)
 Gets the AI_Profile by name.
void ai_exit (void)
 Cleans up global AI.
void ai_think (Pilot *pilot, double dt, int dotask)
 Heart of the AI, brains of the pilot.
void ai_init (Pilot *p)
 Initializes the AI.
void ai_attacked (Pilot *attacked, const unsigned int attacker, double dmg)
 Triggers the attacked() function in the pilot's AI.
void ai_discovered (Pilot *discovered)
 Triggers the discovered() function in the pilot's AI.
void ai_hail (Pilot *recipient)
 Triggers the hail() function in the pilot's AI.
void ai_refuel (Pilot *refueler, unsigned int target)
 Has a pilot attempt to refuel the other.
void ai_getDistress (const Pilot *p, const Pilot *distressed, const Pilot *attacker)
 Sends a distress signal to a pilot.
Taskai_newtask (lua_State *L, Pilot *p, const char *func, int subtask, int pos)
 Creates a new AI task.
void ai_freetask (Task *t)
 Frees an AI task.

Variables

static AI_Profileprofiles = NULL
static nlua_env equip_env = LUA_NOREF
static IntList ai_qtquery
static double ai_dt = 0.
static const luaL_Reg aiL_methods []
Pilotcur_pilot = NULL
static double pilot_acc = 0.
static double pilot_turn = 0.
static int pilot_flags = 0
static char aiL_distressmsg [STRMAX_SHORT]
static int aiL_status = AI_STATUS_NORMAL

Detailed Description

Controls the Pilot AI.

Lua module: ai

AI Overview

Concept: Goal (Task) Based AI with additional Optimization

AI uses the goal (task) based AI approach with tasks scripted in Lua, additionally there is a task that is hard-coded and obligatory in any AI script, the 'control' task, whose sole purpose is to assign tasks if there is no current tasks and optimizes or changes tasks if there are.

For example: Pilot A is attacking Pilot B. Say that Pilot C then comes in the same system and is of the same faction as Pilot B, and therefore attacks Pilot A. Pilot A would keep on fighting Pilot B until the control task kicks in. Then he/she could run if it deems that Pilot C and Pilot B together are too strong for him/her, or attack Pilot C because it's an easier target to finish off then Pilot B. Therefore there are endless possibilities and it's up to the AI coder to set up.

Specification

  • AI will follow basic tasks defined from Lua AI script.
    • if Task is NULL, AI will run "control" task
    • Task is continued every frame
    • Tasks can have subtasks which will be closed when parent task is dead.
    • "control" task is a special task that MUST exist in any given Pilot AI (missiles and such will use "seek")
    • "control" task is not permanent, but transitory
    • "control" task sets another task
  • "control" task is also run at a set rate (depending on Lua global "control_rate") to choose optimal behaviour (task)

Memory

The AI currently has per-pilot memory which is accessible as "mem". This memory is actually stored in the table pilotmem[cur_pilot->id]. This allows the pilot to keep some memory always accessible between runs without having to rely on the storage space a task has.

Garbage Collector

The tasks are not deleted directly but are marked for deletion and are then cleaned up in a garbage collector. This is to avoid accessing invalid task memory.

Note
Nothing in this file can be considered reentrant. Plan accordingly.
Todo
Clean up most of the code, it was written as one of the first subsystems and is pretty lacking in quite a few aspects.

Definition in file ai.c.

Macro Definition Documentation

◆ AI_DISTRESS

#define AI_DISTRESS   ( 1 << 0 )

Sent distress signal.

Definition at line 104 of file ai.c.

◆ ai_isFlag

#define ai_isFlag ( f)
Value:
( pilot_flags & f )
static int pilot_flags
Definition ai.c:343

Checks pilot flag f

Definition at line 102 of file ai.c.

◆ ai_setFlag

#define ai_setFlag ( f)
Value:
( pilot_flags |= f )

Sets pilot flag f

Definition at line 101 of file ai.c.

◆ AI_STATUS_CREATE

#define AI_STATUS_CREATE   2

AI is running create function.

Definition at line 351 of file ai.c.

◆ AI_STATUS_NORMAL

#define AI_STATUS_NORMAL   1

Normal AI function behaviour.

Definition at line 350 of file ai.c.

Function Documentation

◆ ai_attacked()

void ai_attacked ( Pilot * attacked,
const unsigned int attacker,
double dmg )

Triggers the attacked() function in the pilot's AI.

Parameters
attackedPilot that is attacked.
[in]attackerID of the attacker.
[in]dmgDamage done by the attacker.

Definition at line 951 of file ai.c.

◆ ai_cleartasks()

void ai_cleartasks ( Pilot * p)

Clears the pilot's tasks.

Parameters
pPilot to clear tasks of.

Definition at line 548 of file ai.c.

◆ ai_create()

void ai_create ( Pilot * pilot)
static

Runs the create() function in the pilot.

Should create all the gear and such the pilot has.

Parameters
pilotPilot to "create".

Definition at line 1120 of file ai.c.

◆ ai_createTask()

Task * ai_createTask ( lua_State * L,
int subtask )
static

Creates a new task based on stack information.

Definition at line 1259 of file ai.c.

◆ ai_curTask()

Task * ai_curTask ( Pilot * pilot)

Gets the current running task.

Definition at line 385 of file ai.c.

◆ ai_destroy()

void ai_destroy ( Pilot * p)

Destroys the ai part of the pilot.

Parameters
[in]pPilot to destroy its AI part.

Definition at line 561 of file ai.c.

◆ ai_discovered()

void ai_discovered ( Pilot * discovered)

Triggers the discovered() function in the pilot's AI.

Parameters
discoveredPilot that is discovered.

Definition at line 989 of file ai.c.

◆ ai_exit()

void ai_exit ( void )

Cleans up global AI.

Definition at line 788 of file ai.c.

◆ ai_freetask()

void ai_freetask ( Task * t)

Frees an AI task.

Parameters
tTask to free.

Definition at line 1232 of file ai.c.

◆ ai_getDistress()

void ai_getDistress ( const Pilot * p,
const Pilot * distressed,
const Pilot * attacker )

Sends a distress signal to a pilot.

Parameters
pPilot receiving the distress signal.
distressedPilot sending the distress signal.
attackerPilot attacking the distressed.

Definition at line 1094 of file ai.c.

◆ ai_getProfile()

AI_Profile * ai_getProfile ( const char * name)

Gets the AI_Profile by name.

Parameters
[in]nameName of the profile to get.
Returns
The profile or NULL on error.

Definition at line 775 of file ai.c.

◆ ai_hail()

void ai_hail ( Pilot * recipient)

Triggers the hail() function in the pilot's AI.

Parameters
recipientPilot that is being hailed.

Definition at line 1025 of file ai.c.

◆ ai_init()

void ai_init ( Pilot * p)

Initializes the AI.

Parameters
pPilot to run initialization when jumping/entering.

Definition at line 929 of file ai.c.

◆ ai_load()

int ai_load ( void )

Initializes the AI stuff which is basically Lua.

Returns
0 on no errors.

Definition at line 589 of file ai.c.

◆ ai_loadEquip()

int ai_loadEquip ( void )
static

Loads the equipment selector script.

Definition at line 654 of file ai.c.

◆ ai_loadProfile()

int ai_loadProfile ( AI_Profile * prof,
const char * filename )
static

Initializes an AI_Profile and adds it to the stack.

Parameters
profAI profile to load.
[in]filenameFile to create the profile from.
Returns
0 on no error.

Definition at line 699 of file ai.c.

◆ ai_newtask()

Task * ai_newtask ( lua_State * L,
Pilot * p,
const char * func,
int subtask,
int pos )

Creates a new AI task.

Definition at line 1169 of file ai.c.

◆ ai_pinit()

int ai_pinit ( Pilot * p,
const char * ai )

Initializes the pilot in the ai.

Mainly used to create the pilot's memory table.

Parameters
pPilot to initialize in AI.
aiAI to initialize pilot.
Returns
0 on success.

Definition at line 494 of file ai.c.

◆ ai_refuel()

void ai_refuel ( Pilot * refueler,
unsigned int target )

Has a pilot attempt to refuel the other.

Parameters
refuelerPilot doing the refueling.
targetPilot to refuel.

Definition at line 1061 of file ai.c.

◆ ai_run()

void ai_run ( nlua_env env,
int nargs )
static

Attempts to run a function.

Parameters
[in]envLua env to run function in.
[in]nargsNumber of arguments to run.

Definition at line 476 of file ai.c.

◆ ai_setMemory()

int ai_setMemory ( void )
static

Sets the cur_pilot's ai.

Definition at line 397 of file ai.c.

◆ ai_setPilot()

AIMemory ai_setPilot ( Pilot * p)

Sets the pilot for further AI calls.

Parameters
pPilot to set.

Definition at line 416 of file ai.c.

◆ ai_sort()

int ai_sort ( const void * p1,
const void * p2 )
static

Definition at line 577 of file ai.c.

◆ ai_taskGC()

void ai_taskGC ( Pilot * pilot)
static

Runs the garbage collector on the pilot's tasks.

Parameters
pilotPilot to clean up.

Definition at line 359 of file ai.c.

◆ ai_tasktarget()

int ai_tasktarget ( lua_State * L,
const Task * t )
static

Pushes a task target.

Definition at line 1288 of file ai.c.

◆ ai_think()

void ai_think ( Pilot * pilot,
double dt,
int dotask )

Heart of the AI, brains of the pilot.

Parameters
pilotPilot that needs to think.
dtCurrent delta tick.
dotaskWhether or not to do the task, or just control tick.

Definition at line 812 of file ai.c.

◆ ai_thinkApply()

void ai_thinkApply ( Pilot * p)

Applies the result of thinking.

Parameters
pPilot to apply to.

Definition at line 454 of file ai.c.

◆ ai_thinkSetup()

void ai_thinkSetup ( double dt)

Sets up the pilot for thinking.

Definition at line 440 of file ai.c.

◆ ai_unsetPilot()

void ai_unsetPilot ( AIMemory oldmem)

Finishes setting up a pilot.

Definition at line 428 of file ai.c.

◆ nlua_loadAI()

int nlua_loadAI ( nlua_env env)

Definition at line 686 of file ai.c.

Variable Documentation

◆ ai_dt

double ai_dt = 0.
static

Current update tick, useful in some cases.

Definition at line 112 of file ai.c.

◆ ai_qtquery

IntList ai_qtquery
static

Quadtree query.

Definition at line 111 of file ai.c.

◆ aiL_distressmsg

char aiL_distressmsg[STRMAX_SHORT]
static

Buffer to store distress message.

Definition at line 345 of file ai.c.

◆ aiL_methods

const luaL_Reg aiL_methods[]
static

Lua AI Function table.

Definition at line 241 of file ai.c.

◆ aiL_status

int aiL_status = AI_STATUS_NORMAL
static

Current AI run status.

Definition at line 352 of file ai.c.

◆ cur_pilot

Pilot* cur_pilot = NULL

Current pilot. All functions use this.

Definition at line 340 of file ai.c.

◆ equip_env

nlua_env equip_env = LUA_NOREF
static

Equipment enviornment.

Definition at line 110 of file ai.c.

◆ pilot_acc

double pilot_acc = 0.
static

Current pilot's acceleration.

Definition at line 341 of file ai.c.

◆ pilot_flags

int pilot_flags = 0
static

Handle stuff like weapon firing.

Definition at line 343 of file ai.c.

◆ pilot_turn

double pilot_turn = 0.
static

Current pilot's turning.

Definition at line 342 of file ai.c.

◆ profiles

AI_Profile* profiles = NULL
static

Array of AI_Profiles loaded.

Definition at line 109 of file ai.c.