80#include "gatherable.h"
84#include "nlua_asteroid.h"
85#include "nlua_faction.h"
86#include "nlua_pilot.h"
104#define ai_setFlag(f) (pilot_flags |= f )
105#define ai_isFlag(f) (pilot_flags & f )
107#define AI_PRIMARY (1<<0)
108#define AI_SECONDARY (1<<1)
109#define AI_DISTRESS (1<<2)
121static void ai_run( nlua_env env,
int nargs );
126static int ai_sort(
const void *p1,
const void *p2 );
171static int aiL_aim( lua_State *L );
173static int aiL_dir( lua_State *L );
345#define AI_STATUS_NORMAL 1
346#define AI_STATUS_CREATE 2
368 pointer->
next = NULL;
397 nlua_setenv(naevL, env,
"mem");
417static void ai_run( nlua_env env,
int nargs )
419 if (nlua_pcall(env, nargs, 0)) {
439 strncpy(buf, ai,
sizeof(buf)-1);
440 buf[
sizeof(
buf)-1] =
'\0';
445 WARN( _(
"AI Profile '%s' not found, using dummy fallback."), buf);
446 snprintf(buf,
sizeof(buf),
"dummy" );
450 WARN( _(
"Dummy AI Profile not valid! Things are going to break.") );
459 lua_rawgeti( naevL, LUA_REGISTRYINDEX, prof->
lua_mem );
461 while (lua_next(naevL,-2) != 0) {
462 lua_pushvalue(naevL,-2);
463 lua_pushvalue(naevL,-2);
464 lua_remove(naevL, -3);
465 lua_settable(naevL,-5);
468 p->lua_mem = luaL_ref( naevL, LUA_REGISTRYINDEX );
472 pilot_setFlag(p, PILOT_CREATED_AI);
475 p->tcontrol = RNGF() * p->ai->control_rate;
504 if (!pilot_isPlayer(p)) {
505 luaL_unref( naevL, LUA_REGISTRYINDEX, p->lua_mem );
506 p->lua_mem = LUA_NOREF;
513static int ai_sort(
const void *p1,
const void *p2 )
517 return strcmp( ai1->
name, ai2->
name );
528 Uint32 time = SDL_GetTicks();
531 files = PHYSFS_enumerateFiles( AI_PATH );
537 for (
size_t i=0; files[i]!=NULL; i++) {
545 snprintf( path,
sizeof(path), AI_PATH
"%s", files[i] );
550 WARN( _(
"Error loading AI profile '%s'"), path);
558 PHYSFS_freeList( files );
561 time = SDL_GetTicks() - time;
578 const char *filename = AI_EQUIP_PATH;
589 if (nlua_dobufenv(
equip_env, buf, bufsize, filename) != 0) {
590 WARN( _(
"Error loading file: %s\n"
592 "Most likely Lua file has improper syntax, please check"),
593 filename, lua_tostring(naevL, -1));
617 len = strlen(filename)-strlen(AI_PATH)-strlen(
".lua");
618 prof->
name = malloc(len+1);
619 strncpy( prof->
name, &filename[strlen(AI_PATH)], len );
620 prof->
name[len] =
'\0';
631 lua_pushboolean( naevL, 1 );
632 nlua_setenv( naevL, env,
"__ai" );
636 lua_pushvalue(naevL,-1);
637 prof->
lua_mem = luaL_ref( naevL, LUA_REGISTRYINDEX );
638 nlua_setenv(naevL, env,
"mem");
642 if (nlua_dobufenv(env, buf, bufsize, filename) != 0) {
643 WARN( _(
"Error loading AI file: %s\n"
645 "Most likely Lua file has improper syntax, please check"),
646 filename, lua_tostring(naevL,-1));
655 str = _(
"AI Profile '%s' is missing '%s' function!");
658 WARN( str, filename,
"control" );
661 WARN( str, filename,
"control_manual" );
664 WARN( str, filename,
"refuel" );
667 WARN( str, filename,
"create" );
670 nlua_getenv(naevL, env,
"control_rate");
688 WARN( _(
"AI Profile '%s' not found in AI stack"), name);
723 if (pilot->
ai == NULL)
747 if (pilot_isFlag(pilot,PILOT_PLAYER) ||
748 pilot_isFlag(
cur_pilot, PILOT_MANUAL_CONTROL)) {
763 if (pilot_isFlag(pilot,PILOT_PLAYER) &&
764 !pilot_isFlag(
cur_pilot, PILOT_MANUAL_CONTROL))
771 lua_rawgeti( naevL, LUA_REGISTRYINDEX, t->
subtask->func );
774 if (data == LUA_NOREF)
778 lua_rawgeti( naevL, LUA_REGISTRYINDEX, t->
func );
782 if (data != LUA_NOREF) {
783 lua_rawgeti( naevL, LUA_REGISTRYINDEX, data );
789 if (pilot_isFlag(
cur_pilot, PILOT_MANUAL_CONTROL)) {
825 if ((p->ai==NULL) || (p->ai->ref_create==LUA_NOREF))
828 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ai->ref_create );
845 hparam[0].
type = HOOK_PARAM_PILOT;
846 hparam[0].
u.
lp = attacker;
847 hparam[1].
type = HOOK_PARAM_NUMBER;
848 hparam[1].
u.
num = dmg;
854 if (attacked->
ai == NULL)
858 if (pilot_isFlag( attacked, PILOT_MANUAL_CONTROL ))
879 if (pilot_isFlag( discovered, PILOT_MANUAL_CONTROL ))
883 if (discovered->
ai == NULL)
890 if (lua_isnil(naevL,-1)) {
913 if (recipient->
ai == NULL)
920 if (lua_isnil(naevL,-1)) {
947 t = calloc( 1,
sizeof(
Task) );
948 t->
name = strdup(
"refuel");
950 t->
func = luaL_ref(naevL, LUA_REGISTRYINDEX);
952 t->
dat = luaL_ref(naevL, LUA_REGISTRYINDEX);
971 if (pilot_isFlag( p, PILOT_MANUAL_CONTROL ))
983 if (lua_isnil(naevL,-1)) {
990 if (attacker != NULL)
1011 if (!pilot_isFlag(pilot, PILOT_CREATED_AI))
1017 char *func =
"equip_generic";
1023 nlua_getenv(naevL, env, func);
1024 nlua_pushenv(naevL, env);
1025 lua_setfenv(naevL, -2);
1027 if (nlua_pcall(env, 1, 0)) {
1028 WARN( _(
"Pilot '%s' equip '%s' -> '%s': %s"), pilot->
name, pilot->
ai->
name, func, lua_tostring(naevL, -1));
1037 if (pilot->
ai == NULL)
1044 if (!pilot_isFlag(pilot, PILOT_CREATED_AI))
1053 Task *t, *curtask, *pointer;
1056 WARN(_(
"Trying to create new task for pilot '%s' that has no AI!"), p->name);
1061 nlua_getenv( L, p->ai->env, func );
1062 luaL_checktype( L, -1, LUA_TFUNCTION );
1065 t = calloc( 1,
sizeof(
Task) );
1066 t->
name = strdup(func);
1067 t->
func = luaL_ref( L, LUA_REGISTRYINDEX );
1072 if ((pos == 1) && (p->task != NULL)) {
1073 for (pointer = p->task; pointer->next != NULL; pointer = pointer->next);
1084 if (curtask == NULL) {
1086 NLUA_ERROR( L, _(
"Trying to add subtask '%s' to non-existent task."), func);
1090 if ((pos == 1) && (curtask->
subtask != NULL)) {
1091 for (pointer = curtask->
subtask; pointer->next != NULL; pointer = pointer->next);
1110 if (t->
func != LUA_NOREF)
1111 luaL_unref(naevL, LUA_REGISTRYINDEX, t->
func);
1113 if (t->
dat != LUA_NOREF)
1114 luaL_unref(naevL, LUA_REGISTRYINDEX, t->
dat);
1123 if (t->
next != NULL) {
1138 const char *func = luaL_checkstring(L,1);
1147 if (lua_gettop(L) > 1) {
1149 t->
dat = luaL_ref(L, LUA_REGISTRYINDEX);
1160 if (t->
dat == LUA_NOREF)
1162 lua_rawgeti(L, LUA_REGISTRYINDEX, t->
dat);
1200 NLUA_ERROR(L, _(
"Trying to pop task when there are no tasks on the stack."));
1217 lua_pushstring(L, t->
name);
1266 NLUA_ERROR(L, _(
"Trying to pop task when there are no tasks on the stack."));
1270 NLUA_ERROR(L, _(
"Trying to pop subtask when there are no subtasks for the task '%s'."), t->
name);
1291 if ((t != NULL) && (t->
subtask != NULL))
1292 lua_pushstring(L, t->
subtask->name);
1309 if ((t == NULL) || (t->
subtask == NULL))
1363 int candidate_id = -1;
1376 if (candidate_id == -1)
1405 NLUA_INVALID_PARAMETER(L);
1432 NLUA_INVALID_PARAMETER(L);
1448 vec2 perp_motion_unit, offset_vect;
1449 int offset_distance;
1462 NLUA_INVALID_PARAMETER(L);
1466 offset_distance = vec2_dot(&perp_motion_unit, &offset_vect);
1468 lua_pushnumber(L, offset_distance);
1487 double time, dist, vel;
1491 if (lua_gettop(L) > 0) {
1520 lua_pushnumber(L, dist);
1534 lua_pushboolean(L, pilot_isWithPlayer(p) && pilot_isFlag(
cur_pilot, PILOT_BRIBED));
1588 if (pilot_isWithPlayer(p)) {
1610 if (pilot_isWithPlayer(p)) {
1667 double n = luaL_optnumber( L, 1, 1. );
1700 double k_diff, k_vel, diff, vx, vy, dx, dy;
1707 tv = &p->solid->pos;
1709 else if (lua_isnumber(L,1)) {
1710 double d = lua_tonumber(L,1);
1714 NLUA_INVALID_PARAMETER(L);
1719 NLUA_INVALID_PARAMETER(L);
1726 if (lua_toboolean(L,2))
1730 vel = lua_toboolean(L, 3);
1747 if (vel && (dx || dy)) {
1749 double d = (vx * dx + vy * dy) / (dx*dx + dy*dy);
1766 lua_pushnumber(L,
ABS(diff));
1792 double k_diff, k_goal, k_enemy, k_mult,
1793 d, diff, dist, factor;
1805 tv = &p->solid->pos;
1807 else if (lua_isnumber(L,1)) {
1808 d = (double)lua_tonumber(L,1);
1812 NLUA_INVALID_PARAMETER(L);
1817 NLUA_INVALID_PARAMETER(L);
1825 vec2_cset( &F, 0., 0.) ;
1827 dist = VMOD(F1) + 0.1;
1828 vec2_cset( &F1, F1.
x * k_goal / dist, F1.
y * k_goal / dist) ;
1835 if (pilot_isDisabled(p_i) )
continue;
1837 if (p_i->
id == p->id)
continue;
1842 if (dist < 750)
continue;
1848 factor = k_enemy * k_mult / (dist*dist*dist);
1854 vec2_cset( &F, F.x + F1.
x, F.y + F1.
y );
1863 lua_pushnumber(L,
ABS(diff));
1878 double diff, mod, angle;
1894 lua_pushnumber(L,
ABS(diff));
1908 vec2 *vec, drift, reference_vector;
1910 double diff, heading_offset_azimuth, drift_radial, drift_azimuthal;
1921 else NLUA_INVALID_PARAMETER(L);
1939 vec2_uv(&drift_radial, &drift_azimuthal, &drift, &reference_vector);
1940 heading_offset_azimuth = angle_diff(
cur_pilot->
solid->
dir, VANGLE(reference_vector));
1946 if (
FABS(heading_offset_azimuth) < M_PI_2) {
1951 speedmap = -1*copysign(1 - 1 / (
FABS(drift_azimuthal/200) + 1), drift_azimuthal) * M_PI_2;
1952 diff = angle_diff(heading_offset_azimuth, speedmap);
1953 azimuthal_sign = -1;
1974 if (heading_offset_azimuth >0)
1981 lua_pushnumber(L,
ABS(diff));
2006 vec2_cset( &tv, VX(p->solid->pos), VY(p->solid->pos) );
2010 else NLUA_INVALID_PARAMETER(L);
2016 (n==-1) ? VANGLE(sv) :
2017 vec2_angle(&sv, &tv));
2024 lua_pushnumber(L, diff);
2038 vec2 *vec, drift, reference_vector;
2040 double diff, heading_offset_azimuth, drift_radial, drift_azimuthal;
2051 else NLUA_INVALID_PARAMETER(L);
2069 vec2_uv(&drift_radial, &drift_azimuthal, &drift, &reference_vector);
2070 heading_offset_azimuth = angle_diff(
cur_pilot->
solid->
dir, VANGLE(reference_vector));
2076 if (
FABS(heading_offset_azimuth) < M_PI_2) {
2081 speedmap = -1*copysign(1 - 1 / (
FABS(drift_azimuthal/200) + 1), drift_azimuthal) * M_PI_2;
2082 diff = angle_diff(heading_offset_azimuth, speedmap);
2094 lua_pushnumber(L, diff);
2107 lua_pushnumber(L, drift);
2124 lua_pushboolean(L, ret);
2142 if (!spob_hasService(
cur_system->spobs[i],SPOB_SERVICE_INHABITED))
2153 if (j == -1)
return 0;
2180 if (!spob_hasService(
cur_system->spobs[i],SPOB_SERVICE_INHABITED))
2191 if (j == -1)
return 0;
2241 if (pilot_isFlag(
cur_pilot, PILOT_NOLAND))
2245 only_friend = lua_toboolean(L, 1);
2254 if (!spob_hasService(pnt, SPOB_SERVICE_LAND))
2256 if (!spob_hasService(pnt, SPOB_SERVICE_INHABITED))
2298 if (!lua_isnoneornil(L,1)) {
2309 NLUA_ERROR( L, _(
"Spob '%s' not found in system '%s'"), pnt->
name,
cur_system->name );
2325 if ((spob->
lua_can_land==LUA_NOREF) && !spob_hasService(spob,SPOB_SERVICE_LAND)) {
2326 lua_pushboolean(L,0);
2337 if (pilot_isFlag(
cur_pilot, PILOT_NOLAND)) {
2338 lua_pushboolean(L,0);
2344 lua_pushboolean(L,0);
2350 lua_pushboolean(L,0);
2357 pilot_setFlag(
cur_pilot, PILOT_LANDING );
2360 lua_rawgeti(naevL, LUA_REGISTRYINDEX, spob->
lua_land);
2363 if (nlua_pcall( spob->
lua_env, 2, 0 )) {
2364 WARN(_(
"Spob '%s' failed to run '%s':\n%s"), spob->
name,
"land", lua_tostring(naevL,-1));
2369 hparam.
type = HOOK_PARAM_SPOB;
2370 hparam.
u.
la = spob->
id;
2373 lua_pushboolean(L,1);
2389 if (!lua_isnoneornil(L,1)) {
2393 NLUA_ERROR(L, _(
"Jump point must be in current system."));
2404 lua_pushnumber(L,dist);
2426 NLUA_ERROR(L, _(
"Jump point must be in current system."));
2432 a = RNGF() * M_PI * 2.;
2433 rad = RNGF() * 0.5 * jp->radius;
2434 vec2_cadd( &vec, rad*cos(a), rad*sin(a) );
2454 double mindist, dist;
2465 if ((!useshidden && jp_isFlag(jiter, JP_HIDDEN)) || jp_isFlag(jiter, JP_EXITONLY))
2472 if (dist < mindist) {
2481 lj.
destid = jp->targetid;
2515 if ((!useshidden && jp_isFlag(jiter, JP_HIDDEN)) || jp_isFlag(jiter, JP_EXITONLY))
2532 if ((!useshidden && jp_isFlag(jiter, JP_HIDDEN)) || jp_isFlag(jiter, JP_EXITONLY))
2541 WARN(_(
"Pilot '%s' can't find jump to leave system!"),
cur_pilot->
name);
2548 lj.
destid = jumps[r]->targetid;
2573 if (lua_gettop(L) > 1)
2574 absolute = lua_toboolean(L,2);
2583 vec2_cset( &vv, p->solid->vel.x, p->solid->vel.y);
2587 dot = vec2_dot( &pv, &vv );
2588 mod =
MAX(VMOD(pv), 1.);
2590 lua_pushnumber(L, dot / mod );
2609 vec2 point, cons, goal, pv;
2610 double radius, angle, Kp, Kd, angle2;
2616 radius = luaL_checknumber(L,2);
2617 angle = luaL_checknumber(L,3);
2618 Kp = luaL_checknumber(L,4);
2619 Kd = luaL_checknumber(L,5);
2621 if (lua_isnoneornil(L, 6))
2622 method =
"velocity";
2624 method = luaL_checkstring(L,6);
2626 if (strcmp( method,
"absolute" ) == 0)
2628 else if (strcmp( method,
"keepangle" ) == 0) {
2629 vec2_cset( &pv, p->solid->pos.x - target->
solid->
pos.
x,
2630 p->solid->pos.y - target->
solid->
pos.
y );
2631 angle2 = VANGLE(pv);
2634 angle2 = angle + VANGLE( target->
solid->
vel );
2636 vec2_cset( &point, VX(target->
solid->
pos) + radius * cos(angle2),
2637 VY(target->
solid->
pos) + radius * sin(angle2) );
2640 vec2_cset( &cons, (point.
x - p->solid->pos.x) * Kp +
2641 (target->
solid->
vel.
x - p->solid->vel.x) *Kd,
2642 (point.
y - p->solid->pos.y) * Kp +
2643 (target->
solid->
vel.
y - p->solid->vel.y) *Kd );
2645 vec2_cset( &goal, cons.
x + p->solid->pos.x, cons.
y + p->solid->pos.y);
2668 vec2 point, cons, goal, *pos, *vel;
2669 double radius, angle, Kp, Kd;
2673 radius = luaL_checknumber(L,3);
2674 angle = luaL_checknumber(L,4);
2675 Kp = luaL_checknumber(L,5);
2676 Kd = luaL_checknumber(L,6);
2678 vec2_cset( &point, pos->
x + radius * cos(angle),
2679 pos->
y + radius * sin(angle) );
2682 vec2_cset( &cons, (point.
x - p->solid->pos.x) * Kp +
2683 (vel->
x - p->solid->vel.x) *Kd,
2684 (point.
y - p->solid->pos.y) * Kp +
2685 (vel->
y - p->solid->vel.y) *Kd );
2687 vec2_cset( &goal, cons.
x + p->solid->pos.x, cons.
y + p->solid->pos.y);
2733 if (lua_gettop(L) > 0) {
2734 int i = lua_toboolean(L,1);
2793 if ((lua_gettop(L) < 1) || lua_isnil(L,1))
2796 rad = lua_tonumber(L,1);
2801 lua_pushnumber(L,i);
2821 i = lua_tointeger(L,1);
2848 id = lua_tonumber(L,1);
2850 if (lua_gettop(L) > 1)
2851 type = lua_toboolean(L,2);
2855 ws = &p->weapon_sets[id];
2857 if (ws->
type == WEAPSET_TYPE_ACTIVE) {
2861 for (
int i=0; i<l; i++) {
2862 if (p->outfits[ ws->
slots[i].
slotid ]->state == PILOT_OUTFIT_OFF) {
2944 if (pilot_isFlag(
cur_pilot, PILOT_COOLDOWN))
2947 if (lua_toboolean(L,1))
2985 unsigned int LB, UB;
2989 LB = luaL_checklong(L,1);
2990 UB = luaL_checklong(L,2);
2993 NLUA_ERROR(L, _(
"Invalid Bounds"));
3018 double mass_factor = luaL_checknumber(L,1);
3019 double health_factor = luaL_checknumber(L,2);
3020 double damage_factor = luaL_checknumber(L,3);
3021 double range_factor = luaL_checknumber(L,4);
3024 mass_factor, health_factor, damage_factor, 1./range_factor );
3043 if (pilot_isWithPlayer(p))
3060 int level = luaL_optinteger( L, 2, -1 );
3076 int level = luaL_optinteger( L, 2, -1 );
3092 int level = luaL_optinteger( L, 2, -1 );
3109 if (!pilot_isDisabled(p)) {
3110 lua_pushboolean(L, 0);
3115 lua_pushboolean(L, !pilot_isFlag(p, PILOT_BOARDED));
3194 int n = luaL_checkint(L,1);
3210 int n = luaL_checkint(L,1);
3247 if (lua_isstring(L,1))
3249 else if (lua_isnil(L,1))
3252 NLUA_INVALID_PARAMETER(L);
3322 if (lua_gettop(L)>0)
3323 b = lua_toboolean(L,1);
3327 lua_pushboolean(L,1);
Task * ai_newtask(lua_State *L, Pilot *p, const char *func, int subtask, int pos)
Creates a new AI task.
static nlua_env equip_env
static AI_Profile * profiles
static void ai_setMemory(void)
Sets the cur_pilot's ai.
void ai_getDistress(Pilot *p, const Pilot *distressed, const Pilot *attacker)
Sends a distress signal to a pilot.
Task * ai_curTask(Pilot *pilot)
Gets the current running task.
void ai_refuel(Pilot *refueler, unsigned int target)
Has a pilot attempt to refuel the other.
static const luaL_Reg aiL_methods[]
static int ai_loadProfile(AI_Profile *prof, const char *filename)
Initializes an AI_Profile and adds it to the stack.
static void ai_create(Pilot *pilot)
Runs the create() function in the pilot.
void ai_cleartasks(Pilot *p)
Clears the pilot's tasks.
void ai_freetask(Task *t)
Frees an AI task.
static Task * ai_createTask(lua_State *L, int subtask)
Creates a new task based on stack information.
static void ai_run(nlua_env env, int nargs)
Attempts to run a function.
static char aiL_distressmsg[STRMAX_SHORT]
void ai_attacked(Pilot *attacked, const unsigned int attacker, double dmg)
Triggers the attacked() function in the pilot's AI.
void ai_destroy(Pilot *p)
Destroys the ai part of the pilot.
void ai_hail(Pilot *recipient)
Triggers the hail() function in the pilot's AI.
static int ai_tasktarget(lua_State *L, Task *t)
Pushes a task target.
void ai_discovered(Pilot *discovered)
Triggers the discovered() function in the pilot's AI.
void ai_exit(void)
Cleans up global AI.
static int ai_loadEquip(void)
Loads the equipment selector script.
void ai_setPilot(Pilot *p)
Sets the pilot for further AI calls.
void ai_think(Pilot *pilot, const double dt)
Heart of the AI, brains of the pilot.
void ai_init(Pilot *p)
Initializes the AI.
static void ai_taskGC(Pilot *pilot)
Runs the garbage collector on the pilot's tasks.
int ai_pinit(Pilot *p, const char *ai)
Initializes the pilot in the ai.
int ai_load(void)
Initializes the AI stuff which is basically Lua.
AI_Profile * ai_getProfile(const char *name)
Gets the AI_Profile by name.
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
int pilot_board(Pilot *p)
Has a pilot attempt to board another pilot.
nlua_env faction_getEquipper(int f)
Gets the equipper state associated to the faction scheduler.
int areEnemies(int a, int b)
Checks whether two factions are enemies.
int faction_usesHiddenJumps(int f)
Checks to see if a faction uses hidden jumps.
int areAllies(int a, int b)
Checks whether two factions are allies or not.
int gatherable_getPos(vec2 *pos, vec2 *vel, int id)
Returns the position and velocity of a gatherable.
int gatherable_getClosest(vec2 pos, double rad)
Gets the closest gatherable from a given position, within a given radius.
static int aiL_ismaxvel(lua_State *L)
Checks to see if pilot is at maximum velocity.
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_relhp(lua_State *L)
Gets the relative health (total shields and armour) between the current pilot and the specified targe...
static int aiL_sethyptarget(lua_State *L)
Sets hyperspace target.
static int aiL_getspobfrompos(lua_State *L)
Get the nearest friendly spob to a given position.
static int aiL_getlandspob(lua_State *L)
Get a random friendly spob.
static int aiL_accel(lua_State *L)
Starts accelerating the pilot.
static int aiL_weapSet(lua_State *L)
Sets the active weapon set, fires another weapon set or activate an outfit.
static int aiL_poptask(lua_State *L)
Pops the current running task.
static int aiL_iface(lua_State *L)
Maintains an intercept pursuit course.
static int aiL_settarget(lua_State *L)
Sets the pilot's target.
static int aiL_nearhyptarget(lua_State *L)
Gets the nearest hyperspace target.
static int aiL_getrndspob(lua_State *L)
Get a random spob.
static int aiL_pilot(lua_State *L)
Gets the AI's pilot. Lua return parameter: Pilot The AI's pilot.
static int aiL_minbrakedist(lua_State *L)
Gets the minimum braking distance.
static int aiL_gatherablePos(lua_State *L)
Gets the pos and vel of a given gatherable.
static int aiL_getenemy_heuristic(lua_State *L)
Gets the nearest enemy within specified heuristic.
static int aiL_relvel(lua_State *L)
Gets the relative velocity of a pilot.
static int aiL_haslockon(lua_State *L)
Checks to see if pilot has a missile lockon.
static int aiL_refuel(lua_State *L)
Attempts to refuel the pilot's target.
static int aiL_getdistance2(lua_State *L)
Gets the squared distance from the pointer.
static int aiL_getdistance(lua_State *L)
Gets the distance from the pointer.
static int aiL_instantJump(lua_State *L)
Checks to see if pilot can instant jump.
static int aiL_land(lua_State *L)
Lands on a spob.
static int aiL_isenemy(lua_State *L)
Checks to see if target is an enemy.
static int aiL_messages(lua_State *L)
Returns and clears the pilots message queue.
static int aiL_turn(lua_State *L)
Starts turning the pilot.
static int aiL_subtaskname(lua_State *L)
Gets the current subtask's name. Lua return parameter: string The current subtask name or nil if ther...
static int aiL_rndhyptarget(lua_State *L)
Gets a random hyperspace target.
static int aiL_dir(lua_State *L)
calculates the direction that the target is relative to the current pilot facing.
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_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 da...
static int aiL_hasturrets(lua_State *L)
Does the pilot have turrets?
static int aiL_isally(lua_State *L)
Checks to see if target is an ally.
static int aiL_relsize(lua_State *L)
Gets the relative size (ship mass) between the current pilot and the specified target.
static int aiL_aim(lua_State *L)
Aims at a pilot, trying to hit it rather than move to it.
static int aiL_credits(lua_State *L)
Sets the pilots credits. Only call in create().
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_canboard(lua_State *L)
Checks to see if pilot can board the target.
static int aiL_idir(lua_State *L)
calculates angle between pilot facing and intercept-course to target.
static int aiL_getnearestpilot(lua_State *L)
gets the nearest pilot to the current pilot
static int aiL_hostile(lua_State *L)
Sets the enemy hostile (basically notifies of an impending attack).
static int aiL_hascannons(lua_State *L)
Does the pilot have cannons?
static int aiL_getBoss(lua_State *L)
Picks a pilot that will command the current pilot.
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_taskname(lua_State *L)
Gets the current task's name. Lua return parameter: string The current task name or nil if there are ...
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 n...
static int aiL_hasprojectile(lua_State *L)
Checks to see if pilot has a projectile after him.
static int aiL_getweapammo(lua_State *L)
Gets the ammo of a weapon.
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_shoot_indicator(lua_State *L)
Access the seeker shoot indicator (that is put to true each time a seeker is shot).
static int aiL_combat(lua_State *L)
Sets the combat flag.
static int aiL_distress(lua_State *L)
Sends a distress signal.
static int aiL_settimer(lua_State *L)
Sets a timer.
static int aiL_shoot(lua_State *L)
Makes the pilot shoot.
static int aiL_setasterotarget(lua_State *L)
Sets the pilot's asteroid target.
static int aiL_getweapspeed(lua_State *L)
Gets the speed of a weapon.
static int aiL_getrndpilot(lua_State *L)
Gets a random pilot in the system. Lua return parameter: Pilot|nil.
static int aiL_set_shoot_indicator(lua_State *L)
Set the seeker shoot indicator.
static int aiL_getenemy(lua_State *L)
Gets the nearest enemy.
static int aiL_stealth(lua_State *L)
Tries to stealth or destealth the pilot.
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 fu...
static int aiL_stop(lua_State *L)
Completely stops the pilot if it is below minimum vel error (no insta-stops).
static int aiL_getflybydistance(lua_State *L)
Gets the distance from the pointer perpendicular to the current pilot's flight vector.
static int aiL_popsubtask(lua_State *L)
Pops the current running task.
static int aiL_face(lua_State *L)
Faces the target.
static int aiL_timeup(lua_State *L)
Checks a timer.
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 cal...
static int aiL_scandone(lua_State *L)
Checks to see if pilot has finished scanning their target.
static int aiL_getweaprange(lua_State *L)
Gets the range of a weapon.
static int aiL_dock(lua_State *L)
Docks the ship.
static int aiL_getenemy_size(lua_State *L)
Gets the nearest enemy within specified size bounds.
static int aiL_hyperspace(lua_State *L)
Tries to enter hyperspace.
static int aiL_hasafterburner(lua_State *L)
Does the pilot have afterburners?
static int aiL_board(lua_State *L)
Attempts to board the pilot's target.
static int aiL_hasfighterbays(lua_State *L)
Does the pilot have fighter bays?
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_isstopped(lua_State *L)
Checks to see if pilot is stopped.
void naev_renderLoadscreen(void)
Renders the loadscreen if necessary.
Header file with generic functions and naev-specifics.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
int ndata_matchExt(const char *path, const char *ext)
Sees if a file matches an extension.
static char buf[NEWS_MAX_LENGTH]
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
int nlua_refenvtype(nlua_env env, const char *name, int type)
Gets the reference of a global in a lua environment if it matches a type.
int lua_isasteroid(lua_State *L, int ind)
Checks to see if ind is a asteroid.
LuaAsteroid_t * luaL_checkasteroid(lua_State *L, int ind)
Gets asteroid at index or raises error if there is no asteroid at index.
Asteroid * luaL_validasteroid(lua_State *L, int ind)
Gets asteroid at index raising an error if type doesn't match.
LuaJump * luaL_checkjump(lua_State *L, int ind)
Gets jump at index raising an error if isn't a jump.
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
JumpPoint * luaL_validjump(lua_State *L, int ind)
Gets a jump directly.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Pilot * luaL_validpilot(lua_State *L, int ind)
Makes sure the pilot is valid or raises a Lua error.
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
unsigned int pilot_getNearestEnemy_size(const Pilot *p, double target_mass_LB, double target_mass_UB)
Gets the nearest enemy to the pilot closest to the pilot whose mass is between LB and UB.
void pilot_setThrust(Pilot *p, double thrust)
Sets the pilot's thrust.
int pilot_isHostile(const Pilot *p)
Checks to see if pilot is hostile to the player.
double pilot_relhp(const Pilot *cur_pilot, const Pilot *p)
Gets the relative hp(combined shields and armour) between the current pilot and the specified target.
double pilot_relsize(const Pilot *cur_pilot, const Pilot *p)
Gets the relative size(shipmass) between the current pilot and the specified target.
double pilot_reldps(const Pilot *cur_pilot, const Pilot *p)
Gets the relative damage output(total DPS) between the current pilot and the specified target.
unsigned int pilot_getNearestEnemy(const Pilot *p)
Gets the nearest enemy to the pilot.
void pilot_setTurn(Pilot *p, double turn)
Sets the pilot's turn.
int pilot_isFriendly(const Pilot *p)
Checks to see if pilot is friendly to the player.
int pilot_refuelStart(Pilot *p)
Attempts to start refueling the pilot's target.
void pilot_setHostile(Pilot *p)
Marks pilot as hostile to player.
static Pilot ** pilot_stack
void pilot_distress(Pilot *p, Pilot *attacker, const char *msg)
Has the pilot broadcast a distress signal.
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
int pilot_canTarget(const Pilot *p)
Same as pilot_validTarget but without the range check.
unsigned int pilot_getNearestEnemy_heuristic(const Pilot *p, double mass_factor, double health_factor, double damage_factor, double range_factor)
Gets the nearest enemy to the pilot closest to the pilot whose mass is between LB and UB.
double pilot_aimAngle(Pilot *p, const vec2 *pos, const vec2 *vel)
Returns the angle for a pilot to aim at another pilot.
int pilot_brake(Pilot *p)
Causes the pilot to turn around and brake.
unsigned int pilot_getBoss(const Pilot *p)
Get the strongest ally in a given range.
void pilot_setTarget(Pilot *p, unsigned int id)
Sets the target of the pilot.
int pilot_inRangePilot(const Pilot *p, const Pilot *target, double *dist2)
Check to see if a pilot is in sensor range of another.
int pilot_ewScanCheck(const Pilot *p)
Checks to see if a scan is done.
void pilot_destealth(Pilot *p)
Destealths a pilot.
int pilot_stealth(Pilot *p)
Stealths a pilot.
int pilot_runHookParam(Pilot *p, int hook_type, const HookParam *param, int nparam)
Tries to run a pilot hook if he has it.
int pilot_runHook(Pilot *p, int hook_type)
Tries to run a pilot hook if he has it.
void pilot_healLanded(Pilot *pilot)
Cures the pilot as if he was landed.
int pilot_dock(Pilot *p, Pilot *target)
Docks the pilot on its target pilot.
int pilot_shoot(Pilot *p, int level)
Makes the pilot shoot.
void pilot_shootStop(Pilot *p, int level)
Have pilot stop shooting their weapon.
void pilot_weapSetAIClear(Pilot *p)
Useful function for AI, clears activeness of all weapon sets.
double pilot_weapSetAmmo(Pilot *p, int id, int level)
Gets the ammo of the current pilot weapon set.
void pilot_weapSetPress(Pilot *p, int id, int type)
Handles a weapon set press.
double pilot_weapSetSpeed(Pilot *p, int id, int level)
Gets the speed of the current pilot weapon set.
double pilot_weapSetRange(Pilot *p, int id, int level)
Gets the range of the current pilot weapon set.
int spob_index(const Spob *p)
Gets the ID of a spob.
double system_getPresence(const StarSystem *sys, int faction)
Get the presence of a faction in a system.
int space_hyperspace(Pilot *p)
Tries to get the pilot into hyperspace.
Represents a single asteroid.
The actual hook parameter.
A weapon set represents a set of weapons that have an action.
PilotWeaponSetOutfit * slots
The representation of an in-game pilot.
double timer[MAX_AI_TIMERS]
unsigned int shoot_indicator
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...