Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: game monster cache on Monster:setType #3249

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10066,6 +10066,25 @@ void Game::removeMonster(const std::shared_ptr<Monster> &monster) {
}
}

void Game::updateMonster(const std::shared_ptr<Monster> &monster, const std::shared_ptr<MonsterType> &monsterType) {
if (!monster) {
return;
}

const auto &oldName = monster->getLowerName();
const auto &newName = monsterType->name;

auto it = monstersNameIndex.find(oldName);
if (it == monstersNameIndex.end()) {
return;
}

size_t index = it->second;

monstersNameIndex.erase(it);
monstersNameIndex[newName] = index;
}

std::shared_ptr<Guild> Game::getGuild(uint32_t id, bool allowOffline /* = flase */) const {
auto it = guilds.find(id);
if (it == guilds.end()) {
Expand Down
1 change: 1 addition & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class Game {

void addMonster(const std::shared_ptr<Monster> &monster);
void removeMonster(const std::shared_ptr<Monster> &monster);
void updateMonster(const std::shared_ptr<Monster> &monster, const std::shared_ptr<MonsterType> &monsterType);

std::shared_ptr<Guild> getGuild(uint32_t id, bool allowOffline = false) const;
std::shared_ptr<Guild> getGuildByName(const std::string &name, bool allowOffline = false) const;
Expand Down
4 changes: 4 additions & 0 deletions src/lua/functions/creatures/monster/monster_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) {
g_logger().warn("[Warning - MonsterFunctions::luaMonsterSetType] Unknown event name: {}", scriptName);
}
}

// Update created monster
g_game().updateMonster(monster, mType);

// Assign new MonsterType
monster->mType = mType;
monster->nameDescription = asLowerCaseString(mType->nameDescription);
Expand Down
Loading