Skip to content

Commit

Permalink
[Fix] Callback fromPosition of some lua scripts that were registered …
Browse files Browse the repository at this point in the history
…by position (#441)

Some scripts registered through position didn't work the fromPosition callback because it was lost between the two functions (with and without the callback item)
  • Loading branch information
dudantas authored Jul 15, 2022
1 parent aafdf52 commit 723f378
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
58 changes: 17 additions & 41 deletions src/lua/creature/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ uint32_t MoveEvents::onCreatureMove(Creature& creature, Tile& tile, MoveEvent_t

MoveEvent* moveEvent = getEvent(tile, eventType);
if (moveEvent) {
ret &= moveEvent->fireStepEvent(creature, pos);
ret &= moveEvent->fireStepEvent(creature, nullptr, pos);
}

for (size_t i = tile.getFirstIndex(), j = tile.getLastIndex(); i < j; ++i) {
Expand All @@ -263,7 +263,7 @@ uint32_t MoveEvents::onCreatureMove(Creature& creature, Tile& tile, MoveEvent_t

moveEvent = getEvent(*tileItem, eventType);
if (moveEvent) {
ret &= moveEvent->fireStepEvent(creature, *tileItem, pos);
ret &= moveEvent->fireStepEvent(creature, tileItem, pos);
}
}
return ret;
Expand Down Expand Up @@ -609,21 +609,29 @@ void MoveEvent::setEventType(MoveEvent_t type) {
eventType = type;
}

uint32_t MoveEvent::fireStepEvent(Creature& creature, Item& item, const Position& pos) {
uint32_t MoveEvent::fireStepEvent(Creature& creature, Item* item, const Position& pos) {
if (isScripted()) {
return executeStep(creature, item, pos);
} else {
return stepFunction(&creature, &item, pos);
return stepFunction(&creature, item, pos);
}
}

bool MoveEvent::executeStep(Creature& creature, Item& item, const Position& pos) {
bool MoveEvent::executeStep(Creature& creature, Item* item, const Position& pos) {
//onStepIn(creature, item, pos, fromPosition)
//onStepOut(creature, item, pos, fromPosition)
if (!scriptInterface->reserveScriptEnv()) {
SPDLOG_ERROR("[MoveEvent::executeStep - Creature {} item {}] "
"Call stack overflow. Too many lua script calls being nested.",
creature.getName(), item.getName());
if (item != nullptr) {
SPDLOG_ERROR("[MoveEvent::executeStep - Creature {} item {}, position {}] "
"Call stack overflow. Too many lua script calls being nested.",
creature.getName(), item->getName(), pos.toString()
);
} else {
SPDLOG_ERROR("[MoveEvent::executeStep - Creature {}, position {}] "
"Call stack overflow. Too many lua script calls being nested.",
creature.getName(), pos.toString()
);
}
return false;
}

Expand All @@ -635,45 +643,13 @@ bool MoveEvent::executeStep(Creature& creature, Item& item, const Position& pos)
scriptInterface->pushFunction(scriptId);
LuaScriptInterface::pushUserdata<Creature>(L, &creature);
LuaScriptInterface::setCreatureMetatable(L, -1, &creature);
LuaScriptInterface::pushThing(L, &item);
LuaScriptInterface::pushThing(L, item);
LuaScriptInterface::pushPosition(L, pos);
LuaScriptInterface::pushPosition(L, creature.getLastPosition());

return scriptInterface->callFunction(4);
}

uint32_t MoveEvent::fireStepEvent(Creature& creature, const Position& pos) {
if (isScripted()) {
return executeStep(creature, pos);
} else {
return stepFunction(&creature, nullptr, pos);
}
}

bool MoveEvent::executeStep(Creature& creature, const Position& pos) {
//onStepIn(creature, pos, fromPosition)
//onStepOut(creature, pos, fromPosition)
if (!scriptInterface->reserveScriptEnv()) {
SPDLOG_ERROR("[MoveEvent::executeStep - Creature {}] "
"Call stack overflow. Too many lua script calls being nested.",
creature.getName());
return false;
}

ScriptEnvironment* env = scriptInterface->getScriptEnv();
env->setScriptId(scriptId, scriptInterface);

lua_State* L = scriptInterface->getLuaState();

scriptInterface->pushFunction(scriptId);
LuaScriptInterface::pushUserdata<Creature>(L, &creature);
LuaScriptInterface::setCreatureMetatable(L, -1, &creature);
LuaScriptInterface::pushPosition(L, pos);
LuaScriptInterface::pushPosition(L, creature.getLastPosition());

return scriptInterface->callFunction(3);
}

uint32_t MoveEvent::fireEquip(Player& player, Item& item, Slots_t toSlot, bool isCheck) {
if (isScripted()) {
if (!equipFunction || equipFunction(this, &player, &item, toSlot, isCheck) == 1) {
Expand Down
8 changes: 2 additions & 6 deletions src/lua/creature/movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ class MoveEvent final : public Event {
return false;
}

uint32_t fireStepEvent(Creature& creature, Item& item, const Position& pos);
// No have item
uint32_t fireStepEvent(Creature& creature, const Position& pos);
uint32_t fireStepEvent(Creature& creature, Item* item, const Position& pos);
uint32_t fireAddRemItem(Item& item, Item& tileItem, const Position& pos);
uint32_t fireAddRemItem(Item& item, const Position& pos);
uint32_t fireEquip(Player& player, Item& item, Slots_t slot, bool isCheck);
Expand All @@ -188,9 +186,7 @@ class MoveEvent final : public Event {
}

// Scripting to lua interface
bool executeStep(Creature& creature, Item& item, const Position& pos);
// No have item
bool executeStep(Creature& creature, const Position& pos);
bool executeStep(Creature& creature, Item* item, const Position& pos);
bool executeEquip(Player& player, Item& item, Slots_t slot, bool isCheck);
bool executeAddRemItem(Item& item, Item& tileItem, const Position& pos);
// No have tile item
Expand Down
6 changes: 4 additions & 2 deletions src/lua/functions/creatures/creature_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,15 @@ int CreatureFunctions::luaCreatureTeleportTo(lua_State* L) {

const Position& position = getPosition(L, 2);
Creature* creature = getUserdata<Creature>(L, 1);
if (!creature) {
lua_pushnil(L);
if (creature == nullptr) {
reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
pushBoolean(L, false);
return 1;
}

const Position oldPosition = creature->getPosition();
if (g_game().internalTeleport(creature, position, pushMovement) != RETURNVALUE_NOERROR) {
reportErrorFunc("Could not teleport creature.");
pushBoolean(L, false);
return 1;
}
Expand Down

0 comments on commit 723f378

Please sign in to comment.