From e2bd98e21ee72839519dfd9b13101c63e17a9bb4 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Mon, 26 Jun 2023 01:42:53 +0300 Subject: [PATCH] CampaignRegions class encapsulation --- client/lobby/CBonusSelection.cpp | 22 +++++----------- client/lobby/CBonusSelection.h | 2 +- lib/campaign/CampaignState.cpp | 41 ++++++++++++++++++++++++++++++ lib/campaign/CampaignState.h | 25 +++++++++++++----- lib/gameState/CGameState.h | 2 +- lib/gameState/CGameStateCampaign.h | 2 +- 6 files changed, 69 insertions(+), 25 deletions(-) diff --git a/client/lobby/CBonusSelection.cpp b/client/lobby/CBonusSelection.cpp index f026ce6a1..8ad35d89f 100644 --- a/client/lobby/CBonusSelection.cpp +++ b/client/lobby/CBonusSelection.cpp @@ -64,7 +64,7 @@ CBonusSelection::CBonusSelection() { OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; - std::string bgName = getCampaign()->getRegions().campPrefix + "_BG.BMP"; + std::string bgName = getCampaign()->getRegions().getBackgroundName(); setBackground(bgName); panelBackground = std::make_shared("CAMPBRF.BMP", 456, 6); @@ -449,27 +449,19 @@ CBonusSelection::CRegion::CRegion(CampaignScenarioID id, bool accessible, bool s : CIntObject(LCLICK | SHOW_POPUP), idOfMapAndRegion(id), accessible(accessible), selectable(selectable) { OBJ_CONSTRUCTION; - static const std::string colors[2][8] = - { - {"R", "B", "N", "G", "O", "V", "T", "P"}, - {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"} - }; - const CampaignRegions::RegionDescription & desc = campDsc.regions[static_cast(idOfMapAndRegion)]; - pos.x += desc.xpos; - pos.y += desc.ypos; + pos += campDsc.getPosition(id); - std::string prefix = campDsc.campPrefix + desc.infix + "_"; - std::string suffix = colors[campDsc.colorSuffixLength - 1][CSH->si->campState->scenario(idOfMapAndRegion).regionColor]; - graphicsNotSelected = std::make_shared(prefix + "En" + suffix + ".BMP"); + auto color = CSH->si->campState->scenario(idOfMapAndRegion).regionColor; + + graphicsNotSelected = std::make_shared(campDsc.getAvailableName(id, color)); graphicsNotSelected->disable(); - graphicsSelected = std::make_shared(prefix + "Se" + suffix + ".BMP"); + graphicsSelected = std::make_shared(campDsc.getSelectedName(id, color)); graphicsSelected->disable(); - graphicsStriped = std::make_shared(prefix + "Co" + suffix + ".BMP"); + graphicsStriped = std::make_shared(campDsc.getConqueredName(id, color)); graphicsStriped->disable(); pos.w = graphicsNotSelected->pos.w; pos.h = graphicsNotSelected->pos.h; - } void CBonusSelection::CRegion::updateState() diff --git a/client/lobby/CBonusSelection.h b/client/lobby/CBonusSelection.h index 795b8b58f..0c5112d00 100644 --- a/client/lobby/CBonusSelection.h +++ b/client/lobby/CBonusSelection.h @@ -16,7 +16,7 @@ VCMI_LIB_NAMESPACE_BEGIN class CampaignState; -struct CampaignRegions; +class CampaignRegions; VCMI_LIB_NAMESPACE_END diff --git a/lib/campaign/CampaignState.cpp b/lib/campaign/CampaignState.cpp index 77947ad6b..0f5bdbf6e 100644 --- a/lib/campaign/CampaignState.cpp +++ b/lib/campaign/CampaignState.cpp @@ -66,6 +66,47 @@ CampaignRegions CampaignRegions::getLegacy(int campId) return campDescriptions.at(campId); } +std::string CampaignRegions::getBackgroundName() const +{ + return campPrefix + "_BG.BMP"; +} + +Point CampaignRegions::getPosition(CampaignScenarioID which) const +{ + auto const & region = regions[static_cast(which)]; + return Point(region.xpos, region.ypos); +} + +std::string CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, std::string type) const +{ + auto const & region = regions[static_cast(which)]; + + static const std::string colors[2][8] = + { + {"R", "B", "N", "G", "O", "V", "T", "P"}, + {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"} + }; + + std::string color = colors[colorSuffixLength - 1][colorIndex]; + + return campPrefix + region.infix + "_" + type + color + ".BMP"; +} + +std::string CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const +{ + return getNameFor(which, color, "En"); +} + +std::string CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const +{ + return getNameFor(which, color, "Se"); +} + +std::string CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const +{ + return getNameFor(which, color, "Co"); +} + bool CampaignBonus::isBonusForHero() const { diff --git a/lib/campaign/CampaignState.h b/lib/campaign/CampaignState.h index 1e76addf5..f86be2449 100644 --- a/lib/campaign/CampaignState.h +++ b/lib/campaign/CampaignState.h @@ -23,8 +23,9 @@ class CMap; class CMapHeader; class CMapInfo; class JsonNode; +class Point; -struct DLL_LINKAGE CampaignRegions +class DLL_LINKAGE CampaignRegions { std::string campPrefix; int colorSuffixLength; @@ -46,6 +47,15 @@ struct DLL_LINKAGE CampaignRegions std::vector regions; + std::string getNameFor(CampaignScenarioID which, int color, std::string type) const; + +public: + std::string getBackgroundName() const; + Point getPosition(CampaignScenarioID which) const; + std::string getAvailableName(CampaignScenarioID which, int color) const; + std::string getSelectedName(CampaignScenarioID which, int color) const; + std::string getConqueredName(CampaignScenarioID which, int color) const; + template void serialize(Handler &h, const int formatVersion) { h & campPrefix; @@ -101,7 +111,7 @@ public: struct DLL_LINKAGE CampaignBonus { - CampaignBonusType type = CampaignBonusType::NONE; //uses EBonusType + CampaignBonusType type = CampaignBonusType::NONE; //purpose depends on type int32_t info1 = 0; @@ -119,10 +129,8 @@ struct DLL_LINKAGE CampaignBonus } }; -class DLL_LINKAGE CampaignTravel +struct DLL_LINKAGE CampaignTravel { -public: - struct DLL_LINKAGE WhatHeroKeeps { bool experience = false; @@ -160,9 +168,8 @@ public: } }; -class DLL_LINKAGE CampaignScenario +struct DLL_LINKAGE CampaignScenario { -public: std::string mapName; //*.h3m std::string scenarioName; //from header. human-readble std::set preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c) @@ -242,12 +249,16 @@ class DLL_LINKAGE CampaignState : public Campaign CampaignHeroes crossover; public: + /// Returns last completed scenario, if any std::optional lastScenario() const; + std::optional currentScenario() const; std::set conqueredScenarios() const; + /// Returns bonus selected for specific scenario std::optional getBonus(CampaignScenarioID which) const; + /// Returns index of selected bonus for specified scenario std::optional getBonusID(CampaignScenarioID which) const; /// Returns true if selected scenario can be selected and started by player diff --git a/lib/gameState/CGameState.h b/lib/gameState/CGameState.h index 085717e09..1ea530a6b 100644 --- a/lib/gameState/CGameState.h +++ b/lib/gameState/CGameState.h @@ -26,7 +26,7 @@ class CMap; struct CPack; class CHeroClass; struct EventCondition; -class CampaignTravel; +struct CampaignTravel; class CStackInstance; class CGameStateCampaign; diff --git a/lib/gameState/CGameStateCampaign.h b/lib/gameState/CGameStateCampaign.h index 7a4dc0604..e6e106f28 100644 --- a/lib/gameState/CGameStateCampaign.h +++ b/lib/gameState/CGameStateCampaign.h @@ -14,7 +14,7 @@ VCMI_LIB_NAMESPACE_BEGIN struct CampaignBonus; -class CampaignTravel; +struct CampaignTravel; class CGHeroInstance; class CGameState; class CMap;