Skip to content

Commit

Permalink
fix: hazard spawn initialization (#3184)
Browse files Browse the repository at this point in the history
Fixes bugs introduced here:
#3076

By mistake I removed the onSpawn from the hazard and didn't realize it.
  • Loading branch information
dudantas authored Dec 21, 2024
1 parent 05ff8be commit f2750bb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
12 changes: 6 additions & 6 deletions data/libs/systems/hazard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ function HazardMonster.onSpawn(monster, position)
if not zones then
return true
end

logger.debug("Monster {} spawned in hazard zone, position {}", monster:getName(), position:toString())
for _, zone in ipairs(zones) do
local hazard = Hazard.getByName(zone:getName())
if hazard then
monster:hazard(true)
if hazard then
monster:hazardCrit(hazard.crit)
monster:hazardDodge(hazard.dodge)
monster:hazardDamageBoost(hazard.damageBoost)
monster:hazardDefenseBoost(hazard.defenseBoost)
end
monster:hazardCrit(hazard.crit)
monster:hazardDodge(hazard.dodge)
monster:hazardDamageBoost(hazard.damageBoost)
monster:hazardDefenseBoost(hazard.defenseBoost)
end
end
return true
Expand Down
6 changes: 5 additions & 1 deletion data/scripts/lib/register_monster_type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ end
registerMonsterType.name = function(mtype, mask)
if mask.name then
mtype:name(mask.name)
-- Try register hazard monsters
mtype.onSpawn = function(monster, spawnPosition)
HazardMonster.onSpawn(monster, spawnPosition)
end
end
end
registerMonsterType.description = function(mtype, mask)
Expand Down Expand Up @@ -194,7 +198,7 @@ registerMonsterType.flags = function(mtype, mask)
end
if mask.flags.rewardBoss then
mtype:isRewardBoss(mask.flags.rewardBoss)
mtype.onSpawn = function(monster)
mtype.onSpawn = function(monster, spawnPosition)
monster:setRewardBoss()
end
end
Expand Down
7 changes: 4 additions & 3 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ void Monster::onAttackedByPlayer(const std::shared_ptr<Player> &attackerPlayer)
}
}

void Monster::onSpawn() {
void Monster::onSpawn(const Position &position) {
if (mType->info.spawnEvent != -1) {
// onSpawn(self)
// onSpawn(self, spawnPosition)
LuaScriptInterface* scriptInterface = mType->info.scriptInterface;
if (!scriptInterface->reserveScriptEnv()) {
g_logger().error("Monster {} creature {}] Call stack overflow. Too many lua "
Expand All @@ -520,8 +520,9 @@ void Monster::onSpawn() {

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

scriptInterface->callVoidFunction(1);
scriptInterface->callVoidFunction(2);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Monster final : public Creature {
void onCreatureMove(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Tile> &newTile, const Position &newPos, const std::shared_ptr<Tile> &oldTile, const Position &oldPos, bool teleport) override;
void onCreatureSay(const std::shared_ptr<Creature> &creature, SpeakClasses type, const std::string &text) override;
void onAttackedByPlayer(const std::shared_ptr<Player> &attackerPlayer);
void onSpawn();
void onSpawn(const Position &position);

void drainHealth(const std::shared_ptr<Creature> &attacker, int32_t damage) override;
void changeHealth(int32_t healthChange, bool sendHealthChange = true) override;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, spawnBlock_t &sb, const

spawnedMonsterMap[spawnMonsterId] = monster;
sb.lastSpawn = OTSYS_TIME();
monster->onSpawn();
monster->onSpawn(sb.pos);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ int GameFunctions::luaGameCreateMonster(lua_State* L) {
const bool extended = Lua::getBoolean(L, 3, false);
const bool force = Lua::getBoolean(L, 4, false);
if (g_game().placeCreature(monster, position, extended, force)) {
monster->onSpawn();
monster->onSpawn(position);
const auto &mtype = monster->getMonsterType();
if (mtype && mtype->info.raceid > 0 && mtype->info.bosstiaryRace == BosstiaryRarity_t::RARITY_ARCHFOE) {
for (const auto &spectator : Spectators().find<Player>(monster->getPosition(), true)) {
Expand Down

0 comments on commit f2750bb

Please sign in to comment.