/
nlua_evt.c
370 lines (308 loc) · 7.61 KB
/
nlua_evt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file nlua_evt.c
*
* @brief Handles the event lua bindings.
*/
#include "nlua_evt.h"
#include "naev.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include "nlua.h"
#include "nluadef.h"
#include "nlua_system.h"
#include "nlua_hook.h"
#include "log.h"
#include "event.h"
#include "mission.h"
#include "player.h"
#include "npc.h"
/**
* @brief Event system Lua bindings.
*
* An example would be:
* @code
* evt.finish() -- finishes the event
* @endcode
*
* @luamod evt
*/
/*
* libraries
*/
/* evt */
static int evt_npcAdd( lua_State *L );
static int evt_npcRm( lua_State *L );
static int evt_finish( lua_State *L );
static int evt_save( lua_State *L );
static int evt_claim( lua_State *L );
static const luaL_reg evt_methods[] = {
{ "npcAdd", evt_npcAdd },
{ "npcRm", evt_npcRm },
{ "save", evt_save },
{ "finish", evt_finish },
{ "claim", evt_claim },
{0,0}
}; /**< Mission lua methods. */
/*
* individual library loading
*/
/**
* @brief Loads the event lua library.
* @param L Lua state.
*/
int nlua_loadEvt( lua_State *L )
{
luaL_register(L, "evt", evt_methods);
return 0;
}
/**
* @brief Sets up the Lua environment to run a function.
*/
lua_State *event_setupLua( Event_t *ev, const char *func )
{
lua_State *L;
/* Load event. */
L = ev->L;
#if DEBUGGING
lua_pushcfunction(L, nlua_errTrace);
#endif /* DEBUGGING */
/* Set up event pointer. */
lua_pushlightuserdata( L, ev );
lua_setglobal( L, "__evt" );
/* Get function. */
lua_getglobal(L, func );
return L;
}
/**
* @brief Runs the Lua for an event.
*/
int event_runLua( Event_t *ev, const char *func )
{
event_setupLua( ev, func );
return event_runLuaFunc( ev, func, 0 );
}
/**
* @brief Gets the current running event from user data.
*/
Event_t *event_getFromLua( lua_State *L )
{
Event_t *ev;
lua_getglobal( L, "__evt" );
ev = (Event_t*) lua_touserdata( L, -1 );
lua_pop( L, 1 );
return ev;
}
/**
* @brief Runs a Lua func with nargs.
*
* @return -1 on error, 1 on misn.finish() call, 2 if event got deleted
* and 0 normally.
*/
int event_runLuaFunc( Event_t *ev, const char *func, int nargs )
{
int ret, errf;
const char* err;
lua_State *L;
int evt_delete;
/* Comfortability. */
L = ev->L;
#if DEBUGGING
errf = -2-nargs;
#else /* DEBUGGING */
errf = 0;
#endif /* DEBUGGING */
ret = lua_pcall(L, nargs, 0, errf);
if (ret != 0) { /* error has occured */
err = (lua_isstring(L,-1)) ? lua_tostring(L,-1) : NULL;
if ((err==NULL) || (strcmp(err,"__done__")!=0)) {
WARN("Event '%s' -> '%s': %s",
event_getData(ev->id), func, (err) ? err : "unknown error");
ret = -1;
}
else
ret = 1;
lua_pop(L, 1);
}
#if DEBUGGING
lua_pop(L, 1);
#endif /* DEBUGGING */
/* Time to remove the event. */
lua_getglobal( L, "__evt_delete" );
evt_delete = lua_toboolean(L,-1);
lua_pop(L,1);
if (evt_delete) {
ret = 2;
event_remove( ev->id );
}
return ret;
}
/**
* @brief Adds an NPC.
*
* @usage npc_id = evt.npcAdd( "my_func", "Mr. Test", "none", "A test." ) -- Creates an NPC.
*
* @luaparam func Name of the function to run when approaching.
* @luaparam name Name of the NPC
* @luaparam portrait Portrait to use for the NPC (from gfx/portraits/).
* @luaparam desc Description assosciated to the NPC.
* @luaparam priority Optional priority argument (defaults to 5, highest is 0, lowest is 10).
* @luareturn The ID of the NPC to pass to npcRm.
* @luafunc npcAdd( func, name, portrait, desc, priority )
*/
static int evt_npcAdd( lua_State *L )
{
unsigned int id;
int priority;
const char *func, *name, *gfx, *desc;
char portrait[PATH_MAX];
Event_t *cur_event;
/* Handle parameters. */
func = luaL_checkstring(L, 1);
name = luaL_checkstring(L, 2);
gfx = luaL_checkstring(L, 3);
desc = luaL_checkstring(L, 4);
/* Optional priority. */
if (lua_gettop(L) > 4)
priority = luaL_checkint( L, 5 );
else
priority = 5;
/* Set path. */
snprintf( portrait, PATH_MAX, "gfx/portraits/%s.png", gfx );
cur_event = event_getFromLua(L);
/* Add npc. */
id = npc_add_event( cur_event->id, func, name, priority, portrait, desc );
/* Return ID. */
if (id > 0) {
lua_pushnumber( L, id );
return 1;
}
return 0;
}
/**
* @brief Removes an NPC.
*
* @usage evt.npcRm( npc_id )
*
* @luaparam id ID of the NPC to remove.
* @luafunc npcRm( id )
*/
static int evt_npcRm( lua_State *L )
{
unsigned int id;
int ret;
Event_t *cur_event;
id = luaL_checklong(L, 1);
cur_event = event_getFromLua(L);
ret = npc_rm_event( id, cur_event->id );
if (ret != 0)
NLUA_ERROR(L, "Invalid NPC ID!");
return 0;
}
/**
* @brief Finishes the event.
*
* @luaparam properly If true and the event is unique it marks the event
* as completed. If false or nil it deletes the event but
* doesn't mark it as completed.
* @luafunc finish( properly )
*/
static int evt_finish( lua_State *L )
{
int b;
Event_t *cur_event;
b = lua_toboolean(L,1);
lua_pushboolean( L, b );
lua_setglobal( L, "__evt_delete" );
cur_event = event_getFromLua(L);
if (b && event_isUnique(cur_event->id))
player_eventFinished( cur_event->data );
lua_pushstring(L, "__done__");
lua_error(L); /* shouldn't return */
return 0;
}
/**
* @brief Saves an event.
*
* @usage evt.save() -- Saves an event, which is by default disabled.
*
* @luaparam enable If true or nil sets the event to save, otherwise tells the event to not save.
* @luafunc save( enable )
*/
static int evt_save( lua_State *L )
{
int b;
Event_t *cur_event;
if (lua_gettop(L)==0)
b = 1;
else
b = lua_toboolean(L,1);
cur_event = event_getFromLua(L);
cur_event->save = b;
return 0;
}
/**
* @brief Tries to claim systems.
*
* Claiming systems is a way to avoid mission/event collisions preemptively.
*
* Note it does not actually claim the systems if it fails to claim. It also
* does not work more then once.
*
* @usage if not evt.claim( { system.get("Gamma Polaris") } ) then evt.finish( false ) end
* @usage if not evt.claim( system.get("Gamma Polaris") ) then evt.finish( false ) end
*
* @luaparam systems Table of systems to claim or a single system.
* @luareturn true if was able to claim, false otherwise.
* @luafunc claim( systems )
*/
static int evt_claim( lua_State *L )
{
LuaSystem *ls;
SysClaim_t *claim;
Event_t *cur_event;
/* Get current event. */
cur_event = event_getFromLua(L);
/* Check to see if already claimed. */
if (cur_event->claims != NULL) {
NLUA_ERROR(L, "Event trying to claim but already has.");
return 0;
}
/* Create the claim. */
claim = claim_create();
/* Handle parameters. */
if (lua_istable(L,1)) {
/* Iterate over table. */
lua_pushnil(L);
while (lua_next(L, 1) != 0) {
if (lua_issystem(L,-1)) {
ls = lua_tosystem( L, -1 );
claim_add( claim, ls->id );
}
lua_pop(L,1);
}
}
else if (lua_issystem(L, 1)) {
ls = lua_tosystem( L, 1 );
claim_add( claim, ls->id );
}
else
NLUA_INVALID_PARAMETER(L);
/* Test claim. */
if (claim_test( claim )) {
claim_destroy( claim );
lua_pushboolean(L,0);
return 1;
}
/* Set the claim. */
cur_event->claims = claim;
claim_activate( claim );
lua_pushboolean(L,1);
return 1;
}