Info.cpp 13 KB

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