1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* JsonRandom.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"
#include "JsonRandom.h"
#include "../JsonNode.h"
#include "../CRandomGenerator.h"
#include "../StringConstants.h"
#include "../VCMI_Lib.h"
#include "../CModHandler.h"
#include "../CArtHandler.h"
#include "../CCreatureHandler.h"
#include "../CCreatureSet.h"
#include "../spells/CSpellHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
namespace JsonRandom
{
si32 loadValue(const JsonNode & value, CRandomGenerator & rng, si32 defaultValue)
{
if (value.isNull())
return defaultValue;
if (value.isNumber())
return static_cast<si32>(value.Float());
if (!value["amount"].isNull())
return static_cast<si32>(value["amount"].Float());
si32 min = static_cast<si32>(value["min"].Float());
si32 max = static_cast<si32>(value["max"].Float());
return rng.getIntRange(min, max)();
}
TResources loadResources(const JsonNode & value, CRandomGenerator & rng)
{
TResources ret;
for (size_t i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
{
ret[i] = loadValue(value[GameConstants::RESOURCE_NAMES[i]], rng);
}
return ret;
}
std::vector<si32> loadPrimary(const JsonNode & value, CRandomGenerator & rng)
{
std::vector<si32> ret;
for (auto & name : PrimarySkill::names)
{
ret.push_back(loadValue(value[name], rng));
}
return ret;
}
std::map<SecondarySkill, si32> loadSecondary(const JsonNode & value, CRandomGenerator & rng)
{
std::map<SecondarySkill, si32> ret;
for (auto & pair : value.Struct())
{
SecondarySkill id(VLC->modh->identifiers.getIdentifier(pair.second.meta, "skill", pair.first).get());
ret[id] = loadValue(pair.second, rng);
}
return ret;
}
ArtifactID loadArtifact(const JsonNode & value, CRandomGenerator & rng)
{
if (value.getType() == JsonNode::JsonType::DATA_STRING)
return ArtifactID(VLC->modh->identifiers.getIdentifier("artifact", value).get());
std::set<CArtifact::EartClass> allowedClasses;
std::set<ArtifactPosition> allowedPositions;
ui32 minValue = 0;
ui32 maxValue = std::numeric_limits<ui32>::max();
if (value["class"].getType() == JsonNode::JsonType::DATA_STRING)
allowedClasses.insert(VLC->arth->stringToClass(value["class"].String()));
else
for (auto & entry : value["class"].Vector())
allowedClasses.insert(VLC->arth->stringToClass(entry.String()));
if (value["slot"].getType() == JsonNode::JsonType::DATA_STRING)
allowedPositions.insert(VLC->arth->stringToSlot(value["class"].String()));
else
for (auto & entry : value["slot"].Vector())
allowedPositions.insert(VLC->arth->stringToSlot(entry.String()));
if (!value["minValue"].isNull()) minValue = static_cast<ui32>(value["minValue"].Float());
if (!value["maxValue"].isNull()) maxValue = static_cast<ui32>(value["maxValue"].Float());
return VLC->arth->pickRandomArtifact(rng, [=](ArtifactID artID) -> bool
{
CArtifact * art = VLC->arth->objects[artID];
if (!vstd::iswithin(art->price, minValue, maxValue))
return false;
if (!allowedClasses.empty() && !allowedClasses.count(art->aClass))
return false;
if (!allowedPositions.empty())
{
for (auto pos : art->possibleSlots[ArtBearer::HERO])
{
if (allowedPositions.count(pos))
return true;
}
return false;
}
return true;
});
}
std::vector<ArtifactID> loadArtifacts(const JsonNode & value, CRandomGenerator & rng)
{
std::vector<ArtifactID> ret;
for (const JsonNode & entry : value.Vector())
{
ret.push_back(loadArtifact(entry, rng));
}
return ret;
}
SpellID loadSpell(const JsonNode & value, CRandomGenerator & rng, std::vector<SpellID> spells)
{
if (value.getType() == JsonNode::JsonType::DATA_STRING)
return SpellID(VLC->modh->identifiers.getIdentifier("spell", value).get());
if (value["type"].getType() == JsonNode::JsonType::DATA_STRING)
return SpellID(VLC->modh->identifiers.getIdentifier("spell", value["type"]).get());
vstd::erase_if(spells, [=](SpellID spell)
{
return VLC->spellh->objects[spell]->level != si32(value["level"].Float());
});
return SpellID(*RandomGeneratorUtil::nextItem(spells, rng));
}
std::vector<SpellID> loadSpells(const JsonNode & value, CRandomGenerator & rng, std::vector<SpellID> spells)
{
// possible extensions: (taken from spell json config)
// "type": "adventure",//"adventure", "combat", "ability"
// "school": {"air":true, "earth":true, "fire":true, "water":true},
// "level": 1,
std::vector<SpellID> ret;
for (const JsonNode & entry : value.Vector())
{
ret.push_back(loadSpell(entry, rng, spells));
}
return ret;
}
CStackBasicDescriptor loadCreature(const JsonNode & value, CRandomGenerator & rng)
{
CStackBasicDescriptor stack;
stack.type = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", value["type"]).get()];
stack.count = loadValue(value, rng);
if (!value["upgradeChance"].isNull() && !stack.type->upgrades.empty())
{
if (int(value["upgradeChance"].Float()) > rng.nextInt(99)) // select random upgrade
{
stack.type = VLC->creh->objects[*RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)];
}
}
return stack;
}
std::vector<CStackBasicDescriptor> loadCreatures(const JsonNode & value, CRandomGenerator & rng)
{
std::vector<CStackBasicDescriptor> ret;
for (const JsonNode & node : value.Vector())
{
ret.push_back(loadCreature(node, rng));
}
return ret;
}
std::vector<RandomStackInfo> evaluateCreatures(const JsonNode & value)
{
std::vector<RandomStackInfo> ret;
for (const JsonNode & node : value.Vector())
{
RandomStackInfo info;
if (!node["amount"].isNull())
info.minAmount = info.maxAmount = static_cast<si32>(node["amount"].Float());
else
{
info.minAmount = static_cast<si32>(node["min"].Float());
info.maxAmount = static_cast<si32>(node["max"].Float());
}
const CCreature * crea = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", node["type"]).get()];
info.allowedCreatures.push_back(crea);
if (node["upgradeChance"].Float() > 0)
{
for (auto creaID : crea->upgrades)
info.allowedCreatures.push_back(VLC->creh->objects[creaID]);
}
ret.push_back(info);
}
return ret;
}
//std::vector<Component> loadComponents(const JsonNode & value)
//{
// std::vector<Component> ret;
// return ret;
// //TODO
//}
std::vector<Bonus> DLL_LINKAGE loadBonuses(const JsonNode & value)
{
std::vector<Bonus> ret;
for (const JsonNode & entry : value.Vector())
{
auto bonus = JsonUtils::parseBonus(entry);
ret.push_back(*bonus);
}
return ret;
}
}
VCMI_LIB_NAMESPACE_END
|