CGTownInstance.cpp 34 KB

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