Info.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Info.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 "Info.h"
  12. #include "Configuration.h"
  13. #include "Limiter.h"
  14. #include "Reward.h"
  15. #include "../CGeneralTextHandler.h"
  16. #include "../IGameCallback.h"
  17. #include "../JsonRandom.h"
  18. #include "../mapObjects/IObjectInterface.h"
  19. #include "../modding/IdentifierStorage.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. namespace {
  22. MetaString loadMessage(const JsonNode & value, const TextIdentifier & textIdentifier, EMetaText textSource = EMetaText::ADVOB_TXT )
  23. {
  24. MetaString ret;
  25. if (value.isVector())
  26. {
  27. for(const auto & entry : value.Vector())
  28. {
  29. if (entry.isNumber())
  30. ret.appendLocalString(textSource, static_cast<ui32>(entry.Float()));
  31. if (entry.isString())
  32. ret.appendRawString(entry.String());
  33. }
  34. return ret;
  35. }
  36. if (value.isNumber())
  37. {
  38. ret.appendLocalString(textSource, static_cast<ui32>(value.Float()));
  39. return ret;
  40. }
  41. if (value.String().empty())
  42. return ret;
  43. ret.appendTextID(textIdentifier.get());
  44. return ret;
  45. }
  46. bool testForKey(const JsonNode & value, const std::string & key)
  47. {
  48. for(const auto & reward : value["rewards"].Vector())
  49. {
  50. if (!reward[key].isNull())
  51. return true;
  52. }
  53. return false;
  54. }
  55. }
  56. void Rewardable::Info::init(const JsonNode & objectConfig, const std::string & objectName)
  57. {
  58. objectTextID = objectName;
  59. auto loadString = [&](const JsonNode & entry, const TextIdentifier & textID){
  60. if (entry.isString() && !entry.String().empty())
  61. VLC->generaltexth->registerString(entry.meta, textID, entry.String());
  62. };
  63. parameters = objectConfig;
  64. for(size_t i = 0; i < parameters["rewards"].Vector().size(); ++i)
  65. {
  66. const JsonNode message = parameters["rewards"][i]["message"];
  67. loadString(message, TextIdentifier(objectName, "rewards", i));
  68. }
  69. for(size_t i = 0; i < parameters["onVisited"].Vector().size(); ++i)
  70. {
  71. const JsonNode message = parameters["onVisited"][i]["message"];
  72. loadString(message, TextIdentifier(objectName, "onVisited", i));
  73. }
  74. for(size_t i = 0; i < parameters["onEmpty"].Vector().size(); ++i)
  75. {
  76. const JsonNode message = parameters["onEmpty"][i]["message"];
  77. loadString(message, TextIdentifier(objectName, "onEmpty", i));
  78. }
  79. loadString(parameters["onSelectMessage"], TextIdentifier(objectName, "onSelect"));
  80. loadString(parameters["description"], TextIdentifier(objectName, "description"));
  81. loadString(parameters["notVisitedTooltip"], TextIdentifier(objectName, "notVisitedText"));
  82. loadString(parameters["visitedTooltip"], TextIdentifier(objectName, "visitedTooltip"));
  83. loadString(parameters["onVisitedMessage"], TextIdentifier(objectName, "onVisited"));
  84. loadString(parameters["onEmptyMessage"], TextIdentifier(objectName, "onEmpty"));
  85. }
  86. Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
  87. {
  88. Rewardable::LimitersList result;
  89. for (const auto & input : source.Vector())
  90. {
  91. auto newLimiter = std::make_shared<Rewardable::Limiter>();
  92. configureLimiter(object, rng, *newLimiter, input);
  93. result.push_back(newLimiter);
  94. }
  95. return result;
  96. }
  97. void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const
  98. {
  99. auto const & variables = object.variables.values;
  100. limiter.dayOfWeek = JsonRandom::loadValue(source["dayOfWeek"], rng, variables);
  101. limiter.daysPassed = JsonRandom::loadValue(source["daysPassed"], rng, variables);
  102. limiter.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng, variables);
  103. limiter.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng, variables);
  104. limiter.canLearnSkills = source["canLearnSkills"].Bool();
  105. limiter.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng, variables);
  106. limiter.manaPoints = JsonRandom::loadValue(source["manaPoints"], rng, variables);
  107. limiter.resources = JsonRandom::loadResources(source["resources"], rng, variables);
  108. limiter.primary = JsonRandom::loadPrimaries(source["primary"], rng, variables);
  109. limiter.secondary = JsonRandom::loadSecondaries(source["secondary"], rng, variables);
  110. limiter.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng, variables);
  111. limiter.spells = JsonRandom::loadSpells(source["spells"], rng, variables);
  112. limiter.canLearnSpells = JsonRandom::loadSpells(source["canLearnSpells"], rng, variables);
  113. limiter.creatures = JsonRandom::loadCreatures(source["creatures"], rng, variables);
  114. limiter.players = JsonRandom::loadColors(source["colors"], rng, variables);
  115. limiter.heroes = JsonRandom::loadHeroes(source["heroes"], rng);
  116. limiter.heroClasses = JsonRandom::loadHeroClasses(source["heroClasses"], rng);
  117. limiter.allOf = configureSublimiters(object, rng, source["allOf"] );
  118. limiter.anyOf = configureSublimiters(object, rng, source["anyOf"] );
  119. limiter.noneOf = configureSublimiters(object, rng, source["noneOf"] );
  120. }
  121. void Rewardable::Info::configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Reward & reward, const JsonNode & source) const
  122. {
  123. auto const & variables = object.variables.values;
  124. reward.resources = JsonRandom::loadResources(source["resources"], rng, variables);
  125. reward.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng, variables);
  126. reward.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng, variables);
  127. reward.manaDiff = JsonRandom::loadValue(source["manaPoints"], rng, variables);
  128. reward.manaOverflowFactor = JsonRandom::loadValue(source["manaOverflowFactor"], rng, variables);
  129. reward.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng, variables, -1);
  130. reward.movePoints = JsonRandom::loadValue(source["movePoints"], rng, variables);
  131. reward.movePercentage = JsonRandom::loadValue(source["movePercentage"], rng, variables, -1);
  132. reward.removeObject = source["removeObject"].Bool();
  133. reward.bonuses = JsonRandom::loadBonuses(source["bonuses"]);
  134. reward.primary = JsonRandom::loadPrimaries(source["primary"], rng, variables);
  135. reward.secondary = JsonRandom::loadSecondaries(source["secondary"], rng, variables);
  136. reward.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng, variables);
  137. reward.spells = JsonRandom::loadSpells(source["spells"], rng, variables);
  138. reward.creatures = JsonRandom::loadCreatures(source["creatures"], rng, variables);
  139. if(!source["spellCast"].isNull() && source["spellCast"].isStruct())
  140. {
  141. reward.spellCast.first = JsonRandom::loadSpell(source["spellCast"]["spell"], rng, variables);
  142. reward.spellCast.second = source["spellCast"]["schoolLevel"].Integer();
  143. }
  144. if (!source["revealTiles"].isNull())
  145. {
  146. auto const & entry = source["revealTiles"];
  147. reward.revealTiles = RewardRevealTiles();
  148. reward.revealTiles->radius = JsonRandom::loadValue(entry["radius"], rng, variables);
  149. reward.revealTiles->hide = entry["hide"].Bool();
  150. reward.revealTiles->scoreSurface = JsonRandom::loadValue(entry["surface"], rng, variables);
  151. reward.revealTiles->scoreSubterra = JsonRandom::loadValue(entry["subterra"], rng, variables);
  152. reward.revealTiles->scoreWater = JsonRandom::loadValue(entry["water"], rng, variables);
  153. reward.revealTiles->scoreRock = JsonRandom::loadValue(entry["rock"], rng, variables);
  154. }
  155. for ( auto node : source["changeCreatures"].Struct() )
  156. {
  157. CreatureID from(VLC->identifiers()->getIdentifier(node.second.meta, "creature", node.first).value());
  158. CreatureID dest(VLC->identifiers()->getIdentifier(node.second.meta, "creature", node.second.String()).value());
  159. reward.extraComponents.emplace_back(Component::EComponentType::CREATURE, dest.getNum(), 0, 0);
  160. reward.creaturesChange[from] = dest;
  161. }
  162. }
  163. void Rewardable::Info::configureResetInfo(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::ResetInfo & resetParameters, const JsonNode & source) const
  164. {
  165. resetParameters.period = static_cast<ui32>(source["period"].Float());
  166. resetParameters.visitors = source["visitors"].Bool();
  167. resetParameters.rewards = source["rewards"].Bool();
  168. }
  169. void Rewardable::Info::configureVariables(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
  170. {
  171. for(const auto & category : source.Struct())
  172. {
  173. for(const auto & entry : category.second.Struct())
  174. {
  175. JsonNode preset = object.getPresetVariable(category.first, entry.first);
  176. const JsonNode & input = preset.isNull() ? entry.second : preset;
  177. int32_t value = -1;
  178. if (category.first == "number")
  179. value = JsonRandom::loadValue(input, rng, object.variables.values);
  180. if (category.first == "artifact")
  181. value = JsonRandom::loadArtifact(input, rng, object.variables.values).getNum();
  182. if (category.first == "spell")
  183. value = JsonRandom::loadSpell(input, rng, object.variables.values).getNum();
  184. // TODO
  185. // if (category.first == "creature")
  186. // value = JsonRandom::loadCreature(input, rng, object.variables.values).type->getId();
  187. // TODO
  188. // if (category.first == "resource")
  189. // value = JsonRandom::loadResource(input, rng, object.variables.values).getNum();
  190. if (category.first == "primarySkill")
  191. value = static_cast<int>(JsonRandom::loadPrimary(input, rng, object.variables.values));
  192. if (category.first == "secondarySkill")
  193. value = JsonRandom::loadSecondary(input, rng, object.variables.values).getNum();
  194. object.initVariable(category.first, entry.first, value);
  195. }
  196. }
  197. }
  198. void Rewardable::Info::configureRewards(
  199. Rewardable::Configuration & object,
  200. CRandomGenerator & rng, const
  201. JsonNode & source,
  202. Rewardable::EEventType event,
  203. const std::string & modeName) const
  204. {
  205. for(size_t i = 0; i < source.Vector().size(); ++i)
  206. {
  207. const JsonNode reward = source.Vector()[i];
  208. if (!reward["appearChance"].isNull())
  209. {
  210. JsonNode chance = reward["appearChance"];
  211. std::string diceID = std::to_string(chance["dice"].Integer());
  212. auto diceValue = object.getVariable("dice", diceID);
  213. if (!diceValue.has_value())
  214. {
  215. const JsonNode & preset = object.getPresetVariable("dice", diceID);
  216. if (preset.isNull())
  217. object.initVariable("dice", diceID, rng.getIntRange(0, 99)());
  218. else
  219. object.initVariable("dice", diceID, preset.Integer());
  220. diceValue = object.getVariable("dice", diceID);
  221. }
  222. assert(diceValue.has_value());
  223. if (!chance["min"].isNull())
  224. {
  225. int min = static_cast<int>(chance["min"].Float());
  226. if (min > *diceValue)
  227. continue;
  228. }
  229. if (!chance["max"].isNull())
  230. {
  231. int max = static_cast<int>(chance["max"].Float());
  232. if (max <= *diceValue)
  233. continue;
  234. }
  235. }
  236. Rewardable::VisitInfo info;
  237. configureLimiter(object, rng, info.limiter, reward["limiter"]);
  238. configureReward(object, rng, info.reward, reward);
  239. info.visitType = event;
  240. info.message = loadMessage(reward["message"], TextIdentifier(objectTextID, modeName, i));
  241. info.description = loadMessage(reward["description"], TextIdentifier(objectTextID, "description", modeName, i), EMetaText::GENERAL_TXT);
  242. for (const auto & artifact : info.reward.artifacts )
  243. {
  244. info.message.replaceLocalString(EMetaText::ART_NAMES, artifact.getNum());
  245. info.description.replaceLocalString(EMetaText::ART_NAMES, artifact.getNum());
  246. }
  247. for (const auto & artifact : info.reward.spells )
  248. {
  249. info.message.replaceLocalString(EMetaText::SPELL_NAME, artifact.getNum());
  250. info.description.replaceLocalString(EMetaText::SPELL_NAME, artifact.getNum());
  251. }
  252. for (const auto & secondary : info.reward.secondary )
  253. {
  254. info.message.replaceLocalString(EMetaText::SEC_SKILL_NAME, secondary.first.getNum());
  255. info.description.replaceLocalString(EMetaText::SEC_SKILL_NAME, secondary.first.getNum());
  256. }
  257. for (const auto & variable : object.variables.values )
  258. {
  259. if( boost::algorithm::starts_with(variable.first, "spell"))
  260. {
  261. info.message.replaceLocalString(EMetaText::SPELL_NAME, variable.second);
  262. info.description.replaceLocalString(EMetaText::SPELL_NAME, variable.second);
  263. }
  264. if( boost::algorithm::starts_with(variable.first, "secondarySkill"))
  265. {
  266. info.message.replaceLocalString(EMetaText::SEC_SKILL_NAME, variable.second);
  267. info.description.replaceLocalString(EMetaText::SEC_SKILL_NAME, variable.second);
  268. }
  269. }
  270. object.info.push_back(info);
  271. }
  272. }
  273. void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const
  274. {
  275. object.info.clear();
  276. configureVariables(object, rng, parameters["variables"]);
  277. configureRewards(object, rng, parameters["rewards"], Rewardable::EEventType::EVENT_FIRST_VISIT, "rewards");
  278. configureRewards(object, rng, parameters["onVisited"], Rewardable::EEventType::EVENT_ALREADY_VISITED, "onVisited");
  279. configureRewards(object, rng, parameters["onEmpty"], Rewardable::EEventType::EVENT_NOT_AVAILABLE, "onEmpty");
  280. object.onSelect = loadMessage(parameters["onSelectMessage"], TextIdentifier(objectTextID, "onSelect"));
  281. object.description = loadMessage(parameters["description"], TextIdentifier(objectTextID, "description"));
  282. object.notVisitedTooltip = loadMessage(parameters["notVisitedTooltip"], TextIdentifier(objectTextID, "notVisitedTooltip"), EMetaText::GENERAL_TXT);
  283. object.visitedTooltip = loadMessage(parameters["visitedTooltip"], TextIdentifier(objectTextID, "visitedTooltip"), EMetaText::GENERAL_TXT);
  284. if (object.notVisitedTooltip.empty())
  285. object.notVisitedTooltip.appendTextID("core.genrltxt.353");
  286. if (object.visitedTooltip.empty())
  287. object.visitedTooltip.appendTextID("core.genrltxt.352");
  288. if (!parameters["onVisitedMessage"].isNull())
  289. {
  290. Rewardable::VisitInfo onVisited;
  291. onVisited.visitType = Rewardable::EEventType::EVENT_ALREADY_VISITED;
  292. onVisited.message = loadMessage(parameters["onVisitedMessage"], TextIdentifier(objectTextID, "onVisited"));
  293. for (const auto & variable : object.variables.values )
  294. {
  295. if( boost::algorithm::starts_with(variable.first, "spell"))
  296. onVisited.message.replaceLocalString(EMetaText::SPELL_NAME, variable.second);
  297. if( boost::algorithm::starts_with(variable.first, "secondarySkill"))
  298. onVisited.message.replaceLocalString(EMetaText::SEC_SKILL_NAME, variable.second);
  299. }
  300. object.info.push_back(onVisited);
  301. }
  302. if (!parameters["onEmptyMessage"].isNull())
  303. {
  304. Rewardable::VisitInfo onEmpty;
  305. onEmpty.visitType = Rewardable::EEventType::EVENT_NOT_AVAILABLE;
  306. onEmpty.message = loadMessage(parameters["onEmptyMessage"], TextIdentifier(objectTextID, "onEmpty"));
  307. for (const auto & variable : object.variables.values )
  308. {
  309. if( boost::algorithm::starts_with(variable.first, "spell"))
  310. onEmpty.message.replaceLocalString(EMetaText::SPELL_NAME, variable.second);
  311. if( boost::algorithm::starts_with(variable.first, "secondarySkill"))
  312. onEmpty.message.replaceLocalString(EMetaText::SEC_SKILL_NAME, variable.second);
  313. }
  314. object.info.push_back(onEmpty);
  315. }
  316. configureResetInfo(object, rng, object.resetParameters, parameters["resetParameters"]);
  317. object.canRefuse = parameters["canRefuse"].Bool();
  318. if(parameters["showInInfobox"].isNull())
  319. object.infoWindowType = EInfoWindowMode::AUTO;
  320. else
  321. object.infoWindowType = parameters["showInInfobox"].Bool() ? EInfoWindowMode::INFO : EInfoWindowMode::MODAL;
  322. auto visitMode = parameters["visitMode"].String();
  323. for(int i = 0; i < Rewardable::VisitModeString.size(); ++i)
  324. {
  325. if(Rewardable::VisitModeString[i] == visitMode)
  326. {
  327. object.visitMode = i;
  328. break;
  329. }
  330. }
  331. auto selectMode = parameters["selectMode"].String();
  332. for(int i = 0; i < Rewardable::SelectModeString.size(); ++i)
  333. {
  334. if(Rewardable::SelectModeString[i] == selectMode)
  335. {
  336. object.selectMode = i;
  337. break;
  338. }
  339. }
  340. if (object.visitMode == Rewardable::VISIT_LIMITER)
  341. configureLimiter(object, rng, object.visitLimiter, parameters["visitLimiter"]);
  342. }
  343. bool Rewardable::Info::givesResources() const
  344. {
  345. return testForKey(parameters, "resources");
  346. }
  347. bool Rewardable::Info::givesExperience() const
  348. {
  349. return testForKey(parameters, "gainedExp") || testForKey(parameters, "gainedLevels");
  350. }
  351. bool Rewardable::Info::givesMana() const
  352. {
  353. return testForKey(parameters, "manaPoints") || testForKey(parameters, "manaPercentage");
  354. }
  355. bool Rewardable::Info::givesMovement() const
  356. {
  357. return testForKey(parameters, "movePoints") || testForKey(parameters, "movePercentage");
  358. }
  359. bool Rewardable::Info::givesPrimarySkills() const
  360. {
  361. return testForKey(parameters, "primary");
  362. }
  363. bool Rewardable::Info::givesSecondarySkills() const
  364. {
  365. return testForKey(parameters, "secondary");
  366. }
  367. bool Rewardable::Info::givesArtifacts() const
  368. {
  369. return testForKey(parameters, "artifacts");
  370. }
  371. bool Rewardable::Info::givesCreatures() const
  372. {
  373. return testForKey(parameters, "spells");
  374. }
  375. bool Rewardable::Info::givesSpells() const
  376. {
  377. return testForKey(parameters, "creatures");
  378. }
  379. bool Rewardable::Info::givesBonuses() const
  380. {
  381. return testForKey(parameters, "bonuses");
  382. }
  383. const JsonNode & Rewardable::Info::getParameters() const
  384. {
  385. return parameters;
  386. }
  387. VCMI_LIB_NAMESPACE_END