2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
typedef si32 TResource;
|
2012-02-20 19:26:14 +03:00
|
|
|
typedef si64 TResourceCap; //to avoid overflow when adding integers. Signed values are easier to control.
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2012-09-02 13:33:41 +03:00
|
|
|
class JsonNode;
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
namespace Res
|
|
|
|
{
|
|
|
|
class ResourceSet;
|
|
|
|
bool canAfford(const ResourceSet &res, const ResourceSet &price); //can a be used to pay price b
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
enum ERes
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2013-04-21 15:49:26 +03:00
|
|
|
WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
|
|
|
|
|
|
|
|
WOOD_AND_ORE = 127 // special case for town bonus resource
|
2011-07-05 09:14:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//class to be representing a vector of resource
|
2012-09-24 21:52:30 +03:00
|
|
|
class ResourceSet : public std::vector<int>
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
public:
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE ResourceSet();
|
2012-09-02 13:33:41 +03:00
|
|
|
// read resources set from json. Format example: { "gold": 500, "wood":5 }
|
|
|
|
DLL_LINKAGE ResourceSet(const JsonNode & node);
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define scalarOperator(OPSIGN) \
|
2012-09-15 22:16:16 +03:00
|
|
|
ResourceSet operator OPSIGN(const TResource &rhs) const \
|
2011-07-05 09:14:07 +03:00
|
|
|
{ \
|
|
|
|
ResourceSet ret = *this; \
|
|
|
|
for(int i = 0; i < size(); i++) \
|
|
|
|
ret[i] = at(i) OPSIGN rhs; \
|
|
|
|
\
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define vectorOperator(OPSIGN) \
|
2012-09-15 22:16:16 +03:00
|
|
|
ResourceSet operator OPSIGN(const ResourceSet &rhs) const \
|
2011-07-05 09:14:07 +03:00
|
|
|
{ \
|
|
|
|
ResourceSet ret = *this; \
|
|
|
|
for(int i = 0; i < size(); i++) \
|
|
|
|
ret[i] = at(i) OPSIGN rhs[i]; \
|
|
|
|
\
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define opEqOperator(OPSIGN, RHS_TYPE) \
|
2012-09-15 22:16:16 +03:00
|
|
|
ResourceSet& operator OPSIGN ## =(const RHS_TYPE &rhs) \
|
2011-07-05 09:14:07 +03:00
|
|
|
{ \
|
|
|
|
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?"
|
2012-09-15 22:16:16 +03:00
|
|
|
int operator/(const ResourceSet &rhs)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
int ret = INT_MAX;
|
|
|
|
for(int i = 0; i < size(); i++)
|
2011-07-05 22:05:41 +03:00
|
|
|
if(rhs[i])
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amin(ret, at(i) / rhs[i]);
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
ResourceSet & operator=(const TResource &rhs)
|
2012-01-03 04:55:26 +03:00
|
|
|
{
|
|
|
|
for(int i = 0; i < size(); i++)
|
|
|
|
at(i) = rhs;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
ResourceSet operator-() const
|
|
|
|
{
|
|
|
|
ResourceSet ret;
|
|
|
|
for(int i = 0; i < size(); i++)
|
|
|
|
ret[i] = -at(i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
// WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
|
2011-07-05 09:14:07 +03:00
|
|
|
// 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;
|
2012-09-15 22:16:16 +03:00
|
|
|
//
|
2011-07-05 09:14:07 +03:00
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & static_cast<std::vector<int>&>(*this);
|
|
|
|
}
|
|
|
|
|
2012-01-26 19:48:53 +03:00
|
|
|
DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
|
2013-07-23 18:03:01 +03:00
|
|
|
DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
|
2011-12-14 00:23:17 +03:00
|
|
|
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;
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
//special iterator of iterating over non-zero resources in set
|
2011-12-14 00:23:17 +03:00
|
|
|
class DLL_LINKAGE nziterator
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
|
|
|
struct ResEntry
|
|
|
|
{
|
2013-04-20 14:34:01 +03:00
|
|
|
Res::ERes resType;
|
|
|
|
TResourceCap resVal;
|
2011-07-05 09:14:07 +03:00
|
|
|
} cur;
|
|
|
|
const ResourceSet &rs;
|
|
|
|
void advance();
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
public:
|
|
|
|
nziterator(const ResourceSet &RS);
|
|
|
|
bool valid();
|
|
|
|
nziterator operator++();
|
|
|
|
nziterator operator++(int);
|
|
|
|
const ResEntry& operator*() const;
|
|
|
|
const ResEntry* operator->() const;
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef Res::ResourceSet TResources;
|
|
|
|
|