CBank.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. if (bc == 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. bc = 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. }
  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_RESET:
  89. // FIXME: Object reset must be done by separate netpack from server
  90. initObj(cb->gameState()->getRandomGenerator());
  91. daycounter = 1; //yes, 1 since "today" daycounter won't be incremented
  92. break;
  93. case ObjProperty::BANK_CLEAR:
  94. bc.reset();
  95. break;
  96. }
  97. }
  98. void CBank::newTurn(CRandomGenerator & rand) const
  99. {
  100. if (bc == nullptr)
  101. {
  102. if (resetDuration != 0)
  103. {
  104. if (daycounter >= resetDuration)
  105. cb->setObjPropertyValue(id, ObjProperty::BANK_RESET); //daycounter 0
  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. int banktext = 0;
  120. switch (ID.toEnum())
  121. {
  122. case Obj::DERELICT_SHIP:
  123. banktext = 41;
  124. break;
  125. case Obj::DRAGON_UTOPIA:
  126. banktext = 47;
  127. break;
  128. case Obj::CRYPT:
  129. banktext = 119;
  130. break;
  131. case Obj::SHIPWRECK:
  132. banktext = 122;
  133. break;
  134. case Obj::PYRAMID:
  135. banktext = 105;
  136. break;
  137. case Obj::CREATURE_BANK:
  138. default:
  139. banktext = 32;
  140. break;
  141. }
  142. BlockingDialog bd(true, false);
  143. bd.player = h->getOwner();
  144. bd.soundID = soundBase::invalid; // Sound is handled in json files, else two sounds are played
  145. bd.text.appendLocalString(EMetaText::ADVOB_TXT, banktext);
  146. bd.components = getPopupComponents(h->getOwner());
  147. if (banktext == 32)
  148. bd.text.replaceRawString(getObjectName());
  149. cb->showBlockingDialog(&bd);
  150. }
  151. void CBank::doVisit(const CGHeroInstance * hero) const
  152. {
  153. int textID = -1;
  154. InfoWindow iw;
  155. iw.type = EInfoWindowMode::AUTO;
  156. iw.player = hero->getOwner();
  157. MetaString loot;
  158. if (bc)
  159. {
  160. switch (ID.toEnum())
  161. {
  162. case Obj::DERELICT_SHIP:
  163. textID = 43;
  164. break;
  165. case Obj::CRYPT:
  166. textID = 121;
  167. break;
  168. case Obj::SHIPWRECK:
  169. textID = 124;
  170. break;
  171. case Obj::PYRAMID:
  172. textID = 106;
  173. break;
  174. case Obj::CREATURE_BANK:
  175. case Obj::DRAGON_UTOPIA:
  176. default:
  177. textID = 34;
  178. break;
  179. }
  180. }
  181. else
  182. {
  183. switch (ID.toEnum())
  184. {
  185. case Obj::SHIPWRECK:
  186. case Obj::DERELICT_SHIP:
  187. case Obj::CRYPT:
  188. {
  189. GiveBonus gbonus;
  190. gbonus.id = hero->id;
  191. gbonus.bonus.duration = BonusDuration::ONE_BATTLE;
  192. gbonus.bonus.source = BonusSource::OBJECT_TYPE;
  193. gbonus.bonus.sid = BonusSourceID(ID);
  194. gbonus.bonus.type = BonusType::MORALE;
  195. gbonus.bonus.val = -1;
  196. switch (ID.toEnum())
  197. {
  198. case Obj::SHIPWRECK:
  199. textID = 123;
  200. gbonus.bdescr.appendRawString(VLC->generaltexth->arraytxt[99]);
  201. break;
  202. case Obj::DERELICT_SHIP:
  203. textID = 42;
  204. gbonus.bdescr.appendRawString(VLC->generaltexth->arraytxt[101]);
  205. break;
  206. case Obj::CRYPT:
  207. textID = 120;
  208. gbonus.bdescr.appendRawString(VLC->generaltexth->arraytxt[98]);
  209. break;
  210. }
  211. cb->giveHeroBonus(&gbonus);
  212. iw.components.emplace_back(ComponentType::MORALE, -1);
  213. iw.soundID = soundBase::GRAVEYARD;
  214. break;
  215. }
  216. case Obj::PYRAMID:
  217. {
  218. GiveBonus gb;
  219. gb.bonus = Bonus(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, -2, BonusSourceID(id), VLC->generaltexth->arraytxt[70]);
  220. gb.id = hero->id;
  221. cb->giveHeroBonus(&gb);
  222. textID = 107;
  223. iw.components.emplace_back(ComponentType::LUCK, -2);
  224. break;
  225. }
  226. case Obj::CREATURE_BANK:
  227. case Obj::DRAGON_UTOPIA:
  228. default:
  229. iw.text.appendRawString(VLC->generaltexth->advobtxt[33]);// This was X, now is completely empty
  230. iw.text.replaceRawString(getObjectName());
  231. }
  232. if(textID != -1)
  233. {
  234. iw.text.appendLocalString(EMetaText::ADVOB_TXT, textID);
  235. }
  236. cb->showInfoDialog(&iw);
  237. }
  238. //grant resources
  239. if (bc)
  240. {
  241. for (GameResID it : GameResID::ALL_RESOURCES())
  242. {
  243. if (bc->resources[it] != 0)
  244. {
  245. iw.components.emplace_back(ComponentType::RESOURCE, it, bc->resources[it]);
  246. loot.appendRawString("%d %s");
  247. loot.replaceNumber(bc->resources[it]);
  248. loot.replaceName(it);
  249. cb->giveResource(hero->getOwner(), it, bc->resources[it]);
  250. }
  251. }
  252. //grant artifacts
  253. for (auto & elem : bc->artifacts)
  254. {
  255. iw.components.emplace_back(ComponentType::ARTIFACT, elem);
  256. loot.appendRawString("%s");
  257. loot.replaceName(elem);
  258. cb->giveHeroNewArtifact(hero, elem.toArtifact(), ArtifactPosition::FIRST_AVAILABLE);
  259. }
  260. //display loot
  261. if (!iw.components.empty())
  262. {
  263. iw.text.appendLocalString(EMetaText::ADVOB_TXT, textID);
  264. if (textID == 34)
  265. {
  266. const auto * strongest = boost::range::max_element(bc->guards, [](const CStackBasicDescriptor & a, const CStackBasicDescriptor & b)
  267. {
  268. return a.type->getFightValue() < b.type->getFightValue();
  269. })->type;
  270. iw.text.replaceNamePlural(strongest->getId());
  271. iw.text.replaceRawString(loot.buildList());
  272. }
  273. cb->showInfoDialog(&iw);
  274. }
  275. loot.clear();
  276. iw.components.clear();
  277. iw.text.clear();
  278. if (!bc->spells.empty())
  279. {
  280. std::set<SpellID> spells;
  281. bool noWisdom = false;
  282. if(textID == 106)
  283. {
  284. iw.text.appendLocalString(EMetaText::ADVOB_TXT, textID); //pyramid
  285. }
  286. for(const SpellID & spellId : bc->spells)
  287. {
  288. const auto * spell = spellId.toEntity(VLC);
  289. iw.text.appendName(spellId);
  290. if(spell->getLevel() <= hero->maxSpellLevel())
  291. {
  292. if(hero->canLearnSpell(spell))
  293. {
  294. spells.insert(spellId);
  295. iw.components.emplace_back(ComponentType::SPELL, spellId);
  296. }
  297. }
  298. else
  299. noWisdom = true;
  300. }
  301. if (!hero->getArt(ArtifactPosition::SPELLBOOK))
  302. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 109); //no spellbook
  303. else if(noWisdom)
  304. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 108); //no expert Wisdom
  305. if(!iw.components.empty() || !iw.text.toString().empty())
  306. cb->showInfoDialog(&iw);
  307. if(!spells.empty())
  308. cb->changeSpells(hero, true, spells);
  309. }
  310. iw.components.clear();
  311. iw.text.clear();
  312. //grant creatures
  313. CCreatureSet ourArmy;
  314. for(const auto & slot : bc->creatures)
  315. {
  316. ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->getId()), slot.type->getId(), slot.count);
  317. }
  318. for(const auto & elem : ourArmy.Slots())
  319. {
  320. iw.components.emplace_back(ComponentType::CREATURE, elem.second->getId(), elem.second->getCount());
  321. loot.appendRawString("%s");
  322. loot.replaceName(*elem.second);
  323. }
  324. if(ourArmy.stacksCount())
  325. {
  326. if(ourArmy.stacksCount() == 1 && ourArmy.Slots().begin()->second->count == 1)
  327. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 185);
  328. else
  329. iw.text.appendLocalString(EMetaText::ADVOB_TXT, 186);
  330. iw.text.replaceRawString(loot.buildList());
  331. iw.text.replaceRawString(hero->getNameTranslated());
  332. cb->showInfoDialog(&iw);
  333. cb->giveCreatures(this, hero, ourArmy, false);
  334. }
  335. cb->setObjPropertyValue(id, ObjProperty::BANK_CLEAR); //bc = nullptr
  336. }
  337. }
  338. void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  339. {
  340. if (result.winner == 0)
  341. {
  342. doVisit(hero);
  343. }
  344. }
  345. void CBank::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  346. {
  347. if (answer)
  348. {
  349. if (bc) // not looted bank
  350. cb->startBattleI(hero, this, true);
  351. else
  352. doVisit(hero);
  353. }
  354. }
  355. VCMI_LIB_NAMESPACE_END