CBank.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * CBank.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 "CBank.h"
  12. #include "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CSoundBase.h"
  15. #include "CommonConstructors.h"
  16. #include "../CSpellHandler.h"
  17. #include "../IGameCallback.h"
  18. #include "../CGameState.h"
  19. using namespace boost::assign;
  20. ///helpers
  21. static std::string & visitedTxt(const bool visited)
  22. {
  23. int id = visited ? 352 : 353;
  24. return VLC->generaltexth->allTexts[id];
  25. }
  26. CBank::CBank()
  27. {
  28. }
  29. CBank::~CBank()
  30. {
  31. }
  32. void CBank::initObj()
  33. {
  34. daycounter = 0;
  35. resetDuration = 0;
  36. VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, cb->gameState()->getRandomGenerator());
  37. }
  38. std::string CBank::getHoverText(PlayerColor player) const
  39. {
  40. // TODO: USE BANK_SPECIFIC NAMES
  41. // TODO: record visited players
  42. bool visited = (bc == nullptr);
  43. return visitedTxt(visited);
  44. }
  45. void CBank::setConfig(const BankConfig & config)
  46. {
  47. bc.reset(new BankConfig(config));
  48. clear(); // remove all stacks, if any
  49. for (auto & stack : config.guards)
  50. setCreature (SlotID(stacksCount()), stack.type->idNumber, stack.count);
  51. }
  52. void CBank::setPropertyDer (ui8 what, ui32 val)
  53. {
  54. switch (what)
  55. {
  56. case ObjProperty::BANK_DAYCOUNTER: //daycounter
  57. daycounter+=val;
  58. break;
  59. case ObjProperty::BANK_RESET:
  60. initObj();
  61. daycounter = 1; //yes, 1 since "today" daycounter won't be incremented
  62. break;
  63. case ObjProperty::BANK_CLEAR:
  64. bc.reset();
  65. break;
  66. }
  67. }
  68. void CBank::newTurn() const
  69. {
  70. if (bc == nullptr)
  71. {
  72. if (resetDuration != 0)
  73. {
  74. if (daycounter >= resetDuration)
  75. cb->setObjProperty (id, ObjProperty::BANK_RESET, 0); //daycounter 0
  76. else
  77. cb->setObjProperty (id, ObjProperty::BANK_DAYCOUNTER, 1); //daycounter++
  78. }
  79. }
  80. }
  81. bool CBank::wasVisited (PlayerColor player) const
  82. {
  83. return !bc; //FIXME: player A should not know about visit done by player B
  84. }
  85. void CBank::onHeroVisit (const CGHeroInstance * h) const
  86. {
  87. if (bc)
  88. {
  89. int banktext = 0;
  90. ui16 soundID = soundBase::ROGUE;
  91. switch (ID)
  92. {
  93. case Obj::CREATURE_BANK:
  94. banktext = 32;
  95. break;
  96. case Obj::DERELICT_SHIP:
  97. banktext = 41;
  98. break;
  99. case Obj::DRAGON_UTOPIA:
  100. banktext = 47;
  101. break;
  102. case Obj::CRYPT:
  103. banktext = 119;
  104. break;
  105. case Obj::SHIPWRECK:
  106. banktext = 122;
  107. break;
  108. case Obj::PYRAMID:
  109. soundID = soundBase::MYSTERY;
  110. banktext = 105;
  111. break;
  112. }
  113. BlockingDialog bd (true, false);
  114. bd.player = h->getOwner();
  115. bd.soundID = soundID;
  116. bd.text.addTxt(MetaString::ADVOB_TXT, banktext);
  117. //if (ID == Obj::CREATURE_BANK)
  118. // bd.text.addReplacement(VLC->objh->creBanksNames[index]); // FIXME: USE BANK SPECIFIC NAMES
  119. cb->showBlockingDialog (&bd);
  120. }
  121. else
  122. {
  123. InfoWindow iw;
  124. iw.soundID = soundBase::GRAVEYARD;
  125. iw.player = h->getOwner();
  126. if (ID == Obj::PYRAMID) // You come upon the pyramid ... pyramid is completely empty.
  127. {
  128. iw.text << VLC->generaltexth->advobtxt[107];
  129. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  130. GiveBonus gb;
  131. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id.getNum(),VLC->generaltexth->arraytxt[70]);
  132. gb.id = h->id.getNum();
  133. cb->giveHeroBonus(&gb);
  134. }
  135. else
  136. {
  137. iw.text << VLC->generaltexth->advobtxt[33];// This was X, now is completely empty
  138. //iw.text.addReplacement(VLC->objh->creBanksNames[index]); // FIXME: USE BANK SPECIFIC NAMES
  139. }
  140. cb->showInfoDialog(&iw);
  141. }
  142. }
  143. void CBank::doVisit(const CGHeroInstance * hero) const
  144. {
  145. int textID = -1;
  146. InfoWindow iw;
  147. iw.player = hero->getOwner();
  148. MetaString loot;
  149. switch (ID)
  150. {
  151. case Obj::CREATURE_BANK:
  152. case Obj::DRAGON_UTOPIA:
  153. textID = 34;
  154. break;
  155. case Obj::DERELICT_SHIP:
  156. if (!bc)
  157. textID = 43;
  158. else
  159. {
  160. GiveBonus gbonus;
  161. gbonus.id = hero->id.getNum();
  162. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  163. gbonus.bonus.source = Bonus::OBJECT;
  164. gbonus.bonus.sid = ID;
  165. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  166. gbonus.bonus.type = Bonus::MORALE;
  167. gbonus.bonus.val = -1;
  168. cb->giveHeroBonus(&gbonus);
  169. textID = 42;
  170. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  171. }
  172. break;
  173. case Obj::CRYPT:
  174. if (bc)
  175. textID = 121;
  176. else
  177. {
  178. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  179. GiveBonus gbonus;
  180. gbonus.id = hero->id.getNum();
  181. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  182. gbonus.bonus.source = Bonus::OBJECT;
  183. gbonus.bonus.sid = ID;
  184. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  185. gbonus.bonus.type = Bonus::MORALE;
  186. gbonus.bonus.val = -1;
  187. cb->giveHeroBonus(&gbonus);
  188. textID = 120;
  189. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  190. }
  191. break;
  192. case Obj::SHIPWRECK:
  193. if (bc)
  194. textID = 124;
  195. else
  196. textID = 123;
  197. break;
  198. case Obj::PYRAMID:
  199. textID = 106;
  200. }
  201. //grant resources
  202. if (bc)
  203. {
  204. for (int it = 0; it < bc->resources.size(); it++)
  205. {
  206. if (bc->resources[it] != 0)
  207. {
  208. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  209. loot << "%d %s";
  210. loot.addReplacement(iw.components.back().val);
  211. loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
  212. cb->giveResource (hero->getOwner(), static_cast<Res::ERes>(it), bc->resources[it]);
  213. }
  214. }
  215. //grant artifacts
  216. for (auto & elem : bc->artifacts)
  217. {
  218. iw.components.push_back (Component (Component::ARTIFACT, elem, 0, 0));
  219. loot << "%s";
  220. loot.addReplacement(MetaString::ART_NAMES, elem);
  221. cb->giveHeroNewArtifact (hero, VLC->arth->artifacts[elem], ArtifactPosition::FIRST_AVAILABLE);
  222. }
  223. //display loot
  224. if (!iw.components.empty())
  225. {
  226. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  227. if (textID == 34)
  228. {
  229. const CCreature * strongest = boost::range::max_element(bc->guards, [](const CStackBasicDescriptor & a, const CStackBasicDescriptor & b)
  230. {
  231. return a.type->fightValue < b.type->fightValue;
  232. })->type;
  233. iw.text.addReplacement(MetaString::CRE_PL_NAMES, strongest->idNumber);
  234. iw.text.addReplacement(loot.buildList());
  235. }
  236. cb->showInfoDialog(&iw);
  237. }
  238. if (!bc->spells.empty())
  239. {
  240. std::set<SpellID> spells;
  241. bool noWisdom = false;
  242. for (SpellID spell : bc->spells)
  243. {
  244. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  245. if (VLC->spellh->objects[spell]->level <= hero->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  246. {
  247. spells.insert(spell);
  248. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  249. }
  250. else
  251. noWisdom = true;
  252. }
  253. if (!hero->getArt(ArtifactPosition::SPELLBOOK))
  254. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  255. else if (noWisdom)
  256. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  257. if (spells.empty())
  258. cb->changeSpells (hero, true, spells);
  259. }
  260. loot.clear();
  261. iw.components.clear();
  262. iw.text.clear();
  263. //grant creatures
  264. CCreatureSet ourArmy;
  265. for (auto slot : bc->creatures)
  266. {
  267. ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->idNumber), slot.type->idNumber, slot.count);
  268. }
  269. for (auto & elem : ourArmy.Slots())
  270. {
  271. iw.components.push_back(Component(*elem.second));
  272. loot << "%s";
  273. loot.addReplacement(*elem.second);
  274. }
  275. if (ourArmy.Slots().size())
  276. {
  277. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second->count == 1)
  278. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  279. else
  280. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  281. iw.text.addReplacement(loot.buildList());
  282. iw.text.addReplacement(hero->name);
  283. cb->showInfoDialog(&iw);
  284. cb->giveCreatures(this, hero, ourArmy, false);
  285. }
  286. cb->setObjProperty (id, ObjProperty::BANK_CLEAR, 0); //bc = nullptr
  287. }
  288. }
  289. void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  290. {
  291. if (result.winner == 0)
  292. {
  293. doVisit(hero);
  294. }
  295. }
  296. void CBank::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  297. {
  298. if (answer)
  299. {
  300. if (bc) // not looted bank
  301. cb->startBattleI(hero, this, true);
  302. else
  303. doVisit(hero);
  304. }
  305. }