1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
vcmi/lib/ResourceSet.cpp
Ivan Savenko 0ca9f64573 Next part of town configuration:
- town screen is mostly implemented, has some minor issues
- factions are now separate from towns, neutrals have faction with id=9
- more constants to GameConstants: town-specific buildings, strings for terrains and resources
- replaced most access to builtBuildings with isBuilt() method
- replaced id's with enums for town subtype and buildings id's
2012-09-05 12:49:23 +00:00

100 lines
2.1 KiB
C++

#include "StdInc.h"
#include "ResourceSet.h"
#include "GameConstants.h"
#include "JsonNode.h"
Res::ResourceSet::ResourceSet()
{
resize(GameConstants::RESOURCE_QUANTITY, 0);
}
Res::ResourceSet::ResourceSet(const JsonNode & node)
{
reserve(GameConstants::RESOURCE_QUANTITY);
BOOST_FOREACH(std::string name, GameConstants::RESOURCE_NAMES)
push_back(node[name].Float());
}
bool Res::ResourceSet::nonZero() const
{
for(int i = 0; i < size(); i++)
if(at(i))
return true;
return false;
}
void Res::ResourceSet::amax(const TResourceCap &val)
{
for(int i = 0; i < size(); i++)
::vstd::amax(at(i), val);
}
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)
{
assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
if(price[i] > res[i])
return false;
return true;
}
bool Res::ResourceSet::nziterator::valid()
{
return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
}
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
{
cur.resType++;
} while(cur.resType < GameConstants::RESOURCE_QUANTITY && !(cur.resVal=rs[cur.resType]));
if(cur.resType >= GameConstants::RESOURCE_QUANTITY)
cur.resVal = -1;
}
Res::ResourceSet::nziterator::nziterator(const ResourceSet &RS)
: rs(RS)
{
cur.resType = 0;
cur.resVal = rs[0];
if(!valid())
advance();
}