1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/lib/ResourceSet.cpp

170 lines
4.1 KiB
C++
Raw Normal View History

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
*
*/
#include "StdInc.h"
2023-03-17 01:19:04 +02:00
#include "GameConstants.h"
#include "ResourceSet.h"
#include "StringConstants.h"
#include "JsonNode.h"
#include "serializer/JsonSerializeFormat.h"
#include "VCMI_Lib.h"
#include "mapObjects/CObjectHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
2023-04-05 02:26:29 +02:00
ResourceSet::ResourceSet(const JsonNode & node)
{
2023-03-17 01:19:04 +02:00
for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
2023-04-02 22:27:15 +02:00
container[i] = static_cast<int>(node[GameConstants::RESOURCE_NAMES[i]].Float());
}
2023-04-05 02:26:29 +02:00
ResourceSet::ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal,
2018-07-26 12:06:55 +02:00
TResource gems, TResource gold, TResource mithril)
{
2023-04-05 02:26:29 +02:00
container[GameResID(EGameResID::WOOD)] = wood;
container[GameResID(EGameResID::MERCURY)] = mercury;
container[GameResID(EGameResID::ORE)] = ore;
container[GameResID(EGameResID::SULFUR)] = sulfur;
container[GameResID(EGameResID::CRYSTAL)] = crystal;
container[GameResID(EGameResID::GEMS)] = gems;
container[GameResID(EGameResID::GOLD)] = gold;
container[GameResID(EGameResID::MITHRIL)] = mithril;
2018-07-26 12:06:55 +02:00
}
2023-04-05 02:26:29 +02:00
void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
{
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);
}
2023-04-05 02:26:29 +02:00
bool ResourceSet::nonZero() const
{
2023-03-13 23:26:44 +02:00
for(const auto & elem : *this)
if(elem)
return true;
return false;
}
2023-04-05 02:26:29 +02:00
void ResourceSet::amax(const TResourceCap &val)
{
for(auto & elem : *this)
vstd::amax(elem, val);
}
2023-04-05 02:26:29 +02:00
void ResourceSet::amin(const TResourceCap &val)
{
for(auto & elem : *this)
vstd::amin(elem, val);
}
2023-04-05 02:26:29 +02:00
void ResourceSet::positive()
{
for(auto & elem : *this)
vstd::amax(elem, 0);
}
2023-04-05 02:26:29 +02:00
static bool 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;
}
2023-04-05 02:26:29 +02:00
bool ResourceSet::canBeAfforded(const ResourceSet &res) const
{
return VCMI_LIB_WRAP_NAMESPACE(canAfford(res, *this));
}
bool ResourceSet::canAfford(const ResourceSet &price) const
{
return VCMI_LIB_WRAP_NAMESPACE(canAfford(*this, price));
}
TResourceCap 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;
}
2023-04-05 02:26:29 +02:00
std::string 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();
}
2023-04-05 02:26:29 +02:00
bool ResourceSet::nziterator::valid() const
{
return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
}
2023-04-05 02:26:29 +02:00
ResourceSet::nziterator ResourceSet::nziterator::operator++()
{
advance();
return *this;
}
2023-04-05 02:26:29 +02:00
ResourceSet::nziterator ResourceSet::nziterator::operator++(int)
{
nziterator ret = *this;
advance();
return ret;
}
2023-04-05 02:26:29 +02:00
const ResourceSet::nziterator::ResEntry& ResourceSet::nziterator::operator*() const
{
return cur;
}
2023-04-05 02:26:29 +02:00
const ResourceSet::nziterator::ResEntry * ResourceSet::nziterator::operator->() const
{
return &cur;
}
2023-04-05 02:26:29 +02:00
void 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;
}
2023-04-05 02:26:29 +02:00
ResourceSet::nziterator::nziterator(const ResourceSet &RS)
: rs(RS)
{
2023-04-05 02:26:29 +02:00
cur.resType = EGameResID::WOOD;
cur.resVal = rs[EGameResID::WOOD];
if(!valid())
advance();
2016-02-09 12:08:20 +02:00
}
VCMI_LIB_NAMESPACE_END