naev 0.11.5
nlua_faction.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
12#include <lauxlib.h>
13
14#include "naev.h"
17#include "nlua_faction.h"
18
19#include "array.h"
20#include "faction.h"
21#include "log.h"
22#include "nlua_colour.h"
23#include "nlua_tex.h"
24#include "nluadef.h"
25
26/* Internal useful functions. */
27static LuaFaction luaL_validfactionSilent( lua_State *L, int ind );
28/* Faction metatable methods */
29static int factionL_exists( lua_State *L );
30static int factionL_get( lua_State *L );
31static int factionL_getAll(lua_State *L);
32static int factionL_player( lua_State *L );
33static int factionL_eq( lua_State *L );
34static int factionL_name( lua_State *L );
35static int factionL_nameRaw( lua_State *L );
36static int factionL_longname( lua_State *L );
37static int factionL_areenemies( lua_State *L );
38static int factionL_areallies( lua_State *L );
39static int factionL_usesHiddenJumps( lua_State *L );
40static int factionL_modplayer( lua_State *L );
41static int factionL_modplayersingle( lua_State *L );
42static int factionL_modplayerraw( lua_State *L );
43static int factionL_setplayerstanding( lua_State *L );
44static int factionL_playerstanding( lua_State *L );
45static int factionL_playerstandingdefault( lua_State *L );
46static int factionL_enemies( lua_State *L );
47static int factionL_allies( lua_State *L );
48static int factionL_logo( lua_State *L );
49static int factionL_colour( lua_State *L );
50static int factionL_isKnown( lua_State *L );
51static int factionL_setKnown( lua_State *L );
52static int factionL_isInvisible( lua_State *L );
53static int factionL_isStatic( lua_State *L );
54static int factionL_tags( lua_State *L );
55static int factionL_dynAdd( lua_State *L );
56static int factionL_dynAlly( lua_State *L );
57static int factionL_dynEnemy( lua_State *L );
58static const luaL_Reg faction_methods[] = {
59 { "exists", factionL_exists },
60 { "get", factionL_get },
61 { "getAll", factionL_getAll },
62 { "player", factionL_player },
63 { "__eq", factionL_eq },
64 { "__tostring", factionL_name },
65 { "name", factionL_name },
66 { "nameRaw", factionL_nameRaw },
67 { "longname", factionL_longname },
68 { "areEnemies", factionL_areenemies },
69 { "areAllies", factionL_areallies },
70 { "modPlayer", factionL_modplayer },
71 { "modPlayerSingle", factionL_modplayersingle },
72 { "modPlayerRaw", factionL_modplayerraw },
73 { "setPlayerStanding", factionL_setplayerstanding },
74 { "playerStanding", factionL_playerstanding },
75 { "playerStandingDefault", factionL_playerstandingdefault },
76 { "enemies", factionL_enemies },
77 { "allies", factionL_allies },
78 { "usesHiddenJumps", factionL_usesHiddenJumps },
79 { "logo", factionL_logo },
80 { "colour", factionL_colour },
81 { "known", factionL_isKnown },
82 { "setKnown", factionL_setKnown },
83 { "invisible", factionL_isInvisible },
84 { "static", factionL_isStatic },
85 { "tags", factionL_tags },
86 { "dynAdd", factionL_dynAdd },
87 { "dynAlly", factionL_dynAlly },
88 { "dynEnemy", factionL_dynEnemy },
89 {0,0}
90};
98int nlua_loadFaction( nlua_env env )
99{
100 nlua_register(env, FACTION_METATABLE, faction_methods, 1);
101 return 0; /* No error */
102}
103
126static int factionL_exists( lua_State *L )
127{
128 const char *s = luaL_checkstring(L,1);
129 if (faction_exists(s)) {
131 return 1;
132 }
133 return 0;
134}
135
145static int factionL_get( lua_State *L )
146{
147 LuaFaction f = luaL_validfaction(L,1);
148 lua_pushfaction(L,f);
149 return 1;
150}
151
158static int factionL_getAll(lua_State *L)
159{
160 const int* factions = faction_getAll();
161 lua_newtable(L);
162 for (int i=0; i<array_size(factions); i++) {
163 lua_pushfaction( L, factions[i] );
164 lua_rawseti( L, -2, i+1 );
165 }
166 array_free(factions);
167 return 1;
168}
169
178static int factionL_player( lua_State *L )
179{
181 return 1;
182}
183
191LuaFaction lua_tofaction( lua_State *L, int ind )
192{
193 return *((LuaFaction*) lua_touserdata(L,ind));
194}
195
196static LuaFaction luaL_validfactionSilent( lua_State *L, int ind )
197{
198 if (lua_isfaction(L,ind))
199 return lua_tofaction(L,ind);
200 else if (lua_isstring(L,ind))
201 return faction_get( lua_tostring(L, ind) );
202 luaL_typerror(L, ind, FACTION_METATABLE);
203 return 0;
204}
205
213LuaFaction luaL_validfaction( lua_State *L, int ind )
214{
215 int id = luaL_validfactionSilent( L, ind );
216 if (id == -1)
217 return NLUA_ERROR(L,_("Faction '%s' not found in stack."), lua_tostring(L,ind) );
218 return id;
219}
220
228LuaFaction* lua_pushfaction( lua_State *L, LuaFaction faction )
229{
230 LuaFaction *f = (LuaFaction*) lua_newuserdata(L, sizeof(LuaFaction));
231 *f = faction;
232 luaL_getmetatable(L, FACTION_METATABLE);
233 lua_setmetatable(L, -2);
234 return f;
235}
243int lua_isfaction( lua_State *L, int ind )
244{
245 int ret;
246
247 if (lua_getmetatable(L,ind)==0)
248 return 0;
249 lua_getfield(L, LUA_REGISTRYINDEX, FACTION_METATABLE);
250
251 ret = 0;
252 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
253 ret = 1;
254
255 lua_pop(L, 2); /* remove both metatables */
256 return ret;
257}
258
271static int factionL_eq( lua_State *L )
272{
273 int a = luaL_validfaction(L,1);
274 int b = luaL_validfaction(L,2);
275 lua_pushboolean(L, a == b);
276 return 1;
277}
278
293static int factionL_name( lua_State *L )
294{
295 int f = luaL_validfaction(L,1);
296 lua_pushstring(L, faction_shortname(f));
297 return 1;
298}
299
314static int factionL_nameRaw( lua_State *L )
315{
316 int f = luaL_validfaction(L,1);
317 lua_pushstring(L, faction_name(f));
318 return 1;
319}
320
334static int factionL_longname( lua_State *L )
335{
336 int f = luaL_validfaction(L,1);
337 lua_pushstring(L, faction_longname(f));
338 return 1;
339}
340
351static int factionL_areenemies( lua_State *L )
352{
353 int f = luaL_validfaction(L,1);
354 int ff = luaL_validfaction(L,2);
355 lua_pushboolean(L, areEnemies( f, ff ));
356 return 1;
357}
358
369static int factionL_areallies( lua_State *L )
370{
371 int f = luaL_validfaction(L,1);
372 int ff = luaL_validfaction(L,2);
373 lua_pushboolean(L, areAllies( f, ff ));
374 return 1;
375}
376
388static int factionL_modplayer( lua_State *L )
389{
390 int f = luaL_validfaction(L,1);
391 double n = luaL_checknumber(L,2);
392 faction_modPlayer( f, n, "script" );
393 return 0;
394}
395
407static int factionL_modplayersingle( lua_State *L )
408{
409 int f = luaL_validfaction(L,1);
410 double n = luaL_checknumber(L,2);
411 faction_modPlayerSingle( f, n, "script" );
412 return 0;
413}
414
427static int factionL_modplayerraw( lua_State *L )
428{
429 int f = luaL_validfaction(L,1);
430 double n = luaL_checknumber(L,2);
431 faction_modPlayerRaw( f, n );
432 return 0;
433}
434
444static int factionL_setplayerstanding( lua_State *L )
445{
446 int f = luaL_validfaction( L, 1 );
447 double n = luaL_checknumber( L, 2 );
448 faction_setPlayer( f, n );
449 return 0;
450}
451
464static int factionL_playerstanding( lua_State *L )
465{
466 int f = luaL_validfaction( L, 1 );
467 double n;
468 if (!lua_isnoneornil(L,2))
469 n = luaL_checknumber(L,2);
470 else
471 n = faction_getPlayer( f );
472 lua_pushnumber( L, n );
473 lua_pushstring( L, faction_getStandingTextAtValue( f, n ) );
474 return 2;
475}
476
484static int factionL_playerstandingdefault( lua_State *L )
485{
486 int f = luaL_validfaction( L, 1 );
487 double n = faction_getPlayerDef( f );
488 lua_pushnumber( L, n );
489 return 1;
490}
491
501static int factionL_enemies( lua_State *L )
502{
503 const int *factions;
504 int f = luaL_validfaction(L,1);
505
506 /* Push the enemies in a table. */
507 lua_newtable(L);
508 factions = faction_getEnemies( f );
509 for (int i=0; i<array_size(factions); i++) {
510 lua_pushfaction(L, factions[i]); /* value */
511 lua_rawseti(L,-2,i+1);
512 }
513
514 return 1;
515}
516
526static int factionL_allies( lua_State *L )
527{
528 const int *factions;
529 int f = luaL_validfaction(L,1);
530
531 /* Push the enemies in a table. */
532 lua_newtable(L);
533 factions = faction_getAllies( f );
534 for (int i=0; i<array_size(factions); i++) {
535 lua_pushfaction(L, factions[i]); /* value */
536 lua_rawseti(L,-2,i+1);
537 }
538
539 return 1;
540}
541
549static int factionL_usesHiddenJumps( lua_State *L )
550{
551 int f = luaL_validfaction(L,1);
552 lua_pushboolean( L, faction_usesHiddenJumps(f) );
553 return 1;
554}
555
563static int factionL_logo( lua_State *L )
564{
565 int lf = luaL_validfaction(L,1);
566 const glTexture *tex = faction_logo( lf );
567 if (tex == NULL)
568 return 0;
569 lua_pushtex( L, gl_dupTexture( tex ) );
570 return 1;
571}
572
580static int factionL_colour( lua_State *L )
581{
582 int lf = luaL_validfaction(L,1);
583 const glColour *col = faction_getColour(lf);
584 if (col == NULL)
585 return 0;
586 lua_pushcolour( L, *col );
587 return 1;
588}
589
599static int factionL_isKnown( lua_State *L )
600{
601 int fac = luaL_validfaction(L, 1);
602 lua_pushboolean(L, faction_isKnown(fac));
603 return 1;
604}
605
614static int factionL_setKnown( lua_State *L )
615{
616 int fac = luaL_validfaction(L, 1);
617 int b = lua_toboolean(L, 2);
618 faction_setKnown( fac, b );
619 return 0;
620}
621
631static int factionL_isInvisible( lua_State *L )
632{
633 int fac = luaL_validfaction(L, 1);
634 lua_pushboolean(L, faction_isInvisible(fac));
635 return 1;
636}
637
647static int factionL_isStatic( lua_State *L )
648{
649 int fac = luaL_validfaction(L, 1);
650 lua_pushboolean(L, faction_isStatic(fac));
651 return 1;
652}
653
663static int factionL_tags( lua_State *L )
664{
665 int fac = luaL_validfaction(L, 1);
666 const char **tags = faction_tags( fac );
667 lua_newtable(L);
668 for (int i=0; i<array_size(tags); i++) {
669 lua_pushstring(L, tags[i]);
670 lua_pushboolean(L,1);
671 lua_rawset(L,-3);
672 }
673 return 1;
674}
675
687static int factionL_dynAdd( lua_State *L )
688{
689 LuaFaction fac, newfac;
690 const char *name, *display, *ai;
691 const glColour *colour;
692 int clear_allies, clear_enemies;
693 double player;
694 int set_player;
695
696 if (!lua_isnoneornil(L, 1))
697 fac = luaL_validfactionSilent(L,1); /* Won't error. */
698 else
699 fac = -1;
700 name = luaL_checkstring(L,2);
701 display = luaL_optstring(L,3,name);
702 set_player = 0;
703
704 /* Just return existing and ignore the rest. */
705 if (faction_exists(name)) {
706 int f = faction_get(name);
707 if (!faction_isDynamic(f))
708 return NLUA_ERROR(L,_("Trying to overwrite existing faction '%s' with dynamic faction!"),name);
709
710 lua_pushfaction( L, f );
711 return 1;
712 }
713
714 /* Parse parameters. */
715 if (lua_istable(L,4)) {
716 lua_getfield(L,4,"ai");
717 ai = luaL_optstring(L,-1,NULL);
718 lua_pop(L,1);
719
720 lua_getfield(L,4,"clear_allies");
721 clear_allies = lua_toboolean(L,-1);
722 lua_pop(L,1);
723
724 lua_getfield(L,4,"clear_enemies");
725 clear_enemies = lua_toboolean(L,-1);
726 lua_pop(L,1);
727
728 lua_getfield(L,4,"player");
729 if (lua_isnumber(L,-1)) {
730 player = lua_tonumber(L,-1);
731 set_player = 1;
732 }
733 lua_pop(L,1);
734
735 lua_getfield(L,4,"colour");
736 if (lua_isstring(L,-1))
737 colour = col_fromName( lua_tostring(L,-1) );
738 else
739 colour = lua_tocolour( L, -1 );
740 lua_pop(L,1);
741 }
742 else {
743 ai = NULL;
744 clear_allies = 0;
745 clear_enemies = 0;
746 player = 0.;
747 colour = NULL;
748 }
749
750 /* Create new faction. */
751 newfac = faction_dynAdd( fac, name, display, ai, colour );
752
753 /* Clear if necessary. */
754 if (clear_allies)
755 faction_clearAlly( newfac );
756 if (clear_enemies)
757 faction_clearEnemy( newfac );
758 if (set_player)
759 faction_setPlayer( newfac, player );
760
761 lua_pushfaction( L, newfac );
762 return 1;
763}
764
773static int factionL_dynAlly( lua_State *L )
774{
775 LuaFaction fac, ally;
776 int remove;
777 fac = luaL_validfaction(L,1);
778 if (!faction_isDynamic(fac))
779 return NLUA_ERROR(L,_("Can only add allies to dynamic factions"));
780 ally = luaL_validfaction(L,2);
781 remove = lua_toboolean(L,3);
782 if (remove)
783 faction_rmAlly(fac, ally);
784 else
785 faction_addAlly(fac, ally);
786 return 0;
787}
788
797static int factionL_dynEnemy( lua_State *L )
798{
799 LuaFaction fac, enemy;
800 int remove;
801 fac = luaL_validfaction(L,1);
802 if (!faction_isDynamic(fac))
803 return NLUA_ERROR(L,_("Can only add allies to dynamic factions"));
804 enemy = luaL_validfaction(L,2);
805 remove = lua_toboolean(L,3);
806 if (remove)
807 faction_rmEnemy(fac, enemy);
808 else
809 faction_addEnemy(fac, enemy);
810 return 0;
811}
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
const char * faction_longname(int f)
Gets the faction's long name (formal, human-readable).
Definition faction.c:348
const glColour * faction_getColour(int f)
Gets the colour of the faction based on it's standing with the player.
Definition faction.c:1035
int faction_exists(const char *name)
Checks to see if a faction exists by name.
Definition faction.c:173
const int * faction_getEnemies(int f)
Gets the list of enemies of a faction.
Definition faction.c:485
int faction_dynAdd(int base, const char *name, const char *display, const char *ai, const glColour *colour)
Dynamically add a faction.
Definition faction.c:1915
int faction_player
Definition faction.c:47
void faction_rmAlly(int f, int o)
Removes an ally from the faction's allies list.
Definition faction.c:703
int faction_isKnown(int id)
Is the faction known?
Definition faction.c:275
const glTexture * faction_logo(int f)
Gets the faction's logo (ideally 256x256).
Definition faction.c:453
void faction_clearEnemy(int f)
Clears all the enemies of a dynamic faction.
Definition faction.c:544
void faction_rmEnemy(int f, int o)
Removes an enemy from the faction's enemies list.
Definition faction.c:610
void faction_setPlayer(int f, double value)
Sets the player's standing with a faction.
Definition faction.c:940
int areEnemies(int a, int b)
Checks whether two factions are enemies.
Definition faction.c:1227
const char * faction_name(int f)
Gets a factions "real" (internal) name.
Definition faction.c:306
const char * faction_shortname(int f)
Gets a factions short name (human-readable).
Definition faction.c:325
double faction_getPlayer(int f)
Gets the player's standing with a faction.
Definition faction.c:981
int faction_isDynamic(int id)
Is faction dynamic.
Definition faction.c:283
const int * faction_getAllies(int f)
Gets the list of allies of a faction.
Definition faction.c:515
void faction_modPlayerRaw(int f, double mod)
Modifies the player's standing without affecting others.
Definition faction.c:903
int faction_usesHiddenJumps(int f)
Checks to see if a faction uses hidden jumps.
Definition faction.c:1873
int faction_isInvisible(int id)
Is the faction invisible?
Definition faction.c:250
const char * faction_getStandingTextAtValue(int f, double value)
Gets the player's standing in human readable form.
Definition faction.c:1078
void faction_clearAlly(int f)
Clears all the ally of a dynamic faction.
Definition faction.c:637
void faction_modPlayer(int f, double mod, const char *source)
Modifies the player's standing with a faction.
Definition faction.c:843
const char ** faction_tags(int f)
Gets the tags the faction has.
Definition faction.c:414
double faction_getPlayerDef(int f)
Gets the player's default standing with a faction.
Definition faction.c:995
int * faction_getAll(void)
Returns all faction IDs in an array (array.h).
Definition faction.c:195
void faction_modPlayerSingle(int f, double mod, const char *source)
Modifies the player's standing without affecting others.
Definition faction.c:883
int faction_setKnown(int id, int state)
Sets the factions known state.
Definition faction.c:291
void faction_addAlly(int f, int o)
Adds an ally to the faction's allies list.
Definition faction.c:657
int faction_isStatic(int id)
Is the faction static?
Definition faction.c:242
void faction_addEnemy(int f, int o)
Adds an enemy to the faction's enemies list.
Definition faction.c:564
int faction_get(const char *name)
Gets a faction ID by name.
Definition faction.c:184
int areAllies(int a, int b)
Checks whether two factions are allies or not.
Definition faction.c:1253
Header file with generic functions and naev-specifics.
glColour * lua_tocolour(lua_State *L, int ind)
Lua bindings to interact with colours.
Definition nlua_colour.c:80
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_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_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_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.
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_usesHiddenJumps(lua_State *L)
Gets whether or not a faction uses hidden jumps.
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.
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_isInvisible(lua_State *L)
Checks to see if a faction is invisible the player.
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.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
Definition nlua_tex.c:130
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
Definition opengl_tex.c:917
Player_t player
Definition player.c:74
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:36