naev 0.12.6
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"
14
15#include "nlua_jump.h"
16
17#include "land_outfits.h"
18#include "map_overlay.h"
19#include "nlua_pilot.h"
20#include "nlua_system.h"
21#include "nlua_vec2.h"
22#include "nluadef.h"
23
24RETURNS_NONNULL static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind,
25 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_reverse( lua_State *L );
32static int jumpL_radius( lua_State *L );
33static int jumpL_position( lua_State *L );
34static int jumpL_angle( lua_State *L );
35static int jumpL_hide( lua_State *L );
36static int jumpL_hidden( lua_State *L );
37static int jumpL_exitonly( lua_State *L );
38static int jumpL_system( lua_State *L );
39static int jumpL_dest( lua_State *L );
40static int jumpL_jumpDist( lua_State *L );
41static int jumpL_isKnown( lua_State *L );
42static int jumpL_setKnown( lua_State *L );
43
44static const luaL_Reg jump_methods[] = {
45 { "get", jumpL_get },
46 { "__eq", jumpL_eq },
47 { "__tostring", jumpL_tostring },
48 { "reverse", jumpL_reverse },
49 { "radius", jumpL_radius },
50 { "pos", jumpL_position },
51 { "angle", jumpL_angle },
52 { "hide", jumpL_hide },
53 { "hidden", jumpL_hidden },
54 { "exitonly", jumpL_exitonly },
55 { "system", jumpL_system },
56 { "dest", jumpL_dest },
57 { "jumpDist", jumpL_jumpDist },
58 { "known", jumpL_isKnown },
59 { "setKnown", jumpL_setKnown },
60 { 0, 0 } };
61
68int nlua_loadJump( nlua_env env )
69{
70 nlua_register( env, JUMP_METATABLE, jump_methods, 1 );
71 return 0; /* No error */
72}
73
95LuaJump *lua_tojump( lua_State *L, int ind )
96{
97 return (LuaJump *)lua_touserdata( L, ind );
98}
99
106LuaJump *luaL_checkjump( lua_State *L, int ind )
107{
108 if ( lua_isjump( L, ind ) )
109 return lua_tojump( L, ind );
110 luaL_typerror( L, ind, JUMP_METATABLE );
111 return NULL;
112}
113
124static JumpPoint *luaL_validjumpSystem( lua_State *L, int ind, int *offset )
125{
126 JumpPoint *jp;
127 StarSystem *a, *b;
128
129 /* Defaults. */
130 jp = NULL;
131 a = NULL;
132 b = NULL;
133
134 if ( lua_isjump( L, ind ) ) {
135 const LuaJump *lj = luaL_checkjump( L, ind );
136 a = system_getIndex( lj->srcid );
137 b = system_getIndex( lj->destid );
138 if ( offset != NULL )
139 *offset = 1;
140 } else if ( lua_gettop( L ) > 1 ) {
141 if ( lua_isstring( L, ind ) )
142 a = system_get( lua_tostring( L, ind ) );
143 else if ( lua_issystem( L, ind ) )
144 a = system_getIndex( lua_tosystem( L, ind ) );
145
146 if ( lua_isstring( L, ind + 1 ) )
147 b = system_get( lua_tostring( L, ind + 1 ) );
148 else if ( lua_issystem( L, ind + 1 ) )
149 b = system_getIndex( lua_tosystem( L, ind + 1 ) );
150
151 if ( offset != NULL )
152 *offset = 2;
153 } else {
154 luaL_typerror( L, ind, JUMP_METATABLE );
155 // noreturn
156 }
157
158 if ( ( b != NULL ) && ( a != NULL ) )
159 jp = jump_getTarget( b, a );
160
161 if ( jp == NULL )
162 NLUA_ERROR( L, _( "Jump is invalid" ) );
163
164 return jp;
165}
166
174JumpPoint *luaL_validjump( lua_State *L, int ind )
175{
176 return luaL_validjumpSystem( L, ind, NULL );
177}
178
186LuaJump *lua_pushjump( lua_State *L, LuaJump jump )
187{
188 LuaJump *j = (LuaJump *)lua_newuserdata( L, sizeof( LuaJump ) );
189 *j = jump;
190 luaL_getmetatable( L, JUMP_METATABLE );
191 lua_setmetatable( L, -2 );
192 return j;
193}
194
202int lua_isjump( lua_State *L, int ind )
203{
204 int ret;
205
206 if ( lua_getmetatable( L, ind ) == 0 )
207 return 0;
208 lua_getfield( L, LUA_REGISTRYINDEX, JUMP_METATABLE );
209
210 ret = 0;
211 if ( lua_rawequal( L, -1, -2 ) ) /* does it have the correct mt? */
212 ret = 1;
213
214 lua_pop( L, 2 ); /* remove both metatables */
215 return ret;
216}
217
233static int jumpL_get( lua_State *L )
234{
235 const StarSystem *a = luaL_validsystem( L, 1 );
236 const StarSystem *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 const LuaJump *a = luaL_checkjump( L, 1 );
269 const LuaJump *b = luaL_checkjump( L, 2 );
270 lua_pushboolean(
271 L, ( ( a->srcid == b->srcid ) && ( a->destid == b->destid ) ) );
272 return 1;
273}
274
282static int jumpL_tostring( lua_State *L )
283{
284 char buf[STRMAX_SHORT];
285 const LuaJump *lj = luaL_checkjump( L, 1 );
286 const StarSystem *src = system_getIndex( lj->srcid );
287 const StarSystem *dst = system_getIndex( lj->destid );
288 snprintf( buf, sizeof( buf ), _( "Jump( %s -> %s )" ), _( src->name ),
289 _( dst->name ) );
290 lua_pushstring( L, buf );
291 return 1;
292}
293
301static int jumpL_reverse( lua_State *L )
302{
303 LuaJump ret;
304 const LuaJump *lj = luaL_checkjump( L, 1 );
305 ret.srcid = lj->destid;
306 ret.destid = lj->srcid;
307 lua_pushjump( L, ret );
308 return 1;
309}
310
319static int jumpL_radius( lua_State *L )
320{
321 const JumpPoint *jp = luaL_validjump( L, 1 );
322 lua_pushnumber( L, jp->radius );
323 return 1;
324}
325
334static int jumpL_position( lua_State *L )
335{
336 const JumpPoint *jp = luaL_validjump( L, 1 );
337 lua_pushvector( L, jp->pos );
338 return 1;
339}
340
349static int jumpL_hide( lua_State *L )
350{
351 const JumpPoint *jp = luaL_validjump( L, 1 );
352 lua_pushnumber( L, jp->hide );
353 return 1;
354}
355
364static int jumpL_angle( lua_State *L )
365{
366 const JumpPoint *jp = luaL_validjump( L, 1 );
367 lua_pushnumber( L, jp->angle );
368 return 1;
369}
370
379static int jumpL_hidden( lua_State *L )
380{
381 const JumpPoint *jp = luaL_validjump( L, 1 );
382 lua_pushboolean( L, jp_isFlag( jp, JP_HIDDEN ) );
383 return 1;
384}
385
395static int jumpL_exitonly( lua_State *L )
396{
397 const JumpPoint *jp = luaL_validjump( L, 1 );
398 lua_pushboolean( L, jp_isFlag( jp, JP_EXITONLY ) );
399 return 1;
400}
401
410static int jumpL_system( lua_State *L )
411{
412 const JumpPoint *jp = luaL_validjumpSystem( L, 1, NULL );
413 lua_pushsystem( L, jp->from->id );
414 return 1;
415}
416
425static int jumpL_dest( lua_State *L )
426{
427 const JumpPoint *jp = luaL_validjump( L, 1 );
428 lua_pushsystem( L, jp->targetid );
429 return 1;
430}
431
441static int jumpL_jumpDist( lua_State *L )
442{
443 const JumpPoint *jp = luaL_validjump( L, 1 );
444 const Pilot *p = luaL_validpilot( L, 2 );
445 lua_pushnumber( L, space_jumpDistance( p, jp ) );
446 return 1;
447}
448
458static int jumpL_isKnown( lua_State *L )
459{
460 const JumpPoint *jp = luaL_validjump( L, 1 );
461 lua_pushboolean( L, jp_isKnown( jp ) );
462 return 1;
463}
464
476static int jumpL_setKnown( lua_State *L )
477{
478 int b, offset, changed;
479 JumpPoint *jp;
480
481 offset = 0;
482 jp = luaL_validjumpSystem( L, 1, &offset );
483
484 /* True if boolean isn't supplied. */
485 if ( lua_gettop( L ) > offset )
486 b = lua_toboolean( L, 1 + offset );
487 else
488 b = 1;
489
490 changed = ( ( b != (int)jp_isKnown( jp ) ) ||
491 ( b != (int)jp_isKnown( jp->returnJump ) ) );
492
493 if ( b ) {
494 jp_setFlag( jp, JP_KNOWN );
495 jp_setFlag( jp->returnJump, JP_KNOWN );
496 } else {
497 jp_rmFlag( jp, JP_KNOWN );
498 jp_rmFlag( jp->returnJump, JP_KNOWN );
499 }
500
501 if ( changed ) {
502 /* Update overlay. */
503 ovr_refresh();
504 /* Update outfits image array - in the case it changes map owned status.
505 */
507 }
508
509 return 0;
510}
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
Header file with generic functions and naev-specifics.
static int jumpL_hide(lua_State *L)
Gets the hide value of a jump in radians.
Definition nlua_jump.c:349
static int jumpL_reverse(lua_State *L)
Gets the opposite jump.
Definition nlua_jump.c:301
static int jumpL_jumpDist(lua_State *L)
Gets the distance from the jump point at which the pilot can jump.
Definition nlua_jump.c:441
static int jumpL_tostring(lua_State *L)
Converts a jump to readable form. Mainly meant to be used for printing.
Definition nlua_jump.c:282
static int jumpL_exitonly(lua_State *L)
Checks whether a jump is exit-only.
Definition nlua_jump.c:395
static int jumpL_setKnown(lua_State *L)
Sets a jump's known state.
Definition nlua_jump.c:476
static int jumpL_system(lua_State *L)
Gets the system that a jump point exists in.
Definition nlua_jump.c:410
static RETURNS_NONNULL JumpPoint * luaL_validjumpSystem(lua_State *L, int ind, int *offset)
Back-end for luaL_validjump.
Definition nlua_jump.c:124
LuaJump * luaL_checkjump(lua_State *L, int ind)
Gets jump at index raising an error if isn't a jump.
Definition nlua_jump.c:106
static int jumpL_angle(lua_State *L)
Gets the angle of a jump in radians.
Definition nlua_jump.c:364
static int jumpL_radius(lua_State *L)
Gets the jump's radius.
Definition nlua_jump.c:319
static int jumpL_isKnown(lua_State *L)
Checks to see if a jump is known by the player.
Definition nlua_jump.c:458
static int jumpL_dest(lua_State *L)
Gets the system that a jump point exits into.
Definition nlua_jump.c:425
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:44
static int jumpL_get(lua_State *L)
Gets a jump.
Definition nlua_jump.c:233
static int jumpL_hidden(lua_State *L)
Checks whether a jump is hidden.
Definition nlua_jump.c:379
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
Definition nlua_jump.c:186
int nlua_loadJump(nlua_env env)
Loads the jump library.
Definition nlua_jump.c:68
JumpPoint * luaL_validjump(lua_State *L, int ind)
Gets a jump directly.
Definition nlua_jump.c:174
int lua_isjump(lua_State *L, int ind)
Checks to see if ind is a jump.
Definition nlua_jump.c:202
LuaJump * lua_tojump(lua_State *L, int ind)
This module allows you to handle the jumps from Lua.
Definition nlua_jump.c:95
static int jumpL_position(lua_State *L)
Gets the position of the jump in the system.
Definition nlua_jump.c:334
Pilot * luaL_validpilot(lua_State *L, int ind)
Makes sure the pilot is valid or raises a Lua error.
Definition nlua_pilot.c:560
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:479
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition space.c:1038
JumpPoint * jump_getTarget(const StarSystem *target, const StarSystem *sys)
Less safe version of jump_get that works with pointers.
Definition space.c:1303
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:1007
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:263