CGPandoraBox.cpp 7.2 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. const_cast<MetaString&>(r.message) = txt;
  105. }
  106. BlockingDialog bd (true, false);
  107. bd.player = h->getOwner();
  108. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 14);
  109. cb->showBlockingDialog(&bd);
  110. }
  111. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  112. {
  113. if(result.winner == 0)
  114. {
  115. CRewardableObject::onHeroVisit(hero);
  116. }
  117. }
  118. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  119. {
  120. if(answer)
  121. {
  122. if(stacksCount() > 0) //if pandora's box is protected by army
  123. {
  124. hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL);
  125. cb->startBattleI(hero, this); //grants things after battle
  126. }
  127. else if(getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
  128. {
  129. hero->showInfoDialog(15);
  130. cb->removeObject(this);
  131. }
  132. else //if it gives something without battle
  133. {
  134. CRewardableObject::onHeroVisit(hero);
  135. }
  136. }
  137. }
  138. void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
  139. {
  140. CRewardableObject::serializeJsonOptions(handler);
  141. handler.serializeString("guardMessage", message);
  142. if(!handler.saving)
  143. {
  144. //backward compatibility
  145. CCreatureSet::serializeJson(handler, "guards", 7);
  146. Rewardable::Reward reward;
  147. auto addReward = [this, &reward](bool condition)
  148. {
  149. if(condition)
  150. {
  151. configuration.info.emplace_back();
  152. configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  153. configuration.info.back().reward = reward;
  154. }
  155. };
  156. addReward(true);
  157. int val;
  158. handler.serializeInt("experience", reward.heroExperience, 0);
  159. addReward(reward.heroExperience);
  160. handler.serializeInt("mana", reward.manaDiff, 0);
  161. addReward(reward.manaDiff);
  162. handler.serializeInt("morale", val, 0);
  163. if(val)
  164. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT, val, id);
  165. addReward(val);
  166. handler.serializeInt("luck", val, 0);
  167. if(val)
  168. reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
  169. addReward(val);
  170. reward.resources.serializeJson(handler, "resources");
  171. addReward(reward.resources.nonZero());
  172. {
  173. bool updateReward = false;
  174. auto s = handler.enterStruct("primarySkills");
  175. for(int idx = 0; idx < reward.primary.size(); idx ++)
  176. {
  177. handler.serializeInt(NPrimarySkill::names[idx], reward.primary[idx], 0);
  178. updateReward |= reward.primary[idx];
  179. }
  180. addReward(updateReward);
  181. }
  182. handler.serializeIdArray("artifacts", reward.artifacts);
  183. addReward(!reward.artifacts.empty());
  184. handler.serializeIdArray("spells", reward.spells);
  185. addReward(!reward.spells.empty());
  186. handler.enterArray("creatures").serializeStruct(reward.creatures);
  187. addReward(!reward.creatures.empty());
  188. {
  189. bool updateReward = false;
  190. auto s = handler.enterStruct("secondarySkills");
  191. for(const auto & p : handler.getCurrent().Struct())
  192. {
  193. const std::string skillName = p.first;
  194. const std::string levelId = p.second.String();
  195. const int rawId = CSkillHandler::decodeSkill(skillName);
  196. if(rawId < 0)
  197. {
  198. logGlobal->error("Invalid secondary skill %s", skillName);
  199. continue;
  200. }
  201. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  202. if(level < 0)
  203. {
  204. logGlobal->error("Invalid secondary skill level %s", levelId);
  205. continue;
  206. }
  207. reward.secondary[rawId] = level;
  208. updateReward = true;
  209. }
  210. addReward(updateReward);
  211. }
  212. }
  213. }
  214. void CGEvent::init()
  215. {
  216. blockVisit = false;
  217. configuration.selectMode = Rewardable::SELECT_ALL;
  218. for(auto & i : configuration.info)
  219. i.reward.removeObject = removeAfterVisit;
  220. }
  221. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  222. {
  223. if(availableFor.count(h->tempOwner) == 0)
  224. return;
  225. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  226. {
  227. if(humanActivate)
  228. activated(h);
  229. }
  230. else if(computerActivate)
  231. activated(h);
  232. }
  233. void CGEvent::activated( const CGHeroInstance * h ) const
  234. {
  235. if(stacksCount() > 0)
  236. {
  237. InfoWindow iw;
  238. iw.player = h->tempOwner;
  239. if(!message.empty())
  240. iw.text.appendRawString(message);
  241. else
  242. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  243. cb->showInfoDialog(&iw);
  244. cb->startBattleI(h, this);
  245. }
  246. else
  247. {
  248. CRewardableObject::onHeroVisit(h);
  249. }
  250. }
  251. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  252. {
  253. CGPandoraBox::serializeJsonOptions(handler);
  254. handler.serializeBool("aIActivable", computerActivate, false);
  255. handler.serializeBool("humanActivable", humanActivate, true);
  256. handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
  257. handler.serializeIdArray("availableFor", availableFor);
  258. }
  259. VCMI_LIB_NAMESPACE_END