CGPandoraBox.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * CGPandoraBox.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 "CGPandoraBox.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "../CSoundBase.h"
  15. #include "../CSkillHandler.h"
  16. #include "../StartInfo.h"
  17. #include "../callback/IGameInfoCallback.h"
  18. #include "../callback/IGameEventCallback.h"
  19. #include "../constants/StringConstants.h"
  20. #include "../networkPacks/PacksForClient.h"
  21. #include "../networkPacks/PacksForClientBattle.h"
  22. #include "../mapObjects/CGHeroInstance.h"
  23. #include "../serializer/JsonSerializeFormat.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. void CGPandoraBox::init()
  26. {
  27. blockVisit = true;
  28. configuration.info.emplace_back();
  29. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  30. for(auto & i : configuration.info)
  31. {
  32. i.reward.removeObject = true;
  33. if(!message.empty() && i.message.empty() && stacksCount() == 0)
  34. i.message = message;
  35. }
  36. }
  37. void CGPandoraBox::initObj(vstd::RNG & rand)
  38. {
  39. init();
  40. CRewardableObject::initObj(rand);
  41. }
  42. void CGPandoraBox::grantRewardWithMessage(IGameEventCallback & gameEvents, const CGHeroInstance * h, int index, bool markAsVisit) const
  43. {
  44. auto vi = configuration.info.at(index);
  45. if(!vi.message.empty())
  46. {
  47. CRewardableObject::grantRewardWithMessage(gameEvents, h, index, markAsVisit);
  48. return;
  49. }
  50. //split reward message for pandora box
  51. auto setText = [](bool cond, int posId, int negId, const CGHeroInstance * h)
  52. {
  53. MetaString text;
  54. text.appendLocalString(EMetaText::ADVOB_TXT, cond ? posId : negId);
  55. text.replaceRawString(h->getNameTranslated());
  56. return text;
  57. };
  58. auto sendInfoWindow = [&](const MetaString & text, const Rewardable::Reward & reward)
  59. {
  60. InfoWindow iw;
  61. iw.player = h->tempOwner;
  62. iw.text = text;
  63. reward.loadComponents(iw.components, h);
  64. iw.type = EInfoWindowMode::MODAL;
  65. if(!iw.components.empty())
  66. gameEvents.showInfoDialog(&iw);
  67. };
  68. Rewardable::Reward temp;
  69. temp.spells = vi.reward.spells;
  70. temp.heroExperience = vi.reward.heroExperience;
  71. temp.heroLevel = vi.reward.heroLevel;
  72. temp.primary = vi.reward.primary;
  73. temp.secondary = vi.reward.secondary;
  74. temp.heroBonuses = vi.reward.heroBonuses;
  75. temp.manaDiff = vi.reward.manaDiff;
  76. temp.manaPercentage = vi.reward.manaPercentage;
  77. MetaString txt;
  78. if(!vi.reward.spells.empty())
  79. txt = setText(temp.spells.size() == 1, 184, 188, h);
  80. if(vi.reward.heroExperience || vi.reward.heroLevel || !vi.reward.secondary.empty())
  81. txt = setText(true, 175, 175, h);
  82. for(int i : vi.reward.primary)
  83. {
  84. if(i)
  85. {
  86. txt = setText(true, 175, 175, h);
  87. break;
  88. }
  89. }
  90. if(vi.reward.manaDiff || vi.reward.manaPercentage >= 0)
  91. txt = setText(temp.manaDiff > 0, 177, 176, h);
  92. for(auto b : vi.reward.heroBonuses)
  93. {
  94. if(b.val && b.type == BonusType::MORALE)
  95. txt = setText(b.val > 0, 179, 178, h);
  96. if(b.val && b.type == BonusType::LUCK)
  97. txt = setText(b.val > 0, 181, 180, h);
  98. }
  99. sendInfoWindow(txt, temp);
  100. //resource message
  101. temp = Rewardable::Reward{};
  102. temp.resources = vi.reward.resources;
  103. sendInfoWindow(setText(vi.reward.resources.marketValue() > 0, 183, 182, h), temp);
  104. //artifacts message
  105. temp = Rewardable::Reward{};
  106. temp.grantedArtifacts = vi.reward.grantedArtifacts;
  107. sendInfoWindow(setText(true, 183, 183, h), temp);
  108. //creatures message
  109. temp = Rewardable::Reward{};
  110. temp.creatures = vi.reward.creatures;
  111. txt.clear();
  112. if(!vi.reward.creatures.empty())
  113. {
  114. MetaString loot;
  115. for(auto c : vi.reward.creatures)
  116. {
  117. loot.appendRawString("%s");
  118. loot.replaceName(c);
  119. }
  120. if(vi.reward.creatures.size() == 1 && vi.reward.creatures[0].getCount() == 1)
  121. txt.appendLocalString(EMetaText::ADVOB_TXT, 185);
  122. else
  123. txt.appendLocalString(EMetaText::ADVOB_TXT, 186);
  124. txt.replaceRawString(loot.buildList());
  125. txt.replaceRawString(h->getNameTranslated());
  126. }
  127. sendInfoWindow(txt, temp);
  128. //everything else
  129. temp = vi.reward;
  130. temp.heroExperience = 0;
  131. temp.heroLevel = 0;
  132. temp.secondary.clear();
  133. temp.primary.clear();
  134. temp.resources.amin(0);
  135. temp.resources.amax(0);
  136. temp.manaDiff = 0;
  137. temp.manaPercentage = -1;
  138. temp.spells.clear();
  139. temp.creatures.clear();
  140. temp.heroBonuses.clear();
  141. temp.grantedArtifacts.clear();
  142. sendInfoWindow(setText(true, 175, 175, h), temp);
  143. // grant reward afterwards. Note that it may remove object
  144. if(markAsVisit)
  145. markAsVisited(gameEvents, h);
  146. grantReward(gameEvents, index, h);
  147. }
  148. void CGPandoraBox::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  149. {
  150. BlockingDialog bd (true, false);
  151. bd.player = h->getOwner();
  152. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 14);
  153. gameEvents.showBlockingDialog(this, &bd);
  154. }
  155. void CGPandoraBox::battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const
  156. {
  157. if(result.winner == BattleSide::ATTACKER)
  158. {
  159. CRewardableObject::onHeroVisit(gameEvents, hero);
  160. }
  161. }
  162. void CGPandoraBox::blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const
  163. {
  164. if(answer)
  165. {
  166. if(stacksCount() > 0) //if pandora's box is protected by army
  167. {
  168. hero->showInfoDialog(gameEvents, 16, 0, EInfoWindowMode::MODAL);
  169. gameEvents.startBattle(hero, this); //grants things after battle
  170. }
  171. else if(getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
  172. {
  173. hero->showInfoDialog(gameEvents, 15);
  174. gameEvents.removeObject(this, hero->getOwner());
  175. }
  176. else //if it gives something without battle
  177. {
  178. CRewardableObject::onHeroVisit(gameEvents, hero);
  179. }
  180. }
  181. }
  182. void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
  183. {
  184. CRewardableObject::serializeJsonOptions(handler);
  185. handler.serializeStruct("guardMessage", message);
  186. if(!handler.saving)
  187. {
  188. //backward compatibility for VCMI maps that use old Pandora Box format
  189. if(!handler.getCurrent()["guards"].Vector().empty())
  190. CCreatureSet::serializeJson(handler, "guards", 7);
  191. Rewardable::VisitInfo vinfo;
  192. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  193. handler.serializeInt("experience", vinfo.reward.heroExperience, 0);
  194. handler.serializeInt("mana", vinfo.reward.manaDiff, 0);
  195. int val = 0;
  196. handler.serializeInt("morale", val, 0);
  197. if(val)
  198. vinfo.reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  199. handler.serializeInt("luck", val, 0);
  200. if(val)
  201. vinfo.reward.heroBonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  202. vinfo.reward.resources.serializeJson(handler, "resources");
  203. {
  204. auto s = handler.enterStruct("primarySkills");
  205. for(int idx = 0; idx < vinfo.reward.primary.size(); idx ++)
  206. handler.serializeInt(NPrimarySkill::names[idx], vinfo.reward.primary[idx], 0);
  207. }
  208. handler.serializeIdArray("artifacts", vinfo.reward.grantedArtifacts);
  209. handler.serializeIdArray("spells", vinfo.reward.spells);
  210. handler.enterArray("creatures").serializeStruct(vinfo.reward.creatures);
  211. {
  212. auto s = handler.enterStruct("secondarySkills");
  213. for(const auto & p : handler.getCurrent().Struct())
  214. {
  215. const std::string skillName = p.first;
  216. const std::string levelId = p.second.String();
  217. const int rawId = SecondarySkill::decode(skillName);
  218. if(rawId < 0)
  219. {
  220. logGlobal->error("Invalid secondary skill %s", skillName);
  221. continue;
  222. }
  223. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  224. if(level < 0)
  225. {
  226. logGlobal->error("Invalid secondary skill level %s", levelId);
  227. continue;
  228. }
  229. vinfo.reward.secondary[rawId] = level;
  230. }
  231. }
  232. configuration.info.push_back(vinfo);
  233. }
  234. }
  235. void CGEvent::init()
  236. {
  237. blockVisit = false;
  238. configuration.infoWindowType = EInfoWindowMode::MODAL;
  239. for(auto & i : configuration.info)
  240. {
  241. i.reward.removeObject = removeAfterVisit;
  242. if(!message.empty() && i.message.empty() && stacksCount() == 0)
  243. i.message = message;
  244. }
  245. }
  246. void CGEvent::grantRewardWithMessage(IGameEventCallback & gameEvents, const CGHeroInstance * contextHero, int rewardIndex, bool markAsVisit) const
  247. {
  248. CRewardableObject::grantRewardWithMessage(gameEvents, contextHero, rewardIndex, markAsVisit);
  249. }
  250. void CGEvent::onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const
  251. {
  252. if(availableFor.count(h->tempOwner) == 0)
  253. return;
  254. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  255. {
  256. if(humanActivate)
  257. activated(gameEvents, h);
  258. }
  259. else if(computerActivate)
  260. activated(gameEvents, h);
  261. }
  262. void CGEvent::activated(IGameEventCallback & gameEvents, const CGHeroInstance * h ) const
  263. {
  264. if(stacksCount() > 0)
  265. {
  266. InfoWindow iw;
  267. iw.player = h->tempOwner;
  268. if(!message.empty())
  269. iw.text = message;
  270. else
  271. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  272. gameEvents.showInfoDialog(&iw);
  273. gameEvents.startBattle(h, this);
  274. }
  275. else
  276. {
  277. CRewardableObject::onHeroVisit(gameEvents, h);
  278. }
  279. }
  280. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  281. {
  282. CGPandoraBox::serializeJsonOptions(handler);
  283. handler.serializeBool("aIActivable", computerActivate, false);
  284. handler.serializeBool("humanActivable", humanActivate, true);
  285. handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
  286. handler.serializeIdArray("availableFor", availableFor);
  287. }
  288. VCMI_LIB_NAMESPACE_END