2
0

JsonRandom.cpp 8.6 KB

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