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
|
|
|
|
*
|
|
|
|
*/
|
2016-10-11 17:00:54 +02:00
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2023-03-17 01:19:04 +02:00
|
|
|
#include "GameConstants.h"
|
2023-04-05 02:26:29 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-03-31 12:31:48 +02:00
|
|
|
using TResource = int32_t;
|
|
|
|
using TResourceCap = int64_t; //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;
|
2016-11-13 12:38:42 +02:00
|
|
|
class JsonSerializeFormat;
|
2012-09-02 13:33:41 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
class ResourceSet;
|
|
|
|
|
|
|
|
enum class EGameResID : int8_t
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
WOOD_AND_ORE = 127, // special case for town bonus resource
|
|
|
|
INVALID = -1
|
|
|
|
};
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
using GameResID = Identifier<EGameResID>;
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
//class to be representing a vector of resource
|
|
|
|
class ResourceSet
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::array<TResource, GameConstants::RESOURCE_QUANTITY> container;
|
|
|
|
public:
|
|
|
|
// read resources set from json. Format example: { "gold": 500, "wood":5 }
|
|
|
|
DLL_LINKAGE ResourceSet(const JsonNode & node);
|
|
|
|
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);
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define scalarOperator(OPSIGN) \
|
2023-04-05 02:26:29 +02:00
|
|
|
ResourceSet& operator OPSIGN ## =(const TResource &rhs) \
|
|
|
|
{ \
|
|
|
|
for(auto i = 0; i < container.size(); i++) \
|
|
|
|
container.at(i) OPSIGN ## = rhs; \
|
|
|
|
\
|
|
|
|
return *this; \
|
|
|
|
}
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
#define vectorOperator(OPSIGN) \
|
2023-04-05 02:26:29 +02:00
|
|
|
ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
|
|
|
|
{ \
|
|
|
|
for(auto i = 0; i < container.size(); i++) \
|
|
|
|
container.at(i) OPSIGN ## = rhs[i]; \
|
|
|
|
\
|
|
|
|
return *this; \
|
|
|
|
}
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-03-17 01:19:04 +02:00
|
|
|
#define twoOperands(OPSIGN, RHS_TYPE) \
|
2023-04-05 02:26:29 +02:00
|
|
|
friend ResourceSet operator OPSIGN(ResourceSet lhs, const RHS_TYPE &rhs) \
|
|
|
|
{ \
|
|
|
|
lhs OPSIGN ## = rhs; \
|
|
|
|
return lhs; \
|
|
|
|
}
|
|
|
|
|
|
|
|
scalarOperator(+)
|
|
|
|
scalarOperator(-)
|
|
|
|
scalarOperator(*)
|
|
|
|
scalarOperator(/)
|
|
|
|
vectorOperator(+)
|
|
|
|
vectorOperator(-)
|
|
|
|
twoOperands(+, TResource)
|
|
|
|
twoOperands(-, TResource)
|
|
|
|
twoOperands(*, TResource)
|
|
|
|
twoOperands(/, TResource)
|
|
|
|
twoOperands(+, ResourceSet)
|
|
|
|
twoOperands(-, ResourceSet)
|
2023-03-17 01:19:04 +02:00
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
#undef scalarOperator
|
|
|
|
#undef vectorOperator
|
2023-03-17 01:19:04 +02:00
|
|
|
#undef twoOperands
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
using const_reference = decltype(container)::const_reference;
|
|
|
|
using value_type = decltype(container)::value_type;
|
|
|
|
using const_iterator = decltype(container)::const_iterator;
|
|
|
|
using iterator = decltype(container)::iterator;
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
// Array-like interface
|
|
|
|
TResource & operator[](GameResID index)
|
|
|
|
{
|
|
|
|
return operator[](index.getNum());
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
const TResource & operator[](GameResID index) const
|
|
|
|
{
|
|
|
|
return operator[](index.getNum());
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
TResource & operator[](size_t index)
|
|
|
|
{
|
|
|
|
return container.at(index);
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
const TResource & operator[](size_t index) const
|
|
|
|
{
|
|
|
|
return container.at(index);
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
bool empty () const
|
|
|
|
{
|
|
|
|
for(const auto & res : *this)
|
|
|
|
if(res)
|
|
|
|
return false;
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
// C++ range-based for support
|
|
|
|
auto begin () -> decltype (container.begin())
|
|
|
|
{
|
|
|
|
return container.begin();
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
auto end () -> decltype (container.end())
|
|
|
|
{
|
|
|
|
return container.end();
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
auto begin () const -> decltype (container.cbegin())
|
|
|
|
{
|
|
|
|
return container.cbegin();
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
auto end () const -> decltype (container.cend())
|
|
|
|
{
|
|
|
|
return container.cend();
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
auto size () const -> decltype (container.size())
|
|
|
|
{
|
|
|
|
return container.size();
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
//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 < container.size(); i++)
|
|
|
|
if(rhs[i])
|
|
|
|
vstd::amin(ret, container.at(i) / rhs[i]);
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
ResourceSet & operator=(const TResource &rhs)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < container.size(); i++)
|
|
|
|
container.at(i) = rhs;
|
2012-01-03 04:55:26 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2012-01-03 04:55:26 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
ResourceSet operator-() const
|
|
|
|
{
|
|
|
|
ResourceSet ret;
|
|
|
|
for(int i = 0; i < container.size(); i++)
|
|
|
|
ret[i] = -container.at(i);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
bool operator==(const ResourceSet &rhs) const
|
|
|
|
{
|
|
|
|
return this->container == rhs.container;
|
|
|
|
}
|
2023-03-31 12:31:48 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
// 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
|
2011-07-05 09:14:07 +03:00
|
|
|
// 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;
|
|
|
|
// }
|
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & container;
|
|
|
|
}
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
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;
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
DLL_LINKAGE std::string toString() const;
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
//special iterator of iterating over non-zero resources in set
|
|
|
|
class DLL_LINKAGE nziterator
|
|
|
|
{
|
|
|
|
struct ResEntry
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
GameResID resType;
|
|
|
|
TResourceCap resVal;
|
|
|
|
} cur;
|
|
|
|
const ResourceSet &rs;
|
|
|
|
void advance();
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
public:
|
|
|
|
nziterator(const ResourceSet &RS);
|
|
|
|
bool valid() const;
|
|
|
|
nziterator operator++();
|
|
|
|
nziterator operator++(int);
|
|
|
|
const ResEntry& operator*() const;
|
|
|
|
const ResEntry* operator->() const;
|
|
|
|
};
|
2014-02-05 23:25:36 +03:00
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
};
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2023-04-05 02:26:29 +02:00
|
|
|
using TResources = ResourceSet;
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|