JsonRandom.cpp 8.8 KB

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