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)
123static void ai_run( nlua_env env,
int nargs );
128static int ai_sort(
const void *p1,
const void *p2 );
172static int aiL_aim( lua_State *L );
173static int aiL_dir( lua_State *L );
347#define AI_STATUS_NORMAL 1
348#define AI_STATUS_CREATE 2
370 pointer->
next = NULL;
400 nlua_getenv( naevL, env,
"mem" );
401 oldmem = luaL_ref( naevL, LUA_REGISTRYINDEX );
404 nlua_setenv(naevL, env,
"mem");
429 lua_rawgeti( naevL, LUA_REGISTRYINDEX, oldmem.
mem );
430 nlua_setenv( naevL, env,
"mem");
431 luaL_unref( naevL, LUA_REGISTRYINDEX, oldmem.
mem );
479static void ai_run( nlua_env env,
int nargs )
481 if (nlua_pcall(env, nargs, 0)) {
501 strncpy(buf, ai,
sizeof(buf)-1);
502 buf[
sizeof(buf)-1] =
'\0';
507 WARN( _(
"AI Profile '%s' not found, using dummy fallback."), buf);
511 WARN( _(
"Dummy AI Profile not valid! Things are going to break.") );
520 lua_rawgeti( naevL, LUA_REGISTRYINDEX, prof->
lua_mem );
522 while (lua_next(naevL,-2) != 0) {
523 lua_pushvalue(naevL,-2);
524 lua_pushvalue(naevL,-2);
525 lua_remove(naevL, -3);
526 lua_settable(naevL,-5);
529 p->lua_mem = luaL_ref( naevL, LUA_REGISTRYINDEX );
533 pilot_setFlag(p, PILOT_CREATED_AI);
540 p->tcontrol = RNGF() * p->ai->control_rate;
569 if (!pilot_isPlayer(p)) {
570 luaL_unref( naevL, LUA_REGISTRYINDEX, p->lua_mem );
571 p->lua_mem = LUA_NOREF;
578static int ai_sort(
const void *p1,
const void *p2 )
582 return strcmp( ai1->
name, ai2->
name );
594 Uint32 time = SDL_GetTicks();
598 files = PHYSFS_enumerateFiles( AI_PATH );
604 for (
size_t i=0; files[i]!=NULL; i++) {
612 snprintf( path,
sizeof(path), AI_PATH
"%s", files[i] );
617 WARN( _(
"Error loading AI profile '%s'"), path);
625 PHYSFS_freeList( files );
629 time = SDL_GetTicks() - time;
650 const char *filename = AI_EQUIP_PATH;
661 if (nlua_dobufenv(
equip_env, buf, bufsize, filename) != 0) {
662 WARN( _(
"Error loading file: %s\n"
664 "Most likely Lua file has improper syntax, please check"),
665 filename, lua_tostring(naevL, -1));
673int nlua_loadAI( nlua_env env )
695 len = strlen(filename)-strlen(AI_PATH)-strlen(
".lua");
696 prof->
name = malloc(len+1);
697 strncpy( prof->
name, &filename[strlen(AI_PATH)], len );
698 prof->
name[len] =
'\0';
709 lua_pushboolean( naevL, 1 );
710 nlua_setenv( naevL, env,
"__ai" );
714 lua_pushvalue(naevL,-1);
715 prof->
lua_mem = luaL_ref( naevL, LUA_REGISTRYINDEX );
716 nlua_setenv(naevL, env,
"mem");
720 if (nlua_dobufenv(env, buf, bufsize, filename) != 0) {
721 WARN( _(
"Error loading AI file: %s\n"
723 "Most likely Lua file has improper syntax, please check"),
724 filename, lua_tostring(naevL,-1));
733 str = _(
"AI Profile '%s' is missing '%s' function!");
736 WARN( str, filename,
"control" );
739 WARN( str, filename,
"control_manual" );
742 WARN( str, filename,
"refuel" );
745 WARN( str, filename,
"create" );
748 nlua_getenv(naevL, env,
"control_rate");
763 const AI_Profile ai = { .name = (
char*)name };
766 WARN( _(
"AI Profile '%s' not found in AI stack"), name);
804 if (pilot->
ai == NULL)
811 pilot_rmFlag( pilot, PILOT_SCANNING );
826 if (pilot_isFlag(pilot,PILOT_PLAYER) ||
827 pilot_isFlag(
cur_pilot, PILOT_MANUAL_CONTROL)) {
853 lua_rawgeti( naevL, LUA_REGISTRYINDEX, t->
subtask->func );
856 if (data == LUA_NOREF)
860 lua_rawgeti( naevL, LUA_REGISTRYINDEX, t->
func );
864 if (data != LUA_NOREF) {
865 lua_rawgeti( naevL, LUA_REGISTRYINDEX, data );
871 if (pilot_isFlag(
cur_pilot, PILOT_MANUAL_CONTROL)) {
896 if ((p->ai==NULL) || (p->ai->ref_create==LUA_NOREF))
899 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ai->ref_create );
917 hparam[0].
type = HOOK_PARAM_PILOT;
918 hparam[0].
u.
lp = attacker;
919 hparam[1].
type = HOOK_PARAM_NUMBER;
920 hparam[1].
u.
num = dmg;
923 pilot_runHookParam( attacked, PILOT_HOOK_ATTACKED, hparam, 2 );
926 if (attacked->
ai == NULL)
930 if (pilot_isFlag( attacked, PILOT_MANUAL_CONTROL ))
954 if (pilot_isFlag( discovered, PILOT_MANUAL_CONTROL ))
958 if (discovered->
ai == NULL)
965 if (lua_isnil(naevL,-1)) {
992 if (recipient->
ai == NULL)
999 if (lua_isnil(naevL,-1)) {
1028 t = calloc( 1,
sizeof(
Task) );
1029 t->
name = strdup(
"refuel");
1031 t->
func = luaL_ref(naevL, LUA_REGISTRYINDEX);
1033 t->
dat = luaL_ref(naevL, LUA_REGISTRYINDEX);
1052 if (pilot_isFlag( p, PILOT_MANUAL_CONTROL ))
1059 if (attacker != NULL)
1063 pilot_msg( distressed, p,
"distress", -1 );
1077 if (!pilot_isFlag(pilot, PILOT_CREATED_AI))
1083 char *func =
"equip_generic";
1089 nlua_getenv(naevL, env, func);
1090 nlua_pushenv(naevL, env);
1091 lua_setfenv(naevL, -2);
1093 if (nlua_pcall(env, 1, 0)) {
1094 WARN( _(
"Pilot '%s' equip '%s' -> '%s': %s"), pilot->
name, pilot->
ai->
name, func, lua_tostring(naevL, -1));
1103 if (pilot->
ai == NULL)
1110 if (!pilot_isFlag(pilot, PILOT_CREATED_AI))
1122 NLUA_ERROR( L, _(
"Trying to create new task for pilot '%s' that has no AI!"), p->name );
1127 nlua_getenv( L, p->ai->env, func );
1128 luaL_checktype( L, -1, LUA_TFUNCTION );
1131 t = calloc( 1,
sizeof(
Task) );
1132 t->
name = strdup(func);
1133 t->
func = luaL_ref( L, LUA_REGISTRYINDEX );
1138 if ((pos == 1) && (p->task != NULL)) {
1139 for (pointer = p->task; pointer->next != NULL; pointer = pointer->next);
1150 if (curtask == NULL) {
1152 NLUA_ERROR( L, _(
"Trying to add subtask '%s' to non-existent task."), func);
1157 if ((pos == 1) && (curtask->
subtask != NULL)) {
1158 for (pointer = curtask->
subtask; pointer->next != NULL; pointer = pointer->next);
1177 if (t->
func != LUA_NOREF)
1178 luaL_unref(naevL, LUA_REGISTRYINDEX, t->
func);
1180 if (t->
dat != LUA_NOREF)
1181 luaL_unref(naevL, LUA_REGISTRYINDEX, t->
dat);
1190 if (t->
next != NULL) {
1205 const char *func = luaL_checkstring(L,1);
1213 NLUA_ERROR( L, _(
"Failed to create new task for pilot '%s'."),
cur_pilot->
name );
1218 if (lua_gettop(L) > 1) {
1220 t->
dat = luaL_ref(L, LUA_REGISTRYINDEX);
1231 if (t->
dat == LUA_NOREF)
1233 lua_rawgeti(L, LUA_REGISTRYINDEX, t->
dat);
1272 WARN(_(
"Trying to pop task when there are no tasks on the stack."));
1296 lua_pushstring(L, t->
name);
1345 return NLUA_ERROR(L, _(
"Trying to pop task when there are no tasks on the stack."));
1347 return NLUA_ERROR(L, _(
"Trying to pop subtask when there are no subtasks for the task '%s'."), t->
name);
1366 if ((t != NULL) && (t->
subtask != NULL))
1367 lua_pushstring(L, t->
subtask->name);
1384 if ((t == NULL) || (t->
subtask == NULL))
1438 int candidate_id = -1;
1451 if (candidate_id == -1)
1480 NLUA_INVALID_PARAMETER(L,1);
1507 NLUA_INVALID_PARAMETER(L,1);
1523 vec2 perp_motion_unit, offset_vect;
1524 int offset_distance;
1537 NLUA_INVALID_PARAMETER(L,1);
1541 offset_distance = vec2_dot(&perp_motion_unit, &offset_vect);
1543 lua_pushnumber(L, offset_distance);
1563 if (lua_gettop(L) > 0) {
1564 double time, dist, vel;
1582 lua_pushnumber(L, dist);
1599 lua_pushboolean(L, pilot_isWithPlayer(p) && pilot_isFlag(
cur_pilot, PILOT_BRIBED));
1653 if (pilot_isWithPlayer(p)) {
1675 if (pilot_isWithPlayer(p)) {
1720 pilot_setFlag(
cur_pilot, PILOT_SCANNING );
1733 double n = luaL_optnumber( L, 1, 1. );
1766 double k_diff, k_vel, diff, vx, vy, dx, dy;
1774 if (lua_toboolean(L,2))
1783 else if (lua_isnumber(L,1)) {
1784 double d = lua_tonumber(L,1);
1789 lua_pushnumber(L,
ABS(diff));
1795 NLUA_INVALID_PARAMETER(L,1);
1798 vel = lua_toboolean(L, 3);
1815 if (vel && (dx || dy)) {
1817 double d = (vx * dx + vy * dy) / (dx*dx + dy*dy);
1834 lua_pushnumber(L,
ABS(diff));
1859 double d, diff, dist;
1865 const double k_goal = 1.;
1866 const double k_enemy = 6e6;
1878 else if (lua_isnumber(L,1)) {
1879 d = (double)lua_tonumber(L,1);
1883 NLUA_INVALID_PARAMETER(L,1);
1888 NLUA_INVALID_PARAMETER(L,1);
1891 vec2_csetmin( &F, 0., 0.) ;
1893 dist = VMOD(F1) + 0.1;
1894 vec2_cset( &F1, F1.
x * k_goal / dist, F1.
y * k_goal / dist) ;
1903 pilot_collideQueryIL( &
ai_qtquery, x-r, y-r, x+r, y+r );
1904 for (
int i=0; i<il_size(&
ai_qtquery); i++ ) {
1910 if (p_i->
id == p->id)
1912 if (pilot_isDisabled(p_i) )
1918 if (dist <
pow2(750.))
1925 double factor = k_enemy * k_mult / (dist*dist*dist);
1931 vec2_cset( &F, F.x + F1.
x, F.y + F1.
y );
1940 lua_pushnumber(L,
ABS(diff));
1955 double diff, mod, angle;
1971 lua_pushnumber(L,
ABS(diff));
1985 vec2 *vec, drift, reference_vector;
1987 double diff, heading_offset_azimuth, drift_radial, drift_azimuthal;
1997 NLUA_INVALID_PARAMETER(L,1);
2015 vec2_uv(&drift_radial, &drift_azimuthal, &drift, &reference_vector);
2016 heading_offset_azimuth = angle_diff(
cur_pilot->
solid.
dir, VANGLE(reference_vector));
2022 if (
FABS(heading_offset_azimuth) < M_PI_2) {
2027 double speedmap = -1.*copysign(1. - 1. / (
FABS(drift_azimuthal/200.) + 1.), drift_azimuthal) * M_PI_2;
2028 diff = angle_diff(heading_offset_azimuth, speedmap);
2041 lua_pushnumber(L,
ABS(diff));
2061 vec2_cset( &tv, VX(p->solid.pos), VY(p->solid.pos) );
2063 vec2_angle(&sv, &tv));
2071 NLUA_INVALID_PARAMETER(L,1);
2074 lua_pushnumber(L, diff);
2088 vec2 *vec, drift, reference_vector;
2090 double diff, heading_offset_azimuth, drift_radial, drift_azimuthal;
2100 NLUA_INVALID_PARAMETER(L,1);
2118 vec2_uv(&drift_radial, &drift_azimuthal, &drift, &reference_vector);
2119 heading_offset_azimuth = angle_diff(
cur_pilot->
solid.
dir, VANGLE(reference_vector));
2125 if (
FABS(heading_offset_azimuth) < M_PI_2) {
2130 double speedmap = -1.*copysign(1. - 1. / (
FABS(drift_azimuthal/200.) + 1.), drift_azimuthal) * M_PI_2;
2131 diff = angle_diff(heading_offset_azimuth, speedmap);
2143 lua_pushnumber(L, diff);
2156 lua_pushnumber(L, drift);
2168 double dir, accel, diff;
2169 int isstopped = pilot_isStopped(
cur_pilot);
2172 lua_pushboolean(L,1);
2178 accel = -PILOT_REVERSE_THRUST;
2187 if (
ABS(diff) < MIN_DIR_ERR)
2191 lua_pushboolean(L, 0);
2211 if (!spob_hasService(
cur_system->spobs[i],SPOB_SERVICE_INHABITED))
2222 if (j == -1)
return 0;
2250 if (!spob_hasService(
cur_system->spobs[i],SPOB_SERVICE_INHABITED))
2261 if (j == -1)
return 0;
2311 if (pilot_isFlag(
cur_pilot, PILOT_NOLAND))
2315 only_friend = lua_toboolean(L, 1);
2324 if (!spob_hasService(pnt, SPOB_SERVICE_LAND))
2326 if (!spob_hasService(pnt, SPOB_SERVICE_INHABITED))
2368 if (!lua_isnoneornil(L,1)) {
2379 return NLUA_ERROR( L, _(
"Spob '%s' not found in system '%s'"), pnt->
name,
cur_system->name );
2391 if ((spob->
lua_can_land==LUA_NOREF) && !spob_hasService(spob,SPOB_SERVICE_LAND)) {
2392 lua_pushboolean(L,0);
2403 if (pilot_isFlag(
cur_pilot, PILOT_NOLAND)) {
2404 lua_pushboolean(L,0);
2410 lua_pushboolean(L,0);
2416 lua_pushboolean(L,0);
2423 pilot_setFlag(
cur_pilot, PILOT_LANDING );
2426 lua_rawgeti(naevL, LUA_REGISTRYINDEX, spob->
lua_land);
2429 if (nlua_pcall( spob->
lua_env, 2, 0 )) {
2430 WARN(_(
"Spob '%s' failed to run '%s':\n%s"), spob->
name,
"land", lua_tostring(naevL,-1));
2435 hparam.
type = HOOK_PARAM_SPOB;
2436 hparam.
u.
la = spob->
id;
2438 pilot_runHookParam(
cur_pilot, PILOT_HOOK_LAND, &hparam, 1 );
2439 lua_pushboolean(L,1);
2455 if (!lua_isnoneornil(L,1)) {
2459 return NLUA_ERROR(L, _(
"Jump point must be in current system."));
2470 lua_pushnumber(L,dist);
2483 const JumpPoint *jp;
2492 return NLUA_ERROR(L, _(
"Jump point must be in current system."));
2498 a = RNGF() * M_PI * 2.;
2499 rad = RNGF() * 0.5 * jp->radius;
2500 vec2_cadd( &vec, rad*cos(a), rad*sin(a) );
2519 const JumpPoint *jp;
2520 double mindist, dist;
2527 const JumpPoint *jiter = &
cur_system->jumps[i];
2531 if (jp_isFlag( jiter, JP_EXITONLY ))
2535 if (!useshidden && jp_isFlag(jiter, JP_HIDDEN))
2544 if (dist < mindist) {
2553 lj.
destid = jp->targetid;
2587 if ((!useshidden && jp_isFlag(jiter, JP_HIDDEN)) || jp_isFlag(jiter, JP_EXITONLY))
2604 if ((!useshidden && jp_isFlag(jiter, JP_HIDDEN)) || jp_isFlag(jiter, JP_EXITONLY))
2613 WARN(_(
"Pilot '%s' can't find jump to leave system!"),
cur_pilot->
name);
2620 lj.
destid = jumps[r]->targetid;
2669 if (lua_gettop(L) > 1)
2670 absolute = lua_toboolean(L,2);
2679 vec2_cset( &vv, p->solid.vel.x, p->solid.vel.y);
2683 dot = vec2_dot( &pv, &vv );
2684 mod =
MAX(VMOD(pv), 1.);
2686 lua_pushnumber(L, dot / mod );
2705 vec2 point, cons, goal, pv;
2706 double radius, angle, Kp, Kd, angle2;
2707 const Pilot *p, *target;
2712 radius = luaL_checknumber(L,2);
2713 angle = luaL_checknumber(L,3);
2714 Kp = luaL_checknumber(L,4);
2715 Kd = luaL_checknumber(L,5);
2716 method = luaL_optstring(L,6,
"velocity");
2718 if (strcmp( method,
"absolute" ) == 0)
2720 else if (strcmp( method,
"keepangle" ) == 0) {
2721 vec2_cset( &pv, p->solid.pos.x - target->
solid.
pos.
x,
2723 angle2 = VANGLE(pv);
2726 angle2 = angle + VANGLE( target->
solid.
vel );
2728 vec2_cset( &point, VX(target->
solid.
pos) + radius * cos(angle2),
2729 VY(target->
solid.
pos) + radius * sin(angle2) );
2732 vec2_cset( &cons, (point.
x - p->solid.pos.x) * Kp +
2733 (target->
solid.
vel.
x - p->solid.vel.x) *Kd,
2734 (point.
y - p->solid.pos.y) * Kp +
2735 (target->
solid.
vel.
y - p->solid.vel.y) *Kd );
2737 vec2_cset( &goal, cons.
x + p->solid.pos.x, cons.
y + p->solid.pos.y);
2760 vec2 point, cons, goal, *pos, *vel;
2761 double radius, angle, Kp, Kd;
2766 radius = luaL_checknumber(L,3);
2767 angle = luaL_checknumber(L,4);
2768 Kp = luaL_checknumber(L,5);
2769 Kd = luaL_checknumber(L,6);
2771 vec2_cset( &point, pos->
x + radius * cos(angle),
2772 pos->
y + radius * sin(angle) );
2775 vec2_cset( &cons, (point.
x - p->solid.pos.x) * Kp +
2776 (vel->
x - p->solid.vel.x) *Kd,
2777 (point.
y - p->solid.pos.y) * Kp +
2778 (vel->
y - p->solid.vel.y) *Kd );
2780 vec2_cset( &goal, cons.
x + p->solid.pos.x, cons.
y + p->solid.pos.y);
2826 if (lua_gettop(L) > 0) {
2827 int i = lua_toboolean(L,1);
2886 if ((lua_gettop(L) < 1) || lua_isnil(L,1))
2889 rad = lua_tonumber(L,1);
2894 lua_pushnumber(L,i);
2914 i = lua_tointeger(L,1);
2937 id = luaL_checkinteger(L,1);
2939 if (lua_gettop(L) > 1)
2940 type = lua_toboolean(L,2);
3010 if (pilot_isFlag(
cur_pilot, PILOT_COOLDOWN))
3013 if (lua_toboolean(L,1))
3029 if (lua_isnoneornil(L,1)) {
3037 double range = luaL_checknumber(L,1);
3038 double r2 =
pow2(range);
3039 unsigned int tp = 0;
3047 pilot_collideQueryIL( &
ai_qtquery, x-r, y-r, x+r, y+r );
3048 for (
int i=0; i<il_size(&
ai_qtquery); i++ ) {
3060 if (!tp || (td <
d)) {
3078 if (pilot_isWithPlayer(p))
3094 int level = luaL_optinteger( L, 2, -1 );
3110 int level = luaL_optinteger( L, 2, -1 );
3126 int level = luaL_optinteger( L, 2, -1 );
3143 if (!pilot_isDisabled(p)) {
3144 lua_pushboolean(L, 0);
3149 lua_pushboolean(L, !pilot_isFlag(p, PILOT_BOARDED));
3228 int n = luaL_checkint(L,1);
3244 int n = luaL_checkint(L,1);
3281 if (lua_isstring(L,1))
3283 else if (lua_isnoneornil(L,1))
3286 NLUA_INVALID_PARAMETER(L,1);
3354 if (lua_gettop(L)>0)
3355 b = lua_toboolean(L,1);
3359 lua_pushboolean(L,1);
static int ai_setMemory(void)
Sets the cur_pilot's ai.
Task * ai_newtask(lua_State *L, Pilot *p, const char *func, int subtask, int pos)
Creates a new AI task.
void ai_unsetPilot(AIMemory oldmem)
Finishes setting up a pilot.
static nlua_env equip_env
void ai_thinkApply(Pilot *p)
Applies the result of thinking.
static AI_Profile * profiles
static IntList ai_qtquery
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.
static int ai_tasktarget(lua_State *L, const Task *t)
Pushes a task target.
void ai_freetask(Task *t)
Frees an AI task.
void ai_think(Pilot *pilot, double dt, int dotask)
Heart of the AI, brains of the pilot.
void ai_thinkSetup(double dt)
Sets up the pilot for thinking.
void ai_getDistress(const Pilot *p, const Pilot *distressed, const Pilot *attacker)
Sends a distress signal to a pilot.
AIMemory ai_setPilot(Pilot *p)
Sets the pilot for further AI calls.
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.
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_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(const 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_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_hyperspaceAbort(lua_State *L)
Has the AI abandon hyperspace if applicable.
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_canHyperspace(lua_State *L)
Gets whether or not the pilot can hyperspace.
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_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.
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.
int pilot_isHostile(const Pilot *p)
Checks to see if pilot is hostile to the player.
int pilot_brakeCheckReverseThrusters(const Pilot *p)
See if the pilot wants to use their reverse thrusters to brake.
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.
int pilot_validEnemy(const Pilot *p, const Pilot *target)
Checks to see if a pilot is a valid enemy for another pilot.
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.
void pilot_msg(const Pilot *p, const Pilot *receiver, const char *type, unsigned int idx)
Sends a message.
void pilot_setAccel(Pilot *p, double accel)
Sets the pilot's accel.
unsigned int pilot_getNearestEnemy(const Pilot *p)
Gets the nearest enemy to the pilot.
void pilot_hyperspaceAbort(Pilot *p)
Stops the pilot from hyperspacing.
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.
double pilot_aimAngle(Pilot *p, const vec2 *pos, const vec2 *vel)
Returns the angle for a pilot to aim at another pilot.
double pilot_minbrakedist(const Pilot *p, double dt)
Gets the minimum braking distance for the pilot.
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_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_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 space_canHyperspace(const Pilot *p)
Checks to make sure if pilot is far enough away to hyperspace.
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 temporary pilot memory. For use with ai_setPilot and ai_unsetPilot.
Represents a single asteroid.
The actual hook parameter.
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,...