CBank.cpp 8.7 KB

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