naev 0.10.4
nlua_safelanes.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
4
12#include "naev.h"
15#include "nlua_safelanes.h"
16
17#include "array.h"
18#include "collision.h"
19#include "nlua_faction.h"
20#include "nlua_jump.h"
21#include "nlua_spob.h"
22#include "nlua_system.h"
23#include "nlua_vec2.h"
24#include "nluadef.h"
25#include "safelanes.h"
26
27/* diffs */
28static int safelanesL_get( lua_State *L );
29static const luaL_Reg safelanesL_methods[] = {
30 { "get", safelanesL_get },
31 {0,0}
32};
39int nlua_loadSafelanes( nlua_env env )
40{
41 nlua_register(env, "safelanes", safelanesL_methods, 0);
42 return 0;
43}
44
68static int safelanesL_get( lua_State *L )
69{
70 int i, j, faction, standing;
71 StarSystem *sys;
72 SafeLane *lanes;
73 Spob *pnt;
74 JumpPoint *jmp;
75 const char *std;
76
77 if (!lua_isnoneornil(L,1))
78 faction = luaL_validfaction( L, 1 );
79 else
80 faction = -1;
81 std = luaL_optstring(L, 2, NULL);
82 if (!lua_isnoneornil(L,3))
83 sys = luaL_validsystem( L, 3 );
84 else
85 sys = cur_system;
86
87 /* Translate standing into number. */
88 standing = 0;
89 if ((faction >= 0) && (std != NULL)) {
90 if (strcmp(std,"friendly")==0)
91 standing |= SAFELANES_FRIENDLY;
92 else if (strcmp(std,"neutral")==0)
93 standing |= SAFELANES_NEUTRAL;
94 else if (strcmp(std,"hostile")==0)
95 standing |= SAFELANES_HOSTILE;
96 else if (strcmp(std,"non-friendly")==0)
97 standing |= SAFELANES_NEUTRAL | SAFELANES_HOSTILE;
98 else if (strcmp(std,"non-hostile")==0)
99 standing |= SAFELANES_NEUTRAL | SAFELANES_FRIENDLY;
100 else
101 NLUA_ERROR(L,_("Unknown standing type '%s'!"), std);
102 }
103
104 lanes = safelanes_get( faction, standing, sys );
105 lua_newtable( L );
106 for (i=0; i<array_size(lanes); i++) {
107 lua_newtable( L );
108 for (j=0; j<2; j++) {
109 switch (lanes[i].point_type[j]) {
110 case SAFELANE_LOC_SPOB:
111 pnt = spob_getIndex( lanes[i].point_id[j] );
112 //lua_pushspob( L, lanes[i].point_id[j] );
113#ifdef DEBUGGING
114 if (pnt==NULL)
115 NLUA_ERROR(L, _("Spob is invalid"));
116#endif /* DEBUGGING */
117 lua_pushvector( L, pnt->pos );
118 break;
119 case SAFELANE_LOC_DEST_SYS:
120 //jump.srcid = system_index( sys );
121 //jump.destid = lanes[i].point_id[j];
122 //lua_pushjump( L, jump );
123 jmp = jump_getTarget( system_getIndex(lanes[i].point_id[j]), sys );
124#ifdef DEBUGGING
125 if (jmp==NULL)
126 NLUA_ERROR(L, _("Jump is invalid"));
127#endif /* DEBUGGING */
128 lua_pushvector( L, jmp->pos );
129 break;
130 default:
131 NLUA_ERROR( L, _("Safe-lane vertex type is invalid.") );
132 }
133 lua_rawseti( L, -2, j+1 );
134 }
135 lua_pushstring( L, "faction" ); /* key */
136 lua_pushfaction( L, lanes[i].faction ); /* value */
137 lua_rawset( L, -3 );
138 lua_rawseti( L, -2, i+1 );
139 }
140 array_free( lanes );
141
142 return 1;
143}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition: array.h:158
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
Header file with generic functions and naev-specifics.
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
Definition: nlua_faction.c:192
LuaFaction luaL_validfaction(lua_State *L, int ind)
Gets faction (or faction name) at index, raising an error if type isn't a valid faction.
Definition: nlua_faction.c:177
static int safelanesL_get(lua_State *L)
Lua accessor functions to safe lane information.
static const luaL_Reg safelanesL_methods[]
int nlua_loadSafelanes(nlua_env env)
Loads the safelanes Lua library.
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
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
SafeLane * safelanes_get(int faction, int standing, const StarSystem *system)
Gets a set of safelanes for a faction and system.
Definition: safelanes.c:187
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 * cur_system
Definition: space.c:105
Spob * spob_getIndex(int ind)
Gets spob by index.
Definition: space.c:1038
Describes a safe lane, patrolled by a faction, within a system.
Definition: safelanes.h:24
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition: space.h:88
vec2 pos
Definition: space.h:93