JsonRandom.cpp 6.5 KB

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