CBank.cpp 9.9 KB

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