CGTownInstance.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. // Note: bonus uses 1-based levels (Pikeman is level 1), town list uses 0-based (Pikeman in 0-th creatures entry)
  148. TConstBonusListPtr bonuses = getBonuses(Selector::typeSubtype(BonusType::CREATURE_GROWTH, BonusCustomSubtype::creatureLevel(level+1)));
  149. for(const auto & b : *bonuses)
  150. ret.entries.emplace_back(b->val, b->Description());
  151. int dwellingBonus = 0;
  152. if(const PlayerState *p = cb->getPlayerState(tempOwner, false))
  153. {
  154. dwellingBonus = getDwellingBonus(creatures[level].second, p->dwellings);
  155. }
  156. if(dwellingBonus)
  157. ret.entries.emplace_back(VLC->generaltexth->allTexts[591], dwellingBonus); // \nExternal dwellings %+d
  158. if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
  159. ret.entries.emplace_back(subID, BuildingID::GRAIL, ret.totalGrowth() / 2);
  160. return ret;
  161. }
  162. int CGTownInstance::getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const
  163. {
  164. int totalBonus = 0;
  165. for (const auto& dwelling : dwellings)
  166. {
  167. for (const auto& creature : dwelling->creatures)
  168. {
  169. totalBonus += vstd::contains(creatureIds, creature.second[0]) ? 1 : 0;
  170. }
  171. }
  172. return totalBonus;
  173. }
  174. TResources CGTownInstance::dailyIncome() const
  175. {
  176. TResources ret;
  177. for(const auto & p : town->buildings)
  178. {
  179. BuildingID buildingUpgrade;
  180. for(const auto & p2 : town->buildings)
  181. {
  182. if (p2.second->upgrade == p.first)
  183. {
  184. buildingUpgrade = p2.first;
  185. }
  186. }
  187. if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
  188. {
  189. ret += p.second->produce;
  190. }
  191. }
  192. return ret;
  193. }
  194. bool CGTownInstance::hasFort() const
  195. {
  196. return hasBuilt(BuildingID::FORT);
  197. }
  198. bool CGTownInstance::hasCapitol() const
  199. {
  200. return hasBuilt(BuildingID::CAPITOL);
  201. }
  202. CGTownInstance::CGTownInstance():
  203. town(nullptr),
  204. builded(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(CRandomGenerator & 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(CRandomGenerator & 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(CRandomGenerator & 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(CRandomGenerator & 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(CRandomGenerator & 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 (visitingHero != nullptr)
  468. cb->visitCastleObjects(this, visitingHero);
  469. if (garrisonHero != nullptr)
  470. cb->visitCastleObjects(this, garrisonHero);
  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. default:
  654. assert(0);
  655. return false;
  656. }
  657. }
  658. std::vector<TradeItemBuy> CGTownInstance::availableItemsIds(EMarketMode mode) const
  659. {
  660. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  661. {
  662. std::vector<TradeItemBuy> ret;
  663. for(const CArtifact *a : merchantArtifacts)
  664. if(a)
  665. ret.push_back(a->getId());
  666. else
  667. ret.push_back(ArtifactID{});
  668. return ret;
  669. }
  670. else if ( mode == EMarketMode::RESOURCE_SKILL )
  671. {
  672. return universitySkills;
  673. }
  674. else
  675. return IMarket::availableItemsIds(mode);
  676. }
  677. void CGTownInstance::updateAppearance()
  678. {
  679. auto terrain = cb->gameState()->getTile(visitablePos())->terType->getId();
  680. //FIXME: not the best way to do this
  681. auto app = getObjectHandler()->getOverride(terrain, this);
  682. if (app)
  683. appearance = app;
  684. }
  685. std::string CGTownInstance::nodeName() const
  686. {
  687. return "Town (" + (town ? town->faction->getNameTranslated() : "unknown") + ") of " + getNameTranslated();
  688. }
  689. void CGTownInstance::deserializationFix()
  690. {
  691. attachTo(townAndVis);
  692. //Hero is already handled by CGameState::attachArmedObjects
  693. // if(visitingHero)
  694. // visitingHero->attachTo(&townAndVis);
  695. // if(garrisonHero)
  696. // garrisonHero->attachTo(this);
  697. }
  698. void CGTownInstance::updateMoraleBonusFromArmy()
  699. {
  700. auto b = getExportedBonusList().getFirst(Selector::sourceType()(BonusSource::ARMY).And(Selector::type()(BonusType::MORALE)));
  701. if(!b)
  702. {
  703. b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::MORALE, BonusSource::ARMY, 0, BonusSourceID());
  704. addNewBonus(b);
  705. }
  706. if (garrisonHero)
  707. {
  708. b->val = 0;
  709. CBonusSystemNode::treeHasChanged();
  710. }
  711. else
  712. CArmedInstance::updateMoraleBonusFromArmy();
  713. }
  714. void CGTownInstance::recreateBuildingsBonuses()
  715. {
  716. BonusList bl;
  717. getExportedBonusList().getBonuses(bl, Selector::sourceType()(BonusSource::TOWN_STRUCTURE));
  718. for(const auto & b : bl)
  719. removeBonus(b);
  720. for(const auto & bid : builtBuildings)
  721. {
  722. if(vstd::contains(overriddenBuildings, bid)) //tricky! -> checks tavern only if no bratherhood of sword
  723. continue;
  724. auto building = town->buildings.at(bid);
  725. if(building->buildingBonuses.empty())
  726. continue;
  727. for(auto & bonus : building->buildingBonuses)
  728. addNewBonus(bonus);
  729. }
  730. }
  731. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  732. {
  733. if(visitingHero.get() == h)
  734. return;
  735. if(h)
  736. {
  737. PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
  738. assert(p);
  739. h->detachFrom(*p);
  740. h->attachTo(townAndVis);
  741. visitingHero = h;
  742. h->visitedTown = this;
  743. h->inTownGarrison = false;
  744. }
  745. else
  746. {
  747. PlayerState *p = cb->gameState()->getPlayerState(visitingHero->tempOwner);
  748. visitingHero->visitedTown = nullptr;
  749. visitingHero->detachFrom(townAndVis);
  750. visitingHero->attachTo(*p);
  751. visitingHero = nullptr;
  752. }
  753. }
  754. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  755. {
  756. if(garrisonHero.get() == h)
  757. return;
  758. if(h)
  759. {
  760. PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
  761. assert(p);
  762. h->detachFrom(*p);
  763. h->attachTo(*this);
  764. garrisonHero = h;
  765. h->visitedTown = this;
  766. h->inTownGarrison = true;
  767. }
  768. else
  769. {
  770. PlayerState *p = cb->gameState()->getPlayerState(garrisonHero->tempOwner);
  771. garrisonHero->visitedTown = nullptr;
  772. garrisonHero->inTownGarrison = false;
  773. garrisonHero->detachFrom(*this);
  774. garrisonHero->attachTo(*p);
  775. garrisonHero = nullptr;
  776. }
  777. updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
  778. }
  779. bool CGTownInstance::armedGarrison() const
  780. {
  781. return !stacks.empty() || garrisonHero;
  782. }
  783. const CTown * CGTownInstance::getTown() const
  784. {
  785. if(ID == Obj::RANDOM_TOWN)
  786. return VLC->townh->randomTown;
  787. else
  788. {
  789. if(nullptr == town)
  790. {
  791. return (*VLC->townh)[getFaction()]->town;
  792. }
  793. else
  794. return town;
  795. }
  796. }
  797. int CGTownInstance::getTownLevel() const
  798. {
  799. // count all buildings that are not upgrades
  800. int level = 0;
  801. for(const auto & bid : builtBuildings)
  802. {
  803. if(town->buildings.at(bid)->upgrade == BuildingID::NONE)
  804. level++;
  805. }
  806. return level;
  807. }
  808. CBonusSystemNode & CGTownInstance::whatShouldBeAttached()
  809. {
  810. return townAndVis;
  811. }
  812. std::string CGTownInstance::getNameTranslated() const
  813. {
  814. return VLC->generaltexth->translate(nameTextId);
  815. }
  816. std::string CGTownInstance::getNameTextID() const
  817. {
  818. return nameTextId;
  819. }
  820. void CGTownInstance::setNameTextId( const std::string & newName )
  821. {
  822. nameTextId = newName;
  823. }
  824. const CArmedInstance * CGTownInstance::getUpperArmy() const
  825. {
  826. if(garrisonHero)
  827. return garrisonHero;
  828. return this;
  829. }
  830. std::vector<const CGTownBuilding *> CGTownInstance::getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const
  831. {
  832. std::vector<const CGTownBuilding *> ret;
  833. for(auto * const building : bonusingBuildings)
  834. {
  835. if(building->getBuildingSubtype() == subId)
  836. ret.push_back(building);
  837. }
  838. return ret;
  839. }
  840. bool CGTownInstance::hasBuiltSomeTradeBuilding() const
  841. {
  842. for(const auto & bid : builtBuildings)
  843. {
  844. if(town->buildings.at(bid)->IsTradeBuilding())
  845. return true;
  846. }
  847. return false;
  848. }
  849. bool CGTownInstance::hasBuilt(BuildingSubID::EBuildingSubID buildingID) const
  850. {
  851. for(const auto & bid : builtBuildings)
  852. {
  853. if(town->buildings.at(bid)->subId == buildingID)
  854. return true;
  855. }
  856. return false;
  857. }
  858. bool CGTownInstance::hasBuilt(const BuildingID & buildingID) const
  859. {
  860. return vstd::contains(builtBuildings, buildingID);
  861. }
  862. bool CGTownInstance::hasBuilt(const BuildingID & buildingID, FactionID townID) const
  863. {
  864. if (townID == town->faction->getId() || townID == FactionID::ANY)
  865. return hasBuilt(buildingID);
  866. return false;
  867. }
  868. TResources CGTownInstance::getBuildingCost(const BuildingID & buildingID) const
  869. {
  870. if (vstd::contains(town->buildings, buildingID))
  871. return town->buildings.at(buildingID)->resources;
  872. else
  873. {
  874. logGlobal->error("Town %s at %s has no possible building %d!", getNameTranslated(), pos.toString(), buildingID.toEnum());
  875. return TResources();
  876. }
  877. }
  878. CBuilding::TRequired CGTownInstance::genBuildingRequirements(const BuildingID & buildID, bool deep) const
  879. {
  880. const CBuilding * building = town->buildings.at(buildID);
  881. //TODO: find better solution to prevent infinite loops
  882. std::set<BuildingID> processed;
  883. std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
  884. [&](const BuildingID & id) -> CBuilding::TRequired::Variant
  885. {
  886. if (town->buildings.count(id) == 0)
  887. {
  888. logMod->error("Invalid building ID %d in building dependencies!", id.getNum());
  889. return CBuilding::TRequired::OperatorAll();
  890. }
  891. const CBuilding * build = town->buildings.at(id);
  892. CBuilding::TRequired::OperatorAll requirements;
  893. if (!hasBuilt(id))
  894. {
  895. if (deep)
  896. requirements.expressions.emplace_back(id);
  897. else
  898. return id;
  899. }
  900. if(!vstd::contains(processed, id))
  901. {
  902. processed.insert(id);
  903. if (build->upgrade != BuildingID::NONE)
  904. requirements.expressions.push_back(dependTest(build->upgrade));
  905. requirements.expressions.push_back(build->requirements.morph(dependTest));
  906. }
  907. return requirements;
  908. };
  909. CBuilding::TRequired::OperatorAll requirements;
  910. if (building->upgrade != BuildingID::NONE)
  911. {
  912. const CBuilding * upgr = town->buildings.at(building->upgrade);
  913. requirements.expressions.push_back(dependTest(upgr->bid));
  914. processed.clear();
  915. }
  916. requirements.expressions.push_back(building->requirements.morph(dependTest));
  917. CBuilding::TRequired::Variant variant(requirements);
  918. CBuilding::TRequired ret(variant);
  919. ret.minimize();
  920. return ret;
  921. }
  922. void CGTownInstance::addHeroToStructureVisitors(const CGHeroInstance *h, si64 structureInstanceID ) const
  923. {
  924. if(visitingHero == h)
  925. cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  926. else if(garrisonHero == h)
  927. cb->setObjPropertyValue(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  928. else
  929. {
  930. //should never ever happen
  931. logGlobal->error("Cannot add hero %s to visitors of structure # %d", h->getNameTranslated(), structureInstanceID);
  932. throw std::runtime_error("unexpected hero in CGTownInstance::addHeroToStructureVisitors");
  933. }
  934. }
  935. void CGTownInstance::battleFinished(const CGHeroInstance * hero, const BattleResult & result) const
  936. {
  937. if(result.winner == BattleSide::ATTACKER)
  938. {
  939. clearArmy();
  940. onTownCaptured(hero->getOwner());
  941. }
  942. }
  943. void CGTownInstance::onTownCaptured(const PlayerColor & winner) const
  944. {
  945. setOwner(winner);
  946. cb->changeFogOfWar(getSightCenter(), getSightRadius(), winner, ETileVisibility::REVEALED);
  947. }
  948. void CGTownInstance::afterAddToMap(CMap * map)
  949. {
  950. map->towns.emplace_back(this);
  951. }
  952. void CGTownInstance::afterRemoveFromMap(CMap * map)
  953. {
  954. vstd::erase_if_present(map->towns, this);
  955. }
  956. void CGTownInstance::reset()
  957. {
  958. CGTownInstance::merchantArtifacts.clear();
  959. CGTownInstance::universitySkills.clear();
  960. }
  961. void CGTownInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  962. {
  963. CGObjectInstance::serializeJsonOwner(handler);
  964. if(!handler.saving)
  965. handler.serializeEnum("tightFormation", formation, NArmyFormation::names); //for old format
  966. CArmedInstance::serializeJsonOptions(handler);
  967. handler.serializeString("name", nameTextId);
  968. {
  969. auto decodeBuilding = [this](const std::string & identifier) -> si32
  970. {
  971. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), getTown()->getBuildingScope(), identifier);
  972. if(rawId)
  973. return rawId.value();
  974. else
  975. return -1;
  976. };
  977. auto encodeBuilding = [this](si32 index) -> std::string
  978. {
  979. return getTown()->buildings.at(BuildingID(index))->getJsonKey();
  980. };
  981. const std::set<si32> standard = getTown()->getAllBuildings();//by default all buildings are allowed
  982. JsonSerializeFormat::LICSet buildingsLIC(standard, decodeBuilding, encodeBuilding);
  983. if(handler.saving)
  984. {
  985. bool customBuildings = false;
  986. boost::logic::tribool hasFort(false);
  987. for(const BuildingID & id : forbiddenBuildings)
  988. {
  989. buildingsLIC.none.insert(id.getNum());
  990. customBuildings = true;
  991. }
  992. for(const BuildingID & id : builtBuildings)
  993. {
  994. if(id == BuildingID::DEFAULT)
  995. continue;
  996. const CBuilding * building = getTown()->buildings.at(id);
  997. if(building->mode == CBuilding::BUILD_AUTO)
  998. continue;
  999. if(id == BuildingID::FORT)
  1000. hasFort = true;
  1001. buildingsLIC.all.insert(id.getNum());
  1002. customBuildings = true;
  1003. }
  1004. if(customBuildings)
  1005. handler.serializeLIC("buildings", buildingsLIC);
  1006. else
  1007. handler.serializeBool("hasFort",hasFort);
  1008. }
  1009. else
  1010. {
  1011. handler.serializeLIC("buildings", buildingsLIC);
  1012. builtBuildings.insert(BuildingID::VILLAGE_HALL);
  1013. if(buildingsLIC.none.empty() && buildingsLIC.all.empty())
  1014. {
  1015. builtBuildings.insert(BuildingID::DEFAULT);
  1016. bool hasFort = false;
  1017. handler.serializeBool("hasFort",hasFort);
  1018. if(hasFort)
  1019. builtBuildings.insert(BuildingID::FORT);
  1020. }
  1021. else
  1022. {
  1023. for(const si32 item : buildingsLIC.none)
  1024. forbiddenBuildings.insert(BuildingID(item));
  1025. for(const si32 item : buildingsLIC.all)
  1026. builtBuildings.insert(BuildingID(item));
  1027. }
  1028. }
  1029. }
  1030. {
  1031. handler.serializeIdArray( "possibleSpells", possibleSpells);
  1032. handler.serializeIdArray( "obligatorySpells", obligatorySpells);
  1033. }
  1034. }
  1035. FactionID CGTownInstance::getFaction() const
  1036. {
  1037. return FactionID(subID.getNum());
  1038. }
  1039. TerrainId CGTownInstance::getNativeTerrain() const
  1040. {
  1041. return town->faction->getNativeTerrain();
  1042. }
  1043. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1044. : count(_count)
  1045. {
  1046. MetaString formatter;
  1047. formatter.appendRawString(format);
  1048. formatter.replacePositiveNumber(count);
  1049. description = formatter.toString();
  1050. }
  1051. GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count)
  1052. {
  1053. MetaString formatter;
  1054. formatter.appendRawString("%s %+d");
  1055. formatter.replaceRawString((*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated());
  1056. formatter.replacePositiveNumber(count);
  1057. description = formatter.toString();
  1058. }
  1059. GrowthInfo::Entry::Entry(int _count, std::string fullDescription):
  1060. count(_count),
  1061. description(std::move(fullDescription))
  1062. {
  1063. }
  1064. CTownAndVisitingHero::CTownAndVisitingHero()
  1065. {
  1066. setNodeType(TOWN_AND_VISITOR);
  1067. }
  1068. int GrowthInfo::totalGrowth() const
  1069. {
  1070. int ret = 0;
  1071. for(const Entry &entry : entries)
  1072. ret += entry.count;
  1073. return ret;
  1074. }
  1075. void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
  1076. {
  1077. for(const CGTownInstance::TCreaturesSet::value_type & dwelling : creatures)
  1078. {
  1079. if (vstd::contains(dwelling.second, stack.type->getId())) //Dwelling with our creature
  1080. {
  1081. for(const auto & upgrID : dwelling.second)
  1082. {
  1083. if(vstd::contains(stack.type->upgrades, upgrID)) //possible upgrade
  1084. {
  1085. info.newID.push_back(upgrID);
  1086. info.cost.push_back(upgrID.toCreature()->getFullRecruitCost() - stack.type->getFullRecruitCost());
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. VCMI_LIB_NAMESPACE_END