Skip to content

Commit

Permalink
fix: monster spawntime load and save
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Jan 19, 2025
1 parent 76e1f55 commit 734cb98
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
11 changes: 2 additions & 9 deletions source/iomap_otbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ bool IOMapOTBM::loadSpawnsMonster(Map &map, pugi::xml_document &doc) {
break;
}

int32_t spawntime = monsterNode.attribute("spawntime").as_int();
uint16_t spawntime = monsterNode.attribute("spawntime").as_uint(0);
if (spawntime == 0) {
spawntime = g_settings.getInteger(Config::DEFAULT_SPAWN_MONSTER_TIME);
}
Expand Down Expand Up @@ -1773,14 +1773,7 @@ bool IOMapOTBM::saveSpawns(Map &map, pugi::xml_document &doc) {
monsterNode.append_attribute("x") = x;
monsterNode.append_attribute("y") = y;
monsterNode.append_attribute("z") = spawnPosition.z;
auto monsterSpawnTime = monster->getSpawnMonsterTime();
uint16_t maxUint16 = std::numeric_limits<uint16_t>::max();
uint16_t minUint16 = std::numeric_limits<uint16_t>::min();
if (std::cmp_greater(monsterSpawnTime, maxUint16) || std::cmp_greater(monsterSpawnTime, minUint16)) {
monsterSpawnTime = 60;
}

monsterNode.append_attribute("spawntime") = monsterSpawnTime;
monsterNode.append_attribute("spawntime") = monster->getSpawnMonsterTime();
if (monster->getDirection() != NORTH) {
monsterNode.append_attribute("direction") = monster->getDirection();
}
Expand Down
6 changes: 3 additions & 3 deletions source/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class Monster {
weight = newWeight;
}

int getSpawnMonsterTime() const noexcept {
uint16_t getSpawnMonsterTime() const noexcept {
return spawntime;
}
void setSpawnMonsterTime(int time) noexcept {
void setSpawnMonsterTime(uint16_t time) noexcept {
spawntime = time;
}

Expand All @@ -87,7 +87,7 @@ class Monster {
std::string type_name;
Direction direction;
uint8_t weight;
int spawntime;
uint16_t spawntime;
bool saved;
bool selected;
};
Expand Down
2 changes: 1 addition & 1 deletion source/monster_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void MonsterBrush::drawMonster(BaseMap* map, Tile* tile, void* parameter) {
});
if (it == tile->monsters.end()) {
const auto monster = newd Monster(monster_type);
monster->setSpawnMonsterTime(*(int*)parameter);
monster->setSpawnMonsterTime(*(uint16_t*)parameter);
tile->monsters.emplace_back(monster);
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/spawn_monster_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void SpawnMonsterBrush::draw(BaseMap* map, Tile* tile, void* parameter) {

auto size = std::max(1, *(int*)parameter);
auto side = size * 2 + 1;
int time = g_settings.getInteger(Config::DEFAULT_SPAWN_MONSTER_TIME);
uint16_t spawnTime = g_settings.getInteger(Config::DEFAULT_SPAWN_MONSTER_TIME);
int density = g_settings.getInteger(Config::SPAWN_MONSTER_DENSITY);
if (tile && tile->spawnMonster == nullptr) {
tile->spawnMonster = newd SpawnMonster(size);
Expand Down Expand Up @@ -99,7 +99,7 @@ void SpawnMonsterBrush::draw(BaseMap* map, Tile* tile, void* parameter) {

if (tileSpawn) {
auto monsterBrush = monsters[rand() % monsters.size()];
monsterBrush->drawMonster(map, tileSpawn, &time);
monsterBrush->drawMonster(map, tileSpawn, &spawnTime);
}
}
}
Expand Down

1 comment on commit 734cb98

@Mifuno
Copy link

@Mifuno Mifuno commented on 734cb98 Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image
fix works

Please sign in to comment.