/** * @brief Initializes the CLI environment. */ int cli_init (void) { /* Already loaded. */ if (cli_state != NULL) return 0; /* Set the height. */ cli_height = CLI_HEIGHT; /* Create the state. */ cli_state = nlua_newState(); nlua_loadStandard( cli_state, 0 ); nlua_loadCol( cli_state, 0 ); nlua_loadTex( cli_state, 0 ); nlua_loadBackground( cli_state, 0 ); nlua_loadCamera( cli_state, 0 ); nlua_loadTk( cli_state ); nlua_loadCLI( cli_state ); nlua_loadMusic( cli_state, 0 ); luaL_register( cli_state, "_G", cli_methods ); lua_settop( cli_state, 0 ); /* Mark as console. */ lua_pushboolean( cli_state, 1 ); lua_setglobal( cli_state, "__cli" ); /* Set the font. */ cli_font = malloc( sizeof(glFont) ); gl_fontInit( cli_font, "dat/mono.ttf", conf.font_size_console ); /* Clear the buffer. */ memset( cli_buffer, 0, sizeof(cli_buffer) ); return 0; }
/** * @brief Runs the rescue script if players are stuck. */ static void land_stranded (void) { char *buf; size_t bufsize; const char *file = "dat/rescue.lua"; /* Nothing to do if there's no rescue script. */ if (!ndata_exists(file)) return; if (rescue_env == LUA_NOREF) { rescue_env = nlua_newEnv(1); nlua_loadStandard( rescue_env ); nlua_loadTk( rescue_env ); buf = ndata_read( file, &bufsize ); if (nlua_dobufenv(rescue_env, buf, bufsize, file) != 0) { WARN( _("Error loading file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check"), file, lua_tostring(naevL,-1)); free(buf); return; } free(buf); } /* Run Lua. */ nlua_getenv(rescue_env,"rescue"); if (nlua_pcall(rescue_env, 0, 0)) { /* error has occurred */ WARN( _("Rescue: 'rescue' : '%s'"), lua_tostring(naevL,-1)); lua_pop(naevL,1); } }
/** * @brief Runs the rescue script if players are stuck. */ static void land_stranded (void) { char *buf; uint32_t bufsize; const char *file = "dat/rescue.lua"; int errf; lua_State *L; /* Nothing to do if there's no rescue script. */ if (!ndata_exists(file)) return; if (rescue_L == NULL) { rescue_L = nlua_newState(); nlua_loadStandard( rescue_L, 0 ); nlua_loadTk( rescue_L ); L = rescue_L; buf = ndata_read( file, &bufsize ); if (luaL_dobuffer(L, buf, bufsize, file) != 0) { WARN("Error loading file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check", file, lua_tostring(L,-1)); free(buf); return; } free(buf); } else L = rescue_L; #if DEBUGGING lua_pushcfunction(L, nlua_errTrace); errf = -2; #else /* DEBUGGING */ errf = 0; #endif /* DEBUGGING */ /* Run Lua. */ lua_getglobal(L,"rescue"); if (lua_pcall(L, 0, 0, errf)) { /* error has occurred */ WARN("Rescue: 'rescue' : '%s'", lua_tostring(L,-1)); lua_pop(L,1); } #if DEBUGGING lua_pop(L,1); #endif }
/** * @brief Creates an event. * * @param data Data to base event off of. */ static int event_create( int dataid ) { lua_State *L; uint32_t bufsize; char *buf; Event_t *ev; EventData_t *data; /* Create the event. */ event_nactive++; if (event_nactive > event_mactive) { event_mactive += EVENT_CHUNK; event_active = realloc( event_active, sizeof(Event_t) * event_mactive ); } ev = &event_active[ event_nactive-1 ]; memset( ev, 0, sizeof(Event_t) ); ev->id = ++event_genid; /* Create unique ID. */ /* Add the data. */ ev->data = dataid; data = &event_data[dataid]; /* Open the new state. */ ev->L = nlua_newState(); L = ev->L; nlua_loadStandard(L,0); nlua_loadEvt(L); nlua_loadHook(L); nlua_loadTk(L); /* Load file. */ buf = ndata_read( data->lua, &bufsize ); if (buf == NULL) { WARN("Event '%s' Lua script not found.", data->lua ); return -1; } if (luaL_dobuffer(L, buf, bufsize, data->lua) != 0) { WARN("Error loading event file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check", data->lua, lua_tostring(L,-1)); return -1; } free(buf); /* Run Lua. */ event_runLua( ev, "create" ); return 0; }
/** * @brief Registers all the mission libraries. * * @param L Lua state. * @return 0 on success. */ int misn_loadLibs( lua_State *L ) { nlua_loadStandard(L,0); nlua_loadMisn(L); nlua_loadTk(L); nlua_loadHook(L); nlua_loadMusic(L,0); nlua_loadTex(L,0); nlua_loadBackground(L,0); nlua_loadCamera(L,0); if (player_isTut()) nlua_loadTut(L); return 0; }
/** * @brief Initializes the CLI environment. */ int cli_init (void) { /* Already loaded. */ if (cli_state != NULL) return 0; /* Calculate size. */ cli_width = SCREEN_W - 100; cli_height = SCREEN_H - 100; /* Create the state. */ cli_state = nlua_newState(); nlua_loadStandard( cli_state, 0 ); nlua_loadCol( cli_state, 0 ); nlua_loadTex( cli_state, 0 ); nlua_loadBackground( cli_state, 0 ); nlua_loadCamera( cli_state, 0 ); nlua_loadTk( cli_state ); nlua_loadCLI( cli_state ); nlua_loadMusic( cli_state, 0 ); luaL_register( cli_state, "_G", cli_methods ); lua_settop( cli_state, 0 ); /* Mark as console. */ lua_pushboolean( cli_state, 1 ); lua_setglobal( cli_state, "__cli" ); /* Set the font. */ cli_font = malloc( sizeof(glFont) ); gl_fontInit( cli_font, "dat/mono.ttf", CONSOLE_FONT_SIZE ); /* Clear the buffer. */ memset( cli_buffer, 0, sizeof(cli_buffer) ); /* Put a friendly message at first. */ cli_addMessage( "Welcome to the Lua console!" ); cli_addMessage( "" ); return 0; }
/** * @brief Creates an event. * * @param data Data to base event off of. * @param id ID to use (0 to generate). * @return 0 on success. */ static int event_create( int dataid, unsigned int *id ) { size_t bufsize; char *buf; Event_t *ev; EventData_t *data; /* Create the event. */ event_nactive++; if (event_nactive > event_mactive) { event_mactive += EVENT_CHUNK; event_active = realloc( event_active, sizeof(Event_t) * event_mactive ); } ev = &event_active[ event_nactive-1 ]; memset( ev, 0, sizeof(Event_t) ); if ((id != NULL) && (*id != 0)) ev->id = *id; else ev->id = event_genID(); /* Add the data. */ ev->data = dataid; data = &event_data[dataid]; /* Open the new state. */ ev->env = nlua_newEnv(1); nlua_loadStandard(ev->env); nlua_loadEvt(ev->env); nlua_loadHook(ev->env); nlua_loadCamera(ev->env); nlua_loadTex(ev->env); nlua_loadBackground(ev->env); nlua_loadMusic(ev->env); nlua_loadTk(ev->env); if (player_isTut()) nlua_loadTut(ev->env); /* Load file. */ buf = ndata_read( data->lua, &bufsize ); if (buf == NULL) { WARN(_("Event '%s' Lua script not found."), data->lua ); return -1; } if (nlua_dobufenv(ev->env, buf, bufsize, data->lua) != 0) { WARN(_("Error loading event file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check"), data->lua, lua_tostring(naevL,-1)); free(buf); return -1; } free(buf); /* Run Lua. */ if ((id==NULL) || (*id==0)) event_runLua( ev, "create" ); if (id != NULL) *id = ev->id; return 0; }