13#include "nlua_system.h"
16#include "gatherable.h"
17#include "land_outfits.h"
19#include "map_overlay.h"
20#include "nlua_commodity.h"
21#include "nlua_faction.h"
130 return *( (LuaSystem *)lua_touserdata( L, ind ) );
143 luaL_typerror( L, ind, SYSTEM_METATABLE );
162 }
else if ( lua_isstring( L, ind ) )
165 luaL_typerror( L, ind, SYSTEM_METATABLE );
170 NLUA_ERROR( L, _(
"System is invalid" ) );
184 LuaSystem *s = (LuaSystem *)lua_newuserdata( L,
sizeof( LuaSystem ) );
186 luaL_getmetatable( L, SYSTEM_METATABLE );
187 lua_setmetatable( L, -2 );
202 if ( lua_getmetatable( L, ind ) == 0 )
204 lua_getfield( L, LUA_REGISTRYINDEX, SYSTEM_METATABLE );
207 if ( lua_rawequal( L, -1, -2 ) )
248 if ( lua_isstring( L, 1 ) ) {
256 lua_pushvalue( L, 1 );
259 NLUA_INVALID_PARAMETER( L, 1 );
263 return NLUA_ERROR( L, _(
"No matching systems found." ) );
279 for (
int i = 0; i <
array_size( sys ); i++ ) {
281 lua_rawseti( L, -2, i + 1 );
304 lua_pushboolean( L, 1 );
306 lua_pushboolean( L, 0 );
326 lua_pushstring( L, system_name( sys ) );
360 lua_pushstring( L, sys->name );
374 if ( s->faction == -1 )
390 if ( s->background == NULL )
392 lua_pushstring( L, s->background );
410 lua_pushnumber( L, s->nebu_density );
411 lua_pushnumber( L, s->nebu_volatility );
425 lua_pushnumber( L, s->interference );
455 StarSystem *start, *goal;
459 h = lua_toboolean( L, 3 );
460 k = !lua_toboolean( L, 4 );
462 if ( !lua_isnoneornil( L, 2 ) ) {
471 if ( start->id == goal->id ) {
472 lua_pushnumber( L, 0. );
476 s = map_getJumpPath( start, NULL, goal, k, h, NULL, NULL );
478 lua_pushnumber( L, HUGE_VAL );
513 StarSystem *sys, *sysp;
517 h = lua_toboolean( L, 3 );
524 s = map_getJumpPath( sys, NULL, sysp, 1, h, NULL, NULL );
537 lua_rawseti( L, -2, ++pushed );
539 for (
int i = 0; i <
array_size( s ) - 1; i++ ) {
544 lua_rawseti( L, -2, ++pushed );
570 h = lua_toboolean( L, 2 );
574 for (
int i = 0; i <
array_size( s->jumps ); i++ ) {
576 if ( jp_isFlag( &s->jumps[i], JP_EXITONLY ) )
578 if ( !h && jp_isFlag( &s->jumps[i], JP_HIDDEN ) )
582 lua_rawseti( L, -2,
id++ );
601 int exitonly = lua_toboolean( L, 2 );
606 for (
int i = 0; i <
array_size( s->jumps ); i++ ) {
609 if ( ( exitonly ) && ( jp_isFlag( &s->jumps[i], JP_EXITONLY ) ) )
613 lj.
destid = s->jumps[i].targetid;
615 lua_rawseti( L, -2, ++pushed );
636 for (
int i = 0; i <
array_size( s->asteroids ); i++ ) {
639 lua_pushinteger( L, i + 1 );
640 lua_setfield( L, -2,
"id" );
643 lua_setfield( L, -2,
"pos" );
645 lua_pushnumber( L, s->asteroids[i].density );
646 lua_setfield( L, -2,
"density" );
648 lua_pushnumber( L, s->asteroids[i].radius );
649 lua_setfield( L, -2,
"radius" );
651 lua_rawseti( L, -2, i + 1 );
675 unsigned int player_only;
678 vec2 zero = { .x = 0., .y = 0., .mod = 0., .angle = 0. };
683 nb = luaL_checkint( L, 2 );
684 pos = luaL_optvector( L, 3, &zero );
685 vel = luaL_optvector( L, 4, &zero );
686 lifelength = luaL_optnumber( L, 5, -1. );
687 player_only = lua_toboolean( L, 6 );
690 L,
gatherable_init( commodity, pos, vel, lifelength, nb, player_only ) );
715 for (
int i = 0; i <
array_size( s->presence ); i++ ) {
717 if ( s->presence[i].value <= 0 )
720 lua_pushstring( L,
faction_name( s->presence[i].faction ) );
721 lua_pushnumber( L, s->presence[i].value );
722 lua_settable( L, -3 );
744 for (
int i = 0; i <
array_size( s->spobs ); i++ ) {
746 lua_rawseti( L, -2, i + 1 );
784 if ( lua_isstring( L, 2 ) ) {
786 const char *cmd = lua_tostring( L, 2 );
790 if ( strcmp( cmd,
"all" ) == 0 )
792 else if ( strcmp( cmd,
"friendly" ) == 0 )
794 else if ( strcmp( cmd,
"hostile" ) == 0 )
796 else if ( strcmp( cmd,
"neutral" ) == 0 )
811 for (
int i = 0; i <
array_size( fct ); i++ ) {
822 lua_pushnumber( L, presence );
840 lua_pushnumber( L, sys->radius );
856 lua_pushboolean( L, sys_isKnown( sys ) );
877 b = lua_toboolean( L, 2 );
878 if ( lua_gettop( L ) > 2 )
879 r = lua_toboolean( L, 3 );
882 sys_setFlag( sys, SYSTEM_KNOWN );
884 sys_rmFlag( sys, SYSTEM_KNOWN );
888 for (
int i = 0; i <
array_size( sys->spobs ); i++ )
890 for (
int i = 0; i <
array_size( sys->jumps ); i++ )
891 jp_setFlag( &sys->jumps[i], JP_KNOWN );
893 for (
int i = 0; i <
array_size( sys->spobs ); i++ )
894 spob_rmFlag( sys->spobs[i], SPOB_KNOWN );
895 for (
int i = 0; i <
array_size( sys->jumps ); i++ )
896 jp_rmFlag( &sys->jumps[i], JP_KNOWN );
919 lua_pushboolean( L, sys_isFlag( sys, SYSTEM_HIDDEN ) );
935 int b = lua_toboolean( L, 2 );
937 sys_setFlag( sys, SYSTEM_HIDDEN );
939 sys_rmFlag( sys, SYSTEM_HIDDEN );
982 str = luaL_optstring( L, 2, NULL );
983 r = luaL_optnumber( L, 3, -1. );
987 id = ovr_mrkAddPoint( str, vec->
x, vec->
y );
989 id = ovr_mrkAddCircle( str, vec->
x, vec->
y, r );
990 lua_pushnumber( L,
id );
1004 unsigned int id = luaL_checklong( L, 1 );
1023 if ( lua_isnoneornil( L, 2 ) ) {
1025 for (
int i = 0; i <
array_size( sys->presence ); i++ ) {
1026 const char *name =
faction_name( sys->presence[i].faction );
1027 lua_pushnumber( L, sys->presence[i].local );
1028 lua_setfield( L, -2, name );
1034 for (
int i = 0; i <
array_size( sys->presence ); i++ ) {
1035 if ( sys->presence[i].faction == f ) {
1036 lua_pushnumber( L, sys->presence[i].local );
1056 double m = luaL_checknumber( L, 3 );
1060 faction_updateSingle( f );
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
int * faction_getGroup(int which)
Returns an array of faction ids.
const char * faction_name(int f)
Gets a factions "real" (internal) name.
double faction_reputation(int f)
Gets the player's standing with a faction.
int gatherable_init(const Commodity *com, const vec2 *pos, const vec2 *vel, double lifeleng, int qtt, unsigned int player_only)
Initializes a gatherable object.
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
int nlua_helperTags(lua_State *L, int idx, char *const *tags)
Helper function to deal with tags.
Commodity * luaL_validcommodity(lua_State *L, int ind)
Makes sure the commodity is valid or raises a Lua error.
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.
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
int lua_isspob(lua_State *L, int ind)
Checks to see if ind is a spob.
static int systemL_name(lua_State *L)
Returns the system's translated name.
static int systemL_adjacent(lua_State *L)
Gets all the adjacent systems to a system.
static int systemL_position(lua_State *L)
Returns the position of the system.
static int systemL_eq(lua_State *L)
Check systems for equality.
LuaSystem luaL_checksystem(lua_State *L, int ind)
Gets system at index raising an error if type doesn't match.
static int systemL_faction(lua_State *L)
Gets system faction.
static int systemL_markerClear(lua_State *L)
Clears the system markers.
static int systemL_setReputation(lua_State *L)
Sets the stored local reputation of a system.
static int systemL_markerAdd(lua_State *L)
Adds a system marker.
int nlua_loadSystem(nlua_env env)
Loads the system library.
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
static int systemL_presence(lua_State *L)
Gets the presence in the system.
static int systemL_jumpPath(lua_State *L)
Gets jump path from current system, or to another.
static int systemL_interference(lua_State *L)
Gets the system's interference level.
static int systemL_addGatherable(lua_State *L)
Adds a gatherable object.
static int systemL_setHidden(lua_State *L)
Sets a system to be hidden to the player.
static int systemL_markerRm(lua_State *L)
Removes a system marker.
static int systemL_reputation(lua_State *L)
Gets the stored local reputation of a system.
static const luaL_Reg system_methods[]
static int systemL_asteroidFields(lua_State *L)
Gets all the asteroid fields in a system.
static int systemL_hidden(lua_State *L)
Checks to see if a system is hidden by the player.
static int systemL_background(lua_State *L)
Gets system background.
static int systemL_nebula(lua_State *L)
Gets the system's nebula parameters.
static int systemL_tags(lua_State *L)
Gets the system tags.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
static int systemL_jumps(lua_State *L)
Gets all the jumps in a system.
static int systemL_presences(lua_State *L)
Returns the factions that have presence in a system and their respective presence values....
static int systemL_isknown(lua_State *L)
Checks to see if a system is known by the player.
static int systemL_spobs(lua_State *L)
Gets the spobs in a system.
static int systemL_jumpdistance(lua_State *L)
Gets jump distance from current system, or to another.
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
static int systemL_cur(lua_State *L)
Gets the current system.
static int systemL_setknown(lua_State *L)
Sets a system's known state.
static int systemL_get(lua_State *L)
Gets a system.
static int systemL_getAll(lua_State *L)
Gets all the systems. Lua return parameter: {System,...} A list of all the systems.
static int systemL_radius(lua_State *L)
Gets the radius of the system.
static int systemL_nameRaw(lua_State *L)
Returns the system's raw (untranslated) name.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
StarSystem * system_getIndex(int id)
Get the system by its index.
int spob_index(const Spob *p)
Gets the ID of a spob.
StarSystem * system_getAll(void)
Gets an array (array.h) of all star systems.
StarSystem * system_get(const char *sysname)
Get the system from its name.
void spob_setKnown(Spob *p)
Sets a spob's known status, if it's real.
double system_getPresence(const StarSystem *sys, int faction)
Get the presence of a faction in a system.
const char * spob_getSystemName(const char *spobname)
Get the name of a system from a spobname.
int system_index(const StarSystem *sys)
Gets the index of a star system.
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Represents presence in a system.