naev 0.11.5
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
69static int safelanesL_get( lua_State *L )
70{
71 int faction, standing, onlyknown;
72 StarSystem *sys;
73 SafeLane *lanes;
74 const char *std;
75
76 if (!lua_isnoneornil(L,1))
77 faction = luaL_validfaction( L, 1 );
78 else
79 faction = -1;
80 std = luaL_optstring(L, 2, NULL);
81 if (!lua_isnoneornil(L,3))
82 sys = luaL_validsystem( L, 3 );
83 else
84 sys = cur_system;
85 onlyknown = lua_toboolean(L,4);
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 return NLUA_ERROR(L,_("Unknown standing type '%s'!"), std);
102 }
103
104 /* Get and process the lanes. */
105 lanes = safelanes_get( faction, standing, sys );
106 lua_newtable( L );
107 for (int i=0; i<array_size(lanes); i++) {
108 const SafeLane *l = &lanes[i];
109 if (onlyknown) {
110 /* Have to do a knowness check. */
111 int known = 1;
112 for (int j=0; j<2; j++) {
113 switch (l->point_type[j]) {
114 case SAFELANE_LOC_SPOB:
115 if (!spob_isKnown( spob_getIndex( l->point_id[j] )))
116 known = 0;
117 break;
118 case SAFELANE_LOC_DEST_SYS:
119 if (!jp_isKnown( jump_getTarget( system_getIndex(l->point_id[j]), sys)))
120 known = 0;
121 break;
122 default:
123 return NLUA_ERROR( L, _("Safe-lane vertex type is invalid.") );
124 }
125 }
126 if (!known)
127 continue;
128 }
129 lua_newtable( L );
130 for (int j=0; j<2; j++) {
131 const Spob *pnt;
132 const JumpPoint *jmp;
133 switch (l->point_type[j]) {
134 case SAFELANE_LOC_SPOB:
135 pnt = spob_getIndex( l->point_id[j] );
136 //lua_pushspob( L, l->point_id[j] );
137#ifdef DEBUGGING
138 if (pnt==NULL)
139 return NLUA_ERROR(L, _("Spob is invalid"));
140#endif /* DEBUGGING */
141 lua_pushvector( L, pnt->pos );
142 break;
143 case SAFELANE_LOC_DEST_SYS:
144 //jump.srcid = system_index( sys );
145 //jump.destid = l->point_id[j];
146 //lua_pushjump( L, jump );
147 jmp = jump_getTarget( system_getIndex(l->point_id[j]), sys );
148#ifdef DEBUGGING
149 if (jmp==NULL)
150 return NLUA_ERROR(L, _("Jump is invalid"));
151#endif /* DEBUGGING */
152 lua_pushvector( L, jmp->pos );
153 break;
154 default:
155 return NLUA_ERROR( L, _("Safe-lane vertex type is invalid.") );
156 }
157 lua_rawseti( L, -2, j+1 );
158 }
159 lua_pushstring( L, "faction" ); /* key */
160 lua_pushfaction( L, l->faction ); /* value */
161 lua_rawset( L, -3 );
162 lua_rawseti( L, -2, i+1 );
163 }
164 array_free( lanes );
165
166 return 1;
167}
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.
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.
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.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
SafeLane * safelanes_get(int faction, int standing, const StarSystem *system)
Gets a set of safelanes for a faction and system.
Definition safelanes.c:190
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 * cur_system
Definition space.c:106
Spob * spob_getIndex(int ind)
Gets spob by index.
Definition space.c:1082
Describes a safe lane, patrolled by a faction, within a system.
Definition safelanes.h:24
int point_id[2]
Definition safelanes.h:27
int faction
Definition safelanes.h:25
SafeLaneLocType point_type[2]
Definition safelanes.h:26
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:89
vec2 pos
Definition space.h:94