Lunatic: in event interface, pass actor, player, dist.

git-svn-id: https://svn.eduke32.com/eduke32@2746 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-06-10 18:56:10 +00:00
parent 0ac242293b
commit 90b169d8c8
4 changed files with 28 additions and 14 deletions

View file

@ -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) int32_t VM_OnEvent(int32_t iEventID, int32_t iActor, int32_t iPlayer, int32_t lDist, int32_t iReturn)
{ {
#ifdef LUNATIC #ifdef LUNATIC
if (El_IsInitialized(&g_ElState)) if (El_IsInitialized(&g_ElState) && El_HaveEvent(iEventID))
El_CallEvent(&g_ElState, iEventID); El_CallEvent(&g_ElState, iEventID, iActor, iPlayer, lDist);
#endif #endif
if (!apScriptGameEvent[iEventID]) if (!apScriptGameEvent[iEventID])
return iReturn; return iReturn;

View file

@ -10,12 +10,12 @@
#include "osd.h" #include "osd.h"
#include "gameexec.h" #include "gameexec.h"
#include "gamedef.h" // EventNames[] #include "gamedef.h" // EventNames[], MAXEVENTS
#include "lunatic.h" #include "lunatic.h"
// this serves two purposes: // this serves two purposes:
// the values as booleans and the addresses as keys to the Lua registry // 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... // forward-decls...
@ -150,30 +150,33 @@ static int32_t SetEvent_luacf(lua_State *L)
return 0; 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... // XXX: estate must be the one where the events were registered...
// make a global? // make a global?
int32_t i; int32_t i;
if (!g_elEvents[eventidx]) lua_State *const L = estate->L;
return 0;
lua_pushlightuserdata(estate->L, &g_elEvents[eventidx]); // push address lua_pushlightuserdata(L, &g_elEvents[eventidx]); // push address
lua_gettable(estate->L, LUA_REGISTRYINDEX); // get lua function lua_gettable(L, LUA_REGISTRYINDEX); // get lua function
lua_pushinteger(L, iActor);
lua_pushinteger(L, iPlayer);
lua_pushinteger(L, lDist);
// -- call it! -- // -- 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? if (i == LUA_ERRMEM) // XXX: should be more sophisticated. Clean up stack? Do GC?
return -1; return -1;
if (i == LUA_ERRRUN) if (i == LUA_ERRRUN)
{ {
OSD_Printf("event \"%s\" (state \"%s\") runtime error: %s\n", EventNames[eventidx].text, OSD_Printf("event \"%s\" (state \"%s\") runtime error: %s\n", EventNames[eventidx].text,
estate->name, lua_tostring(estate->L, 1)); // get err msg estate->name, lua_tostring(L, 1)); // get err msg
lua_pop(estate->L, 1); lua_pop(L, 1);
return 4; return 4;
} }

View file

@ -5,18 +5,23 @@
#include <lua.h> #include <lua.h>
#include "gamedef.h" // EventNames[], MAXEVENTS
typedef struct typedef struct
{ {
char *name; char *name;
lua_State *L; lua_State *L;
} El_State; } El_State;
extern uint8_t g_elEvents[MAXEVENTS]; // shouldn't be used directly
// -- functions -- // -- functions --
int32_t El_CreateState(El_State *estate, const char *name); int32_t El_CreateState(El_State *estate, const char *name);
void El_DestroyState(El_State *estate); 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_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_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 #endif

View file

@ -4,6 +4,10 @@
print('---=== ELua Test script ===---') print('---=== ELua Test script ===---')
local function printf(fmt, ...)
print(string.format(fmt, ...))
end
local function checkfail(funcstr) local function checkfail(funcstr)
local status, res = pcall(DBG_.loadstring(funcstr)) local status, res = pcall(DBG_.loadstring(funcstr))
if (status) then 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. -- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains.
checkfail('gv.luaJIT_setmode(nil, 0, 0)') 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 ===---') print('---=== END TEST SCRIPT ===---')