CGPandoraBox.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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::Reward reward;
  148. auto addReward = [this, &reward](bool condition)
  149. {
  150. if(condition)
  151. {
  152. configuration.info.emplace_back();
  153. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  154. configuration.info.back().reward = reward;
  155. reward = Rewardable::Reward{};
  156. }
  157. };
  158. addReward(true);
  159. int val;
  160. handler.serializeInt("experience", reward.heroExperience, 0);
  161. addReward(reward.heroExperience);
  162. handler.serializeInt("mana", reward.manaDiff, 0);
  163. addReward(reward.manaDiff);
  164. handler.serializeInt("morale", val, 0);
  165. if(val)
  166. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
  167. addReward(val);
  168. handler.serializeInt("luck", val, 0);
  169. if(val)
  170. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
  171. addReward(val);
  172. reward.resources.serializeJson(handler, "resources");
  173. addReward(reward.resources.nonZero());
  174. {
  175. bool updateReward = false;
  176. auto s = handler.enterStruct("primarySkills");
  177. for(int idx = 0; idx < reward.primary.size(); idx ++)
  178. {
  179. handler.serializeInt(NPrimarySkill::names[idx], reward.primary[idx], 0);
  180. updateReward |= reward.primary[idx];
  181. }
  182. addReward(updateReward);
  183. }
  184. handler.serializeIdArray("artifacts", reward.artifacts);
  185. addReward(!reward.artifacts.empty());
  186. handler.serializeIdArray("spells", reward.spells);
  187. addReward(!reward.spells.empty());
  188. handler.enterArray("creatures").serializeStruct(reward.creatures);
  189. addReward(!reward.creatures.empty());
  190. {
  191. bool updateReward = false;
  192. auto s = handler.enterStruct("secondarySkills");
  193. for(const auto & p : handler.getCurrent().Struct())
  194. {
  195. const std::string skillName = p.first;
  196. const std::string levelId = p.second.String();
  197. const int rawId = CSkillHandler::decodeSkill(skillName);
  198. if(rawId < 0)
  199. {
  200. logGlobal->error("Invalid secondary skill %s", skillName);
  201. continue;
  202. }
  203. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  204. if(level < 0)
  205. {
  206. logGlobal->error("Invalid secondary skill level %s", levelId);
  207. continue;
  208. }
  209. reward.secondary[rawId] = level;
  210. updateReward = true;
  211. }
  212. addReward(updateReward);
  213. }
  214. }
  215. }
  216. void CGEvent::init()
  217. {
  218. blockVisit = false;
  219. configuration.selectMode = Rewardable::SELECT_ALL;
  220. for(auto & i : configuration.info)
  221. i.reward.removeObject = removeAfterVisit;
  222. }
  223. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  224. {
  225. if(availableFor.count(h->tempOwner) == 0)
  226. return;
  227. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  228. {
  229. if(humanActivate)
  230. activated(h);
  231. }
  232. else if(computerActivate)
  233. activated(h);
  234. }
  235. void CGEvent::activated( const CGHeroInstance * h ) const
  236. {
  237. if(stacksCount() > 0)
  238. {
  239. InfoWindow iw;
  240. iw.player = h->tempOwner;
  241. if(!message.empty())
  242. iw.text.appendRawString(message);
  243. else
  244. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  245. cb->showInfoDialog(&iw);
  246. cb->startBattleI(h, this);
  247. }
  248. else
  249. {
  250. CRewardableObject::onHeroVisit(h);
  251. }
  252. }
  253. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  254. {
  255. CGPandoraBox::serializeJsonOptions(handler);
  256. handler.serializeBool("aIActivable", computerActivate, false);
  257. handler.serializeBool("humanActivable", humanActivate, true);
  258. handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
  259. handler.serializeIdArray("availableFor", availableFor);
  260. }
  261. VCMI_LIB_NAMESPACE_END