1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* ResourceSet.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
typedef si32 TResource;
typedef si64 TResourceCap; //to avoid overflow when adding integers. Signed values are easier to control.
class JsonNode;
class JsonSerializeFormat;
namespace Res
{
class ResourceSet;
bool canAfford(const ResourceSet &res, const ResourceSet &price); //can a be used to pay price b
enum ERes
{
WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
WOOD_AND_ORE = 127, // special case for town bonus resource
INVALID = -1
};
//class to be representing a vector of resource
class ResourceSet : public std::vector<int>
{
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);
#define scalarOperator(OPSIGN) \
ResourceSet operator OPSIGN(const TResource &rhs) const \
{ \
ResourceSet ret = *this; \
for(int i = 0; i < (int)size(); i++) \
ret[i] = at(i) OPSIGN rhs; \
\
return ret; \
}
#define vectorOperator(OPSIGN) \
ResourceSet operator OPSIGN(const ResourceSet &rhs) const \
{ \
ResourceSet ret = *this; \
for(int i = 0; i < (int)size(); i++) \
ret[i] = at(i) OPSIGN rhs[i]; \
\
return ret; \
}
#define opEqOperator(OPSIGN, RHS_TYPE) \
ResourceSet& operator OPSIGN ## =(const RHS_TYPE &rhs) \
{ \
return *this = *this OPSIGN rhs; \
}
scalarOperator(+)
scalarOperator(-)
scalarOperator(*)
scalarOperator(/)
opEqOperator(+, TResource)
opEqOperator(-, TResource)
opEqOperator(*, TResource)
vectorOperator(+)
vectorOperator(-)
opEqOperator(+, ResourceSet)
opEqOperator(-, ResourceSet)
#undef scalarOperator
#undef vectorOperator
#undef opEqOperator
//to be used for calculations of type "how many units of sth can I afford?"
int operator/(const ResourceSet &rhs)
{
int ret = INT_MAX;
for(int i = 0; i < (int)size(); i++)
if(rhs[i])
vstd::amin(ret, at(i) / rhs[i]);
return ret;
}
ResourceSet & operator=(const TResource &rhs)
{
for(int i = 0; i < (int)size(); i++)
at(i) = rhs;
return *this;
}
ResourceSet operator-() const
{
ResourceSet ret;
for(int i = 0; i < (int)size(); i++)
ret[i] = -at(i);
return ret;
}
// WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
// that doesn't work the other way: a > b doesn't mean that a cannot be afforded with b, it's still b can afford a
// bool operator<(const ResourceSet &rhs)
// {
// for(int i = 0; i < size(); i++)
// if(at(i) >= rhs[i])
// return false;
//
// return true;
// }
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<std::vector<int>&>(*this);
}
DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
DLL_LINKAGE void amin(const TResourceCap &val); //performs vstd::amin on each element
DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
DLL_LINKAGE TResourceCap marketValue() const;
DLL_LINKAGE std::string toString() const;
//special iterator of iterating over non-zero resources in set
class DLL_LINKAGE nziterator
{
struct ResEntry
{
Res::ERes resType;
TResourceCap resVal;
} cur;
const ResourceSet &rs;
void advance();
public:
nziterator(const ResourceSet &RS);
bool valid();
nziterator operator++();
nziterator operator++(int);
const ResEntry& operator*() const;
const ResEntry* operator->() const;
};
};
}
typedef Res::ResourceSet TResources;
|