CBank.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "../spells/CSpellHandler.h"
  17. #include "../IGameCallback.h"
  18. #include "../CGameState.h"
  19. ///helpers
  20. static std::string & visitedTxt(const bool visited)
  21. {
  22. int id = visited ? 352 : 353;
  23. return VLC->generaltexth->allTexts[id];
  24. }
  25. CBank::CBank()
  26. {
  27. daycounter = 0;
  28. resetDuration = 0;
  29. }
  30. CBank::~CBank()
  31. {
  32. }
  33. void CBank::initObj(CRandomGenerator & rand)
  34. {
  35. daycounter = 0;
  36. resetDuration = 0;
  37. VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, rand);
  38. }
  39. std::string CBank::getHoverText(PlayerColor player) const
  40. {
  41. // TODO: record visited players
  42. return getObjectName() + " " + visitedTxt(bc == nullptr);
  43. }
  44. void CBank::setConfig(const BankConfig & config)
  45. {
  46. bc.reset(new BankConfig(config));
  47. clear(); // remove all stacks, if any
  48. for (auto & stack : config.guards)
  49. setCreature (SlotID(stacksCount()), stack.type->idNumber, stack.count);
  50. }
  51. void CBank::setPropertyDer (ui8 what, ui32 val)
  52. {
  53. switch (what)
  54. {
  55. case ObjProperty::BANK_DAYCOUNTER: //daycounter
  56. daycounter+=val;
  57. break;
  58. case ObjProperty::BANK_RESET:
  59. // FIXME: Object reset must be done by separate netpack from server
  60. initObj(cb->gameState()->getRandomGenerator());
  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(CRandomGenerator & rand) 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(getObjectName());
  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(getObjectName());
  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. loot.clear();
  239. iw.components.clear();
  240. iw.text.clear();
  241. if (!bc->spells.empty())
  242. {
  243. std::set<SpellID> spells;
  244. bool noWisdom = false;
  245. for(const SpellID & spellId : bc->spells)
  246. {
  247. const CSpell * spell = spellId.toSpell();
  248. iw.text.addTxt (MetaString::SPELL_NAME, spellId);
  249. if(spell->level <= hero->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  250. {
  251. if(hero->canLearnSpell(spell))
  252. {
  253. spells.insert(spellId);
  254. iw.components.push_back(Component (Component::SPELL, spellId, 0, 0));
  255. }
  256. }
  257. else
  258. noWisdom = true;
  259. }
  260. if (!hero->getArt(ArtifactPosition::SPELLBOOK))
  261. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  262. else if (noWisdom)
  263. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  264. if(!iw.components.empty() || !iw.text.toString().empty())
  265. cb->showInfoDialog(&iw);
  266. if(!spells.empty())
  267. cb->changeSpells (hero, true, spells);
  268. }
  269. iw.components.clear();
  270. iw.text.clear();
  271. //grant creatures
  272. CCreatureSet ourArmy;
  273. for (auto slot : bc->creatures)
  274. {
  275. ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->idNumber), slot.type->idNumber, slot.count);
  276. }
  277. for (auto & elem : ourArmy.Slots())
  278. {
  279. iw.components.push_back(Component(*elem.second));
  280. loot << "%s";
  281. loot.addReplacement(*elem.second);
  282. }
  283. if(ourArmy.stacksCount())
  284. {
  285. if(ourArmy.stacksCount() == 1 && ourArmy.Slots().begin()->second->count == 1)
  286. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  287. else
  288. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  289. iw.text.addReplacement(loot.buildList());
  290. iw.text.addReplacement(hero->name);
  291. cb->showInfoDialog(&iw);
  292. cb->giveCreatures(this, hero, ourArmy, false);
  293. }
  294. cb->setObjProperty (id, ObjProperty::BANK_CLEAR, 0); //bc = nullptr
  295. }
  296. }
  297. void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  298. {
  299. if (result.winner == 0)
  300. {
  301. doVisit(hero);
  302. }
  303. }
  304. void CBank::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  305. {
  306. if (answer)
  307. {
  308. if (bc) // not looted bank
  309. cb->startBattleI(hero, this, true);
  310. else
  311. doVisit(hero);
  312. }
  313. }