13#include "nlua_pilot.h"
16static int pilot_shipLmem(
const Pilot *p )
20 nlua_getenv( naevL, p->ship->lua_env,
"mem" );
21 oldmem = luaL_ref( naevL, LUA_REGISTRYINDEX );
23 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->lua_ship_mem );
24 nlua_setenv( naevL, p->ship->lua_env,
"mem" );
27static void pilot_shipLunmem(
const Pilot *p,
int oldmem )
29 lua_rawgeti( naevL, LUA_REGISTRYINDEX, oldmem );
30 nlua_setenv( naevL, p->ship->lua_env,
"mem");
31 luaL_unref( naevL, LUA_REGISTRYINDEX, oldmem );
33static void shipLRunWarning(
const Pilot *p,
const Ship *s,
const char *name,
const char *error )
35 WARN( _(
"Pilot '%s''s ship '%s' -> '%s':\n%s"), p->name, s->
name, name, error );
47 if (p->lua_ship_mem == LUA_NOREF) {
48 lua_newtable( naevL );
49 p->lua_ship_mem = luaL_ref( naevL, LUA_REGISTRYINDEX );
52 if (p->ship->lua_init == LUA_NOREF)
55 oldmem = pilot_shipLmem( p );
58 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_init );
60 if (nlua_pcall( p->ship->lua_env, 1, 0 )) {
61 shipLRunWarning( p, p->ship,
"init", lua_tostring(naevL,-1) );
63 pilot_shipLunmem( p, oldmem );
66 pilot_shipLunmem( p, oldmem );
78 if (p->ship->lua_cleanup != LUA_NOREF) {
79 int oldmem = pilot_shipLmem( p );
82 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_cleanup );
84 if (nlua_pcall( p->ship->lua_env, 1, 0 )) {
85 shipLRunWarning( p, p->ship,
"cleanup", lua_tostring(naevL,-1) );
87 pilot_shipLunmem( p, oldmem );
90 pilot_shipLunmem( p, oldmem );
94 if (p->lua_ship_mem != LUA_NOREF) {
95 luaL_unref( naevL, LUA_REGISTRYINDEX, p->lua_ship_mem );
96 p->lua_ship_mem = LUA_NOREF;
111 if (p->ship->lua_update == LUA_NOREF)
115 if (p->ship->lua_dt > 0.) {
116 p->lua_ship_timer -= dt;
117 if (p->lua_ship_timer > 0.)
119 p->lua_ship_timer += p->ship->lua_dt;
122 oldmem = pilot_shipLmem( p );
125 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_update );
127 lua_pushnumber( naevL, dt );
128 if (nlua_pcall( p->ship->lua_env, 2, 0 )) {
129 shipLRunWarning( p, p->ship,
"update", lua_tostring(naevL,-1) );
131 pilot_shipLunmem( p, oldmem );
134 pilot_shipLunmem( p, oldmem );
147 if (p->ship->lua_explode_init == LUA_NOREF)
149 oldmem = pilot_shipLmem( p );
152 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_explode_init );
154 if (nlua_pcall( p->ship->lua_env, 1, 0 )) {
155 shipLRunWarning( p, p->ship,
"explode_init", lua_tostring(naevL,-1) );
157 pilot_shipLunmem( p, oldmem );
160 pilot_shipLunmem( p, oldmem );
174 if (p->ship->lua_explode_update == LUA_NOREF)
176 oldmem = pilot_shipLmem( p );
179 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_explode_update );
181 lua_pushnumber( naevL, dt );
182 if (nlua_pcall( p->ship->lua_env, 2, 0 )) {
183 shipLRunWarning( p, p->ship,
"explode_update", lua_tostring(naevL,-1) );
185 pilot_shipLunmem( p, oldmem );
188 pilot_shipLunmem( p, oldmem );
Header file with generic functions and naev-specifics.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
int pilot_shipLInit(Pilot *p)
Initializes the pilot ship Lua.
int pilot_shipLExplodeUpdate(Pilot *p, double dt)
Updates the pilot explosion Lua stuff.
int pilot_shipLUpdate(Pilot *p, double dt)
Updates the pilot Lua stuff.
int pilot_shipLCleanup(Pilot *p)
Cleans up the pilot ship Lua.
int pilot_shipLExplodeInit(Pilot *p)
Initializes the pilot explosion stuff.
The representation of an in-game pilot.