2
0

JsonRandom.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <vstd/StringUtils.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. #include "CSkillHandler.h"
  23. #include "IGameCallback.h"
  24. #include "mapObjects/IObjectInterface.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. namespace JsonRandom
  27. {
  28. si32 loadValue(const JsonNode & value, CRandomGenerator & rng, si32 defaultValue)
  29. {
  30. if(value.isNull())
  31. return defaultValue;
  32. if(value.isNumber())
  33. return static_cast<si32>(value.Float());
  34. if(value.isVector())
  35. {
  36. const auto & vector = value.Vector();
  37. size_t index= rng.getIntRange(0, vector.size()-1)();
  38. return loadValue(vector[index], rng, 0);
  39. }
  40. if(value.isStruct())
  41. {
  42. if (!value["amount"].isNull())
  43. return static_cast<si32>(loadValue(value["amount"], rng, defaultValue));
  44. si32 min = static_cast<si32>(loadValue(value["min"], rng, 0));
  45. si32 max = static_cast<si32>(loadValue(value["max"], rng, 0));
  46. return rng.getIntRange(min, max)();
  47. }
  48. return defaultValue;
  49. }
  50. std::string loadKey(const JsonNode & value, CRandomGenerator & rng, const std::set<std::string> & valuesSet)
  51. {
  52. if(value.isString())
  53. return value.String();
  54. if(value.isStruct())
  55. {
  56. if(!value["type"].isNull())
  57. return value["type"].String();
  58. if(!value["anyOf"].isNull())
  59. return RandomGeneratorUtil::nextItem(value["anyOf"].Vector(), rng)->String();
  60. if(!value["noneOf"].isNull())
  61. {
  62. auto copyValuesSet = valuesSet;
  63. for(auto & s : value["noneOf"].Vector())
  64. copyValuesSet.erase(s.String());
  65. if(!copyValuesSet.empty())
  66. return *RandomGeneratorUtil::nextItem(copyValuesSet, rng);
  67. }
  68. }
  69. return valuesSet.empty() ? "" : *RandomGeneratorUtil::nextItem(valuesSet, rng);
  70. }
  71. TResources loadResources(const JsonNode & value, CRandomGenerator & rng)
  72. {
  73. TResources ret;
  74. if (value.isVector())
  75. {
  76. for (const auto & entry : value.Vector())
  77. ret += loadResource(entry, rng);
  78. return ret;
  79. }
  80. for (size_t i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  81. {
  82. ret[i] = loadValue(value[GameConstants::RESOURCE_NAMES[i]], rng);
  83. }
  84. return ret;
  85. }
  86. TResources loadResource(const JsonNode & value, CRandomGenerator & rng)
  87. {
  88. std::set<std::string> defaultResources(std::begin(GameConstants::RESOURCE_NAMES), std::end(GameConstants::RESOURCE_NAMES) - 1); //except mithril
  89. std::string resourceName = loadKey(value, rng, defaultResources);
  90. si32 resourceAmount = loadValue(value, rng, 0);
  91. si32 resourceID(VLC->modh->identifiers.getIdentifier(value.meta, "resource", resourceName).value());
  92. TResources ret;
  93. ret[resourceID] = resourceAmount;
  94. return ret;
  95. }
  96. std::vector<si32> loadPrimary(const JsonNode & value, CRandomGenerator & rng)
  97. {
  98. std::vector<si32> ret;
  99. if(value.isStruct())
  100. {
  101. for(const auto & name : PrimarySkill::names)
  102. {
  103. ret.push_back(loadValue(value[name], rng));
  104. }
  105. }
  106. if(value.isVector())
  107. {
  108. ret.resize(GameConstants::PRIMARY_SKILLS, 0);
  109. std::set<std::string> defaultStats(std::begin(PrimarySkill::names), std::end(PrimarySkill::names));
  110. for(const auto & element : value.Vector())
  111. {
  112. auto key = loadKey(element, rng, defaultStats);
  113. defaultStats.erase(key);
  114. int id = vstd::find_pos(PrimarySkill::names, key);
  115. if(id != -1)
  116. ret[id] += loadValue(element, rng);
  117. }
  118. }
  119. return ret;
  120. }
  121. std::map<SecondarySkill, si32> loadSecondary(const JsonNode & value, CRandomGenerator & rng)
  122. {
  123. std::map<SecondarySkill, si32> ret;
  124. if(value.isStruct())
  125. {
  126. for(const auto & pair : value.Struct())
  127. {
  128. SecondarySkill id(VLC->modh->identifiers.getIdentifier(pair.second.meta, "skill", pair.first).value());
  129. ret[id] = loadValue(pair.second, rng);
  130. }
  131. }
  132. if(value.isVector())
  133. {
  134. std::set<std::string> defaultSkills;
  135. for(const auto & skill : VLC->skillh->objects)
  136. {
  137. IObjectInterface::cb->isAllowed(2, skill->getIndex());
  138. auto scopeAndName = vstd::splitStringToPair(skill->getJsonKey(), ':');
  139. if(scopeAndName.first == CModHandler::scopeBuiltin() || scopeAndName.first == value.meta)
  140. defaultSkills.insert(scopeAndName.second);
  141. else
  142. defaultSkills.insert(skill->getJsonKey());
  143. }
  144. for(const auto & element : value.Vector())
  145. {
  146. auto key = loadKey(element, rng, defaultSkills);
  147. defaultSkills.erase(key); //avoid dupicates
  148. if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "skill", key))
  149. {
  150. SecondarySkill id(identifier.value());
  151. ret[id] = loadValue(element, rng);
  152. }
  153. }
  154. }
  155. return ret;
  156. }
  157. ArtifactID loadArtifact(const JsonNode & value, CRandomGenerator & rng)
  158. {
  159. if (value.getType() == JsonNode::JsonType::DATA_STRING)
  160. return ArtifactID(VLC->modh->identifiers.getIdentifier("artifact", value).value());
  161. std::set<CArtifact::EartClass> allowedClasses;
  162. std::set<ArtifactPosition> allowedPositions;
  163. ui32 minValue = 0;
  164. ui32 maxValue = std::numeric_limits<ui32>::max();
  165. if (value["class"].getType() == JsonNode::JsonType::DATA_STRING)
  166. allowedClasses.insert(CArtHandler::stringToClass(value["class"].String()));
  167. else
  168. for(const auto & entry : value["class"].Vector())
  169. allowedClasses.insert(CArtHandler::stringToClass(entry.String()));
  170. if (value["slot"].getType() == JsonNode::JsonType::DATA_STRING)
  171. allowedPositions.insert(ArtifactPosition(value["class"].String()));
  172. else
  173. for(const auto & entry : value["slot"].Vector())
  174. allowedPositions.insert(ArtifactPosition(entry.String()));
  175. if (!value["minValue"].isNull()) minValue = static_cast<ui32>(value["minValue"].Float());
  176. if (!value["maxValue"].isNull()) maxValue = static_cast<ui32>(value["maxValue"].Float());
  177. return VLC->arth->pickRandomArtifact(rng, [=](const ArtifactID & artID) -> bool
  178. {
  179. CArtifact * art = VLC->arth->objects[artID];
  180. if(!vstd::iswithin(art->getPrice(), minValue, maxValue))
  181. return false;
  182. if(!allowedClasses.empty() && !allowedClasses.count(art->aClass))
  183. return false;
  184. if(!IObjectInterface::cb->isAllowed(1, art->getIndex()))
  185. return false;
  186. if(!allowedPositions.empty())
  187. {
  188. for(const auto & pos : art->getPossibleSlots().at(ArtBearer::HERO))
  189. {
  190. if(allowedPositions.count(pos))
  191. return true;
  192. }
  193. return false;
  194. }
  195. return true;
  196. });
  197. }
  198. std::vector<ArtifactID> loadArtifacts(const JsonNode & value, CRandomGenerator & rng)
  199. {
  200. std::vector<ArtifactID> ret;
  201. for (const JsonNode & entry : value.Vector())
  202. {
  203. ret.push_back(loadArtifact(entry, rng));
  204. }
  205. return ret;
  206. }
  207. SpellID loadSpell(const JsonNode & value, CRandomGenerator & rng, std::vector<SpellID> spells)
  208. {
  209. if (value.getType() == JsonNode::JsonType::DATA_STRING)
  210. return SpellID(VLC->modh->identifiers.getIdentifier("spell", value).value());
  211. if (!value["level"].isNull())
  212. {
  213. int32_t spellLevel = value["level"].Float();
  214. vstd::erase_if(spells, [=](const SpellID & spell)
  215. {
  216. return VLC->spellh->getById(spell)->getLevel() != spellLevel;
  217. });
  218. }
  219. if (!value["school"].isNull())
  220. {
  221. int32_t schoolID = VLC->modh->identifiers.getIdentifier("spellSchool", value["school"]).value();
  222. vstd::erase_if(spells, [=](const SpellID & spell)
  223. {
  224. return !VLC->spellh->getById(spell)->hasSchool(ESpellSchool(schoolID));
  225. });
  226. }
  227. if (spells.empty())
  228. {
  229. logMod->warn("Failed to select suitable random spell!");
  230. return SpellID::NONE;
  231. }
  232. return SpellID(*RandomGeneratorUtil::nextItem(spells, rng));
  233. }
  234. std::vector<SpellID> loadSpells(const JsonNode & value, CRandomGenerator & rng, const std::vector<SpellID> & spells)
  235. {
  236. std::vector<SpellID> ret;
  237. for (const JsonNode & entry : value.Vector())
  238. {
  239. ret.push_back(loadSpell(entry, rng, spells));
  240. }
  241. return ret;
  242. }
  243. CStackBasicDescriptor loadCreature(const JsonNode & value, CRandomGenerator & rng)
  244. {
  245. CStackBasicDescriptor stack;
  246. stack.type = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", value["type"]).value()];
  247. stack.count = loadValue(value, rng);
  248. if (!value["upgradeChance"].isNull() && !stack.type->upgrades.empty())
  249. {
  250. if (int(value["upgradeChance"].Float()) > rng.nextInt(99)) // select random upgrade
  251. {
  252. stack.type = VLC->creh->objects[*RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)];
  253. }
  254. }
  255. return stack;
  256. }
  257. std::vector<CStackBasicDescriptor> loadCreatures(const JsonNode & value, CRandomGenerator & rng)
  258. {
  259. std::vector<CStackBasicDescriptor> ret;
  260. for (const JsonNode & node : value.Vector())
  261. {
  262. ret.push_back(loadCreature(node, rng));
  263. }
  264. return ret;
  265. }
  266. std::vector<RandomStackInfo> evaluateCreatures(const JsonNode & value)
  267. {
  268. std::vector<RandomStackInfo> ret;
  269. for (const JsonNode & node : value.Vector())
  270. {
  271. RandomStackInfo info;
  272. if (!node["amount"].isNull())
  273. info.minAmount = info.maxAmount = static_cast<si32>(node["amount"].Float());
  274. else
  275. {
  276. info.minAmount = static_cast<si32>(node["min"].Float());
  277. info.maxAmount = static_cast<si32>(node["max"].Float());
  278. }
  279. const CCreature * crea = VLC->creh->objects[VLC->modh->identifiers.getIdentifier("creature", node["type"]).value()];
  280. info.allowedCreatures.push_back(crea);
  281. if (node["upgradeChance"].Float() > 0)
  282. {
  283. for(const auto & creaID : crea->upgrades)
  284. info.allowedCreatures.push_back(VLC->creh->objects[creaID]);
  285. }
  286. ret.push_back(info);
  287. }
  288. return ret;
  289. }
  290. //std::vector<Component> loadComponents(const JsonNode & value)
  291. //{
  292. // std::vector<Component> ret;
  293. // return ret;
  294. // //TODO
  295. //}
  296. std::vector<Bonus> DLL_LINKAGE loadBonuses(const JsonNode & value)
  297. {
  298. std::vector<Bonus> ret;
  299. for (const JsonNode & entry : value.Vector())
  300. {
  301. if(auto bonus = JsonUtils::parseBonus(entry))
  302. ret.push_back(*bonus);
  303. }
  304. return ret;
  305. }
  306. }
  307. VCMI_LIB_NAMESPACE_END