CBank.cpp 9.7 KB

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