JsonRandom.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. *
  3. * JsonRandom.cpp, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. #include "StdInc.h"
  12. #include "JsonRandom.h"
  13. #include "../JsonNode.h"
  14. #include "../CRandomGenerator.h"
  15. #include "../StringConstants.h"
  16. #include "../VCMI_Lib.h"
  17. #include "../CModHandler.h"
  18. #include "../CArtHandler.h"
  19. #include "../CCreatureHandler.h"
  20. #include "../CCreatureSet.h"
  21. #include "../spells/CSpellHandler.h"
  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.getType() == JsonNode::DATA_FLOAT)
  29. return value.Float();
  30. if (!value["amount"].isNull())
  31. return value["amount"].Float();
  32. si32 min = value["min"].Float();
  33. si32 max = 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::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::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::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 = value["minValue"].Float();
  83. if (!value["maxValue"].isNull()) maxValue = value["maxValue"].Float();
  84. return VLC->arth->pickRandomArtifact(rng, [=](ArtifactID artID) -> bool
  85. {
  86. CArtifact * art = VLC->arth->artifacts[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::DATA_STRING)
  115. return SpellID(VLC->modh->identifiers.getIdentifier("spell", value).get());
  116. if (value["type"].getType() == JsonNode::DATA_STRING)
  117. return SpellID(VLC->modh->identifiers.getIdentifier("spell", value["type"]).get());
  118. vstd::erase_if(spells, [=](SpellID spell)
  119. {
  120. return VLC->spellh->objects[spell]->level != si32(value["level"].Float());
  121. });
  122. return SpellID(*RandomGeneratorUtil::nextItem(spells, rng));
  123. }
  124. std::vector<SpellID> loadSpells(const JsonNode & value, CRandomGenerator & rng, std::vector<SpellID> spells)
  125. {
  126. // possible extensions: (taken from spell json config)
  127. // "type": "adventure",//"adventure", "combat", "ability"
  128. // "school": {"air":true, "earth":true, "fire":true, "water":true},
  129. // "level": 1,
  130. std::vector<SpellID> ret;
  131. for (const JsonNode & entry : value.Vector())
  132. {
  133. ret.push_back(loadSpell(entry, rng, spells));
  134. }
  135. return ret;
  136. }
  137. CStackBasicDescriptor loadCreature(const JsonNode & value, CRandomGenerator & rng)
  138. {
  139. CStackBasicDescriptor stack;
  140. stack.type = VLC->creh->creatures[VLC->modh->identifiers.getIdentifier("creature", value["type"]).get()];
  141. stack.count = loadValue(value, rng);
  142. if (!value["upgradeChance"].isNull() && !stack.type->upgrades.empty())
  143. {
  144. if (int(value["upgradeChance"].Float()) > rng.nextInt(99)) // select random upgrade
  145. {
  146. stack.type = VLC->creh->creatures[*RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)];
  147. }
  148. }
  149. return stack;
  150. }
  151. std::vector<CStackBasicDescriptor> loadCreatures(const JsonNode & value, CRandomGenerator & rng)
  152. {
  153. std::vector<CStackBasicDescriptor> ret;
  154. for (const JsonNode & node : value.Vector())
  155. {
  156. ret.push_back(loadCreature(node, rng));
  157. }
  158. return ret;
  159. }
  160. std::vector<RandomStackInfo> evaluateCreatures(const JsonNode & value)
  161. {
  162. std::vector<RandomStackInfo> ret;
  163. for (const JsonNode & node : value.Vector())
  164. {
  165. RandomStackInfo info;
  166. if (!node["amount"].isNull())
  167. info.minAmount = info.maxAmount = node["amount"].Float();
  168. else
  169. {
  170. info.minAmount = node["min"].Float();
  171. info.maxAmount = node["max"].Float();
  172. }
  173. const CCreature * crea = VLC->creh->creatures[VLC->modh->identifiers.getIdentifier("creature", node["type"]).get()];
  174. info.allowedCreatures.push_back(crea);
  175. if (node["upgradeChance"].Float() > 0)
  176. {
  177. for (auto creaID : crea->upgrades)
  178. info.allowedCreatures.push_back(VLC->creh->creatures[creaID]);
  179. }
  180. ret.push_back(info);
  181. }
  182. return ret;
  183. }
  184. //std::vector<Component> loadComponents(const JsonNode & value)
  185. //{
  186. // std::vector<Component> ret;
  187. // return ret;
  188. // //TODO
  189. //}
  190. std::vector<Bonus> DLL_LINKAGE loadBonuses(const JsonNode & value)
  191. {
  192. std::vector<Bonus> ret;
  193. for (const JsonNode & entry : value.Vector())
  194. {
  195. auto bonus = JsonUtils::parseBonus(entry);
  196. ret.push_back(*bonus);
  197. }
  198. return ret;
  199. }
  200. }