JsonRandom.cpp 7.7 KB

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