1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-21 12:06:49 +02:00

Modernize resourceSet

This commit is contained in:
Konstantin 2023-03-17 02:19:04 +03:00
parent ecbbbeda9b
commit bbbbfe00f0
12 changed files with 54 additions and 72 deletions

View File

@ -166,8 +166,8 @@ bool BuildingManager::getBuildingOptions(const CGTownInstance * t)
//below algorithm focuses on economy growth at start of the game, saving money instead of build rushing is handled by Build goal //below algorithm focuses on economy growth at start of the game, saving money instead of build rushing is handled by Build goal
//changing code blocks order will alter behavior by changing order of adding elements to immediateBuildings / expensiveBuildings //changing code blocks order will alter behavior by changing order of adding elements to immediateBuildings / expensiveBuildings
TResources currentRes = cb->getResourceAmount(); // TResources currentRes = cb->getResourceAmount();
TResources currentIncome = t->dailyIncome(); // TResources currentIncome = t->dailyIncome();
if(tryBuildAnyStructure(t, essential)) if(tryBuildAnyStructure(t, essential))
return true; return true;

View File

@ -1459,7 +1459,7 @@ void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
{ {
EVENT_HANDLER_CALLED_BY_CLIENT; EVENT_HANDLER_CALLED_BY_CLIENT;
auto state = obj->shipyardStatus(); auto state = obj->shipyardStatus();
std::vector<si32> cost; TResources cost;
obj->getBoatCost(cost); obj->getBoatCost(cost);
GH.pushIntT<CShipyardWindow>(cost, state, obj->getBoatType(), [=](){ cb->buildBoat(obj); }); GH.pushIntT<CShipyardWindow>(cost, state, obj->getBoatType(), [=](){ cb->buildBoat(obj); });
} }

View File

@ -1079,7 +1079,7 @@ void CExchangeWindow::updateWidgets()
} }
} }
CShipyardWindow::CShipyardWindow(const std::vector<si32> & cost, int state, int boatType, const std::function<void()> & onBuy) CShipyardWindow::CShipyardWindow(const TResources & cost, int state, int boatType, const std::function<void()> & onBuy)
: CStatusbarWindow(PLAYER_COLORED, "TPSHIP") : CStatusbarWindow(PLAYER_COLORED, "TPSHIP")
{ {
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
@ -1456,15 +1456,11 @@ void CHillFortWindow::updateGarrisons()
{ {
std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
TResources totalSumm; // totalSum[resource ID] = value TResources totalSum; // totalSum[resource ID] = value
totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
for(int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
totalSumm[i]=0;
for(int i=0; i<slotsCount; i++) for(int i=0; i<slotsCount; i++)
{ {
costs[i].clear(); costs[i].fill(0);
int newState = getState(SlotID(i)); int newState = getState(SlotID(i));
if(newState != -1) if(newState != -1)
{ {
@ -1473,7 +1469,7 @@ void CHillFortWindow::updateGarrisons()
if(info.newID.size())//we have upgrades here - update costs if(info.newID.size())//we have upgrades here - update costs
{ {
costs[i] = info.cost[0] * hero->getStackCount(SlotID(i)); costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
totalSumm += costs[i]; totalSum += costs[i];
} }
} }
@ -1495,7 +1491,7 @@ void CHillFortWindow::updateGarrisons()
if(allUpgraded) if(allUpgraded)
newState = 1; newState = 1;
if(!totalSumm.canBeAfforded(myRes)) if(!totalSum.canBeAfforded(myRes))
newState = 0; newState = 0;
} }
@ -1543,7 +1539,7 @@ void CHillFortWindow::updateGarrisons()
for(int i = 0; i < resCount; i++) for(int i = 0; i < resCount; i++)
{ {
if(totalSumm[i] == 0) if(totalSum[i] == 0)
{ {
totalIcons[i]->visible = false; totalIcons[i]->visible = false;
totalLabels[i]->setText(""); totalLabels[i]->setText("");
@ -1551,7 +1547,7 @@ void CHillFortWindow::updateGarrisons()
else else
{ {
totalIcons[i]->visible = true; totalIcons[i]->visible = true;
totalLabels[i]->setText(std::to_string(totalSumm[i])); totalLabels[i]->setText(std::to_string(totalSum[i]));
} }
} }
} }

View File

@ -358,7 +358,7 @@ class CShipyardWindow : public CStatusbarWindow
std::shared_ptr<CButton> quit; std::shared_ptr<CButton> quit;
public: public:
CShipyardWindow(const std::vector<si32> & cost, int state, int boatType, const std::function<void()> & onBuy); CShipyardWindow(const TResources & cost, int state, int boatType, const std::function<void()> & onBuy);
}; };
/// Creature transformer window /// Creature transformer window

View File

@ -11,6 +11,7 @@
#include "CCreatureHandler.h" #include "CCreatureHandler.h"
#include "CGeneralTextHandler.h" #include "CGeneralTextHandler.h"
#include "ResourceSet.h"
#include "filesystem/Filesystem.h" #include "filesystem/Filesystem.h"
#include "VCMI_Lib.h" #include "VCMI_Lib.h"
#include "CGameState.h" #include "CGameState.h"
@ -267,7 +268,7 @@ bool CCreature::isEvil () const
return (*VLC->townh)[faction]->alignment == EAlignment::EVIL; return (*VLC->townh)[faction]->alignment == EAlignment::EVIL;
} }
si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought si32 CCreature::maxAmount(const TResources &res) const //how many creatures can be bought
{ {
int ret = 2147483645; int ret = 2147483645;
int resAmnt = static_cast<int>(std::min(res.size(),cost.size())); int resAmnt = static_cast<int>(std::min(res.size(),cost.size()));

View File

@ -197,7 +197,7 @@ public:
bool isGood () const; bool isGood () const;
bool isEvil () const; bool isEvil () const;
si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought si32 maxAmount(const TResources &res) const; //how many creatures can be bought
static CCreature::CreatureQuantityId getQuantityID(const int & quantity); static CCreature::CreatureQuantityId getQuantityID(const int & quantity);
static std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId); static std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId);
static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range

View File

@ -9,6 +9,7 @@
*/ */
#include "StdInc.h" #include "StdInc.h"
#include "GameConstants.h"
#include "ResourceSet.h" #include "ResourceSet.h"
#include "StringConstants.h" #include "StringConstants.h"
#include "JsonNode.h" #include "JsonNode.h"
@ -18,37 +19,27 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
Res::ResourceSet::ResourceSet()
{
resize(GameConstants::RESOURCE_QUANTITY, 0);
}
Res::ResourceSet::ResourceSet(const JsonNode & node) Res::ResourceSet::ResourceSet(const JsonNode & node)
{ {
reserve(GameConstants::RESOURCE_QUANTITY); for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
for(const std::string & name : GameConstants::RESOURCE_NAMES) at(i) = static_cast<int>(node[GameConstants::RESOURCE_NAMES[i]].Float());
push_back(static_cast<int>(node[name].Float()));
} }
Res::ResourceSet::ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal, Res::ResourceSet::ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal,
TResource gems, TResource gold, TResource mithril) TResource gems, TResource gold, TResource mithril)
{ {
resize(GameConstants::RESOURCE_QUANTITY); this[Res::WOOD] = wood;
auto * d = data(); this[Res::MERCURY] = mercury;
d[Res::WOOD] = wood; this[Res::ORE] = ore;
d[Res::MERCURY] = mercury; this[Res::SULFUR] = sulfur;
d[Res::ORE] = ore; this[Res::CRYSTAL] = crystal;
d[Res::SULFUR] = sulfur; this[Res::GEMS] = gems;
d[Res::CRYSTAL] = crystal; this[Res::GOLD] = gold;
d[Res::GEMS] = gems; this[Res::MITHRIL] = mithril;
d[Res::GOLD] = gold;
d[Res::MITHRIL] = mithril;
} }
void Res::ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName) void Res::ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
{ {
if(!handler.saving)
resize(GameConstants::RESOURCE_QUANTITY, 0);
if(handler.saving && !nonZero()) if(handler.saving && !nonZero())
return; return;
auto s = handler.enterStruct(fieldName); auto s = handler.enterStruct(fieldName);

View File

@ -10,6 +10,7 @@
#pragma once #pragma once
#include "GameConstants.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
typedef si32 TResource; typedef si32 TResource;
@ -32,60 +33,57 @@ namespace Res
}; };
//class to be representing a vector of resource //class to be representing a vector of resource
class ResourceSet : public std::vector<int> class ResourceSet : public std::array<int, GameConstants::RESOURCE_QUANTITY>
{ {
public: public:
DLL_LINKAGE ResourceSet();
// read resources set from json. Format example: { "gold": 500, "wood":5 } // read resources set from json. Format example: { "gold": 500, "wood":5 }
DLL_LINKAGE ResourceSet(const JsonNode & node); DLL_LINKAGE ResourceSet(const JsonNode & node);
DLL_LINKAGE ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal, DLL_LINKAGE ResourceSet(TResource wood = 0, TResource mercury = 0, TResource ore = 0, TResource sulfur = 0, TResource crystal = 0,
TResource gems, TResource gold, TResource mithril = 0); TResource gems = 0, TResource gold = 0, TResource mithril = 0);
#define scalarOperator(OPSIGN) \ #define scalarOperator(OPSIGN) \
ResourceSet operator OPSIGN(const TResource &rhs) const \ ResourceSet& operator OPSIGN ## =(const TResource &rhs) \
{ \ { \
ResourceSet ret = *this; \ for(auto i = 0; i < size(); i++) \
for(int i = 0; i < (int)size(); i++) \ at(i) OPSIGN ## = rhs; \
ret[i] = at(i) OPSIGN rhs; \
\ \
return ret; \ return *this; \
} }
#define vectorOperator(OPSIGN) \ #define vectorOperator(OPSIGN) \
ResourceSet operator OPSIGN(const ResourceSet &rhs) const \ ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
{ \ { \
ResourceSet ret = *this; \
for(int i = 0; i < (int)size(); i++) \ for(int i = 0; i < (int)size(); i++) \
ret[i] = at(i) OPSIGN rhs[i]; \ at(i) OPSIGN ## = rhs[i]; \
\ \
return ret; \ return *this; \
} }
#define twoOperands(OPSIGN, RHS_TYPE) \
#define opEqOperator(OPSIGN, RHS_TYPE) \ friend ResourceSet operator OPSIGN(ResourceSet lhs, const RHS_TYPE &rhs) \
ResourceSet& operator OPSIGN ## =(const RHS_TYPE &rhs) \
{ \ { \
return *this = *this OPSIGN rhs; \ lhs OPSIGN ## = rhs; \
return lhs; \
} }
scalarOperator(+) scalarOperator(+)
scalarOperator(-) scalarOperator(-)
scalarOperator(*) scalarOperator(*)
scalarOperator(/) scalarOperator(/)
opEqOperator(+, TResource)
opEqOperator(-, TResource)
opEqOperator(*, TResource)
vectorOperator(+) vectorOperator(+)
vectorOperator(-) vectorOperator(-)
opEqOperator(+, ResourceSet) twoOperands(+, TResource)
opEqOperator(-, ResourceSet) twoOperands(-, TResource)
twoOperands(*, TResource)
twoOperands(/, TResource)
twoOperands(+, ResourceSet)
twoOperands(-, ResourceSet)
#undef scalarOperator #undef scalarOperator
#undef vectorOperator #undef vectorOperator
#undef opEqOperator #undef twoOperands
//to be used for calculations of type "how many units of sth can I afford?" //to be used for calculations of type "how many units of sth can I afford?"
int operator/(const ResourceSet &rhs) int operator/(const ResourceSet &rhs)
@ -127,7 +125,7 @@ namespace Res
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
h & static_cast<std::vector<int>&>(*this); h & static_cast<std::array<int, GameConstants::RESOURCE_QUANTITY>&>(*this);
} }
DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName); DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
@ -167,7 +165,7 @@ namespace Res
}; };
} }
typedef Res::ResourceSet TResources; using TResources = Res::ResourceSet;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -492,9 +492,8 @@ void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visit
} }
} }
void IShipyard::getBoatCost( std::vector<si32> &cost ) const void IShipyard::getBoatCost(TResources & cost) const
{ {
cost.resize(GameConstants::RESOURCE_QUANTITY);
cost[Res::WOOD] = 10; cost[Res::WOOD] = 10;
cost[Res::GOLD] = 1000; cost[Res::GOLD] = 1000;
} }

View File

@ -14,6 +14,7 @@
#include "../int3.h" #include "../int3.h"
#include "../HeroBonus.h" #include "../HeroBonus.h"
#include "../NetPacksBase.h" #include "../NetPacksBase.h"
#include "../ResourceSet.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -104,7 +105,7 @@ class DLL_LINKAGE IShipyard : public IBoatGenerator
public: public:
IShipyard(const CGObjectInstance *O); IShipyard(const CGObjectInstance *O);
virtual void getBoatCost(std::vector<si32> &cost) const; virtual void getBoatCost(TResources & cost) const;
static const IShipyard *castFrom(const CGObjectInstance *obj); static const IShipyard *castFrom(const CGObjectInstance *obj);
static IShipyard *castFrom(CGObjectInstance *obj); static IShipyard *castFrom(CGObjectInstance *obj);

View File

@ -513,9 +513,6 @@ void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fi
{ {
auto r = handler.enterStruct("resources"); auto r = handler.enterStruct("resources");
if(!handler.saving)
m7resources.resize(GameConstants::RESOURCE_QUANTITY-1);
for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++) for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
{ {
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0); handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], m7resources[idx], 0);

View File

@ -2195,7 +2195,6 @@ void CMapLoaderH3M::readSpells(std::set<SpellID>& dest)
void CMapLoaderH3M::readResourses(TResources& resources) void CMapLoaderH3M::readResourses(TResources& resources)
{ {
resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
for(int x = 0; x < 7; ++x) for(int x = 0; x < 7; ++x)
{ {
resources[x] = reader->readUInt32(); resources[x] = reader->readUInt32();