Info.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "Limiter.h"
  13. #include "Reward.h"
  14. #include "Configuration.h"
  15. #include "../CRandomGenerator.h"
  16. #include "../StringConstants.h"
  17. #include "../CCreatureHandler.h"
  18. #include "../CModHandler.h"
  19. #include "../JsonRandom.h"
  20. #include "../IGameCallback.h"
  21. #include "../CGeneralTextHandler.h"
  22. #include "../JsonNode.h"
  23. #include "../IGameCallback.h"
  24. #include "../CPlayerState.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. namespace {
  27. MetaString loadMessage(const JsonNode & value)
  28. {
  29. MetaString ret;
  30. if (value.isNumber())
  31. ret.addTxt(MetaString::ADVOB_TXT, static_cast<ui32>(value.Float()));
  32. else
  33. ret << value.String();
  34. return ret;
  35. }
  36. bool testForKey(const JsonNode & value, const std::string & key)
  37. {
  38. for(const auto & reward : value["rewards"].Vector())
  39. {
  40. if (!reward[key].isNull())
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. void Rewardable::Info::init(const JsonNode & objectConfig)
  47. {
  48. parameters = objectConfig;
  49. }
  50. Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
  51. {
  52. Rewardable::LimitersList result;
  53. for (const auto & input : source.Vector())
  54. {
  55. auto newLimiter = std::make_shared<Rewardable::Limiter>();
  56. configureLimiter(object, rng, *newLimiter, input);
  57. result.push_back(newLimiter);
  58. }
  59. return result;
  60. }
  61. void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const
  62. {
  63. std::vector<SpellID> spells;
  64. for (size_t i=0; i<6; i++)
  65. IObjectInterface::cb->getAllowedSpells(spells, static_cast<ui16>(i));
  66. limiter.dayOfWeek = JsonRandom::loadValue(source["dayOfWeek"], rng);
  67. limiter.daysPassed = JsonRandom::loadValue(source["daysPassed"], rng);
  68. limiter.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng);
  69. limiter.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng)
  70. + JsonRandom::loadValue(source["minLevel"], rng); // VCMI 1.1 compatibilty
  71. limiter.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng);
  72. limiter.manaPoints = JsonRandom::loadValue(source["manaPoints"], rng);
  73. limiter.resources = JsonRandom::loadResources(source["resources"], rng);
  74. limiter.primary = JsonRandom::loadPrimary(source["primary"], rng);
  75. limiter.secondary = JsonRandom::loadSecondary(source["secondary"], rng);
  76. limiter.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng);
  77. limiter.spells = JsonRandom::loadSpells(source["spells"], rng, spells);
  78. limiter.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
  79. limiter.allOf = configureSublimiters(object, rng, source["allOf"] );
  80. limiter.anyOf = configureSublimiters(object, rng, source["anyOf"] );
  81. limiter.noneOf = configureSublimiters(object, rng, source["noneOf"] );
  82. }
  83. void Rewardable::Info::configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Reward & reward, const JsonNode & source) const
  84. {
  85. reward.resources = JsonRandom::loadResources(source["resources"], rng);
  86. reward.heroExperience = JsonRandom::loadValue(source["heroExperience"], rng)
  87. + JsonRandom::loadValue(source["gainedExp"], rng); // VCMI 1.1 compatibilty
  88. reward.heroLevel = JsonRandom::loadValue(source["heroLevel"], rng)
  89. + JsonRandom::loadValue(source["gainedLevels"], rng); // VCMI 1.1 compatibilty
  90. reward.manaDiff = JsonRandom::loadValue(source["manaPoints"], rng);
  91. reward.manaOverflowFactor = JsonRandom::loadValue(source["manaOverflowFactor"], rng);
  92. reward.manaPercentage = JsonRandom::loadValue(source["manaPercentage"], rng, -1);
  93. reward.movePoints = JsonRandom::loadValue(source["movePoints"], rng);
  94. reward.movePercentage = JsonRandom::loadValue(source["movePercentage"], rng, -1);
  95. reward.removeObject = source["removeObject"].Bool();
  96. reward.bonuses = JsonRandom::loadBonuses(source["bonuses"]);
  97. reward.primary = JsonRandom::loadPrimary(source["primary"], rng);
  98. reward.secondary = JsonRandom::loadSecondary(source["secondary"], rng);
  99. std::vector<SpellID> spells;
  100. for (size_t i=0; i<6; i++)
  101. IObjectInterface::cb->getAllowedSpells(spells, static_cast<ui16>(i));
  102. reward.artifacts = JsonRandom::loadArtifacts(source["artifacts"], rng);
  103. reward.spells = JsonRandom::loadSpells(source["spells"], rng, spells);
  104. reward.creatures = JsonRandom::loadCreatures(source["creatures"], rng);
  105. if(!source["spellCast"].isNull() && source["spellCast"].isStruct())
  106. {
  107. reward.spellCast.first = JsonRandom::loadSpell(source["spellCast"]["spell"], rng);
  108. reward.spellCast.second = source["spellCast"]["schoolLevel"].Integer();
  109. }
  110. for ( auto node : source["changeCreatures"].Struct() )
  111. {
  112. CreatureID from(VLC->modh->identifiers.getIdentifier(node.second.meta, "creature", node.first).value());
  113. CreatureID dest(VLC->modh->identifiers.getIdentifier(node.second.meta, "creature", node.second.String()).value());
  114. reward.extraComponents.emplace_back(Component::EComponentType::CREATURE, dest.getNum(), 0, 0);
  115. reward.creaturesChange[from] = dest;
  116. }
  117. }
  118. void Rewardable::Info::configureResetInfo(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::ResetInfo & resetParameters, const JsonNode & source) const
  119. {
  120. resetParameters.period = static_cast<ui32>(source["period"].Float());
  121. resetParameters.visitors = source["visitors"].Bool();
  122. resetParameters.rewards = source["rewards"].Bool();
  123. }
  124. void Rewardable::Info::configureRewards(
  125. Rewardable::Configuration & object,
  126. CRandomGenerator & rng, const
  127. JsonNode & source,
  128. std::map<si32, si32> & thrownDice,
  129. Rewardable::EEventType event ) const
  130. {
  131. for (const JsonNode & reward : source.Vector())
  132. {
  133. if (!reward["appearChance"].isNull())
  134. {
  135. JsonNode chance = reward["appearChance"];
  136. si32 diceID = static_cast<si32>(chance["dice"].Float());
  137. if (thrownDice.count(diceID) == 0)
  138. thrownDice[diceID] = rng.getIntRange(0, 99)();
  139. if (!chance["min"].isNull())
  140. {
  141. int min = static_cast<int>(chance["min"].Float());
  142. if (min > thrownDice[diceID])
  143. continue;
  144. }
  145. if (!chance["max"].isNull())
  146. {
  147. int max = static_cast<int>(chance["max"].Float());
  148. if (max <= thrownDice[diceID])
  149. continue;
  150. }
  151. }
  152. Rewardable::VisitInfo info;
  153. configureLimiter(object, rng, info.limiter, reward["limiter"]);
  154. configureReward(object, rng, info.reward, reward);
  155. info.visitType = event;
  156. info.message = loadMessage(reward["message"]);
  157. for (const auto & artifact : info.reward.artifacts )
  158. info.message.addReplacement(MetaString::ART_NAMES, artifact.getNum());
  159. for (const auto & artifact : info.reward.spells )
  160. info.message.addReplacement(MetaString::SPELL_NAME, artifact.getNum());
  161. object.info.push_back(info);
  162. }
  163. }
  164. void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const
  165. {
  166. object.info.clear();
  167. std::map<si32, si32> thrownDice;
  168. configureRewards(object, rng, parameters["rewards"], thrownDice, Rewardable::EEventType::EVENT_FIRST_VISIT);
  169. configureRewards(object, rng, parameters["onVisited"], thrownDice, Rewardable::EEventType::EVENT_ALREADY_VISITED);
  170. configureRewards(object, rng, parameters["onEmpty"], thrownDice, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
  171. object.onSelect = loadMessage(parameters["onSelectMessage"]);
  172. if (!parameters["onVisitedMessage"].isNull())
  173. {
  174. Rewardable::VisitInfo onVisited;
  175. onVisited.visitType = Rewardable::EEventType::EVENT_ALREADY_VISITED;
  176. onVisited.message = loadMessage(parameters["onVisitedMessage"]);
  177. object.info.push_back(onVisited);
  178. }
  179. if (!parameters["onEmptyMessage"].isNull())
  180. {
  181. Rewardable::VisitInfo onEmpty;
  182. onEmpty.visitType = Rewardable::EEventType::EVENT_NOT_AVAILABLE;
  183. onEmpty.message = loadMessage(parameters["onEmptyMessage"]);
  184. object.info.push_back(onEmpty);
  185. }
  186. configureResetInfo(object, rng, object.resetParameters, parameters["resetParameters"]);
  187. object.canRefuse = parameters["canRefuse"].Bool();
  188. if(parameters["showInInfobox"].isNull())
  189. object.infoWindowType = EInfoWindowMode::AUTO;
  190. else
  191. object.infoWindowType = parameters["showInInfobox"].Bool() ? EInfoWindowMode::INFO : EInfoWindowMode::MODAL;
  192. auto visitMode = parameters["visitMode"].String();
  193. for(int i = 0; i < Rewardable::VisitModeString.size(); ++i)
  194. {
  195. if(Rewardable::VisitModeString[i] == visitMode)
  196. {
  197. object.visitMode = i;
  198. break;
  199. }
  200. }
  201. auto selectMode = parameters["selectMode"].String();
  202. for(int i = 0; i < Rewardable::SelectModeString.size(); ++i)
  203. {
  204. if(Rewardable::SelectModeString[i] == selectMode)
  205. {
  206. object.selectMode = i;
  207. break;
  208. }
  209. }
  210. }
  211. bool Rewardable::Info::givesResources() const
  212. {
  213. return testForKey(parameters, "resources");
  214. }
  215. bool Rewardable::Info::givesExperience() const
  216. {
  217. return testForKey(parameters, "gainedExp") || testForKey(parameters, "gainedLevels");
  218. }
  219. bool Rewardable::Info::givesMana() const
  220. {
  221. return testForKey(parameters, "manaPoints") || testForKey(parameters, "manaPercentage");
  222. }
  223. bool Rewardable::Info::givesMovement() const
  224. {
  225. return testForKey(parameters, "movePoints") || testForKey(parameters, "movePercentage");
  226. }
  227. bool Rewardable::Info::givesPrimarySkills() const
  228. {
  229. return testForKey(parameters, "primary");
  230. }
  231. bool Rewardable::Info::givesSecondarySkills() const
  232. {
  233. return testForKey(parameters, "secondary");
  234. }
  235. bool Rewardable::Info::givesArtifacts() const
  236. {
  237. return testForKey(parameters, "artifacts");
  238. }
  239. bool Rewardable::Info::givesCreatures() const
  240. {
  241. return testForKey(parameters, "spells");
  242. }
  243. bool Rewardable::Info::givesSpells() const
  244. {
  245. return testForKey(parameters, "creatures");
  246. }
  247. bool Rewardable::Info::givesBonuses() const
  248. {
  249. return testForKey(parameters, "bonuses");
  250. }
  251. const JsonNode & Rewardable::Info::getParameters() const
  252. {
  253. return parameters;
  254. }
  255. VCMI_LIB_NAMESPACE_END