From 8d5fa41a191d38c4528038c4801b325c9ff36f25 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 2 Nov 2023 17:48:48 +0200 Subject: [PATCH] Minor fixes --- client/lobby/CBonusSelection.cpp | 2 +- lib/CBuildingHandler.cpp | 12 ++++----- lib/CBuildingHandler.h | 2 +- lib/CCreatureHandler.cpp | 41 +++++++++++-------------------- lib/CHeroHandler.cpp | 2 +- lib/CSkillHandler.cpp | 4 +-- lib/constants/EntityIdentifiers.h | 4 --- lib/gameState/CGameState.cpp | 2 +- lib/mapObjects/CBank.cpp | 2 +- lib/mapObjects/CGDwelling.cpp | 2 +- lib/mapObjects/CGHeroInstance.cpp | 2 +- lib/mapObjects/CGPandoraBox.cpp | 2 +- lib/mapObjects/CGTownInstance.cpp | 2 +- lib/mapObjects/MiscObjects.cpp | 4 +-- lib/mapping/CMap.cpp | 13 +++++++++- lib/mapping/MapFormatH3M.cpp | 2 +- lib/mapping/MapFormatJson.cpp | 2 +- lib/rewardable/Reward.cpp | 2 +- lib/rmg/CMapGenOptions.cpp | 2 +- lib/rmg/CMapGenOptions.h | 2 +- 20 files changed, 51 insertions(+), 55 deletions(-) diff --git a/client/lobby/CBonusSelection.cpp b/client/lobby/CBonusSelection.cpp index 2566bf075..dabc75028 100644 --- a/client/lobby/CBonusSelection.cpp +++ b/client/lobby/CBonusSelection.cpp @@ -161,7 +161,7 @@ void CBonusSelection::createBonusesIcons() break; case CampaignBonusType::BUILDING: { - int faction = -1; + FactionID faction; for(auto & elem : CSH->si->playerInfos) { if(elem.second.isControlledByHuman()) diff --git a/lib/CBuildingHandler.cpp b/lib/CBuildingHandler.cpp index 6db07ac15..be986ee8b 100644 --- a/lib/CBuildingHandler.cpp +++ b/lib/CBuildingHandler.cpp @@ -12,7 +12,7 @@ VCMI_LIB_NAMESPACE_BEGIN -BuildingID CBuildingHandler::campToERMU(int camp, int townType, const std::set & builtBuildings) +BuildingID CBuildingHandler::campToERMU(int camp, FactionID townType, const std::set & builtBuildings) { static const std::vector campToERMU = { @@ -47,13 +47,13 @@ BuildingID CBuildingHandler::campToERMU(int camp, int townType, const std::set 1) + if(hordeLvlsPerTType[townType.getNum()].size() > 1) { - BuildingID dwellingID(BuildingID::DWELL_UP_FIRST + hordeLvlsPerTType[townType][1]); + BuildingID dwellingID(BuildingID::DWELL_UP_FIRST + hordeLvlsPerTType[townType.getNum()][1]); if(vstd::contains(builtBuildings, dwellingID)) //if upgraded dwelling is built return BuildingID::HORDE_2_UPGR; diff --git a/lib/CBuildingHandler.h b/lib/CBuildingHandler.h index a0aab5615..059b9f087 100644 --- a/lib/CBuildingHandler.h +++ b/lib/CBuildingHandler.h @@ -16,7 +16,7 @@ VCMI_LIB_NAMESPACE_BEGIN class DLL_LINKAGE CBuildingHandler { public: - static BuildingID campToERMU(int camp, int townType, const std::set & builtBuildings); + static BuildingID campToERMU(int camp, FactionID townType, const std::set & builtBuildings); }; VCMI_LIB_NAMESPACE_END diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index 2b723aac5..54c3083da 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -263,7 +263,7 @@ bool CCreature::isDoubleWide() const */ bool CCreature::isGood () const { - return VLC->factions()->getByIndex(faction)->getAlignment() == EAlignment::GOOD; + return VLC->factions()->getById(faction)->getAlignment() == EAlignment::GOOD; } /** @@ -272,7 +272,7 @@ bool CCreature::isGood () const */ bool CCreature::isEvil () const { - return VLC->factions()->getByIndex(faction)->getAlignment() == EAlignment::EVIL; + return VLC->factions()->getById(faction)->getAlignment() == EAlignment::EVIL; } si32 CCreature::maxAmount(const TResources &res) const //how many creatures can be bought @@ -1350,34 +1350,23 @@ CCreatureHandler::~CCreatureHandler() CreatureID CCreatureHandler::pickRandomMonster(CRandomGenerator & rand, int tier) const { - int r = 0; - if(tier == -1) //pick any allowed creature + std::vector allowed; + for(const auto & creature : objects) { - do - { - r = (*RandomGeneratorUtil::nextItem(objects, rand))->getId(); - } while (objects[r] && objects[r]->special); // find first "not special" creature + if(creature->special) + continue; + + if (creature->level == tier || tier == -1) + allowed.push_back(creature->getId()); } - else + + if(allowed.empty()) { - assert(vstd::iswithin(tier, 1, 7)); - std::vector allowed; - for(const auto & creature : objects) - { - if(!creature->special && creature->level == tier) - allowed.push_back(creature->getId()); - } - - if(allowed.empty()) - { - logGlobal->warn("Cannot pick a random creature of tier %d!", tier); - return CreatureID::NONE; - } - - return *RandomGeneratorUtil::nextItem(allowed, rand); + logGlobal->warn("Cannot pick a random creature of tier %d!", tier); + return CreatureID::NONE; } - assert (r >= 0); //should always be, but it crashed - return CreatureID(r); + + return *RandomGeneratorUtil::nextItem(allowed, rand); } diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index f284dbf16..a555bec93 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -151,7 +151,7 @@ bool CHeroClass::isMagicHero() const EAlignment CHeroClass::getAlignment() const { - return VLC->factions()->getByIndex(faction)->getAlignment(); + return VLC->factions()->getById(faction)->getAlignment(); } int32_t CHeroClass::getIndex() const diff --git a/lib/CSkillHandler.cpp b/lib/CSkillHandler.cpp index de080a1c1..1d467b846 100644 --- a/lib/CSkillHandler.cpp +++ b/lib/CSkillHandler.cpp @@ -120,7 +120,7 @@ DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInf DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill) { - out << "Skill(" << (int)skill.id << "," << skill.identifier << "): ["; + out << "Skill(" << skill.id.getNum() << "," << skill.identifier << "): ["; for(int i=0; i < skill.levels.size(); i++) out << (i ? "," : "") << skill.levels[i]; return out << "]"; @@ -233,7 +233,7 @@ CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & skillAtLevel.iconMedium = levelNode["images"]["medium"].String(); skillAtLevel.iconLarge = levelNode["images"]["large"].String(); } - logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id); + logMod->debug("loaded secondary skill %s(%d)", identifier, skill->id.getNum()); return skill; } diff --git a/lib/constants/EntityIdentifiers.h b/lib/constants/EntityIdentifiers.h index 2a0aaf2ca..8cb2537bf 100644 --- a/lib/constants/EntityIdentifiers.h +++ b/lib/constants/EntityIdentifiers.h @@ -401,10 +401,6 @@ class BuildingID : public IdentifierWithEnum { public: using IdentifierWithEnum::IdentifierWithEnum; - - DLL_LINKAGE static si32 decode(const std::string & identifier); - DLL_LINKAGE static std::string encode(const si32 index); - static std::string entityType(); }; class MapObjectBaseID : public IdentifierBase diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 4707150f4..5355ee9da 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -226,7 +226,7 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog // Explicitly initialize static variables for(auto & elem : players) { - CGKeys::playerKeyMap[elem.first] = std::set(); + CGKeys::playerKeyMap[elem.first] = std::set(); } for(auto & elem : teams) { diff --git a/lib/mapObjects/CBank.cpp b/lib/mapObjects/CBank.cpp index dad8101ba..962db0023 100644 --- a/lib/mapObjects/CBank.cpp +++ b/lib/mapObjects/CBank.cpp @@ -271,7 +271,7 @@ void CBank::doVisit(const CGHeroInstance * hero) const loot.appendRawString("%d %s"); loot.replaceNumber(bc->resources[it]); loot.replaceLocalString(EMetaText::RES_NAMES, it); - cb->giveResource(hero->getOwner(), static_cast(it), bc->resources[it]); + cb->giveResource(hero->getOwner(), it, bc->resources[it]); } } //grant artifacts diff --git a/lib/mapObjects/CGDwelling.cpp b/lib/mapObjects/CGDwelling.cpp index b26f5c03d..58eb91f1d 100644 --- a/lib/mapObjects/CGDwelling.cpp +++ b/lib/mapObjects/CGDwelling.cpp @@ -344,7 +344,7 @@ void CGDwelling::updateGuards() const //default condition - creatures are of level 5 or higher for (auto creatureEntry : creatures) { - if (VLC->creatures()->getByIndex(creatureEntry.second.at(0))->getLevel() >= 5 && ID != Obj::REFUGEE_CAMP) + if (VLC->creatures()->getById(creatureEntry.second.at(0))->getLevel() >= 5 && ID != Obj::REFUGEE_CAMP) { guarded = true; break; diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index d9cd4b6c6..08f4c5be9 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -1679,7 +1679,7 @@ void CGHeroInstance::serializeCommonOptions(JsonSerializeFormat & handler) { auto addSkill = [this](const std::string & skillId, const std::string & levelId) { - const int rawId = CSkillHandler::decodeSkill(skillId); + const int rawId = SecondarySkill::decode(skillId); if(rawId < 0) { logGlobal->error("Invalid secondary skill %s", skillId); diff --git a/lib/mapObjects/CGPandoraBox.cpp b/lib/mapObjects/CGPandoraBox.cpp index 22d5aa6e6..17e05e47f 100644 --- a/lib/mapObjects/CGPandoraBox.cpp +++ b/lib/mapObjects/CGPandoraBox.cpp @@ -257,7 +257,7 @@ void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler) const std::string skillName = p.first; const std::string levelId = p.second.String(); - const int rawId = CSkillHandler::decodeSkill(skillName); + const int rawId = SecondarySkill::decode(skillName); if(rawId < 0) { logGlobal->error("Invalid secondary skill %s", skillName); diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index a58d67f56..c5cc44551 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -584,7 +584,7 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const TQuantity count = creatureGrowth(i); if (!count) // no dwelling - count = VLC->creatures()->getByIndex(c)->getGrowth(); + count = VLC->creatures()->getById(c)->getGrowth(); {//no lower tiers or above current month diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index cec1db913..5909b42bf 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -119,7 +119,7 @@ void CGMine::initObj(CRandomGenerator & rand) } else { - producedResource = GameResID(getObjTypeIndex()); + producedResource = GameResID(getObjTypeIndex().getNum()); } producedQuantity = defaultResProduction(); } @@ -773,7 +773,7 @@ void CGArtifact::initObj(CRandomGenerator & rand) std::string CGArtifact::getObjectName() const { - return VLC->artifacts()->getByIndex(getArtifact())->getNameTranslated(); + return VLC->artifacts()->getById(getArtifact())->getNameTranslated(); } void CGArtifact::onHeroVisit(const CGHeroInstance * h) const diff --git a/lib/mapping/CMap.cpp b/lib/mapping/CMap.cpp index 5ba1c3abc..e116bf4e6 100644 --- a/lib/mapping/CMap.cpp +++ b/lib/mapping/CMap.cpp @@ -72,7 +72,18 @@ void CCastleEvent::serializeJson(JsonSerializeFormat & handler) { CMapEvent::serializeJson(handler); - handler.serializeIdArray("buildings", buildings); + // TODO: handler.serializeIdArray("buildings", buildings); + { + std::vector temp(buildings.begin(), buildings.end()); + auto a = handler.enterArray("buildings"); + a.syncSize(temp); + for(int i = 0; i < temp.size(); ++i) + { + a.serializeInt(i, temp[i]); + buildings.insert(temp[i]); + } + } + { auto a = handler.enterArray("creatures"); a.syncSize(creatures); diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index 03d6bda80..70822f1e8 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -883,7 +883,7 @@ void CMapLoaderH3M::readPredefinedHeroes() } map->predefinedHeroes.emplace_back(hero); - logGlobal->debug("Map '%s': Hero predefined in map: %s", mapName, VLC->heroh->getByIndex(hero->subID)->getJsonKey()); + logGlobal->debug("Map '%s': Hero predefined in map: %s", mapName, VLC->heroh->getById(hero->getHeroType())->getJsonKey()); } } diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index c5ddcc26c..f435dab2d 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -831,7 +831,7 @@ void CMapFormatJson::serializeOptions(JsonSerializeFormat & handler) serializePredefinedHeroes(handler); - handler.serializeLIC("allowedAbilities", &CSkillHandler::decodeSkill, &CSkillHandler::encodeSkill, VLC->skillh->getDefaultAllowed(), map->allowedAbilities); + handler.serializeLIC("allowedAbilities", &SecondarySkill::decode, &SecondarySkill::encode, VLC->skillh->getDefaultAllowed(), map->allowedAbilities); handler.serializeLIC("allowedArtifacts", &ArtifactID::decode, &ArtifactID::encode, VLC->arth->getDefaultAllowed(), map->allowedArtifact); diff --git a/lib/rewardable/Reward.cpp b/lib/rewardable/Reward.cpp index 135206d90..c56837e57 100644 --- a/lib/rewardable/Reward.cpp +++ b/lib/rewardable/Reward.cpp @@ -133,7 +133,7 @@ void Rewardable::Reward::serializeJson(JsonSerializeFormat & handler) std::vector> fieldValue(secondary.begin(), secondary.end()); a.serializeStruct>(fieldValue, [](JsonSerializeFormat & h, std::pair & e) { - h.serializeId("skill", e.first, SecondarySkill{}, VLC->skillh->decodeSkill, VLC->skillh->encodeSkill); + h.serializeId("skill", e.first); h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);}); }); a.syncSize(fieldValue); diff --git a/lib/rmg/CMapGenOptions.cpp b/lib/rmg/CMapGenOptions.cpp index 439dfd5e6..1ceb3331d 100644 --- a/lib/rmg/CMapGenOptions.cpp +++ b/lib/rmg/CMapGenOptions.cpp @@ -187,7 +187,7 @@ const std::map & CMapGenOptions::g return players; } -void CMapGenOptions::setStartingTownForPlayer(const PlayerColor & color, si32 town) +void CMapGenOptions::setStartingTownForPlayer(const PlayerColor & color, FactionID town) { auto it = players.find(color); if(it == players.end()) assert(0); diff --git a/lib/rmg/CMapGenOptions.h b/lib/rmg/CMapGenOptions.h index a4795620d..278821fbb 100644 --- a/lib/rmg/CMapGenOptions.h +++ b/lib/rmg/CMapGenOptions.h @@ -114,7 +114,7 @@ public: /// The first player colors belong to standard players and the last player colors belong to comp only players. /// All standard players are by default of type EPlayerType::AI. const std::map & getPlayersSettings() const; - void setStartingTownForPlayer(const PlayerColor & color, si32 town); + void setStartingTownForPlayer(const PlayerColor & color, FactionID town); /// Sets a player type for a standard player. A standard player is the opposite of a computer only player. The /// values which can be chosen for the player type are EPlayerType::AI or EPlayerType::HUMAN. void setPlayerTypeForStandardPlayer(const PlayerColor & color, EPlayerType playerType);