naev 0.12.5
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"
18
19#include "nlua_player.h"
20
21#include "array.h"
22#include "board.h"
23#include "camera.h"
24#include "comm.h"
25#include "event.h"
26#include "gui.h"
27#include "gui_omsg.h"
28#include "hook.h"
29#include "info.h"
30#include "land.h"
31#include "land_outfits.h"
32#include "map.h"
33#include "map_overlay.h"
34#include "menu.h"
35#include "mission.h"
36#include "ndata.h"
37#include "nlua_colour.h"
38#include "nlua_commodity.h"
39#include "nlua_misn.h"
40#include "nlua_outfit.h"
41#include "nlua_pilot.h"
42#include "nlua_ship.h"
43#include "nlua_spob.h"
44#include "nlua_system.h"
45#include "nlua_time.h"
46#include "nlua_vec2.h"
47#include "nluadef.h"
48#include "pause.h"
49#include "player.h"
50#include "player_autonav.h"
51#include "player_fleet.h"
52#include "player_gui.h"
53#include "player_inventory.h"
54#include "rng.h"
55#include "save.h"
56#include "sound.h"
57#include "start.h"
58
59#define PLAYER_CHECK() \
60 if ( player.p == NULL ) \
61 return 0
62
63/* Player methods. */
64static int playerL_getname( lua_State *L );
65static int playerL_shipname( lua_State *L );
66static int playerL_pay( lua_State *L );
67static int playerL_credits( lua_State *L );
68static int playerL_wealth( lua_State *L );
69static int playerL_msg( lua_State *L );
70static int playerL_msgClear( lua_State *L );
71static int playerL_msgToggle( lua_State *L );
72static int playerL_omsgAdd( lua_State *L );
73static int playerL_omsgChange( lua_State *L );
74static int playerL_omsgRm( lua_State *L );
75static int playerL_allowSave( lua_State *L );
76/* Faction stuff. */
77static int playerL_getPosition( lua_State *L );
78static int playerL_getPilot( lua_State *L );
79/* Fuel stuff. */
80static int playerL_jumps( lua_State *L );
81static int playerL_fuel( lua_State *L );
82static int playerL_refuel( lua_State *L );
83static int playerL_autonav( lua_State *L );
84static int playerL_autonavSetSpeed( lua_State *L );
85static int playerL_autonavSetTarget( lua_State *L );
86static int playerL_autonavSetPos( lua_State *L );
87static int playerL_autonavRoute( lua_State *L );
88static int playerL_autonavDest( lua_State *L );
89static int playerL_autonavAbort( lua_State *L );
90static int playerL_autonavReset( lua_State *L );
91static int playerL_autonavEnd( lua_State *L );
92/* Cinematics. */
93static int playerL_dt_default( lua_State *L );
94static int playerL_speed( lua_State *L );
95static int playerL_setSpeed( lua_State *L );
96static int playerL_cinematics( lua_State *L );
97static int playerL_cinematicsCheck( lua_State *L );
98static int playerL_damageSPFX( lua_State *L );
99static int playerL_screenshot( lua_State *L );
100/* Board stuff. */
101static int playerL_tryBoard( lua_State *L );
102static int playerL_unboard( lua_State *L );
103/* Land stuff. */
104static int playerL_isLanded( lua_State *L );
105static int playerL_takeoff( lua_State *L );
106static int playerL_tryLand( lua_State *L );
107static int playerL_land( lua_State *L );
108static int playerL_landAllow( lua_State *L );
109static int playerL_landWindow( lua_State *L );
110/* Hail stuff. */
111static int playerL_commOpen( lua_State *L );
112static int playerL_commClose( lua_State *L );
113/* Shipvars. */
114static int playerL_shipvarPeek( lua_State *L );
115static int playerL_shipvarPush( lua_State *L );
116static int playerL_shipvarPop( lua_State *L );
117/* Outfit and ship management stuff. */
118static int playerL_ships( lua_State *L );
119static int playerL_shipOutfits( lua_State *L );
120static int playerL_shipMetadata( lua_State *L );
121static int playerL_shipDeploy( lua_State *L );
122static int playerL_outfits( lua_State *L );
123static int playerL_outfitNum( lua_State *L );
124static int playerL_outfitAdd( lua_State *L );
125static int playerL_outfitRm( lua_State *L );
126static int playerL_shipAdd( lua_State *L );
127static int playerL_shipSwap( lua_State *L );
128/* Mission/event management stuff. */
129static int playerL_missions( lua_State *L );
130static int playerL_misnActive( lua_State *L );
131static int playerL_misnDone( lua_State *L );
132static int playerL_misnDoneList( lua_State *L );
133static int playerL_evtActive( lua_State *L );
134static int playerL_evtDone( lua_State *L );
135static int playerL_evtDoneList( lua_State *L );
136/* Handle GUI. */
137static int playerL_gui( lua_State *L );
138static int playerL_guiList( lua_State *L );
139static int playerL_guiSet( lua_State *L );
140/* Fleet stuff. */
141static int playerL_fleetList( lua_State *L );
142static int playerL_fleetCargoFree( lua_State *L );
143static int playerL_fleetCargoMissionFree( lua_State *L );
144static int playerL_fleetCargoUsed( lua_State *L );
145static int playerL_fleetCargoOwned( lua_State *L );
146static int playerL_fleetCargoAdd( lua_State *L );
147static int playerL_fleetCargoRm( lua_State *L );
148static int playerL_fleetCargoJet( lua_State *L );
149static int playerL_fleetCargoList( lua_State *L );
150/* Inventory. */
151static int playerL_inventory( lua_State *L );
152static int playerL_inventoryAdd( lua_State *L );
153static int playerL_inventoryRm( lua_State *L );
154static int playerL_inventoryOwned( lua_State *L );
155/* Misc stuff. */
156static int playerL_teleport( lua_State *L );
157static int playerL_dt_mod( lua_State *L );
158static int playerL_fleetCapacity( lua_State *L );
159static int playerL_fleetCapacitySet( lua_State *L );
160static int playerL_chapter( lua_State *L );
161static int playerL_chapterSet( lua_State *L );
162static int playerL_infoButtonRegister( lua_State *L );
163static int playerL_infoButtonUnregister( lua_State *L );
164static int playerL_canDiscover( lua_State *L );
165static int playerL_save( lua_State *L );
166static int playerL_saveBackup( lua_State *L );
167static int playerL_gameover( lua_State *L );
168static int playerL_start( lua_State *L );
169
170static const luaL_Reg playerL_methods[] = {
171 { "name", playerL_getname },
172 { "ship", playerL_shipname },
173 { "pay", playerL_pay },
174 { "credits", playerL_credits },
175 { "wealth", playerL_wealth },
176 { "msg", playerL_msg },
177 { "msgClear", playerL_msgClear },
178 { "msgToggle", playerL_msgToggle },
179 { "omsgAdd", playerL_omsgAdd },
180 { "omsgChange", playerL_omsgChange },
181 { "omsgRm", playerL_omsgRm },
182 { "allowSave", playerL_allowSave },
183 { "pos", playerL_getPosition },
184 { "pilot", playerL_getPilot },
185 { "jumps", playerL_jumps },
186 { "fuel", playerL_fuel },
187 { "refuel", playerL_refuel },
188 { "autonav", playerL_autonav },
189 { "autonavSetSpeed", playerL_autonavSetSpeed },
190 { "autonavSetTarget", playerL_autonavSetTarget },
191 { "autonavSetPos", playerL_autonavSetPos },
192 { "autonavRoute", playerL_autonavRoute },
193 { "autonavDest", playerL_autonavDest },
194 { "autonavAbort", playerL_autonavAbort },
195 { "autonavReset", playerL_autonavReset },
196 { "autonavEnd", playerL_autonavEnd },
197 { "dt_default", playerL_dt_default },
198 { "speed", playerL_speed },
199 { "setSpeed", playerL_setSpeed },
200 { "cinematics", playerL_cinematics },
201 { "cinematicsCheck", playerL_cinematicsCheck },
202 { "damageSPFX", playerL_damageSPFX },
203 { "screenshot", playerL_screenshot },
204 { "tryBoard", playerL_tryBoard },
205 { "unboard", playerL_unboard },
206 { "isLanded", playerL_isLanded },
207 { "takeoff", playerL_takeoff },
208 { "tryLand", playerL_tryLand },
209 { "land", playerL_land },
210 { "landAllow", playerL_landAllow },
211 { "landWindow", playerL_landWindow },
212 { "commOpen", playerL_commOpen },
213 { "commClose", playerL_commClose },
214 { "shipvarPeek", playerL_shipvarPeek },
215 { "shipvarPush", playerL_shipvarPush },
216 { "shipvarPop", playerL_shipvarPop },
217 { "ships", playerL_ships },
218 { "shipOutfits", playerL_shipOutfits },
219 { "shipMetadata", playerL_shipMetadata },
220 { "shipDeploy", playerL_shipDeploy },
221 { "outfits", playerL_outfits },
222 { "outfitNum", playerL_outfitNum },
223 { "outfitAdd", playerL_outfitAdd },
224 { "outfitRm", playerL_outfitRm },
225 { "shipAdd", playerL_shipAdd },
226 { "shipSwap", playerL_shipSwap },
227 { "missions", playerL_missions },
228 { "misnActive", playerL_misnActive },
229 { "misnDone", playerL_misnDone },
230 { "misnDoneList", playerL_misnDoneList },
231 { "evtActive", playerL_evtActive },
232 { "evtDone", playerL_evtDone },
233 { "evtDoneList", playerL_evtDoneList },
234 { "gui", playerL_gui },
235 { "guiList", playerL_guiList },
236 { "guiSet", playerL_guiSet },
237 { "fleetList", playerL_fleetList },
238 { "fleetCargoFree", playerL_fleetCargoFree },
239 { "fleetCargoMissionFree", playerL_fleetCargoMissionFree },
240 { "fleetCargoUsed", playerL_fleetCargoUsed },
241 { "fleetCargoOwned", playerL_fleetCargoOwned },
242 { "fleetCargoAdd", playerL_fleetCargoAdd },
243 { "fleetCargoRm", playerL_fleetCargoRm },
244 { "fleetCargoJet", playerL_fleetCargoJet },
245 { "fleetCargoList", playerL_fleetCargoList },
246 { "inventory", playerL_inventory },
247 { "inventoryAdd", playerL_inventoryAdd },
248 { "inventoryRm", playerL_inventoryRm },
249 { "inventoryOwned", playerL_inventoryOwned },
250 { "teleport", playerL_teleport },
251 { "dt_mod", playerL_dt_mod },
252 { "fleetCapacity", playerL_fleetCapacity },
253 { "fleetCapacitySet", playerL_fleetCapacitySet },
254 { "chapter", playerL_chapter },
255 { "chapterSet", playerL_chapterSet },
256 { "infoButtonRegister", playerL_infoButtonRegister },
257 { "infoButtonUnregister", playerL_infoButtonUnregister },
258 { "canDiscover", playerL_canDiscover },
259 { "save", playerL_save },
260 { "saveBackup", playerL_saveBackup },
261 { "gameover", playerL_gameover },
262 { "start", playerL_start },
263 { 0, 0 } };
264
269int nlua_loadPlayer( nlua_env env )
270{
271 nlua_register( env, "player", playerL_methods, 0 );
272 return 0;
273}
274
292static int playerL_getname( lua_State *L )
293{
294 PLAYER_CHECK();
295 lua_pushstring( L, player.name );
296 return 1;
297}
298
307static int playerL_shipname( lua_State *L )
308{
309 PLAYER_CHECK();
310 lua_pushstring( L, player.p->name );
311 return 1;
312}
313
323static int playerL_pay( lua_State *L )
324{
325 PLAYER_CHECK();
326
327 credits_t money;
328 int nohooks;
329 const char *reason;
330
331 money = CLAMP( CREDITS_MIN, CREDITS_MAX,
332 (credits_t)round( luaL_checknumber( L, 1 ) ) );
333 player_modCredits( money );
334 if ( lua_isstring( L, 2 ) ) {
335 nohooks = 0;
336 reason = lua_tostring( L, 2 );
337 } else {
338 nohooks = lua_toboolean( L, 2 );
339 reason = NULL;
340 }
341
342 if ( !nohooks ) {
343 HookParam p[3];
344 p[0].type = HOOK_PARAM_NUMBER;
345 p[0].u.num = (double)money;
346 if ( reason != NULL ) {
347 p[1].type = HOOK_PARAM_STRING;
348 p[1].u.str = reason;
349 } else {
350 p[1].type = HOOK_PARAM_NIL;
351 }
352 p[2].type = HOOK_PARAM_SENTINEL;
353 hooks_runParam( "pay", p );
354 }
355
356 return 0;
357}
358
371static int playerL_credits( lua_State *L )
372{
373 credits_t creds = ( player.p == NULL ) ? 0 : player.p->credits;
374
375 /* Parse parameters. */
376 int decimals = luaL_optinteger( L, 1, -1 );
377
378 /* Push return. */
379 lua_pushnumber( L, creds );
380 if ( decimals >= 0 ) {
381 char buf[ECON_CRED_STRLEN];
382 credits2str( buf, creds, decimals );
383 lua_pushstring( L, buf );
384 return 2;
385 }
386 return 1;
387}
388
402static int playerL_wealth( lua_State *L )
403{
404 credits_t wealth;
405 /* Parse parameters. */
406 int decimals = luaL_optinteger( L, 1, -1 );
407
408 if ( player.p != NULL ) {
409 const PlayerShip_t *ps = player_getShipStack();
410 const PlayerOutfit_t *po = player_getOutfits();
411 wealth = player.p->credits + pilot_worth( player.p, 0 );
412
413 /* Compute total wealth. */
414 for ( int i = 0; i < array_size( ps ); i++ )
415 wealth += pilot_worth( ps[i].p, 0 );
416 for ( int i = 0; i < array_size( po ); i++ )
417 wealth += po[i].q * po[i].o->price;
418 } else
419 wealth = 0;
420
421 /* Push return. */
422 lua_pushnumber( L, wealth );
423 if ( decimals >= 0 ) {
424 char buf[ECON_CRED_STRLEN];
425 credits2str( buf, wealth, decimals );
426 lua_pushstring( L, buf );
427 return 2;
428 }
429 return 1;
430}
431
440static int playerL_msg( lua_State *L )
441{
442 PLAYER_CHECK();
443
444 const char *str = luaL_checkstring( L, 1 );
445 int display = lua_toboolean( L, 2 );
446 player_messageRaw( str );
447 if ( display )
448 pilot_setCommMsg( player.p, str );
449
450 return 0;
451}
452
457static int playerL_msgClear( lua_State *L )
458{
459 (void)L;
460 PLAYER_CHECK();
462 return 0;
463}
464
471static int playerL_msgToggle( lua_State *L )
472{
473 PLAYER_CHECK();
474 player_messageToggle( lua_toboolean( L, 1 ) );
475 return 0;
476}
477
490static int playerL_omsgAdd( lua_State *L )
491{
492 const char *str;
493 double duration;
494 unsigned int id;
495 int fontsize;
496 const glColour *col;
497
498 PLAYER_CHECK();
499
500 /* Input. */
501 str = luaL_checkstring( L, 1 );
502 duration = luaL_optnumber( L, 2, 10. );
503 fontsize = luaL_optinteger( L, 3, OMSG_FONT_DEFAULT_SIZE );
504 col = luaL_optcolour( L, 4, &cWhite );
505
506 /* Infinity. */
507 if ( duration < DOUBLE_TOL )
508 duration = INFINITY;
509
510 /* Output. */
511 id = omsg_add( str, duration, fontsize, col );
512 lua_pushnumber( L, id );
513 return 1;
514}
515
526static int playerL_omsgChange( lua_State *L )
527{
528 const char *str;
529 double duration;
530 unsigned int id;
531 int ret;
532
533 PLAYER_CHECK();
534
535 /* Input. */
536 id = luaL_checklong( L, 1 );
537 str = luaL_checkstring( L, 2 );
538 duration = luaL_checknumber( L, 3 );
539
540 /* Infinity. */
541 if ( duration < DOUBLE_TOL )
542 duration = INFINITY;
543
544 /* Output. */
545 ret = omsg_change( id, str, duration );
546 lua_pushboolean( L, !ret );
547 return 1;
548}
549
556static int playerL_omsgRm( lua_State *L )
557{
558 PLAYER_CHECK();
559 unsigned int id = luaL_checklong( L, 1 );
560 omsg_rm( id );
561 return 0;
562}
563
571static int playerL_allowSave( lua_State *L )
572{
573 PLAYER_CHECK();
574 unsigned int b;
575 if ( lua_gettop( L ) == 0 )
576 b = 1;
577 else
578 b = lua_toboolean( L, 1 );
579
580 if ( b )
581 player_rmFlag( PLAYER_NOSAVE );
582 else
583 player_setFlag( PLAYER_NOSAVE );
584 return 0;
585}
586
595static int playerL_getPosition( lua_State *L )
596{
597 PLAYER_CHECK();
598 lua_pushvector( L, player.p->solid.pos );
599 return 1;
600}
601
608static int playerL_getPilot( lua_State *L )
609{
610 /* No need to run check here or stuff that depends on player.pilot() working
611 * will fail. */
612 lua_pushpilot( L, PLAYER_ID );
613 return 1;
614}
615
624static int playerL_jumps( lua_State *L )
625{
626 if ( player.p == NULL )
627 lua_pushnumber( L, 0 );
628 else
629 lua_pushnumber( L, pilot_getJumps( player.p ) );
630 return 1;
631}
632
642static int playerL_fuel( lua_State *L )
643{
644 if ( player.p == NULL ) {
645 lua_pushnumber( L, 0 );
646 lua_pushnumber( L, 0 );
647 } else {
648 lua_pushnumber( L, player.p->fuel );
649 lua_pushnumber( L, player.p->fuel_consumption );
650 }
651 return 2;
652}
653
663static int playerL_refuel( lua_State *L )
664{
665 PLAYER_CHECK();
666
667 if ( lua_gettop( L ) > 0 ) {
668 double f = luaL_checknumber( L, 1 );
669 player.p->fuel += f;
670 } else
671 player.p->fuel = player.p->fuel_max;
672
673 /* Make sure value is valid. */
674 player.p->fuel = CLAMP( 0, player.p->fuel_max, player.p->fuel );
675
676 return 0;
677}
678
687static int playerL_autonav( lua_State *L )
688{
689 PLAYER_CHECK();
690 lua_pushboolean( L, player_isFlag( PLAYER_AUTONAV ) );
691 lua_pushnumber( L, player.speed_autonav );
692 return 2;
693}
694
698static int playerL_autonavSetSpeed( lua_State *L )
699{
700 double speed = luaL_optnumber( L, 1, -1 );
701 double sound = luaL_optnumber( L, 2, speed );
702
703 if ( speed > 0. ) {
704 pause_setSpeed( speed * conf.game_speed );
705 sound_setSpeed( sound );
706 player.speed_autonav = speed;
707 } else {
709 }
710
711 return 0;
712}
713
722static int playerL_autonavSetTarget( lua_State *L )
723{
724 PLAYER_CHECK();
725 const StarSystem *sys = luaL_validsystem( L, 1 );
726 if ( pilot_isFlag( player.p, PILOT_HYP_PREP ) ||
727 pilot_isFlag( player.p, PILOT_HYP_BEGIN ) ||
728 pilot_isFlag( player.p, PILOT_HYPERSPACE ) ) {
729 WARN( _( "Unable to set autonav target while jumping!" ) );
730 return 0;
731 }
732 map_select( sys, lua_toboolean( L, 2 ) );
733 return 0;
734}
735
743static int playerL_autonavSetPos( lua_State *L )
744{
745 PLAYER_CHECK();
746 const vec2 *pos = luaL_optvector( L, 1, NULL );
747 if ( pos == NULL )
748 ovr_autonavClear();
749 else
750 ovr_autonavPos( pos->x, pos->y );
751 return 0;
752}
753
763static int playerL_autonavDest( lua_State *L )
764{
765 PLAYER_CHECK();
766 LuaSystem ls;
767 const StarSystem *dest;
768 int jumps;
769
770 /* Get destination. */
771 dest = map_getDestination( &jumps );
772 if ( dest == NULL )
773 return 0;
774
775 ls = system_index( dest );
776 lua_pushsystem( L, ls );
777 lua_pushnumber( L, jumps );
778 return 2;
779}
780
789static int playerL_autonavRoute( lua_State *L )
790{
791 PLAYER_CHECK();
792 StarSystem *const *path = map_getRoute();
793 lua_newtable( L );
794 for ( int i = 0; i < array_size( path ); i++ ) {
795 LuaSystem ls = system_index( path[i] );
796 lua_pushsystem( L, ls );
797 lua_rawseti( L, -2, i + 1 );
798 }
799 return 1;
800}
801
812static int playerL_autonavAbort( lua_State *L )
813{
814 PLAYER_CHECK();
815 const char *str = luaL_optstring( L, 1, NULL );
816 player_autonavAbort( str );
817 return 0;
818}
819
829static int playerL_autonavReset( lua_State *L )
830{
831 PLAYER_CHECK();
832 double timer = luaL_optnumber( L, 1, 0. );
833 player_autonavReset( timer );
834 return 0;
835}
836
844static int playerL_autonavEnd( lua_State *L )
845{
846 PLAYER_CHECK();
847 (void)L;
849 return 0;
850}
851
858static int playerL_dt_default( lua_State *L )
859{
860 lua_pushnumber( L, player_dt_default() );
861 return 1;
862}
863
870static int playerL_speed( lua_State *L )
871{
872 lua_pushnumber( L, player.speed / conf.game_speed );
873 return 1;
874}
875
886static int playerL_setSpeed( lua_State *L )
887{
888 double speed = luaL_optnumber( L, 1, -1 );
889 double sound = luaL_optnumber( L, 2, speed );
890
891 if ( speed > 0. ) {
892 player.speed = speed * conf.game_speed * player.speed_autonav;
893 pause_setSpeed( player.speed );
894 sound_setSpeed( sound );
895 } else {
896 player.speed = conf.game_speed * player.speed_autonav;
898 }
899
900 return 0;
901}
902
922static int playerL_cinematics( lua_State *L )
923{
924 int b, f_gui, f_2x;
925 const char *abort_msg;
926 double speed;
927
928 /* Defaults. */
929 abort_msg = NULL;
930 f_gui = 0;
931 f_2x = 0;
932 speed = 1.;
933
934 /* Parse parameters. */
935 b = lua_toboolean( L, 1 );
936 if ( !lua_isnoneornil( L, 2 ) ) {
937 if ( !lua_istable( L, 2 ) )
938 return NLUA_ERROR( L, _( "Second parameter to cinematics should be a "
939 "table of options or omitted!" ) );
940
941 lua_getfield( L, 2, "abort" );
942 if ( !lua_isnil( L, -1 ) )
943 abort_msg = luaL_checkstring( L, -1 );
944 lua_pop( L, 1 );
945
946 lua_getfield( L, 2, "gui" );
947 f_gui = lua_toboolean( L, -1 );
948 lua_pop( L, 1 );
949
950 lua_getfield( L, 2, "no2x" );
951 f_2x = lua_toboolean( L, -1 );
952 lua_pop( L, 1 );
953
954 lua_getfield( L, 2, "speed" );
955 speed = luaL_optnumber( L, -1, 1. );
956 lua_pop( L, 1 );
957 }
958
959 if ( b ) {
960 /* Reset speeds. This will override the player's ship base speed. */
961 player.speed = speed;
962 sound_setSpeed( speed );
963 pause_setSpeed( speed );
964
965 /* Get rid of stuff that could be bothersome. */
966 player_autonavAbort( abort_msg );
967 ovr_setOpen( 0 );
968
969 /* Handle options. */
970 player_setFlag( PLAYER_CINEMATICS );
971 if ( !f_gui )
972 player_setFlag( PLAYER_CINEMATICS_GUI );
973 if ( f_2x )
974 player_setFlag( PLAYER_CINEMATICS_2X );
975
976 /* Redo viewport. */
977 gl_setDefViewport( 0., 0., gl_screen.nw, gl_screen.nh );
978 } else {
979 /* Reset speed properly to player speed. */
981
982 /* Clean up flags. */
983 player_rmFlag( PLAYER_CINEMATICS );
984 player_rmFlag( PLAYER_CINEMATICS_GUI );
985 player_rmFlag( PLAYER_CINEMATICS_2X );
986
987 /* Reload GUI. */
988 gui_reload();
989 }
990
991 return 0;
992}
993
997static int playerL_cinematicsCheck( lua_State *L )
998{
999 lua_pushboolean( L, player_isFlag( PLAYER_CINEMATICS ) );
1000 return 1;
1001}
1002
1011static int playerL_damageSPFX( lua_State *L )
1012{
1013 double spfx_mod = luaL_checknumber( L, 1 );
1014 spfx_shake( spfx_mod );
1015 spfx_damage( spfx_mod );
1016 return 0;
1017}
1018
1024static int playerL_screenshot( lua_State *L )
1025{
1026 (void)L;
1028 return 0;
1029}
1030
1039static int playerL_tryBoard( lua_State *L )
1040{
1041 int ret = player_tryBoard( lua_toboolean( L, 1 ) );
1042 switch ( ret ) {
1043 case PLAYER_BOARD_IMPOSSIBLE:
1044 lua_pushstring( L, "impossible" );
1045 break;
1046 case PLAYER_BOARD_RETRY:
1047 lua_pushstring( L, "retry" );
1048 break;
1049 case PLAYER_BOARD_OK:
1050 lua_pushstring( L, "ok" );
1051 break;
1052 default:
1053 lua_pushstring( L, "error" );
1054 break;
1055 }
1056 return 1;
1057}
1058
1068static int playerL_unboard( lua_State *L )
1069{
1070 (void)L;
1071 board_unboard();
1072 return 0;
1073}
1074
1081static int playerL_isLanded( lua_State *L )
1082{
1083 lua_pushboolean( L, landed );
1084 return 1;
1085}
1086
1095static int playerL_takeoff( lua_State *L )
1096{
1097 PLAYER_CHECK();
1098
1099 if ( !landed )
1100 return NLUA_ERROR( L, _( "Player must be landed to force takeoff." ) );
1101 if ( !pilot_isSpaceworthy( player.p ) )
1102 return NLUA_ERROR( L,
1103 _( "Player must be spaceworthy to force takeoff!" ) );
1104
1106
1107 return 0;
1108}
1109
1118static int playerL_tryLand( lua_State *L )
1119{
1120 int ret = player_land( lua_toboolean( L, 1 ) );
1121 switch ( ret ) {
1122 case PLAYER_LAND_DENIED:
1123 lua_pushstring( L, "impossible" );
1124 break;
1125 case PLAYER_LAND_OK:
1126 lua_pushstring( L, "ok" );
1127 break;
1128 case PLAYER_LAND_AGAIN:
1129 lua_pushstring( L, "retry" );
1130 break;
1131 default:
1132 lua_pushstring( L, "error" );
1133 break;
1134 }
1135 return 1;
1136}
1137
1147static int playerL_land( lua_State *L )
1148{
1149 PLAYER_CHECK();
1150
1151 Spob *spob = luaL_validspob( L, 1 );
1152 const char *sysname = spob_getSystemName( spob->name );
1153 if ( sysname == NULL )
1154 return NLUA_ERROR( L, _( "Spob '%s' is not in a system!" ), spob->name );
1155
1156 /* Unboard just in case. */
1157 board_unboard();
1158
1159 if ( strcmp( sysname, cur_system->name ) != 0 ) {
1160 /* Refer to playerL_teleport for the voodoo that happens here. */
1161 pilot_rmFlag( player.p, PILOT_HYPERSPACE );
1162 pilot_rmFlag( player.p, PILOT_HYP_BEGIN );
1163 pilot_rmFlag( player.p, PILOT_HYP_BRAKE );
1164 pilot_rmFlag( player.p, PILOT_HYP_PREP );
1167
1169
1171
1172 space_init( sysname, 0 );
1173
1174 ovr_initAlpha();
1175 }
1176 player.p->solid.pos = spob->pos; /* Set position to target. */
1177
1178 /* End autonav. */
1180
1181 /* Stop afterburning. */
1183 /* Stop accelerating. */
1185 /* Stop stealth. */
1187
1188 /* Stop all on outfits. */
1189 if ( pilot_outfitOffAll( player.p ) > 0 )
1191
1192 /* Do whatever the spob wants to do. */
1193 if ( spob->lua_land != LUA_NOREF ) {
1194 spob_luaInitMem( spob );
1195 lua_rawgeti( naevL, LUA_REGISTRYINDEX, spob->lua_land ); /* f */
1196 lua_pushspob( naevL, spob_index( spob ) );
1197 lua_pushpilot( naevL, player.p->id );
1198 if ( nlua_pcall( spob->lua_env, 2, 0 ) ) {
1199 NLUA_WARN( L, _( "Spob '%s' failed to run '%s':\n%s" ), spob->name,
1200 "land", lua_tostring( naevL, -1 ) );
1201 lua_pop( naevL, 1 );
1202 }
1203
1204 return 0;
1205 }
1206
1207 space_queueLand( spob );
1208 return 0;
1209}
1210
1227static int playerL_landAllow( lua_State *L )
1228{
1229 int b;
1230 const char *str = NULL;
1231
1232 if ( lua_gettop( L ) > 0 ) {
1233 b = lua_toboolean( L, 1 );
1234 if ( lua_isstring( L, 2 ) )
1235 str = lua_tostring( L, 2 );
1236 } else
1237 b = 1;
1238
1239 if ( b )
1240 player_rmFlag( PLAYER_NOLAND );
1241 else {
1242 player_setFlag( PLAYER_NOLAND );
1243 player_nolandMsg( str );
1244 }
1245 return 0;
1246}
1247
1265static int playerL_landWindow( lua_State *L )
1266{
1267 int ret;
1268 const char *str;
1269 int win;
1270
1271 if ( !landed )
1272 return NLUA_ERROR( L,
1273 _( "Must be landed to set the active land window." ) );
1274
1275 str = luaL_checkstring( L, 1 );
1276 if ( strcasecmp( str, "main" ) == 0 )
1277 win = LAND_WINDOW_MAIN;
1278 else if ( strcasecmp( str, "bar" ) == 0 )
1279 win = LAND_WINDOW_BAR;
1280 else if ( strcasecmp( str, "missions" ) == 0 )
1281 win = LAND_WINDOW_MISSION;
1282 else if ( strcasecmp( str, "outfits" ) == 0 )
1283 win = LAND_WINDOW_OUTFITS;
1284 else if ( strcasecmp( str, "shipyard" ) == 0 )
1285 win = LAND_WINDOW_SHIPYARD;
1286 else if ( strcasecmp( str, "equipment" ) == 0 )
1287 win = LAND_WINDOW_EQUIPMENT;
1288 else if ( strcasecmp( str, "commodity" ) == 0 )
1289 win = LAND_WINDOW_COMMODITY;
1290 else
1291 NLUA_INVALID_PARAMETER( L, 1 );
1292
1293 /* Sets the window. */
1294 ret = land_setWindow( win );
1295
1296 lua_pushboolean( L, !ret );
1297 return 1;
1298}
1299
1309static int playerL_commOpen( lua_State *L )
1310{
1311 if ( lua_ispilot( L, 1 ) ) {
1312 const Pilot *p = luaL_validpilot( L, 1 );
1313 int ret = comm_openPilot( p->id );
1314 lua_pushboolean( L, ret == 0 );
1315 return 1;
1316 } else if ( lua_isspob( L, 1 ) ) {
1317 Spob *spb = luaL_validspob( L, 1 );
1318 int ret = comm_openSpob( spb );
1319 lua_pushboolean( L, ret == 0 );
1320 return 1;
1321 } else
1322 NLUA_INVALID_PARAMETER( L, 1 );
1323 return 0;
1324}
1325
1331static int playerL_commClose( lua_State *L )
1332{
1333 (void)L;
1335 return 0;
1336}
1337
1338static PlayerShip_t *playerL_shipvarShip( lua_State *L, int idx )
1339{
1340 if ( lua_isnoneornil( L, idx ) )
1341 return &player.ps;
1342 return player_getPlayerShip( luaL_checkstring( L, idx ) );
1343}
1344
1356static int playerL_shipvarPeek( lua_State *L )
1357{
1358 PLAYER_CHECK();
1359 const char *str = luaL_checkstring( L, 1 );
1360 PlayerShip_t *ps = playerL_shipvarShip( L, 2 );
1361 const lvar *var = lvar_get( ps->p->shipvar, str );
1362 if ( var != NULL )
1363 return lvar_push( L, var );
1364 return 0;
1365}
1366
1376static int playerL_shipvarPush( lua_State *L )
1377{
1378 PLAYER_CHECK();
1379 const char *str = luaL_checkstring( L, 1 );
1380 lvar var = lvar_tovar( L, str, 2 );
1381 PlayerShip_t *ps = playerL_shipvarShip( L, 3 );
1382 if ( ps->p->shipvar == NULL )
1383 ps->p->shipvar = array_create( lvar );
1384 lvar_addArray( &ps->p->shipvar, &var, 1 );
1385 return 0;
1386}
1387
1396static int playerL_shipvarPop( lua_State *L )
1397{
1398 PLAYER_CHECK();
1399 const char *str = luaL_checkstring( L, 1 );
1400 PlayerShip_t *ps = playerL_shipvarShip( L, 2 );
1401 lvar *var = lvar_get( ps->p->shipvar, str );
1402 if ( var != NULL )
1403 lvar_rmArray( &ps->p->shipvar, var );
1404 return 0;
1405}
1406
1415static int playerL_ships( lua_State *L )
1416{
1417 if ( player.p == NULL ) {
1418 lua_newtable( L );
1419 return 1;
1420 }
1421
1422 const PlayerShip_t *ships = player_getShipStack();
1423 lua_newtable( L ); /* t */
1424 for ( int i = 0; i < array_size( ships ); i++ ) {
1425 lua_newtable( L ); /* t, k, t */
1426
1427 lua_pushstring( L, ships[i].p->name ); /* t, k, t, s */
1428 lua_setfield( L, -2, "name" ); /* t, k, t */
1429
1430 lua_pushship( L, ships[i].p->ship ); /* t, k, t, s */
1431 lua_setfield( L, -2, "ship" ); /* t, k, t */
1432
1433 lua_pushboolean( L, ships[i].deployed ); /* t, k, t, s */
1434 lua_setfield( L, -2, "deployed" ); /* t, k, t */
1435
1436 lua_rawseti( L, -2, i + 1 ); /* t */
1437 }
1438 return 1;
1439}
1440
1451static int playerL_shipOutfits( lua_State *L )
1452{
1453 if ( player.p == NULL ) {
1454 lua_newtable( L );
1455 return 1;
1456 }
1457
1458 const char *str;
1459 int j;
1460 const PlayerShip_t *ships;
1461 Pilot *p;
1462
1463 /* Get name. */
1464 str = luaL_optstring( L, 1, NULL );
1465 ships = player_getShipStack();
1466
1467 /* Get outfit. */
1468 lua_newtable( L );
1469
1470 p = NULL;
1471 if ( ( str == NULL ) || ( strcmp( str, player.p->name ) == 0 ) )
1472 p = player.p;
1473 else {
1474 for ( int i = 0; i < array_size( ships ); i++ ) {
1475 if ( strcmp( str, ships[i].p->name ) == 0 ) {
1476 p = ships[i].p;
1477 break;
1478 }
1479 }
1480 }
1481
1482 if ( p == NULL )
1483 return NLUA_ERROR( L, _( "Player does not own a ship named '%s'" ), str );
1484
1485 lua_newtable( L );
1486 j = 1;
1487 for ( int i = 0; i < array_size( p->outfits ); i++ ) {
1488 if ( p->outfits[i]->outfit == NULL )
1489 continue;
1490
1491 /* Set the outfit. */
1492 lua_pushoutfit( L, p->outfits[i]->outfit );
1493 lua_rawseti( L, -2, j++ );
1494 }
1495
1496 return 1;
1497}
1498
1507static int playerL_shipMetadata( lua_State *L )
1508{
1509 PLAYER_CHECK();
1510
1511 int destroyed = 0;
1512 const char *str = luaL_optstring( L, 1, NULL );
1513 const PlayerShip_t *ships = player_getShipStack();
1514 const PlayerShip_t *ps = NULL;
1515 if ( ( str == NULL ) || ( strcmp( str, player.p->name ) == 0 ) )
1516 ps = &player.ps;
1517 else {
1518 for ( int i = 0; i < array_size( ships ); i++ ) {
1519 if ( strcmp( str, ships[i].p->name ) == 0 ) {
1520 ps = &ships[i];
1521 break;
1522 }
1523 }
1524 }
1525 if ( ps == NULL )
1526 return NLUA_ERROR( L, _( "Player does not own a ship named '%s'" ), str );
1527
1528 lua_newtable( L );
1529
1530 lua_pushnumber( L, ps->time_played );
1531 lua_setfield( L, -2, "time_played" );
1532
1533 lua_pushstring( L, ps->acquired );
1534 lua_setfield( L, -2, "acquired" );
1535
1536 lua_pushtime( L, ps->acquired_date );
1537 lua_setfield( L, -2, "acquired_date" );
1538
1539 lua_pushnumber( L, ps->dmg_done_shield );
1540 lua_setfield( L, -2, "dmg_done_shield" );
1541
1542 lua_pushnumber( L, ps->dmg_done_armour );
1543 lua_setfield( L, -2, "dmg_done_armour" );
1544
1545 lua_pushnumber( L, ps->dmg_done_shield + ps->dmg_done_armour );
1546 lua_setfield( L, -2, "dmg_done" );
1547
1548 lua_pushnumber( L, ps->dmg_taken_shield );
1549 lua_setfield( L, -2, "dmg_taken_shield" );
1550
1551 lua_pushnumber( L, ps->dmg_taken_armour );
1552 lua_setfield( L, -2, "dmg_taken_armour" );
1553
1554 lua_pushnumber( L, ps->dmg_taken_shield + ps->dmg_taken_armour );
1555 lua_setfield( L, -2, "dmg_taken" );
1556
1557 lua_pushinteger( L, ps->jumped_times );
1558 lua_setfield( L, -2, "jumped_times" );
1559
1560 lua_pushinteger( L, ps->landed_times );
1561 lua_setfield( L, -2, "landed_times" );
1562
1563 lua_pushinteger( L, ps->death_counter );
1564 lua_setfield( L, -2, "death_counter" );
1565
1566 for ( int i = 0; i < SHIP_CLASS_TOTAL; i++ )
1567 destroyed += ps->ships_destroyed[i];
1568 lua_pushinteger( L, destroyed );
1569 lua_setfield( L, -2, "ships_destroyed" );
1570
1571 return 1;
1572}
1573
1582static int playerL_shipDeploy( lua_State *L )
1583{
1584 PLAYER_CHECK();
1585 const char *shipname = luaL_checkstring( L, 1 );
1586 int deploy = lua_toboolean( L, 2 );
1587 PlayerShip_t *ps = player_getPlayerShip( shipname );
1588 ps->deployed = deploy;
1589 return 0;
1590}
1591
1604static int playerL_outfits( lua_State *L )
1605{
1606 if ( player.p == NULL ) {
1607 lua_newtable( L );
1608 return 1;
1609 }
1610
1611 const PlayerOutfit_t *outfits = player_getOutfits();
1612 int unequipped_only = lua_toboolean( L, 1 );
1613 const PlayerShip_t *pstack = player_getShipStack();
1614 int n = 1;
1615 /* Set up and get owned outfits. */
1616 lua_newtable( L );
1617 for ( int i = 0; i < array_size( outfits ); i++ ) {
1618 lua_pushoutfit( L, outfits[i].o );
1619 lua_rawseti( L, -2, n++ );
1620 }
1621 /* Add all outfits on player ships. */
1622 if ( !unequipped_only ) {
1623 for ( int i = 0; i < array_size( pstack ); i++ ) {
1624 for ( int j = 0; j < array_size( pstack[i].p->outfits ); j++ ) {
1625 const Outfit *o = pstack[i].p->outfits[j]->outfit;
1626 int found = 0;
1627 for ( int k = 0; k < array_size( outfits ); k++ ) {
1628 if ( outfits[k].o == o ) {
1629 found = 1;
1630 break;
1631 }
1632 }
1633 if ( found ) {
1634 lua_pushoutfit( L, o );
1635 lua_rawseti( L, -2, n++ );
1636 }
1637 }
1638 }
1639 }
1640 return 1;
1641}
1642
1656static int playerL_outfitNum( lua_State *L )
1657{
1658 if ( player.p == NULL ) {
1659 lua_pushnumber( L, 0. );
1660 return 1;
1661 }
1662
1663 const Outfit *o;
1664 int q, unequipped_only;
1665
1666 /* Handle parameters. */
1667 o = luaL_validoutfit( L, 1 );
1668 unequipped_only = lua_toboolean( L, 2 );
1669
1670 /* Count the outfit. */
1671 if ( unequipped_only )
1672 q = player_outfitOwned( o );
1673 else
1674 q = player_outfitOwnedTotal( o );
1675 lua_pushnumber( L, q );
1676
1677 return 1;
1678}
1679
1690static int playerL_outfitAdd( lua_State *L )
1691{
1692 PLAYER_CHECK();
1693
1694 /* Handle parameters. */
1695 const Outfit *o = luaL_validoutfit( L, 1 );
1696 int q = luaL_optinteger( L, 2, 1 );
1697
1698 /* Add the outfits. */
1699 player_addOutfit( o, q );
1700
1701 /* Update equipment list. */
1703
1704 return 0;
1705}
1706
1718static int playerL_outfitRm( lua_State *L )
1719{
1720 const Outfit *o;
1721 int q = luaL_optinteger( L, 2, 1 );
1722
1723 /* Handle special case it's "all". */
1724 if ( lua_isstring( L, 1 ) ) {
1725 const char *str = luaL_checkstring( L, 1 );
1726
1727 if ( strcmp( str, "all" ) == 0 ) {
1728 const PlayerOutfit_t *poutfits = player_getOutfits();
1729 const Outfit **outfits =
1730 array_create_size( const Outfit *, array_size( poutfits ) );
1731 for ( int i = 0; i < array_size( poutfits ); i++ )
1732 array_push_back( &outfits, poutfits[i].o );
1733
1734 for ( int i = 0; i < array_size( outfits ); i++ ) {
1735 o = outfits[i];
1736 q = player_outfitOwned( o );
1737 player_rmOutfit( o, q );
1738 }
1739 /* Clean up. */
1740 array_free( outfits );
1741
1742 /* Update equipment list. */
1744 return 0;
1745 }
1746 }
1747
1748 /* Usual case. */
1749 o = luaL_validoutfit( L, 1 );
1750 player_rmOutfit( o, q );
1751
1752 /* Update equipment list. */
1754
1755 return 0;
1756}
1757
1777static int playerL_shipAdd( lua_State *L )
1778{
1779 PlayerShip_t *new_ship;
1780 /* Handle parameters. */
1781 const Ship *s = luaL_validship( L, 1 );
1782 const char *name = luaL_optstring( L, 2, _( s->name ) );
1783 const char *acquired = luaL_optstring( L, 3, NULL );
1784 int noname = lua_toboolean( L, 4 );
1785 /* Add the ship, look in case it's cancelled. */
1786 do {
1787 new_ship = player_newShip( s, name, 0, acquired, noname );
1788 } while ( new_ship == NULL );
1789 /* Return the new name. */
1790 lua_pushstring( L, new_ship->p->name );
1791 return 1;
1792}
1793
1810static int playerL_shipSwap( lua_State *L )
1811{
1812 PLAYER_CHECK();
1813
1814 const char *str = luaL_checkstring( L, 1 );
1815 int ignore_cargo = lua_toboolean( L, 2 );
1816 const char *cur = player.p->name;
1817 player_swapShip( str, !ignore_cargo );
1818 if ( lua_toboolean( L, 3 ) )
1819 player_rmShip( cur );
1820
1821 return 0;
1822}
1823
1834static int playerL_missions( lua_State *L )
1835{
1836 if ( player.p == NULL ) {
1837 lua_newtable( L );
1838 return 1;
1839 }
1840 int j = 1;
1841 lua_newtable( L );
1842 for ( int i = 0; i < array_size( player_missions ); i++ ) {
1843 const Mission *pm = player_missions[i];
1844 const MissionData *md = pm->data;
1845 if ( pm->id == 0 )
1846 continue;
1847
1848 misn_pushMissionData( L, md );
1849 /* Add player mission-specific data. */
1850 if ( pm->title != NULL ) {
1851 lua_pushstring( L, pm->title );
1852 lua_setfield( L, -2, "title" );
1853 }
1854 if ( pm->desc != NULL ) {
1855 lua_pushstring( L, pm->desc );
1856 lua_setfield( L, -2, "desc" );
1857 }
1858 if ( pm->reward != NULL ) {
1859 lua_pushstring( L, pm->reward );
1860 lua_setfield( L, -2, "reward" );
1861 }
1862 lua_pushinteger( L, md->avail.chance );
1863 lua_setfield( L, -2, "chance" );
1864 lua_pushinteger( L, md->avail.priority );
1865 lua_setfield( L, -2, "priority" );
1866 if ( md->avail.chapter != NULL ) {
1867 lua_pushstring( L, md->avail.chapter );
1868 lua_setfield( L, -2, "chapter" );
1869 }
1870 if ( md->avail.cond != NULL ) {
1871 lua_pushstring( L, md->avail.cond );
1872 lua_setfield( L, -2, "cond" );
1873 }
1874 if ( md->avail.done != NULL ) {
1875 lua_pushstring( L, md->avail.done );
1876 lua_setfield( L, -2, "done" );
1877 }
1878 if ( md->avail.spob != NULL ) {
1879 lua_pushstring( L, md->avail.spob );
1880 lua_setfield( L, -2, "spob" );
1881 }
1882 if ( md->avail.system != NULL ) {
1883 lua_pushstring( L, md->avail.system );
1884 lua_setfield( L, -2, "system" );
1885 }
1886 if ( mis_isFlag( md, MISSION_UNIQUE ) ) {
1887 lua_pushboolean( L, 1 );
1888 lua_setfield( L, -2, "unique" );
1889 }
1890 /* Tags. */
1891 lua_newtable( L );
1892 for ( int k = 0; k < array_size( md->tags ); k++ ) {
1893 lua_pushboolean( L, 1 );
1894 lua_setfield( L, -2, md->tags[k] );
1895 }
1896 lua_setfield( L, -2, "tags" );
1897 lua_rawseti( L, -2, j++ );
1898 }
1899 return 1;
1900}
1901
1913static int playerL_misnActive( lua_State *L )
1914{
1915 PLAYER_CHECK();
1916 const char *str = luaL_checkstring( L, 1 );
1917 const MissionData *misn = mission_getFromName( str );
1918 if ( misn == NULL )
1919 return NLUA_ERROR( L, _( "Mission '%s' not found in stack" ), str );
1920 int n = mission_alreadyRunning( misn );
1921 if ( n > 0 )
1922 lua_pushinteger( L, n );
1923 else
1924 lua_pushboolean( L, 0 );
1925 return 1;
1926}
1927
1939static int playerL_misnDone( lua_State *L )
1940{
1941 PLAYER_CHECK();
1942 const char *str = luaL_checkstring( L, 1 );
1943 int id = mission_getID( str );
1944 if ( id == -1 )
1945 return NLUA_ERROR( L, _( "Mission '%s' not found in stack" ), str );
1946 lua_pushboolean( L, player_missionAlreadyDone( id ) );
1947 return 1;
1948}
1949
1956static int playerL_misnDoneList( lua_State *L )
1957{
1958 if ( player.p == NULL ) {
1959 lua_newtable( L );
1960 return 1;
1961 }
1962 const int *done = player_missionsDoneList();
1963 lua_newtable( L );
1964 for ( int i = 0; i < array_size( done ); i++ ) {
1965 mission_toLuaTable( L, mission_get( done[i] ) );
1966 lua_rawseti( L, -2, i + 1 );
1967 }
1968 return 1;
1969}
1970
1981static int playerL_evtActive( lua_State *L )
1982{
1983 PLAYER_CHECK();
1984 const char *str = luaL_checkstring( L, 1 );
1985 int evtid = event_dataID( str );
1986 if ( evtid < 0 )
1987 return NLUA_ERROR( L, _( "Event '%s' not found in stack" ), str );
1988 lua_pushboolean( L, event_alreadyRunning( evtid ) );
1989 return 1;
1990}
1991
2002static int playerL_evtDone( lua_State *L )
2003{
2004 PLAYER_CHECK();
2005 const char *str = luaL_checkstring( L, 1 );
2006 int id = event_dataID( str );
2007 if ( id == -1 )
2008 return NLUA_ERROR( L, _( "Event '%s' not found in stack" ), str );
2009 lua_pushboolean( L, player_eventAlreadyDone( id ) );
2010 return 1;
2011}
2012
2019static int playerL_evtDoneList( lua_State *L )
2020{
2021 if ( player.p == NULL ) {
2022 lua_newtable( L );
2023 return 1;
2024 }
2025 const int *done = player_eventsDoneList();
2026 lua_newtable( L );
2027 for ( int i = 0; i < array_size( done ); i++ ) {
2028 event_toLuaTable( L, done[i] );
2029 lua_rawseti( L, -2, i + 1 );
2030 }
2031 return 1;
2032}
2033
2040static int playerL_gui( lua_State *L )
2041{
2042 lua_pushstring( L, gui_pick() );
2043 return 1;
2044}
2045
2053static int playerL_guiList( lua_State *L )
2054{
2055 const char **gui = player_guiList();
2056
2057 lua_newtable( L );
2058 for ( int i = 0; i < array_size( gui ); i++ ) {
2059 lua_pushstring( L, gui[i] );
2060 lua_rawseti( L, -2, i + 1 );
2061 }
2062
2063 return 1;
2064}
2065
2072static int playerL_guiSet( lua_State *L )
2073{
2074 const char *name = luaL_checkstring( L, 1 );
2075 if ( !gui_exists( name ) )
2076 return NLUA_ERROR( L, _( "GUI '%s' does not exist!" ), name );
2077 free( player.gui );
2078 player.gui = strdup( name );
2079 gui_load( gui_pick() );
2080 return 0;
2081}
2082
2090static int playerL_fleetList( lua_State *L )
2091{
2092 if ( player.p == NULL ) {
2093 lua_newtable( L );
2094 return 1;
2095 }
2096 int n = 1;
2097 const PlayerShip_t *pstack = player_getShipStack();
2098 lua_newtable( L );
2099 for ( int i = 0; i < array_size( pstack ); i++ ) {
2100 const PlayerShip_t *ps = &pstack[i];
2101 if ( !ps->deployed )
2102 continue;
2103 /* We can avoid the spaceWorthy check as they will be set to false due to
2104 * not having a pilot. */
2105 /*if (!!pilot_isSpaceworthy(ps->p))
2106 continue;*/
2107
2108 if ( ps->p == NULL )
2109 lua_pushboolean( L, 0 );
2110 else
2111 lua_pushpilot( L, ps->p->id );
2112 lua_rawseti( L, -2, n++ );
2113 }
2114 return 1;
2115}
2116
2123static int playerL_fleetCargoFree( lua_State *L )
2124{
2125 if ( player.p == NULL )
2126 lua_pushinteger( L, 0 );
2127 else
2128 lua_pushinteger( L, pfleet_cargoFree() );
2129 return 1;
2130}
2131
2140static int playerL_fleetCargoMissionFree( lua_State *L )
2141{
2142 if ( player.p == NULL ) {
2143 lua_pushinteger( L, 0 );
2144 return 0;
2145 }
2146 lua_pushinteger( L, pfleet_cargoMissionFree() );
2147 return 1;
2148}
2149
2156static int playerL_fleetCargoUsed( lua_State *L )
2157{
2158 if ( player.p == NULL )
2159 lua_pushinteger( L, 0 );
2160 else
2161 lua_pushinteger( L, pfleet_cargoUsed() );
2162 return 1;
2163}
2164
2174static int playerL_fleetCargoOwned( lua_State *L )
2175{
2176 const Commodity *c = luaL_validcommodity( L, 1 );
2177 if ( player.p == NULL )
2178 lua_pushinteger( L, 0 );
2179 else
2180 lua_pushinteger( L, pfleet_cargoOwned( c ) );
2181 return 1;
2182}
2183
2192static int playerL_fleetCargoAdd( lua_State *L )
2193{
2194 const Commodity *c = luaL_validcommodity( L, 1 );
2195 int q = luaL_checkinteger( L, 2 );
2196 if ( player.p == NULL )
2197 lua_pushinteger( L, 0 );
2198 else
2199 lua_pushinteger( L, pfleet_cargoAdd( c, q ) );
2200 return 1;
2201}
2202
2211static int playerL_fleetCargoRm( lua_State *L )
2212{
2213 const Commodity *c = luaL_validcommodity( L, 1 );
2214 int q = luaL_checkinteger( L, 2 );
2215 if ( player.p == NULL )
2216 lua_pushinteger( L, 0 );
2217 else
2218 lua_pushinteger( L, pfleet_cargoRm( c, q, 0 ) );
2219 return 1;
2220}
2221
2231static int playerL_fleetCargoJet( lua_State *L )
2232{
2233 const Commodity *c = luaL_validcommodity( L, 1 );
2234 int q = luaL_checkinteger( L, 2 );
2235 if ( player.p == NULL )
2236 lua_pushinteger( L, 0 );
2237 else
2238 lua_pushinteger( L, pfleet_cargoRm( c, q, 1 ) );
2239 return 1;
2240}
2241
2251static int playerL_fleetCargoList( lua_State *L )
2252{
2253 if ( player.p == NULL ) {
2254 lua_newtable( L );
2255 return 1;
2256 }
2257 Commodity *call = commodity_getAll();
2258 int n = 0;
2259 lua_newtable( L ); /* t */
2260 for ( int i = 0; i < array_size( call ); i++ ) {
2261 Commodity *c = &call[i];
2262 int q = pfleet_cargoOwned( c );
2263 if ( q <= 0 )
2264 continue;
2265
2266 lua_newtable( L ); /* t, t */
2267
2268 lua_pushcommodity( L, c ); /* t, t, c */
2269 lua_setfield( L, -2, "c" ); /* t, t */
2270
2271 lua_pushinteger( L, q ); /* t, t, q */
2272 lua_setfield( L, -2, "q" ); /* t, t */
2273
2274 lua_rawseti( L, -2, ++n ); /* t */
2275 }
2276 return 1;
2277}
2278
2288static int playerL_inventory( lua_State *L )
2289{
2290 if ( player.p == NULL ) {
2291 lua_newtable( L );
2292 return 1;
2293 }
2294 const PlayerItem *inv = player_inventory();
2295 lua_newtable( L );
2296 for ( int i = 0; i < array_size( inv ); i++ ) {
2297 const PlayerItem *pi = &inv[i];
2298 lua_newtable( L );
2299
2300 lua_pushstring( L, pi->name );
2301 lua_setfield( L, -2, "name" );
2302
2303 lua_pushinteger( L, pi->quantity );
2304 lua_setfield( L, -2, "quantity" );
2305
2306 lua_rawseti( L, -2, i + 1 );
2307 }
2308 return 1;
2309}
2310
2319static int playerL_inventoryAdd( lua_State *L )
2320{
2321 const char *name = luaL_checkstring( L, 1 );
2322 int q = luaL_optinteger( L, 2, 1 );
2323 if ( player.p == NULL )
2324 lua_pushinteger( L, 0 );
2325 else
2326 lua_pushinteger( L, player_inventoryAdd( name, q ) );
2327 return 1;
2328}
2329
2338static int playerL_inventoryRm( lua_State *L )
2339{
2340 const char *name = luaL_checkstring( L, 1 );
2341 int q = luaL_optinteger( L, 2, 1 );
2342 if ( player.p == NULL )
2343 lua_pushinteger( L, 0 );
2344 else
2345 lua_pushinteger( L, player_inventoryRemove( name, q ) );
2346 return 1;
2347}
2348
2356static int playerL_inventoryOwned( lua_State *L )
2357{
2358 const char *name = luaL_checkstring( L, 1 );
2359 if ( player.p == NULL )
2360 lua_pushinteger( L, 0 );
2361 else
2362 lua_pushinteger( L, player_inventoryAmount( name ) );
2363 return 1;
2364}
2365
2389static int playerL_teleport( lua_State *L )
2390{
2391 PLAYER_CHECK();
2392 Spob *pnt;
2393 const char *name, *pntname;
2394 int no_simulate, silent;
2395
2396 /* Must not be landed. */
2397 if ( landed )
2398 return NLUA_ERROR( L, _( "Can not teleport the player while landed!" ) );
2399 if ( comm_isOpen() )
2400 return NLUA_ERROR(
2401 L, _( "Can not teleport the player while the comm is open!" ) );
2402 if ( player_isBoarded() )
2403 return NLUA_ERROR(
2404 L, _( "Can not teleport the player while they are boarded!" ) );
2405 pnt = NULL;
2406
2407 pilot_rmFlag( player.p, PILOT_LANDING );
2408
2409 /* Get a system. */
2410 if ( lua_issystem( L, 1 ) ) {
2411 const StarSystem *sys = luaL_validsystem( L, 1 );
2412 name = system_getIndex( sys->id )->name;
2413 }
2414 /* Get a spob. */
2415 else if ( lua_isspob( L, 1 ) ) {
2416 pnt = luaL_validspob( L, 1 );
2417 name = spob_getSystemName( pnt->name );
2418 if ( name == NULL )
2419 return NLUA_ERROR( L, _( "Spob '%s' does not belong to a system." ),
2420 pnt->name );
2421 }
2422 /* Get destination from string. */
2423 else if ( lua_isstring( L, 1 ) ) {
2424 const char *sysname;
2425 name = lua_tostring( L, 1 );
2426 sysname = system_existsCase( name );
2427 if ( sysname == NULL ) {
2428 /* No system found, assume destination string is the name of a spob. */
2429 pntname = name;
2430 name = spob_getSystemName( pntname );
2431 pnt = spob_get( pntname );
2432 if ( pnt == NULL )
2433 return NLUA_ERROR(
2434 L, _( "'%s' is not a valid teleportation target." ), name );
2435
2436 if ( name == NULL )
2437 return NLUA_ERROR( L, _( "Spob '%s' does not belong to a system." ),
2438 pntname );
2439 } else
2440 name = sysname;
2441 } else
2442 NLUA_INVALID_PARAMETER( L, 1 );
2443
2444 no_simulate = lua_toboolean( L, 2 );
2445 silent = lua_toboolean( L, 3 );
2446
2447 /* Check if system exists. */
2448 if ( system_get( name ) == NULL )
2449 return NLUA_ERROR( L, _( "System '%s' does not exist." ), name );
2450
2451 /* Unboard just in case. */
2452 board_unboard();
2453
2454 /* Jump out hook is run first. */
2455 hooks_run( "jumpout" );
2456
2457 /* Just in case remove hyperspace flags. */
2458 pilot_rmFlag( player.p, PILOT_HYPERSPACE );
2459 pilot_rmFlag( player.p, PILOT_HYP_BEGIN );
2460 pilot_rmFlag( player.p, PILOT_HYP_BRAKE );
2461 pilot_rmFlag( player.p, PILOT_HYP_PREP );
2462 /* Don't follow anything. */
2465 cam_vel( 0., 0. );
2466
2467 /* Free graphics. */
2469
2470 /* Reset targets when teleporting.
2471 * Both of these functions invoke gui_setNav(), which updates jump and
2472 * spob targets simultaneously. Thus, invalid reads may arise and the
2473 * target reset must be done prior to calling space_init and destroying
2474 * the old system. */
2476
2477 /* Hide messages if not needed. */
2478 if ( silent )
2479 player_messageToggle( 0 ); /* space_init will reset it, so no need to. */
2480
2481 /* Go to the new system. */
2482 space_init( name, !no_simulate );
2483
2484 /* Map gets deformed when jumping this way. */
2485 map_clear();
2486
2487 /* Initialize alpha as needed. */
2488 ovr_initAlpha();
2489
2490 /* Run hooks - order is important. */
2492 hooks_run( "jumpin" );
2493 hooks_run( "enter" );
2494 events_trigger( EVENT_TRIGGER_ENTER );
2495 missions_run( MIS_AVAIL_ENTER, -1, NULL, NULL );
2496
2497 /* Move to spob. */
2498 if ( pnt != NULL )
2499 player.p->solid.pos = pnt->pos;
2500
2501 /* Move all escorts to new position. */
2502 Pilot *const *pilot_stack = pilot_getAll();
2503 for ( int i = 0; i < array_size( pilot_stack ); i++ ) {
2504 Pilot *p = pilot_stack[i];
2505 if ( p->parent == PLAYER_ID ) {
2506 memcpy( &p->solid.pos, &player.p->solid.pos, sizeof( vec2 ) );
2507 vec2_padd( &p->solid.pos, 200. + 200. * RNGF(), 2. * M_PI * RNGF() );
2508
2509 /* Clean up trails. */
2510 pilot_clearTrails( p );
2511 }
2512 }
2513
2514 return 0;
2515}
2516
2523static int playerL_dt_mod( lua_State *L )
2524{
2525 lua_pushnumber( L, dt_mod );
2526 return 1;
2527}
2528
2537static int playerL_fleetCapacity( lua_State *L )
2538{
2539 PLAYER_CHECK();
2540 int nships = 0;
2541 const PlayerShip_t *pships;
2542 pfleet_update();
2543 lua_pushnumber( L, player.fleet_capacity );
2544 lua_pushnumber( L, player.fleet_used );
2545 pships = player_getShipStack();
2546 for ( int i = 0; i < array_size( pships ); i++ ) {
2547 if ( !pships[i].deployed )
2548 continue;
2549 nships++;
2550 }
2551 lua_pushboolean( L, ( nships == 0 ) ||
2552 ( player.fleet_used <= player.fleet_capacity ) );
2553 return 3;
2554}
2555
2562static int playerL_fleetCapacitySet( lua_State *L )
2563{
2564 PLAYER_CHECK();
2565 player.fleet_capacity = luaL_checkinteger( L, 1 );
2566 return 0;
2567}
2568
2575static int playerL_chapter( lua_State *L )
2576{
2577 PLAYER_CHECK();
2578 lua_pushstring( L, player.chapter );
2579 return 1;
2580}
2581
2588static int playerL_chapterSet( lua_State *L )
2589{
2590 PLAYER_CHECK();
2591 const char *str = luaL_checkstring( L, 1 );
2592 free( player.chapter );
2593 player.chapter = strdup( str );
2594 return 0;
2595}
2596
2609static int playerL_infoButtonRegister( lua_State *L )
2610{
2611 int id;
2612 const char *caption = luaL_checkstring( L, 1 );
2613 int priority = luaL_optinteger( L, 3, 5 );
2614 const char *key = luaL_optstring( L, 4, "" );
2615 luaL_checktype( L, 2, LUA_TFUNCTION );
2616 lua_pushvalue( L, 2 );
2617 id = info_buttonRegister( caption, priority, SDL_GetKeyFromName( key ) );
2618 lua_pushinteger( L, id );
2619 return 1;
2620}
2621
2628static int playerL_infoButtonUnregister( lua_State *L )
2629{
2630 int id = luaL_checkinteger( L, 1 );
2631 int ret = info_buttonUnregister( id );
2632 if ( ret != 0 )
2633 NLUA_WARN( L, _( "Failed to unregister info button with id '%d'!" ), id );
2634 return 0;
2635}
2636
2644static int playerL_canDiscover( lua_State *L )
2645{
2646 player.discover_off = !lua_toboolean( L, 1 );
2647 return 0;
2648}
2649
2658static int playerL_save( lua_State *L )
2659{
2660 const char *savename = luaL_optstring( L, 1, "autosave" );
2661 Spob *savespob = NULL;
2662 Spob *prevspob = NULL;
2663 if ( !lua_isnoneornil( L, 2 ) )
2664 savespob = luaL_validspob( L, 2 );
2665
2666 if ( !landed && ( savespob == NULL ) )
2667 return NLUA_ERROR( L, _( "Unable to save when not landed and land spob "
2668 "is not specified!" ) );
2669 else if ( landed && ( savespob != NULL ) )
2670 return NLUA_ERROR( L, _( "Unable to save when landed and land_spob does "
2671 "not match landed spob!" ) );
2672
2673 if ( savespob != NULL ) {
2674 prevspob = land_spob;
2675 land_spob = savespob;
2676 }
2677 lua_pushboolean( L, save_all_with_name( savename ) );
2678 if ( savespob != NULL )
2679 land_spob = prevspob;
2680
2681 return 1;
2682}
2683
2691static int playerL_saveBackup( lua_State *L )
2692{
2693 char file[PATH_MAX], backup[PATH_MAX];
2694 const char *filename =
2695 luaL_checkstring( L, 1 ); /* TODO sanitize path and such. */
2696 if ( strcmp( filename, "autosave" ) == 0 )
2697 return NLUA_ERROR( L, _( "Can not back up save to 'autosave'." ) );
2698 snprintf( file, sizeof( file ), "saves/%s/autosave.ns", player.name );
2699 snprintf( backup, sizeof( backup ), "saves/%s/%s.ns", player.name,
2700 filename );
2701 lua_pushboolean( L, ndata_copyIfExists( file, backup ) );
2702 return 1;
2703}
2704
2710static int playerL_gameover( lua_State *L )
2711{
2712 (void)L;
2713 player_setFlag( PLAYER_DESTROYED );
2714 menu_death();
2715 return 0;
2716}
2717
2728static int playerL_start( lua_State *L )
2729{
2730 double x, y;
2731 vec2 v;
2732 lua_newtable( L );
2733
2734 lua_pushstring( L, start_name() );
2735 lua_setfield( L, -2, "name" );
2736
2737 lua_pushship( L, ship_get( start_ship() ) );
2738 lua_setfield( L, -2, "ship" );
2739
2740 lua_pushstring( L, start_shipname() );
2741 lua_setfield( L, -2, "shipname" );
2742
2743 lua_pushstring( L, start_acquired() );
2744 lua_setfield( L, -2, "acquired" );
2745
2746 lua_pushstring( L, start_gui() );
2747 lua_setfield( L, -2, "gui" );
2748
2749 lua_pushinteger( L, start_credits() );
2750 lua_setfield( L, -2, "credits" );
2751
2752 lua_pushtime( L, start_date() );
2753 lua_setfield( L, -2, "date" );
2754
2756 lua_setfield( L, -2, "system" );
2757
2758 start_position( &x, &y );
2759 vec2_cset( &v, x, y );
2760 lua_pushvector( L, v );
2761 lua_setfield( L, -2, "position" );
2762
2763 if ( start_mission() != NULL ) {
2764 lua_pushstring( L, start_mission() );
2765 lua_setfield( L, -2, "mission" );
2766 }
2767
2768 if ( start_event() != NULL ) {
2769 lua_pushstring( L, start_event() );
2770 lua_setfield( L, -2, "event" );
2771 }
2772
2773 lua_pushstring( L, start_chapter() );
2774 lua_setfield( L, -2, "chapter" );
2775
2776 lua_pushstring( L, start_spob_lua_default() );
2777 lua_setfield( L, -2, "spob_lua_default" );
2778
2779 lua_pushstring( L, start_dtype_default() );
2780 lua_setfield( L, -2, "dtype_default" );
2781
2782 return 1;
2783}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
#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:179
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition array.h:134
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
int player_tryBoard(int noisy)
Attempt to board the player's target.
Definition board.c:152
int player_isBoarded(void)
Gets if the player is boarded.
Definition board.c:38
void board_unboard(void)
Forces unboarding of the pilot.
Definition board.c:210
void cam_vel(double vx, double vy)
Sets the camera velocity.
Definition camera.c:149
int comm_openPilot(unsigned int pilot)
Opens the communication dialogue with a pilot.
Definition comm.c:60
int comm_openSpob(Spob *spob)
Opens a communication dialogue with a spob.
Definition comm.c:187
int comm_isOpen(void)
Check to see if the comm window is open.
Definition comm.c:41
void comm_queueClose(void)
Queues a close command when possible.
Definition comm.c:49
Commodity * commodity_getAll(void)
Gets all the commodities.
Definition commodity.c:140
void credits2str(char *str, credits_t credits, int decimals)
Converts credits to a usable string for displaying.
Definition commodity.c:75
int event_alreadyRunning(int data)
Check to see if an event is already running.
Definition event.c:302
int event_dataID(const char *evdata)
Gets the event data id from name.
Definition event.c:766
void events_trigger(EventTrigger_t trigger)
Runs all the events matching a trigger.
Definition event.c:319
void gui_clearMessages(void)
Clears the GUI messages.
Definition gui.c:1037
const char * gui_pick(void)
Determines which GUI should be used.
Definition gui.c:1933
int gui_exists(const char *name)
Checks to see if a GUI exists.
Definition gui.c:1952
int gui_load(const char *name)
Attempts to load the actual GUI.
Definition gui.c:1965
void gui_reload(void)
Reloads the GUI.
Definition gui.c:1845
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:1029
int hooks_run(const char *stack)
Runs all the hooks of stack.
Definition hook.c:1049
Handles the info menu.
void land_queueTakeoff(void)
Queue a takeoff.
Definition land.c:159
int land_setWindow(int window)
Sets the land window tab.
Definition land.c:1399
int landed
Definition land.c:78
Spob * land_spob
Definition land.c:87
void outfits_updateEquipmentOutfits(void)
Updates the outfitter and equipment outfit image arrays.
int lvar_addArray(lvar **arr, const 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(const lvar *arr, const char *str)
Gets a lua var by name.
Definition lvar.c:41
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:86
int lvar_push(lua_State *L, const lvar *v)
Pushes a lua var to a lua state.
Definition lvar.c:56
Handles the important game menus.
void menu_death(void)
Player death menu, appears when player got creamed.
Definition menu.c:633
Mission ** player_missions
Definition mission.c:45
const MissionData * mission_getFromName(const char *name)
Gets mission data from a name.
Definition mission.c:125
int mission_alreadyRunning(const MissionData *misn)
Checks to see if mission is already running.
Definition mission.c:229
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:326
int mission_getID(const char *name)
Gets id from mission name.
Definition mission.c:99
const MissionData * mission_get(int id)
Gets a MissionData based on ID.
Definition mission.c:115
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition naev.h:41
#define PATH_MAX
Definition naev.h:57
int ndata_copyIfExists(const char *file1, const char *file2)
Copy a file, if it exists.
Definition ndata.c:358
lua_State * naevL
Definition nlua.c:54
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.
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition nlua_pilot.c:576
Pilot * luaL_validpilot(lua_State *L, int ind)
Makes sure the pilot is valid or raises a Lua error.
Definition nlua_pilot.c:560
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
Definition nlua_pilot.c:591
static int playerL_shipDeploy(lua_State *L)
Sets the deployed status of a player's ship.
static int playerL_outfitAdd(lua_State *L)
Adds an outfit to the player's outfit list.
static int playerL_evtActive(lua_State *L)
Checks to see if the player has an event active.
static int playerL_shipvarPush(lua_State *L)
Pushes a ship variable.
static int playerL_shipMetadata(lua_State *L)
Gets meta-data about one of the player's ships.
static int playerL_unboard(lua_State *L)
Unboards the player from its boarded target.
static int playerL_fleetCargoAdd(lua_State *L)
Tries to add an amount of commodity to the player's fleet.
static int playerL_fleetCapacitySet(lua_State *L)
Sets the fleet capacity of the player.
static int playerL_fleetCargoRm(lua_State *L)
Tries to remove an amount of commodity to the player's fleet.
static int playerL_missions(lua_State *L)
Gets the list of the player's active missions.
static int playerL_inventoryRm(lua_State *L)
Removes an item to the player's in-game inventory.
static int playerL_msg(lua_State *L)
Sends the player an in-game message.
static int playerL_cinematicsCheck(lua_State *L)
Checks to see if the game is in cinematics mode.
static int playerL_inventory(lua_State *L)
Gets the contents of the player's inventory.
static int playerL_autonavSetPos(lua_State *L)
Indicates the player where their autonav target position is.
static int playerL_shipSwap(lua_State *L)
Swaps the player's current ship with a ship they own by name.
static int playerL_credits(lua_State *L)
Gets how many credits the player has on him.
static int playerL_outfits(lua_State *L)
Gets all the outfits the player owns.
static int playerL_saveBackup(lua_State *L)
Backs up the player's last autosave with a custom name.
static int playerL_dt_default(lua_State *L)
Gets the current time modifier for the player.
static int playerL_pay(lua_State *L)
Pays the player an amount of money.
static int playerL_shipname(lua_State *L)
Gets the player's ship's name (given by the player).
static int playerL_autonavEnd(lua_State *L)
Ends the autonav system. You probably want to use player.autonavAbort instead of this.
static int playerL_setSpeed(lua_State *L)
Sets the game speed directly.
static int playerL_fuel(lua_State *L)
Gets the amount of fuel a player has.
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...
static int playerL_autonavSetSpeed(lua_State *L)
Sets the autonav speed.
static int playerL_isLanded(lua_State *L)
Checks to see if the player is landed or not.
static int playerL_autonavSetTarget(lua_State *L)
Set the autonav target system to travel to.
static int playerL_autonavRoute(lua_State *L)
Gets the player's autonav route.
static int playerL_infoButtonUnregister(lua_State *L)
Unregisters a button in the info window.
static int playerL_landAllow(lua_State *L)
Allows or disallows the player to land.
static int playerL_damageSPFX(lua_State *L)
Applies the damage effects to the player.
static int playerL_ships(lua_State *L)
Gets the names of the player's ships.
static int playerL_evtDoneList(lua_State *L)
Gets a list of all the events the player has done.
static int playerL_takeoff(lua_State *L)
Forces the player to take off if they are landed.
static int playerL_cinematics(lua_State *L)
Puts the game in cinematics mode or back to regular mode.
static int playerL_fleetCapacity(lua_State *L)
Gets the fleet capacity (and used) of the player.
static int playerL_inventoryAdd(lua_State *L)
Adds an item to the player's in-game inventory.
static int playerL_commClose(lua_State *L)
Forces the player to close comm if they are chatting.
static int playerL_autonavAbort(lua_State *L)
Stops the players autonav if active.
static int playerL_speed(lua_State *L)
Gets the current speed of the player.
static int playerL_gui(lua_State *L)
Gets the player's current GUI.
static int playerL_jumps(lua_State *L)
Gets a player's jump range based on their remaining fuel.
static int playerL_fleetCargoJet(lua_State *L)
Tries to remove an amount of commodity to the player's fleet and jettisons it into space.
static const luaL_Reg playerL_methods[]
static int playerL_landWindow(lua_State *L)
Sets the active land window.
static int playerL_outfitRm(lua_State *L)
Removes an outfit from the player's outfit list.
static int playerL_refuel(lua_State *L)
Refuels the player.
static int playerL_tryLand(lua_State *L)
Tries to make the player land.
static int playerL_msgToggle(lua_State *L)
Clears the player's message buffer.
static int playerL_dt_mod(lua_State *L)
Gets the dt_mod of the player, which multiplies all time stuff.
static int playerL_tryBoard(lua_State *L)
Tries to make the player board their target.
static int playerL_teleport(lua_State *L)
Teleports the player to a new spob or system (only if not landed).
static int playerL_getPilot(lua_State *L)
Gets the player's associated pilot.
int nlua_loadPlayer(nlua_env env)
Loads the player Lua library.
static int playerL_chapterSet(lua_State *L)
Sets the player's current chapter.
static int playerL_infoButtonRegister(lua_State *L)
Registers a button in the info window.
static int playerL_shipvarPeek(lua_State *L)
Peeks at a ship variable.
static int playerL_omsgRm(lua_State *L)
Removes an overlay message.
static int playerL_omsgChange(lua_State *L)
Changes an overlay message.
static int playerL_start(lua_State *L)
Gets information about the player's starting point.
static int playerL_fleetList(lua_State *L)
Lists the ships in the player's fleet.
static int playerL_misnDoneList(lua_State *L)
Gets a list of all the missions the player has done.
static int playerL_fleetCargoList(lua_State *L)
Gets the list of all the cargos in the player's fleet.
static int playerL_chapter(lua_State *L)
Gets the player's current chapter.
static int playerL_getname(lua_State *L)
Lua bindings to interact with the player.
static int playerL_omsgAdd(lua_State *L)
Adds an overlay message.
static int playerL_fleetCargoUsed(lua_State *L)
Gets the amount of cargo space used in the player's fleet.
static int playerL_fleetCargoFree(lua_State *L)
Gets the amount of cargo space free in the player's fleet.
static int playerL_outfitNum(lua_State *L)
Gets the number of outfits the player owns in their list (excludes equipped on ships).
static int playerL_inventoryOwned(lua_State *L)
Checks to see how much of an item the player has in their inventory.
static int playerL_shipAdd(lua_State *L)
Gives the player a new ship.
static int playerL_allowSave(lua_State *L)
Sets player save ability.
static int playerL_guiList(lua_State *L)
Lists the GUIs the player can use.
static int playerL_guiSet(lua_State *L)
Sets the player's GUI.
static int playerL_autonavReset(lua_State *L)
Resets the game speed without disabling autonav.
static int playerL_evtDone(lua_State *L)
Checks to see if player has done an event.
static int playerL_getPosition(lua_State *L)
Gets the player's position.
static int playerL_land(lua_State *L)
Automagically lands the player on a spob.
static int playerL_fleetCargoOwned(lua_State *L)
Gets the amount of cargo space used by a specific commodity in the player's fleet.
static int playerL_misnActive(lua_State *L)
Checks to see if the player has a mission active.
static int playerL_wealth(lua_State *L)
Gets how many credits the player owns both directly, and in the form of assets (ships,...
static int playerL_autonav(lua_State *L)
Checks to see if the player has autonav enabled.
static int playerL_msgClear(lua_State *L)
Clears the player's message buffer.
static int playerL_misnDone(lua_State *L)
Checks to see if player has done a mission.
static int playerL_commOpen(lua_State *L)
Opens communication with a pilot or spob.
static int playerL_save(lua_State *L)
Saves the game.
static int playerL_autonavDest(lua_State *L)
Gets the player's long term autonav destination.
static int playerL_shipvarPop(lua_State *L)
Pops a ship variable.
static int playerL_screenshot(lua_State *L)
Takes a screenshot (same as the keyboard action).
static int playerL_fleetCargoMissionFree(lua_State *L)
Gets the free space for mission cargos on the player's ship. This can be less than the amount of free...
static int playerL_gameover(lua_State *L)
Gives the player a game over message.
static int playerL_shipOutfits(lua_State *L)
Gets the outfits for one of the player's ships.
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship on the stack.
Definition nlua_ship.c:193
const Ship * luaL_validship(lua_State *L, int ind)
Makes sure the ship is valid or raises a Lua error.
Definition nlua_ship.c:168
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
Definition nlua_spob.c:203
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
Definition nlua_spob.c:177
int lua_isspob(lua_State *L, int ind)
Checks to see if ind is a spob.
Definition nlua_spob.c:218
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
Definition nlua_time.c:123
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
void gl_setDefViewport(int x, int y, int w, int h)
Sets the default viewport.
Definition opengl.c:743
glInfo gl_screen
Definition opengl.c:47
void pause_setSpeed(double mod)
Adjusts the game's dt modifier.
Definition pause.c:61
double dt_mod
Definition pause.c:20
void pilot_setCommMsg(Pilot *p, const char *s)
Sets the overhead communication message of the pilot.
Definition pilot.c:1137
static Pilot ** pilot_stack
Definition pilot.c:51
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition pilot.c:93
credits_t pilot_worth(const Pilot *p, int count_unique)
Gets the price or worth of a pilot in credits.
Definition pilot.c:4482
int pilot_getJumps(const Pilot *p)
Gets the amount of jumps the pilot has left.
Definition pilot.c:1317
void pilot_clearTrails(Pilot *p)
Resets the trails for a pilot.
Definition pilot.c:3722
void pilot_destealth(Pilot *p)
Destealths a pilot.
Definition pilot_ew.c:585
void pilot_outfitLOnjumpin(Pilot *pilot)
Runs Lua outfits when pilot jumps into a system.
void pilot_calcStats(Pilot *pilot)
Recalculates the pilot's stats based on his outfits.
int pilot_isSpaceworthy(const Pilot *p)
Pilot safety check - makes sure stats are safe.
void pilot_afterburnOver(Pilot *p)
Deactivates the afterburner.
int pilot_outfitOffAll(Pilot *p)
Disables all active outfits for a pilot.
int player_eventAlreadyDone(int id)
Checks to see if player has already completed a event.
Definition player.c:3180
int player_rmOutfit(const Outfit *o, int quantity)
Remove an outfit from the player's outfit stack.
Definition player.c:3053
void player_swapShip(const char *shipname, int move_cargo)
Swaps player's current ship with their ship named shipname.
Definition player.c:590
int player_addOutfit(const Outfit *o, int quantity)
Adds an outfit to the player outfit stack.
Definition player.c:2988
void player_resetSpeed(void)
Resets the player speed stuff.
Definition player.c:1521
const PlayerShip_t * player_getShipStack(void)
Gets the array (array.h) of the player's ships.
Definition player.c:2804
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:470
void player_rmShip(const char *shipname)
Removes one of the player's ships.
Definition player.c:775
int player_outfitOwnedTotal(const Outfit *o)
Definition player.c:2912
PlayerShip_t * player_getPlayerShip(const char *shipname)
Gets a specific ship.
Definition player.c:2863
double player_dt_default(void)
Returns the player's total default time delta based on time dilation stuff.
Definition player.c:2001
int player_missionAlreadyDone(int id)
Checks to see if player has already completed a mission.
Definition player.c:3125
credits_t player_modCredits(credits_t amount)
Modifies the amount of credits the player has.
Definition player.c:1047
int * player_eventsDoneList(void)
Gets a list of all the events the player has done.
Definition player.c:3196
void player_accelOver(void)
Done accelerating.
Definition player.c:2220
int player_land(int loud)
Try to land or target closest spob if no land target.
Definition player.c:1653
Player_t player
Definition player.c:77
int player_outfitOwned(const Outfit *o)
Gets how many of the outfit the player owns.
Definition player.c:2882
void player_targetClearAll(void)
Clears all player targets: hyperspace, spob, asteroid, etc...
Definition player.c:2336
void player_nolandMsg(const char *str)
Sets the no land message.
Definition player.c:1822
const PlayerOutfit_t * player_getOutfits(void)
Gets an array (array.h) of the player's outfits.
Definition player.c:2940
void player_screenshot(void)
Takes a screenshot.
Definition player.c:2415
int * player_missionsDoneList(void)
Gets a list of all the missions the player has done.
Definition player.c:3141
void player_autonavEnd(void)
Ends the autonav.
void player_autonavReset(double s)
Resets the game speed without disabling autonav.
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.
int pfleet_cargoOwned(const Commodity *com)
Gets the total amount of a commodity type owned by the player's fleet.
int pfleet_cargoAdd(const Commodity *com, int q)
Adds some cargo to the player's fleet.
int pfleet_cargoRm(const Commodity *com, int q, int jet)
Removes some cargo from the player's fleet.
int pfleet_cargoMissionFree(void)
Gets the free mission cargo space in the player's fleet.
int pfleet_cargoUsed(void)
Gets the total cargo space used by the player's fleet.
void pfleet_update(void)
Updates the used fleet capacity of the player.
const char ** player_guiList(void)
Gets the list of GUIs.
Definition player_gui.c:103
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:256
int save_all_with_name(const char *name)
Saves the current game.
Definition save.c:108
const Ship * ship_get(const char *name)
Gets a ship based on its name.
Definition ship.c:99
void sound_setSpeed(double s)
Sets the speed to play the sound at.
Definition sound.c:1158
void space_init(const char *sysname, int do_simulate)
Initializes the system.
Definition space.c:1620
void space_gfxUnload(StarSystem *sys)
Unloads all the graphics for a star system.
Definition space.c:2281
Spob * spob_get(const char *spobname)
Gets a spob based on its name.
Definition space.c:1107
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition space.c:1038
int spob_index(const Spob *p)
Gets the ID of a spob.
Definition space.c:1158
const char * system_existsCase(const char *sysname)
Checks to see if a system exists case insensitively.
Definition space.c:936
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:1007
StarSystem * cur_system
Definition space.c:110
const char * spob_getSystemName(const char *spobname)
Get the name of a system from a spobname.
Definition space.c:1082
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:4731
void spob_luaInitMem(const Spob *spob)
Initializes the memory fo a spob.
Definition space.c:2109
int system_index(const StarSystem *sys)
Gets the index of a star system.
Definition space.c:1049
void spfx_shake(double mod)
Increases the current rumble level.
Definition spfx.c:966
void spfx_damage(double mod)
Increases the current damage level.
Definition spfx.c:988
const char * start_acquired(void)
Gets the module's starting ship was acquired.
Definition start.c:209
const char * start_event(void)
Gets the starting event of the player.
Definition start.c:274
const char * start_mission(void)
Gets the starting mission of the player.
Definition start.c:265
void start_position(double *x, double *y)
Gets the starting position of the player.
Definition start.c:255
const char * start_chapter(void)
Gets the player's starting chapter.
Definition start.c:283
const char * start_name(void)
Gets the module name.
Definition start.c:182
const char * start_dtype_default(void)
Gets the default damage type.
Definition start.c:301
const char * start_ship(void)
Gets the module player starting ship.
Definition start.c:191
ntime_t start_date(void)
Gets the starting date.
Definition start.c:236
const char * start_spob_lua_default(void)
Gets the default spob Lua file.
Definition start.c:292
const char * start_shipname(void)
Gets the module's starting ship's name.
Definition start.c:200
const char * start_gui(void)
Gets the module's starting ship was acquired.
Definition start.c:218
const char * start_system(void)
Gets the starting system name.
Definition start.c:245
unsigned int start_credits(void)
Gets the player's starting credits.
Definition start.c:227
Represents a commodity.
Definition commodity.h:57
The actual hook parameter.
Definition hook.h:40
HookParamType type
Definition hook.h:41
char * chapter
Definition mission.h:45
char * done
Definition mission.h:53
char * spob
Definition mission.h:43
char * system
Definition mission.h:44
char * cond
Definition mission.h:51
Static mission data.
Definition mission.h:64
MissionAvail_t avail
Definition mission.h:67
char ** tags
Definition mission.h:75
Represents an active mission.
Definition mission.h:83
unsigned int id
Definition mission.h:86
char * reward
Definition mission.h:92
char * desc
Definition mission.h:91
char * title
Definition mission.h:90
const MissionData * data
Definition mission.h:84
A ship outfit, depends radically on the type.
Definition outfit.h:372
const Outfit * outfit
Definition pilot.h:149
The representation of an in-game pilot.
Definition pilot.h:263
unsigned int id
Definition pilot.h:264
lvar * shipvar
Definition pilot.h:445
PilotOutfitSlot ** outfits
Definition pilot.h:354
char * name
Definition pilot.h:265
Represents an item in the player inventory.
Wrapper for outfits.
Definition player.h:64
Player ship.
Definition player.h:72
double time_played
Definition player.h:83
double dmg_done_armour
Definition player.h:87
unsigned int death_counter
Definition player.h:94
double dmg_taken_armour
Definition player.h:89
ntime_t acquired_date
Definition player.h:85
int deployed
Definition player.h:80
double dmg_taken_shield
Definition player.h:88
unsigned int landed_times
Definition player.h:93
Pilot * p
Definition player.h:73
unsigned int ships_destroyed[SHIP_CLASS_TOTAL]
Definition player.h:90
char * acquired
Definition player.h:84
unsigned int jumped_times
Definition player.h:92
double dmg_done_shield
Definition player.h:86
Represents a space ship.
Definition ship.h:97
char * name
Definition ship.h:100
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:102
int lua_land
Definition space.h:170
nlua_env lua_env
Definition space.h:164
char * name
Definition space.h:105
vec2 pos
Definition space.h:109
Contains a mission variable.
Definition lvar.h:24
Represents a 2d vector.
Definition vec2.h:45
double y
Definition vec2.h:47
double x
Definition vec2.h:46