naev 0.12.5
nlua_evt.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <stdio.h>
11#include <stdlib.h>
13
14#include "nlua_evt.h"
15
16#include "event.h"
17#include "land.h"
18#include "log.h"
19#include "ndata.h"
20#include "nlua.h"
21#include "nlua_system.h"
22#include "nlua_tex.h"
23#include "nluadef.h"
24#include "npc.h"
25#include "player.h"
36/*
37 * libraries
38 */
39/* evt */
40static int evtL_npcAdd( lua_State *L );
41static int evtL_npcRm( lua_State *L );
42static int evtL_finish( lua_State *L );
43static int evtL_save( lua_State *L );
44static int evtL_claim( lua_State *L );
45
46static const luaL_Reg evtL_methods[] = {
47 { "npcAdd", evtL_npcAdd }, { "npcRm", evtL_npcRm },
48 { "save", evtL_save }, { "finish", evtL_finish },
49 { "claim", evtL_claim }, { 0, 0 } };
50
51/*
52 * individual library loading
53 */
58int nlua_loadEvt( nlua_env env )
59{
60 nlua_register( env, "evt", evtL_methods, 0 );
61 return 0;
62}
63
67void event_runStart( unsigned int eventid, const char *func )
68{
69 uintptr_t *evptr;
70 const Event_t *ev = event_get( eventid );
71 if ( ev == NULL )
72 return;
73
74 /* Set up event pointer. */
75 evptr = lua_newuserdata( naevL, sizeof( Event_t *) );
76 *evptr = ev->id;
77 nlua_setenv( naevL, ev->env, "__evt" );
78
79 /* Get function. */
80 nlua_getenv( naevL, ev->env, func );
81}
82
90int event_run( unsigned int eventid, const char *func )
91{
92 int ret;
93 event_runStart( eventid, func );
94 ret = event_runFunc( eventid, func, 0 );
95 return ret;
96}
97
103Event_t *event_getFromLua( lua_State *L )
104{
105 Event_t *ev;
106 uintptr_t *evptr;
107 nlua_getenv( L, __NLUA_CURENV, "__evt" );
108 evptr = lua_touserdata( L, -1 );
109 ev = evptr ? event_get( *evptr ) : NULL;
110 lua_pop( L, 1 );
111 return ev;
112}
113
120int event_runFunc( unsigned int eventid, const char *func, int nargs )
121{
122 int ret;
123 int evt_delete;
124 Event_t *ev = event_get( eventid );
125 if ( ev == NULL )
126 return 0;
127
128 ret = nlua_pcall( ev->env, nargs, 0 );
129 if ( ret != 0 ) { /* error has occurred */
130 const char *err =
131 ( lua_isstring( naevL, -1 ) ) ? lua_tostring( naevL, -1 ) : NULL;
132 if ( ( err == NULL ) || ( strcmp( err, NLUA_DONE ) != 0 ) ) {
133 WARN( _( "Event '%s' -> '%s': %s" ), event_getData( ev->id ), func,
134 ( err ) ? err : _( "unknown error" ) );
135 ret = -1;
136 } else
137 ret = 1;
138 lua_pop( naevL, 1 );
139 }
140
141 /* Time to remove the event. */
142 ev =
143 event_get( eventid ); /* The Lua call may have invalidated the pointer. */
144 if ( ev == NULL ) {
145 WARN( _( "Event deleted in middle of hook call! Likely caused by another "
146 "hook finishing the event triggered by this hook (\"%s\")." ),
147 func );
148 return -1;
149 }
150 nlua_getenv( naevL, ev->env, "__evt_delete" );
151 evt_delete = lua_toboolean( naevL, -1 );
152 lua_pop( naevL, 1 );
153 if ( evt_delete ) {
154 ret = 2;
155 event_remove( ev->id );
156 }
157
158 return ret;
159}
160
180static int evtL_npcAdd( lua_State *L )
181{
182 unsigned int id;
183 int priority;
184 const char *func, *name, *desc;
185 glTexture *portrait, *bg;
186 Event_t *cur_event;
187
188 /* Handle parameters. */
189 func = luaL_checkstring( L, 1 );
190 name = luaL_checkstring( L, 2 );
191 portrait = luaL_validtex( L, 3, GFX_PATH "portraits/" );
192 desc = luaL_checkstring( L, 4 );
193
194 /* Optional parameters. */
195 priority = luaL_optinteger( L, 5, 5 );
196 if ( !lua_isnoneornil( L, 6 ) )
197 bg = luaL_validtex( L, 6, GFX_PATH "portraits/" );
198 else
199 bg = NULL;
200
201 cur_event = event_getFromLua( L );
202
203 /* Add npc. */
204 id =
205 npc_add_event( cur_event->id, func, name, priority, portrait, desc, bg );
206
207 bar_regen();
208
209 /* Return ID. */
210 if ( id > 0 ) {
211 lua_pushnumber( L, id );
212 return 1;
213 }
214 return 0;
215}
216
225static int evtL_npcRm( lua_State *L )
226{
227 unsigned int id;
228 int ret;
229 Event_t *cur_event;
230
231 id = luaL_checklong( L, 1 );
232
233 cur_event = event_getFromLua( L );
234 ret = npc_rm_event( id, cur_event->id );
235
236 bar_regen();
237
238 if ( ret != 0 )
239 return NLUA_ERROR( L, _( "Invalid NPC ID!" ) );
240 return 0;
241}
242
251static int evtL_finish( lua_State *L )
252{
253 const Event_t *cur_event = event_getFromLua( L );
254 int b = lua_toboolean( L, 1 );
255 lua_pushboolean( L, 1 );
256 nlua_setenv( L, cur_event->env, "__evt_delete" );
257
258 if ( b && event_isUnique( cur_event->id ) )
259 player_eventFinished( cur_event->data );
260
261 lua_pushstring( L, NLUA_DONE );
262 lua_error( L ); /* shouldn't return */
263
264 return 0;
265}
266
276static int evtL_save( lua_State *L )
277{
278 int b;
279 Event_t *cur_event = event_getFromLua( L );
280 if ( lua_gettop( L ) == 0 )
281 b = 1;
282 else
283 b = lua_toboolean( L, 1 );
284 cur_event->save = b;
285 return 0;
286}
287
313static int evtL_claim( lua_State *L )
314{
315 Claim_t *claim;
316 Event_t *cur_event;
317 int inclusive;
318
319 /* Get current event. */
320 cur_event = event_getFromLua( L );
321
322 inclusive = lua_toboolean( L, 2 );
323
324 /* Check to see if already claimed. */
325 if ( !claim_isNull( cur_event->claims ) )
326 return NLUA_ERROR( L, _( "Event trying to claim but already has." ) );
327
328 /* Create the claim. */
329 claim = claim_create( !inclusive );
330
331 /* Handle parameters. */
332 if ( lua_istable( L, 1 ) ) {
333 /* Iterate over table. */
334 lua_pushnil( L );
335 while ( lua_next( L, 1 ) != 0 ) {
336 if ( lua_issystem( L, -1 ) )
337 claim_addSys( claim, lua_tosystem( L, -1 ) );
338 else if ( lua_isstring( L, -1 ) )
339 claim_addStr( claim, lua_tostring( L, -1 ) );
340 lua_pop( L, 1 );
341 }
342 } else if ( lua_issystem( L, 1 ) )
343 claim_addSys( claim, lua_tosystem( L, 1 ) );
344 else if ( lua_isstring( L, 1 ) )
345 claim_addStr( claim, lua_tostring( L, 1 ) );
346 else
347 NLUA_INVALID_PARAMETER( L, 1 );
348
349 /* Test claim. */
350 if ( claim_test( claim ) ) {
351 claim_destroy( claim );
352 lua_pushboolean( L, 0 );
353 return 1;
354 }
355
356 /* Set the claim. */
357 cur_event->claims = claim;
358 claim_activate( claim );
359 lua_pushboolean( L, 1 );
360 return 1;
361}
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
void claim_activate(Claim_t *claim)
Activates a claim on a system.
Definition claim.c:252
int claim_isNull(const Claim_t *claim)
See if a claim actually contains data.
Definition claim.c:95
Claim_t * claim_create(int exclusive)
Creates a system claim.
Definition claim.c:42
const char * event_getData(unsigned int eventid)
Gets the name of the event data.
Definition event.c:145
void event_remove(unsigned int eventid)
Removes an event by ID.
Definition event.c:268
Event_t * event_get(unsigned int eventid)
Gets an event.
Definition event.c:104
int event_isUnique(unsigned int eventid)
Checks to see if an event is unique.
Definition event.c:159
void bar_regen(void)
Regenerates the bar list.
Definition land.c:432
nlua_env __NLUA_CURENV
Definition nlua.c:55
lua_State * naevL
Definition nlua.c:54
void event_runStart(unsigned int eventid, const char *func)
Starts running a function, allows programmer to set up arguments.
Definition nlua_evt.c:67
int event_runFunc(unsigned int eventid, const char *func, int nargs)
Runs a function previously set up with event_runStart.
Definition nlua_evt.c:120
static int evtL_claim(lua_State *L)
Tries to claim systems or strings.
Definition nlua_evt.c:313
static int evtL_finish(lua_State *L)
Finishes the event.
Definition nlua_evt.c:251
int event_run(unsigned int eventid, const char *func)
Runs the event function.
Definition nlua_evt.c:90
static int evtL_npcAdd(lua_State *L)
Event system Lua bindings.
Definition nlua_evt.c:180
static int evtL_npcRm(lua_State *L)
Removes an NPC.
Definition nlua_evt.c:225
Event_t * event_getFromLua(lua_State *L)
Gets the current running event from user data.
Definition nlua_evt.c:103
static int evtL_save(lua_State *L)
Saves an event.
Definition nlua_evt.c:276
static const luaL_Reg evtL_methods[]
Definition nlua_evt.c:46
int nlua_loadEvt(nlua_env env)
Loads the event Lua library.
Definition nlua_evt.c:58
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.
glTexture * luaL_validtex(lua_State *L, int ind, const char *searchpath)
Gets texture directly or from a filename (string) at index or raises error if there is no texture at ...
Definition nlua_tex.c:113
unsigned int npc_add_event(unsigned int evt, const char *func, const char *name, int priority, glTexture *portrait, const char *desc, glTexture *background)
Adds a event NPC to the mission computer.
Definition npc.c:190
int npc_rm_event(unsigned int id, unsigned int evt)
removes an event NPC.
Definition npc.c:235
void player_eventFinished(int id)
Marks a event as completed.
Definition player.c:3151
Activated event structure.
Definition event.h:12
unsigned int id
Definition event.h:13
int save
Definition event.h:16
int data
Definition event.h:14
nlua_env env
Definition event.h:15
Claim_t * claims
Definition event.h:17
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:43