CGTownInstance.cpp 34 KB

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