naev 0.11.5
nlua_evt.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <math.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14#include "naev.h"
17#include "nlua_evt.h"
18
19#include "event.h"
20#include "land.h"
21#include "log.h"
22#include "mission.h"
23#include "ndata.h"
24#include "nlua.h"
25#include "nlua_hook.h"
26#include "nlua_system.h"
27#include "nlua_tex.h"
28#include "nluadef.h"
29#include "npc.h"
30#include "nstring.h"
31#include "player.h"
32
44/*
45 * libraries
46 */
47/* evt */
48static int evtL_npcAdd( lua_State *L );
49static int evtL_npcRm( lua_State *L );
50static int evtL_finish( lua_State *L );
51static int evtL_save( lua_State *L );
52static int evtL_claim( lua_State *L );
53static const luaL_Reg evtL_methods[] = {
54 { "npcAdd", evtL_npcAdd },
55 { "npcRm", evtL_npcRm },
56 { "save", evtL_save },
57 { "finish", evtL_finish },
58 { "claim", evtL_claim },
59 {0,0}
60};
62/*
63 * individual library loading
64 */
69int nlua_loadEvt( nlua_env env )
70{
71 nlua_register(env, "evt", evtL_methods, 0);
72 return 0;
73}
74
78void event_runStart( unsigned int eventid, const char *func )
79{
80 uintptr_t *evptr;
81 const Event_t *ev = event_get( eventid );
82 if (ev == NULL)
83 return;
84
85 /* Set up event pointer. */
86 evptr = lua_newuserdata( naevL, sizeof(Event_t*) );
87 *evptr = ev->id;
88 nlua_setenv( naevL, ev->env, "__evt" );
89
90 /* Get function. */
91 nlua_getenv( naevL, ev->env, func );
92}
93
101int event_run( unsigned int eventid, const char *func )
102{
103 int ret;
104 event_runStart( eventid, func );
105 ret = event_runFunc( eventid, func, 0 );
106 return ret;
107}
108
114Event_t *event_getFromLua( lua_State *L )
115{
116 Event_t *ev;
117 uintptr_t *evptr;
118 nlua_getenv( L, __NLUA_CURENV, "__evt" );
119 evptr = lua_touserdata( L, -1 );
120 ev = evptr ? event_get( *evptr ) : NULL;
121 lua_pop( L, 1 );
122 return ev;
123}
124
131int event_runFunc( unsigned int eventid, const char *func, int nargs )
132{
133 int ret;
134 int evt_delete;
135 Event_t *ev;
136
137 ev = event_get( eventid );
138 if (ev == NULL)
139 return 0;
140
141 ret = nlua_pcall(ev->env, nargs, 0);
142 if (ret != 0) { /* error has occurred */
143 const char* err = (lua_isstring(naevL,-1)) ? lua_tostring(naevL,-1) : NULL;
144 if ((err==NULL) || (strcmp(err,NLUA_DONE)!=0)) {
145 WARN(_("Event '%s' -> '%s': %s"),
146 event_getData(ev->id), func, (err) ? err : _("unknown error"));
147 ret = -1;
148 }
149 else
150 ret = 1;
151 lua_pop(naevL, 1);
152 }
153
154 /* Time to remove the event. */
155 ev = event_get( eventid ); /* The Lua call may have invalidated the pointer. */
156 if (ev==NULL) {
157 WARN(_("Event deleted in middle of hook call! Likely caused by another hook finishing the event triggered by this hook (\"%s\")."), func);
158 return -1;
159 }
160 nlua_getenv(naevL, ev->env, "__evt_delete");
161 evt_delete = lua_toboolean(naevL,-1);
162 lua_pop(naevL,1);
163 if (evt_delete) {
164 ret = 2;
165 event_remove( ev->id );
166 }
167
168 return ret;
169}
170
185static int evtL_npcAdd( lua_State *L )
186{
187 unsigned int id;
188 int priority;
189 const char *func, *name, *desc;
190 glTexture *portrait, *bg;
191 Event_t *cur_event;
192
193 /* Handle parameters. */
194 func = luaL_checkstring(L, 1);
195 name = luaL_checkstring(L, 2);
196 portrait = luaL_validtex(L, 3, GFX_PATH"portraits/");
197 desc = luaL_checkstring(L, 4);
198
199 /* Optional parameters. */
200 priority = luaL_optinteger(L,5,5);
201 if (!lua_isnoneornil(L,6))
202 bg = luaL_validtex(L,6,GFX_PATH"portraits/");
203 else
204 bg = NULL;
205
206 cur_event = event_getFromLua(L);
207
208 /* Add npc. */
209 id = npc_add_event( cur_event->id, func, name, priority, portrait, desc, bg );
210
211 bar_regen();
212
213 /* Return ID. */
214 if (id > 0) {
215 lua_pushnumber( L, id );
216 return 1;
217 }
218 return 0;
219}
220
229static int evtL_npcRm( lua_State *L )
230{
231 unsigned int id;
232 int ret;
233 Event_t *cur_event;
234
235 id = luaL_checklong(L, 1);
236
237 cur_event = event_getFromLua(L);
238 ret = npc_rm_event( id, cur_event->id );
239
240 bar_regen();
241
242 if (ret != 0)
243 return NLUA_ERROR(L, _("Invalid NPC ID!"));
244 return 0;
245}
246
255static int evtL_finish( lua_State *L )
256{
257 const Event_t *cur_event = event_getFromLua(L);
258 int b = lua_toboolean(L,1);
259 lua_pushboolean( L, 1 );
260 nlua_setenv( L, cur_event->env, "__evt_delete" );
261
262 if (b && event_isUnique(cur_event->id))
263 player_eventFinished( cur_event->data );
264
265 lua_pushstring(L, NLUA_DONE);
266 lua_error(L); /* shouldn't return */
267
268 return 0;
269}
270
279static int evtL_save( lua_State *L )
280{
281 int b;
282 Event_t *cur_event = event_getFromLua(L);
283 if (lua_gettop(L)==0)
284 b = 1;
285 else
286 b = lua_toboolean(L,1);
287 cur_event->save = b;
288 return 0;
289}
290
309static int evtL_claim( lua_State *L )
310{
311 Claim_t *claim;
312 Event_t *cur_event;
313 int inclusive;
314
315 /* Get current event. */
316 cur_event = event_getFromLua(L);
317
318 inclusive = lua_toboolean(L,2);
319
320 /* Check to see if already claimed. */
321 if (!claim_isNull(cur_event->claims))
322 return NLUA_ERROR(L, _("Event trying to claim but already has."));
323
324 /* Create the claim. */
325 claim = claim_create( !inclusive );
326
327 /* Handle parameters. */
328 if (lua_istable(L,1)) {
329 /* Iterate over table. */
330 lua_pushnil(L);
331 while (lua_next(L, 1) != 0) {
332 if (lua_issystem(L,-1))
333 claim_addSys( claim, lua_tosystem( L, -1 ) );
334 else if (lua_isstring(L,-1))
335 claim_addStr( claim, lua_tostring( L, -1 ) );
336 lua_pop(L,1);
337 }
338 }
339 else if (lua_issystem(L, 1))
340 claim_addSys( claim, lua_tosystem( L, 1 ) );
341 else if (lua_isstring(L, 1))
342 claim_addStr( claim, lua_tostring( L, 1 ) );
343 else
344 NLUA_INVALID_PARAMETER(L,1);
345
346 /* Test claim. */
347 if (claim_test( claim )) {
348 claim_destroy( claim );
349 lua_pushboolean(L,0);
350 return 1;
351 }
352
353 /* Set the claim. */
354 cur_event->claims = claim;
355 claim_activate( claim );
356 lua_pushboolean(L,1);
357 return 1;
358}
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:251
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:382
Header file with generic functions and naev-specifics.
void event_runStart(unsigned int eventid, const char *func)
Starts running a function, allows programmer to set up arguments.
Definition nlua_evt.c:78
int event_runFunc(unsigned int eventid, const char *func, int nargs)
Runs a function previously set up with event_runStart.
Definition nlua_evt.c:131
static int evtL_claim(lua_State *L)
Tries to claim systems or strings.
Definition nlua_evt.c:309
static int evtL_finish(lua_State *L)
Finishes the event.
Definition nlua_evt.c:255
int event_run(unsigned int eventid, const char *func)
Runs the event function.
Definition nlua_evt.c:101
static int evtL_npcAdd(lua_State *L)
Event system Lua bindings.
Definition nlua_evt.c:185
static int evtL_npcRm(lua_State *L)
Removes an NPC.
Definition nlua_evt.c:229
Event_t * event_getFromLua(lua_State *L)
Gets the current running event from user data.
Definition nlua_evt.c:114
static int evtL_save(lua_State *L)
Saves an event.
Definition nlua_evt.c:279
static const luaL_Reg evtL_methods[]
Definition nlua_evt.c:53
int nlua_loadEvt(nlua_env env)
Loads the event Lua library.
Definition nlua_evt.c:69
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:115
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:234
void player_eventFinished(int id)
Marks a event as completed.
Definition player.c:2985
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:36