CGPandoraBox.cpp 9.0 KB

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