CGTownInstance.cpp 34 KB

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