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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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"
|
2012-09-05 15:49:23 +03:00
|
|
|
|
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)
|
2012-09-05 15:49:23 +03:00
|
|
|
push_back(node[name].Float());
|
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)
|
|
|
|
::vstd::amax(elem, val);
|
2011-07-05 09:14:07 +03:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:03:01 +03:00
|
|
|
void Res::ResourceSet::positive()
|
|
|
|
{
|
|
|
|
for(auto & elem : *this)
|
|
|
|
::vstd::amax(elem, 0);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|