naev 0.10.4
nlua_pilotoutfit.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_pilotoutfit.h"
16
17#include "log.h"
18#include "nlua_outfit.h"
19#include "nluadef.h"
20#include "rng.h"
21#include "slots.h"
22
23int pilotoutfit_modified = 0;
24
25/* Pilot outfit metatable methods. */
26static int poL_outfit( lua_State *L );
27static int poL_state( lua_State *L );
28static int poL_progress( lua_State *L );
29static int poL_set( lua_State *L );
30static int poL_clear( lua_State *L );
31static const luaL_Reg poL_methods[] = {
32 { "outfit", poL_outfit },
33 { "state", poL_state },
34 { "progress", poL_progress },
35 { "set", poL_set },
36 { "clear", poL_clear },
37 {0,0}
38};
46int nlua_loadPilotOutfit( nlua_env env )
47{
48 nlua_register(env, PILOTOUTFIT_METATABLE, poL_methods, 1);
49 return 0;
50}
51
64PilotOutfitSlot* lua_topilotoutfit( lua_State *L, int ind )
65{
66 return *((PilotOutfitSlot**) lua_touserdata(L,ind));
67}
75PilotOutfitSlot* luaL_checkpilotoutfit( lua_State *L, int ind )
76{
77 if (lua_ispilotoutfit(L,ind))
78 return lua_topilotoutfit(L,ind);
79 luaL_typerror(L, ind, PILOTOUTFIT_METATABLE);
80 return NULL;
81}
89PilotOutfitSlot* luaL_validpilotoutfit( lua_State *L, int ind )
90{
92
93 if (lua_ispilotoutfit(L, ind))
94 o = luaL_checkpilotoutfit(L,ind);
95 else {
96 luaL_typerror(L, ind, PILOTOUTFIT_METATABLE);
97 return NULL;
98 }
99
100 if (o == NULL)
101 NLUA_ERROR(L, _("Pilot Outfit is invalid."));
102
103 return o;
104}
113{
114 PilotOutfitSlot **lpo = (PilotOutfitSlot**) lua_newuserdata(L, sizeof(PilotOutfitSlot*));
115 *lpo = po;
116 luaL_getmetatable(L, PILOTOUTFIT_METATABLE);
117 lua_setmetatable(L, -2);
118 return lpo;
119}
127int lua_ispilotoutfit( lua_State *L, int ind )
128{
129 int ret;
130
131 if (lua_getmetatable(L,ind)==0)
132 return 0;
133 lua_getfield(L, LUA_REGISTRYINDEX, PILOTOUTFIT_METATABLE);
134
135 ret = 0;
136 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
137 ret = 1;
138
139 lua_pop(L, 2); /* remove both metatables */
140 return ret;
141}
142
150static int poL_outfit( lua_State *L )
151{
153 lua_pushoutfit(L, po->outfit );
154 return 1;
155}
156
164static int poL_state( lua_State *L )
165{
167 const char *state = luaL_optstring(L,2,NULL);
168 PilotOutfitState pos = po->state;
169
170 if (!outfit_isMod( po->outfit ))
171 NLUA_ERROR( L, _("'pilotoutfit.%s' only works with modifier outfits!"), "state");
172
173 if (state==NULL || strcmp(state,"off")==0)
174 po->state = PILOT_OUTFIT_OFF;
175 else if (strcmp(state,"warmup")==0)
176 po->state = PILOT_OUTFIT_WARMUP;
177 else if (strcmp(state,"on")==0)
178 po->state = PILOT_OUTFIT_ON;
179 else if (strcmp(state,"cooldown")==0)
180 po->state = PILOT_OUTFIT_COOLDOWN;
181 else
182 NLUA_ERROR( L, _("Unknown PilotOutfit state '%s'!"), state );
183
184 /* Mark as modified if state changed. */
185 if (pos != po->state)
186 pilotoutfit_modified = 1;
187
188 return 0;
189}
190
198static int poL_progress( lua_State *L )
199{
201
202 if (!outfit_isMod( po->outfit ))
203 NLUA_ERROR( L, _("'pilotoutfit.%s' only works with modifier outfits!"), "progress");
204
205 po->progress = CLAMP( 0., 1., luaL_checknumber(L,2) );
206 return 0;
207}
208
217static int poL_set( lua_State *L )
218{
220 const char *name = luaL_checkstring(L,2);
221 double value = luaL_checknumber(L,3);
222 ss_statsSet( &po->lua_stats, name, value, 1 );
223 pilotoutfit_modified = 1;
224 return 0;
225}
226
233static int poL_clear( lua_State *L )
234{
236 ss_statsInit( &po->lua_stats );
237 pilotoutfit_modified = 1;
238 return 0;
239}
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition: naev.h:41
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
Definition: nlua_outfit.c:160
static const luaL_Reg poL_methods[]
int nlua_loadPilotOutfit(nlua_env env)
Loads the pilot outfit library.
PilotOutfitSlot * luaL_validpilotoutfit(lua_State *L, int ind)
Makes sure the outfit is valid or raises a Lua error.
static int poL_progress(lua_State *L)
Sets the state progress of the PilotOutfit.
PilotOutfitSlot * luaL_checkpilotoutfit(lua_State *L, int ind)
Gets outfit at index or raises error if there is no outfit at index.
PilotOutfitSlot ** lua_pushpilotoutfit(lua_State *L, PilotOutfitSlot *po)
Pushes a pilot outfit on the stack.
static int poL_set(lua_State *L)
Sets a temporary ship stat modifier of the pilot outfit.
static int poL_outfit(lua_State *L)
Gets the outfit of the PilotOutfit.
PilotOutfitSlot * lua_topilotoutfit(lua_State *L, int ind)
Lua bindings to interact with pilot outfits.
int lua_ispilotoutfit(lua_State *L, int ind)
Checks to see if ind is a pilot outfit.
static int poL_state(lua_State *L)
Sets the state of the PilotOutfit.
static int poL_clear(lua_State *L)
Clears all the temporary ship stat modifiers of the pilot outfit.
int outfit_isMod(const Outfit *o)
Checks if outfit is a ship modification.
Definition: outfit.c:532
int ss_statsInit(ShipStats *stats)
Initializes a stat structure.
Definition: shipstats.c:347
int ss_statsSet(ShipStats *s, const char *name, double value, int overwrite)
Sets a ship stat by name.
Definition: shipstats.c:819
Stores an outfit the pilot has.
Definition: pilot.h:108
double progress
Definition: pilot.h:127
PilotOutfitState state
Definition: pilot.h:123
const Outfit * outfit
Definition: pilot.h:112
ShipStats lua_stats
Definition: pilot.h:140