naev 0.12.5
nlua_naev.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 <time.h>
12
13#include "SDL_timer.h"
14
15#include "naev.h"
17
18#include "nlua_naev.h"
19
20#include "array.h"
21#include "console.h"
22#include "debug.h"
23#include "difficulty.h"
24#include "event.h"
25#include "hook.h"
26#include "info.h"
27#include "input.h"
28#include "land.h"
29#include "menu.h"
30#include "nlua_misn.h"
31#include "nlua_system.h"
32#include "nluadef.h"
33#include "pause.h"
34#include "player.h"
35#include "plugin.h"
36#include "semver.h"
37
38static int cache_table = LUA_NOREF; /* No reference. */
39
40/* Naev methods. */
41static int naevL_version( lua_State *L );
42static int naevL_versionTest( lua_State *L );
43static int naevL_language( lua_State *L );
44static int naevL_lastplayed( lua_State *L );
45static int naevL_date( lua_State *L );
46static int naevL_ticks( lua_State *L );
47static int naevL_ticksGame( lua_State *L );
48static int naevL_clock( lua_State *L );
49static int naevL_fps( lua_State *L );
50static int naevL_keyGet( lua_State *L );
51static int naevL_keyEnable( lua_State *L );
52static int naevL_keyEnableAll( lua_State *L );
53static int naevL_keyDisableAll( lua_State *L );
54static int naevL_eventStart( lua_State *L );
55static int naevL_eventReload( lua_State *L );
56static int naevL_missionList( lua_State *L );
57static int naevL_missionStart( lua_State *L );
58static int naevL_missionTest( lua_State *L );
59static int naevL_missionReload( lua_State *L );
60static int naevL_shadersReload( lua_State *L );
61static int naevL_isSimulation( lua_State *L );
62static int naevL_conf( lua_State *L );
63static int naevL_confSet( lua_State *L );
64static int naevL_cache( lua_State *L );
65static int naevL_trigger( lua_State *L );
66static int naevL_claimTest( lua_State *L );
67static int naevL_plugins( lua_State *L );
68static int naevL_menuInfo( lua_State *L );
69static int naevL_menuSmall( lua_State *L );
70static int naevL_isPaused( lua_State *L );
71static int naevL_pause( lua_State *L );
72static int naevL_unpause( lua_State *L );
73static int naevL_hasTextInput( lua_State *L );
74static int naevL_setTextInput( lua_State *L );
75static int naevL_unit( lua_State *L );
76static int naevL_quadtreeParams( lua_State *L );
77static int naevL_difficulty( lua_State *L );
78#if DEBUGGING
79static int naevL_envs( lua_State *L );
80static int naevL_debugTrails( lua_State *L );
81static int naevL_debugCollisions( lua_State *L );
82#endif /* DEBUGGING */
83
84static const luaL_Reg naev_methods[] = {
85 { "version", naevL_version },
86 { "versionTest", naevL_versionTest },
87 { "language", naevL_language },
88 { "lastplayed", naevL_lastplayed },
89 { "date", naevL_date },
90 { "ticks", naevL_ticks },
91 { "ticksGame", naevL_ticksGame },
92 { "clock", naevL_clock },
93 { "fps", naevL_fps },
94 { "keyGet", naevL_keyGet },
95 { "keyEnable", naevL_keyEnable },
96 { "keyEnableAll", naevL_keyEnableAll },
97 { "keyDisableAll", naevL_keyDisableAll },
98 { "eventStart", naevL_eventStart },
99 { "eventReload", naevL_eventReload },
100 { "missionList", naevL_missionList },
101 { "missionStart", naevL_missionStart },
102 { "missionTest", naevL_missionTest },
103 { "missionReload", naevL_missionReload },
104 { "shadersReload", naevL_shadersReload },
105 { "isSimulation", naevL_isSimulation },
106 { "conf", naevL_conf },
107 { "confSet", naevL_confSet },
108 { "cache", naevL_cache },
109 { "trigger", naevL_trigger },
110 { "claimTest", naevL_claimTest },
111 { "plugins", naevL_plugins },
112 { "menuInfo", naevL_menuInfo },
113 { "menuSmall", naevL_menuSmall },
114 { "isPaused", naevL_isPaused },
115 { "pause", naevL_pause },
116 { "unpause", naevL_unpause },
117 { "hasTextInput", naevL_hasTextInput },
118 { "setTextInput", naevL_setTextInput },
119 { "unit", naevL_unit },
120 { "quadtreeParams", naevL_quadtreeParams },
121 { "difficulty", naevL_difficulty },
122#if DEBUGGING
123 { "envs", naevL_envs },
124 { "debugTrails", naevL_debugTrails },
125 { "debugCollisions", naevL_debugCollisions },
126#endif /* DEBUGGING */
127 { 0, 0 } };
128
135int nlua_loadNaev( nlua_env env )
136{
137 nlua_register( env, "naev", naev_methods, 0 );
138
139 /* Create cache. */
140 if ( cache_table == LUA_NOREF ) {
141 lua_newtable( naevL );
142 cache_table = luaL_ref( naevL, LUA_REGISTRYINDEX );
143 }
144
145 return 0;
146}
147
153
163static int naevL_version( lua_State *L )
164{
165 lua_pushstring( L, naev_version( 0 ) );
166 if ( player.loaded_version == NULL )
167 lua_pushnil( L );
168 else
169 lua_pushstring( L, player.loaded_version );
170 return 2;
171}
172
181static int naevL_versionTest( lua_State *L )
182{
183 const char *s1, *s2;
184 semver_t sv1, sv2;
185 int res;
186 /* Parse inputs. */
187 s1 = luaL_checkstring( L, 1 );
188 s2 = luaL_checkstring( L, 2 );
189 if ( semver_parse( s1, &sv1 ) )
190 NLUA_WARN( L, _( "Failed to parse version string '%s'!" ), s1 );
191 if ( semver_parse( s2, &sv2 ) )
192 NLUA_WARN( L, _( "Failed to parse version string '%s'!" ), s2 );
193
194 /* Check version. */
195 res = semver_compare( sv1, sv2 );
196
197 /* Cleanup. */
198 semver_free( &sv1 );
199 semver_free( &sv2 );
200
201 lua_pushinteger( L, res );
202 return 1;
203}
204
212static int naevL_language( lua_State *L )
213{
214 lua_pushstring( L, gettext_getLanguage() );
215 return 1;
216}
217
225static int naevL_lastplayed( lua_State *L )
226{
227 double d = difftime( time( NULL ), player.last_played );
228 double g = difftime( time( NULL ), conf.last_played );
229 lua_pushnumber( L, d / ( 3600. * 24. ) ); /*< convert to days */
230 lua_pushnumber( L, g / ( 3600. * 24. ) ); /*< convert to days */
231 return 2;
232}
233
239static int naevL_date( lua_State *L )
240{
241 const char *s = luaL_optstring( L, 1, "%c" );
242 time_t t = luaL_opt( L, (time_t)luaL_checknumber, 2, time( NULL ) );
243 struct tm *stm;
244 if ( *s == '!' ) { /* UTC? */
245 stm = gmtime( &t );
246 s++; /* skip `!' */
247 } else
248 stm = localtime( &t );
249 if ( stm == NULL ) /* invalid date? */
250 lua_pushnil( L );
251 else if ( strcmp( s, "*t" ) == 0 ) {
252 lua_createtable( L, 0, 9 ); /* 9 = number of fields */
253#define setfield( L, key, value ) \
254 do { \
255 lua_pushinteger( L, value ); \
256 lua_setfield( L, -2, key ); \
257 } while ( 0 )
258#define setboolfield( L, key, value ) \
259 do { \
260 if ( value >= 0 ) { \
261 lua_pushinteger( L, value ); \
262 lua_setfield( L, -2, key ); \
263 } \
264 } while ( 0 )
265 setfield( L, "sec", stm->tm_sec );
266 setfield( L, "min", stm->tm_min );
267 setfield( L, "hour", stm->tm_hour );
268 setfield( L, "day", stm->tm_mday );
269 setfield( L, "month", stm->tm_mon + 1 );
270 setfield( L, "year", stm->tm_year + 1900 );
271 setfield( L, "wday", stm->tm_wday + 1 );
272 setfield( L, "yday", stm->tm_yday + 1 );
273 setboolfield( L, "isdst", stm->tm_isdst );
274#undef setfield
275#undef setboolfield
276 } else {
277 char cc[3];
278 luaL_Buffer b;
279 cc[0] = '%';
280 cc[2] = '\0';
281 luaL_buffinit( L, &b );
282 for ( ; *s; s++ ) {
283 if ( *s != '%' || *( s + 1 ) == '\0' ) /* no conversion specifier? */
284 luaL_addchar( &b, *s );
285 else {
286 size_t reslen;
287 char buff[200]; /* should be big enough for any conversion result */
288 cc[1] = *( ++s );
289 reslen = strftime( buff, sizeof( buff ), cc, stm );
290 luaL_addlstring( &b, buff, reslen );
291 }
292 }
293 luaL_pushresult( &b );
294 }
295 return 1;
296}
297
306static int naevL_ticksGame( lua_State *L )
307{
308 lua_pushnumber( L, elapsed_time_mod );
309 return 1;
310}
311
320static int naevL_ticks( lua_State *L )
321{
322 lua_pushnumber( L, (double)SDL_GetPerformanceCounter() /
323 (double)SDL_GetPerformanceFrequency() );
324 return 1;
325}
326
333static int naevL_clock( lua_State *L )
334{
335 lua_pushnumber( L, (double)clock() / (double)CLOCKS_PER_SEC );
336 return 1;
337}
338
345static int naevL_fps( lua_State *L )
346{
347 lua_pushnumber( L, fps_current() );
348 return 1;
349}
350
360static int naevL_keyGet( lua_State *L )
361{
362 char buf[128];
363 const char *keyname = luaL_checkstring( L, 1 );
364 input_getKeybindDisplay( input_keyFromBrief( keyname ), buf, sizeof( buf ) );
365 lua_pushstring( L, buf );
366 return 1;
367}
368
380static int naevL_keyEnable( lua_State *L )
381{
382 const char *key = luaL_checkstring( L, 1 );
383 int enable = lua_toboolean( L, 2 );
384
385 input_toggleEnable( input_keyFromBrief( key ), enable );
386 return 0;
387}
388
395static int naevL_keyEnableAll( lua_State *L )
396{
397 (void)L;
399 return 0;
400}
401
408static int naevL_keyDisableAll( lua_State *L )
409{
410 (void)L;
412 return 0;
413}
414
423static int naevL_eventStart( lua_State *L )
424{
425 const char *str = luaL_checkstring( L, 1 );
426 int ret = event_start( str, NULL );
427
428 if ( cli_isOpen() && landed )
429 bar_regen();
430
431 lua_pushboolean( L, !ret );
432 return 1;
433}
434
442static int naevL_missionList( lua_State *L )
443{
444 const MissionData *misns = mission_list();
445 lua_newtable( L );
446 for ( int i = 0; i < array_size( misns ); i++ ) {
447 misn_pushMissionData( L, &misns[i] );
448 lua_rawseti( L, -2, i + 1 );
449 }
450 return 1;
451}
452
463static int naevL_missionStart( lua_State *L )
464{
465 const char *str = luaL_checkstring( L, 1 );
466 int ret = mission_start( str, NULL );
467
468 if ( cli_isOpen() && landed ) {
469 bar_regen();
470 misn_regen();
471 }
472
473 lua_pushboolean( L, ( ret == 0 ) || ( ret == 3 ) );
474 lua_pushboolean( L, ( ret == 3 ) );
475 return 2;
476}
477
492static int naevL_missionTest( lua_State *L )
493{
494 lua_pushboolean( L, mission_test( luaL_checkstring( L, 1 ) ) );
495 return 1;
496}
497
508static int naevL_eventReload( lua_State *L )
509{
510 const char *str = luaL_checkstring( L, 1 );
511 int ret = event_reload( str );
512
513 lua_pushboolean( L, !ret );
514 return 1;
515}
516
527static int naevL_missionReload( lua_State *L )
528{
529 const char *str = luaL_checkstring( L, 1 );
530 int ret = mission_reload( str );
531
532 lua_pushboolean( L, !ret );
533 return 1;
534}
535
542static int naevL_shadersReload( lua_State *L )
543{
544 (void)L;
545 shaders_unload();
546 shaders_load();
547 return 0;
548}
549
556static int naevL_isSimulation( lua_State *L )
557{
558 lua_pushboolean( L, space_isSimulation() );
559 return 1;
560}
561
562#define PUSH_STRING( L, name, value ) \
563 lua_pushstring( L, name ); \
564 lua_pushstring( L, value ); \
565 lua_rawset( L, -3 )
566#define PUSH_DOUBLE( L, name, value ) \
567 lua_pushstring( L, name ); \
568 lua_pushnumber( L, value ); \
569 lua_rawset( L, -3 )
570#define PUSH_INT( L, name, value ) \
571 lua_pushstring( L, name ); \
572 lua_pushinteger( L, value ); \
573 lua_rawset( L, -3 )
574#define PUSH_BOOL( L, name, value ) \
575 lua_pushstring( L, name ); \
576 lua_pushboolean( L, value ); \
577 lua_rawset( L, -3 )
585static int naevL_conf( lua_State *L )
586{
587 lua_newtable( L );
588 PUSH_STRING( L, "data", conf.ndata );
589 PUSH_STRING( L, "language", conf.language );
590 PUSH_STRING( L, "difficulty", conf.difficulty );
591 PUSH_INT( L, "fsaa", conf.fsaa );
592 PUSH_BOOL( L, "vsync", conf.vsync );
593 PUSH_INT( L, "width", conf.width );
594 PUSH_INT( L, "height", conf.height );
595 PUSH_DOUBLE( L, "scalefactor", conf.scalefactor );
596 PUSH_DOUBLE( L, "nebu_scale", conf.nebu_scale );
597 PUSH_BOOL( L, "fullscreen", conf.fullscreen );
598 PUSH_BOOL( L, "modesetting", conf.modesetting );
599 PUSH_BOOL( L, "notresizable", conf.notresizable );
600 PUSH_BOOL( L, "borderless", conf.borderless );
601 PUSH_BOOL( L, "minimize", conf.minimize );
602 PUSH_DOUBLE( L, "colourblind_sim", conf.colourblind_sim );
603 PUSH_DOUBLE( L, "colourblind_correct", conf.colourblind_correct );
604 PUSH_INT( L, "colourblind_type", conf.colourblind_type );
605 PUSH_DOUBLE( L, "game_speed", conf.game_speed );
606 PUSH_DOUBLE( L, "bg_brightness", conf.bg_brightness );
607 PUSH_DOUBLE( L, "nebu_nonuniformity", conf.nebu_nonuniformity );
608 PUSH_DOUBLE( L, "nebu_saturation", conf.nebu_saturation );
609 PUSH_DOUBLE( L, "gamma_correction", conf.gamma_correction );
610 PUSH_BOOL( L, "low_memory", conf.low_memory );
611 PUSH_BOOL( L, "showfps", conf.fps_show );
612 PUSH_INT( L, "maxfps", conf.fps_max );
613 PUSH_BOOL( L, "showpause", conf.pause_show );
614 PUSH_BOOL( L, "al_efx", conf.al_efx );
615 PUSH_BOOL( L, "nosound", conf.nosound );
616 PUSH_DOUBLE( L, "sound", conf.sound );
617 PUSH_DOUBLE( L, "music", conf.music );
618 /* joystick */
619 PUSH_INT( L, "mesg_visible", conf.mesg_visible );
620 PUSH_DOUBLE( L, "map_overlay_opacity", conf.map_overlay_opacity );
621 PUSH_BOOL( L, "big_icons", conf.big_icons );
622 PUSH_INT( L, "repeat_delay", conf.repeat_delay );
623 PUSH_INT( L, "repeat_freq", conf.repeat_freq );
624 PUSH_BOOL( L, "zoom_manual", conf.zoom_manual );
625 PUSH_DOUBLE( L, "zoom_far", conf.zoom_far );
626 PUSH_DOUBLE( L, "zoom_near", conf.zoom_near );
627 PUSH_DOUBLE( L, "zoom_speed", conf.zoom_speed );
628 PUSH_INT( L, "font_size_console", conf.font_size_console );
629 PUSH_INT( L, "font_size_intro", conf.font_size_intro );
630 PUSH_INT( L, "font_size_def", conf.font_size_def );
631 PUSH_INT( L, "font_size_small", conf.font_size_small );
632 PUSH_BOOL( L, "redirect_file", conf.redirect_file );
633 PUSH_INT( L, "doubletap_sensitivity", conf.doubletap_sens );
634 PUSH_DOUBLE( L, "mouse_hide", conf.mouse_hide );
635 PUSH_BOOL( L, "mouse_fly", conf.mouse_fly );
636 PUSH_INT( L, "mouse_accel", conf.mouse_accel );
637 PUSH_DOUBLE( L, "mouse_doubleclick", conf.mouse_doubleclick );
638 PUSH_BOOL( L, "devmode", conf.devmode );
639 PUSH_BOOL( L, "devautosave", conf.devautosave );
640 PUSH_BOOL( L, "lua_enet", conf.lua_enet );
641 PUSH_BOOL( L, "lua_repl", conf.lua_repl );
642 PUSH_BOOL( L, "conf_nosave", conf.nosave );
643 PUSH_STRING( L, "last_version", conf.lastversion );
644 PUSH_BOOL( L, "translation_warning_seen", conf.translation_warning_seen );
645 PUSH_BOOL( L, "fpu_except", conf.fpu_except );
646 PUSH_STRING( L, "dev_data_dir", conf.dev_data_dir );
647 PUSH_BOOL( L, "puzzle_skip", conf.puzzle_skip );
648 return 1;
649}
650#undef PUSH_STRING
651#undef PUSH_DOUBLE
652#undef PUSH_INT
653#undef PUSH_BOOL
654
662static int naevL_confSet( lua_State *L )
663{
664 (void)L;
665 /* TODO implement. */
666 return NLUA_ERROR( L, _( "unimplemented" ) );
667}
668
678static int naevL_cache( lua_State *L )
679{
680 lua_rawgeti( L, LUA_REGISTRYINDEX, cache_table );
681 return 1;
682}
683
699static int naevL_trigger( lua_State *L )
700{
701 HookParam hp[HOOK_MAX_PARAM];
702 const char *hookname = luaL_checkstring( L, 1 );
703
704 /* Set up hooks. */
705 if ( !lua_isnoneornil( L, 2 ) ) {
706 /* Since this doesn't get saved and is triggered by Lua code, we can
707 * actually pass references here. */
708 hp[0].type = HOOK_PARAM_REF;
709 lua_pushvalue( L, 2 );
710 hp[0].u.ref = luaL_ref( L, LUA_REGISTRYINDEX );
711 hp[1].type = HOOK_PARAM_SENTINEL;
712 } else
713 hp[0].type = HOOK_PARAM_SENTINEL;
714
715 /* Run the deferred hooks. */
716 hooks_runParamDeferred( hookname, hp );
717 return 0;
718}
719
734static int naevL_claimTest( lua_State *L )
735{
736 int inclusive = lua_toboolean( L, 2 );
737 Claim_t *claim = claim_create( !inclusive );
738
739 if ( lua_istable( L, 1 ) ) {
740 /* Iterate over table. */
741 lua_pushnil( L );
742 while ( lua_next( L, 1 ) != 0 ) {
743 if ( lua_issystem( L, -1 ) )
744 claim_addSys( claim, lua_tosystem( L, -1 ) );
745 else if ( lua_isstring( L, -1 ) )
746 claim_addStr( claim, lua_tostring( L, -1 ) );
747 lua_pop( L, 1 );
748 }
749 } else if ( lua_issystem( L, 1 ) )
750 claim_addSys( claim, lua_tosystem( L, 1 ) );
751 else if ( lua_isstring( L, 1 ) )
752 claim_addStr( claim, lua_tostring( L, 1 ) );
753 else
754 NLUA_INVALID_PARAMETER( L, 1 );
755
756 /* Only test, but don't apply case. */
757 lua_pushboolean( L, !claim_test( claim ) );
758 claim_destroy( claim );
759 return 1;
760}
761
768static int naevL_plugins( lua_State *L )
769{
770 const plugin_t *plugins = plugin_list();
771 lua_newtable( L );
772 for ( int i = 0; i < array_size( plugins ); i++ ) {
773 const plugin_t *plg = &plugins[i];
774 lua_newtable( L );
775
776#define STRING( x ) \
777 lua_pushstring( L, plg->x ); \
778 lua_setfield( L, -2, #x )
779#define INTEGER( x ) \
780 lua_pushinteger( L, plg->x ); \
781 lua_setfield( L, -2, #x )
782#define BOOL( x ) \
783 lua_pushboolean( L, plg->x ); \
784 lua_setfield( L, -2, #x )
785
786 STRING( name );
787 STRING( author );
788 STRING( version );
789 STRING( description );
790 STRING( compatibility );
791 STRING( mountpoint );
792
793 INTEGER( priority );
794
795 BOOL( compatible );
796 BOOL( total_conversion );
797
798#undef BOOL
799#undef INTEGER
800#undef STRING
801
802 lua_rawseti( L, -2, i + 1 );
803 }
804 return 1;
805}
806
824static int naevL_menuInfo( lua_State *L )
825{
826 const char *str;
827 int window;
828
829 if ( menu_open )
830 return 0;
831
832 if ( lua_gettop( L ) > 0 )
833 str = luaL_checkstring( L, 1 );
834 else {
835 /* No parameter. */
836 menu_info( INFO_DEFAULT );
837 return 0;
838 }
839
840 /* Parse string. */
841 if ( strcasecmp( str, "main" ) == 0 )
842 window = INFO_MAIN;
843 else if ( strcasecmp( str, "ship" ) == 0 )
844 window = INFO_SHIP;
845 else if ( strcasecmp( str, "weapons" ) == 0 )
846 window = INFO_WEAPONS;
847 else if ( strcasecmp( str, "cargo" ) == 0 )
848 window = INFO_CARGO;
849 else if ( strcasecmp( str, "missions" ) == 0 )
850 window = INFO_MISSIONS;
851 else if ( strcasecmp( str, "standings" ) == 0 )
852 window = INFO_STANDINGS;
853 else
854 return NLUA_ERROR( L, _( "Invalid window info name '%s'." ), str );
855
856 /* Open window. */
857 menu_info( window );
858
859 return 0;
860}
861
873static int naevL_menuSmall( lua_State *L )
874{
875 menu_small( 0, lua_toboolean( L, 1 ), lua_toboolean( L, 2 ),
876 lua_toboolean( L, 3 ) );
877 return 0;
878}
879
886static int naevL_isPaused( lua_State *L )
887{
888 lua_pushboolean( L, paused );
889 return 1;
890}
891
897static int naevL_pause( lua_State *L )
898{
899 (void)L;
900 pause_game();
901 return 0;
902}
903
911static int naevL_unpause( lua_State *L )
912{
913 if ( landed )
914 return NLUA_ERROR( L, _( "Unable to unpause the game when landed!" ) );
915 unpause_game();
916 return 0;
917}
918
925static int naevL_hasTextInput( lua_State *L )
926{
927 lua_pushboolean( L, SDL_EventState( SDL_TEXTINPUT, SDL_QUERY ) == SDL_TRUE );
928 return 1;
929}
930
941static int naevL_setTextInput( lua_State *L )
942{
943 if ( lua_toboolean( L, 1 ) ) {
944 SDL_Rect input_pos;
945 input_pos.x = luaL_checkinteger( L, 2 );
946 input_pos.y = luaL_checkinteger( L, 3 );
947 input_pos.w = luaL_checkinteger( L, 4 );
948 input_pos.h = luaL_checkinteger( L, 5 );
949 SDL_EventState( SDL_TEXTINPUT, SDL_ENABLE );
950 SDL_StartTextInput();
951 SDL_SetTextInputRect( &input_pos );
952 } else {
953 SDL_StopTextInput();
954 SDL_EventState( SDL_TEXTINPUT, SDL_DISABLE );
955 }
956 return 0;
957}
958
959static const char *unittbl[] = {
960 "time", _UNIT_TIME, "per_time", _UNIT_PER_TIME, "distance",
961 _UNIT_DISTANCE, "speed", _UNIT_SPEED, "accel", _UNIT_ACCEL,
962 "energy", _UNIT_ENERGY, "power", _UNIT_POWER, "angle",
963 _UNIT_ANGLE, "rotation", _UNIT_ROTATION, "mass", _UNIT_MASS,
964 "cpu", _UNIT_CPU, "unit", _UNIT_UNIT, "percent",
965 _UNIT_PERCENT,
966};
975static int naevL_unit( lua_State *L )
976{
977 if ( lua_isnoneornil( L, 1 ) ) {
978 lua_newtable( L );
979 for ( unsigned int i = 0; i < sizeof( unittbl ) / sizeof( unittbl[0] );
980 i += 2 ) {
981 lua_pushstring( L, _( unittbl[i + 1] ) );
982 lua_setfield( L, -2, unittbl[i] );
983 }
984 return 1;
985 } else {
986 const char *str = luaL_checkstring( L, 1 );
987 for ( unsigned int i = 0; i < sizeof( unittbl ) / sizeof( unittbl[0] );
988 i += 2 ) {
989 if ( strcmp( unittbl[i], str ) == 0 ) {
990 lua_pushstring( L, _( unittbl[i + 1] ) );
991 return 1;
992 }
993 }
994 }
995 NLUA_INVALID_PARAMETER( L, 1 );
996}
997
1006static int naevL_quadtreeParams( lua_State *L )
1007{
1008 int max_elem = luaL_checkinteger( L, 1 );
1009 int depth = luaL_checkinteger( L, 2 );
1010 pilot_quadtreeParams( max_elem, depth );
1011 return 0;
1012}
1013
1024static int naevL_difficulty( lua_State *L )
1025{
1026 const Difficulty *dif = difficulty_cur();
1027 lua_pushstring( L, dif->name );
1028 ss_statsGetLuaTableList( L, dif->stats, lua_toboolean( L, 1 ) );
1029 return 2;
1030}
1031
1032#if DEBUGGING
1041static int naevL_envs( lua_State *L )
1042{
1043 nlua_pushEnvTable( L );
1044 return 1;
1045}
1046
1056static int naevL_debugTrails( lua_State *L )
1057{
1058 int state = ( lua_gettop( L ) > 0 ) ? lua_toboolean( L, 1 ) : 1;
1059 if ( state )
1060 debug_setFlag( DEBUG_MARK_EMITTER );
1061 else
1062 debug_rmFlag( DEBUG_MARK_EMITTER );
1063 return 0;
1064}
1065
1073static int naevL_debugCollisions( lua_State *L )
1074{
1075 int state = ( lua_gettop( L ) > 0 ) ? lua_toboolean( L, 1 ) : 1;
1076 if ( state )
1077 debug_setFlag( DEBUG_MARK_COLLISION );
1078 else
1079 debug_rmFlag( DEBUG_MARK_COLLISION );
1080 return 0;
1081}
1082#endif /* DEBUGGING */
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
int claim_test(const Claim_t *claim)
Tests to see if a system claim would have collisions.
Definition claim.c:112
void claim_destroy(Claim_t *claim)
Destroys a system claim.
Definition claim.c:189
int claim_addStr(Claim_t *claim, const char *str)
Adds a string claim to a claim.
Definition claim.c:59
int claim_addSys(Claim_t *claim, int ss_id)
Adds a claim to a system claim.
Definition claim.c:77
Claim_t * claim_create(int exclusive)
Creates a system claim.
Definition claim.c:42
int event_start(const char *name, unsigned int *id)
Starts an event.
Definition event.c:123
const char * gettext_getLanguage(void)
Gets the active (primary) translation language. Even in case of a complex locale, this will be the na...
Definition gettext.c:122
int hooks_runParamDeferred(const char *stack, const HookParam *param)
Runs all the hooks of stack in the next frame. Does not trigger right away.
Definition hook.c:996
Handles the info menu.
#define INFO_SHIP
Definition info.h:9
#define INFO_DEFAULT
Definition info.h:16
#define INFO_CARGO
Definition info.h:11
#define INFO_WEAPONS
Definition info.h:10
#define INFO_STANDINGS
Definition info.h:13
#define INFO_MAIN
Definition info.h:8
#define INFO_MISSIONS
Definition info.h:12
void input_disableAll(void)
Disables all the keybinds.
Definition input.c:436
void input_toggleEnable(KeySemanticType key, int enable)
Enables or disables a keybind.
Definition input.c:445
KeySemanticType input_keyFromBrief(const char *target)
Definition input.c:1930
void input_enableAll(void)
Enables all the keybinds.
Definition input.c:427
void input_getKeybindDisplay(KeySemanticType keybind, char *buf, int len)
Gets the display name (translated and human-readable) of a keybind.
Definition input.c:547
void bar_regen(void)
Regenerates the bar list.
Definition land.c:432
int landed
Definition land.c:78
void misn_regen(void)
Regenerates the mission list.
Definition land.c:419
Handles the important game menus.
int mission_start(const char *name, unsigned int *id)
Starts a mission.
Definition mission.c:365
const MissionData * mission_list(void)
Returns all the missions.
Definition mission.c:218
int mission_test(const char *name)
Tests the conditionals of a mission.
Definition mission.c:398
double elapsed_time_mod
Definition naev.c:117
double fps_current(void)
Gets the current FPS.
Definition naev.c:995
Header file with generic functions and naev-specifics.
const char * naev_version(int long_version)
Returns the version in a human readable string.
lua_State * naevL
Definition nlua.c:54
static int naevL_isSimulation(lua_State *L)
Gets whether or not the universe is being simulated or not.
Definition nlua_naev.c:556
static int naevL_menuInfo(lua_State *L)
Opens the info menu window.
Definition nlua_naev.c:824
static int naevL_unit(lua_State *L)
Gets the translated string corresponding to an in-game unit. Lua function parameter:[opt=nil] string ...
Definition nlua_naev.c:975
static int naevL_conf(lua_State *L)
Gets the configuration information.
Definition nlua_naev.c:585
static int naevL_fps(lua_State *L)
Gets the current game FPS as displayed to the player.
Definition nlua_naev.c:345
static int naevL_missionTest(lua_State *L)
Tests a missions conditionals to see if it can be started by the player.
Definition nlua_naev.c:492
static int naevL_eventReload(lua_State *L)
Reloads an event's script, providing a convenient way to test and hopefully not corrupt the game's st...
Definition nlua_naev.c:508
static int naevL_shadersReload(lua_State *L)
Reloads all the Naev shaders excluding those created by the shader library.
Definition nlua_naev.c:542
static int naevL_quadtreeParams(lua_State *L)
Modifies the Naev internal quadtree lookup parameters.
Definition nlua_naev.c:1006
static int naevL_lastplayed(lua_State *L)
Gets how many days it has been since the player last played Naev.
Definition nlua_naev.c:225
static int naevL_confSet(lua_State *L)
Sets configuration variables. Note that not all are supported.
Definition nlua_naev.c:662
static int naevL_date(lua_State *L)
Equivalent to os.date from standard Lua.
Definition nlua_naev.c:239
static int naevL_claimTest(lua_State *L)
Tests a claim of a system or strings.
Definition nlua_naev.c:734
static int naevL_missionStart(lua_State *L)
Starts a mission, does no check start conditions.
Definition nlua_naev.c:463
static int naevL_keyDisableAll(lua_State *L)
Disables all inputs.
Definition nlua_naev.c:408
int nlua_loadNaev(nlua_env env)
Loads the Naev Lua library.
Definition nlua_naev.c:135
static int naevL_difficulty(lua_State *L)
Gets information about the current difficulty setting.
Definition nlua_naev.c:1024
static int naevL_keyGet(lua_State *L)
Gets a human-readable name for the key bound to a function.
Definition nlua_naev.c:360
static int naevL_pause(lua_State *L)
Pauses the game.
Definition nlua_naev.c:897
static int naevL_clock(lua_State *L)
Gets the approximate CPU processing time.
Definition nlua_naev.c:333
static int naevL_plugins(lua_State *L)
Gets the list of available plugins.
Definition nlua_naev.c:768
static int naevL_setTextInput(lua_State *L)
Enables or disables text inputting.
Definition nlua_naev.c:941
static int naevL_menuSmall(lua_State *L)
Opens the small menu window.
Definition nlua_naev.c:873
static int naevL_hasTextInput(lua_State *L)
Checks to see if text inputting is enabled.
Definition nlua_naev.c:925
static int naevL_cache(lua_State *L)
Gets the global Lua runtime cache. This is shared among all environments and is cleared when the game...
Definition nlua_naev.c:678
static int naevL_version(lua_State *L)
Naev generic Lua bindings.
Definition nlua_naev.c:163
static int naevL_keyEnableAll(lua_State *L)
Enables all inputs.
Definition nlua_naev.c:395
static int naevL_missionReload(lua_State *L)
Reloads a mission's script, providing a convenient way to test and hopefully not corrupt the game's s...
Definition nlua_naev.c:527
static int naevL_keyEnable(lua_State *L)
Disables or enables a specific keybinding.
Definition nlua_naev.c:380
static int naevL_language(lua_State *L)
Gets the current language locale.
Definition nlua_naev.c:212
static int naevL_ticks(lua_State *L)
Gets the seconds since the program started running.
Definition nlua_naev.c:320
static int naevL_trigger(lua_State *L)
Triggers manually a hook stack. This is run deferred (next frame). Meant mainly to be used with hook....
Definition nlua_naev.c:699
static int naevL_isPaused(lua_State *L)
Checks to see if the game is paused.
Definition nlua_naev.c:886
static int naevL_eventStart(lua_State *L)
Starts an event, does not start check conditions.
Definition nlua_naev.c:423
static int naevL_missionList(lua_State *L)
Lists all the missions in the game.
Definition nlua_naev.c:442
static int naevL_versionTest(lua_State *L)
Tests two semver version strings.
Definition nlua_naev.c:181
static int naevL_unpause(lua_State *L)
Unpauses the game.
Definition nlua_naev.c:911
static int naevL_ticksGame(lua_State *L)
Gets the game seconds since the program started running.
Definition nlua_naev.c:306
static const luaL_Reg naev_methods[]
Definition nlua_naev.c:84
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
void pause_game(void)
Pauses the game.
Definition pause.c:25
int paused
Definition pause.c:18
void unpause_game(void)
Unpauses the game.
Definition pause.c:43
void pilot_quadtreeParams(int max_elem, int depth)
Sets the quad tree parameters. Can have significant impact on performance.
Definition pilot.c:4592
Player_t player
Definition player.c:77
static plugin_t * plugins
Definition plugin.c:26
const plugin_t * plugin_list(void)
Returns the list of all the plugins.
Definition plugin.c:396
static const double d[]
Definition rng.c:263
int ss_statsGetLuaTableList(lua_State *L, const ShipStatList *list, int internal)
Converts ship stats to a Lua table, which is pushed on the Lua stack.
Definition shipstats.c:1157
int space_isSimulation(void)
returns whether we're just simulating.
Definition space.c:1601
ShipStatList * stats
Definition difficulty.h:12
char * name
Definition difficulty.h:9
The actual hook parameter.
Definition hook.h:40
HookParamType type
Definition hook.h:41
int ref
Definition hook.h:55
union HookParam::@065274143236224234262250043114351136253171035204 u
Static mission data.
Definition mission.h:64