2
0

CGPandoraBox.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "../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())
  33. i.message = message;
  34. }
  35. }
  36. void CGPandoraBox::initObj(CRandomGenerator & 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.bonuses = vi.reward.bonuses;
  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.bonuses)
  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.artifacts = vi.reward.artifacts;
  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].count == 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.bonuses.clear();
  140. temp.artifacts.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(&bd);
  153. }
  154. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  155. {
  156. if(result.winner == 0)
  157. {
  158. CRewardableObject::onHeroVisit(hero);
  159. }
  160. }
  161. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 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->startBattleI(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. bool hasSomething = false;
  191. Rewardable::VisitInfo vinfo;
  192. vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
  193. handler.serializeInt("experience", vinfo.reward.heroExperience, 0);
  194. handler.serializeInt("mana", vinfo.reward.manaDiff, 0);
  195. int val = 0;
  196. handler.serializeInt("morale", val, 0);
  197. if(val)
  198. vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  199. handler.serializeInt("luck", val, 0);
  200. if(val)
  201. vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
  202. vinfo.reward.resources.serializeJson(handler, "resources");
  203. {
  204. auto s = handler.enterStruct("primarySkills");
  205. for(int idx = 0; idx < vinfo.reward.primary.size(); idx ++)
  206. {
  207. handler.serializeInt(NPrimarySkill::names[idx], vinfo.reward.primary[idx], 0);
  208. if(vinfo.reward.primary[idx])
  209. hasSomething = true;
  210. }
  211. }
  212. handler.serializeIdArray("artifacts", vinfo.reward.artifacts);
  213. handler.serializeIdArray("spells", vinfo.reward.spells);
  214. handler.enterArray("creatures").serializeStruct(vinfo.reward.creatures);
  215. {
  216. auto s = handler.enterStruct("secondarySkills");
  217. for(const auto & p : handler.getCurrent().Struct())
  218. {
  219. const std::string skillName = p.first;
  220. const std::string levelId = p.second.String();
  221. const int rawId = SecondarySkill::decode(skillName);
  222. if(rawId < 0)
  223. {
  224. logGlobal->error("Invalid secondary skill %s", skillName);
  225. continue;
  226. }
  227. const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
  228. if(level < 0)
  229. {
  230. logGlobal->error("Invalid secondary skill level %s", levelId);
  231. continue;
  232. }
  233. vinfo.reward.secondary[rawId] = level;
  234. }
  235. }
  236. hasSomething = hasSomething
  237. || vinfo.reward.heroExperience
  238. || vinfo.reward.manaDiff
  239. || vinfo.reward.resources.nonZero()
  240. || !vinfo.reward.artifacts.empty()
  241. || !vinfo.reward.bonuses.empty()
  242. || !vinfo.reward.creatures.empty()
  243. || !vinfo.reward.secondary.empty();
  244. if(hasSomething)
  245. configuration.info.push_back(vinfo);
  246. }
  247. }
  248. void CGEvent::init()
  249. {
  250. blockVisit = false;
  251. configuration.infoWindowType = EInfoWindowMode::MODAL;
  252. for(auto & i : configuration.info)
  253. {
  254. i.reward.removeObject = removeAfterVisit;
  255. if(!message.empty() && i.message.empty())
  256. i.message = message;
  257. }
  258. }
  259. void CGEvent::grantRewardWithMessage(const CGHeroInstance * contextHero, int rewardIndex, bool markAsVisit) const
  260. {
  261. CRewardableObject::grantRewardWithMessage(contextHero, rewardIndex, markAsVisit);
  262. }
  263. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  264. {
  265. if(availableFor.count(h->tempOwner) == 0)
  266. return;
  267. if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
  268. {
  269. if(humanActivate)
  270. activated(h);
  271. }
  272. else if(computerActivate)
  273. activated(h);
  274. }
  275. void CGEvent::activated( const CGHeroInstance * h ) const
  276. {
  277. if(stacksCount() > 0)
  278. {
  279. InfoWindow iw;
  280. iw.player = h->tempOwner;
  281. if(!message.empty())
  282. iw.text = message;
  283. else
  284. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
  285. cb->showInfoDialog(&iw);
  286. cb->startBattleI(h, this);
  287. }
  288. else
  289. {
  290. CRewardableObject::onHeroVisit(h);
  291. }
  292. }
  293. void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
  294. {
  295. CGPandoraBox::serializeJsonOptions(handler);
  296. handler.serializeBool("aIActivable", computerActivate, false);
  297. handler.serializeBool("humanActivable", humanActivate, true);
  298. handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
  299. handler.serializeIdArray("availableFor", availableFor);
  300. }
  301. VCMI_LIB_NAMESPACE_END