Info.cpp 9.9 KB

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