CGPandoraBox.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "../NetPacks.h"
  15. #include "../CSoundBase.h"
  16. #include "../CSkillHandler.h"
  17. #include "../StartInfo.h"
  18. #include "../IGameCallback.h"
  19. #include "../constants/StringConstants.h"
  20. #include "../serializer/JsonSerializeFormat.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. void CGPandoraBox::init()
  23. {
  24. blockVisit = true;
  25. configuration.selectMode = Rewardable::SELECT_ALL;
  26. for(auto & i : configuration.info)
  27. i.reward.removeObject = true;
  28. }
  29. void CGPandoraBox::initObj(CRandomGenerator & rand)
  30. {
  31. init();
  32. CRewardableObject::initObj(rand);
  33. }
  34. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  35. {
  36. auto setText = [](MetaString & text, int tId, const CGHeroInstance * h)
  37. {
  38. text.appendLocalString(EMetaText::ADVOB_TXT, tId);
  39. text.replaceRawString(h->getNameTranslated());
  40. };
  41. for(auto i : getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT))
  42. {
  43. MetaString txt;
  44. auto & r = configuration.info[i];
  45. if(r.reward.spells.size() == 1)
  46. setText(txt, 184, h);
  47. else if(!r.reward.spells.empty())
  48. setText(txt, 188, h);
  49. if(r.reward.heroExperience || !r.reward.secondary.empty())
  50. setText(txt, 175, h);
  51. for(int ps : r.reward.primary)
  52. {
  53. if(ps)
  54. {
  55. setText(txt, 175, h);
  56. break;
  57. }
  58. }
  59. if(r.reward.manaDiff < 0)
  60. setText(txt, 176, h);
  61. if(r.reward.manaDiff > 0)
  62. setText(txt, 177, h);
  63. for(auto b : r.reward.bonuses)
  64. {
  65. if(b.type == BonusType::MORALE)
  66. {
  67. if(b.val < 0)
  68. setText(txt, 178, h);
  69. if(b.val > 0)
  70. setText(txt, 179, h);
  71. }
  72. if(b.type == BonusType::LUCK)
  73. {
  74. if(b.val < 0)
  75. setText(txt, 180, h);
  76. if(b.val > 0)
  77. setText(txt, 181, h);
  78. }
  79. }
  80. for(auto res : r.reward.resources)
  81. {
  82. if(res < 0)
  83. setText(txt, 182, h);
  84. if(res > 0)
  85. setText(txt, 183, h);
  86. }
  87. if(!r.reward.artifacts.empty())
  88. setText(txt, 183, h);
  89. if(!r.reward.creatures.empty())
  90. {
  91. MetaString loot;
  92. for(auto c : r.reward.creatures)
  93. {
  94. loot.appendRawString("%s");
  95. loot.replaceCreatureName(c);
  96. }
  97. if(r.reward.creatures.size() == 1 && r.reward.creatures[0].count == 1)
  98. txt.appendLocalString(EMetaText::ADVOB_TXT, 185);
  99. else
  100. txt.appendLocalString(EMetaText::ADVOB_TXT, 186);
  101. txt.replaceRawString(loot.buildList());
  102. txt.replaceRawString(h->getNameTranslated());
  103. }
  104. if(r.message.empty())
  105. const_cast<MetaString&>(r.message) = txt;
  106. }
  107. BlockingDialog bd (true, false);
  108. bd.player = h->getOwner();
  109. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 14);
  110. cb->showBlockingDialog(&bd);
  111. }
  112. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  113. {
  114. if(result.winner == 0)
  115. {
  116. CRewardableObject::onHeroVisit(hero);
  117. }
  118. }
  119. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  120. {
  121. if(answer)
  122. {
  123. if(stacksCount() > 0) //if pandora's box is protected by army
  124. {
  125. hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL);
  126. cb->startBattleI(hero, this); //grants things after battle
  127. }
  128. else if(getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
  129. {
  130. hero->showInfoDialog(15);
  131. cb->removeObject(this);
  132. }
  133. else //if it gives something without battle
  134. {
  135. CRewardableObject::onHeroVisit(hero);
  136. }
  137. }
  138. }
  139. void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
  140. {
  141. CRewardableObject::serializeJsonOptions(handler);
  142. handler.serializeString("guardMessage", message);
  143. if(!handler.saving)
  144. {
  145. //backward compatibility
  146. CCreatureSet::serializeJson(handler, "guards", 7);
  147. Rewardable::VisitInfo vinfo;
  148. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  149. auto addReward = [this, &vinfo](bool condition)
  150. {
  151. if(condition)
  152. {
  153. configuration.info.push_back(vinfo);
  154. vinfo = Rewardable::VisitInfo{};
  155. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  156. }
  157. };
  158. int val;
  159. handler.serializeInt("experience", vinfo.reward.heroExperience, 0);
  160. addReward(vinfo.reward.heroExperience);
  161. handler.serializeInt("mana", vinfo.reward.manaDiff, 0);
  162. addReward(vinfo.reward.manaDiff);
  163. handler.serializeInt("morale", val, 0);
  164. if(val)
  165. vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
  166. addReward(val);
  167. handler.serializeInt("luck", val, 0);
  168. if(val)
  169. vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
  170. addReward(val);
  171. vinfo.reward.resources.serializeJson(handler, "resources");
  172. addReward(vinfo.reward.resources.nonZero());
  173. {
  174. bool updateReward = false;
  175. auto s = handler.enterStruct("primarySkills");
  176. for(int idx = 0; idx < vinfo.reward.primary.size(); idx ++)
  177. {
  178. handler.serializeInt(NPrimarySkill::names[idx], vinfo.reward.primary[idx], 0);
  179. updateReward |= bool(vinfo.reward.primary[idx]);
  180. }
  181. addReward(updateReward);
  182. }
  183. handler.serializeIdArray("artifacts", vinfo.reward.artifacts);
  184. addReward(!vinfo.reward.artifacts.empty());
  185. handler.serializeIdArray("spells", vinfo.reward.spells);
  186. addReward(!vinfo.reward.spells.empty());
  187. handler.enterArray("creatures").serializeStruct(vinfo.reward.creatures);
  188. addReward(!vinfo.reward.creatures.empty());
  189. {
  190. bool updateReward = false;
  191. auto s = handler.enterStruct("secondarySkills");
  192. for(const auto & p : handler.getCurrent().Struct())
  193. {
  194. const std::string skillName = p.first;
  195. const std::string levelId = p.second.String();
  196. const int rawId = CSkillHandler::decodeSkill(skillName);
  197. if(rawId < 0)
  198. {
  199. logGlobal->error("Invalid secondary skill %s", skillName);
  200. continue;
  201. }
  202. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  203. if(level < 0)
  204. {
  205. logGlobal->error("Invalid secondary skill level %s", levelId);
  206. continue;
  207. }
  208. vinfo.reward.secondary[rawId] = level;
  209. updateReward = true;
  210. }
  211. addReward(updateReward);
  212. }
  213. }
  214. }
  215. void CGEvent::init()
  216. {
  217. blockVisit = false;
  218. configuration.selectMode = Rewardable::SELECT_ALL;
  219. for(auto & i : configuration.info)
  220. i.reward.removeObject = removeAfterVisit;
  221. }
  222. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  223. {
  224. if(availableFor.count(h->tempOwner) == 0)
  225. return;
  226. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  227. {
  228. if(humanActivate)
  229. activated(h);
  230. }
  231. else if(computerActivate)
  232. activated(h);
  233. }
  234. void CGEvent::activated( const CGHeroInstance * h ) const
  235. {
  236. if(stacksCount() > 0)
  237. {
  238. InfoWindow iw;
  239. iw.player = h->tempOwner;
  240. if(!message.empty())
  241. iw.text.appendRawString(message);
  242. else
  243. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  244. cb->showInfoDialog(&iw);
  245. cb->startBattleI(h, this);
  246. }
  247. else
  248. {
  249. CRewardableObject::onHeroVisit(h);
  250. }
  251. }
  252. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  253. {
  254. CGPandoraBox::serializeJsonOptions(handler);
  255. handler.serializeBool("aIActivable", computerActivate, false);
  256. handler.serializeBool("humanActivable", humanActivate, true);
  257. handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
  258. handler.serializeIdArray("availableFor", availableFor);
  259. }
  260. VCMI_LIB_NAMESPACE_END