naev 0.10.4
nlua_jump.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_jump.h"
16
17#include "nluadef.h"
18#include "nlua_vec2.h"
19#include "nlua_system.h"
20#include "land_outfits.h"
21#include "map_overlay.h"
22#include "log.h"
23
24RETURNS_NONNULL static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind, int *offset );
25
26/* Jump metatable methods */
27static int jumpL_get( lua_State *L );
28static int jumpL_eq( lua_State *L );
29static int jumpL_tostring( lua_State *L );
30static int jumpL_radius( lua_State *L );
31static int jumpL_position( lua_State *L );
32static int jumpL_angle( lua_State *L );
33static int jumpL_hidden( lua_State *L );
34static int jumpL_exitonly( lua_State *L );
35static int jumpL_system( lua_State *L );
36static int jumpL_dest( lua_State *L );
37static int jumpL_isKnown( lua_State *L );
38static int jumpL_setKnown( lua_State *L );
39static const luaL_Reg jump_methods[] = {
40 { "get", jumpL_get },
41 { "__eq", jumpL_eq },
42 { "__tostring", jumpL_tostring },
43 { "radius", jumpL_radius },
44 { "pos", jumpL_position },
45 { "angle", jumpL_angle },
46 { "hidden", jumpL_hidden },
47 { "exitonly", jumpL_exitonly },
48 { "system", jumpL_system },
49 { "dest", jumpL_dest },
50 { "known", jumpL_isKnown },
51 { "setKnown", jumpL_setKnown },
52 {0,0}
53};
61int nlua_loadJump( nlua_env env )
62{
63 nlua_register(env, JUMP_METATABLE, jump_methods, 1);
64 return 0; /* No error */
65}
66
89LuaJump* lua_tojump( lua_State *L, int ind )
90{
91 return (LuaJump*) lua_touserdata(L,ind);
92}
100LuaJump* luaL_checkjump( lua_State *L, int ind )
101{
102 if (lua_isjump(L,ind))
103 return lua_tojump(L,ind);
104 luaL_typerror(L, ind, JUMP_METATABLE);
105 return NULL;
106}
107
118static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind, int *offset )
119{
120 JumpPoint *jp;
121 StarSystem *a, *b;
122
123 /* Defaults. */
124 jp = NULL;
125 a = NULL;
126 b = NULL;
127
128 if (lua_isjump(L, ind)) {
129 LuaJump *lj = luaL_checkjump(L, ind);
130 a = system_getIndex( lj->srcid );
131 b = system_getIndex( lj->destid );
132 if (offset != NULL)
133 *offset = 1;
134 }
135 else if (lua_gettop(L) > 1) {
136 if (lua_isstring(L, ind))
137 a = system_get( lua_tostring( L, ind ));
138 else if (lua_issystem(L, ind))
139 a = system_getIndex( lua_tosystem(L, ind) );
140
141 if (lua_isstring(L, ind+1))
142 b = system_get( lua_tostring( L, ind+1 ));
143 else if (lua_issystem(L, ind+1))
144 b = system_getIndex( lua_tosystem(L, ind+1) );
145
146 if (offset != NULL)
147 *offset = 2;
148 }
149 else {
150 luaL_typerror(L, ind, JUMP_METATABLE);
151 // noreturn
152 }
153
154 if ((b != NULL) && (a != NULL))
155 jp = jump_getTarget( b, a );
156
157 if (jp == NULL)
158 NLUA_ERROR(L, _("Jump is invalid"));
159
160 return jp;
161}
162
170JumpPoint* luaL_validjump( lua_State *L, int ind )
171{
172 return luaL_validjumpSystem( L, ind, NULL );
173}
174
182LuaJump* lua_pushjump( lua_State *L, LuaJump jump )
183{
184 LuaJump *j = (LuaJump*) lua_newuserdata(L, sizeof(LuaJump));
185 *j = jump;
186 luaL_getmetatable(L, JUMP_METATABLE);
187 lua_setmetatable(L, -2);
188 return j;
189}
190
198int lua_isjump( lua_State *L, int ind )
199{
200 int ret;
201
202 if (lua_getmetatable(L,ind)==0)
203 return 0;
204 lua_getfield(L, LUA_REGISTRYINDEX, JUMP_METATABLE);
205
206 ret = 0;
207 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
208 ret = 1;
209
210 lua_pop(L, 2); /* remove both metatables */
211 return ret;
212}
213
228static int jumpL_get( lua_State *L )
229{
230 LuaJump lj;
231 StarSystem *a, *b;
232
233 a = luaL_validsystem(L,1);
234 b = luaL_validsystem(L,2);
235
236 if ((a == NULL) || (b == NULL)) {
237 NLUA_ERROR(L, _("No matching jump points found."));
238 return 0;
239 }
240
241 if (jump_getTarget(b, a) != NULL) {
242 lj.srcid = a->id;
243 lj.destid = b->id;
244 lua_pushjump(L, lj);
245
246 /* The inverse. If it doesn't exist, there are bigger problems. */
247 lj.srcid = b->id;
248 lj.destid = a->id;
249 lua_pushjump(L, lj);
250 return 2;
251 }
252
253 return 0;
254}
255
265static int jumpL_eq( lua_State *L )
266{
267 LuaJump *a, *b;
268 a = luaL_checkjump(L,1);
269 b = luaL_checkjump(L,2);
270 lua_pushboolean(L,((a->srcid == b->srcid) && (a->destid == b->destid)));
271 return 1;
272}
273
280static int jumpL_tostring( lua_State *L )
281{
282 char buf[STRMAX_SHORT];
283 LuaJump *lj = luaL_checkjump(L,1);
284 StarSystem *src = system_getIndex( lj->srcid );
285 StarSystem *dst = system_getIndex( lj->destid );
286 snprintf( buf, sizeof(buf), _("Jump( %s -> %s )"), _(src->name), _(dst->name) );
287 lua_pushstring( L, buf );
288 return 1;
289}
290
299static int jumpL_radius( lua_State *L )
300{
301 JumpPoint *jp = luaL_validjump(L,1);
302 lua_pushnumber(L,jp->radius);
303 return 1;
304}
305
314static int jumpL_position( lua_State *L )
315{
316 JumpPoint *jp = luaL_validjump(L,1);
317 lua_pushvector(L, jp->pos);
318 return 1;
319}
320
329static int jumpL_angle( lua_State *L )
330{
331 JumpPoint *jp = luaL_validjump(L,1);
332 lua_pushnumber(L, jp->angle);
333 return 1;
334}
335
344static int jumpL_hidden( lua_State *L )
345{
346 JumpPoint *jp = luaL_validjump(L,1);
347 lua_pushboolean(L, jp_isFlag(jp, JP_HIDDEN) );
348 return 1;
349}
350
359static int jumpL_exitonly( lua_State *L )
360{
361 JumpPoint *jp = luaL_validjump(L,1);
362 lua_pushboolean(L, jp_isFlag(jp, JP_EXITONLY) );
363 return 1;
364}
365
374static int jumpL_system( lua_State *L )
375{
376 JumpPoint *jp = luaL_validjumpSystem( L, 1, NULL );
377 lua_pushsystem( L, jp->from->id );
378 return 1;
379}
380
389static int jumpL_dest( lua_State *L )
390{
391 JumpPoint *jp = luaL_validjump(L,1);
392 lua_pushsystem(L,jp->targetid);
393 return 1;
394}
395
405static int jumpL_isKnown( lua_State *L )
406{
407 JumpPoint *jp = luaL_validjump(L,1);
408 lua_pushboolean(L, jp_isKnown(jp));
409 return 1;
410}
411
420static int jumpL_setKnown( lua_State *L )
421{
422 int b, offset, changed;
423 JumpPoint *jp;
424
425 offset = 0;
426 jp = luaL_validjumpSystem( L, 1, &offset );
427
428 /* True if boolean isn't supplied. */
429 if (lua_gettop(L) > offset)
430 b = lua_toboolean(L, 1 + offset);
431 else
432 b = 1;
433
434 changed = (b != (int)jp_isKnown(jp));
435
436 if (b)
437 jp_setFlag( jp, JP_KNOWN );
438 else
439 jp_rmFlag( jp, JP_KNOWN );
440
441 if (changed) {
442 /* Update overlay. */
443 ovr_refresh();
444 /* Update outfits image array - in the case it changes map owned status. */
446 }
447
448 return 0;
449}
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
Definition: land_outfits.c:523
Header file with generic functions and naev-specifics.
static int jumpL_tostring(lua_State *L)
Converts a jump to readable form. Mainly meant to be used for printing.
Definition: nlua_jump.c:280
static int jumpL_exitonly(lua_State *L)
Checks whether a jump is exit-only.
Definition: nlua_jump.c:359
static int jumpL_setKnown(lua_State *L)
Sets a jump's known state.
Definition: nlua_jump.c:420
static int jumpL_system(lua_State *L)
Gets the system that a jump point exists in.
Definition: nlua_jump.c:374
static RETURNS_NONNULL JumpPoint * luaL_validjumpSystem(lua_State *L, int ind, int *offset)
Back-end for luaL_validjump.
Definition: nlua_jump.c:118
LuaJump * luaL_checkjump(lua_State *L, int ind)
Gets jump at index raising an error if isn't a jump.
Definition: nlua_jump.c:100
static int jumpL_angle(lua_State *L)
Gets the angle of a jump in radians.
Definition: nlua_jump.c:329
static int jumpL_radius(lua_State *L)
Gets the jump's radius.
Definition: nlua_jump.c:299
static int jumpL_isKnown(lua_State *L)
Checks to see if a jump is known by the player.
Definition: nlua_jump.c:405
static int jumpL_dest(lua_State *L)
Gets the system that a jump point exits into.
Definition: nlua_jump.c:389
static int jumpL_eq(lua_State *L)
You can use the '==' operator within Lua to compare jumps with this.
Definition: nlua_jump.c:265
static const luaL_Reg jump_methods[]
Definition: nlua_jump.c:39
static int jumpL_get(lua_State *L)
Gets a jump.
Definition: nlua_jump.c:228
static int jumpL_hidden(lua_State *L)
Checks whether a jump is hidden.
Definition: nlua_jump.c:344
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
Definition: nlua_jump.c:182
int nlua_loadJump(nlua_env env)
Loads the jump library.
Definition: nlua_jump.c:61
JumpPoint * luaL_validjump(lua_State *L, int ind)
Gets a jump directly.
Definition: nlua_jump.c:170
int lua_isjump(lua_State *L, int ind)
Checks to see if ind is a jump.
Definition: nlua_jump.c:198
LuaJump * lua_tojump(lua_State *L, int ind)
This module allows you to handle the jumps from Lua.
Definition: nlua_jump.c:89
static int jumpL_position(lua_State *L)
Gets the position of the jump in the system.
Definition: nlua_jump.c:314
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
Definition: nlua_system.c:185
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
Definition: nlua_system.c:156
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
Definition: nlua_system.c:130
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
Definition: nlua_system.c:201
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
static const double a[]
Definition: rng.c:247
static const double b[]
Definition: rng.c:256
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition: space.c:944
JumpPoint * jump_getTarget(const StarSystem *target, const StarSystem *sys)
Less safe version of jump_get that works with pointers.
Definition: space.c:1198
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition: space.c:914
Lua jump Wrapper.
Definition: nlua_jump.h:14
int destid
Definition: nlua_jump.h:16
int srcid
Definition: nlua_jump.h:15