From ebd7fc402b40dc9bf61b69389e82c15b5a5ff7e3 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Thu, 9 Feb 2012 22:45:18 +0000 Subject: [PATCH] Lunatic: interface to events. git-svn-id: https://svn.eduke32.com/eduke32@2329 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/gameexec.c | 9 +++ polymer/eduke32/source/lunatic/defs.ilua | 96 ++++++++++++++++++++++++ polymer/eduke32/source/lunatic/lunatic.c | 61 +++++++++++++++ polymer/eduke32/source/lunatic/lunatic.h | 2 +- polymer/eduke32/source/lunatic/test.elua | 3 + 5 files changed, 170 insertions(+), 1 deletion(-) diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 71fcf2fd3..7d1623171 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -34,6 +34,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "osd.h" #include "menus.h" +#ifdef LUNATIC_ENABLE +# include "lunatic.h" +extern El_State g_ElState; // this needs to go into a header sometime +#endif + #if KRANDDEBUG # define GAMEEXEC_INLINE # define GAMEEXEC_STATIC @@ -94,6 +99,10 @@ void VM_ScriptInfo(void) void VM_OnEvent(register int32_t iEventID, register int32_t iActor, register int32_t iPlayer, register int32_t lDist) { +#ifdef LUNATIC_ENABLE + if (El_IsInitialized(&g_ElState)) + El_CallEvent(&g_ElState, iEventID); +#endif if (!apScriptGameEvent[iEventID]) return; diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index aed484a51..31551f896 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -248,6 +248,99 @@ typedef struct { char display_bonus_screen; char show_level_text; } user_defs; + +// KEEPINSYNC2 +enum GameEvent_t { + EVENT_INIT, + EVENT_ENTERLEVEL, + EVENT_RESETWEAPONS, + EVENT_RESETINVENTORY, + EVENT_HOLSTER, + EVENT_LOOKLEFT, + EVENT_LOOKRIGHT, + EVENT_SOARUP, + EVENT_SOARDOWN, + EVENT_CROUCH, + EVENT_JUMP, + EVENT_RETURNTOCENTER, + EVENT_LOOKUP, + EVENT_LOOKDOWN, + EVENT_AIMUP, + EVENT_FIRE, + EVENT_CHANGEWEAPON, + EVENT_GETSHOTRANGE, + EVENT_GETAUTOAIMANGLE, + EVENT_GETLOADTILE, + EVENT_CHEATGETSTEROIDS, + EVENT_CHEATGETHEAT, + EVENT_CHEATGETBOOT, + EVENT_CHEATGETSHIELD, + EVENT_CHEATGETSCUBA, + EVENT_CHEATGETHOLODUKE, + EVENT_CHEATGETJETPACK, + EVENT_CHEATGETFIRSTAID, + EVENT_QUICKKICK, + EVENT_INVENTORY, + EVENT_USENIGHTVISION, + EVENT_USESTEROIDS, + EVENT_INVENTORYLEFT, + EVENT_INVENTORYRIGHT, + EVENT_HOLODUKEON, + EVENT_HOLODUKEOFF, + EVENT_USEMEDKIT, + EVENT_USEJETPACK, + EVENT_TURNAROUND, + EVENT_DISPLAYWEAPON, + EVENT_FIREWEAPON, + EVENT_SELECTWEAPON, + EVENT_MOVEFORWARD, + EVENT_MOVEBACKWARD, + EVENT_TURNLEFT, + EVENT_TURNRIGHT, + EVENT_STRAFELEFT, + EVENT_STRAFERIGHT, + EVENT_WEAPKEY1, + EVENT_WEAPKEY2, + EVENT_WEAPKEY3, + EVENT_WEAPKEY4, + EVENT_WEAPKEY5, + EVENT_WEAPKEY6, + EVENT_WEAPKEY7, + EVENT_WEAPKEY8, + EVENT_WEAPKEY9, + EVENT_WEAPKEY10, + EVENT_DRAWWEAPON, + EVENT_DISPLAYCROSSHAIR, + EVENT_DISPLAYREST, + EVENT_DISPLAYSBAR, + EVENT_RESETPLAYER, + EVENT_INCURDAMAGE, + EVENT_AIMDOWN, + EVENT_GAME, + EVENT_PREVIOUSWEAPON, + EVENT_NEXTWEAPON, + EVENT_SWIMUP, + EVENT_SWIMDOWN, + EVENT_GETMENUTILE, + EVENT_SPAWN, + EVENT_LOGO, + EVENT_EGS, + EVENT_DOFIRE, + EVENT_PRESSEDFIRE, + EVENT_USE, + EVENT_PROCESSINPUT, + EVENT_FAKEDOMOVETHINGS, + EVENT_DISPLAYROOMS, + EVENT_KILLIT, + EVENT_LOADACTOR, + EVENT_DISPLAYBONUSSCREEN, + EVENT_DISPLAYMENU, + EVENT_DISPLAYMENUREST, + EVENT_DISPLAYLOADINGSCREEN, + EVENT_ANIMATESPRITES, + EVENT_NEWGAME, + MAXEVENTS +}; ]] ffi.cdef[[ @@ -505,6 +598,9 @@ G_.getbunch = getbunch G_.TEMP_getvollev = TEMP_getvollev -- REMOVE G_.gamevar = gamevar +G_.setevent = setevent -- included in lunatic.c + + -- PiL 14.2 continued setmetatable( G_, { diff --git a/polymer/eduke32/source/lunatic/lunatic.c b/polymer/eduke32/source/lunatic/lunatic.c index def5aac0f..ef9d35e83 100644 --- a/polymer/eduke32/source/lunatic/lunatic.c +++ b/polymer/eduke32/source/lunatic/lunatic.c @@ -9,8 +9,18 @@ #include "cache1d.h" #include "osd.h" +#include "gameexec.h" +#include "gamedef.h" // EventNames[] #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]; + + +// forward-decls... +static int32_t SetEvent_luacf(lua_State *L); + // 0: success, -1: failure int32_t El_CreateState(El_State *estate, const char *name) @@ -30,6 +40,10 @@ int32_t El_CreateState(El_State *estate, const char *name) luaL_openlibs(estate->L); // XXX: only for internal use and testing, obviously + // create misc. global functions in the Lua state + lua_pushcfunction(estate->L, SetEvent_luacf); + lua_setglobal(estate->L, "setevent"); + return 0; } @@ -113,3 +127,50 @@ int32_t El_RunOnce(El_State *estate, const char *fn) return 0; } + +// setupevent(EVENT_..., lua_function) +static int32_t SetEvent_luacf(lua_State *L) +{ + int32_t eventidx = luaL_checkint(L, 1); + + luaL_argcheck(L, (unsigned)eventidx < MAXEVENTS, 1, "must be an event number (0 .. MAXEVENTS-1)"); + luaL_checktype(L, 2, LUA_TFUNCTION); + + lua_pushlightuserdata(L, &g_elEvents[eventidx]); // 3, push address + lua_pushvalue(L, 2); // 4, push copy of lua function + + lua_settable(L, LUA_REGISTRYINDEX); // "registry[&g_elEvents[eventidx]] = " + g_elEvents[eventidx] = 1; + + return 0; +} + +int32_t El_CallEvent(El_State *estate, int32_t eventidx) +{ + // XXX: estate must be the one where the events were registered... + // make a global? + + int32_t i; + + if (!g_elEvents[eventidx]) + return 0; + + lua_pushlightuserdata(estate->L, &g_elEvents[eventidx]); // push address + lua_gettable(estate->L, LUA_REGISTRYINDEX); // get lua function + + // -- call it! -- + + i = lua_pcall(estate->L, 0, 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); + return 4; + } + + return 0; +} diff --git a/polymer/eduke32/source/lunatic/lunatic.h b/polymer/eduke32/source/lunatic/lunatic.h index 119ffa775..e14f8aaf2 100644 --- a/polymer/eduke32/source/lunatic/lunatic.h +++ b/polymer/eduke32/source/lunatic/lunatic.h @@ -17,6 +17,6 @@ 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); } int32_t El_RunOnce(El_State *estate, const char *fn); - +int32_t El_CallEvent(El_State *estate, int32_t eventidx); #endif diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 48961bade..68f680b82 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -82,3 +82,6 @@ checkfail("new_global = 345") -- we should declare globals checkfail('gv.CEILING = 3') -- can't redefine constants in 'gv' print('--- end test script ---') + + +setevent(gv.EVENT_JUMP, function() print("jump!") end)