From 8248feebf772c1ffedb9d86890d183f181ca5c86 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sat, 14 Jan 2023 15:55:08 +0100 Subject: [PATCH 1/3] Implement numeric creature descriptions with config toggle on/off --- client/widgets/MiscWidgets.cpp | 12 +++++++- config/schemas/settings.json | 6 +++- lib/CCreatureHandler.cpp | 48 ++++++++++++++++++++++++------- lib/CCreatureHandler.h | 16 ++++++++++- lib/CCreatureSet.cpp | 26 ++++++++++++----- lib/CCreatureSet.h | 4 +-- lib/CGameState.cpp | 2 +- lib/mapObjects/CGTownInstance.cpp | 6 +++- lib/mapObjects/MiscObjects.cpp | 10 +++++-- 9 files changed, 103 insertions(+), 27 deletions(-) diff --git a/client/widgets/MiscWidgets.cpp b/client/widgets/MiscWidgets.cpp index e11f9ab95..aa92f9350 100644 --- a/client/widgets/MiscWidgets.cpp +++ b/client/widgets/MiscWidgets.cpp @@ -29,6 +29,7 @@ #include "../../lib/CGeneralTextHandler.h" #include "../../lib/CModHandler.h" #include "../../lib/CGameState.h" +#include "../../lib/CConfigHandler.h" void CHoverableArea::hover (bool on) { @@ -253,7 +254,16 @@ void CArmyTooltip::init(const InfoAboutArmy &army) { //if =0 - we have no information about stack size at all if(slot.second.count) - subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)]; + { + if(settings["adventure"]["numericStackQuantities"].Bool()) + { + subtitle = CCreature::getQuantityRangeStringForId((CCreature::CreatureQuantityId)slot.second.count); + } + else + { + subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)]; + } + } } subtitles.push_back(std::make_shared(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, subtitle)); diff --git a/config/schemas/settings.json b/config/schemas/settings.json index 0c78608b1..f71a39a15 100644 --- a/config/schemas/settings.json +++ b/config/schemas/settings.json @@ -128,7 +128,7 @@ "type" : "object", "additionalProperties" : false, "default": {}, - "required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder", "quickCombat" ], + "required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder", "quickCombat", "numericStackQuantities" ], "properties" : { "heroSpeed" : { "type" : "number", @@ -149,6 +149,10 @@ "quickCombat" : { "type" : "boolean", "default" : false + }, + "numericStackQuantities" : { + "type": "boolean", + "default": true } } }, diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index ac50e96c5..8064ec8da 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -171,25 +171,53 @@ int32_t CCreature::getCost(int32_t resIndex) const return 0; } -int CCreature::getQuantityID(const int & quantity) +CCreature::CreatureQuantityId CCreature::getQuantityID(const int & quantity) { if (quantity<5) - return 1; + return CCreature::CreatureQuantityId::FEW; if (quantity<10) - return 2; + return CCreature::CreatureQuantityId::SEVERAL; if (quantity<20) - return 3; + return CCreature::CreatureQuantityId::PACK; if (quantity<50) - return 4; + return CCreature::CreatureQuantityId::LOTS; if (quantity<100) - return 5; + return CCreature::CreatureQuantityId::HORDE; if (quantity<250) - return 6; + return CCreature::CreatureQuantityId::THRONG; if (quantity<500) - return 7; + return CCreature::CreatureQuantityId::SWARM; if (quantity<1000) - return 8; - return 9; + return CCreature::CreatureQuantityId::ZOUNDS; + + return CCreature::CreatureQuantityId::LEGION; +} + +std::string CCreature::getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId) +{ + switch(quantityId) + { + case CCreature::CreatureQuantityId::FEW: + return "1-4"; + case CCreature::CreatureQuantityId::SEVERAL: + return "5-9"; + case CCreature::CreatureQuantityId::PACK: + return "10-19"; + case CCreature::CreatureQuantityId::LOTS: + return "20-49"; + case CCreature::CreatureQuantityId::HORDE: + return "50-99"; + case CCreature::CreatureQuantityId::THRONG: + return "100-249"; + case CCreature::CreatureQuantityId::SWARM: + return "250-499"; + case CCreature::CreatureQuantityId::ZOUNDS: + return "500-999"; + case CCreature::CreatureQuantityId::LEGION: + return "1000+"; + default: + return "[ERROR]"; + } } int CCreature::estimateCreatureCount(ui32 countID) diff --git a/lib/CCreatureHandler.h b/lib/CCreatureHandler.h index c621d617e..0c0be2771 100644 --- a/lib/CCreatureHandler.h +++ b/lib/CCreatureHandler.h @@ -61,6 +61,19 @@ public: std::string smallIconName; std::string largeIconName; + enum class CreatureQuantityId + { + FEW = 1, + SEVERAL, + PACK, + LOTS, + HORDE, + THRONG, + SWARM, + ZOUNDS, + LEGION + }; + struct CreatureAnimation { struct RayColor { @@ -176,7 +189,8 @@ public: bool isGood () const; bool isEvil () const; si32 maxAmount(const std::vector &res) const; //how many creatures can be bought - static int getQuantityID(const int & quantity); //1 - a few, 2 - several, 3 - pack, 4 - lots, 5 - horde, 6 - throng, 7 - swarm, 8 - zounds, 9 - legion + static CCreature::CreatureQuantityId getQuantityID(const int & quantity); + static std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId); static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range bool isMyUpgrade(const CCreature *anotherCre) const; diff --git a/lib/CCreatureSet.cpp b/lib/CCreatureSet.cpp index e1551c9b2..d5a64580a 100644 --- a/lib/CCreatureSet.cpp +++ b/lib/CCreatureSet.cpp @@ -10,6 +10,7 @@ #include "StdInc.h" #include "CCreatureSet.h" +#include "CConfigHandler.h" #include "CCreatureHandler.h" #include "VCMI_Lib.h" #include "CModHandler.h" @@ -372,9 +373,15 @@ std::string CCreatureSet::getRoughAmount(SlotID slot, int mode) const { /// Mode represent return string format /// "Pack" - 0, "A pack of" - 1, "a pack of" - 2 - int quantity = CCreature::getQuantityID(getStackCount(slot)); - if(quantity) - return VLC->generaltexth->arraytxt[(174 + mode) + 3*CCreature::getQuantityID(getStackCount(slot))]; + CCreature::CreatureQuantityId quantity = CCreature::getQuantityID(getStackCount(slot)); + + if((int)quantity) + { + if(settings["adventure"]["numericStackQuantities"].Bool()) + return CCreature::getQuantityRangeStringForId(quantity); + + return VLC->generaltexth->arraytxt[(174 + mode) + 3*(int)quantity]; + } return ""; } @@ -700,7 +707,7 @@ void CStackInstance::init() setNodeType(STACK_INSTANCE); } -int CStackInstance::getQuantityID() const +CCreature::CreatureQuantityId CStackInstance::getQuantityID() const { return CCreature::getQuantityID(count); } @@ -814,10 +821,15 @@ void CStackInstance::setArmyObj(const CArmedInstance * ArmyObj) std::string CStackInstance::getQuantityTXT(bool capitalized) const { - int quantity = getQuantityID(); + CCreature::CreatureQuantityId quantity = getQuantityID(); - if (quantity) - return VLC->generaltexth->arraytxt[174 + quantity*3 - 1 - capitalized]; + if ((int)quantity) + { + if(settings["adventure"]["numericStackQuantities"].Bool()) + return CCreature::getQuantityRangeStringForId(quantity); + + return VLC->generaltexth->arraytxt[174 + (int)quantity*3 - 1 - capitalized]; + } else return ""; } diff --git a/lib/CCreatureSet.h b/lib/CCreatureSet.h index 258f3c7b2..a48df3aeb 100644 --- a/lib/CCreatureSet.h +++ b/lib/CCreatureSet.h @@ -27,7 +27,7 @@ class DLL_LINKAGE CStackBasicDescriptor { public: const CCreature *type; - TQuantity count; + TQuantity count; //exact quantity or quantity ID from CCreature::getQuantityID when getting info about enemy army CStackBasicDescriptor(); CStackBasicDescriptor(CreatureID id, TQuantity Count); @@ -93,7 +93,7 @@ public: std::string bonusToGraphics(const std::shared_ptr& bonus) const; //file name of graphics from StackSkills , in future possibly others virtual ui64 getPower() const; - int getQuantityID() const; + CCreature::CreatureQuantityId getQuantityID() const; std::string getQuantityTXT(bool capitalized = true) const; virtual int getExpRank() const; virtual int getLevel() const; //different for regular stack and commander diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index 8bf972536..65da1922d 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -3101,7 +3101,7 @@ ArmyDescriptor::ArmyDescriptor(const CArmedInstance *army, bool detailed) if(detailed) (*this)[elem.first] = *elem.second; else - (*this)[elem.first] = CStackBasicDescriptor(elem.second->type, elem.second->getQuantityID()); + (*this)[elem.first] = CStackBasicDescriptor(elem.second->type, (int)elem.second->getQuantityID()); } } diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 55b5ac447..8096a36bb 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -14,6 +14,7 @@ #include "../spells/CSpellHandler.h" #include "../NetPacks.h" +#include "../CConfigHandler.h" #include "../CGeneralTextHandler.h" #include "../CModHandler.h" #include "../IGameCallback.h" @@ -203,7 +204,10 @@ void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const bd.player = h->tempOwner; bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards? bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID); - bd.text.addReplacement(MetaString::ARRAY_TXT, 173 + Slots().begin()->second->getQuantityID()*3); + if(settings["adventure"]["numericStackQuantities"].Bool()) + bd.text.addReplacement(CCreature::getQuantityRangeStringForId(Slots().begin()->second->getQuantityID())); + else + bd.text.addReplacement(MetaString::ARRAY_TXT, 173 + (int)Slots().begin()->second->getQuantityID()*3); bd.text.addReplacement(*Slots().begin()->second); cb->showBlockingDialog(&bd); return; diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 3d784a602..c93c05820 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -15,6 +15,7 @@ #include "../NetPacks.h" #include "../CGeneralTextHandler.h" #include "../CSoundBase.h" +#include "../CConfigHandler.h" #include "../CModHandler.h" #include "../CHeroHandler.h" #include "../CSkillHandler.h" @@ -101,9 +102,12 @@ std::string CGCreature::getHoverText(PlayerColor player) const std::string hoverName; MetaString ms; - int pom = stacks.begin()->second->getQuantityID(); - pom = 172 + 3*pom; - ms.addTxt(MetaString::ARRAY_TXT,pom); + CCreature::CreatureQuantityId monsterQuantityId = stacks.begin()->second->getQuantityID(); + int quantityTextIndex = 172 + 3 * (int)monsterQuantityId; + if(settings["adventure"]["numericStackQuantities"].Bool()) + ms << CCreature::getQuantityRangeStringForId(monsterQuantityId); + else + ms.addTxt(MetaString::ARRAY_TXT, quantityTextIndex); ms << " " ; ms.addTxt(MetaString::CRE_PL_NAMES,subID); ms.toString(hoverName); From 804b164c75a38e46176de218e8628a00ed2e77ed Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sun, 15 Jan 2023 16:19:55 +0100 Subject: [PATCH 2/3] Changed getQuantityRangeStringForId handling --- lib/CCreatureHandler.cpp | 42 ++++++++++++++++++---------------------- lib/CCreatureHandler.h | 1 + 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index 8064ec8da..0f251af6b 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -24,6 +24,19 @@ VCMI_LIB_NAMESPACE_BEGIN +const std::map CCreature::creatureQuantityRanges = +{ + {CCreature::CreatureQuantityId::FEW, "1-4"}, + {CCreature::CreatureQuantityId::SEVERAL, "5-9"}, + {CCreature::CreatureQuantityId::PACK, "10-19"}, + {CCreature::CreatureQuantityId::LOTS, "20-49"}, + {CCreature::CreatureQuantityId::HORDE, "50-99"}, + {CCreature::CreatureQuantityId::THRONG, "100-249"}, + {CCreature::CreatureQuantityId::SWARM, "250-499"}, + {CCreature::CreatureQuantityId::ZOUNDS, "500-999"}, + {CCreature::CreatureQuantityId::LEGION, "1000+"} +}; + int32_t CCreature::getIndex() const { return idNumber.toEnum(); @@ -195,29 +208,12 @@ CCreature::CreatureQuantityId CCreature::getQuantityID(const int & quantity) std::string CCreature::getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId) { - switch(quantityId) - { - case CCreature::CreatureQuantityId::FEW: - return "1-4"; - case CCreature::CreatureQuantityId::SEVERAL: - return "5-9"; - case CCreature::CreatureQuantityId::PACK: - return "10-19"; - case CCreature::CreatureQuantityId::LOTS: - return "20-49"; - case CCreature::CreatureQuantityId::HORDE: - return "50-99"; - case CCreature::CreatureQuantityId::THRONG: - return "100-249"; - case CCreature::CreatureQuantityId::SWARM: - return "250-499"; - case CCreature::CreatureQuantityId::ZOUNDS: - return "500-999"; - case CCreature::CreatureQuantityId::LEGION: - return "1000+"; - default: - return "[ERROR]"; - } + if(creatureQuantityRanges.find(quantityId) != creatureQuantityRanges.end()) + return creatureQuantityRanges.at(quantityId); + + logGlobal->error("Wrong quantityId: %d", (int)quantityId); + assert(0); + return "[ERROR]"; } int CCreature::estimateCreatureCount(ui32 countID) diff --git a/lib/CCreatureHandler.h b/lib/CCreatureHandler.h index 0c0be2771..2c2e0a00c 100644 --- a/lib/CCreatureHandler.h +++ b/lib/CCreatureHandler.h @@ -248,6 +248,7 @@ public: private: void fillWarMachine(); + static const std::map creatureQuantityRanges; }; class DLL_LINKAGE CCreatureHandler : public CHandlerBase From e48a4185edab00a3321204f6614430c584c2d228 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Wed, 15 Feb 2023 23:36:09 +0100 Subject: [PATCH 3/3] Use config values from new settings --- client/widgets/MiscWidgets.cpp | 2 +- config/schemas/settings.json | 6 +----- lib/CCreatureSet.cpp | 4 ++-- lib/mapObjects/CGTownInstance.cpp | 2 +- lib/mapObjects/MiscObjects.cpp | 2 +- 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/client/widgets/MiscWidgets.cpp b/client/widgets/MiscWidgets.cpp index eec22585d..329e331e0 100644 --- a/client/widgets/MiscWidgets.cpp +++ b/client/widgets/MiscWidgets.cpp @@ -255,7 +255,7 @@ void CArmyTooltip::init(const InfoAboutArmy &army) //if =0 - we have no information about stack size at all if(slot.second.count) { - if(settings["adventure"]["numericStackQuantities"].Bool()) + if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool()) { subtitle = CCreature::getQuantityRangeStringForId((CCreature::CreatureQuantityId)slot.second.count); } diff --git a/config/schemas/settings.json b/config/schemas/settings.json index 67c4d682c..d3472c3ab 100644 --- a/config/schemas/settings.json +++ b/config/schemas/settings.json @@ -134,7 +134,7 @@ "type" : "object", "additionalProperties" : false, "default": {}, - "required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder", "quickCombat", "numericStackQuantities" ], + "required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder", "quickCombat" ], "properties" : { "heroSpeed" : { "type" : "number", @@ -155,10 +155,6 @@ "quickCombat" : { "type" : "boolean", "default" : false - }, - "numericStackQuantities" : { - "type": "boolean", - "default": true } } }, diff --git a/lib/CCreatureSet.cpp b/lib/CCreatureSet.cpp index 9af45bc8d..a686a436c 100644 --- a/lib/CCreatureSet.cpp +++ b/lib/CCreatureSet.cpp @@ -377,7 +377,7 @@ std::string CCreatureSet::getRoughAmount(SlotID slot, int mode) const if((int)quantity) { - if(settings["adventure"]["numericStackQuantities"].Bool()) + if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool()) return CCreature::getQuantityRangeStringForId(quantity); return VLC->generaltexth->arraytxt[(174 + mode) + 3*(int)quantity]; @@ -825,7 +825,7 @@ std::string CStackInstance::getQuantityTXT(bool capitalized) const if ((int)quantity) { - if(settings["adventure"]["numericStackQuantities"].Bool()) + if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool()) return CCreature::getQuantityRangeStringForId(quantity); return VLC->generaltexth->arraytxt[174 + (int)quantity*3 - 1 - capitalized]; diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 4e8456fab..44d62c991 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -205,7 +205,7 @@ void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const bd.player = h->tempOwner; bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards? bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID); - if(settings["adventure"]["numericStackQuantities"].Bool()) + if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool()) bd.text.addReplacement(CCreature::getQuantityRangeStringForId(Slots().begin()->second->getQuantityID())); else bd.text.addReplacement(MetaString::ARRAY_TXT, 173 + (int)Slots().begin()->second->getQuantityID()*3); diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index f43e3e948..e58aee1d0 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -104,7 +104,7 @@ std::string CGCreature::getHoverText(PlayerColor player) const MetaString ms; CCreature::CreatureQuantityId monsterQuantityId = stacks.begin()->second->getQuantityID(); int quantityTextIndex = 172 + 3 * (int)monsterQuantityId; - if(settings["adventure"]["numericStackQuantities"].Bool()) + if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool()) ms << CCreature::getQuantityRangeStringForId(monsterQuantityId); else ms.addTxt(MetaString::ARRAY_TXT, quantityTextIndex);