CBank.cpp 9.4 KB

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