CBank.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. BlockingDialog bd(true, false);
  119. bd.player = h->getOwner();
  120. bd.text.appendLocalString(EMetaText::ADVOB_TXT, 32);
  121. bd.components = getPopupComponents(h->getOwner());
  122. bd.text.replaceRawString(getObjectName());
  123. cb->showBlockingDialog(this, &bd);
  124. }
  125. void CBank::doVisit(const CGHeroInstance * hero) const
  126. {
  127. InfoWindow iw;
  128. iw.type = EInfoWindowMode::AUTO;
  129. iw.player = hero->getOwner();
  130. MetaString loot;
  131. if (!bankConfig)
  132. {
  133. iw.text.appendRawString(VLC->generaltexth->advobtxt[33]);// This was X, now is completely empty
  134. iw.text.replaceRawString(getObjectName());
  135. cb->showInfoDialog(&iw);
  136. }
  137. //grant resources
  138. if (bankConfig)
  139. {
  140. for (GameResID it : GameResID::ALL_RESOURCES())
  141. {
  142. if (bankConfig->resources[it] != 0)
  143. {
  144. iw.components.emplace_back(ComponentType::RESOURCE, it, bankConfig->resources[it]);
  145. loot.appendRawString("%d %s");
  146. loot.replaceNumber(bankConfig->resources[it]);
  147. loot.replaceName(it);
  148. cb->giveResource(hero->getOwner(), it, bankConfig->resources[it]);
  149. }
  150. }
  151. //grant artifacts
  152. for (auto & elem : bankConfig->artifacts)
  153. {
  154. iw.components.emplace_back(ComponentType::ARTIFACT, elem);
  155. loot.appendRawString("%s");
  156. loot.replaceName(elem);
  157. cb->giveHeroNewArtifact(hero, elem, ArtifactPosition::FIRST_AVAILABLE);
  158. }
  159. //display loot
  160. if (!iw.components.empty())
  161. {
  162. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 34);
  163. const auto * strongest = boost::range::max_element(bankConfig->guards, [](const CStackBasicDescriptor & a, const CStackBasicDescriptor & b)
  164. {
  165. return a.type->getFightValue() < b.type->getFightValue();
  166. })->type;
  167. iw.text.replaceNamePlural(strongest->getId());
  168. iw.text.replaceRawString(loot.buildList());
  169. cb->showInfoDialog(&iw);
  170. }
  171. loot.clear();
  172. iw.components.clear();
  173. iw.text.clear();
  174. if (!bankConfig->spells.empty())
  175. {
  176. std::set<SpellID> spells;
  177. bool noWisdom = false;
  178. for(const SpellID & spellId : bankConfig->spells)
  179. {
  180. const auto * spell = spellId.toEntity(VLC);
  181. iw.text.appendName(spellId);
  182. if(spell->getLevel() <= hero->maxSpellLevel())
  183. {
  184. if(hero->canLearnSpell(spell))
  185. {
  186. spells.insert(spellId);
  187. iw.components.emplace_back(ComponentType::SPELL, spellId);
  188. }
  189. }
  190. else
  191. noWisdom = true;
  192. }
  193. if (!hero->getArt(ArtifactPosition::SPELLBOOK))
  194. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 109); //no spellbook
  195. else if(noWisdom)
  196. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 108); //no expert Wisdom
  197. if(!iw.components.empty() || !iw.text.toString().empty())
  198. cb->showInfoDialog(&iw);
  199. if(!spells.empty())
  200. cb->changeSpells(hero, true, spells);
  201. }
  202. iw.components.clear();
  203. iw.text.clear();
  204. //grant creatures
  205. CCreatureSet ourArmy;
  206. for(const auto & slot : bankConfig->creatures)
  207. {
  208. ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->getId()), slot.type->getId(), slot.count);
  209. }
  210. for(const auto & elem : ourArmy.Slots())
  211. {
  212. iw.components.emplace_back(ComponentType::CREATURE, elem.second->getId(), elem.second->getCount());
  213. loot.appendRawString("%s");
  214. loot.replaceName(*elem.second);
  215. }
  216. if(ourArmy.stacksCount())
  217. {
  218. if(ourArmy.stacksCount() == 1 && ourArmy.Slots().begin()->second->count == 1)
  219. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 185);
  220. else
  221. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 186);
  222. iw.text.replaceRawString(loot.buildList());
  223. iw.text.replaceRawString(hero->getNameTranslated());
  224. cb->showInfoDialog(&iw);
  225. cb->giveCreatures(this, hero, ourArmy, false);
  226. }
  227. cb->setObjPropertyValue(id, ObjProperty::BANK_CLEAR); //bc = nullptr
  228. }
  229. }
  230. void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  231. {
  232. if (result.winner == BattleSide::ATTACKER)
  233. {
  234. doVisit(hero);
  235. }
  236. }
  237. void CBank::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
  238. {
  239. if (answer)
  240. {
  241. if (bankConfig) // not looted bank
  242. cb->startBattleI(hero, this, !regularUnitPlacement);
  243. else
  244. doVisit(hero);
  245. }
  246. }
  247. VCMI_LIB_NAMESPACE_END