JsonRandom.cpp 7.5 KB

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