1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +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
//changing code blocks order will alter behavior by changing order of adding elements to immediateBuildings / expensiveBuildings
TResources currentRes = cb->getResourceAmount();
TResources currentIncome = t->dailyIncome();
// TResources currentRes = cb->getResourceAmount();
// TResources currentIncome = t->dailyIncome();
if(tryBuildAnyStructure(t, essential))
return true;

View File

@ -1459,7 +1459,7 @@ void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
{
EVENT_HANDLER_CALLED_BY_CLIENT;
auto state = obj->shipyardStatus();
std::vector<si32> cost;
TResources cost;
obj->getBoatCost(cost);
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")
{
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
TResources totalSumm; // totalSum[resource ID] = value
totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
for(int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
totalSumm[i]=0;
TResources totalSum; // totalSum[resource ID] = value
for(int i=0; i<slotsCount; i++)
{
costs[i].clear();
costs[i].fill(0);
int newState = getState(SlotID(i));
if(newState != -1)
{
@ -1473,7 +1469,7 @@ void CHillFortWindow::updateGarrisons()
if(info.newID.size())//we have upgrades here - update costs
{
costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
totalSumm += costs[i];
totalSum += costs[i];
}
}
@ -1495,7 +1491,7 @@ void CHillFortWindow::updateGarrisons()
if(allUpgraded)
newState = 1;
if(!totalSumm.canBeAfforded(myRes))
if(!totalSum.canBeAfforded(myRes))
newState = 0;
}
@ -1543,7 +1539,7 @@ void CHillFortWindow::updateGarrisons()
for(int i = 0; i < resCount; i++)
{
if(totalSumm[i] == 0)
if(totalSum[i] == 0)
{
totalIcons[i]->visible = false;
totalLabels[i]->setText("");
@ -1551,7 +1547,7 @@ void CHillFortWindow::updateGarrisons()
else
{
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;
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

View File

@ -11,6 +11,7 @@
#include "CCreatureHandler.h"
#include "CGeneralTextHandler.h"
#include "ResourceSet.h"
#include "filesystem/Filesystem.h"
#include "VCMI_Lib.h"
#include "CGameState.h"
@ -267,7 +268,7 @@ bool CCreature::isEvil () const
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 resAmnt = static_cast<int>(std::min(res.size(),cost.size()));

View File

@ -197,7 +197,7 @@ public:
bool isGood () 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 std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId);
static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range

View File

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

View File

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

View File

@ -14,6 +14,7 @@
#include "../int3.h"
#include "../HeroBonus.h"
#include "../NetPacksBase.h"
#include "../ResourceSet.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -104,7 +105,7 @@ class DLL_LINKAGE IShipyard : public IBoatGenerator
public:
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 IShipyard *castFrom(CGObjectInstance *obj);

View File

@ -513,9 +513,6 @@ void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fi
{
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++)
{
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)
{
resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?
for(int x = 0; x < 7; ++x)
{
resources[x] = reader->readUInt32();