naev 0.10.4
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 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 const char* err;
135 int evt_delete;
136 Event_t *ev;
137
138 ev = event_get( eventid );
139 if (ev == NULL)
140 return 0;
141
142 ret = nlua_pcall(ev->env, nargs, 0);
143 if (ret != 0) { /* error has occurred */
144 err = (lua_isstring(naevL,-1)) ? lua_tostring(naevL,-1) : NULL;
145 if ((err==NULL) || (strcmp(err,NLUA_DONE)!=0)) {
146 WARN(_("Event '%s' -> '%s': %s"),
147 event_getData(ev->id), func, (err) ? err : _("unknown error"));
148 ret = -1;
149 }
150 else
151 ret = 1;
152 lua_pop(naevL, 1);
153 }
154
155 /* Time to remove the event. */
156 ev = event_get( eventid ); /* The Lua call may have invalidated the pointer. */
157 nlua_getenv(naevL, ev->env, "__evt_delete");
158 evt_delete = lua_toboolean(naevL,-1);
159 lua_pop(naevL,1);
160 if (evt_delete) {
161 ret = 2;
162 event_remove( ev->id );
163 }
164
165 return ret;
166}
167
182static int evtL_npcAdd( lua_State *L )
183{
184 unsigned int id;
185 int priority;
186 const char *func, *name, *desc;
187 glTexture *portrait, *bg;
188 Event_t *cur_event;
189
190 /* Handle parameters. */
191 func = luaL_checkstring(L, 1);
192 name = luaL_checkstring(L, 2);
193 portrait = luaL_validtex(L, 3, GFX_PATH"portraits/");
194 desc = luaL_checkstring(L, 4);
195
196 /* Optional parameters. */
197 priority = luaL_optinteger(L,5,5);
198 if (!lua_isnoneornil(L,6))
199 bg = luaL_validtex(L,6,GFX_PATH"portraits/");
200 else
201 bg = NULL;
202
203 cur_event = event_getFromLua(L);
204
205 /* Add npc. */
206 id = npc_add_event( cur_event->id, func, name, priority, portrait, desc, bg );
207
208 bar_regen();
209
210 /* Return ID. */
211 if (id > 0) {
212 lua_pushnumber( L, id );
213 return 1;
214 }
215 return 0;
216}
217
226static int evtL_npcRm( lua_State *L )
227{
228 unsigned int id;
229 int ret;
230 Event_t *cur_event;
231
232 id = luaL_checklong(L, 1);
233
234 cur_event = event_getFromLua(L);
235 ret = npc_rm_event( id, cur_event->id );
236
237 bar_regen();
238
239 if (ret != 0)
240 NLUA_ERROR(L, _("Invalid NPC ID!"));
241 return 0;
242}
243
252static int evtL_finish( lua_State *L )
253{
254 Event_t *cur_event = event_getFromLua(L);
255 int b = lua_toboolean(L,1);
256 lua_pushboolean( L, 1 );
257 nlua_setenv( L, cur_event->env, "__evt_delete" );
258
259 if (b && event_isUnique(cur_event->id))
260 player_eventFinished( cur_event->data );
261
262 lua_pushstring(L, NLUA_DONE);
263 lua_error(L); /* shouldn't return */
264
265 return 0;
266}
267
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
307static int evtL_claim( lua_State *L )
308{
309 Claim_t *claim;
310 Event_t *cur_event;
311 int onlytest, inclusive;
312
313 /* Get current event. */
314 cur_event = event_getFromLua(L);
315
316 onlytest = lua_toboolean(L,2);
317 inclusive = lua_toboolean(L,3);
318
319 /* Check to see if already claimed. */
320 if (!onlytest && (cur_event->claims != NULL)) {
321 NLUA_ERROR(L, _("Event trying to claim but already has."));
322 return 0;
323 }
324
325 /* Create the claim. */
326 claim = claim_create( !inclusive );
327
328 /* Handle parameters. */
329 if (lua_istable(L,1)) {
330 /* Iterate over table. */
331 lua_pushnil(L);
332 while (lua_next(L, 1) != 0) {
333 if (lua_issystem(L,-1))
334 claim_addSys( claim, lua_tosystem( L, -1 ) );
335 else if (lua_isstring(L,-1))
336 claim_addStr( claim, lua_tostring( L, -1 ) );
337 lua_pop(L,1);
338 }
339 }
340 else if (lua_issystem(L, 1))
341 claim_addSys( claim, lua_tosystem( L, 1 ) );
342 else if (lua_isstring(L, 1))
343 claim_addStr( claim, lua_tostring( L, 1 ) );
344 else
345 NLUA_INVALID_PARAMETER(L);
346
347 /* Only test, but don't apply case. */
348 if (onlytest) {
349 lua_pushboolean( L, !claim_test( claim ) );
350 claim_destroy( claim );
351 return 1;
352 }
353
354 /* Test claim. */
355 if (claim_test( claim )) {
356 claim_destroy( claim );
357 lua_pushboolean(L,0);
358 return 1;
359 }
360
361 /* Set the claim. */
362 cur_event->claims = claim;
363 claim_activate( claim );
364 lua_pushboolean(L,1);
365 return 1;
366}
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
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:141
void event_remove(unsigned int eventid)
Removes an event by ID.
Definition: event.c:263
Event_t * event_get(unsigned int eventid)
Gets an event.
Definition: event.c:100
int event_isUnique(unsigned int eventid)
Checks to see if an event is unique.
Definition: event.c:156
void bar_regen(void)
Regenerates the bar list.
Definition: land.c:452
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:307
static int evtL_finish(lua_State *L)
Finishes the event.
Definition: nlua_evt.c:252
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:182
static int evtL_npcRm(lua_State *L)
Removes an NPC.
Definition: nlua_evt.c:226
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:276
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.
Definition: nlua_system.c:130
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
Definition: nlua_system.c:201
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:189
int npc_rm_event(unsigned int id, unsigned int evt)
removes an event NPC.
Definition: npc.c:233
void player_eventFinished(int id)
Marks a event as completed.
Definition: player.c:2951
static const double b[]
Definition: rng.c:256
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:34