Skip to content

Commit

Permalink
perf: remove "onSpawn" and "onHear" EventCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 7, 2024
1 parent e6c0995 commit 1d9d886
Show file tree
Hide file tree
Showing 15 changed files with 2 additions and 324 deletions.

This file was deleted.

16 changes: 0 additions & 16 deletions data/scripts/actions/items/cobra_flask.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
local applyCobraFlaskEffectOnMonsterSpawn = EventCallback("CobraFlaskEffectOnMonsterSpawn")

applyCobraFlaskEffectOnMonsterSpawn.monsterOnSpawn = function(monster, position)
if table.contains({ "cobra scout", "cobra vizier", "cobra assassin" }, monster:getName():lower()) then
if Game.getStorageValue(Global.Storage.CobraFlask) >= os.time() then
monster:setHealth(monster:getMaxHealth() * 0.75)
monster:getPosition():sendMagicEffect(CONST_ME_GREEN_RINGS)
else
Game.setStorageValue(Global.Storage.CobraFlask, -1)
end
end
return true
end

applyCobraFlaskEffectOnMonsterSpawn:register()

local cobraFlask = Action()

function cobraFlask.onUse(player, item, fromPosition, target, toPosition, isHotkey)
Expand Down
19 changes: 2 additions & 17 deletions data/scripts/eventcallbacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Event callbacks are available for several categories of game entities, such as `
- `(bool)` `creatureOnChangeOutfit`
- `(ReturnValue)` `creatureOnAreaCombat`
- `(ReturnValue)` `creatureOnTargetCombat`
- `(void)` `creatureOnHear`
- `(void)` `creatureOnDrainHealth`
- `(void)` `creatureOnCombat`
- `(bool)` `partyOnJoin`
Expand Down Expand Up @@ -51,8 +50,6 @@ Event callbacks are available for several categories of game entities, such as `
- `(void)` `playerOnWalk`
- `(void)` `monsterOnDropLoot`
- `(void)` `monsterPostDropLoot`
- `(void)` `monsterOnSpawn`
- `(void)` `npcOnSpawn`

## Event Callback Usage

Expand Down Expand Up @@ -102,20 +99,8 @@ callback:register()
```lua
local callback = EventCallback("UniqueCallbackName")

function callback.monsterOnSpawn(monster, position)
-- custom behavior when a monster spawns
end

callback:register()
```

### Npc Callback

```lua
local callback = EventCallback("UniqueCallbackName")

function callback.npcOnSpawn(npc, position)
-- custom behavior when a npc spawns
function callback.monsterOnDropLoot(monster, corpse)
logger.info("Monster {} has corpse {}", monster:getName(), corpse:getName());
end

callback:register()
Expand Down
5 changes: 0 additions & 5 deletions data/scripts/eventcallbacks/creature/on_hear.lua

This file was deleted.

27 changes: 0 additions & 27 deletions data/scripts/eventcallbacks/monster/on_spawn.lua

This file was deleted.

2 changes: 0 additions & 2 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, spawnBlock_t &sb, const

spawnedMonsterMap[spawnMonsterId] = monster;
sb.lastSpawn = OTSYS_TIME();
g_events().eventMonsterOnSpawn(monster, sb.pos);
monster->onSpawn();
g_callbacks().executeCallback(EventCallback_t::monsterOnSpawn, &EventCallback::monsterOnSpawn, monster, sb.pos);
return true;
}

Expand Down
3 changes: 0 additions & 3 deletions src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ bool SpawnNpc::spawnNpc(uint32_t spawnId, const std::shared_ptr<NpcType> &npcTyp

spawnedNpcMap.insert(spawned_pair(spawnId, npc));
spawnNpcMap[spawnId].lastSpawnNpc = OTSYS_TIME();

g_events().eventNpcOnSpawn(npc, pos);
g_callbacks().executeCallback(EventCallback_t::npcOnSpawn, &EventCallback::npcOnSpawn, npc, pos);
return true;
}

Expand Down
4 changes: 0 additions & 4 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8713,10 +8713,6 @@ bool Player::saySpell(SpeakClasses type, const std::string &text, bool isGhostMo
}

tmpPlayer->onCreatureSay(static_self_cast<Player>(), type, text);
if (static_self_cast<Player>() != tmpPlayer) {
g_events().eventCreatureOnHear(tmpPlayer, getPlayer(), text, type);
g_callbacks().executeCallback(EventCallback_t::creatureOnHear, &EventCallback::creatureOnHear, tmpPlayer, getPlayer(), text, type);
}
}
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6397,10 +6397,6 @@ bool Game::internalCreatureSay(const std::shared_ptr<Creature> &creature, SpeakC
// event method
for (const auto &spectator : spectators) {
spectator->onCreatureSay(creature, type, text);
if (creature != spectator) {
g_events().eventCreatureOnHear(spectator, creature, text, type);
g_callbacks().executeCallback(EventCallback_t::creatureOnHear, &EventCallback::creatureOnHear, spectator, creature, text, type);
}
}
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/lua/callbacks/callbacks_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum class EventCallback_t : uint16_t {
creatureOnChangeOutfit,
creatureOnAreaCombat,
creatureOnTargetCombat,
creatureOnHear,
creatureOnDrainHealth,
creatureOnCombat,
// Party
Expand Down Expand Up @@ -62,9 +61,6 @@ enum class EventCallback_t : uint16_t {
// Monster
monsterOnDropLoot,
monsterPostDropLoot,
monsterOnSpawn,
// Npc
npcOnSpawn,
// Zone
zoneBeforeCreatureEnter,
zoneBeforeCreatureLeave,
Expand Down
84 changes: 0 additions & 84 deletions src/lua/callbacks/event_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,6 @@ ReturnValue EventCallback::creatureOnTargetCombat(const std::shared_ptr<Creature
return returnValue;
}

void EventCallback::creatureOnHear(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &speaker, const std::string &words, SpeakClasses type) const {
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("[EventCallback::creatureOnHear - "
"Creature {} speaker {}] "
"Call stack overflow. Too many lua script calls being nested.",
creature->getName(), speaker->getName());
return;
}

ScriptEnvironment* scriptEnvironment = LuaScriptInterface::getScriptEnv();
scriptEnvironment->setScriptId(getScriptId(), getScriptInterface());

lua_State* L = getScriptInterface()->getLuaState();
getScriptInterface()->pushFunction(getScriptId());

LuaScriptInterface::pushUserdata<Creature>(L, creature);
LuaScriptInterface::setCreatureMetatable(L, -1, creature);

LuaScriptInterface::pushUserdata<Creature>(L, speaker);
LuaScriptInterface::setCreatureMetatable(L, -1, speaker);

LuaScriptInterface::pushString(L, words);
lua_pushnumber(L, type);

getScriptInterface()->callVoidFunction(4);
}

void EventCallback::creatureOnDrainHealth(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &attacker, CombatType_t &typePrimary, int32_t &damagePrimary, CombatType_t &typeSecondary, int32_t &damageSecondary, TextColor_t &colorPrimary, TextColor_t &colorSecondary) const {
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("[EventCallback::creatureOnDrainHealth - "
Expand Down Expand Up @@ -1162,63 +1135,6 @@ void EventCallback::monsterPostDropLoot(const std::shared_ptr<Monster> &monster,
return getScriptInterface()->callVoidFunction(2);
}

void EventCallback::monsterOnSpawn(const std::shared_ptr<Monster> &monster, const Position &position) const {
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("{} - "
"Position {}"
". Call stack overflow. Too many lua script calls being nested.",
__FUNCTION__, position.toString());
return;
}

ScriptEnvironment* scriptEnvironment = LuaScriptInterface::getScriptEnv();
scriptEnvironment->setScriptId(getScriptId(), getScriptInterface());

lua_State* L = getScriptInterface()->getLuaState();
getScriptInterface()->pushFunction(getScriptId());

LuaScriptInterface::pushUserdata<Monster>(L, monster);
LuaScriptInterface::setMetatable(L, -1, "Monster");
LuaScriptInterface::pushPosition(L, position);

if (LuaScriptInterface::protectedCall(L, 2, 1) != 0) {
LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
} else {
lua_pop(L, 1);
}

LuaScriptInterface::resetScriptEnv();
}

// Npc
void EventCallback::npcOnSpawn(const std::shared_ptr<Npc> &npc, const Position &position) const {
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("{} - "
"Position {}"
". Call stack overflow. Too many lua script calls being nested.",
__FUNCTION__, position.toString());
return;
}

ScriptEnvironment* scriptEnvironment = LuaScriptInterface::getScriptEnv();
scriptEnvironment->setScriptId(getScriptId(), getScriptInterface());

lua_State* L = getScriptInterface()->getLuaState();
getScriptInterface()->pushFunction(getScriptId());

LuaScriptInterface::pushUserdata<Npc>(L, npc);
LuaScriptInterface::setMetatable(L, -1, "Npc");
LuaScriptInterface::pushPosition(L, position);

if (LuaScriptInterface::protectedCall(L, 2, 1) != 0) {
LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
} else {
lua_pop(L, 1);
}

LuaScriptInterface::resetScriptEnv();
}

bool EventCallback::zoneBeforeCreatureEnter(const std::shared_ptr<Zone> &zone, const std::shared_ptr<Creature> &creature) const {
if (!LuaScriptInterface::reserveScriptEnv()) {
g_logger().error("[EventCallback::zoneBeforeCreatureEnter - "
Expand Down
5 changes: 0 additions & 5 deletions src/lua/callbacks/event_callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class EventCallback final : public Script {
bool creatureOnChangeOutfit(const std::shared_ptr<Creature> &creature, const Outfit_t &outfit) const;
ReturnValue creatureOnAreaCombat(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Tile> &tile, bool aggressive) const;
ReturnValue creatureOnTargetCombat(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &target) const;
void creatureOnHear(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &speaker, const std::string &words, SpeakClasses type) const;
void creatureOnDrainHealth(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &attacker, CombatType_t &typePrimary, int32_t &damagePrimary, CombatType_t &typeSecondary, int32_t &damageSecondary, TextColor_t &colorPrimary, TextColor_t &colorSecondary) const;
void creatureOnCombat(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target, CombatDamage &damage) const;

Expand Down Expand Up @@ -137,10 +136,6 @@ class EventCallback final : public Script {
// Monster
void monsterOnDropLoot(const std::shared_ptr<Monster> &monster, const std::shared_ptr<Container> &corpse) const;
void monsterPostDropLoot(const std::shared_ptr<Monster> &monster, const std::shared_ptr<Container> &corpse) const;
void monsterOnSpawn(const std::shared_ptr<Monster> &monster, const Position &position) const;

// Npc
void npcOnSpawn(const std::shared_ptr<Npc> &npc, const Position &position) const;

// Zone
bool zoneBeforeCreatureEnter(const std::shared_ptr<Zone> &zone, const std::shared_ptr<Creature> &creature) const;
Expand Down
Loading

0 comments on commit 1d9d886

Please sign in to comment.