CGPandoraBox.cpp 8.9 KB

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