naev 0.10.4
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_eq( lua_State *L );
32static int factionL_name( lua_State *L );
33static int factionL_nameRaw( lua_State *L );
34static int factionL_longname( lua_State *L );
35static int factionL_areenemies( lua_State *L );
36static int factionL_areallies( lua_State *L );
37static int factionL_usesHiddenJumps( lua_State *L );
38static int factionL_modplayer( lua_State *L );
39static int factionL_modplayersingle( lua_State *L );
40static int factionL_modplayerraw( lua_State *L );
41static int factionL_setplayerstanding( lua_State *L );
42static int factionL_playerstanding( lua_State *L );
43static int factionL_playerstandingdefault( lua_State *L );
44static int factionL_enemies( lua_State *L );
45static int factionL_allies( lua_State *L );
46static int factionL_logo( lua_State *L );
47static int factionL_colour( lua_State *L );
48static int factionL_isknown( lua_State *L );
49static int factionL_setKnown( lua_State *L );
50static int factionL_isInvisible( lua_State *L );
51static int factionL_isStatic( lua_State *L );
52static int factionL_tags( lua_State *L );
53static int factionL_dynAdd( lua_State *L );
54static int factionL_dynAlly( lua_State *L );
55static int factionL_dynEnemy( lua_State *L );
56static const luaL_Reg faction_methods[] = {
57 { "exists", factionL_exists },
58 { "get", factionL_get },
59 { "__eq", factionL_eq },
60 { "__tostring", factionL_name },
61 { "name", factionL_name },
62 { "nameRaw", factionL_nameRaw },
63 { "longname", factionL_longname },
64 { "areEnemies", factionL_areenemies },
65 { "areAllies", factionL_areallies },
66 { "modPlayer", factionL_modplayer },
67 { "modPlayerSingle", factionL_modplayersingle },
68 { "modPlayerRaw", factionL_modplayerraw },
69 { "setPlayerStanding", factionL_setplayerstanding },
70 { "playerStanding", factionL_playerstanding },
71 { "playerStandingDefault", factionL_playerstandingdefault },
72 { "enemies", factionL_enemies },
73 { "allies", factionL_allies },
74 { "usesHiddenJumps", factionL_usesHiddenJumps },
75 { "logo", factionL_logo },
76 { "colour", factionL_colour },
77 { "known", factionL_isknown },
78 { "setKnown", factionL_setKnown },
79 { "invisible", factionL_isInvisible },
80 { "static", factionL_isStatic },
81 { "tags", factionL_tags },
82 { "dynAdd", factionL_dynAdd },
83 { "dynAlly", factionL_dynAlly },
84 { "dynEnemy", factionL_dynEnemy },
85 {0,0}
86};
94int nlua_loadFaction( nlua_env env )
95{
96 nlua_register(env, FACTION_METATABLE, faction_methods, 1);
97 return 0; /* No error */
98}
99
122static int factionL_exists( lua_State *L )
123{
124 const char *s = luaL_checkstring(L,1);
125 if (faction_exists(s)) {
127 return 1;
128 }
129 return 0;
130}
131
141static int factionL_get( lua_State *L )
142{
143 LuaFaction f = luaL_validfaction(L,1);
144 lua_pushfaction(L,f);
145 return 1;
146}
147
155LuaFaction lua_tofaction( lua_State *L, int ind )
156{
157 return *((LuaFaction*) lua_touserdata(L,ind));
158}
159
160static LuaFaction luaL_validfactionSilent( lua_State *L, int ind )
161{
162 if (lua_isfaction(L,ind))
163 return lua_tofaction(L,ind);
164 else if (lua_isstring(L,ind))
165 return faction_get( lua_tostring(L, ind) );
166 luaL_typerror(L, ind, FACTION_METATABLE);
167 return 0;
168}
169
177LuaFaction luaL_validfaction( lua_State *L, int ind )
178{
179 int id = luaL_validfactionSilent( L, ind );
180 if (id == -1)
181 NLUA_ERROR(L,_("Faction '%s' not found in stack."), lua_tostring(L,ind) );
182 return id;
183}
184
192LuaFaction* lua_pushfaction( lua_State *L, LuaFaction faction )
193{
194 LuaFaction *f;
195 f = (LuaFaction*) lua_newuserdata(L, sizeof(LuaFaction));
196 *f = faction;
197 luaL_getmetatable(L, FACTION_METATABLE);
198 lua_setmetatable(L, -2);
199 return f;
200}
208int lua_isfaction( lua_State *L, int ind )
209{
210 int ret;
211
212 if (lua_getmetatable(L,ind)==0)
213 return 0;
214 lua_getfield(L, LUA_REGISTRYINDEX, FACTION_METATABLE);
215
216 ret = 0;
217 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
218 ret = 1;
219
220 lua_pop(L, 2); /* remove both metatables */
221 return ret;
222}
223
236static int factionL_eq( lua_State *L )
237{
238 int a = luaL_validfaction(L,1);
239 int b = luaL_validfaction(L,2);
240 lua_pushboolean(L, a == b);
241 return 1;
242}
243
258static int factionL_name( lua_State *L )
259{
260 int f = luaL_validfaction(L,1);
261 lua_pushstring(L, faction_shortname(f));
262 return 1;
263}
264
279static int factionL_nameRaw( lua_State *L )
280{
281 int f = luaL_validfaction(L,1);
282 lua_pushstring(L, faction_name(f));
283 return 1;
284}
285
299static int factionL_longname( lua_State *L )
300{
301 int f = luaL_validfaction(L,1);
302 lua_pushstring(L, faction_longname(f));
303 return 1;
304}
305
316static int factionL_areenemies( lua_State *L )
317{
318 int f = luaL_validfaction(L,1);
319 int ff = luaL_validfaction(L,2);
320 lua_pushboolean(L, areEnemies( f, ff ));
321 return 1;
322}
323
334static int factionL_areallies( lua_State *L )
335{
336 int f = luaL_validfaction(L,1);
337 int ff = luaL_validfaction(L,2);
338 lua_pushboolean(L, areAllies( f, ff ));
339 return 1;
340}
341
353static int factionL_modplayer( lua_State *L )
354{
355 int f = luaL_validfaction(L,1);
356 double n = luaL_checknumber(L,2);
357 faction_modPlayer( f, n, "script" );
358 return 0;
359}
360
372static int factionL_modplayersingle( lua_State *L )
373{
374 int f = luaL_validfaction(L,1);
375 double n = luaL_checknumber(L,2);
376 faction_modPlayerSingle( f, n, "script" );
377 return 0;
378}
379
392static int factionL_modplayerraw( lua_State *L )
393{
394 int f = luaL_validfaction(L,1);
395 double n = luaL_checknumber(L,2);
396 faction_modPlayerRaw( f, n );
397 return 0;
398}
399
409static int factionL_setplayerstanding( lua_State *L )
410{
411 int f = luaL_validfaction( L, 1 );
412 double n = luaL_checknumber( L, 2 );
413 faction_setPlayer( f, n );
414 return 0;
415}
416
427static int factionL_playerstanding( lua_State *L )
428{
429 int f = luaL_validfaction( L, 1 );
430 double n = faction_getPlayer( f );
431 lua_pushnumber( L, n );
432 lua_pushstring( L, faction_getStandingText( f ) );
433 return 2;
434}
435
443static int factionL_playerstandingdefault( lua_State *L )
444{
445 int f = luaL_validfaction( L, 1 );
446 double n = faction_getPlayerDef( f );
447 lua_pushnumber( L, n );
448 return 1;
449}
450
460static int factionL_enemies( lua_State *L )
461{
462 const int *factions;
463 int f = luaL_validfaction(L,1);
464
465 /* Push the enemies in a table. */
466 lua_newtable(L);
467 factions = faction_getEnemies( f );
468 for (int i=0; i<array_size(factions); i++) {
469 lua_pushfaction(L, factions[i]); /* value */
470 lua_rawseti(L,-2,i+1);
471 }
472
473 return 1;
474}
475
485static int factionL_allies( lua_State *L )
486{
487 const int *factions;
488 int f = luaL_validfaction(L,1);
489
490 /* Push the enemies in a table. */
491 lua_newtable(L);
492 factions = faction_getAllies( f );
493 for (int i=0; i<array_size(factions); i++) {
494 lua_pushfaction(L, factions[i]); /* value */
495 lua_rawseti(L,-2,i+1);
496 }
497
498 return 1;
499}
500
508static int factionL_usesHiddenJumps( lua_State *L )
509{
510 int f = luaL_validfaction(L,1);
511 lua_pushboolean( L, faction_usesHiddenJumps(f) );
512 return 1;
513}
514
522static int factionL_logo( lua_State *L )
523{
524 int lf = luaL_validfaction(L,1);
525 const glTexture *tex = faction_logo( lf );
526 if (tex == NULL)
527 return 0;
528 lua_pushtex( L, gl_dupTexture( tex ) );
529 return 1;
530}
531
539static int factionL_colour( lua_State *L )
540{
541 int lf = luaL_validfaction(L,1);
542 const glColour *col = faction_getColour(lf);
543 if (col == NULL)
544 return 0;
545 lua_pushcolour( L, *col );
546 return 1;
547}
548
558static int factionL_isknown( lua_State *L )
559{
560 int fac = luaL_validfaction(L, 1);
561 lua_pushboolean(L, faction_isKnown(fac));
562 return 1;
563}
564
573static int factionL_setKnown( lua_State *L )
574{
575 int fac = luaL_validfaction(L, 1);
576 int b = lua_toboolean(L, 2);
577 faction_setKnown( fac, b );
578 return 0;
579}
580
590static int factionL_isInvisible( lua_State *L )
591{
592 int fac = luaL_validfaction(L, 1);
593 lua_pushboolean(L, faction_isInvisible(fac));
594 return 1;
595}
596
606static int factionL_isStatic( lua_State *L )
607{
608 int fac = luaL_validfaction(L, 1);
609 lua_pushboolean(L, faction_isStatic(fac));
610 return 1;
611}
612
622static int factionL_tags( lua_State *L )
623{
624 int fac = luaL_validfaction(L, 1);
625 const char **tags = faction_tags( fac );
626 lua_newtable(L);
627 for (int i=0; i<array_size(tags); i++) {
628 lua_pushstring(L, tags[i]);
629 lua_pushboolean(L,1);
630 lua_rawset(L,-3);
631 }
632 return 1;
633}
634
646static int factionL_dynAdd( lua_State *L )
647{
648 LuaFaction fac, newfac;
649 const char *name, *display, *ai;
650 const glColour *colour;
651 int clear_allies, clear_enemies;
652 double player;
653 int set_player;
654
655 if (!lua_isnoneornil(L, 1))
656 fac = luaL_validfactionSilent(L,1); /* Won't error. */
657 else
658 fac = -1;
659 name = luaL_checkstring(L,2);
660 display = luaL_optstring(L,3,name);
661 set_player = 0;
662
663 /* Just return existing and ignore the rest. */
664 if (faction_exists(name)) {
665 int f = faction_get(name);
666 if (!faction_isDynamic(f))
667 NLUA_ERROR(L,_("Trying to overwrite existing faction '%s' with dynamic faction!"),name);
668
669 lua_pushfaction( L, f );
670 return 1;
671 }
672
673 /* Parse parameters. */
674 if (lua_istable(L,4)) {
675 lua_getfield(L,4,"ai");
676 ai = luaL_optstring(L,-1,NULL);
677 lua_pop(L,1);
678
679 lua_getfield(L,4,"clear_allies");
680 clear_allies = lua_toboolean(L,-1);
681 lua_pop(L,1);
682
683 lua_getfield(L,4,"clear_enemies");
684 clear_enemies = lua_toboolean(L,-1);
685 lua_pop(L,1);
686
687 lua_getfield(L,4,"player");
688 if (lua_isnumber(L,-1)) {
689 player = lua_tonumber(L,-1);
690 set_player = 1;
691 }
692 lua_pop(L,1);
693
694 lua_getfield(L,4,"colour");
695 if (lua_isstring(L,-1))
696 colour = col_fromName( lua_tostring(L,-1) );
697 else
698 colour = lua_tocolour( L, -1 );
699 lua_pop(L,1);
700 }
701 else {
702 ai = NULL;
703 clear_allies = 0;
704 clear_enemies = 0;
705 player = 0.;
706 colour = NULL;
707 }
708
709 /* Create new faction. */
710 newfac = faction_dynAdd( fac, name, display, ai, colour );
711
712 /* Clear if necessary. */
713 if (clear_allies)
714 faction_clearAlly( newfac );
715 if (clear_enemies)
716 faction_clearEnemy( newfac );
717 if (set_player)
718 faction_setPlayer( newfac, player );
719
720 lua_pushfaction( L, newfac );
721 return 1;
722}
723
732static int factionL_dynAlly( lua_State *L )
733{
734 LuaFaction fac, ally;
735 int remove;
736 fac = luaL_validfaction(L,1);
737 if (!faction_isDynamic(fac))
738 NLUA_ERROR(L,_("Can only add allies to dynamic factions"));
739 ally = luaL_validfaction(L,2);
740 remove = lua_toboolean(L,3);
741 if (remove)
742 faction_rmAlly(fac, ally);
743 else
744 faction_addAlly(fac, ally);
745 return 0;
746}
747
756static int factionL_dynEnemy( lua_State *L )
757{
758 LuaFaction fac, enemy;
759 int remove;
760 fac = luaL_validfaction(L,1);
761 if (!faction_isDynamic(fac))
762 NLUA_ERROR(L,_("Can only add allies to dynamic factions"));
763 enemy = luaL_validfaction(L,2);
764 remove = lua_toboolean(L,3);
765 if (remove)
766 faction_rmEnemy(fac, enemy);
767 else
768 faction_addEnemy(fac, enemy);
769 return 0;
770}
Provides macros to work with dynamic arrays.
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:346
const glColour * faction_getColour(int f)
Gets the colour of the faction based on it's standing with the player.
Definition: faction.c:1023
int faction_exists(const char *name)
Checks to see if a faction exists by name.
Definition: faction.c:171
const int * faction_getEnemies(int f)
Gets the list of enemies of a faction.
Definition: faction.c:483
int faction_dynAdd(int base, const char *name, const char *display, const char *ai, const glColour *colour)
Dynamically add a faction.
Definition: faction.c:1880
void faction_rmAlly(int f, int o)
Removes an ally from the faction's allies list.
Definition: faction.c:701
int faction_isKnown(int id)
Is the faction known?
Definition: faction.c:273
const glTexture * faction_logo(int f)
Gets the faction's logo (ideally 256x256).
Definition: faction.c:451
void faction_clearEnemy(int f)
Clears all the enemies of a dynamic faction.
Definition: faction.c:542
void faction_rmEnemy(int f, int o)
Removes an enemy from the faction's enemies list.
Definition: faction.c:608
void faction_setPlayer(int f, double value)
Sets the player's standing with a faction.
Definition: faction.c:932
int areEnemies(int a, int b)
Checks whether two factions are enemies.
Definition: faction.c:1197
const char * faction_name(int f)
Gets a factions "real" (internal) name.
Definition: faction.c:304
const char * faction_shortname(int f)
Gets a factions short name (human-readable).
Definition: faction.c:323
const char * faction_getStandingText(int f)
Gets the player's standing in human readable form.
Definition: faction.c:1054
double faction_getPlayer(int f)
Gets the player's standing with a faction.
Definition: faction.c:969
int faction_isDynamic(int id)
Is faction dynamic.
Definition: faction.c:281
const int * faction_getAllies(int f)
Gets the list of allies of a faction.
Definition: faction.c:513
void faction_modPlayerRaw(int f, double mod)
Modifies the player's standing without affecting others.
Definition: faction.c:899
int faction_usesHiddenJumps(int f)
Checks to see if a faction uses hidden jumps.
Definition: faction.c:1838
int faction_isInvisible(int id)
Is the faction invisible?
Definition: faction.c:248
void faction_clearAlly(int f)
Clears all the ally of a dynamic faction.
Definition: faction.c:635
void faction_modPlayer(int f, double mod, const char *source)
Modifies the player's standing with a faction.
Definition: faction.c:839
const char ** faction_tags(int f)
Gets the tags the faction has.
Definition: faction.c:412
double faction_getPlayerDef(int f)
Gets the player's default standing with a faction.
Definition: faction.c:983
void faction_modPlayerSingle(int f, double mod, const char *source)
Modifies the player's standing without affecting others.
Definition: faction.c:879
int faction_setKnown(int id, int state)
Sets the factions known state.
Definition: faction.c:289
void faction_addAlly(int f, int o)
Adds an ally to the faction's allies list.
Definition: faction.c:655
int faction_isStatic(int id)
Is the faction static?
Definition: faction.c:240
void faction_addEnemy(int f, int o)
Adds an enemy to the faction's enemies list.
Definition: faction.c:562
int faction_get(const char *name)
Gets a faction ID by name.
Definition: faction.c:182
int areAllies(int a, int b)
Checks whether two factions are allies or not.
Definition: faction.c:1222
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:78
glColour * lua_pushcolour(lua_State *L, glColour colour)
Pushes a colour on the stack.
Definition: nlua_colour.c:103
static int factionL_setplayerstanding(lua_State *L)
Sets the player's standing with the faction.
Definition: nlua_faction.c:409
static int factionL_modplayerraw(lua_State *L)
Modifies the player's standing with the faction.
Definition: nlua_faction.c:392
static int factionL_longname(lua_State *L)
Gets the faction's translated long name.
Definition: nlua_faction.c:299
static int factionL_tags(lua_State *L)
Gets the tags a faction has.
Definition: nlua_faction.c:622
static int factionL_playerstandingdefault(lua_State *L)
Gets the player's default standing with the faction.
Definition: nlua_faction.c:443
static int factionL_dynAlly(lua_State *L)
Adds or removes allies to a faction. Only works with dynamic factions.
Definition: nlua_faction.c:732
static int factionL_exists(lua_State *L)
Lua bindings to deal with factions.
Definition: nlua_faction.c:122
static int factionL_colour(lua_State *L)
Gets the faction colour.
Definition: nlua_faction.c:539
static int factionL_allies(lua_State *L)
Gets the allies of the faction.
Definition: nlua_faction.c:485
static int factionL_areenemies(lua_State *L)
Checks to see if f is an enemy of e.
Definition: nlua_faction.c:316
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
Definition: nlua_faction.c:192
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.
Definition: nlua_faction.c:177
static int factionL_logo(lua_State *L)
Gets the faction logo.
Definition: nlua_faction.c:522
static int factionL_dynEnemy(lua_State *L)
Adds or removes enemies to a faction. Only works with dynamic factions.
Definition: nlua_faction.c:756
static int factionL_playerstanding(lua_State *L)
Gets the player's standing with the faction.
Definition: nlua_faction.c:427
static int factionL_areallies(lua_State *L)
Checks to see if f is an ally of a.
Definition: nlua_faction.c:334
int nlua_loadFaction(nlua_env env)
Loads the faction library.
Definition: nlua_faction.c:94
static int factionL_setKnown(lua_State *L)
Sets a faction's known state.
Definition: nlua_faction.c:573
static int factionL_usesHiddenJumps(lua_State *L)
Gets whether or not a faction uses hidden jumps.
Definition: nlua_faction.c:508
static int factionL_dynAdd(lua_State *L)
Adds a faction dynamically. Note that if the faction already exists as a dynamic faction,...
Definition: nlua_faction.c:646
static int factionL_nameRaw(lua_State *L)
Gets the faction's raw / "real" (untranslated, internal) name.
Definition: nlua_faction.c:279
static int factionL_name(lua_State *L)
Gets the faction's translated short name.
Definition: nlua_faction.c:258
static int factionL_enemies(lua_State *L)
Gets the enemies of the faction.
Definition: nlua_faction.c:460
static int factionL_modplayersingle(lua_State *L)
Modifies the player's standing with the faction.
Definition: nlua_faction.c:372
int lua_isfaction(lua_State *L, int ind)
Checks to see if ind is a faction.
Definition: nlua_faction.c:208
static const luaL_Reg faction_methods[]
Definition: nlua_faction.c:56
LuaFaction lua_tofaction(lua_State *L, int ind)
Gets faction at index.
Definition: nlua_faction.c:155
static int factionL_isInvisible(lua_State *L)
Checks to see if a faction is invisible the player.
Definition: nlua_faction.c:590
static int factionL_isStatic(lua_State *L)
Checks to see if a faction has a static standing with the player.
Definition: nlua_faction.c:606
static int factionL_isknown(lua_State *L)
Checks to see if a faction is known by the player.
Definition: nlua_faction.c:558
static int factionL_eq(lua_State *L)
__eq (equality) metamethod for factions.
Definition: nlua_faction.c:236
static int factionL_modplayer(lua_State *L)
Modifies the player's standing with the faction.
Definition: nlua_faction.c:353
static int factionL_get(lua_State *L)
Gets the faction based on its name.
Definition: nlua_faction.c:141
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:809
Player_t player
Definition: player.c:73
Abstraction for rendering sprite sheets.
Definition: opengl_tex.h:34