15#include "player_autonav.h"
18#include "map_overlay.h"
21#include "nlua_pilot.h"
34static int func_system = LUA_NOREF;
35static int func_spob = LUA_NOREF;
36static int func_pilot = LUA_NOREF;
37static int func_board = LUA_NOREF;
38static int func_pos = LUA_NOREF;
39static int func_reset = LUA_NOREF;
40static int func_end = LUA_NOREF;
41static int func_abort = LUA_NOREF;
42static int func_think = LUA_NOREF;
43static int func_update = LUA_NOREF;
44static int func_enter = LUA_NOREF;
56 nlua_env env = nlua_newEnv( AUTONAV_PATH );
61 char *buf =
ndata_read( AUTONAV_PATH, &bufsize );
62 if ( nlua_dobufenv( env, buf, bufsize, AUTONAV_PATH ) != 0 ) {
63 WARN( _(
"Error loading file: %s\n"
65 "Most likely Lua file has improper syntax, please check" ),
66 AUTONAV_PATH, lua_tostring(
naevL, -1 ) );
102 if ( pilot_isFlag(
player.p, PILOT_MANUAL_CONTROL ) ||
103 pilot_isDisabled(
player.p ) )
106 if ( (
player.p->nav_hyperspace == -1 ) && (
player.p->nav_spob == -1 ) )
114 player_message(
"#r%s#0", _(
"Not enough fuel to jump for autonav." ) );
118 if ( pilot_isFlag(
player.p, PILOT_NOJUMP ) ) {
126 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_system );
128 WARN(
"%s", lua_tostring(
naevL, -1 ) );
131 player.autonav = AUTONAV_JUMP;
142 if ( pilot_isFlag(
player.p, PILOT_MANUAL_CONTROL ) ||
143 pilot_isDisabled(
player.p ) )
148 player_setFlag( PLAYER_AUTONAV );
153static int autonav_ending = 0;
160 if ( autonav_ending )
164 player_rmFlag( PLAYER_AUTONAV );
167 player.speed_autonav = 1.;
170 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_end );
172 WARN(
"%s", lua_tostring(
naevL, -1 ) );
200 vec2_cset( &pos, x, y );
201 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_pos );
204 WARN(
"%s", lua_tostring(
naevL, -1 ) );
207 player.autonav = AUTONAV_POS;
222 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_spob );
224 lua_pushboolean(
naevL, tryland );
226 WARN(
"%s", lua_tostring(
naevL, -1 ) );
229 player.autonav = AUTONAV_SPOB;
242 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_pilot );
245 WARN(
"%s", lua_tostring(
naevL, -1 ) );
248 player.autonav = AUTONAV_PILOT;
262 if ( inrange != 1 ) {
267 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_board );
270 WARN(
"%s", lua_tostring(
naevL, -1 ) );
273 player.autonav = AUTONAV_PILOT;
284 if ( (
player.p == NULL ) || pilot_isFlag(
player.p, PILOT_HYPERSPACE ) )
288 if ( !player_isFlag( PLAYER_AUTONAV ) )
293 if ( pilot_isFlag(
player.p, PILOT_MANUAL_CONTROL ) )
297 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_abort );
298 if ( reason != NULL )
299 lua_pushstring(
naevL, reason );
301 lua_pushnil(
naevL );
303 WARN(
"%s", lua_tostring(
naevL, -1 ) );
308 if ( pilot_isFlag(
player.p, PILOT_HYP_PREP ) ) {
310 player_message(
"#o%s#0", _(
"Autonav: aborting hyperspace sequence." ) );
321 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_reset );
322 lua_pushnumber(
naevL, s );
324 WARN(
"%s", lua_tostring(
naevL, -1 ) );
341 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_think );
342 lua_pushnumber(
naevL, dt );
344 WARN(
"%s", lua_tostring(
naevL, -1 ) );
359 const double dis_dead = 1.0;
360 const double dis_mod = 2.0;
365 pilot_isFlag(
player.p, PILOT_DEAD ) ||
366 player_isFlag( PLAYER_CINEMATICS ) )
371 if ( pilot_isFlag(
player.p, PILOT_DISABLED ) ) {
372 static double tc_mod = 1.0;
385 double dis_max =
MAX( 5.,
player.p->dtimer / 10. );
386 double dis_ramp = ( dis_max - tc_base ) / dis_mod;
388 double time_left =
player.p->dtimer -
player.p->dtimer_accum;
390 if ( (
player.p->dtimer_accum < dis_dead ) || ( time_left < dis_dead ) )
394 if ( time_left < dis_dead + ( dis_max - tc_base ) * dis_ramp / 2. +
396 tc_mod =
MAX( tc_base, tc_mod - dis_mod * dt );
399 tc_mod =
MIN( dis_max, tc_mod + dis_mod * dt );
407 if ( !player_isFlag( PLAYER_AUTONAV ) )
410 NTracingZone( _ctx, 1 );
412 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_update );
413 lua_pushnumber(
naevL, dt );
415 WARN(
"%s", lua_tostring(
naevL, -1 ) );
419 NTracingZoneEnd( _ctx );
428 if ( !player_isFlag( PLAYER_AUTONAV ) )
431 lua_rawgeti(
naevL, LUA_REGISTRYINDEX, func_enter );
433 WARN(
"%s", lua_tostring(
naevL, -1 ) );
void ai_unsetPilot(AIMemory oldmem)
Finishes setting up a pilot.
void ai_thinkApply(Pilot *p)
Applies the result of thinking.
void ai_thinkSetup(double dt)
Sets up the pilot for thinking.
AIMemory ai_setPilot(Pilot *p)
Sets the pilot for further AI calls.
void player_message(const char *fmt,...)
Adds a mesg to the queue to be displayed on screen.
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 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.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
void pause_setSpeed(double mod)
Adjusts the game's dt modifier.
void pilot_hyperspaceAbort(Pilot *p)
Stops the pilot from hyperspacing.
Pilot * pilot_get(unsigned int id)
Pulls a pilot out of the pilot_stack based on ID.
int pilot_inRangePilot(const Pilot *p, const Pilot *target, double *dist2)
Check to see if a pilot is in sensor range of another.
void player_resetSpeed(void)
Resets the player speed stuff.
void player_hyperspacePreempt(int preempt)
Enables or disables jump points preempting spobs in autoface and target clearing.
double player_dt_default(void)
Returns the player's total default time delta based on time dilation stuff.
int player_getHypPreempt(void)
Returns whether the jump point target should preempt the spob target.
void player_restoreControl(int reason, const char *str)
Aborts autonav and other states that take control of the ship.
void player_accelOver(void)
Done accelerating.
static int player_autonavSetup(void)
Prepares the player to enter autonav.
void player_autonavEnd(void)
Ends the autonav.
void player_autonavStartWindow(unsigned int wid, const char *str)
Starts autonav and closes the window.
void player_autonavPos(double x, double y)
Starts autonav with a local position destination.
int player_autonavInit(void)
Initialize the autonav code.
void player_autonavResetSpeed(void)
Resets the game speed.
void player_autonavStart(void)
Starts autonav.
void player_autonavReset(double s)
Resets the game speed without disabling autonav.
void player_autonavPil(unsigned int p)
Starts autonav with a pilot to follow.
static nlua_env autonav_env
void player_autonavBoard(unsigned int p)
Starts autonav with a pilot to board.
void player_autonavSpob(const char *name, int tryland)
Starts autonav with a spob destination.
void player_autonavEnter(void)
Signal to the autonav that a new system was entered.
void player_autonavAbort(const char *reason)
Aborts autonav.
void player_thinkAutonav(Pilot *pplayer, double dt)
Handles autonav thinking.
void player_updateAutonav(double dt)
Updates the player's autonav.
void sound_setSpeed(double s)
Sets the speed to play the sound at.
Spob * spob_get(const char *spobname)
Gets a spob based on its name.
int spob_index(const Spob *p)
Gets the ID of a spob.
Represents a temporary pilot memory. For use with ai_setPilot and ai_unsetPilot.
The representation of an in-game pilot.
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...