JsonRandom.cpp 9.3 KB

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