CGTownInstance.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. int CGTownInstance::getSightRadius() const //returns sight distance
  34. {
  35. auto ret = CBuilding::HEIGHT_NO_TOWER;
  36. for(const auto & bid : builtBuildings)
  37. {
  38. if(bid.IsSpecialOrGrail())
  39. {
  40. auto height = town->buildings.at(bid)->height;
  41. if(ret < height)
  42. ret = height;
  43. }
  44. }
  45. return ret;
  46. }
  47. void CGTownInstance::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
  48. {
  49. ///this is freakin' overcomplicated solution
  50. switch (what)
  51. {
  52. case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
  53. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, visitingHero->id);
  54. break;
  55. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  56. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, NumericID(0));
  57. break;
  58. case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
  59. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::VISITORS, garrisonHero->id);
  60. break;
  61. case ObjProperty::BONUS_VALUE_FIRST:
  62. bonusValue.first = identifier.getNum();
  63. break;
  64. case ObjProperty::BONUS_VALUE_SECOND:
  65. bonusValue.second = identifier.getNum();
  66. break;
  67. case ObjProperty::REWARD_RANDOMIZE:
  68. bonusingBuildings[identifier.getNum()]->setProperty(ObjProperty::REWARD_RANDOMIZE, NumericID(0));
  69. break;
  70. }
  71. }
  72. CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  73. {
  74. if (hasBuilt(BuildingID::CASTLE))
  75. return CASTLE;
  76. if (hasBuilt(BuildingID::CITADEL))
  77. return CITADEL;
  78. if (hasBuilt(BuildingID::FORT))
  79. return FORT;
  80. return NONE;
  81. }
  82. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  83. {
  84. if (hasBuilt(BuildingID::CAPITOL))
  85. return 3;
  86. if (hasBuilt(BuildingID::CITY_HALL))
  87. return 2;
  88. if (hasBuilt(BuildingID::TOWN_HALL))
  89. return 1;
  90. if (hasBuilt(BuildingID::VILLAGE_HALL))
  91. return 0;
  92. return -1;
  93. }
  94. int CGTownInstance::mageGuildLevel() const
  95. {
  96. if (hasBuilt(BuildingID::MAGES_GUILD_5))
  97. return 5;
  98. if (hasBuilt(BuildingID::MAGES_GUILD_4))
  99. return 4;
  100. if (hasBuilt(BuildingID::MAGES_GUILD_3))
  101. return 3;
  102. if (hasBuilt(BuildingID::MAGES_GUILD_2))
  103. return 2;
  104. if (hasBuilt(BuildingID::MAGES_GUILD_1))
  105. return 1;
  106. return 0;
  107. }
  108. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  109. {
  110. return town->hordeLvl.at(HID);
  111. }
  112. int CGTownInstance::creatureGrowth(const int & level) const
  113. {
  114. return getGrowthInfo(level).totalGrowth();
  115. }
  116. GrowthInfo CGTownInstance::getGrowthInfo(int level) const
  117. {
  118. GrowthInfo ret;
  119. if (level<0 || level >=GameConstants::CREATURES_PER_TOWN)
  120. return ret;
  121. if (creatures[level].second.empty())
  122. return ret; //no dwelling
  123. const Creature *creature = creatures[level].second.back().toEntity(VLC);
  124. const int base = creature->getGrowth();
  125. int castleBonus = 0;
  126. ret.entries.emplace_back(VLC->generaltexth->allTexts[590], base); // \n\nBasic growth %d"
  127. if (hasBuilt(BuildingID::CASTLE))
  128. ret.entries.emplace_back(subID, BuildingID::CASTLE, castleBonus = base);
  129. else if (hasBuilt(BuildingID::CITADEL))
  130. ret.entries.emplace_back(subID, BuildingID::CITADEL, castleBonus = base / 2);
  131. if(town->hordeLvl.at(0) == level)//horde 1
  132. if(hasBuilt(BuildingID::HORDE_1))
  133. ret.entries.emplace_back(subID, BuildingID::HORDE_1, creature->getHorde());
  134. if(town->hordeLvl.at(1) == level)//horde 2
  135. if(hasBuilt(BuildingID::HORDE_2))
  136. ret.entries.emplace_back(subID, BuildingID::HORDE_2, creature->getHorde());
  137. //statue-of-legion-like bonus: % to base+castle
  138. TConstBonusListPtr bonuses2 = getBonuses(Selector::type()(BonusType::CREATURE_GROWTH_PERCENT));
  139. for(const auto & b : *bonuses2)
  140. {
  141. const auto growth = b->val * (base + castleBonus) / 100;
  142. ret.entries.emplace_back(growth, b->Description(growth));
  143. }
  144. //other *-of-legion-like bonuses (%d to growth cumulative with grail)
  145. // Note: bonus uses 1-based levels (Pikeman is level 1), town list uses 0-based (Pikeman in 0-th creatures entry)
  146. TConstBonusListPtr bonuses = getBonuses(Selector::typeSubtype(BonusType::CREATURE_GROWTH, BonusCustomSubtype::creatureLevel(level+1)));
  147. for(const auto & b : *bonuses)
  148. ret.entries.emplace_back(b->val, b->Description());
  149. int dwellingBonus = 0;
  150. if(const PlayerState *p = cb->getPlayerState(tempOwner, false))
  151. {
  152. dwellingBonus = getDwellingBonus(creatures[level].second, p->dwellings);
  153. }
  154. if(dwellingBonus)
  155. ret.entries.emplace_back(VLC->generaltexth->allTexts[591], dwellingBonus); // \nExternal dwellings %+d
  156. if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
  157. ret.entries.emplace_back(subID, BuildingID::GRAIL, ret.totalGrowth() / 2);
  158. return ret;
  159. }
  160. int CGTownInstance::getDwellingBonus(const std::vector<CreatureID>& creatureIds, const std::vector<ConstTransitivePtr<CGDwelling> >& dwellings) const
  161. {
  162. int totalBonus = 0;
  163. for (const auto& dwelling : dwellings)
  164. {
  165. for (const auto& creature : dwelling->creatures)
  166. {
  167. totalBonus += vstd::contains(creatureIds, creature.second[0]) ? 1 : 0;
  168. }
  169. }
  170. return totalBonus;
  171. }
  172. TResources CGTownInstance::dailyIncome() const
  173. {
  174. TResources ret;
  175. for(const auto & p : town->buildings)
  176. {
  177. BuildingID buildingUpgrade;
  178. for(const auto & p2 : town->buildings)
  179. {
  180. if (p2.second->upgrade == p.first)
  181. {
  182. buildingUpgrade = p2.first;
  183. }
  184. }
  185. if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
  186. {
  187. ret += p.second->produce;
  188. }
  189. }
  190. return ret;
  191. }
  192. bool CGTownInstance::hasFort() const
  193. {
  194. return hasBuilt(BuildingID::FORT);
  195. }
  196. bool CGTownInstance::hasCapitol() const
  197. {
  198. return hasBuilt(BuildingID::CAPITOL);
  199. }
  200. CGTownInstance::CGTownInstance(IGameCallback *cb):
  201. CGDwelling(cb),
  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 : cb->gameState()->map->townMerchantArtifacts)
  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 cb->gameState()->map->townUniversitySkills;
  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::serializeJsonOptions(JsonSerializeFormat & handler)
  956. {
  957. CGObjectInstance::serializeJsonOwner(handler);
  958. if(!handler.saving)
  959. handler.serializeEnum("tightFormation", formation, NArmyFormation::names); //for old format
  960. CArmedInstance::serializeJsonOptions(handler);
  961. handler.serializeString("name", nameTextId);
  962. {
  963. auto decodeBuilding = [this](const std::string & identifier) -> si32
  964. {
  965. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), getTown()->getBuildingScope(), identifier);
  966. if(rawId)
  967. return rawId.value();
  968. else
  969. return -1;
  970. };
  971. auto encodeBuilding = [this](si32 index) -> std::string
  972. {
  973. return getTown()->buildings.at(BuildingID(index))->getJsonKey();
  974. };
  975. const std::set<si32> standard = getTown()->getAllBuildings();//by default all buildings are allowed
  976. JsonSerializeFormat::LICSet buildingsLIC(standard, decodeBuilding, encodeBuilding);
  977. if(handler.saving)
  978. {
  979. bool customBuildings = false;
  980. boost::logic::tribool hasFort(false);
  981. for(const BuildingID & id : forbiddenBuildings)
  982. {
  983. buildingsLIC.none.insert(id.getNum());
  984. customBuildings = true;
  985. }
  986. for(const BuildingID & id : builtBuildings)
  987. {
  988. if(id == BuildingID::DEFAULT)
  989. continue;
  990. const CBuilding * building = getTown()->buildings.at(id);
  991. if(building->mode == CBuilding::BUILD_AUTO)
  992. continue;
  993. if(id == BuildingID::FORT)
  994. hasFort = true;
  995. buildingsLIC.all.insert(id.getNum());
  996. customBuildings = true;
  997. }
  998. if(customBuildings)
  999. handler.serializeLIC("buildings", buildingsLIC);
  1000. else
  1001. handler.serializeBool("hasFort",hasFort);
  1002. }
  1003. else
  1004. {
  1005. handler.serializeLIC("buildings", buildingsLIC);
  1006. builtBuildings.insert(BuildingID::VILLAGE_HALL);
  1007. if(buildingsLIC.none.empty() && buildingsLIC.all.empty())
  1008. {
  1009. builtBuildings.insert(BuildingID::DEFAULT);
  1010. bool hasFort = false;
  1011. handler.serializeBool("hasFort",hasFort);
  1012. if(hasFort)
  1013. builtBuildings.insert(BuildingID::FORT);
  1014. }
  1015. else
  1016. {
  1017. for(const si32 item : buildingsLIC.none)
  1018. forbiddenBuildings.insert(BuildingID(item));
  1019. for(const si32 item : buildingsLIC.all)
  1020. builtBuildings.insert(BuildingID(item));
  1021. }
  1022. }
  1023. }
  1024. {
  1025. handler.serializeIdArray( "possibleSpells", possibleSpells);
  1026. handler.serializeIdArray( "obligatorySpells", obligatorySpells);
  1027. }
  1028. }
  1029. FactionID CGTownInstance::getFaction() const
  1030. {
  1031. return FactionID(subID.getNum());
  1032. }
  1033. TerrainId CGTownInstance::getNativeTerrain() const
  1034. {
  1035. return town->faction->getNativeTerrain();
  1036. }
  1037. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1038. : count(_count)
  1039. {
  1040. MetaString formatter;
  1041. formatter.appendRawString(format);
  1042. formatter.replacePositiveNumber(count);
  1043. description = formatter.toString();
  1044. }
  1045. GrowthInfo::Entry::Entry(int subID, const BuildingID & building, int _count): count(_count)
  1046. {
  1047. MetaString formatter;
  1048. formatter.appendRawString("%s %+d");
  1049. formatter.replaceRawString((*VLC->townh)[subID]->town->buildings.at(building)->getNameTranslated());
  1050. formatter.replacePositiveNumber(count);
  1051. description = formatter.toString();
  1052. }
  1053. GrowthInfo::Entry::Entry(int _count, std::string fullDescription):
  1054. count(_count),
  1055. description(std::move(fullDescription))
  1056. {
  1057. }
  1058. CTownAndVisitingHero::CTownAndVisitingHero()
  1059. {
  1060. setNodeType(TOWN_AND_VISITOR);
  1061. }
  1062. int GrowthInfo::totalGrowth() const
  1063. {
  1064. int ret = 0;
  1065. for(const Entry &entry : entries)
  1066. ret += entry.count;
  1067. return ret;
  1068. }
  1069. void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const
  1070. {
  1071. for(const CGTownInstance::TCreaturesSet::value_type & dwelling : creatures)
  1072. {
  1073. if (vstd::contains(dwelling.second, stack.type->getId())) //Dwelling with our creature
  1074. {
  1075. for(const auto & upgrID : dwelling.second)
  1076. {
  1077. if(vstd::contains(stack.type->upgrades, upgrID)) //possible upgrade
  1078. {
  1079. info.newID.push_back(upgrID);
  1080. info.cost.push_back(upgrID.toCreature()->getFullRecruitCost() - stack.type->getFullRecruitCost());
  1081. }
  1082. }
  1083. }
  1084. }
  1085. }
  1086. VCMI_LIB_NAMESPACE_END