JsonRandom.cpp 9.2 KB

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