naev 0.10.4
nlua_player.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11#include <lua.h>
12#include <math.h>
13#include <stdio.h>
14#include <stdlib.h>
15
16#include "naev.h"
19#include "nlua_player.h"
20
21#include "array.h"
22#include "board.h"
23#include "comm.h"
24#include "event.h"
25#include "gui.h"
26#include "gui_omsg.h"
27#include "hook.h"
28#include "info.h"
29#include "land.h"
30#include "land_outfits.h"
31#include "log.h"
32#include "map.h"
33#include "map_overlay.h"
34#include "mission.h"
35#include "ndata.h"
36#include "nlua_colour.h"
37#include "nlua_commodity.h"
38#include "nlua_outfit.h"
39#include "nlua_pilot.h"
40#include "nlua_spob.h"
41#include "nlua_ship.h"
42#include "nlua_system.h"
43#include "nlua_time.h"
44#include "nlua_vec2.h"
45#include "nluadef.h"
46#include "nstring.h"
47#include "pause.h"
48#include "player.h"
49#include "player_fleet.h"
50#include "player_inventory.h"
51#include "save.h"
52
53#define PLAYER_CHECK() if (player.p == NULL) return 0
54
55/* Player methods. */
56static int playerL_getname( lua_State *L );
57static int playerL_shipname( lua_State *L );
58static int playerL_pay( lua_State *L );
59static int playerL_credits( lua_State *L );
60static int playerL_wealth( lua_State *L );
61static int playerL_msg( lua_State *L );
62static int playerL_msgClear( lua_State *L );
63static int playerL_msgToggle( lua_State *L );
64static int playerL_omsgAdd( lua_State *L );
65static int playerL_omsgChange( lua_State *L );
66static int playerL_omsgRm( lua_State *L );
67static int playerL_allowSave( lua_State *L );
68/* Faction stuff. */
69static int playerL_getPosition( lua_State *L );
70static int playerL_getPilot( lua_State *L );
71/* Fuel stuff. */
72static int playerL_jumps( lua_State *L );
73static int playerL_fuel( lua_State *L );
74static int playerL_refuel( lua_State *L );
75static int playerL_autonav( lua_State *L );
76static int playerL_autonavDest( lua_State *L );
77static int playerL_autonavAbort( lua_State *L );
78static int playerL_autonavReset( lua_State *L );
79/* Cinematics. */
80static int playerL_setSpeed( lua_State *L );
81static int playerL_cinematics( lua_State *L );
82static int playerL_damageSPFX( lua_State *L );
83static int playerL_screenshot( lua_State *L );
84/* Board stuff. */
85static int playerL_unboard( lua_State *L );
86/* Land stuff. */
87static int playerL_isLanded( lua_State *L );
88static int playerL_takeoff( lua_State *L );
89static int playerL_land( lua_State *L );
90static int playerL_allowLand( lua_State *L );
91static int playerL_landWindow( lua_State *L );
92/* Hail stuff. */
93static int playerL_commclose( lua_State *L );
94/* Shipvars. */
95static int playerL_shipvarPeek( lua_State *L );
96static int playerL_shipvarPush( lua_State *L );
97static int playerL_shipvarPop( lua_State *L );
98/* Outfit and ship management stuff. */
99static int playerL_ships( lua_State *L );
100static int playerL_shipOutfits( lua_State *L );
101static int playerL_shipMetadata( lua_State *L );
102static int playerL_shipDeploy( lua_State *L );
103static int playerL_outfits( lua_State *L );
104static int playerL_numOutfit( lua_State *L );
105static int playerL_outfitAdd( lua_State *L );
106static int playerL_outfitRm( lua_State *L );
107static int playerL_shipAdd( lua_State *L );
108static int playerL_shipSwap( lua_State *L );
109/* Mission/event management stuff. */
110static int playerL_missions( lua_State *L );
111static int playerL_misnActive( lua_State *L );
112static int playerL_misnDone( lua_State *L );
113static int playerL_misnDoneList( lua_State *L );
114static int playerL_evtActive( lua_State *L );
115static int playerL_evtDone( lua_State *L );
116/* Fleet stuff. */
117static int playerL_fleetList( lua_State *L );
118static int playerL_fleetCargoFree( lua_State *L );
119static int playerL_fleetCargoUsed( lua_State *L );
120static int playerL_fleetCargoOwned( lua_State *L );
121static int playerL_fleetCargoAdd( lua_State *L );
122static int playerL_fleetCargoRm( lua_State *L );
123static int playerL_fleetCargoJet( lua_State *L );
124static int playerL_fleetCargoList( lua_State *L );
125/* Inventory. */
126static int playerL_inventory( lua_State *L );
127static int playerL_inventoryAdd( lua_State *L );
128static int playerL_inventoryRm( lua_State *L );
129static int playerL_inventoryOwned( lua_State *L );
130/* Misc stuff. */
131static int playerL_teleport( lua_State *L );
132static int playerL_dt_mod( lua_State *L );
133static int playerL_fleetCapacity( lua_State *L );
134static int playerL_fleetCapacitySet( lua_State *L );
135static int playerL_chapter( lua_State *L );
136static int playerL_chapterSet( lua_State *L );
137static int playerL_infoButtonRegister( lua_State *L );
138static int playerL_infoButtonUnregister( lua_State *L );
139static int playerL_canDiscover( lua_State *L );
140static int playerL_save( lua_State *L );
141static int playerL_saveBackup( lua_State *L );
142static const luaL_Reg playerL_methods[] = {
143 { "name", playerL_getname },
144 { "ship", playerL_shipname },
145 { "pay", playerL_pay },
146 { "credits", playerL_credits },
147 { "wealth", playerL_wealth },
148 { "msg", playerL_msg },
149 { "msgClear", playerL_msgClear },
150 { "msgToggle", playerL_msgToggle },
151 { "omsgAdd", playerL_omsgAdd },
152 { "omsgChange", playerL_omsgChange },
153 { "omsgRm", playerL_omsgRm },
154 { "allowSave", playerL_allowSave },
155 { "pos", playerL_getPosition },
156 { "pilot", playerL_getPilot },
157 { "jumps", playerL_jumps },
158 { "fuel", playerL_fuel },
159 { "refuel", playerL_refuel },
160 { "autonav", playerL_autonav },
161 { "autonavDest", playerL_autonavDest },
162 { "autonavAbort", playerL_autonavAbort },
163 { "autonavReset", playerL_autonavReset },
164 { "setSpeed", playerL_setSpeed },
165 { "cinematics", playerL_cinematics },
166 { "damageSPFX", playerL_damageSPFX },
167 { "screenshot", playerL_screenshot },
168 { "unboard", playerL_unboard },
169 { "isLanded", playerL_isLanded },
170 { "takeoff", playerL_takeoff },
171 { "land", playerL_land },
172 { "allowLand", playerL_allowLand },
173 { "landWindow", playerL_landWindow },
174 { "commClose", playerL_commclose },
175 { "shipvarPeek", playerL_shipvarPeek },
176 { "shipvarPush", playerL_shipvarPush },
177 { "shipvarPop", playerL_shipvarPop },
178 { "ships", playerL_ships },
179 { "shipOutfits", playerL_shipOutfits },
180 { "shipMetadata", playerL_shipMetadata },
181 { "shipDeploy", playerL_shipDeploy },
182 { "outfits", playerL_outfits },
183 { "numOutfit", playerL_numOutfit },
184 { "outfitAdd", playerL_outfitAdd },
185 { "outfitRm", playerL_outfitRm },
186 { "shipAdd", playerL_shipAdd },
187 { "shipSwap", playerL_shipSwap },
188 { "missions", playerL_missions },
189 { "misnActive", playerL_misnActive },
190 { "misnDone", playerL_misnDone },
191 { "misnDoneList", playerL_misnDoneList },
192 { "evtActive", playerL_evtActive },
193 { "evtDone", playerL_evtDone },
194 { "fleetList", playerL_fleetList },
195 { "fleetCargoFree", playerL_fleetCargoFree },
196 { "fleetCargoUsed", playerL_fleetCargoUsed },
197 { "fleetCargoOwned", playerL_fleetCargoOwned },
198 { "fleetCargoAdd", playerL_fleetCargoAdd },
199 { "fleetCargoRm", playerL_fleetCargoRm },
200 { "fleetCargoJet", playerL_fleetCargoJet },
201 { "fleetCargoList", playerL_fleetCargoList },
202 { "inventory", playerL_inventory },
203 { "inventoryAdd", playerL_inventoryAdd },
204 { "inventoryRm", playerL_inventoryRm },
205 { "inventoryOwned", playerL_inventoryOwned },
206 { "teleport", playerL_teleport },
207 { "dt_mod", playerL_dt_mod },
208 { "fleetCapacity", playerL_fleetCapacity },
209 { "fleetCapacitySet", playerL_fleetCapacitySet },
210 { "chapter", playerL_chapter },
211 { "chapterSet", playerL_chapterSet },
212 { "infoButtonRegister", playerL_infoButtonRegister },
213 { "infoButtonUnregister", playerL_infoButtonUnregister },
214 { "canDiscover", playerL_canDiscover },
215 { "save", playerL_save },
216 { "saveBackup", playerL_saveBackup },
217 {0,0}
218};
224int nlua_loadPlayer( nlua_env env )
225{
226 nlua_register(env, "player", playerL_methods, 0);
227 return 0;
228}
229
247static int playerL_getname( lua_State *L )
248{
249 PLAYER_CHECK();
250 lua_pushstring(L,player.name);
251 return 1;
252}
261static int playerL_shipname( lua_State *L )
262{
263 PLAYER_CHECK();
264 lua_pushstring(L,player.p->name);
265 return 1;
266}
276static int playerL_pay( lua_State *L )
277{
278 HookParam p[3];
279 credits_t money;
280 int nohooks;
281 const char *reason;
282
283 PLAYER_CHECK();
284
285 money = CLAMP( CREDITS_MIN, CREDITS_MAX, (credits_t)round(luaL_checknumber(L,1)) );
286 player_modCredits( money );
287 if (lua_isstring(L,2)) {
288 nohooks = 0;
289 reason = lua_tostring(L,2);
290 }
291 else {
292 nohooks = lua_toboolean(L,2);
293 reason = NULL;
294 }
295
296 if (!nohooks) {
297 p[0].type = HOOK_PARAM_NUMBER;
298 p[0].u.num = (double)money;
299 if (reason != NULL) {
300 p[1].type = HOOK_PARAM_STRING;
301 p[1].u.str = reason;
302 }
303 else {
304 p[1].type = HOOK_PARAM_NIL;
305 }
306 p[2].type = HOOK_PARAM_SENTINEL;
307 hooks_runParam( "pay", p );
308 }
309
310 return 0;
311}
312
324static int playerL_credits( lua_State *L )
325{
326 PLAYER_CHECK();
327 /* Parse parameters. */
328 int decimals = luaL_optinteger(L,1,-1);
329
330 /* Push return. */
331 lua_pushnumber(L, player.p->credits);
332 if (decimals >= 0) {
333 char buf[ ECON_CRED_STRLEN ];
334 credits2str( buf, player.p->credits, decimals );
335 lua_pushstring(L, buf);
336 return 2;
337 }
338 return 1;
339}
340
352static int playerL_wealth( lua_State *L )
353{
354 PLAYER_CHECK();
355 const PlayerShip_t *ps = player_getShipStack();
356 const PlayerOutfit_t *po = player_getOutfits();
357 credits_t wealth = player.p->credits + pilot_worth( player.p );
358 /* Parse parameters. */
359 int decimals = luaL_optinteger(L,1,-1);
360
361 /* Compute total wealth. */
362 for (int i=0; i<array_size(ps); i++)
363 wealth += pilot_worth( ps[i].p );
364 for (int i=0; i<array_size(po); i++)
365 wealth += po[i].q * po[i].o->price;
366
367 /* Push return. */
368 lua_pushnumber(L, wealth);
369 if (decimals >= 0) {
370 char buf[ ECON_CRED_STRLEN ];
371 credits2str( buf, wealth, decimals );
372 lua_pushstring(L, buf);
373 return 2;
374 }
375 return 1;
376}
377
385static int playerL_msg( lua_State *L )
386{
387 PLAYER_CHECK();
388
389 const char *str = luaL_checkstring(L,1);
390 int display = lua_toboolean(L,2);
392 if (display)
393 pilot_setCommMsg( player.p, str );
394
395 return 0;
396}
402static int playerL_msgClear( lua_State *L )
403{
404 (void) L;
405 PLAYER_CHECK();
407 return 0;
408}
415static int playerL_msgToggle( lua_State *L )
416{
417 PLAYER_CHECK();
418 player_messageToggle( lua_toboolean(L,1) );
419 return 0;
420}
432static int playerL_omsgAdd( lua_State *L )
433{
434 const char *str;
435 double duration;
436 unsigned int id;
437 int fontsize;
438 const glColour *col;
439
440 PLAYER_CHECK();
441
442 /* Input. */
443 str = luaL_checkstring(L,1);
444 duration = luaL_optnumber(L,2,10.);
445 fontsize = luaL_optinteger(L,3,OMSG_FONT_DEFAULT_SIZE);
446 col = luaL_optcolour(L,4,&cWhite);
447
448 /* Infinity. */
449 if (duration < 1e-10)
450 duration = INFINITY;
451
452 /* Output. */
453 id = omsg_add( str, duration, fontsize, col );
454 lua_pushnumber( L, id );
455 return 1;
456}
467static int playerL_omsgChange( lua_State *L )
468{
469 const char *str;
470 double duration;
471 unsigned int id;
472 int ret;
473
474 PLAYER_CHECK();
475
476 /* Input. */
477 id = luaL_checklong(L,1);
478 str = luaL_checkstring(L,2);
479 duration = luaL_checknumber(L,3);
480
481 /* Infinity. */
482 if (duration < 1e-10)
483 duration = INFINITY;
484
485 /* Output. */
486 ret = omsg_change( id, str, duration );
487 lua_pushboolean(L,!ret);
488 return 1;
489}
497static int playerL_omsgRm( lua_State *L )
498{
499 PLAYER_CHECK();
500 unsigned int id = luaL_checklong(L,1);
501 omsg_rm( id );
502 return 0;
503}
511static int playerL_allowSave( lua_State *L )
512{
513 unsigned int b;
514 PLAYER_CHECK();
515 if (lua_gettop(L)==0)
516 b = 1;
517 else
518 b = lua_toboolean(L, 1);
519
520 if (b)
521 player_rmFlag(PLAYER_NOSAVE);
522 else
523 player_setFlag(PLAYER_NOSAVE);
524 return 0;
525}
526
535static int playerL_getPosition( lua_State *L )
536{
537 PLAYER_CHECK();
539 return 1;
540}
541
548static int playerL_getPilot( lua_State *L )
549{
550 /* No need to run check here or stuff that depends on player.pilot() working will fail. */
551 lua_pushpilot(L, PLAYER_ID);
552 return 1;
553}
554
563static int playerL_jumps( lua_State *L )
564{
565 PLAYER_CHECK();
566 lua_pushnumber(L, pilot_getJumps(player.p));
567 return 1;
568}
569
579static int playerL_fuel( lua_State *L )
580{
581 PLAYER_CHECK();
582 lua_pushnumber(L,player.p->fuel);
583 lua_pushnumber(L,player.p->fuel_consumption);
584 return 2;
585}
586
596static int playerL_refuel( lua_State *L )
597{
598 PLAYER_CHECK();
599
600 if (lua_gettop(L) > 0) {
601 double f = luaL_checknumber(L,1);
602 player.p->fuel += f;
603 }
604 else
606
607 /* Make sure value is valid. */
609
610 return 0;
611}
612
620static int playerL_autonav( lua_State *L )
621{
622 PLAYER_CHECK();
623 lua_pushboolean( L, player_isFlag( PLAYER_AUTONAV ) );
624 return 1;
625}
626
636static int playerL_autonavDest( lua_State *L )
637{
638 LuaSystem ls;
639 StarSystem *dest;
640 int jumps;
641
642 /* Get destination. */
643 dest = map_getDestination( &jumps );
644 if (dest == NULL)
645 return 0;
646
647 ls = system_index( dest );
648 lua_pushsystem( L, ls );
649 lua_pushnumber( L, jumps );
650 return 2;
651}
652
663static int playerL_autonavAbort( lua_State *L )
664{
665 const char *str = luaL_optstring(L,1,NULL);
666 player_autonavAbort( str );
667 return 0;
668}
669
678static int playerL_autonavReset( lua_State *L )
679{
680 double timer = luaL_optnumber(L,1,0.);
682 player.autonav_timer = timer;
683 return 0;
684}
685
692static int playerL_setSpeed( lua_State *L )
693{
694 double speed = luaL_optnumber( L, 1, -1 );
695
696 if (speed > 0.) {
697 player.speed = speed;
698 sound_setSpeed( speed );
699 pause_setSpeed( speed );
700 }
701 else {
702 player.speed = 1.;
704 }
705
706 return 0;
707}
708
725static int playerL_cinematics( lua_State *L )
726{
727 int b, f_gui, f_2x;
728 const char *abort_msg;
729 double speed;
730
731 /* Defaults. */
732 abort_msg= NULL;
733 f_gui = 0;
734 f_2x = 0;
735 speed = 1.;
736
737 /* Parse parameters. */
738 b = lua_toboolean( L, 1 );
739 if (!lua_isnoneornil(L,2)) {
740 if (!lua_istable(L,2)) {
741 NLUA_ERROR( L, _("Second parameter to cinematics should be a table of options or omitted!") );
742 return 0;
743 }
744
745 lua_getfield( L, 2, "abort" );
746 if (!lua_isnil( L, -1 ))
747 abort_msg = luaL_checkstring( L, -1 );
748 lua_pop( L, 1 );
749
750 lua_getfield( L, 2, "gui" );
751 f_gui = lua_toboolean(L, -1);
752 lua_pop( L, 1 );
753
754 lua_getfield( L, 2, "no2x" );
755 f_2x = lua_toboolean(L, -1);
756 lua_pop( L, 1 );
757
758 lua_getfield( L, 2, "speed" );
759 speed = luaL_optnumber(L,-1,1.);
760 lua_pop( L, 1 );
761 }
762
763 if (b) {
764 /* Reset speeds. This will override the player's ship base speed. */
765 player.speed = speed;
766 sound_setSpeed( speed );
767 pause_setSpeed( speed );
768
769 /* Get rid of stuff that could be bothersome. */
770 player_autonavAbort( abort_msg );
771 ovr_setOpen(0);
772
773 /* Handle options. */
774 player_setFlag( PLAYER_CINEMATICS );
775 if (!f_gui)
776 player_setFlag( PLAYER_CINEMATICS_GUI );
777 if (f_2x)
778 player_setFlag( PLAYER_CINEMATICS_2X );
779
780 /* Redo viewport. */
782 }
783 else {
784 /* Reset speed properly to player speed. */
786
787 /* Clean up flags. */
788 player_rmFlag( PLAYER_CINEMATICS );
789 player_rmFlag( PLAYER_CINEMATICS_GUI );
790 player_rmFlag( PLAYER_CINEMATICS_2X );
791
792 /* Reload GUI. */
793 gui_reload();
794 }
795
796 return 0;
797}
798
805static int playerL_damageSPFX( lua_State *L )
806{
807 double spfx_mod = luaL_checknumber(L,1);
808 spfx_shake( spfx_mod );
809 spfx_damage( spfx_mod );
810 return 0;
811}
812
818static int playerL_screenshot( lua_State *L )
819{
820 (void) L;
822 return 0;
823}
824
834static int playerL_unboard( lua_State *L )
835{
836 (void) L;
838 return 0;
839}
840
847static int playerL_isLanded( lua_State *L )
848{
849 lua_pushboolean( L, landed );
850 return 1;
851}
852
861static int playerL_takeoff( lua_State *L )
862{
863 if (!landed) {
864 NLUA_ERROR(L,_("Player must be landed to force takeoff."));
865 return 0;
866 }
867
869
870 return 0;
871}
872
881static int playerL_land( lua_State *L )
882{
883 PLAYER_CHECK();
884
885 Spob *spob = luaL_validspob(L,1);
886 const char *sysname = spob_getSystem( spob->name );
887 if (sysname == NULL)
888 NLUA_ERROR(L,_("Spob '%s' is not in a system!"), spob->name);
889
890 if (strcmp(sysname,cur_system->name) != 0) {
891 /* Refer to playerL_teleport for the voodoo that happens here. */
892 pilot_rmFlag( player.p, PILOT_HYPERSPACE );
893 pilot_rmFlag( player.p, PILOT_HYP_BEGIN );
894 pilot_rmFlag( player.p, PILOT_HYP_BRAKE );
895 pilot_rmFlag( player.p, PILOT_HYP_PREP );
898
900
902
903 space_init( sysname, 0 );
904
905 ovr_initAlpha();
906 }
907 player.p->solid->pos = spob->pos; /* Set position to target. */
908
909 /* Do whatever the spob wants to do. */
910 if (spob->lua_land != LUA_NOREF) {
911 spob_luaInitMem( spob );
912 lua_rawgeti(naevL, LUA_REGISTRYINDEX, spob->lua_land); /* f */
913 lua_pushspob( naevL, spob_index(spob) );
914 lua_pushpilot( naevL, player.p->id );
915 if (nlua_pcall( spob->lua_env, 2, 0 )) {
916 WARN(_("Spob '%s' failed to run '%s':\n%s"), spob->name, "land", lua_tostring(naevL,-1));
917 lua_pop(naevL,1);
918 }
919
920 return 0;
921 }
922
923 space_queueLand( spob );
924 return 0;
925}
926
941static int playerL_allowLand( lua_State *L )
942{
943
944 int b;
945 const char *str = NULL;
946
947 if (lua_gettop(L) > 0) {
948 b = lua_toboolean(L,1);
949 if (lua_isstring(L,2))
950 str = lua_tostring(L,2);
951 }
952 else
953 b = 1;
954
955 if (b)
956 player_rmFlag( PLAYER_NOLAND );
957 else {
958 player_setFlag( PLAYER_NOLAND );
959 player_nolandMsg( str );
960 }
961 return 0;
962}
963
981static int playerL_landWindow( lua_State *L )
982{
983 int ret;
984 const char *str;
985 int win;
986
987 if (!landed) {
988 NLUA_ERROR(L, _("Must be landed to set the active land window."));
989 return 0;
990 }
991
992 str = luaL_checkstring(L,1);
993 if (strcasecmp(str,"main")==0)
994 win = LAND_WINDOW_MAIN;
995 else if (strcasecmp(str,"bar")==0)
996 win = LAND_WINDOW_BAR;
997 else if (strcasecmp(str,"missions")==0)
998 win = LAND_WINDOW_MISSION;
999 else if (strcasecmp(str,"outfits")==0)
1000 win = LAND_WINDOW_OUTFITS;
1001 else if (strcasecmp(str,"shipyard")==0)
1002 win = LAND_WINDOW_SHIPYARD;
1003 else if (strcasecmp(str,"equipment")==0)
1004 win = LAND_WINDOW_EQUIPMENT;
1005 else if (strcasecmp(str,"commodity")==0)
1006 win = LAND_WINDOW_COMMODITY;
1007 else
1008 NLUA_INVALID_PARAMETER(L);
1009
1010 /* Sets the window. */
1011 ret = land_setWindow( win );
1012
1013 lua_pushboolean( L, !ret );
1014 return 1;
1015}
1016
1022static int playerL_commclose( lua_State *L )
1023{
1024 (void) L;
1026 return 0;
1027}
1028
1029static PlayerShip_t *playerL_shipvarShip( lua_State *L, int idx )
1030{
1031 if (lua_isnoneornil(L,idx))
1032 return &player.ps;
1033 return player_getPlayerShip( luaL_checkstring(L,idx) );
1034}
1035
1045static int playerL_shipvarPeek( lua_State *L )
1046{
1047 const char *str = luaL_checkstring(L,1);
1048 PlayerShip_t *ps = playerL_shipvarShip(L,2);
1049 lvar *var = lvar_get( ps->p->shipvar, str );
1050 if (var != NULL)
1051 return lvar_push( L, var );
1052 return 0;
1053}
1054
1063static int playerL_shipvarPush( lua_State *L )
1064{
1065 const char *str = luaL_checkstring(L,1);
1066 lvar var = lvar_tovar( L, str, 2 );
1067 PlayerShip_t *ps = playerL_shipvarShip(L,3);
1068 if (ps->p->shipvar==NULL)
1069 ps->p->shipvar = array_create( lvar );
1070 lvar_addArray( &ps->p->shipvar, &var, 1 );
1071 return 0;
1072}
1073
1081static int playerL_shipvarPop( lua_State *L )
1082{
1083 const char *str = luaL_checkstring(L,1);
1084 PlayerShip_t *ps = playerL_shipvarShip(L,2);
1085 lvar *var = lvar_get( ps->p->shipvar, str );
1086 if (var != NULL)
1087 lvar_rmArray( &ps->p->shipvar, var );
1088 return 0;
1089}
1090
1099static int playerL_ships( lua_State *L )
1100{
1101 PLAYER_CHECK();
1102
1103 const PlayerShip_t *ships = player_getShipStack();
1104 lua_newtable(L); /* t */
1105 for (int i=0; i<array_size(ships); i++) {
1106 lua_newtable(L); /* t, k, t */
1107
1108 lua_pushstring(L, ships[i].p->name); /* t, k, t, s */
1109 lua_setfield(L, -2, "name"); /* t, k, t */
1110
1111 lua_pushship(L, ships[i].p->ship); /* t, k, t, s */
1112 lua_setfield(L, -2, "ship"); /* t, k, t */
1113
1114 lua_pushboolean(L, ships[i].deployed); /* t, k, t, s */
1115 lua_setfield(L, -2, "deployed"); /* t, k, t */
1116
1117 lua_rawseti(L, -2, i+1); /* t */
1118 }
1119 return 1;
1120}
1121
1131static int playerL_shipOutfits( lua_State *L )
1132{
1133 PLAYER_CHECK();
1134
1135 const char *str;
1136 int j;
1137 const PlayerShip_t *ships;
1138 Pilot *p;
1139
1140 /* Get name. */
1141 str = luaL_optstring(L, 1, NULL);
1142 ships = player_getShipStack();
1143
1144 /* Get outfit. */
1145 lua_newtable(L);
1146
1147 p = NULL;
1148 if ((str==NULL) || (strcmp(str, player.p->name)==0))
1149 p = player.p;
1150 else {
1151 for (int i=0; i<array_size(ships); i++) {
1152 if (strcmp(str, ships[i].p->name)==0) {
1153 p = ships[i].p;
1154 break;
1155 }
1156 }
1157 }
1158
1159 if (p == NULL) {
1160 NLUA_ERROR( L, _("Player does not own a ship named '%s'"), str );
1161 return 0;
1162 }
1163
1164 lua_newtable( L );
1165 j = 1;
1166 for (int i=0; i<array_size(p->outfits); i++) {
1167 if (p->outfits[i]->outfit == NULL)
1168 continue;
1169
1170 /* Set the outfit. */
1171 lua_pushoutfit( L, p->outfits[i]->outfit );
1172 lua_rawseti( L, -2, j++ );
1173 }
1174
1175 return 1;
1176}
1177
1185static int playerL_shipMetadata( lua_State *L )
1186{
1187 PLAYER_CHECK();
1188
1189 int destroyed = 0;
1190 const char *str = luaL_optstring(L, 1, NULL);
1191 const PlayerShip_t *ships = player_getShipStack();
1192 const PlayerShip_t *ps = NULL;
1193 if ((str==NULL) || (strcmp(str, player.p->name)==0))
1194 ps = &player.ps;
1195 else {
1196 for (int i=0; i<array_size(ships); i++) {
1197 if (strcmp(str, ships[i].p->name)==0) {
1198 ps = &ships[i];
1199 break;
1200 }
1201 }
1202 }
1203 if (ps == NULL) {
1204 NLUA_ERROR( L, _("Player does not own a ship named '%s'"), str );
1205 return 0;
1206 }
1207
1208 lua_newtable(L);
1209
1210 lua_pushnumber( L, ps->time_played );
1211 lua_setfield( L, -2, "time_played" );
1212
1213 lua_pushstring( L, ps->acquired );
1214 lua_setfield( L, -2, "acquired" );
1215
1216 lua_pushtime( L, ps->acquired_date );
1217 lua_setfield( L, -2, "acquired_date" );
1218
1219 lua_pushnumber( L, ps->dmg_done_shield );
1220 lua_setfield( L, -2, "dmg_done_shield" );
1221
1222 lua_pushnumber( L, ps->dmg_done_armour );
1223 lua_setfield( L, -2, "dmg_done_armour" );
1224
1225 lua_pushnumber( L, ps->dmg_done_shield+ps->dmg_done_armour );
1226 lua_setfield( L, -2, "dmg_done" );
1227
1228 lua_pushnumber( L, ps->dmg_taken_shield );
1229 lua_setfield( L, -2, "dmg_taken_shield" );
1230
1231 lua_pushnumber( L, ps->dmg_taken_armour );
1232 lua_setfield( L, -2, "dmg_taken_armour" );
1233
1234 lua_pushnumber( L, ps->dmg_taken_shield+ps->dmg_taken_armour );
1235 lua_setfield( L, -2, "dmg_taken" );
1236
1237 lua_pushinteger( L, ps->jumped_times );
1238 lua_setfield( L, -2, "jumped_times" );
1239
1240 lua_pushinteger( L, ps->landed_times );
1241 lua_setfield( L, -2, "landed_times" );
1242
1243 lua_pushinteger( L, ps->death_counter );
1244 lua_setfield( L, -2, "death_counter" );
1245
1246 for (int i=0; i<SHIP_CLASS_TOTAL; i++)
1247 destroyed += ps->ships_destroyed[i];
1248 lua_pushinteger( L, destroyed );
1249 lua_setfield( L, -2, "ships_destroyed" );
1250
1251 return 1;
1252}
1253
1261static int playerL_shipDeploy( lua_State *L )
1262{
1263 const char *shipname = luaL_checkstring(L,1);
1264 int deploy = lua_toboolean(L,2);
1265 PlayerShip_t *ps = player_getPlayerShip( shipname );
1266 ps->deployed = deploy;
1267 return 0;
1268}
1269
1280static int playerL_outfits( lua_State *L )
1281{
1282 PLAYER_CHECK();
1283
1284 const PlayerOutfit_t *outfits = player_getOutfits();
1285 lua_newtable(L);
1286 for (int i=0; i<array_size(outfits); i++) {
1287 lua_pushoutfit(L, outfits[i].o );
1288 lua_rawseti(L, -2, i+1);
1289 }
1290 return 1;
1291}
1292
1303static int playerL_numOutfit( lua_State *L )
1304{
1305 PLAYER_CHECK();
1306
1307 const Outfit *o;
1308 int q, unequipped_only;
1309
1310 /* Handle parameters. */
1311 o = luaL_validoutfit(L, 1);
1312 unequipped_only = lua_toboolean(L, 2);
1313
1314 /* Count the outfit. */
1315 if (unequipped_only)
1316 q = player_outfitOwned( o );
1317 else
1318 q = player_outfitOwnedTotal( o );
1319 lua_pushnumber( L, q );
1320
1321 return 1;
1322}
1333static int playerL_outfitAdd( lua_State *L )
1334{
1335
1336 /* Handle parameters. */
1337 const Outfit *o = luaL_validoutfit(L, 1);
1338 int q = luaL_optinteger(L, 2, 1);
1339
1340 /* Add the outfits. */
1341 player_addOutfit( o, q );
1342
1343 /* Update equipment list. */
1345
1346 return 0;
1347}
1359static int playerL_outfitRm( lua_State *L )
1360{
1361 NLUA_MIN_ARGS(1);
1362
1363 const Outfit *o;
1364 int q = luaL_optinteger(L, 2, 1);
1365
1366 /* Handle special case it's "all". */
1367 if (lua_isstring(L, 1)) {
1368 const char *str = luaL_checkstring(L, 1);
1369
1370 if (strcmp(str,"all")==0) {
1371 const PlayerOutfit_t *poutfits = player_getOutfits();
1372 const Outfit **outfits = array_create_size( const Outfit*, array_size( poutfits ) );
1373 for (int i=0; i<array_size(poutfits); i++)
1374 array_push_back( &outfits, poutfits[i].o );
1375
1376 for (int i=0; i<array_size(outfits); i++) {
1377 o = outfits[i];
1378 q = player_outfitOwned(o);
1379 player_rmOutfit(o, q);
1380 }
1381 /* Clean up. */
1382 array_free(outfits);
1383
1384 /* Update equipment list. */
1386 return 0;
1387 }
1388 }
1389
1390 /* Usual case. */
1391 o = luaL_validoutfit(L, 1);
1392 player_rmOutfit( o, q );
1393
1394 /* Update equipment list. */
1396
1397 return 0;
1398}
1399
1414static int playerL_shipAdd( lua_State *L )
1415{
1416 PlayerShip_t *new_ship;
1417 /* Handle parameters. */
1418 const Ship *s = luaL_validship(L, 1);
1419 const char *name = luaL_optstring(L, 2, _(s->name));
1420 const char *acquired = luaL_optstring(L, 3, NULL);
1421 int noname = lua_toboolean(L, 4);
1422 /* Add the ship, look in case it's cancelled. */
1423 do {
1424 new_ship = player_newShip( s, name, 0, acquired, noname );
1425 } while (new_ship == NULL);
1426 /* Return the new name. */
1427 lua_pushstring( L, new_ship->p->name );
1428 return 1;
1429}
1430
1442static int playerL_shipSwap( lua_State *L )
1443{
1444 PLAYER_CHECK();
1445
1446 const char *str = luaL_checkstring(L,1);
1447 int ignore_cargo= lua_toboolean(L,2);
1448 const char *cur = player.p->name;
1449 player_swapShip( str, !ignore_cargo );
1450 if (lua_toboolean(L,3))
1451 player_rmShip( cur );
1452
1453 return 0;
1454}
1455
1462static int playerL_missions( lua_State *L )
1463{
1464 int j = 1;
1465 lua_newtable(L);
1466 for (int i=0; i<array_size(player_missions); i++) {
1467 if (player_missions[i]->id == 0)
1468 continue;
1469 lua_pushstring( L, player_missions[i]->data->name );
1470 lua_rawseti( L, -2, j++ );
1471 }
1472 return 1;
1473}
1474
1484static int playerL_misnActive( lua_State *L )
1485{
1486 const char *str = luaL_checkstring(L,1);
1487 const MissionData *misn = mission_getFromName( str );
1488 if (misn == NULL) {
1489 NLUA_ERROR(L, _("Mission '%s' not found in stack"), str);
1490 return 0;
1491 }
1492 int n = mission_alreadyRunning( misn );
1493 if (n > 0)
1494 lua_pushinteger( L, n );
1495 else
1496 lua_pushboolean( L, 0 );
1497 return 1;
1498}
1499
1510static int playerL_misnDone( lua_State *L )
1511{
1512 const char *str = luaL_checkstring(L, 1);
1513 int id = mission_getID( str );
1514 if (id == -1) {
1515 NLUA_ERROR(L, _("Mission '%s' not found in stack"), str);
1516 return 0;
1517 }
1518 lua_pushboolean( L, player_missionAlreadyDone( id ) );
1519 return 1;
1520}
1521
1528static int playerL_misnDoneList( lua_State *L )
1529{
1530 int *done = player_missionsDoneList();
1531 lua_newtable(L);
1532 for (int i=0; i<array_size(done); i++) {
1533 mission_toLuaTable( L, mission_get( done[i] ) );
1534 lua_rawseti(L,-2,i+1);
1535 }
1536 return 1;
1537}
1538
1548static int playerL_evtActive( lua_State *L )
1549{
1550 const char *str= luaL_checkstring(L,1);
1551 int evtid = event_dataID( str );
1552 if (evtid < 0) {
1553 NLUA_ERROR(L, _("Event '%s' not found in stack"), str);
1554 return 0;
1555 }
1556 lua_pushboolean( L, event_alreadyRunning( evtid ) );
1557 return 1;
1558}
1559
1570static int playerL_evtDone( lua_State *L )
1571{
1572 const char *str = luaL_checkstring(L, 1);
1573 int id = event_dataID( str );
1574 if (id == -1) {
1575 NLUA_ERROR(L, _("Event '%s' not found in stack"), str);
1576 return 0;
1577 }
1578 lua_pushboolean( L, player_eventAlreadyDone( id ) );
1579 return 1;
1580}
1581
1588static int playerL_fleetList( lua_State *L )
1589{
1590 int n = 1;
1591 const PlayerShip_t* pstack = player_getShipStack();
1592 lua_newtable(L);
1593 for (int i=0; i<array_size(pstack); i++) {
1594 const PlayerShip_t *ps = &pstack[i];
1595 if (!ps->deployed)
1596 continue;
1597 /* We can avoid the spaceWorthy check as they will be set to false due to not having a pilot. */
1598 /*if (!!pilot_isSpaceworthy(ps->p))
1599 continue;*/
1600
1601 if (ps->p==NULL)
1602 lua_pushboolean( L, 0 );
1603 else
1604 lua_pushpilot( L, ps->p->id );
1605 lua_rawseti( L, -2, n++ );
1606 }
1607 return 1;
1608}
1609
1616static int playerL_fleetCargoFree( lua_State *L )
1617{
1618 lua_pushinteger( L, pfleet_cargoFree() );
1619 return 1;
1620}
1621
1628static int playerL_fleetCargoUsed( lua_State *L )
1629{
1630 lua_pushinteger( L, pfleet_cargoUsed() );
1631 return 1;
1632}
1633
1641static int playerL_fleetCargoOwned( lua_State *L )
1642{
1643 Commodity *c = luaL_validcommodity( L, 1 );
1644 lua_pushinteger( L, pfleet_cargoOwned( c ) );
1645 return 1;
1646}
1647
1656static int playerL_fleetCargoAdd( lua_State *L )
1657{
1658 Commodity *c = luaL_validcommodity( L, 1 );
1659 int q = luaL_checkinteger( L, 2 );
1660 lua_pushinteger( L, pfleet_cargoAdd( c, q ) );
1661 return 1;
1662}
1663
1672static int playerL_fleetCargoRm( lua_State *L )
1673{
1674 Commodity *c = luaL_validcommodity( L, 1 );
1675 int q = luaL_checkinteger( L, 2 );
1676 lua_pushinteger( L, pfleet_cargoRm( c, q, 0 ) );
1677 return 1;
1678}
1679
1688static int playerL_fleetCargoJet( lua_State *L )
1689{
1690 Commodity *c = luaL_validcommodity( L, 1 );
1691 int q = luaL_checkinteger( L, 2 );
1692 lua_pushinteger( L, pfleet_cargoRm( c, q, 1 ) );
1693 return 1;
1694}
1695
1704static int playerL_fleetCargoList( lua_State *L )
1705{
1706 Commodity *call = commodity_getAll();
1707 int n = 0;
1708 lua_newtable(L); /* t */
1709 for (int i=0; i<array_size(call); i++) {
1710 Commodity *c = &call[i];
1711 int q = pfleet_cargoOwned( c );
1712 if (q <= 0)
1713 continue;
1714
1715 lua_newtable(L); /* t, t */
1716
1717 lua_pushcommodity( L, c ); /* t, t, c */
1718 lua_setfield( L, -2, "c" ); /* t, t */
1719
1720 lua_pushinteger( L, q ); /* t, t, q */
1721 lua_setfield( L, -2, "q" ); /* t, t */
1722
1723 lua_rawseti( L, -2, ++n ); /* t */
1724 }
1725 return 1;
1726}
1727
1736static int playerL_inventory( lua_State *L )
1737{
1738 const PlayerItem *inv = player_inventory();
1739 lua_newtable(L);
1740 for (int i=0; i<array_size(inv); i++) {
1741 const PlayerItem *pi = &inv[i];
1742 lua_newtable(L);
1743
1744 lua_pushstring(L,pi->name);
1745 lua_setfield(L,-2,"name");
1746
1747 lua_pushinteger(L,pi->quantity);
1748 lua_setfield(L,-2,"quantity");
1749
1750 lua_rawseti(L,-2,i+1);
1751 }
1752 return 1;
1753}
1754
1763static int playerL_inventoryAdd( lua_State *L )
1764{
1765 const char *name = luaL_checkstring(L,1);
1766 int q = luaL_optinteger(L,2,1);
1767 lua_pushinteger( L, player_inventoryAdd( name, q ) );
1768 return 1;
1769}
1770
1779static int playerL_inventoryRm( lua_State *L )
1780{
1781 const char *name = luaL_checkstring(L,1);
1782 int q = luaL_optinteger(L,2,1);
1783 lua_pushinteger( L, player_inventoryRemove( name, q ) );
1784 return 1;
1785}
1786
1794static int playerL_inventoryOwned( lua_State *L )
1795{
1796 const char *name = luaL_checkstring(L,1);
1797 lua_pushinteger( L, player_inventoryAmount( name ) );
1798 return 1;
1799}
1800
1817static int playerL_teleport( lua_State *L )
1818{
1819 Spob *pnt;
1820 const char *name, *pntname;
1821 int no_simulate, silent;
1822
1823 PLAYER_CHECK();
1824
1825 /* Must not be landed. */
1826 if (landed)
1827 NLUA_ERROR(L,_("Can not teleport the player while landed!"));
1828 if (comm_isOpen())
1829 NLUA_ERROR(L,_("Can not teleport the player while the comm is open!"));
1830 if (player_isBoarded())
1831 NLUA_ERROR(L,_("Can not teleport the player while they are boarded!"));
1832 pnt = NULL;
1833
1834 /* Get a system. */
1835 if (lua_issystem(L,1)) {
1836 StarSystem *sys = luaL_validsystem(L,1);
1837 name = system_getIndex(sys->id)->name;
1838 }
1839 /* Get a spob. */
1840 else if (lua_isspob(L,1)) {
1841 pnt = luaL_validspob(L,1);
1842 name = spob_getSystem( pnt->name );
1843 if (name == NULL) {
1844 NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), pnt->name );
1845 return 0;
1846 }
1847 }
1848 /* Get destination from string. */
1849 else if (lua_isstring(L,1)) {
1850 const char *sysname;
1851 name = lua_tostring(L,1);
1852 sysname = system_existsCase( name );
1853 if (sysname == NULL) {
1854 /* No system found, assume destination string is the name of a spob. */
1855 pntname = name;
1856 name = spob_getSystem( pntname );
1857 pnt = spob_get( pntname );
1858 if (pnt == NULL) {
1859 NLUA_ERROR( L, _("'%s' is not a valid teleportation target."), name );
1860 return 0;
1861 }
1862
1863 if (name == NULL) {
1864 NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), pntname );
1865 return 0;
1866 }
1867 }
1868 else
1869 name = sysname;
1870 }
1871 else
1872 NLUA_INVALID_PARAMETER(L);
1873
1874 no_simulate = lua_toboolean(L,2);
1875 silent = lua_toboolean(L,3);
1876
1877 /* Check if system exists. */
1878 if (system_get( name ) == NULL) {
1879 NLUA_ERROR( L, _("System '%s' does not exist."), name );
1880 return 0;
1881 }
1882
1883 /* Jump out hook is run first. */
1884 hooks_run( "jumpout" );
1885
1886 /* Just in case remove hyperspace flags. */
1887 pilot_rmFlag( player.p, PILOT_HYPERSPACE );
1888 pilot_rmFlag( player.p, PILOT_HYP_BEGIN );
1889 pilot_rmFlag( player.p, PILOT_HYP_BRAKE );
1890 pilot_rmFlag( player.p, PILOT_HYP_PREP );
1891 /* Don't follow anything. */
1894
1895 /* Free graphics. */
1897
1898 /* Reset targets when teleporting.
1899 * Both of these functions invoke gui_setNav(), which updates jump and
1900 * spob targets simultaneously. Thus, invalid reads may arise and the
1901 * target reset must be done prior to calling space_init and destroying
1902 * the old system. */
1904
1905 /* Hide messages if not needed. */
1906 if (silent)
1907 player_messageToggle(0); /* space_init will reset it, so no need to. */
1908
1909 /* Go to the new system. */
1910 space_init( name, !no_simulate );
1911
1912 /* Map gets deformed when jumping this way. */
1913 map_clear();
1914
1915 /* Initialize alpha as needed. */
1916 ovr_initAlpha();
1917
1918 /* Run hooks - order is important. */
1920 hooks_run( "jumpin" );
1921 hooks_run( "enter" );
1922 events_trigger( EVENT_TRIGGER_ENTER );
1923 missions_run( MIS_AVAIL_ENTER, -1, NULL, NULL );
1924
1925 /* Move to spob. */
1926 if (pnt != NULL)
1927 player.p->solid->pos = pnt->pos;
1928
1929 /* Move all escorts to new position. */
1930 Pilot *const* pilot_stack = pilot_getAll();
1931 for (int i=0; i<array_size(pilot_stack); i++) {
1932 Pilot *p = pilot_stack[i];
1933 if (p->parent == PLAYER_ID) {
1934 memcpy( &p->solid->pos, &player.p->solid->pos, sizeof(vec2) );
1935 vec2_padd( &p->solid->pos, 200.+200.*RNGF(), 2.*M_PI*RNGF() );
1936
1937 /* Clean up trails. */
1938 pilot_clearTrails( p );
1939 }
1940 }
1941
1942 return 0;
1943}
1944
1951static int playerL_dt_mod( lua_State *L )
1952{
1953 lua_pushnumber(L,dt_mod);
1954 return 1;
1955}
1956
1965static int playerL_fleetCapacity( lua_State *L )
1966{
1967 int nships = 0;
1968 const PlayerShip_t *pships;
1969 pfleet_update();
1970 lua_pushnumber(L,player.fleet_capacity);
1971 lua_pushnumber(L,player.fleet_used);
1972 pships = player_getShipStack();
1973 for (int i=0; i<array_size(pships); i++) {
1974 if (!pships[i].deployed)
1975 continue;
1976 nships++;
1977 }
1978 lua_pushboolean(L, (nships==0) || (player.fleet_used <= player.fleet_capacity));
1979 return 3;
1980}
1981
1988static int playerL_fleetCapacitySet( lua_State *L )
1989{
1990 player.fleet_capacity = luaL_checkinteger(L,1);
1991 return 0;
1992}
1993
2000static int playerL_chapter( lua_State *L )
2001{
2002 lua_pushstring( L, player.chapter );
2003 return 1;
2004}
2005
2012static int playerL_chapterSet( lua_State *L )
2013{
2014 const char *str = luaL_checkstring(L,1);
2015 free( player.chapter );
2016 player.chapter = strdup(str);
2017 return 0;
2018}
2019
2030static int playerL_infoButtonRegister( lua_State *L )
2031{
2032 int id;
2033 const char *caption = luaL_checkstring( L, 1 );
2034 int priority = luaL_optinteger( L, 3, 5 );
2035 const char *key = luaL_optstring( L, 4, "" );
2036 luaL_checktype( L, 2, LUA_TFUNCTION );
2037 lua_pushvalue( L, 2 );
2038 id = info_buttonRegister( caption, priority, SDL_GetKeyFromName( key ) );
2039 lua_pushinteger( L, id );
2040 return 1;
2041}
2042
2049static int playerL_infoButtonUnregister( lua_State *L )
2050{
2051 int id = luaL_checkinteger(L,1);
2052 int ret = info_buttonUnregister( id );
2053 if (ret != 0)
2054 WARN(_("Failed to unregister info button with id '%d'!"), id);
2055 return 0;
2056}
2057
2064static int playerL_canDiscover( lua_State *L )
2065{
2066 player.discover_off = !lua_toboolean(L,1);
2067 return 0;
2068}
2069
2077static int playerL_save( lua_State *L )
2078{
2079 const char *savename = luaL_optstring( L, 1, "autosave" );
2080 Spob *savespob = NULL;
2081 Spob *prevspob;
2082 if (!lua_isnoneornil(L,2))
2083 savespob = luaL_validspob(L,2);
2084
2085 if (!landed && (savespob==NULL))
2086 NLUA_ERROR(L,_("Unable to save when not landed and land spob is not specified!"));
2087 else if (landed && (savespob!=land_spob))
2088 NLUA_ERROR(L,_("Unable to save when landed and land_spob does not match landed spob!"));
2089
2090 if (savespob != NULL) {
2091 prevspob = land_spob;
2092 land_spob = savespob;
2093 }
2094 lua_pushboolean( L, save_all_with_name( savename ) );
2095 if (savespob != NULL)
2096 land_spob = prevspob;
2097
2098 return 1;
2099}
2100
2108static int playerL_saveBackup( lua_State *L )
2109{
2110 char file[PATH_MAX], backup[PATH_MAX];
2111 const char *filename = luaL_checkstring(L,1); /* TODO sanitize path and such. */
2112 if (strcmp(filename,"autosave")==0)
2113 NLUA_ERROR(L,_("Can not back up save to 'autosave'."));
2114 snprintf( file, sizeof(file), "saves/%s/autosave.ns", player.name );
2115 snprintf( backup, sizeof(backup), "saves/%s/%s.ns", player.name, filename );
2116 lua_pushboolean( L, ndata_copyIfExists(file, backup) );
2117 return 1;
2118}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition: array.h:158
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
Definition: array.h:102
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition: array.h:129
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition: array.h:93
int player_isBoarded(void)
Gets if the player is boarded.
Definition: board.c:42
void board_unboard(void)
Forces unboarding of the pilot.
Definition: board.c:197
int comm_isOpen(void)
Check to see if the comm window is open.
Definition: comm.c:51
void comm_queueClose(void)
Queues a close command when possible.
Definition: comm.c:59
int event_alreadyRunning(int data)
Check to see if an event is already running.
Definition: event.c:297
int event_dataID(const char *evdata)
Gets the event data id from name.
Definition: event.c:695
void events_trigger(EventTrigger_t trigger)
Runs all the events matching a trigger.
Definition: event.c:314
void gui_clearMessages(void)
Clears the GUI messages.
Definition: gui.c:973
void gui_reload(void)
Reloads the GUI.
Definition: gui.c:1730
void player_messageRaw(const char *str)
Adds a mesg to the queue to be displayed on screen.
Definition: gui.c:293
void player_messageToggle(int enable)
Toggles if player should receive messages.
Definition: gui.c:283
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
Definition: hook.c:967
int hooks_run(const char *stack)
Runs all the hooks of stack.
Definition: hook.c:987
Handles the info menu.
int info_buttonRegister(const char *caption, int priority, SDL_Keycode key)
Registers a button in the info menu.
Definition: info.c:188
int info_buttonUnregister(int id)
Unregisters a button in the info menu.
Definition: info.c:219
void land_queueTakeoff(void)
Queue a takeoff.
Definition: land.c:143
int land_setWindow(int window)
Sets the land window tab.
Definition: land.c:1186
int landed
Definition: land.c:74
Spob * land_spob
Definition: land.c:82
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
Definition: land_outfits.c:523
int lvar_addArray(lvar **arr, lvar *new_var, int sort)
Adds a var to a var array.
Definition: lvar.c:160
void lvar_rmArray(lvar **arr, lvar *rm_var)
Removes a var from a var array.
Definition: lvar.c:187
lvar * lvar_get(lvar *arr, const char *str)
Gets a lua var by name.
Definition: lvar.c:39
lvar lvar_tovar(lua_State *L, const char *name, int idx)
Gets a lua variable from an index from a lua state.
Definition: lvar.c:84
int lvar_push(lua_State *L, const lvar *v)
Pushes a lua var to a lua state.
Definition: lvar.c:54
Mission ** player_missions
Definition: mission.c:47
const MissionData * mission_getFromName(const char *name)
Gets mission data from a name.
Definition: mission.c:123
int mission_alreadyRunning(const MissionData *misn)
Checks to see if mission is already running.
Definition: mission.c:210
void missions_run(MissionAvailability loc, int faction, const Spob *pnt, const StarSystem *sys)
Runs missions matching location, all Lua side and one-shot.
Definition: mission.c:301
int mission_getID(const char *name)
Gets id from mission name.
Definition: mission.c:98
const MissionData * mission_get(int id)
Gets a MissionData based on ID.
Definition: mission.c:114
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition: naev.h:41
#define PATH_MAX
Definition: naev.h:50
int ndata_copyIfExists(const char *file1, const char *file2)
Copy a file, if it exists.
Definition: ndata.c:304
Commodity ** lua_pushcommodity(lua_State *L, Commodity *commodity)
Pushes a commodity on the stack.
Commodity * luaL_validcommodity(lua_State *L, int ind)
Makes sure the commodity is valid or raises a Lua error.
const Outfit * luaL_validoutfit(lua_State *L, int ind)
Makes sure the outfit is valid or raises a Lua error.
Definition: nlua_outfit.c:135
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
Definition: nlua_outfit.c:160
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition: nlua_pilot.c:495
static int playerL_shipDeploy(lua_State *L)
Sets the deployed status of a player's ship.
Definition: nlua_player.c:1261
static int playerL_outfitAdd(lua_State *L)
Adds an outfit to the player's outfit list.
Definition: nlua_player.c:1333
static int playerL_evtActive(lua_State *L)
Checks to see if the player has an event active.
Definition: nlua_player.c:1548
static int playerL_shipvarPush(lua_State *L)
Pushes a ship variable.
Definition: nlua_player.c:1063
static int playerL_shipMetadata(lua_State *L)
Gets meta-data about one of the player's ships.
Definition: nlua_player.c:1185
static int playerL_unboard(lua_State *L)
Unboards the player from its boarded target.
Definition: nlua_player.c:834
static int playerL_fleetCargoAdd(lua_State *L)
Tries to add an amount of commodity to the player's fleet.
Definition: nlua_player.c:1656
static int playerL_fleetCapacitySet(lua_State *L)
Sets the fleet capacity of the player.
Definition: nlua_player.c:1988
static int playerL_fleetCargoRm(lua_State *L)
Tries to remove an amount of commodity to the player's fleet.
Definition: nlua_player.c:1672
static int playerL_missions(lua_State *L)
Gets the list of the player's active missions.
Definition: nlua_player.c:1462
static int playerL_inventoryRm(lua_State *L)
Removes an item to the player's in-game inventory.
Definition: nlua_player.c:1779
static int playerL_msg(lua_State *L)
Sends the player an in-game message.
Definition: nlua_player.c:385
static int playerL_inventory(lua_State *L)
Gets the contents of the player's inventory.
Definition: nlua_player.c:1736
static int playerL_shipSwap(lua_State *L)
Swaps the player's current ship with a ship they own by name.
Definition: nlua_player.c:1442
static int playerL_credits(lua_State *L)
Gets how many credits the player has on him.
Definition: nlua_player.c:324
static int playerL_outfits(lua_State *L)
Gets all the outfits the player owns.
Definition: nlua_player.c:1280
static int playerL_saveBackup(lua_State *L)
Backs up the player's last autosave with a custom name.
Definition: nlua_player.c:2108
static int playerL_pay(lua_State *L)
Pays the player an amount of money.
Definition: nlua_player.c:276
static int playerL_shipname(lua_State *L)
Gets the player's ship's name (given by the player).
Definition: nlua_player.c:261
static int playerL_setSpeed(lua_State *L)
Sets the game speed directly.
Definition: nlua_player.c:692
static int playerL_fuel(lua_State *L)
Gets the amount of fuel a player has.
Definition: nlua_player.c:579
static int playerL_canDiscover(lua_State *L)
Global toggle to whether or not the player can discover space objects and jumps. Meant to be used wit...
Definition: nlua_player.c:2064
static int playerL_isLanded(lua_State *L)
Checks to see if the player is landed or not.
Definition: nlua_player.c:847
static int playerL_infoButtonUnregister(lua_State *L)
Unregisters a button in the info window.
Definition: nlua_player.c:2049
static int playerL_damageSPFX(lua_State *L)
Applies the damage effects to the player.
Definition: nlua_player.c:805
static int playerL_ships(lua_State *L)
Gets the names of the player's ships.
Definition: nlua_player.c:1099
static int playerL_takeoff(lua_State *L)
Forces the player to take off if they are landed.
Definition: nlua_player.c:861
static int playerL_cinematics(lua_State *L)
Puts the game in cinematics mode or back to regular mode.
Definition: nlua_player.c:725
static int playerL_fleetCapacity(lua_State *L)
Gets the fleet capacity (and used) of the player.
Definition: nlua_player.c:1965
static int playerL_inventoryAdd(lua_State *L)
Adds an item to the player's in-game inventory.
Definition: nlua_player.c:1763
static int playerL_commclose(lua_State *L)
Forces the player to close comm if they are chatting.
Definition: nlua_player.c:1022
static int playerL_autonavAbort(lua_State *L)
Stops the players autonav if active.
Definition: nlua_player.c:663
static int playerL_allowLand(lua_State *L)
Allows or disallows the player to land.
Definition: nlua_player.c:941
static int playerL_jumps(lua_State *L)
Gets a player's jump range based on their remaining fuel.
Definition: nlua_player.c:563
static int playerL_fleetCargoJet(lua_State *L)
Tries to remove an amount of commodity to the player's fleet and jettisons it into space.
Definition: nlua_player.c:1688
static const luaL_Reg playerL_methods[]
Definition: nlua_player.c:142
static int playerL_landWindow(lua_State *L)
Sets the active land window.
Definition: nlua_player.c:981
static int playerL_outfitRm(lua_State *L)
Removes an outfit from the player's outfit list.
Definition: nlua_player.c:1359
static int playerL_refuel(lua_State *L)
Refuels the player.
Definition: nlua_player.c:596
static int playerL_msgToggle(lua_State *L)
Clears the player's message buffer.
Definition: nlua_player.c:415
static int playerL_dt_mod(lua_State *L)
Gets the dt_mod of the player, which multiplies all time stuff.
Definition: nlua_player.c:1951
static int playerL_teleport(lua_State *L)
Teleports the player to a new spob or system (only if not landed).
Definition: nlua_player.c:1817
static int playerL_getPilot(lua_State *L)
Gets the player's associated pilot.
Definition: nlua_player.c:548
int nlua_loadPlayer(nlua_env env)
Loads the player Lua library.
Definition: nlua_player.c:224
static int playerL_chapterSet(lua_State *L)
Sets the player's current chapter.
Definition: nlua_player.c:2012
static int playerL_infoButtonRegister(lua_State *L)
Registers a button in the info window.
Definition: nlua_player.c:2030
static int playerL_shipvarPeek(lua_State *L)
Peeks at a ship variable.
Definition: nlua_player.c:1045
static int playerL_omsgRm(lua_State *L)
Removes an overlay message.
Definition: nlua_player.c:497
static int playerL_omsgChange(lua_State *L)
Changes an overlay message.
Definition: nlua_player.c:467
static int playerL_fleetList(lua_State *L)
Lists the ships in the player's fleet.
Definition: nlua_player.c:1588
static int playerL_misnDoneList(lua_State *L)
Gets a list of all the missions the player has done.
Definition: nlua_player.c:1528
static int playerL_fleetCargoList(lua_State *L)
Gets the list of all the cargos in the player's fleet.
Definition: nlua_player.c:1704
static int playerL_chapter(lua_State *L)
Gets the player's current chapter.
Definition: nlua_player.c:2000
static int playerL_getname(lua_State *L)
Lua bindings to interact with the player.
Definition: nlua_player.c:247
static int playerL_omsgAdd(lua_State *L)
Adds an overlay message.
Definition: nlua_player.c:432
static int playerL_fleetCargoUsed(lua_State *L)
Gets the amount of cargo space used in the player's fleet.
Definition: nlua_player.c:1628
static int playerL_fleetCargoFree(lua_State *L)
Gets the amount of cargo space free in the player's fleet.
Definition: nlua_player.c:1616
static int playerL_inventoryOwned(lua_State *L)
Checks to see how much of an item the player has in their inventory.
Definition: nlua_player.c:1794
static int playerL_shipAdd(lua_State *L)
Gives the player a new ship.
Definition: nlua_player.c:1414
static int playerL_allowSave(lua_State *L)
Sets player save ability.
Definition: nlua_player.c:511
static int playerL_autonavReset(lua_State *L)
Resets the game speed without disabling autonav.
Definition: nlua_player.c:678
static int playerL_evtDone(lua_State *L)
Checks to see if player has done an event.
Definition: nlua_player.c:1570
static int playerL_getPosition(lua_State *L)
Gets the player's position.
Definition: nlua_player.c:535
static int playerL_land(lua_State *L)
Automagically lands the player on a spob.
Definition: nlua_player.c:881
static int playerL_fleetCargoOwned(lua_State *L)
Gets the amount of cargo space used by a specific commodity in the player's fleet.
Definition: nlua_player.c:1641
static int playerL_numOutfit(lua_State *L)
Gets the number of outfits the player owns in their list (excludes equipped on ships).
Definition: nlua_player.c:1303
static int playerL_misnActive(lua_State *L)
Checks to see if the player has a mission active.
Definition: nlua_player.c:1484
static int playerL_wealth(lua_State *L)
Gets how many credits the player owns both directly, and in the form of assets (ships,...
Definition: nlua_player.c:352
static int playerL_autonav(lua_State *L)
Checks to see if the player has autonav enabled.
Definition: nlua_player.c:620
static int playerL_msgClear(lua_State *L)
Clears the player's message buffer.
Definition: nlua_player.c:402
static int playerL_misnDone(lua_State *L)
Checks to see if player has done a mission.
Definition: nlua_player.c:1510
static int playerL_save(lua_State *L)
Saves the game.
Definition: nlua_player.c:2077
static int playerL_autonavDest(lua_State *L)
Gets the player's long term autonav destination.
Definition: nlua_player.c:636
static int playerL_shipvarPop(lua_State *L)
Pops a ship variable.
Definition: nlua_player.c:1081
static int playerL_screenshot(lua_State *L)
Takes a screenshot (same as the keyboard action).
Definition: nlua_player.c:818
static int playerL_shipOutfits(lua_State *L)
Gets the outfits for one of the player's ships.
Definition: nlua_player.c:1131
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship on the stack.
Definition: nlua_ship.c:164
const Ship * luaL_validship(lua_State *L, int ind)
Makes sure the ship is valid or raises a Lua error.
Definition: nlua_ship.c:139
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
Definition: nlua_spob.c:192
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
Definition: nlua_spob.c:164
int lua_isspob(lua_State *L, int ind)
Checks to see if ind is a spob.
Definition: nlua_spob.c:207
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
Definition: nlua_system.c:185
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
Definition: nlua_system.c:156
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
Definition: nlua_system.c:201
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
Definition: nlua_time.c:126
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
void gl_setDefViewport(int x, int y, int w, int h)
Sets the default viewport.
Definition: opengl.c:597
glInfo gl_screen
Definition: opengl.c:51
void pause_setSpeed(double mod)
Adjusts the game's dt modifier.
Definition: pause.c:64
double dt_mod
Definition: pause.c:23
credits_t pilot_worth(const Pilot *p)
Gets the price or worth of a pilot in credits.
Definition: pilot.c:3917
void pilot_setCommMsg(Pilot *p, const char *s)
Sets the overhead communication message of the pilot.
Definition: pilot.c:1139
static Pilot ** pilot_stack
Definition: pilot.c:57
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition: pilot.c:83
int pilot_getJumps(const Pilot *p)
Gets the amount of jumps the pilot has left.
Definition: pilot.c:1316
void pilot_clearTrails(Pilot *p)
Resets the trails for a pilot.
Definition: pilot.c:3287
void pilot_outfitLOnjumpin(Pilot *pilot)
Runs Lua outfits when pilot jumps into a system.
int player_eventAlreadyDone(int id)
Checks to see if player has already completed a event.
Definition: player.c:2971
int player_rmOutfit(const Outfit *o, int quantity)
Remove an outfit from the player's outfit stack.
Definition: player.c:2859
void player_swapShip(const char *shipname, int move_cargo)
Swaps player's current ship with their ship named shipname.
Definition: player.c:501
int player_addOutfit(const Outfit *o, int quantity)
Adds an outfit to the player outfit stack.
Definition: player.c:2803
void player_resetSpeed(void)
Resets the player speed stuff.
Definition: player.c:1416
const PlayerShip_t * player_getShipStack(void)
Gets the array (array.h) of the player's ships.
Definition: player.c:2623
PlayerShip_t * player_newShip(const Ship *ship, const char *def_name, int trade, const char *acquired, int noname)
Creates a new ship for player.
Definition: player.c:382
void player_rmShip(const char *shipname)
Removes one of the player's ships.
Definition: player.c:669
int player_outfitOwnedTotal(const Outfit *o)
Definition: player.c:2729
PlayerShip_t * player_getPlayerShip(const char *shipname)
Gets a specific ship.
Definition: player.c:2682
int player_missionAlreadyDone(int id)
Checks to see if player has already completed a mission.
Definition: player.c:2927
credits_t player_modCredits(credits_t amount)
Modifies the amount of credits the player has.
Definition: player.c:947
void player_accelOver(void)
Done accelerating.
Definition: player.c:2102
Player_t player
Definition: player.c:73
int player_outfitOwned(const Outfit *o)
Gets how many of the outfit the player owns.
Definition: player.c:2701
void player_targetClearAll(void)
Clears all player targets: hyperspace, spob, asteroid, etc...
Definition: player.c:2219
void player_nolandMsg(const char *str)
Sets the no land message.
Definition: player.c:1733
const PlayerOutfit_t * player_getOutfits(void)
Gets an array (array.h) of the player's outfits.
Definition: player.c:2757
void player_screenshot(void)
Takes a screenshot.
Definition: player.c:2299
int * player_missionsDoneList(void)
Gets a list of all the missions the player has done.
Definition: player.c:2941
void player_autonavEnd(void)
Ends the autonav.
void player_autonavResetSpeed(void)
Resets the game speed.
void player_autonavAbort(const char *reason)
Aborts autonav.
int pfleet_cargoFree(void)
Gets the total amount of free cargo space in the player's fleet.
Definition: player_fleet.c:255
int pfleet_cargoOwned(const Commodity *com)
Gets the total amount of a commodity type owned by the player's fleet.
Definition: player_fleet.c:280
int pfleet_cargoAdd(const Commodity *com, int q)
Adds some cargo to the player's fleet.
Definition: player_fleet.c:306
int pfleet_cargoRm(const Commodity *com, int q, int jet)
Removes some cargo from the player's fleet.
Definition: player_fleet.c:335
int pfleet_cargoUsed(void)
Gets the total cargo space used by the player's fleet.
Definition: player_fleet.c:231
void pfleet_update(void)
Updates the used fleet capacity of the player.
Definition: player_fleet.c:24
int player_inventoryAdd(const char *name, int amount)
Adds an item to the player inventory.
int player_inventoryRemove(const char *name, int amount)
Removes an item from the player inventory.
const PlayerItem * player_inventory(void)
Gets the whole player inventory.
int player_inventoryAmount(const char *name)
Gets the amount of an item the player has.
static const double c[]
Definition: rng.c:264
static const double b[]
Definition: rng.c:256
int save_all_with_name(const char *name)
Saves the current game.
Definition: save.c:105
void sound_setSpeed(double s)
Sets the speed to play the sound at.
Definition: sound.c:1160
void space_init(const char *sysname, int do_simulate)
Initializes the system.
Definition: space.c:1501
void space_gfxUnload(StarSystem *sys)
Unloads all the graphics for a star system.
Definition: space.c:2068
Spob * spob_get(const char *spobname)
Gets a spob based on its name.
Definition: space.c:1006
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition: space.c:944
int spob_index(const Spob *p)
Gets the ID of a spob.
Definition: space.c:1055
const char * system_existsCase(const char *sysname)
Checks to see if a system exists case insensitively.
Definition: space.c:858
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition: space.c:914
char * spob_getSystem(const char *spobname)
Get the name of a system from a spobname.
Definition: space.c:980
StarSystem * cur_system
Definition: space.c:105
void space_queueLand(Spob *pnt)
Cues a spob to be landed on. This is not done immediately, but when the engine thinks it is ok to do.
Definition: space.c:4321
void spob_luaInitMem(const Spob *spob)
Initializes the memory fo a spob.
Definition: space.c:1931
int system_index(const StarSystem *sys)
Gets the index of a star system.
Definition: space.c:955
void spfx_shake(double mod)
Increases the current rumble level.
Definition: spfx.c:883
void spfx_damage(double mod)
Increases the current damage level.
Definition: spfx.c:903
Represents a commodity.
Definition: commodity.h:43
The actual hook parameter.
Definition: hook.h:35
Static mission data.
Definition: mission.h:61
A ship outfit, depends radically on the type.
Definition: outfit.h:304
The representation of an in-game pilot.
Definition: pilot.h:210
Solid * solid
Definition: pilot.h:220
unsigned int id
Definition: pilot.h:211
lvar * shipvar
Definition: pilot.h:373
credits_t credits
Definition: pilot.h:317
double fuel_max
Definition: pilot.h:252
double fuel
Definition: pilot.h:253
char * name
Definition: pilot.h:212
double fuel_consumption
Definition: pilot.h:254
Represents an item in the player inventory.
Wrapper for outfits.
Definition: player.h:65
Player ship.
Definition: player.h:73
time_t acquired_date
Definition: player.h:85
double time_played
Definition: player.h:83
double dmg_done_armour
Definition: player.h:87
unsigned int death_counter
Definition: player.h:93
double dmg_taken_armour
Definition: player.h:89
int deployed
Definition: player.h:80
double dmg_taken_shield
Definition: player.h:88
unsigned int landed_times
Definition: player.h:92
Pilot * p
Definition: player.h:74
unsigned int ships_destroyed[SHIP_CLASS_TOTAL]
Definition: player.h:90
char * acquired
Definition: player.h:84
unsigned int jumped_times
Definition: player.h:91
double dmg_done_shield
Definition: player.h:86
Pilot * p
Definition: player.h:101
PlayerShip_t ps
Definition: player.h:102
char * name
Definition: player.h:103
double autonav_timer
Definition: player.h:112
int fleet_used
Definition: player.h:130
char * chapter
Definition: player.h:116
int fleet_capacity
Definition: player.h:131
double speed
Definition: player.h:115
int discover_off
Definition: player.h:118
Represents a space ship.
Definition: ship.h:94
char * name
Definition: ship.h:95
vec2 pos
Definition: physics.h:22
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition: space.h:88
int lua_land
Definition: space.h:140
nlua_env lua_env
Definition: space.h:134
char * name
Definition: space.h:90
vec2 pos
Definition: space.h:93
int nw
Definition: opengl.h:43
int nh
Definition: opengl.h:44
Contains a mission variable.
Definition: lvar.h:24
Represents a 2d vector.
Definition: vec2.h:32