naev 0.11.5
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_pilot.h"
20#include "nlua_system.h"
21#include "land_outfits.h"
22#include "map_overlay.h"
23#include "log.h"
24
25RETURNS_NONNULL static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind, int *offset );
26
27/* Jump metatable methods */
28static int jumpL_get( lua_State *L );
29static int jumpL_eq( lua_State *L );
30static int jumpL_tostring( lua_State *L );
31static int jumpL_radius( lua_State *L );
32static int jumpL_position( lua_State *L );
33static int jumpL_angle( lua_State *L );
34static int jumpL_hidden( lua_State *L );
35static int jumpL_exitonly( lua_State *L );
36static int jumpL_system( lua_State *L );
37static int jumpL_dest( lua_State *L );
38static int jumpL_jumpDist( lua_State *L );
39static int jumpL_isKnown( lua_State *L );
40static int jumpL_setKnown( lua_State *L );
41static const luaL_Reg jump_methods[] = {
42 { "get", jumpL_get },
43 { "__eq", jumpL_eq },
44 { "__tostring", jumpL_tostring },
45 { "radius", jumpL_radius },
46 { "pos", jumpL_position },
47 { "angle", jumpL_angle },
48 { "hidden", jumpL_hidden },
49 { "exitonly", jumpL_exitonly },
50 { "system", jumpL_system },
51 { "dest", jumpL_dest },
52 { "jumpDist", jumpL_jumpDist },
53 { "known", jumpL_isKnown },
54 { "setKnown", jumpL_setKnown },
55 {0,0}
56};
64int nlua_loadJump( nlua_env env )
65{
66 nlua_register(env, JUMP_METATABLE, jump_methods, 1);
67 return 0; /* No error */
68}
69
92LuaJump* lua_tojump( lua_State *L, int ind )
93{
94 return (LuaJump*) lua_touserdata(L,ind);
95}
103LuaJump* luaL_checkjump( lua_State *L, int ind )
104{
105 if (lua_isjump(L,ind))
106 return lua_tojump(L,ind);
107 luaL_typerror(L, ind, JUMP_METATABLE);
108 return NULL;
109}
110
121static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind, int *offset )
122{
123 JumpPoint *jp;
124 StarSystem *a, *b;
125
126 /* Defaults. */
127 jp = NULL;
128 a = NULL;
129 b = NULL;
130
131 if (lua_isjump(L, ind)) {
132 const LuaJump *lj = luaL_checkjump(L, ind);
133 a = system_getIndex( lj->srcid );
134 b = system_getIndex( lj->destid );
135 if (offset != NULL)
136 *offset = 1;
137 }
138 else if (lua_gettop(L) > 1) {
139 if (lua_isstring(L, ind))
140 a = system_get( lua_tostring( L, ind ));
141 else if (lua_issystem(L, ind))
142 a = system_getIndex( lua_tosystem(L, ind) );
143
144 if (lua_isstring(L, ind+1))
145 b = system_get( lua_tostring( L, ind+1 ));
146 else if (lua_issystem(L, ind+1))
147 b = system_getIndex( lua_tosystem(L, ind+1) );
148
149 if (offset != NULL)
150 *offset = 2;
151 }
152 else {
153 luaL_typerror(L, ind, JUMP_METATABLE);
154 // noreturn
155 }
156
157 if ((b != NULL) && (a != NULL))
158 jp = jump_getTarget( b, a );
159
160 if (jp == NULL)
161 NLUA_ERROR(L, _("Jump is invalid"));
162
163 return jp;
164}
165
173JumpPoint* luaL_validjump( lua_State *L, int ind )
174{
175 return luaL_validjumpSystem( L, ind, NULL );
176}
177
185LuaJump* lua_pushjump( lua_State *L, LuaJump jump )
186{
187 LuaJump *j = (LuaJump*) lua_newuserdata(L, sizeof(LuaJump));
188 *j = jump;
189 luaL_getmetatable(L, JUMP_METATABLE);
190 lua_setmetatable(L, -2);
191 return j;
192}
193
201int lua_isjump( lua_State *L, int ind )
202{
203 int ret;
204
205 if (lua_getmetatable(L,ind)==0)
206 return 0;
207 lua_getfield(L, LUA_REGISTRYINDEX, JUMP_METATABLE);
208
209 ret = 0;
210 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
211 ret = 1;
212
213 lua_pop(L, 2); /* remove both metatables */
214 return ret;
215}
216
231static int jumpL_get( lua_State *L )
232{
233 StarSystem *a, *b;
234
235 a = luaL_validsystem(L,1);
236 b = luaL_validsystem(L,2);
237
238 if ((a == NULL) || (b == NULL))
239 return NLUA_ERROR(L, _("No matching jump points found."));
240
241 if (jump_getTarget(b, a) != NULL) {
242 LuaJump lj;
243 lj.srcid = a->id;
244 lj.destid = b->id;
245 lua_pushjump(L, lj);
246
247 /* The inverse. If it doesn't exist, there are bigger problems. */
248 lj.srcid = b->id;
249 lj.destid = a->id;
250 lua_pushjump(L, lj);
251 return 2;
252 }
253
254 return 0;
255}
256
266static int jumpL_eq( lua_State *L )
267{
268 LuaJump *a, *b;
269 a = luaL_checkjump(L,1);
270 b = luaL_checkjump(L,2);
271 lua_pushboolean(L,((a->srcid == b->srcid) && (a->destid == b->destid)));
272 return 1;
273}
274
281static int jumpL_tostring( lua_State *L )
282{
283 char buf[STRMAX_SHORT];
284 const LuaJump *lj = luaL_checkjump(L,1);
285 const StarSystem *src = system_getIndex( lj->srcid );
286 const StarSystem *dst = system_getIndex( lj->destid );
287 snprintf( buf, sizeof(buf), _("Jump( %s -> %s )"), _(src->name), _(dst->name) );
288 lua_pushstring( L, buf );
289 return 1;
290}
291
300static int jumpL_radius( lua_State *L )
301{
302 const JumpPoint *jp = luaL_validjump(L,1);
303 lua_pushnumber(L,jp->radius);
304 return 1;
305}
306
315static int jumpL_position( lua_State *L )
316{
317 const JumpPoint *jp = luaL_validjump(L,1);
318 lua_pushvector(L, jp->pos);
319 return 1;
320}
321
330static int jumpL_angle( lua_State *L )
331{
332 const JumpPoint *jp = luaL_validjump(L,1);
333 lua_pushnumber(L, jp->angle);
334 return 1;
335}
336
345static int jumpL_hidden( lua_State *L )
346{
347 const JumpPoint *jp = luaL_validjump(L,1);
348 lua_pushboolean(L, jp_isFlag(jp, JP_HIDDEN) );
349 return 1;
350}
351
360static int jumpL_exitonly( lua_State *L )
361{
362 const JumpPoint *jp = luaL_validjump(L,1);
363 lua_pushboolean(L, jp_isFlag(jp, JP_EXITONLY) );
364 return 1;
365}
366
375static int jumpL_system( lua_State *L )
376{
377 const JumpPoint *jp = luaL_validjumpSystem( L, 1, NULL );
378 lua_pushsystem( L, jp->from->id );
379 return 1;
380}
381
390static int jumpL_dest( lua_State *L )
391{
392 const JumpPoint *jp = luaL_validjump(L,1);
393 lua_pushsystem(L,jp->targetid);
394 return 1;
395}
396
397
407static int jumpL_jumpDist( lua_State *L )
408{
409 const JumpPoint *jp = luaL_validjump(L,1);
410 const Pilot *p = luaL_validpilot(L,2);
411 lua_pushnumber( L, space_jumpDistance(p,jp) );
412 return 1;
413}
414
424static int jumpL_isKnown( lua_State *L )
425{
426 const JumpPoint *jp = luaL_validjump(L,1);
427 lua_pushboolean(L, jp_isKnown(jp));
428 return 1;
429}
430
441static int jumpL_setKnown( lua_State *L )
442{
443 int b, offset, changed;
444 JumpPoint *jp;
445
446 offset = 0;
447 jp = luaL_validjumpSystem( L, 1, &offset );
448
449 /* True if boolean isn't supplied. */
450 if (lua_gettop(L) > offset)
451 b = lua_toboolean(L, 1 + offset);
452 else
453 b = 1;
454
455 changed = ((b != (int)jp_isKnown(jp)) || (b != (int)jp_isKnown(jp->returnJump)));
456
457 if (b) {
458 jp_setFlag( jp, JP_KNOWN );
459 jp_setFlag( jp->returnJump, JP_KNOWN );
460 }
461 else {
462 jp_rmFlag( jp, JP_KNOWN );
463 jp_rmFlag( jp->returnJump, JP_KNOWN );
464 }
465
466 if (changed) {
467 /* Update overlay. */
468 ovr_refresh();
469 /* Update outfits image array - in the case it changes map owned status. */
471 }
472
473 return 0;
474}
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
Header file with generic functions and naev-specifics.
static int jumpL_jumpDist(lua_State *L)
Gets the distance from the jump point at which the pilot can jump.
Definition nlua_jump.c:407
static int jumpL_tostring(lua_State *L)
Converts a jump to readable form. Mainly meant to be used for printing.
Definition nlua_jump.c:281
static int jumpL_exitonly(lua_State *L)
Checks whether a jump is exit-only.
Definition nlua_jump.c:360
static int jumpL_setKnown(lua_State *L)
Sets a jump's known state.
Definition nlua_jump.c:441
static int jumpL_system(lua_State *L)
Gets the system that a jump point exists in.
Definition nlua_jump.c:375
static RETURNS_NONNULL JumpPoint * luaL_validjumpSystem(lua_State *L, int ind, int *offset)
Back-end for luaL_validjump.
Definition nlua_jump.c:121
LuaJump * luaL_checkjump(lua_State *L, int ind)
Gets jump at index raising an error if isn't a jump.
Definition nlua_jump.c:103
static int jumpL_angle(lua_State *L)
Gets the angle of a jump in radians.
Definition nlua_jump.c:330
static int jumpL_radius(lua_State *L)
Gets the jump's radius.
Definition nlua_jump.c:300
static int jumpL_isKnown(lua_State *L)
Checks to see if a jump is known by the player.
Definition nlua_jump.c:424
static int jumpL_dest(lua_State *L)
Gets the system that a jump point exits into.
Definition nlua_jump.c:390
static int jumpL_eq(lua_State *L)
You can use the '==' operator within Lua to compare jumps with this.
Definition nlua_jump.c:266
static const luaL_Reg jump_methods[]
Definition nlua_jump.c:41
static int jumpL_get(lua_State *L)
Gets a jump.
Definition nlua_jump.c:231
static int jumpL_hidden(lua_State *L)
Checks whether a jump is hidden.
Definition nlua_jump.c:345
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
Definition nlua_jump.c:185
int nlua_loadJump(nlua_env env)
Loads the jump library.
Definition nlua_jump.c:64
JumpPoint * luaL_validjump(lua_State *L, int ind)
Gets a jump directly.
Definition nlua_jump.c:173
int lua_isjump(lua_State *L, int ind)
Checks to see if ind is a jump.
Definition nlua_jump.c:201
LuaJump * lua_tojump(lua_State *L, int ind)
This module allows you to handle the jumps from Lua.
Definition nlua_jump.c:92
static int jumpL_position(lua_State *L)
Gets the position of the jump in the system.
Definition nlua_jump.c:315
Pilot * luaL_validpilot(lua_State *L, int ind)
Makes sure the pilot is valid or raises a Lua error.
Definition nlua_pilot.c:547
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
int space_jumpDistance(const Pilot *p, const JumpPoint *jp)
Distance at which a pilot can jump.
Definition space.c:455
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition space.c:989
JumpPoint * jump_getTarget(const StarSystem *target, const StarSystem *sys)
Less safe version of jump_get that works with pointers.
Definition space.c:1246
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:960
Lua jump Wrapper.
Definition nlua_jump.h:14
int destid
Definition nlua_jump.h:16
int srcid
Definition nlua_jump.h:15
The representation of an in-game pilot.
Definition pilot.h:217