2
0

JsonRandom.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * JsonRandom.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonRandom.h"
  12. #include <vstd/StringUtils.h>
  13. #include "../JsonNode.h"
  14. #include "../CRandomGenerator.h"
  15. #include "../StringConstants.h"
  16. #include "../VCMI_Lib.h"
  17. #include "../CModHandler.h"
  18. #include "../CArtHandler.h"
  19. #include "../CCreatureHandler.h"
  20. #include "../CCreatureSet.h"
  21. #include "../spells/CSpellHandler.h"
  22. #include "../CSkillHandler.h"
  23. #include "../mapObjects/CObjectHandler.h"
  24. #include "../IGameCallback.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. namespace JsonRandom
  27. {
  28. si32 loadValue(const JsonNode & value, CRandomGenerator & rng, si32 defaultValue)
  29. {
  30. if (value.isNull())
  31. return defaultValue;
  32. if (value.isNumber())
  33. return static_cast<si32>(value.Float());
  34. if (value.isVector())
  35. {
  36. const auto & vector = value.Vector();
  37. size_t index= rng.getIntRange(0, vector.size()-1)();
  38. return loadValue(vector[index], rng, 0);
  39. }
  40. if (!value["amount"].isNull())
  41. return static_cast<si32>(loadValue(value["amount"], rng, defaultValue));
  42. si32 min = static_cast<si32>(loadValue(value["min"], rng, 0));
  43. si32 max = static_cast<si32>(loadValue(value["max"], rng, 0));
  44. return rng.getIntRange(min, max)();
  45. }
  46. std::string loadKey(const JsonNode & value, CRandomGenerator & rng, const std::set<std::string> & valuesSet)
  47. {
  48. if(value.isString())
  49. return value.String();
  50. if(value.isStruct())
  51. {
  52. if(!value["type"].isNull())
  53. return value["type"].String();
  54. if(!value["anyOf"].isNull())
  55. return RandomGeneratorUtil::nextItem(value["anyOf"].Vector(), rng)->String();
  56. if(!value["noneOf"].isNull())
  57. {
  58. auto copyValuesSet = valuesSet;
  59. for(auto & s : value["noneOf"].Vector())
  60. copyValuesSet.erase(s.String());
  61. if(!copyValuesSet.empty())
  62. return *RandomGeneratorUtil::nextItem(copyValuesSet, rng);
  63. }
  64. }
  65. return valuesSet.empty() ? "" : *RandomGeneratorUtil::nextItem(valuesSet, rng);
  66. }
  67. TResources loadResources(const JsonNode & value, CRandomGenerator & rng)
  68. {
  69. TResources ret;
  70. if (value.isVector())
  71. {
  72. for (const auto & entry : value.Vector())
  73. ret += loadResource(entry, rng);
  74. return ret;
  75. }
  76. for (size_t i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  77. {
  78. ret[i] = loadValue(value[GameConstants::RESOURCE_NAMES[i]], rng);
  79. }
  80. return ret;
  81. }
  82. TResources loadResource(const JsonNode & value, CRandomGenerator & rng)
  83. {
  84. std::set<std::string> defaultResources(std::begin(GameConstants::RESOURCE_NAMES), std::end(GameConstants::RESOURCE_NAMES) - 1); //except mithril
  85. std::string resourceName = loadKey(value, rng, defaultResources);
  86. si32 resourceAmount = loadValue(value, rng, 0);
  87. si32 resourceID(VLC->modh->identifiers.getIdentifier(value.meta, "resource", resourceName).value());
  88. TResources ret;
  89. ret[resourceID] = resourceAmount;
  90. return ret;
  91. }
  92. std::vector<si32> loadPrimary(const JsonNode & value, CRandomGenerator & rng)
  93. {
  94. std::vector<si32> ret;
  95. if(value.isStruct())
  96. {
  97. for(const auto & name : PrimarySkill::names)
  98. {
  99. ret.push_back(loadValue(value[name], rng));
  100. }
  101. }
  102. if(value.isVector())
  103. {
  104. ret.resize(GameConstants::PRIMARY_SKILLS, 0);
  105. std::set<std::string> defaultStats(std::begin(PrimarySkill::names), std::end(PrimarySkill::names));
  106. for(const auto & element : value.Vector())
  107. {
  108. auto key = loadKey(element, rng, defaultStats);
  109. defaultStats.erase(key);
  110. int id = vstd::find_pos(PrimarySkill::names, key);
  111. if(id != -1)
  112. ret[id] += loadValue(element, rng);
  113. }
  114. }
  115. return ret;
  116. }
  117. std::map<SecondarySkill, si32> loadSecondary(const JsonNode & value, CRandomGenerator & rng)
  118. {
  119. std::map<SecondarySkill, si32> ret;
  120. if(value.isStruct())
  121. {
  122. for(const auto & pair : value.Struct())
  123. {
  124. SecondarySkill id(VLC->modh->identifiers.getIdentifier(pair.second.meta, "skill", pair.first).value());
  125. ret[id] = loadValue(pair.second, rng);
  126. }
  127. }
  128. if(value.isVector())
  129. {
  130. std::set<std::string> defaultSkills;
  131. for(const auto & skill : VLC->skillh->objects)
  132. {
  133. IObjectInterface::cb->isAllowed(2, skill->getIndex());
  134. auto scopeAndName = vstd::splitStringToPair(skill->getJsonKey(), ':');
  135. if(scopeAndName.first == CModHandler::scopeBuiltin() || scopeAndName.first == value.meta)
  136. defaultSkills.insert(scopeAndName.second);
  137. else
  138. defaultSkills.insert(skill->getJsonKey());
  139. }
  140. for(const auto & element : value.Vector())
  141. {
  142. auto key = loadKey(element, rng, defaultSkills);
  143. defaultSkills.erase(key); //avoid dupicates
  144. if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "skill", key))
  145. {
  146. SecondarySkill id(identifier.value());
  147. ret[id] = loadValue(element, rng);
  148. }
  149. }
  150. }
  151. return ret;
  152. }
  153. ArtifactID loadArtifact(const JsonNode & value, CRandomGenerator & rng)
  154. {
  155. if (value.getType() == JsonNode::JsonType::DATA_STRING)
  156. return ArtifactID(VLC->modh->identifiers.getIdentifier("artifact", value).value());
  157. std::set<CArtifact::EartClass> allowedClasses;
  158. std::set<ArtifactPosition> allowedPositions;
  159. ui32 minValue = 0;
  160. ui32 maxValue = std::numeric_limits<ui32>::max();
  161. if (value["class"].getType() == JsonNode::JsonType::DATA_STRING)
  162. allowedClasses.insert(CArtHandler::stringToClass(value["class"].String()));
  163. else
  164. for(const auto & entry : value["class"].Vector())
  165. allowedClasses.insert(CArtHandler::stringToClass(entry.String()));
  166. if (value["slot"].getType() == JsonNode::JsonType::DATA_STRING)
  167. allowedPositions.insert(ArtifactPosition(value["class"].String()));
  168. else
  169. for(const auto & entry : value["slot"].Vector())
  170. allowedPositions.insert(ArtifactPosition(entry.String()));
  171. if (!value["minValue"].isNull()) minValue = static_cast<ui32>(value["minValue"].Float());
  172. if (!value["maxValue"].isNull()) maxValue = static_cast<ui32>(value["maxValue"].Float());
  173. return VLC->arth->pickRandomArtifact(rng, [=](const ArtifactID & artID) -> bool
  174. {
  175. CArtifact * art = VLC->arth->objects[artID];
  176. if(!vstd::iswithin(art->price, minValue, maxValue))
  177. return false;
  178. if(!allowedClasses.empty() && !allowedClasses.count(art->aClass))
  179. return false;
  180. if(!IObjectInterface::cb->isAllowed(1, art->getIndex()))
  181. return false;
  182. if(!allowedPositions.empty())
  183. {
  184. for(const auto & pos : art->possibleSlots[ArtBearer::HERO])
  185. {
  186. if(allowedPositions.count(pos))
  187. return true;
  188. }
  189. return false;
  190. }
  191. return true;
  192. });
  193. }
  194. std::vector<ArtifactID> loadArtifacts(const JsonNode & value, CRandomGenerator & rng)
  195. {
  196. std::vector<ArtifactID> ret;
  197. for (const JsonNode & entry : value.Vector())
  198. {
  199. ret.push_back(loadArtifact(entry, rng));
  200. }
  201. return ret;
  202. }
  203. SpellID loadSpell(const JsonNode & value, CRandomGenerator & rng, std::vector<SpellID> spells)
  204. {
  205. if (value.getType() == JsonNode::JsonType::DATA_STRING)
  206. return SpellID(VLC->modh->identifiers.getIdentifier("spell", value).value());
  207. vstd::erase_if(spells, [=](const SpellID & spell)
  208. {
  209. return VLC->spellh->getById(spell)->getLevel() != si32(value["level"].Float());
  210. });
  211. return SpellID(*RandomGeneratorUtil::nextItem(spells, rng));
  212. }
  213. std::vector<SpellID> loadSpells(const JsonNode & value, CRandomGenerator & rng, const std::vector<SpellID> & spells)
  214. {
  215. // possible extensions: (taken from spell json config)
  216. // "type": "adventure",//"adventure", "combat", "ability"
  217. // "school": {"air":true, "earth":true, "fire":true, "water":true},
  218. // "level": 1,
  219. std::vector<SpellID> ret;
  220. for (const JsonNode & entry : value.Vector())
  221. {
  222. ret.push_back(loadSpell(entry, rng, spells));
  223. }
  224. return ret;
  225. }
  226. CStackBasicDescriptor loadCreature(const JsonNode & value, CRandomGenerator & rng)
  227. {
  228. CStackBasicDescriptor stack;
  229. stack.type = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", value["type"]).value()];
  230. stack.count = loadValue(value, rng);
  231. if (!value["upgradeChance"].isNull() && !stack.type->upgrades.empty())
  232. {
  233. if (int(value["upgradeChance"].Float()) > rng.nextInt(99)) // select random upgrade
  234. {
  235. stack.type = VLC->creh->objects[*RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)];
  236. }
  237. }
  238. return stack;
  239. }
  240. std::vector<CStackBasicDescriptor> loadCreatures(const JsonNode & value, CRandomGenerator & rng)
  241. {
  242. std::vector<CStackBasicDescriptor> ret;
  243. for (const JsonNode & node : value.Vector())
  244. {
  245. ret.push_back(loadCreature(node, rng));
  246. }
  247. return ret;
  248. }
  249. std::vector<RandomStackInfo> evaluateCreatures(const JsonNode & value)
  250. {
  251. std::vector<RandomStackInfo> ret;
  252. for (const JsonNode & node : value.Vector())
  253. {
  254. RandomStackInfo info;
  255. if (!node["amount"].isNull())
  256. info.minAmount = info.maxAmount = static_cast<si32>(node["amount"].Float());
  257. else
  258. {
  259. info.minAmount = static_cast<si32>(node["min"].Float());
  260. info.maxAmount = static_cast<si32>(node["max"].Float());
  261. }
  262. const CCreature * crea = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", node["type"]).value()];
  263. info.allowedCreatures.push_back(crea);
  264. if (node["upgradeChance"].Float() > 0)
  265. {
  266. for(const auto & creaID : crea->upgrades)
  267. info.allowedCreatures.push_back(VLC->creh->objects[creaID]);
  268. }
  269. ret.push_back(info);
  270. }
  271. return ret;
  272. }
  273. //std::vector<Component> loadComponents(const JsonNode & value)
  274. //{
  275. // std::vector<Component> ret;
  276. // return ret;
  277. // //TODO
  278. //}
  279. std::vector<Bonus> DLL_LINKAGE loadBonuses(const JsonNode & value)
  280. {
  281. std::vector<Bonus> ret;
  282. for (const JsonNode & entry : value.Vector())
  283. {
  284. if(auto bonus = JsonUtils::parseBonus(entry))
  285. ret.push_back(*bonus);
  286. }
  287. return ret;
  288. }
  289. }
  290. VCMI_LIB_NAMESPACE_END