JsonRandom.cpp 6.6 KB

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