Info.cpp 17 KB

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