CGPandoraBox.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "../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()
  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. cb->removeAfterVisit(this);
  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. if(gainedExp || changesPrimSkill || abilities.size())
  59. {
  60. TExpType expVal = h->calculateXp(gainedExp);
  61. //getText(iw,afterBattle,175,h); //wtf?
  62. iw.text.addTxt(MetaString::ADVOB_TXT, 175); //%s learns something
  63. iw.text.addReplacement(h->name);
  64. if(expVal)
  65. iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
  66. for(int i=0; i<primskills.size(); i++)
  67. if(primskills[i])
  68. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  69. for(int i=0; i<abilities.size(); i++)
  70. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  71. cb->showInfoDialog(&iw);
  72. //give sec skills
  73. for(int i=0; i<abilities.size(); i++)
  74. {
  75. int curLev = h->getSecSkillLevel(abilities[i]);
  76. if( (curLev && curLev < abilityLevels[i]) || (h->canLearnSkill() ))
  77. {
  78. cb->changeSecSkill(h,abilities[i],abilityLevels[i],true);
  79. }
  80. }
  81. //give prim skills
  82. for(int i=0; i<primskills.size(); i++)
  83. if(primskills[i])
  84. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(i),primskills[i],false);
  85. assert(!cb->isVisitCoveredByAnotherQuery(this, h));
  86. //give exp
  87. if(expVal)
  88. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
  89. }
  90. if(!cb->isVisitCoveredByAnotherQuery(this, h))
  91. giveContentsAfterExp(h);
  92. //Otherwise continuation occurs via post-level-up callback.
  93. }
  94. void CGPandoraBox::giveContentsAfterExp(const CGHeroInstance *h) const
  95. {
  96. bool hadGuardians = hasGuardians; //copy, because flag will be emptied after issuing first post-battle message
  97. std::string msg = message; //in case box is removed in the meantime
  98. InfoWindow iw;
  99. iw.player = h->getOwner();
  100. if(spells.size())
  101. {
  102. std::set<SpellID> spellsToGive;
  103. iw.components.clear();
  104. if (spells.size() > 1)
  105. {
  106. iw.text.addTxt(MetaString::ADVOB_TXT, 188); //%s learns spells
  107. }
  108. else
  109. {
  110. iw.text.addTxt(MetaString::ADVOB_TXT, 184); //%s learns a spell
  111. }
  112. iw.text.addReplacement(h->name);
  113. std::vector<ConstTransitivePtr<CSpell> > * sp = &VLC->spellh->objects;
  114. for(auto i=spells.cbegin(); i != spells.cend(); i++)
  115. {
  116. if ((*sp)[*i]->level <= h->getSecSkillLevel(SecondarySkill::WISDOM) + 2) //enough wisdom
  117. {
  118. iw.components.push_back(Component(Component::SPELL,*i,0,0));
  119. spellsToGive.insert(*i);
  120. }
  121. }
  122. if(!spellsToGive.empty())
  123. {
  124. cb->changeSpells(h,true,spellsToGive);
  125. cb->showInfoDialog(&iw);
  126. }
  127. }
  128. if(manaDiff)
  129. {
  130. getText(iw,hadGuardians,manaDiff,176,177,h);
  131. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  132. cb->showInfoDialog(&iw);
  133. cb->setManaPoints(h->id, h->mana + manaDiff);
  134. }
  135. if(moraleDiff)
  136. {
  137. getText(iw,hadGuardians,moraleDiff,178,179,h);
  138. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  139. cb->showInfoDialog(&iw);
  140. GiveBonus gb;
  141. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id.getNum(),"");
  142. gb.id = h->id.getNum();
  143. cb->giveHeroBonus(&gb);
  144. }
  145. if(luckDiff)
  146. {
  147. getText(iw,hadGuardians,luckDiff,180,181,h);
  148. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  149. cb->showInfoDialog(&iw);
  150. GiveBonus gb;
  151. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id.getNum(),"");
  152. gb.id = h->id.getNum();
  153. cb->giveHeroBonus(&gb);
  154. }
  155. iw.components.clear();
  156. iw.text.clear();
  157. for(int i=0; i<resources.size(); i++)
  158. {
  159. if(resources[i] < 0)
  160. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  161. }
  162. if(iw.components.size())
  163. {
  164. getText(iw,hadGuardians,182,h);
  165. cb->showInfoDialog(&iw);
  166. }
  167. iw.components.clear();
  168. iw.text.clear();
  169. for(int i=0; i<resources.size(); i++)
  170. {
  171. if(resources[i] > 0)
  172. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  173. }
  174. if(iw.components.size())
  175. {
  176. getText(iw,hadGuardians,183,h);
  177. cb->showInfoDialog(&iw);
  178. }
  179. iw.components.clear();
  180. // getText(iw,afterBattle,183,h);
  181. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure
  182. iw.text.addReplacement(h->name);
  183. for(auto & elem : artifacts)
  184. {
  185. iw.components.push_back(Component(Component::ARTIFACT,elem,0,0));
  186. if(iw.components.size() >= 14)
  187. {
  188. cb->showInfoDialog(&iw);
  189. iw.components.clear();
  190. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure - once more?
  191. iw.text.addReplacement(h->name);
  192. }
  193. }
  194. if(iw.components.size())
  195. {
  196. cb->showInfoDialog(&iw);
  197. }
  198. for(int i=0; i<resources.size(); i++)
  199. if(resources[i])
  200. cb->giveResource(h->getOwner(),static_cast<Res::ERes>(i),resources[i]);
  201. for(auto & elem : artifacts)
  202. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[elem],ArtifactPosition::FIRST_AVAILABLE);
  203. iw.components.clear();
  204. iw.text.clear();
  205. if (creatures.Slots().size())
  206. { //this part is taken straight from creature bank
  207. MetaString loot;
  208. for(auto & elem : creatures.Slots())
  209. { //build list of joined creatures
  210. iw.components.push_back(Component(*elem.second));
  211. loot << "%s";
  212. loot.addReplacement(*elem.second);
  213. }
  214. if (creatures.Slots().size() == 1 && creatures.Slots().begin()->second->count == 1)
  215. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  216. else
  217. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  218. iw.text.addReplacement(loot.buildList());
  219. iw.text.addReplacement(h->name);
  220. cb->showInfoDialog(&iw);
  221. cb->giveCreatures(this, h, creatures, true);
  222. }
  223. if(!hasGuardians && msg.size())
  224. {
  225. iw.text << msg;
  226. cb->showInfoDialog(&iw);
  227. }
  228. }
  229. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  230. {
  231. if(afterBattle || !message.size())
  232. {
  233. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  234. iw.text.addReplacement(h->name);
  235. }
  236. else
  237. {
  238. iw.text << message;
  239. afterBattle = true;
  240. }
  241. }
  242. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  243. {
  244. iw.components.clear();
  245. iw.text.clear();
  246. if(afterBattle || !message.size())
  247. {
  248. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  249. iw.text.addReplacement(h->name);
  250. }
  251. else
  252. {
  253. iw.text << message;
  254. afterBattle = true;
  255. }
  256. }
  257. void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  258. {
  259. if(result.winner)
  260. return;
  261. giveContentsUpToExp(hero);
  262. }
  263. void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  264. {
  265. if (answer)
  266. {
  267. if (stacksCount() > 0) //if pandora's box is protected by army
  268. {
  269. showInfoDialog(hero,16,0);
  270. cb->startBattleI(hero, this); //grants things after battle
  271. }
  272. else if (message.size() == 0 && resources.size() == 0
  273. && primskills.size() == 0 && abilities.size() == 0
  274. && abilityLevels.size() == 0 && artifacts.size() == 0
  275. && spells.size() == 0 && creatures.Slots().size() > 0
  276. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  277. {
  278. showInfoDialog(hero,15,0);
  279. cb->removeObject(this);
  280. }
  281. else //if it gives something without battle
  282. {
  283. giveContentsUpToExp(hero);
  284. }
  285. }
  286. }
  287. void CGPandoraBox::heroLevelUpDone(const CGHeroInstance *hero) const
  288. {
  289. giveContentsAfterExp(hero);
  290. }
  291. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  292. {
  293. if(!(availableFor & (1 << h->tempOwner.getNum())))
  294. return;
  295. if(cb->getPlayerSettings(h->tempOwner)->playerID)
  296. {
  297. if(humanActivate)
  298. activated(h);
  299. }
  300. else if(computerActivate)
  301. activated(h);
  302. }
  303. void CGEvent::activated( const CGHeroInstance * h ) const
  304. {
  305. if(stacksCount() > 0)
  306. {
  307. InfoWindow iw;
  308. iw.player = h->tempOwner;
  309. if(message.size())
  310. iw.text << message;
  311. else
  312. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  313. cb->showInfoDialog(&iw);
  314. cb->startBattleI(h, this);
  315. }
  316. else
  317. {
  318. giveContentsUpToExp(h);
  319. }
  320. }