CGTownInstance.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * CGTownInstance.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 "CGTownInstance.h"
  12. #include "CGTownBuilding.h"
  13. #include "../spells/CSpellHandler.h"
  14. #include "../bonuses/Bonus.h"
  15. #include "../battle/IBattleInfoCallback.h"
  16. #include "../CConfigHandler.h"
  17. #include "../CGeneralTextHandler.h"
  18. #include "../IGameCallback.h"
  19. #include "../gameState/CGameState.h"
  20. #include "../mapping/CMap.h"
  21. #include "../CPlayerState.h"
  22. #include "../StartInfo.h"
  23. #include "../TerrainHandler.h"
  24. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  25. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  26. #include "../mapObjects/CGHeroInstance.h"
  27. #include "../modding/ModScope.h"
  28. #include "../networkPacks/StackLocation.h"
  29. #include "../networkPacks/PacksForClient.h"
  30. #include "../networkPacks/PacksForClientBattle.h"
  31. #include "../serializer/JsonSerializeFormat.h"
  32. VCMI_LIB_NAMESPACE_BEGIN
  33. std::vector<const CArtifact *> CGTownInstance::merchantArtifacts;
  34. std::vector<TradeItemBuy> CGTownInstance::universitySkills;
  35. int CGTownInstance::getSightRadius() const //returns sight distance
  36. {
  37. auto ret = CBuilding::HEIGHT_NO_TOWER;
  38. for(const auto & bid : builtBuildings)
  39. {
  40. if(bid.IsSpecialOrGrail())
  41. {
  42. auto height = town->buildings.at(bid)->height;
  43. if(ret < height)
  44. ret = height;
  45. }
  46. }
  47. return ret;
  48. }
  49. void CGTownInstance::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  50. {
  51. ///this is freakin' overcomplicated solution
  52. switch (what)
  53. {
  54. case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
  55. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, visitingHero->id);
  56. break;
  57. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  58. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, NumericID(0));
  59. break;
  60. case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
  61. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, garrisonHero->id);
  62. break;
  63. case ObjProperty::BONUS_VALUE_FIRST:
  64. bonusValue.first = identifier.getNum();
  65. break;
  66. case ObjProperty::BONUS_VALUE_SECOND:
  67. bonusValue.second = identifier.getNum();
  68. break;
  69. case ObjProperty::REWARD_RANDOMIZE:
  70. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::REWARD_RANDOMIZE, NumericID(0));
  71. break;
  72. }
  73. }
  74. CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  75. {
  76. if (hasBuilt(BuildingID::CASTLE))
  77. return CASTLE;
  78. if (hasBuilt(BuildingID::CITADEL))
  79. return CITADEL;
  80. if (hasBuilt(BuildingID::FORT))
  81. return FORT;
  82. return NONE;
  83. }
  84. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  85. {
  86. if (hasBuilt(BuildingID::CAPITOL))
  87. return 3;
  88. if (hasBuilt(BuildingID::CITY_HALL))
  89. return 2;
  90. if (hasBuilt(BuildingID::TOWN_HALL))
  91. return 1;
  92. if (hasBuilt(BuildingID::VILLAGE_HALL))
  93. return 0;
  94. return -1;
  95. }
  96. int CGTownInstance::mageGuildLevel() const
  97. {
  98. if (hasBuilt(BuildingID::MAGES_GUILD_5))
  99. return 5;
  100. if (hasBuilt(BuildingID::MAGES_GUILD_4))
  101. return 4;
  102. if (hasBuilt(BuildingID::MAGES_GUILD_3))
  103. return 3;
  104. if (hasBuilt(BuildingID::MAGES_GUILD_2))
  105. return 2;
  106. if (hasBuilt(BuildingID::MAGES_GUILD_1))
  107. return 1;
  108. return 0;
  109. }
  110. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  111. {
  112. return town->hordeLvl.at(HID);
  113. }
  114. int CGTownInstance::creatureGrowth(const int & level) const
  115. {
  116. return getGrowthInfo(level).totalGrowth();
  117. }
  118. GrowthInfo CGTownInstance::getGrowthInfo(int level) const
  119. {
  120. GrowthInfo ret;
  121. if (level<0 || level >=GameConstants::CREATURES_PER_TOWN)
  122. return ret;
  123. if (creatures[level].second.empty())
  124. return ret; //no dwelling
  125. const Creature *creature = creatures[level].second.back().toEntity(VLC);
  126. const int base = creature->getGrowth();
  127. int castleBonus = 0;
  128. ret.entries.emplace_back(VLC->generaltexth->allTexts[590], base); // \n\nBasic growth %d"
  129. if (hasBuilt(BuildingID::CASTLE))
  130. ret.entries.emplace_back(subID, BuildingID::CASTLE, castleBonus = base);
  131. else if (hasBuilt(BuildingID::CITADEL))
  132. ret.entries.emplace_back(subID, BuildingID::CITADEL, castleBonus = base / 2);
  133. if(town->hordeLvl.at(0) == level)//horde 1
  134. if(hasBuilt(BuildingID::HORDE_1))
  135. ret.entries.emplace_back(subID, BuildingID::HORDE_1, creature->getHorde());
  136. if(town->hordeLvl.at(1) == level)//horde 2
  137. if(hasBuilt(BuildingID::HORDE_2))
  138. ret.entries.emplace_back(subID, BuildingID::HORDE_2, creature->getHorde());
  139. //statue-of-legion-like bonus: % to base+castle
  140. TConstBonusListPtr bonuses2 = getBonuses(Selector::type()(BonusType::CREATURE_GROWTH_PERCENT));
  141. for(const auto & b : *bonuses2)
  142. {
  143. const auto growth = b->val * (base + castleBonus) / 100;
  144. ret.entries.emplace_back(growth, b->Description(growth));
  145. }
  146. //other *-of-legion-like bonuses (%d to growth cumulative with grail)
  147. TConstBonusListPtr bonuses = getBonuses(Selector::typeSubtype(BonusType::CREATURE_GROWTH, BonusCustomSubtype::creatureLevel(level)));
  148. for(const auto & b : *bonuses)
  149. ret.entries.emplace_back(b->val, b->Description());
  150. int dwellingBonus = 0;
  151. if(const PlayerState *p = cb->getPlayerState(tempOwner, false))
  152. {
  153. dwellingBonus = getDwellingBonus(creatures[level].second, p->dwellings);
  154. }
  155. if(dwellingBonus)
  156. ret.entries.emplace_back(VLC->generaltexth->allTexts[591], dwellingBonus); // \nExternal dwellings %+d
  157. if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
  158. ret.entries.emplace_back(subID, BuildingID::GRAIL, ret.totalGrowth() / 2);
  159. return ret;
  160. }
  161. int CGTownInstance::getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const
  162. {
  163. int totalBonus = 0;
  164. for (const auto& dwelling : dwellings)
  165. {
  166. for (const auto& creature : dwelling->creatures)
  167. {
  168. totalBonus += vstd::contains(creatureIds, creature.second[0]) ? 1 : 0;
  169. }
  170. }
  171. return totalBonus;
  172. }
  173. TResources CGTownInstance::dailyIncome() const
  174. {
  175. TResources ret;
  176. for(const auto & p : town->buildings)
  177. {
  178. BuildingID buildingUpgrade;
  179. for(const auto & p2 : town->buildings)
  180. {
  181. if (p2.second->upgrade == p.first)
  182. {
  183. buildingUpgrade = p2.first;
  184. }
  185. }
  186. if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
  187. {
  188. ret += p.second->produce;
  189. }
  190. }
  191. return ret;
  192. }
  193. bool CGTownInstance::hasFort() const
  194. {
  195. return hasBuilt(BuildingID::FORT);
  196. }
  197. bool CGTownInstance::hasCapitol() const
  198. {
  199. return hasBuilt(BuildingID::CAPITOL);
  200. }
  201. CGTownInstance::CGTownInstance():
  202. town(nullptr),
  203. builded(0),
  204. destroyed(0),
  205. identifier(0),
  206. alignmentToPlayer(PlayerColor::NEUTRAL)
  207. {
  208. this->setNodeType(CBonusSystemNode::TOWN);
  209. }
  210. CGTownInstance::~CGTownInstance()
  211. {
  212. for (auto & elem : bonusingBuildings)
  213. delete elem;
  214. }
  215. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  216. {
  217. if(checkGuild && mageGuildLevel() < level)
  218. return 0;
  219. int ret = 6 - level; //how many spells are available at this level
  220. if (hasBuilt(BuildingSubID::LIBRARY))
  221. ret++;
  222. return ret;
  223. }
  224. bool CGTownInstance::needsLastStack() const
  225. {
  226. return garrisonHero != nullptr;
  227. }
  228. void CGTownInstance::setOwner(const PlayerColor & player) const
  229. {
  230. removeCapitols(player);
  231. cb->setOwner(this, player);
  232. }
  233. void CGTownInstance::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  234. {
  235. for (auto building : bonusingBuildings)
  236. building->blockingDialogAnswered(hero, answer);
  237. }
  238. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  239. {
  240. if(cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ) == PlayerRelations::ENEMIES)
  241. {
  242. if(armedGarrison() || visitingHero)
  243. {
  244. const CGHeroInstance * defendingHero = visitingHero ? visitingHero : garrisonHero;
  245. const CArmedInstance * defendingArmy = defendingHero ? (CArmedInstance *)defendingHero : this;
  246. const bool isBattleOutside = isBattleOutsideTown(defendingHero);
  247. if(!isBattleOutside && visitingHero && defendingHero == visitingHero)
  248. {
  249. //we have two approaches to merge armies: mergeGarrisonOnSiege() and used in the CGameHandler::garrisonSwap(ObjectInstanceID tid)
  250. auto * nodeSiege = defendingHero->whereShouldBeAttachedOnSiege(isBattleOutside);
  251. if(nodeSiege == (CBonusSystemNode *)this)
  252. cb->swapGarrisonOnSiege(this->id);
  253. const_cast<CGHeroInstance *>(defendingHero)->inTownGarrison = false; //hack to return visitor from garrison after battle
  254. }
  255. cb->startBattlePrimary(h, defendingArmy, getSightCenter(), h, defendingHero, false, (isBattleOutside ? nullptr : this));
  256. }
  257. else
  258. {
  259. auto heroColor = h->getOwner();
  260. onTownCaptured(heroColor);
  261. if(cb->gameState()->getPlayerStatus(heroColor) == EPlayerStatus::WINNER)
  262. {
  263. return; //we just won game, we do not need to perform any extra actions
  264. //TODO: check how does H3 behave, visiting town on victory can affect campaigns (spells learned, +1 stat building visited)
  265. }
  266. cb->heroVisitCastle(this, h);
  267. }
  268. }
  269. else if(h->visitablePos() == visitablePos())
  270. {
  271. bool commander_recover = h->commander && !h->commander->alive;
  272. if (commander_recover) // rise commander from dead
  273. {
  274. SetCommanderProperty scp;
  275. scp.heroid = h->id;
  276. scp.which = SetCommanderProperty::ALIVE;
  277. scp.amount = 1;
  278. cb->sendAndApply(&scp);
  279. }
  280. cb->heroVisitCastle(this, h);
  281. // TODO(vmarkovtsev): implement payment for rising the commander
  282. if (commander_recover) // info window about commander
  283. {
  284. InfoWindow iw;
  285. iw.player = h->tempOwner;
  286. iw.text.appendRawString(h->commander->getName());
  287. iw.components.emplace_back(ComponentType::CREATURE, h->commander->getId(), h->commander->getCount());
  288. cb->showInfoDialog(&iw);
  289. }
  290. }
  291. else
  292. {
  293. logGlobal->error("%s visits allied town of %s from different pos?", h->getNameTranslated(), getNameTranslated());
  294. }
  295. }
  296. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  297. {
  298. //FIXME: find out why this issue appears on random maps
  299. if(visitingHero == h)
  300. {
  301. cb->stopHeroVisitCastle(this, h);
  302. logGlobal->trace("%s correctly left town %s", h->getNameTranslated(), getNameTranslated());
  303. }
  304. else
  305. logGlobal->warn("Warning, %s tries to leave the town %s but hero is not inside.", h->getNameTranslated(), getNameTranslated());
  306. }
  307. std::string CGTownInstance::getObjectName() const
  308. {
  309. return getNameTranslated() + ", " + town->faction->getNameTranslated();
  310. }
  311. bool CGTownInstance::townEnvisagesBuilding(BuildingSubID::EBuildingSubID subId) const
  312. {
  313. return town->getBuildingType(subId) != BuildingID::NONE;
  314. }
  315. void CGTownInstance::initOverriddenBids()
  316. {
  317. for(const auto & bid : builtBuildings)
  318. {
  319. const auto & overrideThem = town->buildings.at(bid)->overrideBids;
  320. for(const auto & overrideIt : overrideThem)
  321. overriddenBuildings.insert(overrideIt);
  322. }
  323. }
  324. bool CGTownInstance::isBonusingBuildingAdded(BuildingID bid) const
  325. {
  326. auto present = std::find_if(bonusingBuildings.begin(), bonusingBuildings.end(), [&](CGTownBuilding* building)
  327. {
  328. return building->getBuildingType() == bid;
  329. });
  330. return present != bonusingBuildings.end();
  331. }
  332. void CGTownInstance::addTownBonuses(CRandomGenerator & rand)
  333. {
  334. for(const auto & kvp : town->buildings)
  335. {
  336. if(vstd::contains(overriddenBuildings, kvp.first))
  337. continue;
  338. if(kvp.second->IsVisitingBonus())
  339. bonusingBuildings.push_back(new CTownBonus(kvp.second->bid, kvp.second->subId, this));
  340. if(kvp.second->IsWeekBonus())
  341. bonusingBuildings.push_back(new COPWBonus(kvp.second->bid, kvp.second->subId, this));
  342. if(kvp.second->subId == BuildingSubID::CUSTOM_VISITING_REWARD)
  343. bonusingBuildings.push_back(new CTownRewardableBuilding(kvp.second->bid, kvp.second->subId, this, rand));
  344. }
  345. }
  346. DamageRange CGTownInstance::getTowerDamageRange() const
  347. {
  348. assert(hasBuilt(BuildingID::CASTLE));
  349. // http://heroes.thelazy.net/wiki/Arrow_tower
  350. // base damage, irregardless of town level
  351. static constexpr int baseDamage = 6;
  352. // extra damage, for each building in town
  353. static constexpr int extraDamage = 1;
  354. const int minDamage = baseDamage + extraDamage * getTownLevel();
  355. return {
  356. minDamage,
  357. minDamage * 2
  358. };
  359. }
  360. DamageRange CGTownInstance::getKeepDamageRange() const
  361. {
  362. assert(hasBuilt(BuildingID::CITADEL));
  363. // http://heroes.thelazy.net/wiki/Arrow_tower
  364. // base damage, irregardless of town level
  365. static constexpr int baseDamage = 10;
  366. // extra damage, for each building in town
  367. static constexpr int extraDamage = 2;
  368. const int minDamage = baseDamage + extraDamage * getTownLevel();
  369. return {
  370. minDamage,
  371. minDamage * 2
  372. };
  373. }
  374. void CGTownInstance::deleteTownBonus(BuildingID bid)
  375. {
  376. size_t i = 0;
  377. CGTownBuilding * freeIt = nullptr;
  378. for(i = 0; i != bonusingBuildings.size(); i++)
  379. {
  380. if(bonusingBuildings[i]->getBuildingType() == bid)
  381. {
  382. freeIt = bonusingBuildings[i];
  383. break;
  384. }
  385. }
  386. if(freeIt == nullptr)
  387. return;
  388. auto building = town->buildings.at(bid);
  389. auto isVisitingBonus = building->IsVisitingBonus();
  390. auto isWeekBonus = building->IsWeekBonus();
  391. if(!isVisitingBonus && !isWeekBonus)
  392. return;
  393. bonusingBuildings.erase(bonusingBuildings.begin() + i);
  394. delete freeIt;
  395. }
  396. FactionID CGTownInstance::randomizeFaction(CRandomGenerator & rand)
  397. {
  398. if(getOwner().isValidPlayer())
  399. return cb->gameState()->scenarioOps->getIthPlayersSettings(getOwner()).castle;
  400. if(alignmentToPlayer.isValidPlayer())
  401. return cb->gameState()->scenarioOps->getIthPlayersSettings(alignmentToPlayer).castle;
  402. std::vector<FactionID> potentialPicks;
  403. for (FactionID faction(0); faction < FactionID(VLC->townh->size()); ++faction)
  404. if (VLC->factions()->getById(faction)->hasTown())
  405. potentialPicks.push_back(faction);
  406. assert(!potentialPicks.empty());
  407. return *RandomGeneratorUtil::nextItem(potentialPicks, rand);
  408. }
  409. void CGTownInstance::pickRandomObject(CRandomGenerator & rand)
  410. {
  411. assert(ID == MapObjectID::TOWN || ID == MapObjectID::RANDOM_TOWN);
  412. if (ID == MapObjectID::RANDOM_TOWN)
  413. {
  414. ID = MapObjectID::TOWN;
  415. subID = randomizeFaction(rand);
  416. }
  417. assert(ID == Obj::TOWN); // just in case
  418. setType(ID, subID);
  419. town = (*VLC->townh)[getFaction()]->town;
  420. randomizeArmy(getFaction());
  421. updateAppearance();
  422. }
  423. void CGTownInstance::initObj(CRandomGenerator & rand) ///initialize town structures
  424. {
  425. blockVisit = true;
  426. if(townEnvisagesBuilding(BuildingSubID::PORTAL_OF_SUMMONING)) //Dungeon for example
  427. creatures.resize(GameConstants::CREATURES_PER_TOWN + 1);
  428. else
  429. creatures.resize(GameConstants::CREATURES_PER_TOWN);
  430. for (int level = 0; level < GameConstants::CREATURES_PER_TOWN; level++)
  431. {
  432. BuildingID buildID = BuildingID(BuildingID::DWELL_FIRST + level);
  433. int upgradeNum = 0;
  434. for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(GameConstants::CREATURES_PER_TOWN))
  435. {
  436. if (hasBuilt(buildID) && town->creatures.at(level).size() > upgradeNum)
  437. creatures[level].second.push_back(town->creatures[level][upgradeNum]);
  438. }
  439. }
  440. initOverriddenBids();
  441. addTownBonuses(rand); //add special bonuses from buildings to the bonusingBuildings vector.
  442. recreateBuildingsBonuses();
  443. updateAppearance();
  444. }
  445. void CGTownInstance::newTurn(CRandomGenerator & rand) const
  446. {
  447. if (cb->getDate(Date::DAY_OF_WEEK) == 1) //reset on new week
  448. {
  449. //give resources if there's a Mystic Pond
  450. if (hasBuilt(BuildingSubID::MYSTIC_POND)
  451. && cb->getDate(Date::DAY) != 1
  452. && (tempOwner.isValidPlayer())
  453. )
  454. {
  455. int resID = rand.nextInt(2, 5); //bonus to random rare resource
  456. resID = (resID==2)?1:resID;
  457. int resVal = rand.nextInt(1, 4);//with size 1..4
  458. cb->giveResource(tempOwner, static_cast<EGameResID>(resID), resVal);
  459. cb->setObjPropertyValue(id, ObjProperty::BONUS_VALUE_FIRST, resID);
  460. cb->setObjPropertyValue(id, ObjProperty::BONUS_VALUE_SECOND, resVal);
  461. }
  462. for(const auto * manaVortex : getBonusingBuildings(BuildingSubID::MANA_VORTEX))
  463. cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_CLEAR_VISITORS, manaVortex->indexOnTV); //reset visitors for Mana Vortex
  464. //get Mana Vortex or Stables bonuses
  465. //same code is in the CGameHandler::buildStructure method
  466. if (visitingHero != nullptr)
  467. cb->visitCastleObjects(this, visitingHero);
  468. if (garrisonHero != nullptr)
  469. cb->visitCastleObjects(this, garrisonHero);
  470. if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
  471. {
  472. std::vector<SlotID> nativeCrits; //slots
  473. for(const auto & elem : Slots())
  474. {
  475. if (elem.second->type->getFaction() == getFaction()) //native
  476. {
  477. nativeCrits.push_back(elem.first); //collect matching slots
  478. }
  479. }
  480. if(!nativeCrits.empty())
  481. {
  482. SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
  483. StackLocation sl(this, pos);
  484. const CCreature *c = getCreature(pos);
  485. if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
  486. {
  487. cb->changeStackCount(sl, c->getGrowth());
  488. }
  489. else //upgrade
  490. {
  491. cb->changeStackType(sl, c->upgrades.begin()->toCreature());
  492. }
  493. }
  494. if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
  495. {
  496. int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1);
  497. if (!town->creatures[i].empty())
  498. {
  499. CreatureID c = town->creatures[i][0];
  500. SlotID n;
  501. TQuantity count = creatureGrowth(i);
  502. if (!count) // no dwelling
  503. count = VLC->creatures()->getById(c)->getGrowth();
  504. {//no lower tiers or above current month
  505. if ((n = getSlotFor(c)).validSlot())
  506. {
  507. StackLocation sl(this, n);
  508. if (slotEmpty(n))
  509. cb->insertNewStack(sl, c.toCreature(), count);
  510. else //add to existing
  511. cb->changeStackCount(sl, count);
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. for(const auto * rewardableBuilding : getBonusingBuildings(BuildingSubID::CUSTOM_VISITING_REWARD))
  519. rewardableBuilding->newTurn(rand);
  520. }
  521. /*
  522. int3 CGTownInstance::getSightCenter() const
  523. {
  524. return pos - int3(2,0,0);
  525. }
  526. */
  527. bool CGTownInstance::passableFor(PlayerColor color) const
  528. {
  529. if (!armedGarrison())//empty castle - anyone can visit
  530. return true;
  531. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  532. return false;
  533. return cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES;
  534. }
  535. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  536. {
  537. offsets = {int3(-1,2,0), int3(+1,2,0)};
  538. }
  539. CGTownInstance::EGeneratorState CGTownInstance::shipyardStatus() const
  540. {
  541. if (!hasBuilt(BuildingID::SHIPYARD))
  542. return EGeneratorState::UNKNOWN;
  543. return IShipyard::shipyardStatus();
  544. }
  545. const IObjectInterface * CGTownInstance::getObject() const
  546. {
  547. return this;
  548. }
  549. void CGTownInstance::mergeGarrisonOnSiege() const
  550. {
  551. auto getWeakestStackSlot = [&](ui64 powerLimit)
  552. {
  553. std::vector<SlotID> weakSlots;
  554. auto stacksList = visitingHero->stacks;
  555. std::pair<SlotID, CStackInstance *> pair;
  556. while(!stacksList.empty())
  557. {
  558. pair = *vstd::minElementByFun(stacksList, [&](const std::pair<SlotID, CStackInstance *> & elem) { return elem.second->getPower(); });
  559. if(powerLimit > pair.second->getPower() &&
  560. (weakSlots.empty() || pair.second->getPower() == visitingHero->getStack(weakSlots.front()).getPower()))
  561. {
  562. weakSlots.push_back(pair.first);
  563. stacksList.erase(pair.first);
  564. }
  565. else
  566. break;
  567. }
  568. if(!weakSlots.empty())
  569. return *std::max_element(weakSlots.begin(), weakSlots.end());
  570. return SlotID();
  571. };
  572. auto count = static_cast<int>(stacks.size());
  573. for(int i = 0; i < count; i++)
  574. {
  575. auto pair = *vstd::maxElementByFun(stacks, [&](const std::pair<SlotID, CStackInstance *> & elem)
  576. {
  577. ui64 power = elem.second->getPower();
  578. auto dst = visitingHero->getSlotFor(elem.second->getCreatureID());
  579. if(dst.validSlot() && visitingHero->hasStackAtSlot(dst))
  580. power += visitingHero->getStack(dst).getPower();
  581. return power;
  582. });
  583. auto dst = visitingHero->getSlotFor(pair.second->getCreatureID());
  584. if(dst.validSlot())
  585. cb->moveStack(StackLocation(this, pair.first), StackLocation(visitingHero, dst), -1);
  586. else
  587. {
  588. dst = getWeakestStackSlot(static_cast<int>(pair.second->getPower()));
  589. if(dst.validSlot())
  590. cb->swapStacks(StackLocation(this, pair.first), StackLocation(visitingHero, dst));
  591. }
  592. }
  593. }
  594. void CGTownInstance::removeCapitols(const PlayerColor & owner) const
  595. {
  596. if (hasCapitol()) // search if there's an older capitol
  597. {
  598. PlayerState* state = cb->gameState()->getPlayerState(owner); //get all towns owned by player
  599. for (auto i = state->towns.cbegin(); i < state->towns.cend(); ++i)
  600. {
  601. if (*i != this && (*i)->hasCapitol())
  602. {
  603. RazeStructures rs;
  604. rs.tid = id;
  605. rs.bid.insert(BuildingID::CAPITOL);
  606. rs.destroyed = destroyed;
  607. cb->sendAndApply(&rs);
  608. return;
  609. }
  610. }
  611. }
  612. }
  613. void CGTownInstance::clearArmy() const
  614. {
  615. while(!stacks.empty())
  616. {
  617. cb->eraseStack(StackLocation(this, stacks.begin()->first));
  618. }
  619. }
  620. BoatId CGTownInstance::getBoatType() const
  621. {
  622. return town->faction->boatType;
  623. }
  624. int CGTownInstance::getMarketEfficiency() const
  625. {
  626. if(!hasBuiltSomeTradeBuilding())
  627. return 0;
  628. const PlayerState *p = cb->getPlayerState(tempOwner);
  629. assert(p);
  630. int marketCount = 0;
  631. for(const CGTownInstance *t : p->towns)
  632. if(t->hasBuiltSomeTradeBuilding())
  633. marketCount++;
  634. return marketCount;
  635. }
  636. bool CGTownInstance::allowsTrade(EMarketMode mode) const
  637. {
  638. switch(mode)
  639. {
  640. case EMarketMode::RESOURCE_RESOURCE:
  641. case EMarketMode::RESOURCE_PLAYER:
  642. return hasBuilt(BuildingID::MARKETPLACE);
  643. case EMarketMode::ARTIFACT_RESOURCE:
  644. case EMarketMode::RESOURCE_ARTIFACT:
  645. return hasBuilt(BuildingSubID::ARTIFACT_MERCHANT);
  646. case EMarketMode::CREATURE_RESOURCE:
  647. return hasBuilt(BuildingSubID::FREELANCERS_GUILD);
  648. case EMarketMode::CREATURE_UNDEAD:
  649. return hasBuilt(BuildingSubID::CREATURE_TRANSFORMER);
  650. case EMarketMode::RESOURCE_SKILL:
  651. return hasBuilt(BuildingSubID::MAGIC_UNIVERSITY);
  652. default:
  653. assert(0);
  654. return false;
  655. }
  656. }
  657. std::vector<TradeItemBuy> CGTownInstance::availableItemsIds(EMarketMode mode) const
  658. {
  659. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  660. {
  661. std::vector<TradeItemBuy> ret;
  662. for(const CArtifact *a : merchantArtifacts)
  663. if(a)
  664. ret.push_back(a->getId());
  665. else
  666. ret.push_back(ArtifactID{});
  667. return ret;
  668. }
  669. else if ( mode == EMarketMode::RESOURCE_SKILL )
  670. {
  671. return universitySkills;
  672. }
  673. else
  674. return IMarket::availableItemsIds(mode);
  675. }
  676. void CGTownInstance::updateAppearance()
  677. {
  678. auto terrain = cb->gameState()->getTile(visitablePos())->terType->getId();
  679. //FIXME: not the best way to do this
  680. auto app = getObjectHandler()->getOverride(terrain, this);
  681. if (app)
  682. appearance = app;
  683. }
  684. std::string CGTownInstance::nodeName() const
  685. {
  686. return "Town (" + (town ? town->faction->getNameTranslated() : "unknown") + ") of " + getNameTranslated();
  687. }
  688. void CGTownInstance::deserializationFix()
  689. {
  690. attachTo(townAndVis);
  691. //Hero is already handled by CGameState::attachArmedObjects
  692. // if(visitingHero)
  693. // visitingHero->attachTo(&townAndVis);
  694. // if(garrisonHero)
  695. // garrisonHero->attachTo(this);
  696. }
  697. void CGTownInstance::updateMoraleBonusFromArmy()
  698. {
  699. auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
  700. if(!b)
  701. {
  702. b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
  703. addNewBonus(b);
  704. }
  705. if (garrisonHero)
  706. {
  707. b->val = 0;
  708. CBonusSystemNode::treeHasChanged();
  709. }
  710. else
  711. CArmedInstance::updateMoraleBonusFromArmy();
  712. }
  713. void CGTownInstance::recreateBuildingsBonuses()
  714. {
  715. BonusList bl;
  716. getExportedBonusList().getBonuses(bl, Selector::sourceType()(BonusSource::TOWN_STRUCTURE));
  717. for(const auto & b : bl)
  718. removeBonus(b);
  719. for(const auto & bid : builtBuildings)
  720. {
  721. if(vstd::contains(overriddenBuildings, bid)) //tricky! -> checks tavern only if no bratherhood of sword
  722. continue;
  723. auto building = town->buildings.at(bid);
  724. if(building->buildingBonuses.empty())
  725. continue;
  726. for(auto & bonus : building->buildingBonuses)
  727. addNewBonus(bonus);
  728. }
  729. }
  730. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  731. {
  732. if(visitingHero.get() == h)
  733. return;
  734. if(h)
  735. {
  736. PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
  737. assert(p);
  738. h->detachFrom(*p);
  739. h->attachTo(townAndVis);
  740. visitingHero = h;
  741. h->visitedTown = this;
  742. h->inTownGarrison = false;
  743. }
  744. else
  745. {
  746. PlayerState *p = cb->gameState()->getPlayerState(visitingHero->tempOwner);
  747. visitingHero->visitedTown = nullptr;
  748. visitingHero->detachFrom(townAndVis);
  749. visitingHero->attachTo(*p);
  750. visitingHero = nullptr;
  751. }
  752. }
  753. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  754. {
  755. if(garrisonHero.get() == h)
  756. return;
  757. if(h)
  758. {
  759. PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
  760. assert(p);
  761. h->detachFrom(*p);
  762. h->attachTo(*this);
  763. garrisonHero = h;
  764. h->visitedTown = this;
  765. h->inTownGarrison = true;
  766. }
  767. else
  768. {
  769. PlayerState *p = cb->gameState()->getPlayerState(garrisonHero->tempOwner);
  770. garrisonHero->visitedTown = nullptr;
  771. garrisonHero->inTownGarrison = false;
  772. garrisonHero->detachFrom(*this);
  773. garrisonHero->attachTo(*p);
  774. garrisonHero = nullptr;
  775. }
  776. updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
  777. }
  778. bool CGTownInstance::armedGarrison() const
  779. {
  780. return !stacks.empty() || garrisonHero;
  781. }
  782. const CTown * CGTownInstance::getTown() const
  783. {
  784. if(ID == Obj::RANDOM_TOWN)
  785. return VLC->townh->randomTown;
  786. else
  787. {
  788. if(nullptr == town)
  789. {
  790. return (*VLC->townh)[getFaction()]->town;
  791. }
  792. else
  793. return town;
  794. }
  795. }
  796. int CGTownInstance::getTownLevel() const
  797. {
  798. // count all buildings that are not upgrades
  799. int level = 0;
  800. for(const auto & bid : builtBuildings)
  801. {
  802. if(town->buildings.at(bid)->upgrade == BuildingID::NONE)
  803. level++;
  804. }
  805. return level;
  806. }
  807. CBonusSystemNode & CGTownInstance::whatShouldBeAttached()
  808. {
  809. return townAndVis;
  810. }
  811. std::string CGTownInstance::getNameTranslated() const
  812. {
  813. return VLC->generaltexth->translate(nameTextId);
  814. }
  815. std::string CGTownInstance::getNameTextID() const
  816. {
  817. return nameTextId;
  818. }
  819. void CGTownInstance::setNameTextId( const std::string & newName )
  820. {
  821. nameTextId = newName;
  822. }
  823. const CArmedInstance * CGTownInstance::getUpperArmy() const
  824. {
  825. if(garrisonHero)
  826. return garrisonHero;
  827. return this;
  828. }
  829. std::vector<const CGTownBuilding *> CGTownInstance::getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const
  830. {
  831. std::vector<const CGTownBuilding *> ret;
  832. for(auto * const building : bonusingBuildings)
  833. {
  834. if(building->getBuildingSubtype() == subId)
  835. ret.push_back(building);
  836. }
  837. return ret;
  838. }
  839. bool CGTownInstance::hasBuiltSomeTradeBuilding() const
  840. {
  841. for(const auto & bid : builtBuildings)
  842. {
  843. if(town->buildings.at(bid)->IsTradeBuilding())
  844. return true;
  845. }
  846. return false;
  847. }
  848. bool CGTownInstance::hasBuilt(BuildingSubID::EBuildingSubID buildingID) const
  849. {
  850. for(const auto & bid : builtBuildings)
  851. {
  852. if(town->buildings.at(bid)->subId == buildingID)
  853. return true;
  854. }
  855. return false;
  856. }
  857. bool CGTownInstance::hasBuilt(const BuildingID & buildingID) const
  858. {
  859. return vstd::contains(builtBuildings, buildingID);
  860. }
  861. bool CGTownInstance::hasBuilt(const BuildingID & buildingID, FactionID townID) const
  862. {
  863. if (townID == town->faction->getId() || townID == FactionID::ANY)
  864. return hasBuilt(buildingID);
  865. return false;
  866. }
  867. TResources CGTownInstance::getBuildingCost(const BuildingID & buildingID) const
  868. {
  869. if (vstd::contains(town->buildings, buildingID))
  870. return town->buildings.at(buildingID)->resources;
  871. else
  872. {
  873. logGlobal->error("Town %s at %s has no possible building %d!", getNameTranslated(), pos.toString(), buildingID.toEnum());
  874. return TResources();
  875. }
  876. }
  877. CBuilding::TRequired CGTownInstance::genBuildingRequirements(const BuildingID & buildID, bool deep) const
  878. {
  879. const CBuilding * building = town->buildings.at(buildID);
  880. //TODO: find better solution to prevent infinite loops
  881. std::set<BuildingID> processed;
  882. std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
  883. [&](const BuildingID & id) -> CBuilding::TRequired::Variant
  884. {
  885. if (town->buildings.count(id) == 0)
  886. {
  887. logMod->error("Invalid building ID %d in building dependencies!", id.getNum());
  888. return CBuilding::TRequired::OperatorAll();
  889. }
  890. const CBuilding * build = town->buildings.at(id);
  891. CBuilding::TRequired::OperatorAll requirements;
  892. if (!hasBuilt(id))
  893. {
  894. if (deep)
  895. requirements.expressions.emplace_back(id);
  896. else
  897. return id;
  898. }
  899. if(!vstd::contains(processed, id))
  900. {
  901. processed.insert(id);
  902. if (build->upgrade != BuildingID::NONE)
  903. requirements.expressions.push_back(dependTest(build->upgrade));
  904. requirements.expressions.push_back(build->requirements.morph(dependTest));
  905. }
  906. return requirements;
  907. };
  908. CBuilding::TRequired::OperatorAll requirements;
  909. if (building->upgrade != BuildingID::NONE)
  910. {
  911. const CBuilding * upgr = town->buildings.at(building->upgrade);
  912. requirements.expressions.push_back(dependTest(upgr->bid));
  913. processed.clear();
  914. }
  915. requirements.expressions.push_back(building->requirements.morph(dependTest));
  916. CBuilding::TRequired::Variant variant(requirements);
  917. CBuilding::TRequired ret(variant);
  918. ret.minimize();
  919. return ret;
  920. }
  921. void CGTownInstance::addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID ) const
  922. {
  923. if(visitingHero == h)
  924. cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  925. else if(garrisonHero == h)
  926. cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  927. else
  928. {
  929. //should never ever happen
  930. logGlobal->error("Cannot add hero %s to visitors of structure # %d", h->getNameTranslated(), structureInstanceID);
  931. throw std::runtime_error("unexpected hero in CGTownInstance::addHeroToStructureVisitors");
  932. }
  933. }
  934. void CGTownInstance::battleFinished(const CGHeroInstance * hero, const BattleResult & result) const
  935. {
  936. if(result.winner == BattleSide::ATTACKER)
  937. {
  938. clearArmy();
  939. onTownCaptured(hero->getOwner());
  940. }
  941. }
  942. void CGTownInstance::onTownCaptured(const PlayerColor & winner) const
  943. {
  944. setOwner(winner);
  945. cb->changeFogOfWar(getSightCenter(), getSightRadius(), winner, ETileVisibility::REVEALED);
  946. }
  947. void CGTownInstance::afterAddToMap(CMap * map)
  948. {
  949. map->towns.emplace_back(this);
  950. }
  951. void CGTownInstance::afterRemoveFromMap(CMap * map)
  952. {
  953. vstd::erase_if_present(map->towns, this);
  954. }
  955. void CGTownInstance::reset()
  956. {
  957. CGTownInstance::merchantArtifacts.clear();
  958. CGTownInstance::universitySkills.clear();
  959. }
  960. void CGTownInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  961. {
  962. CGObjectInstance::serializeJsonOwner(handler);
  963. if(!handler.saving)
  964. handler.serializeEnum("tightFormation", formation, NArmyFormation::names); //for old format
  965. CArmedInstance::serializeJsonOptions(handler);
  966. handler.serializeString("name", nameTextId);
  967. {
  968. auto decodeBuilding = [this](const std::string & identifier) -> si32
  969. {
  970. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), getTown()->getBuildingScope(), identifier);
  971. if(rawId)
  972. return rawId.value();
  973. else
  974. return -1;
  975. };
  976. auto encodeBuilding = [this](si32 index) -> std::string
  977. {
  978. return getTown()->buildings.at(BuildingID(index))->getJsonKey();
  979. };
  980. const std::set<si32> standard = getTown()->getAllBuildings();//by default all buildings are allowed
  981. JsonSerializeFormat::LICSet buildingsLIC(standard, decodeBuilding, encodeBuilding);
  982. if(handler.saving)
  983. {
  984. bool customBuildings = false;
  985. boost::logic::tribool hasFort(false);
  986. for(const BuildingID & id : forbiddenBuildings)
  987. {
  988. buildingsLIC.none.insert(id.getNum());
  989. customBuildings = true;
  990. }
  991. for(const BuildingID & id : builtBuildings)
  992. {
  993. if(id == BuildingID::DEFAULT)
  994. continue;
  995. const CBuilding * building = getTown()->buildings.at(id);
  996. if(building->mode == CBuilding::BUILD_AUTO)
  997. continue;
  998. if(id == BuildingID::FORT)
  999. hasFort = true;
  1000. buildingsLIC.all.insert(id.getNum());
  1001. customBuildings = true;
  1002. }
  1003. if(customBuildings)
  1004. handler.serializeLIC("buildings", buildingsLIC);
  1005. else
  1006. handler.serializeBool("hasFort",hasFort);
  1007. }
  1008. else
  1009. {
  1010. handler.serializeLIC("buildings", buildingsLIC);
  1011. builtBuildings.insert(BuildingID::VILLAGE_HALL);
  1012. if(buildingsLIC.none.empty() && buildingsLIC.all.empty())
  1013. {
  1014. builtBuildings.insert(BuildingID::DEFAULT);
  1015. bool hasFort = false;
  1016. handler.serializeBool("hasFort",hasFort);
  1017. if(hasFort)
  1018. builtBuildings.insert(BuildingID::FORT);
  1019. }
  1020. else
  1021. {
  1022. for(const si32 item : buildingsLIC.none)
  1023. forbiddenBuildings.insert(BuildingID(item));
  1024. for(const si32 item : buildingsLIC.all)
  1025. builtBuildings.insert(BuildingID(item));
  1026. }
  1027. }
  1028. }
  1029. {
  1030. handler.serializeIdArray( "possibleSpells", possibleSpells);
  1031. handler.serializeIdArray( "obligatorySpells", obligatorySpells);
  1032. }
  1033. }
  1034. FactionID CGTownInstance::getFaction() const
  1035. {
  1036. return FactionID(subID.getNum());
  1037. }
  1038. TerrainId CGTownInstance::getNativeTerrain() const
  1039. {
  1040. return town->faction->getNativeTerrain();
  1041. }
  1042. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1043. : count(_count)
  1044. {
  1045. description = boost::str(boost::format(format) % count);
  1046. }
  1047. GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count)
  1048. {
  1049. description = boost::str(boost::format("%s %+d") % (*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated() % count);
  1050. }
  1051. GrowthInfo::Entry::Entry(int _count, std::string fullDescription):
  1052. count(_count),
  1053. description(std::move(fullDescription))
  1054. {
  1055. }
  1056. CTownAndVisitingHero::CTownAndVisitingHero()
  1057. {
  1058. setNodeType(TOWN_AND_VISITOR);
  1059. }
  1060. int GrowthInfo::totalGrowth() const
  1061. {
  1062. int ret = 0;
  1063. for(const Entry &entry : entries)
  1064. ret += entry.count;
  1065. return ret;
  1066. }
  1067. void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
  1068. {
  1069. for(const CGTownInstance::TCreaturesSet::value_type & dwelling : creatures)
  1070. {
  1071. if (vstd::contains(dwelling.second, stack.type->getId())) //Dwelling with our creature
  1072. {
  1073. for(const auto & upgrID : dwelling.second)
  1074. {
  1075. if(vstd::contains(stack.type->upgrades, upgrID)) //possible upgrade
  1076. {
  1077. info.newID.push_back(upgrID);
  1078. info.cost.push_back(upgrID.toCreature()->getFullRecruitCost() - stack.type->getFullRecruitCost());
  1079. }
  1080. }
  1081. }
  1082. }
  1083. }
  1084. VCMI_LIB_NAMESPACE_END