From 9229bb2c0af33736f3bc3dc3a4557b4411a6aac0 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sun, 5 Feb 2017 21:08:01 +0100 Subject: [PATCH] Add entity_v(), node_v() Lua functions --- Aquaria/Path.cpp | 5 +++ Aquaria/Path.h | 1 + Aquaria/ScriptInterface.cpp | 68 ++++++++++++++++++++++++++++++++++--- Aquaria/ScriptInterface.h | 2 ++ Aquaria/ScriptedEntity.cpp | 5 +++ Aquaria/ScriptedEntity.h | 1 + 6 files changed, 78 insertions(+), 4 deletions(-) diff --git a/Aquaria/Path.cpp b/Aquaria/Path.cpp index 8d4187c..8f1b6df 100644 --- a/Aquaria/Path.cpp +++ b/Aquaria/Path.cpp @@ -724,6 +724,11 @@ void Path::luaDebugMsg(const std::string &func, const std::string &msg) debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg); } +int Path::pushLuaVars(lua_State *L) +{ + return script ? script->pushLocalVars(L) : 0; +} + MinimapIcon *Path::ensureMinimapIcon() { if(!minimapIcon) diff --git a/Aquaria/Path.h b/Aquaria/Path.h index f445733..777eb77 100644 --- a/Aquaria/Path.h +++ b/Aquaria/Path.h @@ -157,6 +157,7 @@ public: int messageVariadic(lua_State *L, int nparams); void luaDebugMsg(const std::string &func, const std::string &msg); + int pushLuaVars(lua_State *L); }; #endif diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 9a5f7ef..19f3b74 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -414,10 +414,22 @@ static void ensureType(lua_State *L, T *& ptr, ScriptObjectType ty) } } } +template +static void ensureTypeNoError(lua_State *L, T *& ptr, ScriptObjectType ty) +{ + if (ptr) + { + ScriptObject *so = (ScriptObject*)(ptr); + if (!so->isType(ty)) + ptr = NULL; + } +} # define ENSURE_TYPE(ptr, ty) ensureType(L, (ptr), (ty)) +# define ENSURE_TYPE_NO_ERROR(ptr, ty) ensureTypeNoError(L, (ptr), (ty)) # define typecheckOnly(func) func #else # define ENSURE_TYPE(ptr, ty) +# define ENSURE_TYPE_NO_ERROR(ptr, ty) # define typecheckOnly(func) #endif @@ -441,6 +453,18 @@ ScriptedEntity *scriptedEntity(lua_State *L, int slot = 1) return se; } +static inline +ScriptedEntity *scriptedEntityOpt(lua_State *L, int slot = 1) +{ + ScriptedEntity *se = (ScriptedEntity*)lua_touserdata(L, slot); + ENSURE_TYPE_NO_ERROR(se, SCO_SCRIPTED_ENTITY); // Dont't error if not ScriptedEntity... + if (se) + return se; + + ENSURE_TYPE(se, SCO_ENTITY); // ... but error if not even Entity + return NULL; // still return NULL +} + static inline CollideEntity *collideEntity(lua_State *L, int slot = 1) { @@ -708,14 +732,23 @@ static void safePath(lua_State *L, const std::string& path) #define luaReturnVec4(x,y,z,w) do {lua_pushnumber(L, (x)); lua_pushnumber(L, (y)); lua_pushnumber(L, (z)); lua_pushnumber(L, (w)); return 4;} while(0) #define luaReturnNil() return 0; -// Set the global "v" to the instance's local variable table. Must be -// called when starting a script. -static void fixupLocalVars(lua_State *L) +static void pushLocalVarTab(lua_State *L, lua_State *Lv) { lua_getglobal(L, "_threadvars"); - lua_pushlightuserdata(L, L); + // [_thv] + lua_pushlightuserdata(L, Lv); + // [_thv][L] lua_gettable(L, -2); + // [_thv][v] lua_remove(L, -2); + // [v] +} + +// Set the global "v" to the instance's local variable table. Must be +// called when starting a script. +static void fixupLocalVars(lua_State *L) +{ + pushLocalVarTab(L, L); lua_setglobal(L, "v"); } @@ -3003,6 +3036,19 @@ luaFunc(entity_getTargetPriority) luaReturnInt(e ? e->targetPriority : 0); } +// returns nil for non-scripted entities +luaFunc(entity_v) +{ + ScriptedEntity *se = scriptedEntityOpt(L); + return se ? se->pushLuaVars(L) : 0; +} + +luaFunc(node_v) +{ + Path *n = path(L); + return n ? n->pushLuaVars(L) : 0; +} + luaFunc(isQuitFlag) { luaReturnBool(dsq->isQuitFlag()); @@ -7894,6 +7940,11 @@ luaFunc(getNextFilteredEntity) return 2; } +// MISSING FOR ANDROID COMPAT +//luaFunc(getEntityList) +//luaFunc(entity_getEntityListInRange) // (me, range) +//luaFunc(vector_getEntityListInRange) // (x, y, range) + luaFunc(getEntity) { Entity *ent = 0; @@ -9802,6 +9853,9 @@ static const struct { luaRegister(entity_setActivationType), + luaRegister(entity_v), + luaRegister(node_v), + luaRegister(isQuitFlag), luaRegister(isDeveloperKeys), luaRegister(isDemo), @@ -12007,3 +12061,9 @@ int Script::callVariadic(const char *name, lua_State *fromL, int nparams, void * return nparams; } + +int Script::pushLocalVars(lua_State *Ltarget) +{ + pushLocalVarTab(Ltarget, L); + return 1; +} diff --git a/Aquaria/ScriptInterface.h b/Aquaria/ScriptInterface.h index 4230ea9..5e0f70b 100644 --- a/Aquaria/ScriptInterface.h +++ b/Aquaria/ScriptInterface.h @@ -72,6 +72,8 @@ public: // function(pointer, ...) - anything that is already on the stack is forwarded. Results are left on the stack. // Returns how many values the called function returned, or -1 in case of error. int callVariadic(const char *name, lua_State *L, int nparams, void *param); + // Pushes the entity's "v" table on top of the passed Lua stack. Returns number of things pushed (0 or 1) + int pushLocalVars(lua_State *Ltarget); lua_State *getLuaState() {return L;} const std::string &getFile() {return file;} diff --git a/Aquaria/ScriptedEntity.cpp b/Aquaria/ScriptedEntity.cpp index 67d3e13..0beec25 100644 --- a/Aquaria/ScriptedEntity.cpp +++ b/Aquaria/ScriptedEntity.cpp @@ -112,6 +112,11 @@ int ScriptedEntity::messageVariadic(lua_State *L, int nparams) return Entity::messageVariadic(L, nparams); } +int ScriptedEntity::pushLuaVars(lua_State *L) +{ + return script ? script->pushLocalVars(L) : 0; +} + void ScriptedEntity::warpSegments() { Segmented::warpSegments(position); diff --git a/Aquaria/ScriptedEntity.h b/Aquaria/ScriptedEntity.h index 763b870..5fc0da3 100644 --- a/Aquaria/ScriptedEntity.h +++ b/Aquaria/ScriptedEntity.h @@ -50,6 +50,7 @@ public: void entityDied(Entity *e); void message(const std::string &msg, int v); int messageVariadic(lua_State *L, int nparams); + int pushLuaVars(lua_State *L); static bool runningActivation;