CGPandoraBox.cpp 9.1 KB

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