2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* ResourceSet.cpp, 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-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2011-07-05 09:14:07 +03:00
|
|
|
#include "ResourceSet.h"
|
2012-12-14 18:32:53 +03:00
|
|
|
#include "StringConstants.h"
|
2012-09-02 13:33:41 +03:00
|
|
|
#include "JsonNode.h"
|
2016-11-13 12:38:42 +02:00
|
|
|
#include "serializer/JsonSerializeFormat.h"
|
2018-03-17 10:46:16 +02:00
|
|
|
#include "VCMI_Lib.h"
|
|
|
|
#include "mapObjects/CObjectHandler.h"
|
2012-09-05 15:49:23 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
Res::ResourceSet::ResourceSet()
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
resize(GameConstants::RESOURCE_QUANTITY, 0);
|
2012-09-05 15:49:23 +03:00
|
|
|
}
|
2012-09-02 13:33:41 +03:00
|
|
|
|
|
|
|
Res::ResourceSet::ResourceSet(const JsonNode & node)
|
2012-09-05 15:49:23 +03:00
|
|
|
{
|
|
|
|
reserve(GameConstants::RESOURCE_QUANTITY);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(std::string name : GameConstants::RESOURCE_NAMES)
|
2020-10-01 10:38:06 +02:00
|
|
|
push_back((int)node[name].Float());
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2018-07-26 12:06:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
//TODO: add proper support for mithril to map format
|
|
|
|
for(int idx = 0; idx < GameConstants::RESOURCE_QUANTITY - 1; idx ++)
|
|
|
|
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], this->operator[](idx), 0);
|
|
|
|
}
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
bool Res::ResourceSet::nonZero() const
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : *this)
|
|
|
|
if(elem)
|
2011-07-05 09:14:07 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-26 19:48:53 +03:00
|
|
|
void Res::ResourceSet::amax(const TResourceCap &val)
|
2011-07-05 09:14:07 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : *this)
|
2022-07-26 15:07:42 +02:00
|
|
|
vstd::amax(elem, val);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2016-10-11 17:00:54 +02:00
|
|
|
void Res::ResourceSet::amin(const TResourceCap &val)
|
|
|
|
{
|
|
|
|
for(auto & elem : *this)
|
2022-07-26 15:07:42 +02:00
|
|
|
vstd::amin(elem, val);
|
2016-10-11 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:03:01 +03:00
|
|
|
void Res::ResourceSet::positive()
|
|
|
|
{
|
|
|
|
for(auto & elem : *this)
|
2022-07-26 15:07:42 +02:00
|
|
|
vstd::amax(elem, 0);
|
2013-07-23 18:03:01 +03:00
|
|
|
}
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
bool Res::ResourceSet::canBeAfforded(const ResourceSet &res) const
|
|
|
|
{
|
|
|
|
return Res::canAfford(res, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Res::ResourceSet::canAfford(const ResourceSet &price) const
|
|
|
|
{
|
|
|
|
return Res::canAfford(*this, price);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Res::canAfford(const ResourceSet &res, const ResourceSet &price)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
|
|
|
|
for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
|
2011-07-05 09:14:07 +03:00
|
|
|
if(price[i] > res[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-17 10:46:16 +02:00
|
|
|
TResourceCap Res::ResourceSet::marketValue() const
|
|
|
|
{
|
|
|
|
TResourceCap total = 0;
|
|
|
|
for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
|
|
|
|
total += static_cast<TResourceCap>(VLC->objh->resVals[i]) * static_cast<TResourceCap>(operator[](i));
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
std::string Res::ResourceSet::toString() const
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "[";
|
|
|
|
for(auto it = begin(); it != end(); ++it)
|
|
|
|
{
|
|
|
|
out << *it;
|
|
|
|
if(std::prev(end()) != it) out << ", ";
|
|
|
|
}
|
|
|
|
out << "]";
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2011-07-05 09:14:07 +03:00
|
|
|
bool Res::ResourceSet::nziterator::valid()
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++()
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++(int)
|
|
|
|
{
|
|
|
|
nziterator ret = *this;
|
|
|
|
advance();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Res::ResourceSet::nziterator::ResEntry& Res::ResourceSet::nziterator::operator*() const
|
|
|
|
{
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Res::ResourceSet::nziterator::ResEntry * Res::ResourceSet::nziterator::operator->() const
|
|
|
|
{
|
|
|
|
return &cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Res::ResourceSet::nziterator::advance()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-04-20 14:34:01 +03:00
|
|
|
vstd::advance(cur.resType, +1);
|
2011-12-14 00:23:17 +03:00
|
|
|
} while(cur.resType < GameConstants::RESOURCE_QUANTITY && !(cur.resVal=rs[cur.resType]));
|
2011-07-05 09:14:07 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
if(cur.resType >= GameConstants::RESOURCE_QUANTITY)
|
2011-07-05 09:14:07 +03:00
|
|
|
cur.resVal = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Res::ResourceSet::nziterator::nziterator(const ResourceSet &RS)
|
|
|
|
: rs(RS)
|
|
|
|
{
|
2013-04-20 14:34:01 +03:00
|
|
|
cur.resType = WOOD;
|
|
|
|
cur.resVal = rs[WOOD];
|
2011-07-05 09:14:07 +03:00
|
|
|
|
|
|
|
if(!valid())
|
|
|
|
advance();
|
2016-02-09 12:08:20 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|