From 0fdbf54937f1a888b06e38049d0170b8042eaed6 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 27 Jun 2023 19:32:27 +0300 Subject: [PATCH] Use HeroTypeID instead of int --- client/lobby/CBonusSelection.cpp | 2 +- lib/GameConstants.cpp | 2 ++ lib/GameConstants.h | 2 ++ lib/gameState/CGameState.cpp | 14 +++++++------- lib/gameState/CGameState.h | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/client/lobby/CBonusSelection.cpp b/client/lobby/CBonusSelection.cpp index 5834bd7e4..e27aeaaf1 100644 --- a/client/lobby/CBonusSelection.cpp +++ b/client/lobby/CBonusSelection.cpp @@ -82,7 +82,7 @@ CBonusSelection::CBonusSelection() mapName = std::make_shared(481, 219, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, CSH->mi->getName()); labelMapDescription = std::make_shared(481, 253, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]); - mapDescription = std::make_shared("", Rect(480, 280, 286, 117), 1); + mapDescription = std::make_shared("", Rect(480, 278, 292, 108), 1); labelChooseBonus = std::make_shared(511, 432, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[71]); groupBonuses = std::make_shared(std::bind(&IServerAPI::setCampaignBonus, CSH, _1)); diff --git a/lib/GameConstants.cpp b/lib/GameConstants.cpp index 026b88aa4..01b691b53 100644 --- a/lib/GameConstants.cpp +++ b/lib/GameConstants.cpp @@ -38,6 +38,8 @@ VCMI_LIB_NAMESPACE_BEGIN +const HeroTypeID HeroTypeID::NONE = HeroTypeID(-1); + const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2); const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER = SlotID(-3); const SlotID SlotID::WAR_MACHINES_SLOT = SlotID(-4); diff --git a/lib/GameConstants.h b/lib/GameConstants.h index 11ec82534..920147226 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -329,6 +329,8 @@ class HeroTypeID : public BaseForID ///json serialization helpers static si32 decode(const std::string & identifier); static std::string encode(const si32 index); + + DLL_LINKAGE static const HeroTypeID NONE; }; class SlotID : public BaseForID diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 4533d2d74..796182010 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -177,18 +177,18 @@ CGHeroInstance * CGameState::HeroesPool::pickHeroFor(bool native, return ret; } -int CGameState::pickNextHeroType(const PlayerColor & owner) +HeroTypeID CGameState::pickNextHeroType(const PlayerColor & owner) { const PlayerSettings &ps = scenarioOps->getIthPlayersSettings(owner); if(ps.hero >= 0 && !isUsedHero(HeroTypeID(ps.hero))) //we haven't used selected hero { - return ps.hero; + return HeroTypeID(ps.hero); } return pickUnusedHeroTypeRandomly(owner); } -int CGameState::pickUnusedHeroTypeRandomly(const PlayerColor & owner) +HeroTypeID CGameState::pickUnusedHeroTypeRandomly(const PlayerColor & owner) { //list of available heroes for this faction and others std::vector factionHeroes; @@ -206,23 +206,23 @@ int CGameState::pickUnusedHeroTypeRandomly(const PlayerColor & owner) // select random hero native to "our" faction if(!factionHeroes.empty()) { - return RandomGeneratorUtil::nextItem(factionHeroes, getRandomGenerator())->getNum(); + return *RandomGeneratorUtil::nextItem(factionHeroes, getRandomGenerator()); } logGlobal->warn("Cannot find free hero of appropriate faction for player %s - trying to get first available...", owner.getStr()); if(!otherHeroes.empty()) { - return RandomGeneratorUtil::nextItem(otherHeroes, getRandomGenerator())->getNum(); + return *RandomGeneratorUtil::nextItem(otherHeroes, getRandomGenerator()); } logGlobal->error("No free allowed heroes!"); auto notAllowedHeroesButStillBetterThanCrash = getUnusedAllowedHeroes(true); if(!notAllowedHeroesButStillBetterThanCrash.empty()) - return notAllowedHeroesButStillBetterThanCrash.begin()->getNum(); + return *notAllowedHeroesButStillBetterThanCrash.begin(); logGlobal->error("No free heroes at all!"); assert(0); //current code can't handle this situation - return -1; // no available heroes at all + return HeroTypeID::NONE; // no available heroes at all } std::pair CGameState::pickObject (CGObjectInstance *obj) diff --git a/lib/gameState/CGameState.h b/lib/gameState/CGameState.h index 57e263d16..99548f5b7 100644 --- a/lib/gameState/CGameState.h +++ b/lib/gameState/CGameState.h @@ -215,8 +215,8 @@ private: bool isUsedHero(const HeroTypeID & hid) const; //looks in heroes and prisons std::set getUnusedAllowedHeroes(bool alsoIncludeNotAllowed = false) const; std::pair pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns - int pickUnusedHeroTypeRandomly(const PlayerColor & owner); // picks a unused hero type randomly - int pickNextHeroType(const PlayerColor & owner); // picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly + HeroTypeID pickUnusedHeroTypeRandomly(const PlayerColor & owner); // picks a unused hero type randomly + HeroTypeID pickNextHeroType(const PlayerColor & owner); // picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly UpgradeInfo fillUpgradeInfo(const CStackInstance &stack) const; // ---- data -----