naev 0.11.5
pilot_ship.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "nlua_pilot.h"
14#include "nlua_ship.h"
15
16static int pilot_shipLmem( const Pilot *p )
17{
18 int oldmem;
19 /* Get old memory. */
20 nlua_getenv( naevL, p->ship->lua_env, "mem" ); /* oldmem */
21 oldmem = luaL_ref( naevL, LUA_REGISTRYINDEX ); /* */
22 /* Set the memory. */
23 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->lua_ship_mem ); /* mem */
24 nlua_setenv( naevL, p->ship->lua_env, "mem" );
25 return oldmem;
26}
27static void pilot_shipLunmem( const Pilot *p, int oldmem )
28{
29 lua_rawgeti( naevL, LUA_REGISTRYINDEX, oldmem );
30 nlua_setenv( naevL, p->ship->lua_env, "mem"); /* pm */
31 luaL_unref( naevL, LUA_REGISTRYINDEX, oldmem );
32}
33static void shipLRunWarning( const Pilot *p, const Ship *s, const char *name, const char *error )
34{
35 WARN( _("Pilot '%s''s ship '%s' -> '%s':\n%s"), p->name, s->name, name, error );
36}
37
45{
46 int oldmem;
47 if (p->lua_ship_mem == LUA_NOREF) {
48 lua_newtable( naevL ); /* mem */
49 p->lua_ship_mem = luaL_ref( naevL, LUA_REGISTRYINDEX ); /* */
50 }
51
52 if (p->ship->lua_init == LUA_NOREF)
53 return 0;
54
55 oldmem = pilot_shipLmem( p );
56
57 /* Set up the function: init( p ) */
58 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_init ); /* f */
59 lua_pushpilot( naevL, p->id ); /* f, p */
60 if (nlua_pcall( p->ship->lua_env, 1, 0 )) { /* */
61 shipLRunWarning( p, p->ship, "init", lua_tostring(naevL,-1) );
62 lua_pop(naevL, 1);
63 pilot_shipLunmem( p, oldmem );
64 return -1;
65 }
66 pilot_shipLunmem( p, oldmem );
67 return 1;
68}
69
77{
78 if (p->ship->lua_cleanup != LUA_NOREF) {
79 int oldmem = pilot_shipLmem( p );
80
81 /* Set up the function: cleanup( p ) */
82 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_cleanup ); /* f */
83 lua_pushpilot( naevL, p->id ); /* f, p */
84 if (nlua_pcall( p->ship->lua_env, 1, 0 )) { /* */
85 shipLRunWarning( p, p->ship, "cleanup", lua_tostring(naevL,-1) );
86 lua_pop(naevL, 1);
87 pilot_shipLunmem( p, oldmem );
88 return -1;
89 }
90 pilot_shipLunmem( p, oldmem );
91 }
92
93 /* Clear Lua if necessary. */
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;
97 }
98 return 0;
99}
100
108int pilot_shipLUpdate( Pilot *p, double dt )
109{
110 int oldmem;
111 if (p->ship->lua_update == LUA_NOREF)
112 return 0;
113
114 /* Use timer. */
115 if (p->ship->lua_dt > 0.) {
116 p->lua_ship_timer -= dt;
117 if (p->lua_ship_timer > 0.)
118 return 0;
119 p->lua_ship_timer += p->ship->lua_dt;
120 }
121
122 oldmem = pilot_shipLmem( p );
123
124 /* Set up the function: update( p ) */
125 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_update ); /* f */
126 lua_pushpilot( naevL, p->id ); /* f, p */
127 lua_pushnumber( naevL, dt ); /* f, p, dt */
128 if (nlua_pcall( p->ship->lua_env, 2, 0 )) { /* */
129 shipLRunWarning( p, p->ship, "update", lua_tostring(naevL,-1) );
130 lua_pop(naevL, 1);
131 pilot_shipLunmem( p, oldmem );
132 return -1;
133 }
134 pilot_shipLunmem( p, oldmem );
135 return 0;
136}
137
145{
146 int oldmem;
147 if (p->ship->lua_explode_init == LUA_NOREF)
148 return 0;
149 oldmem = pilot_shipLmem( p );
150
151 /* Set up the function: explode_init( p ) */
152 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_explode_init ); /* f */
153 lua_pushpilot( naevL, p->id ); /* f, p */
154 if (nlua_pcall( p->ship->lua_env, 1, 0 )) { /* */
155 shipLRunWarning( p, p->ship, "explode_init", lua_tostring(naevL,-1) );
156 lua_pop(naevL, 1);
157 pilot_shipLunmem( p, oldmem );
158 return -1;
159 }
160 pilot_shipLunmem( p, oldmem );
161 return 0;
162}
163
171int pilot_shipLExplodeUpdate( Pilot *p, double dt )
172{
173 int oldmem;
174 if (p->ship->lua_explode_update == LUA_NOREF)
175 return 0;
176 oldmem = pilot_shipLmem( p );
177
178 /* Set up the function: explode_update( p ) */
179 lua_rawgeti( naevL, LUA_REGISTRYINDEX, p->ship->lua_explode_update ); /* f */
180 lua_pushpilot( naevL, p->id ); /* f, p */
181 lua_pushnumber( naevL, dt ); /* f, p, dt */
182 if (nlua_pcall( p->ship->lua_env, 2, 0 )) { /* */
183 shipLRunWarning( p, p->ship, "explode_update", lua_tostring(naevL,-1) );
184 lua_pop(naevL, 1);
185 pilot_shipLunmem( p, oldmem );
186 return -1;
187 }
188 pilot_shipLunmem( p, oldmem );
189 return 0;
190}
Header file with generic functions and naev-specifics.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition nlua_pilot.c:563
int pilot_shipLInit(Pilot *p)
Initializes the pilot ship Lua.
Definition pilot_ship.c:44
int pilot_shipLExplodeUpdate(Pilot *p, double dt)
Updates the pilot explosion Lua stuff.
Definition pilot_ship.c:171
int pilot_shipLUpdate(Pilot *p, double dt)
Updates the pilot Lua stuff.
Definition pilot_ship.c:108
int pilot_shipLCleanup(Pilot *p)
Cleans up the pilot ship Lua.
Definition pilot_ship.c:76
int pilot_shipLExplodeInit(Pilot *p)
Initializes the pilot explosion stuff.
Definition pilot_ship.c:144
The representation of an in-game pilot.
Definition pilot.h:217
Represents a space ship.
Definition ship.h:94
char * name
Definition ship.h:95