naev 0.12.5
nlua_safelanes.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
4
10#include "nlua_safelanes.h"
11
12#include "array.h"
13#include "nlua_faction.h"
14#include "nlua_system.h"
15#include "nlua_vec2.h"
16#include "nluadef.h"
17#include "safelanes.h"
18
19/* diffs */
20static int safelanesL_get( lua_State *L );
21
22static const luaL_Reg safelanesL_methods[] = {
23 { "get", safelanesL_get }, { 0, 0 } };
24
30int nlua_loadSafelanes( nlua_env env )
31{
32 nlua_register( env, "safelanes", safelanesL_methods, 0 );
33 return 0;
34}
35
64static int safelanesL_get( lua_State *L )
65{
66 int faction, standing, onlyknown;
67 StarSystem *sys;
68 SafeLane *lanes;
69 const char *std;
70
71 if ( !lua_isnoneornil( L, 1 ) )
72 faction = luaL_validfaction( L, 1 );
73 else
74 faction = -1;
75 std = luaL_optstring( L, 2, NULL );
76 if ( !lua_isnoneornil( L, 3 ) )
77 sys = luaL_validsystem( L, 3 );
78 else
79 sys = cur_system;
80 onlyknown = lua_toboolean( L, 4 );
81
82 /* Translate standing into number. */
83 standing = 0;
84 if ( ( faction >= 0 ) && ( std != NULL ) ) {
85 if ( strcmp( std, "friendly" ) == 0 )
86 standing |= SAFELANES_FRIENDLY;
87 else if ( strcmp( std, "neutral" ) == 0 )
88 standing |= SAFELANES_NEUTRAL;
89 else if ( strcmp( std, "hostile" ) == 0 )
90 standing |= SAFELANES_HOSTILE;
91 else if ( strcmp( std, "non-friendly" ) == 0 )
92 standing |= SAFELANES_NEUTRAL | SAFELANES_HOSTILE;
93 else if ( strcmp( std, "non-hostile" ) == 0 )
94 standing |= SAFELANES_NEUTRAL | SAFELANES_FRIENDLY;
95 else
96 return NLUA_ERROR( L, _( "Unknown standing type '%s'!" ), std );
97 }
98
99 /* Get and process the lanes. */
100 lanes = safelanes_get( faction, standing, sys );
101 lua_newtable( L );
102 for ( int i = 0; i < array_size( lanes ); i++ ) {
103 const SafeLane *l = &lanes[i];
104 if ( onlyknown ) {
105 /* Have to do a knowness check. */
106 int known = 1;
107 for ( int j = 0; j < 2; j++ ) {
108 switch ( l->point_type[j] ) {
109 case SAFELANE_LOC_SPOB:
110 if ( !spob_isKnown( spob_getIndex( l->point_id[j] ) ) )
111 known = 0;
112 break;
113 case SAFELANE_LOC_DEST_SYS:
114 if ( !jp_isKnown( jump_getTarget(
115 system_getIndex( l->point_id[j] ), sys ) ) )
116 known = 0;
117 break;
118 default:
119 return NLUA_ERROR( L, _( "Safe-lane vertex type is invalid." ) );
120 }
121 }
122 if ( !known )
123 continue;
124 }
125 lua_newtable( L );
126 for ( int j = 0; j < 2; j++ ) {
127 const Spob *pnt;
128 const JumpPoint *jmp;
129 switch ( l->point_type[j] ) {
130 case SAFELANE_LOC_SPOB:
131 pnt = spob_getIndex( l->point_id[j] );
132 // lua_pushspob( L, l->point_id[j] );
133#ifdef DEBUGGING
134 if ( pnt == NULL )
135 return NLUA_ERROR( L, _( "Spob is invalid" ) );
136#endif /* DEBUGGING */
137 lua_pushvector( L, pnt->pos );
138 break;
139 case SAFELANE_LOC_DEST_SYS:
140 // jump.srcid = system_index( sys );
141 // jump.destid = l->point_id[j];
142 // lua_pushjump( L, jump );
143 jmp = jump_getTarget( system_getIndex( l->point_id[j] ), sys );
144#ifdef DEBUGGING
145 if ( jmp == NULL )
146 return NLUA_ERROR( L, _( "Jump is invalid" ) );
147#endif /* DEBUGGING */
148 lua_pushvector( L, jmp->pos );
149 break;
150 default:
151 return NLUA_ERROR( L, _( "Safe-lane vertex type is invalid." ) );
152 }
153 lua_rawseti( L, -2, j + 1 );
154 }
155 lua_pushstring( L, "faction" ); /* key */
156 lua_pushfaction( L, l->faction ); /* value */
157 lua_rawset( L, -3 );
158 lua_rawseti( L, -2, i + 1 );
159 }
160 array_free( lanes );
161
162 return 1;
163}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
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:233
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 * cur_system
Definition space.c:110
Spob * spob_getIndex(int ind)
Gets spob by index.
Definition space.c:1140
Describes a safe lane, patrolled by a faction, within a system.
Definition safelanes.h:29
int point_id[2]
Definition safelanes.h:32
int faction
Definition safelanes.h:30
SafeLaneLocType point_type[2]
Definition safelanes.h:31
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:102
vec2 pos
Definition space.h:109