15#include "nlua_faction.h"
20#include "nlua_colour.h"
21#include "nlua_system.h"
26static LuaFaction luaL_validfactionSilent( lua_State *L,
int ind );
149 const char *s = luaL_checkstring( L, 1 );
183 for (
int i = 0; i <
array_size( factions ); i++ ) {
185 lua_rawseti( L, -2, i + 1 );
214 return *( (LuaFaction *)lua_touserdata( L, ind ) );
217LuaFaction lua_checkfaction( lua_State *L,
int ind )
221 luaL_typerror( L, ind, FACTION_METATABLE );
225static LuaFaction luaL_validfactionSilent( lua_State *L,
int ind )
229 else if ( lua_isstring( L, ind ) )
231 luaL_typerror( L, ind, FACTION_METATABLE );
245 int id = luaL_validfactionSilent( L, ind );
247 return NLUA_ERROR( L, _(
"Faction '%s' not found in stack." ),
248 lua_tostring( L, ind ) );
261 LuaFaction *f = (LuaFaction *)lua_newuserdata( L,
sizeof( LuaFaction ) );
263 luaL_getmetatable( L, FACTION_METATABLE );
264 lua_setmetatable( L, -2 );
278 if ( lua_getmetatable( L, ind ) == 0 )
280 lua_getfield( L, LUA_REGISTRYINDEX, FACTION_METATABLE );
283 if ( lua_rawequal( L, -1, -2 ) )
306 lua_pushboolean( L, a == b );
405 if ( !lua_isnoneornil( L, 3 ) )
428 if ( !lua_isnoneornil( L, 3 ) )
431 lua_pushboolean( L,
areAllies( f, ff ) );
461 double mod = luaL_checknumber( L, 2 );
462 const StarSystem *sys =
464 const char *reason = luaL_optstring( L, 4,
"script" );
465 double ret =
faction_hit( f, sys, mod, reason, lua_toboolean( L, 5 ) );
466 lua_pushnumber( L, ret );
488 double mod = luaL_checknumber( L, 2 );
489 const StarSystem *sys =
491 const char *reason = luaL_optstring( L, 4,
"script" );
493 lua_pushnumber( L, ret );
525 if ( lua_isnoneornil( L, 2 ) )
559 double n = luaL_checknumber( L, 2 );
579 faction_applyLocalThreshold( f, sys );
599 NLUA_DEPRECATED( L,
"modPlayer" );
601 double n = luaL_checknumber( L, 2 );
602 const char *reason = luaL_optstring( L, 3,
"script" );
623 NLUA_DEPRECATED( L,
"modPlayerSingle" );
625 double n = luaL_checknumber( L, 2 );
626 const char *reason = luaL_optstring( L, 3,
"script" );
645 NLUA_DEPRECATED( L,
"modPlayerRaw" );
647 double n = luaL_checknumber( L, 2 );
664 NLUA_DEPRECATED( L,
"setPlayerStanding" );
666 double n = luaL_checknumber( L, 2 );
687 NLUA_DEPRECATED( L,
"playerStanding" );
690 if ( !lua_isnoneornil( L, 2 ) )
691 n = luaL_checknumber( L, 2 );
694 lua_pushnumber( L, n );
708 NLUA_DEPRECATED( L,
"playerStandingDefault" );
711 lua_pushnumber( L, n );
732 for (
int i = 0; i <
array_size( factions ); i++ ) {
734 lua_rawseti( L, -2, i + 1 );
757 for (
int i = 0; i <
array_size( factions ); i++ ) {
759 lua_rawseti( L, -2, i + 1 );
841 int b = lua_toboolean( L, 2 );
892 double val = faction_reputationOverride( fac, &set );
895 lua_pushnumber( L, val );
910 int set = !lua_isnoneornil( L, 2 );
911 faction_setReputationOverride( fac, set, luaL_optnumber( L, 2, 0. ) );
953 LuaFaction fac, newfac;
954 const char *name, *display, *ai;
955 const glColour *colour;
956 int clear_allies, clear_enemies;
960 if ( !lua_isnoneornil( L, 1 ) )
961 fac = luaL_validfactionSilent( L, 1 );
964 name = luaL_checkstring( L, 2 );
965 display = luaL_optstring( L, 3, name );
972 return NLUA_ERROR( L,
973 _(
"Trying to overwrite existing faction '%s' with "
974 "dynamic faction!" ),
982 if ( lua_istable( L, 4 ) ) {
983 lua_getfield( L, 4,
"ai" );
984 ai = luaL_optstring( L, -1, NULL );
987 lua_getfield( L, 4,
"clear_allies" );
988 clear_allies = lua_toboolean( L, -1 );
991 lua_getfield( L, 4,
"clear_enemies" );
992 clear_enemies = lua_toboolean( L, -1 );
995 lua_getfield( L, 4,
"player" );
996 if ( lua_isnumber( L, -1 ) ) {
997 player = lua_tonumber( L, -1 );
1002 lua_getfield( L, 4,
"colour" );
1003 if ( lua_isstring( L, -1 ) )
1004 colour = col_fromName( lua_tostring( L, -1 ) );
1022 if ( clear_enemies )
1042 LuaFaction fac, ally;
1046 return NLUA_ERROR( L, _(
"Can only add allies to dynamic factions" ) );
1048 remove = lua_toboolean( L, 3 );
1068 LuaFaction fac, enemy;
1072 return NLUA_ERROR( L, _(
"Can only add allies to dynamic factions" ) );
1074 remove = lua_toboolean( L, 3 );
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.
const char * faction_longname(int f)
Gets the faction's long name (formal, human-readable).
const glColour * faction_reputationColour(int f)
Gets the colour of the faction based on it's standing with the player.
int faction_exists(const char *name)
Checks to see if a faction exists by name.
const int * faction_getEnemies(int f)
Gets the list of enemies of a faction.
int faction_dynAdd(int base, const char *name, const char *display, const char *ai, const glColour *colour)
Dynamically add a faction.
void faction_rmAlly(int f, int o)
Removes an ally from the faction's allies list.
int faction_isKnown(int id)
Is the faction known?
double faction_reputationDefault(int f)
Gets the player's default standing with a faction.
const glTexture * faction_logo(int f)
Gets the faction's logo (ideally 256x256).
void faction_clearEnemy(int f)
Clears all the enemies of a dynamic faction.
void faction_rmEnemy(int f, int o)
Removes an enemy from the faction's enemies list.
int areEnemies(int a, int b)
Checks whether two factions are enemies.
const char *const * faction_tags(int f)
Gets the tags the faction has.
double faction_hit(int f, const StarSystem *sys, double mod, const char *source, int single)
Handles a faction hit against a faction and how to apply it.
double faction_hitTest(int f, const StarSystem *sys, double mod, const char *source)
Tests a faction hit to see how much it would apply. Does not actually modify standing.
const char * faction_name(int f)
Gets a factions "real" (internal) name.
const char * faction_shortname(int f)
Gets a factions short name (human-readable).
int faction_isDynamic(int id)
Is faction dynamic.
int areNeutral(int a, int b)
Checks whether two factions are true neutral.
const int * faction_getAllies(int f)
Gets the list of allies of a faction.
void faction_modPlayerRaw(int f, double mod)
Modifies the player's standing without affecting others.
int faction_usesHiddenJumps(int f)
Checks to see if a faction uses hidden jumps.
int faction_isInvisible(int id)
Is the faction invisible?
const char * faction_getStandingTextAtValue(int f, double value)
Gets the player's standing in human readable form.
void faction_clearAlly(int f)
Clears all the ally of a dynamic faction.
void faction_modPlayer(int f, double mod, const char *source)
Modifies the player's standing with a faction.
void faction_setReputation(int f, double value)
Sets the player's standing with a faction.
int * faction_getAll(void)
Returns all faction IDs in an array (array.h).
void faction_modPlayerSingle(int f, double mod, const char *source)
Modifies the player's standing without affecting others.
int faction_setKnown(int id, int state)
Sets the factions known state.
void faction_addAlly(int f, int o)
Adds an ally to the faction's allies list.
double faction_reputation(int f)
Gets the player's standing with a faction.
int faction_isStatic(int id)
Is the faction static?
void faction_addEnemy(int f, int o)
Adds an enemy to the faction's enemies list.
int faction_get(const char *name)
Gets a faction ID by name.
int areAllies(int a, int b)
Checks whether two factions are allies or not.
int nlua_helperTags(lua_State *L, int idx, char *const *tags)
Helper function to deal with tags.
glColour * lua_tocolour(lua_State *L, int ind)
Lua bindings to interact with colours.
glColour * lua_pushcolour(lua_State *L, glColour colour)
Pushes a colour on the stack.
static int factionL_setplayerstanding(lua_State *L)
Sets the player's standing with the faction.
static int factionL_modplayerraw(lua_State *L)
Modifies the player's standing with the faction.
static int factionL_setReputationOverride(lua_State *L)
Gets the overriden reputation value of a faction.
static int factionL_reputationGlobal(lua_State *L)
Gets the player's global reputation with the faction.
static int factionL_longname(lua_State *L)
Gets the faction's translated long name.
static int factionL_tags(lua_State *L)
Gets the tags a faction has.
static int factionL_playerstandingdefault(lua_State *L)
Gets the player's default standing with the faction.
static int factionL_getAll(lua_State *L)
Gets all the factions.
static int factionL_dynAlly(lua_State *L)
Adds or removes allies to a faction. Only works with dynamic factions.
static int factionL_exists(lua_State *L)
Lua bindings to deal with factions.
static int factionL_colour(lua_State *L)
Gets the faction colour.
static int factionL_hitTest(lua_State *L)
Simulates modifying the player's standing with a faction and computes how much would be changed.
static int factionL_allies(lua_State *L)
Gets the allies of the faction.
static int factionL_areenemies(lua_State *L)
Checks to see if f is an enemy of e.
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 factionL_logo(lua_State *L)
Gets the faction logo.
static int factionL_dynEnemy(lua_State *L)
Adds or removes enemies to a faction. Only works with dynamic factions.
static int factionL_reputationDefault(lua_State *L)
Gets the player's default reputation with the faction.
static int factionL_playerstanding(lua_State *L)
Gets the player's standing with the faction.
static int factionL_areallies(lua_State *L)
Checks to see if f is an ally of a.
static int factionL_setReputationGlobal(lua_State *L)
Overrides the player's faction global standing with a faction. Use sparingly as it overrites local st...
int nlua_loadFaction(nlua_env env)
Loads the faction library.
static int factionL_setKnown(lua_State *L)
Sets a faction's known state.
static int factionL_reputationOverride(lua_State *L)
Gets the overriden reputation value of a faction.
static int factionL_usesHiddenJumps(lua_State *L)
Gets whether or not a faction uses hidden jumps.
static int factionL_hit(lua_State *L)
Modifies the player's standing with the faction.
static int factionL_dynAdd(lua_State *L)
Adds a faction dynamically. Note that if the faction already exists as a dynamic faction,...
static int factionL_nameRaw(lua_State *L)
Gets the faction's raw / "real" (untranslated, internal) name.
static int factionL_name(lua_State *L)
Gets the faction's translated short name.
static int factionL_enemies(lua_State *L)
Gets the enemies of the faction.
static int factionL_modplayersingle(lua_State *L)
Modifies the player's standing with the faction.
static int factionL_applyLocalThreshold(lua_State *L)
Enforces the local threshold of a faction starting at a particular system. Meant to be used when comp...
int lua_isfaction(lua_State *L, int ind)
Checks to see if ind is a faction.
static const luaL_Reg faction_methods[]
LuaFaction lua_tofaction(lua_State *L, int ind)
Gets faction at index.
static int factionL_reputationText(lua_State *L)
Gets the human readable standing text correpsonding (translated).
static int factionL_isInvisible(lua_State *L)
Checks to see if a faction is invisible the player.
static int factionL_areneutral(lua_State *L)
Checks to see if two factions are truly neutral with respect to each other.
static int factionL_isStatic(lua_State *L)
Checks to see if a faction has a static standing with the player.
static int factionL_eq(lua_State *L)
__eq (equality) metamethod for factions.
static int factionL_modplayer(lua_State *L)
Modifies the player's standing with the faction.
static int factionL_isKnown(lua_State *L)
Checks to see if a faction is known by the player.
static int factionL_get(lua_State *L)
Gets the faction based on its name.
static int factionL_player(lua_State *L)
Gets the player's faction.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
Abstraction for rendering sprite sheets.