From 90b169d8c86db5e27677125262d2a2332c9d6658 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 10 Jun 2012 18:56:10 +0000 Subject: [PATCH] Lunatic: in event interface, pass actor, player, dist. git-svn-id: https://svn.eduke32.com/eduke32@2746 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/gameexec.c | 4 ++-- polymer/eduke32/source/lunatic/lunatic.c | 23 +++++++++++++---------- polymer/eduke32/source/lunatic/lunatic.h | 7 ++++++- polymer/eduke32/source/lunatic/test.elua | 8 +++++++- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 8a6d176a4..e57f81b1e 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -97,8 +97,8 @@ void VM_ScriptInfo(void) int32_t VM_OnEvent(int32_t iEventID, int32_t iActor, int32_t iPlayer, int32_t lDist, int32_t iReturn) { #ifdef LUNATIC - if (El_IsInitialized(&g_ElState)) - El_CallEvent(&g_ElState, iEventID); + if (El_IsInitialized(&g_ElState) && El_HaveEvent(iEventID)) + El_CallEvent(&g_ElState, iEventID, iActor, iPlayer, lDist); #endif if (!apScriptGameEvent[iEventID]) return iReturn; diff --git a/polymer/eduke32/source/lunatic/lunatic.c b/polymer/eduke32/source/lunatic/lunatic.c index 360e4f34a..2e97d8c2d 100644 --- a/polymer/eduke32/source/lunatic/lunatic.c +++ b/polymer/eduke32/source/lunatic/lunatic.c @@ -10,12 +10,12 @@ #include "osd.h" #include "gameexec.h" -#include "gamedef.h" // EventNames[] +#include "gamedef.h" // EventNames[], MAXEVENTS #include "lunatic.h" // this serves two purposes: // the values as booleans and the addresses as keys to the Lua registry -static uint8_t g_elEvents[MAXEVENTS]; +uint8_t g_elEvents[MAXEVENTS]; // forward-decls... @@ -150,30 +150,33 @@ static int32_t SetEvent_luacf(lua_State *L) return 0; } -int32_t El_CallEvent(El_State *estate, int32_t eventidx) +int32_t El_CallEvent(El_State *estate, int32_t eventidx, int32_t iActor, int32_t iPlayer, int32_t lDist) { // XXX: estate must be the one where the events were registered... // make a global? int32_t i; - if (!g_elEvents[eventidx]) - return 0; + lua_State *const L = estate->L; - lua_pushlightuserdata(estate->L, &g_elEvents[eventidx]); // push address - lua_gettable(estate->L, LUA_REGISTRYINDEX); // get lua function + lua_pushlightuserdata(L, &g_elEvents[eventidx]); // push address + lua_gettable(L, LUA_REGISTRYINDEX); // get lua function + + lua_pushinteger(L, iActor); + lua_pushinteger(L, iPlayer); + lua_pushinteger(L, lDist); // -- call it! -- - i = lua_pcall(estate->L, 0, 0, 0); + i = lua_pcall(L, 3, 0, 0); if (i == LUA_ERRMEM) // XXX: should be more sophisticated. Clean up stack? Do GC? return -1; if (i == LUA_ERRRUN) { OSD_Printf("event \"%s\" (state \"%s\") runtime error: %s\n", EventNames[eventidx].text, - estate->name, lua_tostring(estate->L, 1)); // get err msg - lua_pop(estate->L, 1); + estate->name, lua_tostring(L, 1)); // get err msg + lua_pop(L, 1); return 4; } diff --git a/polymer/eduke32/source/lunatic/lunatic.h b/polymer/eduke32/source/lunatic/lunatic.h index 0c6676353..684d0e5c5 100644 --- a/polymer/eduke32/source/lunatic/lunatic.h +++ b/polymer/eduke32/source/lunatic/lunatic.h @@ -5,18 +5,23 @@ #include +#include "gamedef.h" // EventNames[], MAXEVENTS + + typedef struct { char *name; lua_State *L; } El_State; +extern uint8_t g_elEvents[MAXEVENTS]; // shouldn't be used directly // -- functions -- int32_t El_CreateState(El_State *estate, const char *name); void El_DestroyState(El_State *estate); static inline int32_t El_IsInitialized(const El_State *estate) { return (estate->L != NULL); } +static inline int32_t El_HaveEvent(int32_t eventidx) { return g_elEvents[eventidx]!=0; } int32_t El_RunOnce(El_State *estate, const char *fn); -int32_t El_CallEvent(El_State *estate, int32_t eventidx); +int32_t El_CallEvent(El_State *estate, int32_t eventidx, int32_t iActor, int32_t iPlayer, int32_t lDist); #endif diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 22a7df2f2..9946154f8 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -4,6 +4,10 @@ print('---=== ELua Test script ===---') +local function printf(fmt, ...) + print(string.format(fmt, ...)) +end + local function checkfail(funcstr) local status, res = pcall(DBG_.loadstring(funcstr)) if (status) then @@ -107,7 +111,9 @@ checkfail('string.dump(setevent)') -- string.dump is unavailable -- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains. checkfail('gv.luaJIT_setmode(nil, 0, 0)') -setevent(gv.EVENT_JUMP, function() print("jump!") end) +setevent(gv.EVENT_JUMP, function(actori, playeri, dist) + printf("jump i=%d p=%d d=%d", actori, playeri, dist) + end) print('---=== END TEST SCRIPT ===---')