CBank.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. int banktext = 0;
  88. switch (ID)
  89. {
  90. case Obj::DERELICT_SHIP:
  91. banktext = 41;
  92. break;
  93. case Obj::DRAGON_UTOPIA:
  94. banktext = 47;
  95. break;
  96. case Obj::CRYPT:
  97. banktext = 119;
  98. break;
  99. case Obj::SHIPWRECK:
  100. banktext = 122;
  101. break;
  102. case Obj::PYRAMID:
  103. banktext = 105;
  104. break;
  105. case Obj::CREATURE_BANK:
  106. default:
  107. banktext = 32;
  108. break;
  109. }
  110. BlockingDialog bd(true, false);
  111. bd.player = h->getOwner();
  112. bd.soundID = soundBase::invalid; // Sound is handled in json files, else two sounds are played
  113. bd.text.addTxt(MetaString::ADVOB_TXT, banktext);
  114. if (banktext == 32)
  115. bd.text.addReplacement(getObjectName());
  116. cb->showBlockingDialog(&bd);
  117. }
  118. void CBank::doVisit(const CGHeroInstance * hero) const
  119. {
  120. int textID = -1;
  121. InfoWindow iw;
  122. iw.player = hero->getOwner();
  123. MetaString loot;
  124. if (bc)
  125. {
  126. switch (ID)
  127. {
  128. case Obj::DERELICT_SHIP:
  129. textID = 43;
  130. break;
  131. case Obj::CRYPT:
  132. textID = 121;
  133. break;
  134. case Obj::SHIPWRECK:
  135. textID = 124;
  136. break;
  137. case Obj::PYRAMID:
  138. textID = 106;
  139. break;
  140. case Obj::CREATURE_BANK:
  141. case Obj::DRAGON_UTOPIA:
  142. default:
  143. textID = 34;
  144. break;
  145. }
  146. }
  147. else
  148. {
  149. switch (ID)
  150. {
  151. case Obj::SHIPWRECK:
  152. case Obj::DERELICT_SHIP:
  153. case Obj::CRYPT:
  154. {
  155. GiveBonus gbonus;
  156. gbonus.id = hero->id.getNum();
  157. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  158. gbonus.bonus.source = Bonus::OBJECT;
  159. gbonus.bonus.sid = ID;
  160. gbonus.bonus.type = Bonus::MORALE;
  161. gbonus.bonus.val = -1;
  162. switch (ID)
  163. {
  164. case Obj::SHIPWRECK:
  165. textID = 123;
  166. gbonus.bdescr << VLC->generaltexth->arraytxt[99];
  167. break;
  168. case Obj::DERELICT_SHIP:
  169. textID = 42;
  170. gbonus.bdescr << VLC->generaltexth->arraytxt[101];
  171. break;
  172. case Obj::CRYPT:
  173. textID = 120;
  174. gbonus.bdescr << VLC->generaltexth->arraytxt[98];
  175. break;
  176. }
  177. cb->giveHeroBonus(&gbonus);
  178. iw.components.push_back(Component(Component::MORALE, 0, -1, 0));
  179. iw.soundID = soundBase::GRAVEYARD;
  180. break;
  181. }
  182. case Obj::PYRAMID:
  183. {
  184. GiveBonus gb;
  185. gb.bonus = Bonus(Bonus::ONE_BATTLE, Bonus::LUCK, Bonus::OBJECT, -2, id.getNum(), VLC->generaltexth->arraytxt[70]);
  186. gb.id = hero->id.getNum();
  187. cb->giveHeroBonus(&gb);
  188. textID = 107;
  189. iw.components.push_back(Component(Component::LUCK, 0, -2, 0));
  190. break;
  191. }
  192. case Obj::CREATURE_BANK:
  193. case Obj::DRAGON_UTOPIA:
  194. default:
  195. iw.text << VLC->generaltexth->advobtxt[33];// This was X, now is completely empty
  196. iw.text.addReplacement(getObjectName());
  197. }
  198. if(textID != -1)
  199. {
  200. iw.text.addTxt(MetaString::ADVOB_TXT, textID);
  201. }
  202. cb->showInfoDialog(&iw);
  203. }
  204. //grant resources
  205. if (bc)
  206. {
  207. for (int it = 0; it < bc->resources.size(); it++)
  208. {
  209. if (bc->resources[it] != 0)
  210. {
  211. iw.components.push_back(Component(Component::RESOURCE, it, bc->resources[it], 0));
  212. loot << "%d %s";
  213. loot.addReplacement(iw.components.back().val);
  214. loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
  215. cb->giveResource(hero->getOwner(), static_cast<Res::ERes>(it), bc->resources[it]);
  216. }
  217. }
  218. //grant artifacts
  219. for (auto & elem : bc->artifacts)
  220. {
  221. iw.components.push_back(Component(Component::ARTIFACT, elem, 0, 0));
  222. loot << "%s";
  223. loot.addReplacement(MetaString::ART_NAMES, elem);
  224. cb->giveHeroNewArtifact(hero, VLC->arth->artifacts[elem], ArtifactPosition::FIRST_AVAILABLE);
  225. }
  226. //display loot
  227. if (!iw.components.empty())
  228. {
  229. iw.text.addTxt(MetaString::ADVOB_TXT, textID);
  230. if (textID == 34)
  231. {
  232. const CCreature * strongest = boost::range::max_element(bc->guards, [](const CStackBasicDescriptor & a, const CStackBasicDescriptor & b)
  233. {
  234. return a.type->fightValue < b.type->fightValue;
  235. })->type;
  236. iw.text.addReplacement(MetaString::CRE_PL_NAMES, strongest->idNumber);
  237. iw.text.addReplacement(loot.buildList());
  238. }
  239. cb->showInfoDialog(&iw);
  240. }
  241. loot.clear();
  242. iw.components.clear();
  243. iw.text.clear();
  244. if (!bc->spells.empty())
  245. {
  246. std::set<SpellID> spells;
  247. bool noWisdom = false;
  248. if(textID == 106)
  249. {
  250. iw.text.addTxt(MetaString::ADVOB_TXT, textID); //pyramid
  251. }
  252. for(const SpellID & spellId : bc->spells)
  253. {
  254. const CSpell * spell = spellId.toSpell();
  255. iw.text.addTxt(MetaString::SPELL_NAME, spellId);
  256. if(spell->level <= hero->maxSpellLevel())
  257. {
  258. if(hero->canLearnSpell(spell))
  259. {
  260. spells.insert(spellId);
  261. iw.components.push_back(Component(Component::SPELL, spellId, 0, 0));
  262. }
  263. }
  264. else
  265. noWisdom = true;
  266. }
  267. if (!hero->getArt(ArtifactPosition::SPELLBOOK))
  268. iw.text.addTxt(MetaString::ADVOB_TXT, 109); //no spellbook
  269. else if(noWisdom)
  270. iw.text.addTxt(MetaString::ADVOB_TXT, 108); //no expert Wisdom
  271. if(!iw.components.empty() || !iw.text.toString().empty())
  272. cb->showInfoDialog(&iw);
  273. if(!spells.empty())
  274. cb->changeSpells(hero, true, spells);
  275. }
  276. iw.components.clear();
  277. iw.text.clear();
  278. //grant creatures
  279. CCreatureSet ourArmy;
  280. for (auto slot : bc->creatures)
  281. {
  282. ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->idNumber), slot.type->idNumber, slot.count);
  283. }
  284. for (auto & elem : ourArmy.Slots())
  285. {
  286. iw.components.push_back(Component(*elem.second));
  287. loot << "%s";
  288. loot.addReplacement(*elem.second);
  289. }
  290. if(ourArmy.stacksCount())
  291. {
  292. if(ourArmy.stacksCount() == 1 && ourArmy.Slots().begin()->second->count == 1)
  293. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  294. else
  295. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  296. iw.text.addReplacement(loot.buildList());
  297. iw.text.addReplacement(hero->name);
  298. cb->showInfoDialog(&iw);
  299. cb->giveCreatures(this, hero, ourArmy, false);
  300. }
  301. cb->setObjProperty(id, ObjProperty::BANK_CLEAR, 0); //bc = nullptr
  302. }
  303. }
  304. void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  305. {
  306. if (result.winner == 0)
  307. {
  308. doVisit(hero);
  309. }
  310. }
  311. void CBank::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  312. {
  313. if (answer)
  314. {
  315. if (bc) // not looted bank
  316. cb->startBattleI(hero, this, true);
  317. else
  318. doVisit(hero);
  319. }
  320. }