CGPandoraBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 "../NetPacks.h"
  13. #include "../CSoundBase.h"
  14. #include "../spells/CSpellHandler.h"
  15. #include "../StartInfo.h"
  16. #include "../IGameCallback.h"
  17. ///helpers
  18. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  19. {
  20. InfoWindow iw;
  21. iw.soundID = soundID;
  22. iw.player = playerID;
  23. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  24. IObjectInterface::cb->sendAndApply(&iw);
  25. }
  26. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  27. {
  28. const PlayerColor playerID = h->getOwner();
  29. showInfoDialog(playerID,txtID,soundID);
  30. }
  31. void CGPandoraBox::initObj(CRandomGenerator & rand)
  32. {
  33. blockVisit = (ID==Obj::PANDORAS_BOX); //block only if it's really pandora's box (events also derive from that class)
  34. hasGuardians = stacks.size();
  35. }
  36. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  37. {
  38. BlockingDialog bd (true, false);
  39. bd.player = h->getOwner();
  40. bd.soundID = soundBase::QUEST;
  41. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  42. cb->showBlockingDialog (&bd);
  43. }
  44. void CGPandoraBox::giveContentsUpToExp(const CGHeroInstance *h) const
  45. {
  46. afterSuccessfulVisit();
  47. InfoWindow iw;
  48. iw.player = h->getOwner();
  49. bool changesPrimSkill = false;
  50. for (auto & elem : primskills)
  51. {
  52. if(elem)
  53. {
  54. changesPrimSkill = true;
  55. break;
  56. }
  57. }
  58. std::vector<std::pair<SecondarySkill, ui8>> unpossessedAbilities; //ability + ability level
  59. int abilitiesRequiringSlot = 0;
  60. //filter out unnecessary secondary skills
  61. for (int i = 0; i < abilities.size(); i++)
  62. {
  63. int curLev = h->getSecSkillLevel(abilities[i]);
  64. bool abilityCanUseSlot = !curLev && ((h->secSkills.size() + abilitiesRequiringSlot) < GameConstants::SKILL_PER_HERO); //limit new abilities to number of slots
  65. if (abilityCanUseSlot)
  66. abilitiesRequiringSlot++;
  67. if ((curLev && curLev < abilityLevels[i]) || abilityCanUseSlot)
  68. {
  69. unpossessedAbilities.push_back({ abilities[i], abilityLevels[i] });
  70. }
  71. }
  72. if(gainedExp || changesPrimSkill || unpossessedAbilities.size())
  73. {
  74. TExpType expVal = h->calculateXp(gainedExp);
  75. //getText(iw,afterBattle,175,h); //wtf?
  76. iw.text.addTxt(MetaString::ADVOB_TXT, 175); //%s learns something
  77. iw.text.addReplacement(h->name);
  78. if(expVal)
  79. iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
  80. for(int i=0; i<primskills.size(); i++)
  81. if(primskills[i])
  82. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  83. for(auto abilityData : unpossessedAbilities)
  84. iw.components.push_back(Component(Component::SEC_SKILL, abilityData.first, abilityData.second, 0));
  85. cb->showInfoDialog(&iw);
  86. //give sec skills
  87. for (auto abilityData : unpossessedAbilities)
  88. cb->changeSecSkill(h, abilityData.first, abilityData.second, true);
  89. assert(h->secSkills.size() <= GameConstants::SKILL_PER_HERO);
  90. //give prim skills
  91. for(int i=0; i<primskills.size(); i++)
  92. if(primskills[i])
  93. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(i),primskills[i],false);
  94. assert(!cb->isVisitCoveredByAnotherQuery(this, h));
  95. //give exp
  96. if(expVal)
  97. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  98. }
  99. //else { } //TODO:Create information that box was empty for now, and deliver to CGPandoraBox::giveContentsAfterExp or refactor
  100. if(!cb->isVisitCoveredByAnotherQuery(this, h))
  101. giveContentsAfterExp(h);
  102. //Otherwise continuation occurs via post-level-up callback.
  103. }
  104. void CGPandoraBox::giveContentsAfterExp(const CGHeroInstance *h) const
  105. {
  106. bool hadGuardians = hasGuardians; //copy, because flag will be emptied after issuing first post-battle message
  107. std::string msg = message; //in case box is removed in the meantime
  108. InfoWindow iw;
  109. iw.player = h->getOwner();
  110. //TODO: reuse this code for Scholar skill
  111. if(spells.size())
  112. {
  113. std::set<SpellID> spellsToGive;
  114. std::vector<ConstTransitivePtr<CSpell> > * sp = &VLC->spellh->objects;
  115. auto i = spells.cbegin();
  116. while (i != spells.cend())
  117. {
  118. iw.components.clear();
  119. iw.text.clear();
  120. spellsToGive.clear();
  121. for (; i != spells.cend(); i++)
  122. {
  123. if ((*sp)[*i]->level <= h->getSecSkillLevel(SecondarySkill::WISDOM) + 2) //enough wisdom
  124. {
  125. iw.components.push_back(Component(Component::SPELL, *i, 0, 0));
  126. spellsToGive.insert(*i);
  127. }
  128. if (spellsToGive.size() == 8) //display up to 8 spells at once
  129. {
  130. break;
  131. }
  132. }
  133. if (!spellsToGive.empty())
  134. {
  135. if (spellsToGive.size() > 1)
  136. {
  137. iw.text.addTxt(MetaString::ADVOB_TXT, 188); //%s learns spells
  138. }
  139. else
  140. {
  141. iw.text.addTxt(MetaString::ADVOB_TXT, 184); //%s learns a spell
  142. }
  143. iw.text.addReplacement(h->name);
  144. cb->changeSpells(h, true, spellsToGive);
  145. cb->showInfoDialog(&iw);
  146. }
  147. }
  148. }
  149. if(manaDiff)
  150. {
  151. getText(iw,hadGuardians,manaDiff,176,177,h);
  152. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  153. cb->showInfoDialog(&iw);
  154. cb->setManaPoints(h->id, h->mana + manaDiff);
  155. }
  156. if(moraleDiff)
  157. {
  158. getText(iw,hadGuardians,moraleDiff,178,179,h);
  159. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  160. cb->showInfoDialog(&iw);
  161. GiveBonus gb;
  162. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id.getNum(),"");
  163. gb.id = h->id.getNum();
  164. cb->giveHeroBonus(&gb);
  165. }
  166. if(luckDiff)
  167. {
  168. getText(iw,hadGuardians,luckDiff,180,181,h);
  169. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  170. cb->showInfoDialog(&iw);
  171. GiveBonus gb;
  172. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id.getNum(),"");
  173. gb.id = h->id.getNum();
  174. cb->giveHeroBonus(&gb);
  175. }
  176. iw.components.clear();
  177. iw.text.clear();
  178. for(int i=0; i<resources.size(); i++)
  179. {
  180. if(resources[i] < 0)
  181. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  182. }
  183. if(iw.components.size())
  184. {
  185. getText(iw,hadGuardians,182,h);
  186. cb->showInfoDialog(&iw);
  187. }
  188. iw.components.clear();
  189. iw.text.clear();
  190. for(int i=0; i<resources.size(); i++)
  191. {
  192. if(resources[i] > 0)
  193. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  194. }
  195. if(iw.components.size())
  196. {
  197. getText(iw,hadGuardians,183,h);
  198. cb->showInfoDialog(&iw);
  199. }
  200. iw.components.clear();
  201. // getText(iw,afterBattle,183,h);
  202. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure
  203. iw.text.addReplacement(h->name);
  204. for(auto & elem : artifacts)
  205. {
  206. iw.components.push_back(Component(Component::ARTIFACT,elem,0,0));
  207. if(iw.components.size() >= 14)
  208. {
  209. cb->showInfoDialog(&iw);
  210. iw.components.clear();
  211. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure - once more?
  212. iw.text.addReplacement(h->name);
  213. }
  214. }
  215. if(iw.components.size())
  216. {
  217. cb->showInfoDialog(&iw);
  218. }
  219. for(int i=0; i<resources.size(); i++)
  220. if(resources[i])
  221. cb->giveResource(h->getOwner(),static_cast<Res::ERes>(i),resources[i]);
  222. for(auto & elem : artifacts)
  223. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[elem],ArtifactPosition::FIRST_AVAILABLE);
  224. iw.components.clear();
  225. iw.text.clear();
  226. if(creatures.stacksCount())
  227. { //this part is taken straight from creature bank
  228. MetaString loot;
  229. for(auto & elem : creatures.Slots())
  230. { //build list of joined creatures
  231. iw.components.push_back(Component(*elem.second));
  232. loot << "%s";
  233. loot.addReplacement(*elem.second);
  234. }
  235. if(creatures.stacksCount() == 1 && creatures.Slots().begin()->second->count == 1)
  236. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  237. else
  238. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  239. iw.text.addReplacement(loot.buildList());
  240. iw.text.addReplacement(h->name);
  241. cb->showInfoDialog(&iw);
  242. cb->giveCreatures(this, h, creatures, false);
  243. }
  244. if(!hasGuardians && msg.size())
  245. {
  246. iw.text << msg;
  247. cb->showInfoDialog(&iw);
  248. }
  249. }
  250. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  251. {
  252. if(afterBattle || !message.size())
  253. {
  254. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  255. iw.text.addReplacement(h->name);
  256. }
  257. else
  258. {
  259. iw.text << message;
  260. afterBattle = true;
  261. }
  262. }
  263. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  264. {
  265. iw.components.clear();
  266. iw.text.clear();
  267. if(afterBattle || !message.size())
  268. {
  269. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  270. iw.text.addReplacement(h->name);
  271. }
  272. else
  273. {
  274. iw.text << message;
  275. afterBattle = true;
  276. }
  277. }
  278. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  279. {
  280. if(result.winner == 0)
  281. {
  282. giveContentsUpToExp(hero);
  283. }
  284. }
  285. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  286. {
  287. if(answer)
  288. {
  289. if(stacksCount() > 0) //if pandora's box is protected by army
  290. {
  291. showInfoDialog(hero,16,0);
  292. cb->startBattleI(hero, this); //grants things after battle
  293. }
  294. else if(message.size() == 0 && resources.size() == 0
  295. && primskills.size() == 0 && abilities.size() == 0
  296. && abilityLevels.size() == 0 && artifacts.size() == 0
  297. && spells.size() == 0 && creatures.stacksCount() > 0
  298. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  299. {
  300. showInfoDialog(hero,15,0);
  301. cb->removeObject(this);
  302. }
  303. else //if it gives something without battle
  304. {
  305. giveContentsUpToExp(hero);
  306. }
  307. }
  308. }
  309. void CGPandoraBox::heroLevelUpDone(const CGHeroInstance *hero) const
  310. {
  311. giveContentsAfterExp(hero);
  312. }
  313. void CGPandoraBox::afterSuccessfulVisit() const
  314. {
  315. cb->removeAfterVisit(this);
  316. }
  317. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  318. {
  319. if(!(availableFor & (1 << h->tempOwner.getNum())))
  320. return;
  321. if(cb->getPlayerSettings(h->tempOwner)->playerID)
  322. {
  323. if(humanActivate)
  324. activated(h);
  325. }
  326. else if(computerActivate)
  327. activated(h);
  328. }
  329. void CGEvent::activated( const CGHeroInstance * h ) const
  330. {
  331. if(stacksCount() > 0)
  332. {
  333. InfoWindow iw;
  334. iw.player = h->tempOwner;
  335. if(message.size())
  336. iw.text << message;
  337. else
  338. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  339. cb->showInfoDialog(&iw);
  340. cb->startBattleI(h, this);
  341. }
  342. else
  343. {
  344. giveContentsUpToExp(h);
  345. }
  346. }
  347. void CGEvent::afterSuccessfulVisit() const
  348. {
  349. if(removeAfterVisit)
  350. {
  351. cb->removeAfterVisit(this);
  352. }
  353. else if(hasGuardians)
  354. hasGuardians = false;
  355. }