1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-08-09 15:49:52 +00:00

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
This commit is contained in:
fgenesis 2023-02-24 02:10:33 +01:00
parent 66843d338d
commit 01295cda7a
9 changed files with 96 additions and 66 deletions

View file

@ -3036,12 +3036,12 @@ void Avatar::rmbu(int source, InputDevice device)
if (entityToActivate) if (entityToActivate)
{ {
activateEntity = entityToActivate; entityToActivate->activate(this, source);
entityToActivate = 0; entityToActivate = 0;
} }
if (pathToActivate) if (pathToActivate)
{ {
pathToActivate->activate(); pathToActivate->activate(this, source);
pathToActivate = 0; pathToActivate = 0;
} }
} }
@ -3661,7 +3661,6 @@ Avatar::Avatar() : Entity(), ActionMapper()
damageTime = vars->avatarDamageTime; damageTime = vars->avatarDamageTime;
activateEntity = 0;
canMove = true; canMove = true;
//scale = Vector(0.5, 0.5); //scale = Vector(0.5, 0.5);
scale = Vector(0.5, 0.5); scale = Vector(0.5, 0.5);
@ -5301,11 +5300,7 @@ void Avatar::onUpdate(float dt)
} }
applyRidingPosition(); applyRidingPosition();
if (activateEntity)
{
activateEntity->activate();
activateEntity = 0;
}
if (bone_head) if (bone_head)
headPosition = bone_head->getWorldPosition(); headPosition = bone_head->getWorldPosition();

View file

@ -415,7 +415,6 @@ protected:
float revertTimer; float revertTimer;
void endCharge(); void endCharge();
Entity *activateEntity;
bool canMove; bool canMove;
void onEnterState(int action); void onEnterState(int action);

View file

@ -105,6 +105,7 @@ public:
virtual void message(const std::string &msg, int v) {} virtual void message(const std::string &msg, int v) {}
virtual int messageVariadic(lua_State *L, int nparams) { return 0; } 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()); bool isUnderWater(const Vector &o=Vector());
@ -196,7 +197,7 @@ public:
Entity *getTargetEntity(int t=0); Entity *getTargetEntity(int t=0);
void setTargetEntity(Entity *e, int t=0); void setTargetEntity(Entity *e, int t=0);
virtual void activate(){} virtual void activate(Entity *by, int source){}
SkeletalSprite skeletalSprite; SkeletalSprite skeletalSprite;

View file

@ -644,11 +644,11 @@ bool Path::action(int id, int state, int source, InputDevice device)
return true; return true;
} }
void Path::activate(Entity *e) void Path::activate(Entity *e, int source)
{ {
if (hasScript() && activateFunction) if (hasScript() && activateFunction)
{ {
if (!script->call("activate", this, e)) if (!script->call("activate", this, e, source))
{ {
luaDebugMsg("activate", script->getLastError()); luaDebugMsg("activate", script->getLastError());
activateFunction = false; activateFunction = false;
@ -656,6 +656,7 @@ void Path::activate(Entity *e)
} }
} }
void Path::removeNode(size_t idx) void Path::removeNode(size_t idx)
{ {
std::vector<PathNode> copy = nodes; std::vector<PathNode> 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) if (script)
{ {
int res = script->callVariadic("msg", L, nparams, this); int res = script->callVariadic(func, L, nparams, this);
if (res < 0) if (res < 0)
luaDebugMsg("msg", script->getLastError()); luaDebugMsg(func, script->getLastError());
else else
return res; return res;
} }
return 0; 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) void Path::luaDebugMsg(const std::string &func, const std::string &msg)
{ {
debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg); debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg);

View file

@ -109,7 +109,7 @@ public:
Vector getEnterPosition(int outAmount=1); Vector getEnterPosition(int outAmount=1);
Vector getEnterNormal(); Vector getEnterNormal();
void activate(Entity *e=0); void activate(Entity *e, int source);
void refreshScript(); void refreshScript();
MinimapIcon *ensureMinimapIcon(); MinimapIcon *ensureMinimapIcon();
@ -155,7 +155,9 @@ public:
void parseWarpNodeData(const std::string &dataString); void parseWarpNodeData(const std::string &dataString);
int callVariadic(const char *func, lua_State *L, int nparams);
int messageVariadic(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); void luaDebugMsg(const std::string &func, const std::string &msg);
}; };

View file

@ -579,6 +579,17 @@ Entity *entity(lua_State *L, int slot = 1)
return ent; 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 static inline
Vector getVector(lua_State *L, int slot = 1) Vector getVector(lua_State *L, int slot = 1)
{ {
@ -3042,9 +3053,7 @@ luaFunc(entity_getLookAtPoint)
luaFunc(entity_setRiding) luaFunc(entity_setRiding)
{ {
Entity *e = entity(L); Entity *e = entity(L);
Entity *e2 = 0; Entity *e2 = entityOpt(L, 2);
if (lua_touserdata(L, 2) != NULL)
e2 = entity(L, 2);
if (e) if (e)
{ {
e->setRiding(e2); e->setRiding(e2);
@ -3351,10 +3360,7 @@ luaFunc(entity_findNearestEntityOfType)
luaFunc(createShot) luaFunc(createShot)
{ {
Entity *e = entity(L,2); Entity *e = entity(L,2);
Entity *t = 0; Entity *t = entityOpt(L, 3);
if (lua_touserdata(L, 3) != NULL)
t = entity(L,3);
Shot *s = 0;
Vector pos, aim; Vector pos, aim;
pos.x = lua_tonumber(L, 4); pos.x = lua_tonumber(L, 4);
pos.y = lua_tonumber(L, 5); pos.y = lua_tonumber(L, 5);
@ -3362,7 +3368,7 @@ luaFunc(createShot)
aim.y = lua_tonumber(L, 7); 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); luaReturnPtr(s);
} }
@ -4416,37 +4422,21 @@ luaFunc(cam_toNode)
luaFunc(cam_toEntity) luaFunc(cam_toEntity)
{ {
if (lua_touserdata(L, 1) == NULL) Entity *e = entityOpt(L);
{ if(e)
Vector *pos = 0; dsq->game->setCameraFollowEntity(e);
dsq->game->setCameraFollow(pos);
}
else else
{ dsq->game->setCameraFollow((Vector*)NULL);
Entity *e = entity(L);
if (e)
{
dsq->game->setCameraFollowEntity(e);
}
}
luaReturnNil(); luaReturnNil();
} }
luaFunc(cam_setPosition) luaFunc(cam_setPosition)
{ {
float x = lua_tonumber(L, 1); InterpolatedVector& camvec = dsq->game->cameraInterp;
float y = lua_tonumber(L, 2); camvec.stop();
float time = lua_tonumber(L, 3); interpolateVec2(L, camvec, 1);
int loopType = lua_tointeger(L, 4);
bool pingPong = getBool(L, 5);
bool ease = getBool(L, 6);
Vector p(x,y); dsq->cameraPos = dsq->game->getCameraPositionFor(camvec);
dsq->game->cameraInterp.stop();
dsq->game->cameraInterp.interpolateTo(p, time, loopType, pingPong, ease);
dsq->cameraPos = dsq->game->getCameraPositionFor(dsq->game->cameraInterp);
luaReturnNil(); luaReturnNil();
} }
@ -6318,14 +6308,9 @@ luaFunc(entity_getPrevState)
luaFunc(entity_setTarget) luaFunc(entity_setTarget)
{ {
Entity *e = entity(L); Entity *e = entity(L);
Entity *t = 0;
if (lua_touserdata(L, 2) != NULL)
{
t = entity(L, 2);
}
if (e) if (e)
{ {
e->setTargetEntity(t); e->setTargetEntity(entityOpt(L, 2));
} }
luaReturnNil(); luaReturnNil();
} }
@ -7599,12 +7584,25 @@ luaFunc(getEntityByID)
luaFunc(node_activate) luaFunc(node_activate)
{ {
Path *p = path(L); Path *p = path(L);
Entity *e = 0;
if (lua_touserdata(L, 2) != NULL)
e = entity(L, 2);
if (p) 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(); luaReturnNil();
} }
@ -10654,6 +10652,7 @@ static const struct {
luaRegister(loadSound), luaRegister(loadSound),
luaRegister(node_activate), luaRegister(node_activate),
luaRegister(entity_activate),
luaRegister(node_getName), luaRegister(node_getName),
luaRegister(node_getLabel), luaRegister(node_getLabel),
luaRegister(node_getPathPosition), luaRegister(node_getPathPosition),
@ -12189,6 +12188,15 @@ bool Script::call(const char *name, void *param1, const char *param2, void *para
return doCall(3); 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) bool Script::call(const char *name, void *param1, void *param2, void *param3)
{ {
lookupFunc(name); lookupFunc(name);

View file

@ -55,6 +55,8 @@ public:
bool call(const char *name, void *param1, const char *param2, float param3); bool call(const char *name, void *param1, const char *param2, float param3);
// function(pointer, string, pointer) // function(pointer, string, pointer)
bool call(const char *name, void *param1, const char *param2, void *param3); 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) // function(pointer, pointer, pointer)
bool call(const char *name, void *param1, void *param2, void *param3); bool call(const char *name, void *param1, void *param2, void *param3);
// function(pointer, number, number, number) // function(pointer, number, number, number)

View file

@ -79,17 +79,27 @@ void ScriptedEntity::message(const std::string &msg, int v)
Entity::message(msg, 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) if (script)
{ {
int res = script->callVariadic("msg", L, nparams, this); int res = script->callVariadic(func, L, nparams, this);
if (res < 0) if (res < 0)
luaDebugMsg("msg", script->getLastError()); luaDebugMsg(func, script->getLastError());
else else
return res; 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() void ScriptedEntity::warpSegments()
@ -646,15 +656,15 @@ void ScriptedEntity::onHitWall()
} }
} }
void ScriptedEntity::activate() void ScriptedEntity::activate(Entity *by, int source)
{ {
if (runningActivation) return; if (runningActivation) return;
Entity::activate(); Entity::activate(by, source);
runningActivation = true; runningActivation = true;
if (script) if (script)
{ {
if (!script->call("activate", this)) if (!script->call("activate", this, by, source))
luaDebugMsg("activate", script->getLastError()); luaDebugMsg("activate", script->getLastError());
} }
runningActivation = false; runningActivation = false;

View file

@ -45,12 +45,14 @@ public:
typedef std::map<std::string, RenderObject*> PartMap; typedef std::map<std::string, RenderObject*> PartMap;
PartMap partMap; PartMap partMap;
bool surfaceMoveDir; bool surfaceMoveDir;
void activate(); void activate(Entity *by, int source); // override
void warpSegments(); void warpSegments();
void lightFlare(); void lightFlare();
void entityDied(Entity *e); void entityDied(Entity *e);
void message(const std::string &msg, int v); 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 messageVariadic(lua_State *L, int nparams);
int activateVariadic(lua_State *L, int nparams);
static bool runningActivation; static bool runningActivation;