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