From 01295cda7a947a96d29267ad555353020fd0b96e Mon Sep 17 00:00:00 2001 From: fgenesis Date: Fri, 24 Feb 2023 02:10:33 +0100 Subject: [PATCH] Changes to entity/node activation: - Lua func node_activate() is now variadic and can pass any params to the target's activate() - Add Lua func entity_activate() that works in the same manner - Activating nodes/entities via RMB now passes (naija ptr, int input source) to activate() - Don't delay entity activation by 1 frame - Minor cleanups to some Lua functions; no functional changes there --- Aquaria/Avatar.cpp | 11 ++--- Aquaria/Avatar.h | 1 - Aquaria/Entity.h | 3 +- Aquaria/Path.cpp | 21 +++++++-- Aquaria/Path.h | 4 +- Aquaria/ScriptInterface.cpp | 92 ++++++++++++++++++++----------------- Aquaria/ScriptInterface.h | 2 + Aquaria/ScriptedEntity.cpp | 24 +++++++--- Aquaria/ScriptedEntity.h | 4 +- 9 files changed, 96 insertions(+), 66 deletions(-) diff --git a/Aquaria/Avatar.cpp b/Aquaria/Avatar.cpp index 65a2ad5..6debfe6 100644 --- a/Aquaria/Avatar.cpp +++ b/Aquaria/Avatar.cpp @@ -3036,12 +3036,12 @@ void Avatar::rmbu(int source, InputDevice device) if (entityToActivate) { - activateEntity = entityToActivate; + entityToActivate->activate(this, source); entityToActivate = 0; } if (pathToActivate) { - pathToActivate->activate(); + pathToActivate->activate(this, source); pathToActivate = 0; } } @@ -3661,7 +3661,6 @@ Avatar::Avatar() : Entity(), ActionMapper() damageTime = vars->avatarDamageTime; - activateEntity = 0; canMove = true; //scale = Vector(0.5, 0.5); scale = Vector(0.5, 0.5); @@ -5301,11 +5300,7 @@ void Avatar::onUpdate(float dt) } applyRidingPosition(); - if (activateEntity) - { - activateEntity->activate(); - activateEntity = 0; - } + if (bone_head) headPosition = bone_head->getWorldPosition(); diff --git a/Aquaria/Avatar.h b/Aquaria/Avatar.h index be7fb2d..8662108 100644 --- a/Aquaria/Avatar.h +++ b/Aquaria/Avatar.h @@ -415,7 +415,6 @@ protected: float revertTimer; void endCharge(); - Entity *activateEntity; bool canMove; void onEnterState(int action); diff --git a/Aquaria/Entity.h b/Aquaria/Entity.h index 5e0689a..acf6666 100644 --- a/Aquaria/Entity.h +++ b/Aquaria/Entity.h @@ -105,6 +105,7 @@ public: virtual void message(const std::string &msg, int v) {} virtual int messageVariadic(lua_State *L, int nparams) { return 0; } + virtual int activateVariadic(lua_State *L, int nparams) { return 0; } bool isUnderWater(const Vector &o=Vector()); @@ -196,7 +197,7 @@ public: Entity *getTargetEntity(int t=0); void setTargetEntity(Entity *e, int t=0); - virtual void activate(){} + virtual void activate(Entity *by, int source){} SkeletalSprite skeletalSprite; diff --git a/Aquaria/Path.cpp b/Aquaria/Path.cpp index 0635754..ca366f5 100644 --- a/Aquaria/Path.cpp +++ b/Aquaria/Path.cpp @@ -644,11 +644,11 @@ bool Path::action(int id, int state, int source, InputDevice device) return true; } -void Path::activate(Entity *e) +void Path::activate(Entity *e, int source) { if (hasScript() && activateFunction) { - if (!script->call("activate", this, e)) + if (!script->call("activate", this, e, source)) { luaDebugMsg("activate", script->getLastError()); activateFunction = false; @@ -656,6 +656,7 @@ void Path::activate(Entity *e) } } + void Path::removeNode(size_t idx) { std::vector copy = nodes; @@ -701,19 +702,29 @@ void Path::addNode(size_t idx) } } -int Path::messageVariadic(lua_State *L, int nparams) +int Path::callVariadic(const char* func, lua_State* L, int nparams) { if (script) { - int res = script->callVariadic("msg", L, nparams, this); + int res = script->callVariadic(func, L, nparams, this); if (res < 0) - luaDebugMsg("msg", script->getLastError()); + luaDebugMsg(func, script->getLastError()); else return res; } return 0; } +int Path::messageVariadic(lua_State *L, int nparams) +{ + return callVariadic("msg", L, nparams); +} + +int Path::activateVariadic(lua_State* L, int nparams) +{ + return callVariadic("activate", L, nparams); +} + void Path::luaDebugMsg(const std::string &func, const std::string &msg) { debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg); diff --git a/Aquaria/Path.h b/Aquaria/Path.h index 3af4727..dbf142a 100644 --- a/Aquaria/Path.h +++ b/Aquaria/Path.h @@ -109,7 +109,7 @@ public: Vector getEnterPosition(int outAmount=1); Vector getEnterNormal(); - void activate(Entity *e=0); + void activate(Entity *e, int source); void refreshScript(); MinimapIcon *ensureMinimapIcon(); @@ -155,7 +155,9 @@ public: void parseWarpNodeData(const std::string &dataString); + int callVariadic(const char *func, lua_State *L, int nparams); int messageVariadic(lua_State *L, int nparams); + int activateVariadic(lua_State *L, int nparams); void luaDebugMsg(const std::string &func, const std::string &msg); }; diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 2bae152..dd46dbf 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -579,6 +579,17 @@ Entity *entity(lua_State *L, int slot = 1) return ent; } +static inline +Entity *entityOpt(lua_State *L, int slot = 1) +{ + Entity *ent = (Entity*)lua_touserdata(L, slot); + if(ent) + { + ENSURE_TYPE(ent, SCO_ENTITY); + } + return ent; +} + static inline Vector getVector(lua_State *L, int slot = 1) { @@ -3042,9 +3053,7 @@ luaFunc(entity_getLookAtPoint) luaFunc(entity_setRiding) { Entity *e = entity(L); - Entity *e2 = 0; - if (lua_touserdata(L, 2) != NULL) - e2 = entity(L, 2); + Entity *e2 = entityOpt(L, 2); if (e) { e->setRiding(e2); @@ -3351,10 +3360,7 @@ luaFunc(entity_findNearestEntityOfType) luaFunc(createShot) { Entity *e = entity(L,2); - Entity *t = 0; - if (lua_touserdata(L, 3) != NULL) - t = entity(L,3); - Shot *s = 0; + Entity *t = entityOpt(L, 3); Vector pos, aim; pos.x = lua_tonumber(L, 4); pos.y = lua_tonumber(L, 5); @@ -3362,7 +3368,7 @@ luaFunc(createShot) aim.y = lua_tonumber(L, 7); - s = dsq->game->fireShot(getString(L, 1), e, t, pos, aim); + Shot *s = dsq->game->fireShot(getString(L, 1), e, t, pos, aim); luaReturnPtr(s); } @@ -4416,37 +4422,21 @@ luaFunc(cam_toNode) luaFunc(cam_toEntity) { - if (lua_touserdata(L, 1) == NULL) - { - Vector *pos = 0; - dsq->game->setCameraFollow(pos); - } + Entity *e = entityOpt(L); + if(e) + dsq->game->setCameraFollowEntity(e); else - { - Entity *e = entity(L); - if (e) - { - dsq->game->setCameraFollowEntity(e); - } - } + dsq->game->setCameraFollow((Vector*)NULL); luaReturnNil(); } luaFunc(cam_setPosition) { - float x = lua_tonumber(L, 1); - float y = lua_tonumber(L, 2); - float time = lua_tonumber(L, 3); - int loopType = lua_tointeger(L, 4); - bool pingPong = getBool(L, 5); - bool ease = getBool(L, 6); + InterpolatedVector& camvec = dsq->game->cameraInterp; + camvec.stop(); + interpolateVec2(L, camvec, 1); - Vector p(x,y); - - dsq->game->cameraInterp.stop(); - dsq->game->cameraInterp.interpolateTo(p, time, loopType, pingPong, ease); - - dsq->cameraPos = dsq->game->getCameraPositionFor(dsq->game->cameraInterp); + dsq->cameraPos = dsq->game->getCameraPositionFor(camvec); luaReturnNil(); } @@ -6318,14 +6308,9 @@ luaFunc(entity_getPrevState) luaFunc(entity_setTarget) { Entity *e = entity(L); - Entity *t = 0; - if (lua_touserdata(L, 2) != NULL) - { - t = entity(L, 2); - } if (e) { - e->setTargetEntity(t); + e->setTargetEntity(entityOpt(L, 2)); } luaReturnNil(); } @@ -7599,12 +7584,25 @@ luaFunc(getEntityByID) luaFunc(node_activate) { Path *p = path(L); - Entity *e = 0; - if (lua_touserdata(L, 2) != NULL) - e = entity(L, 2); if (p) { - p->activate(e); + // pass everything on the stack except the node pointer + int res = p->activateVariadic(L, lua_gettop(L) - 1); + if (res >= 0) + return res; + } + luaReturnNil(); +} + +luaFunc(entity_activate) +{ + Entity *e = entity(L); + if (e) + { + // pass everything on the stack except the entity pointer + int res = e->activateVariadic(L, lua_gettop(L) - 1); + if (res >= 0) + return res; } luaReturnNil(); } @@ -10654,6 +10652,7 @@ static const struct { luaRegister(loadSound), luaRegister(node_activate), + luaRegister(entity_activate), luaRegister(node_getName), luaRegister(node_getLabel), luaRegister(node_getPathPosition), @@ -12189,6 +12188,15 @@ bool Script::call(const char *name, void *param1, const char *param2, void *para return doCall(3); } +bool Script::call(const char *name, void *param1, void *param2, int param3) +{ + lookupFunc(name); + luaPushPointer(L, param1); + luaPushPointer(L, param2); + lua_pushinteger(L, param3); + return doCall(3); +} + bool Script::call(const char *name, void *param1, void *param2, void *param3) { lookupFunc(name); diff --git a/Aquaria/ScriptInterface.h b/Aquaria/ScriptInterface.h index ec37452..885ce4f 100644 --- a/Aquaria/ScriptInterface.h +++ b/Aquaria/ScriptInterface.h @@ -55,6 +55,8 @@ public: bool call(const char *name, void *param1, const char *param2, float param3); // function(pointer, string, pointer) bool call(const char *name, void *param1, const char *param2, void *param3); + // function(pointer, pointer, int) + bool call(const char *name, void *param1, void *param2, int param3); // function(pointer, pointer, pointer) bool call(const char *name, void *param1, void *param2, void *param3); // function(pointer, number, number, number) diff --git a/Aquaria/ScriptedEntity.cpp b/Aquaria/ScriptedEntity.cpp index 8e2a142..05aef44 100644 --- a/Aquaria/ScriptedEntity.cpp +++ b/Aquaria/ScriptedEntity.cpp @@ -79,17 +79,27 @@ void ScriptedEntity::message(const std::string &msg, int v) Entity::message(msg, v); } -int ScriptedEntity::messageVariadic(lua_State *L, int nparams) +int ScriptedEntity::callVariadic(const char* func, lua_State* L, int nparams) { if (script) { - int res = script->callVariadic("msg", L, nparams, this); + int res = script->callVariadic(func, L, nparams, this); if (res < 0) - luaDebugMsg("msg", script->getLastError()); + luaDebugMsg(func, script->getLastError()); else return res; } - return Entity::messageVariadic(L, nparams); + return 0; +} + +int ScriptedEntity::messageVariadic(lua_State *L, int nparams) +{ + return callVariadic("msg", L, nparams); +} + +int ScriptedEntity::activateVariadic(lua_State* L, int nparams) +{ + return callVariadic("activate", L, nparams); } void ScriptedEntity::warpSegments() @@ -646,15 +656,15 @@ void ScriptedEntity::onHitWall() } } -void ScriptedEntity::activate() +void ScriptedEntity::activate(Entity *by, int source) { if (runningActivation) return; - Entity::activate(); + Entity::activate(by, source); runningActivation = true; if (script) { - if (!script->call("activate", this)) + if (!script->call("activate", this, by, source)) luaDebugMsg("activate", script->getLastError()); } runningActivation = false; diff --git a/Aquaria/ScriptedEntity.h b/Aquaria/ScriptedEntity.h index 8805c3f..5d18f35 100644 --- a/Aquaria/ScriptedEntity.h +++ b/Aquaria/ScriptedEntity.h @@ -45,12 +45,14 @@ public: typedef std::map PartMap; PartMap partMap; bool surfaceMoveDir; - void activate(); + void activate(Entity *by, int source); // override void warpSegments(); void lightFlare(); void entityDied(Entity *e); void message(const std::string &msg, int v); + int callVariadic(const char *func, lua_State *L, int nparams); int messageVariadic(lua_State *L, int nparams); + int activateVariadic(lua_State *L, int nparams); static bool runningActivation;