CGPandoraBox.cpp 9.2 KB

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