CGTownInstance.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 "CObjectClassesHandler.h"
  13. #include "../NetPacks.h"
  14. #include "../CGeneralTextHandler.h"
  15. #include "../CModHandler.h"
  16. #include "../IGameCallback.h"
  17. #include "../CGameState.h"
  18. std::vector<const CArtifact *> CGTownInstance::merchantArtifacts;
  19. std::vector<int> CGTownInstance::universitySkills;
  20. void CGDwelling::initObj()
  21. {
  22. switch(ID)
  23. {
  24. case Obj::CREATURE_GENERATOR1:
  25. case Obj::CREATURE_GENERATOR4:
  26. {
  27. VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, cb->gameState()->getRandomGenerator());
  28. if (getOwner() != PlayerColor::NEUTRAL)
  29. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  30. assert(!creatures.empty());
  31. assert(!creatures[0].second.empty());
  32. break;
  33. }
  34. case Obj::REFUGEE_CAMP:
  35. //is handled within newturn func
  36. break;
  37. case Obj::WAR_MACHINE_FACTORY:
  38. creatures.resize(3);
  39. creatures[0].second.push_back(CreatureID::BALLISTA);
  40. creatures[1].second.push_back(CreatureID::FIRST_AID_TENT);
  41. creatures[2].second.push_back(CreatureID::AMMO_CART);
  42. break;
  43. default:
  44. assert(0);
  45. break;
  46. }
  47. }
  48. void CGDwelling::setPropertyDer(ui8 what, ui32 val)
  49. {
  50. switch (what)
  51. {
  52. case ObjProperty::OWNER: //change owner
  53. if (ID == Obj::CREATURE_GENERATOR1) //single generators
  54. {
  55. if (tempOwner != PlayerColor::NEUTRAL)
  56. {
  57. std::vector<ConstTransitivePtr<CGDwelling> >* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  58. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  59. }
  60. if (PlayerColor(val) != PlayerColor::NEUTRAL) //can new owner be neutral?
  61. cb->gameState()->players[PlayerColor(val)].dwellings.push_back (this);
  62. }
  63. break;
  64. case ObjProperty::AVAILABLE_CREATURE:
  65. creatures.resize(1);
  66. creatures[0].second.resize(1);
  67. creatures[0].second[0] = CreatureID(val);
  68. break;
  69. }
  70. }
  71. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  72. {
  73. if(ID == Obj::REFUGEE_CAMP && !creatures[0].first) //Refugee Camp, no available cres
  74. {
  75. InfoWindow iw;
  76. iw.player = h->tempOwner;
  77. iw.text.addTxt(MetaString::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  78. iw.text.addReplacement(MetaString::OBJ_NAMES, ID);
  79. cb->sendAndApply(&iw);
  80. return;
  81. }
  82. PlayerRelations::PlayerRelations relations = cb->gameState()->getPlayerRelations( h->tempOwner, tempOwner );
  83. if ( relations == PlayerRelations::ALLIES )
  84. return;//do not allow recruiting or capturing
  85. if( !relations && stacksCount() > 0) //object is guarded, owned by enemy
  86. {
  87. BlockingDialog bd(true,false);
  88. bd.player = h->tempOwner;
  89. bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  90. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  91. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + Slots().begin()->second->getQuantityID()*3);
  92. bd.text.addReplacement(*Slots().begin()->second);
  93. cb->showBlockingDialog(&bd);
  94. return;
  95. }
  96. if(!relations && ID != Obj::WAR_MACHINE_FACTORY)
  97. {
  98. cb->setOwner(this, h->tempOwner);
  99. }
  100. BlockingDialog bd (true,false);
  101. bd.player = h->tempOwner;
  102. if(ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4)
  103. {
  104. bd.text.addTxt(MetaString::ADVOB_TXT, ID == Obj::CREATURE_GENERATOR1 ? 35 : 36); //{%s} Would you like to recruit %s? / {%s} Would you like to recruit %s, %s, %s, or %s?
  105. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  106. for(auto & elem : creatures)
  107. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  108. }
  109. else if(ID == Obj::REFUGEE_CAMP)
  110. {
  111. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  112. bd.text.addReplacement(MetaString::OBJ_NAMES, ID);
  113. for(auto & elem : creatures)
  114. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  115. }
  116. else if(ID == Obj::WAR_MACHINE_FACTORY)
  117. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  118. else
  119. throw std::runtime_error("Illegal dwelling!");
  120. cb->showBlockingDialog(&bd);
  121. }
  122. void CGDwelling::newTurn() const
  123. {
  124. if(cb->getDate(Date::DAY_OF_WEEK) != 1) //not first day of week
  125. return;
  126. //town growths and War Machines Factories are handled separately
  127. if(ID == Obj::TOWN || ID == Obj::WAR_MACHINE_FACTORY)
  128. return;
  129. if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
  130. {
  131. cb->setObjProperty(id, ObjProperty::AVAILABLE_CREATURE, VLC->creh->pickRandomMonster(cb->gameState()->getRandomGenerator()));
  132. }
  133. bool change = false;
  134. SetAvailableCreatures sac;
  135. sac.creatures = creatures;
  136. sac.tid = id;
  137. for (size_t i = 0; i < creatures.size(); i++)
  138. {
  139. if(creatures[i].second.size())
  140. {
  141. CCreature *cre = VLC->creh->creatures[creatures[i].second[0]];
  142. TQuantity amount = cre->growth * (1 + cre->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(Bonus::CREATURE_GROWTH);
  143. if (VLC->modh->settings.DWELLINGS_ACCUMULATE_CREATURES && ID != Obj::REFUGEE_CAMP) //camp should not try to accumulate different kinds of creatures
  144. sac.creatures[i].first += amount;
  145. else
  146. sac.creatures[i].first = amount;
  147. change = true;
  148. }
  149. }
  150. if(change)
  151. cb->sendAndApply(&sac);
  152. }
  153. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h) const
  154. {
  155. CreatureID crid = creatures[0].second[0];
  156. CCreature *crs = VLC->creh->creatures[crid];
  157. TQuantity count = creatures[0].first;
  158. if(crs->level == 1 && ID != Obj::REFUGEE_CAMP) //first level - creatures are for free
  159. {
  160. if(count) //there are available creatures
  161. {
  162. SlotID slot = h->getSlotFor(crid);
  163. if(!slot.validSlot()) //no available slot
  164. {
  165. InfoWindow iw;
  166. iw.player = h->tempOwner;
  167. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  168. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  169. cb->showInfoDialog(&iw);
  170. }
  171. else //give creatures
  172. {
  173. SetAvailableCreatures sac;
  174. sac.tid = id;
  175. sac.creatures = creatures;
  176. sac.creatures[0].first = 0;
  177. InfoWindow iw;
  178. iw.player = h->tempOwner;
  179. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  180. iw.text.addReplacement(count);
  181. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  182. cb->showInfoDialog(&iw);
  183. cb->sendAndApply(&sac);
  184. cb->addToSlot(StackLocation(h, slot), crs, count);
  185. }
  186. }
  187. else //there no creatures
  188. {
  189. InfoWindow iw;
  190. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  191. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  192. iw.player = h->tempOwner;
  193. cb->sendAndApply(&iw);
  194. }
  195. }
  196. else
  197. {
  198. if(ID == Obj::WAR_MACHINE_FACTORY) //pick available War Machines
  199. {
  200. //there is 1 war machine available to recruit if hero doesn't have one
  201. SetAvailableCreatures sac;
  202. sac.tid = id;
  203. sac.creatures = creatures;
  204. sac.creatures[0].first = !h->getArt(ArtifactPosition::MACH1); //ballista
  205. sac.creatures[1].first = !h->getArt(ArtifactPosition::MACH3); //first aid tent
  206. sac.creatures[2].first = !h->getArt(ArtifactPosition::MACH2); //ammo cart
  207. cb->sendAndApply(&sac);
  208. }
  209. OpenWindow ow;
  210. ow.id1 = id.getNum();
  211. ow.id2 = h->id.getNum();
  212. ow.window = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP)
  213. ? OpenWindow::RECRUITMENT_FIRST
  214. : OpenWindow::RECRUITMENT_ALL;
  215. cb->sendAndApply(&ow);
  216. }
  217. }
  218. void CGDwelling::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  219. {
  220. if (result.winner == 0)
  221. {
  222. onHeroVisit(hero);
  223. }
  224. }
  225. void CGDwelling::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  226. {
  227. auto relations = cb->getPlayerRelations(getOwner(), hero->getOwner());
  228. if(stacksCount() > 0 && relations == PlayerRelations::ENEMIES) //guards present
  229. {
  230. if(answer)
  231. cb->startBattleI(hero, this);
  232. }
  233. else if(answer)
  234. {
  235. heroAcceptsCreatures(hero);
  236. }
  237. }
  238. int CGTownInstance::getSightRadious() const //returns sight distance
  239. {
  240. if (subID == ETownType::TOWER)
  241. {
  242. if (hasBuilt(BuildingID::GRAIL)) //skyship
  243. return -1; //entire map
  244. if (hasBuilt(BuildingID::LOOKOUT_TOWER)) //lookout tower
  245. return 20;
  246. }
  247. return 5;
  248. }
  249. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  250. {
  251. ///this is freakin' overcomplicated solution
  252. switch (what)
  253. {
  254. case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
  255. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id.getNum());
  256. break;
  257. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  258. bonusingBuildings[val]->setProperty (ObjProperty::STRUCTURE_CLEAR_VISITORS, 0);
  259. break;
  260. case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
  261. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id.getNum());
  262. break;
  263. case ObjProperty::BONUS_VALUE_FIRST:
  264. bonusValue.first = val;
  265. break;
  266. case ObjProperty::BONUS_VALUE_SECOND:
  267. bonusValue.second = val;
  268. break;
  269. }
  270. }
  271. CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  272. {
  273. if (hasBuilt(BuildingID::CASTLE))
  274. return CASTLE;
  275. if (hasBuilt(BuildingID::CITADEL))
  276. return CITADEL;
  277. if (hasBuilt(BuildingID::FORT))
  278. return FORT;
  279. return NONE;
  280. }
  281. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  282. {
  283. if (hasBuilt(BuildingID::CAPITOL))
  284. return 3;
  285. if (hasBuilt(BuildingID::CITY_HALL))
  286. return 2;
  287. if (hasBuilt(BuildingID::TOWN_HALL))
  288. return 1;
  289. if (hasBuilt(BuildingID::VILLAGE_HALL))
  290. return 0;
  291. return -1;
  292. }
  293. int CGTownInstance::mageGuildLevel() const
  294. {
  295. if (hasBuilt(BuildingID::MAGES_GUILD_5))
  296. return 5;
  297. if (hasBuilt(BuildingID::MAGES_GUILD_4))
  298. return 4;
  299. if (hasBuilt(BuildingID::MAGES_GUILD_3))
  300. return 3;
  301. if (hasBuilt(BuildingID::MAGES_GUILD_2))
  302. return 2;
  303. if (hasBuilt(BuildingID::MAGES_GUILD_1))
  304. return 1;
  305. return 0;
  306. }
  307. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  308. {
  309. return town->hordeLvl.at(HID);
  310. }
  311. int CGTownInstance::creatureGrowth(const int & level) const
  312. {
  313. return getGrowthInfo(level).totalGrowth();
  314. }
  315. GrowthInfo CGTownInstance::getGrowthInfo(int level) const
  316. {
  317. GrowthInfo ret;
  318. if (level<0 || level >=GameConstants::CREATURES_PER_TOWN)
  319. return ret;
  320. if (creatures[level].second.empty())
  321. return ret; //no dwelling
  322. const CCreature *creature = VLC->creh->creatures[creatures[level].second.back()];
  323. const int base = creature->growth;
  324. int castleBonus = 0;
  325. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[590], base));// \n\nBasic growth %d"
  326. if (hasBuilt(BuildingID::CASTLE))
  327. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::CASTLE, castleBonus = base));
  328. else if (hasBuilt(BuildingID::CITADEL))
  329. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::CITADEL, castleBonus = base / 2));
  330. if(town->hordeLvl.at(0) == level)//horde 1
  331. if(hasBuilt(BuildingID::HORDE_1))
  332. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::HORDE_1, creature->hordeGrowth));
  333. if(town->hordeLvl.at(1) == level)//horde 2
  334. if(hasBuilt(BuildingID::HORDE_2))
  335. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::HORDE_2, creature->hordeGrowth));
  336. int dwellingBonus = 0;
  337. if(const PlayerState *p = cb->getPlayer(tempOwner, false))
  338. {
  339. for(const CGDwelling *dwelling : p->dwellings)
  340. if(vstd::contains(creatures[level].second, dwelling->creatures[0].second[0]))
  341. dwellingBonus++;
  342. }
  343. if(dwellingBonus)
  344. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[591], dwellingBonus));// \nExternal dwellings %+d
  345. //other *-of-legion-like bonuses (%d to growth cumulative with grail)
  346. TBonusListPtr bonuses = getBonuses(Selector::type(Bonus::CREATURE_GROWTH).And(Selector::subtype(level)));
  347. for(const Bonus *b : *bonuses)
  348. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val));
  349. //statue-of-legion-like bonus: % to base+castle
  350. TBonusListPtr bonuses2 = getBonuses(Selector::type(Bonus::CREATURE_GROWTH_PERCENT));
  351. for(const Bonus *b : *bonuses2)
  352. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val * (base + castleBonus) / 100));
  353. if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
  354. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::GRAIL, ret.totalGrowth() / 2));
  355. return ret;
  356. }
  357. TResources CGTownInstance::dailyIncome() const
  358. {
  359. TResources ret;
  360. for (auto & p : town->buildings)
  361. {
  362. BuildingID buildingUpgrade;
  363. for (auto & p2 : town->buildings)
  364. {
  365. if (p2.second->upgrade == p.first)
  366. {
  367. buildingUpgrade = p2.first;
  368. }
  369. }
  370. if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
  371. {
  372. ret += p.second->produce;
  373. }
  374. }
  375. return ret;
  376. }
  377. bool CGTownInstance::hasFort() const
  378. {
  379. return hasBuilt(BuildingID::FORT);
  380. }
  381. bool CGTownInstance::hasCapitol() const
  382. {
  383. return hasBuilt(BuildingID::CAPITOL);
  384. }
  385. CGTownInstance::CGTownInstance()
  386. :IShipyard(this), IMarket(this), town(nullptr), builded(0), destroyed(0), identifier(0), alignment(0xff)
  387. {
  388. }
  389. CGTownInstance::~CGTownInstance()
  390. {
  391. for (auto & elem : bonusingBuildings)
  392. delete elem;
  393. }
  394. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  395. {
  396. if(checkGuild && mageGuildLevel() < level)
  397. return 0;
  398. int ret = 6 - level; //how many spells are available at this level
  399. if (hasBuilt(BuildingID::LIBRARY, ETownType::TOWER))
  400. ret++;
  401. return ret;
  402. }
  403. bool CGTownInstance::needsLastStack() const
  404. {
  405. if(garrisonHero)
  406. return true;
  407. else return false;
  408. }
  409. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  410. {
  411. if( !cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ))//if this is enemy
  412. {
  413. if(armedGarrison() || visitingHero)
  414. {
  415. const CGHeroInstance *defendingHero = nullptr;
  416. const CArmedInstance *defendingArmy = this;
  417. if(visitingHero)
  418. defendingHero = visitingHero;
  419. else if(garrisonHero)
  420. defendingHero = garrisonHero;
  421. if(defendingHero)
  422. defendingArmy = defendingHero;
  423. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  424. //TODO
  425. //"borrowing" army from garrison to visiting hero
  426. cb->startBattlePrimary(h, defendingArmy, getSightCenter(), h, defendingHero, false, (outsideTown ? nullptr : this));
  427. }
  428. else
  429. {
  430. cb->setOwner(this, h->tempOwner);
  431. removeCapitols(h->getOwner());
  432. cb->heroVisitCastle(this, h);
  433. }
  434. }
  435. else if(h->visitablePos() == visitablePos())
  436. {
  437. if (h->commander && !h->commander->alive) //rise commander. TODO: interactive script
  438. {
  439. SetCommanderProperty scp;
  440. scp.heroid = h->id;
  441. scp.which = SetCommanderProperty::ALIVE;
  442. scp.amount = 1;
  443. cb->sendAndApply (&scp);
  444. }
  445. cb->heroVisitCastle(this, h);
  446. }
  447. else
  448. {
  449. logGlobal->errorStream() << h->name << " visits allied town of " << name << " from different pos?";
  450. }
  451. }
  452. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  453. {
  454. //FIXME: find out why this issue appears on random maps
  455. if (visitingHero == h)
  456. {
  457. cb->stopHeroVisitCastle(this, h);
  458. //logGlobal->warnStream() << h->name << " correctly left town " << name;
  459. }
  460. else
  461. logGlobal->warnStream() << "Warning, " << h->name << " tries to leave the town " << name << " but hero is not inside.";
  462. }
  463. std::string CGTownInstance::getObjectName() const
  464. {
  465. return name + ", " + town->faction->name;
  466. }
  467. void CGTownInstance::initObj()
  468. ///initialize town structures
  469. {
  470. blockVisit = true;
  471. if (subID == ETownType::DUNGEON)
  472. creatures.resize(GameConstants::CREATURES_PER_TOWN+1);//extra dwelling for Dungeon
  473. else
  474. creatures.resize(GameConstants::CREATURES_PER_TOWN);
  475. for (int level = 0; level < GameConstants::CREATURES_PER_TOWN; level++)
  476. {
  477. BuildingID buildID = BuildingID(BuildingID::DWELL_FIRST).advance(level);
  478. int upgradeNum = 0;
  479. for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(GameConstants::CREATURES_PER_TOWN))
  480. {
  481. if (hasBuilt(buildID) && town->creatures.at(level).size() > upgradeNum)
  482. creatures[level].second.push_back(town->creatures[level][upgradeNum]);
  483. }
  484. }
  485. switch (subID)
  486. { //add new visitable objects
  487. case 0:
  488. bonusingBuildings.push_back (new COPWBonus(BuildingID::STABLES, this));
  489. break;
  490. case 5:
  491. bonusingBuildings.push_back (new COPWBonus(BuildingID::MANA_VORTEX, this));
  492. //fallthrough
  493. case 2: case 3: case 6:
  494. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_4, this));
  495. break;
  496. case 7:
  497. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_1, this));
  498. break;
  499. }
  500. //add special bonuses from buildings
  501. recreateBuildingsBonuses();
  502. updateAppearance();
  503. }
  504. void CGTownInstance::newTurn() const
  505. {
  506. if (cb->getDate(Date::DAY_OF_WEEK) == 1) //reset on new week
  507. {
  508. auto & rand = cb->gameState()->getRandomGenerator();
  509. //give resources for Rampart, Mystic Pond
  510. if (hasBuilt(BuildingID::MYSTIC_POND, ETownType::RAMPART)
  511. && cb->getDate(Date::DAY) != 1 && (tempOwner < PlayerColor::PLAYER_LIMIT))
  512. {
  513. int resID = rand.nextInt(2, 5); //bonus to random rare resource
  514. resID = (resID==2)?1:resID;
  515. int resVal = rand.nextInt(1, 4);//with size 1..4
  516. cb->giveResource(tempOwner, static_cast<Res::ERes>(resID), resVal);
  517. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID);
  518. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal);
  519. }
  520. if ( subID == ETownType::DUNGEON )
  521. for (auto & elem : bonusingBuildings)
  522. {
  523. if ((elem)->ID == BuildingID::MANA_VORTEX)
  524. cb->setObjProperty (id, ObjProperty::STRUCTURE_CLEAR_VISITORS, (elem)->id); //reset visitors for Mana Vortex
  525. }
  526. if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
  527. {
  528. std::vector<SlotID> nativeCrits; //slots
  529. for (auto & elem : Slots())
  530. {
  531. if (elem.second->type->faction == subID) //native
  532. {
  533. nativeCrits.push_back(elem.first); //collect matching slots
  534. }
  535. }
  536. if (nativeCrits.size())
  537. {
  538. SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
  539. StackLocation sl(this, pos);
  540. const CCreature *c = getCreature(pos);
  541. if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
  542. {
  543. cb->changeStackCount(sl, c->growth);
  544. }
  545. else //upgrade
  546. {
  547. cb->changeStackType(sl, VLC->creh->creatures[*c->upgrades.begin()]);
  548. }
  549. }
  550. if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
  551. {
  552. int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1);
  553. if (!town->creatures[i].empty())
  554. {
  555. CreatureID c = town->creatures[i][0];
  556. SlotID n;
  557. TQuantity count = creatureGrowth(i);
  558. if (!count) // no dwelling
  559. count = VLC->creh->creatures[c]->growth;
  560. {//no lower tiers or above current month
  561. if ((n = getSlotFor(c)).validSlot())
  562. {
  563. StackLocation sl(this, n);
  564. if (slotEmpty(n))
  565. cb->insertNewStack(sl, VLC->creh->creatures[c], count);
  566. else //add to existing
  567. cb->changeStackCount(sl, count);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. /*
  576. int3 CGTownInstance::getSightCenter() const
  577. {
  578. return pos - int3(2,0,0);
  579. }
  580. */
  581. bool CGTownInstance::passableFor(PlayerColor color) const
  582. {
  583. if (!armedGarrison())//empty castle - anyone can visit
  584. return true;
  585. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  586. return false;
  587. if (cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES)
  588. return true;
  589. return false;
  590. }
  591. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  592. {
  593. offsets = {int3(-1,2,0), int3(-3,2,0)};
  594. }
  595. void CGTownInstance::removeCapitols (PlayerColor owner) const
  596. {
  597. if (hasCapitol()) // search if there's an older capitol
  598. {
  599. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  600. for (auto i = state->towns.cbegin(); i < state->towns.cend(); ++i)
  601. {
  602. if (*i != this && (*i)->hasCapitol())
  603. {
  604. RazeStructures rs;
  605. rs.tid = id;
  606. rs.bid.insert(BuildingID::CAPITOL);
  607. rs.destroyed = destroyed;
  608. cb->sendAndApply(&rs);
  609. return;
  610. }
  611. }
  612. }
  613. }
  614. int CGTownInstance::getBoatType() const
  615. {
  616. switch (town->faction->alignment)
  617. {
  618. case EAlignment::EVIL : return 0;
  619. case EAlignment::GOOD : return 1;
  620. case EAlignment::NEUTRAL : return 2;
  621. }
  622. assert(0);
  623. return -1;
  624. }
  625. int CGTownInstance::getMarketEfficiency() const
  626. {
  627. if (!hasBuilt(BuildingID::MARKETPLACE))
  628. return 0;
  629. const PlayerState *p = cb->getPlayer(tempOwner);
  630. assert(p);
  631. int marketCount = 0;
  632. for(const CGTownInstance *t : p->towns)
  633. if(t->hasBuilt(BuildingID::MARKETPLACE))
  634. marketCount++;
  635. return marketCount;
  636. }
  637. bool CGTownInstance::allowsTrade(EMarketMode::EMarketMode mode) const
  638. {
  639. switch(mode)
  640. {
  641. case EMarketMode::RESOURCE_RESOURCE:
  642. case EMarketMode::RESOURCE_PLAYER:
  643. return hasBuilt(BuildingID::MARKETPLACE);
  644. case EMarketMode::ARTIFACT_RESOURCE:
  645. case EMarketMode::RESOURCE_ARTIFACT:
  646. return hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::TOWER)
  647. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::DUNGEON)
  648. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::CONFLUX);
  649. case EMarketMode::CREATURE_RESOURCE:
  650. return hasBuilt(BuildingID::FREELANCERS_GUILD, ETownType::STRONGHOLD);
  651. case EMarketMode::CREATURE_UNDEAD:
  652. return hasBuilt(BuildingID::SKELETON_TRANSFORMER, ETownType::NECROPOLIS);
  653. case EMarketMode::RESOURCE_SKILL:
  654. return hasBuilt(BuildingID::MAGIC_UNIVERSITY, ETownType::CONFLUX);
  655. default:
  656. assert(0);
  657. return false;
  658. }
  659. }
  660. std::vector<int> CGTownInstance::availableItemsIds(EMarketMode::EMarketMode mode) const
  661. {
  662. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  663. {
  664. std::vector<int> ret;
  665. for(const CArtifact *a : merchantArtifacts)
  666. if(a)
  667. ret.push_back(a->id);
  668. else
  669. ret.push_back(-1);
  670. return ret;
  671. }
  672. else if ( mode == EMarketMode::RESOURCE_SKILL )
  673. {
  674. return universitySkills;
  675. }
  676. else
  677. return IMarket::availableItemsIds(mode);
  678. }
  679. void CGTownInstance::setType(si32 ID, si32 subID)
  680. {
  681. assert(ID == Obj::TOWN); // just in case
  682. CGObjectInstance::setType(ID, subID);
  683. town = VLC->townh->factions[subID]->town;
  684. randomizeArmy(subID);
  685. updateAppearance();
  686. }
  687. void CGTownInstance::updateAppearance()
  688. {
  689. //FIXME: not the best way to do this
  690. auto app = VLC->objtypeh->getHandlerFor(ID, subID)->getOverride(cb->gameState()->getTile(visitablePos())->terType, this);
  691. if (app)
  692. appearance = app.get();
  693. }
  694. std::string CGTownInstance::nodeName() const
  695. {
  696. return "Town (" + (town ? town->faction->name : "unknown") + ") of " + name;
  697. }
  698. void CGTownInstance::deserializationFix()
  699. {
  700. attachTo(&townAndVis);
  701. //Hero is already handled by CGameState::attachArmedObjects
  702. // if(visitingHero)
  703. // visitingHero->attachTo(&townAndVis);
  704. // if(garrisonHero)
  705. // garrisonHero->attachTo(this);
  706. }
  707. void CGTownInstance::updateMoraleBonusFromArmy()
  708. {
  709. Bonus *b = getBonusList().getFirst(Selector::sourceType(Bonus::ARMY).And(Selector::type(Bonus::MORALE)));
  710. if(!b)
  711. {
  712. b = new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  713. addNewBonus(b);
  714. }
  715. if (garrisonHero)
  716. b->val = 0;
  717. else
  718. CArmedInstance::updateMoraleBonusFromArmy();
  719. }
  720. void CGTownInstance::recreateBuildingsBonuses()
  721. {
  722. static TPropagatorPtr playerProp(new CPropagatorNodeType(PLAYER));
  723. BonusList bl;
  724. getExportedBonusList().getBonuses(bl, Selector::sourceType(Bonus::TOWN_STRUCTURE));
  725. for(Bonus *b : bl)
  726. removeBonus(b);
  727. //tricky! -> checks tavern only if no bratherhood of sword or not a castle
  728. if(subID != ETownType::CASTLE || !addBonusIfBuilt(BuildingID::BROTHERHOOD, Bonus::MORALE, +2))
  729. addBonusIfBuilt(BuildingID::TAVERN, Bonus::MORALE, +1);
  730. if(subID == ETownType::CASTLE) //castle
  731. {
  732. addBonusIfBuilt(BuildingID::LIGHTHOUSE, Bonus::SEA_MOVEMENT, +500, playerProp);
  733. addBonusIfBuilt(BuildingID::GRAIL, Bonus::MORALE, +2, playerProp); //colossus
  734. }
  735. else if(subID == ETownType::RAMPART) //rampart
  736. {
  737. addBonusIfBuilt(BuildingID::FOUNTAIN_OF_FORTUNE, Bonus::LUCK, +2); //fountain of fortune
  738. addBonusIfBuilt(BuildingID::GRAIL, Bonus::LUCK, +2, playerProp); //guardian spirit
  739. }
  740. else if(subID == ETownType::TOWER) //tower
  741. {
  742. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +15, PrimarySkill::KNOWLEDGE); //grail
  743. }
  744. else if(subID == ETownType::INFERNO) //Inferno
  745. {
  746. addBonusIfBuilt(BuildingID::STORMCLOUDS, Bonus::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); //Brimstone Clouds
  747. }
  748. else if(subID == ETownType::NECROPOLIS) //necropolis
  749. {
  750. addBonusIfBuilt(BuildingID::COVER_OF_DARKNESS, Bonus::DARKNESS, +20);
  751. addBonusIfBuilt(BuildingID::NECROMANCY_AMPLIFIER, Bonus::SECONDARY_SKILL_PREMY, +10, playerProp, SecondarySkill::NECROMANCY); //necromancy amplifier
  752. addBonusIfBuilt(BuildingID::GRAIL, Bonus::SECONDARY_SKILL_PREMY, +20, playerProp, SecondarySkill::NECROMANCY); //Soul prison
  753. }
  754. else if(subID == ETownType::DUNGEON) //Dungeon
  755. {
  756. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +12, PrimarySkill::SPELL_POWER); //grail
  757. }
  758. else if(subID == ETownType::STRONGHOLD) //Stronghold
  759. {
  760. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +20, PrimarySkill::ATTACK); //grail
  761. }
  762. else if(subID == ETownType::FORTRESS) //Fortress
  763. {
  764. addBonusIfBuilt(BuildingID::GLYPHS_OF_FEAR, Bonus::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); //Glyphs of Fear
  765. addBonusIfBuilt(BuildingID::BLOOD_OBELISK, Bonus::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); //Blood Obelisk
  766. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::ATTACK); //grail
  767. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::DEFENSE); //grail
  768. }
  769. else if(subID == ETownType::CONFLUX)
  770. {
  771. }
  772. }
  773. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype /*= -1*/)
  774. {
  775. static auto emptyPropagator = TPropagatorPtr();
  776. return addBonusIfBuilt(building, type, val, emptyPropagator, subtype);
  777. }
  778. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype /*= -1*/)
  779. {
  780. if(hasBuilt(building))
  781. {
  782. std::ostringstream descr;
  783. descr << town->buildings.at(building)->Name() << " ";
  784. if(val > 0)
  785. descr << "+";
  786. else if(val < 0)
  787. descr << "-";
  788. descr << val;
  789. Bonus *b = new Bonus(Bonus::PERMANENT, type, Bonus::TOWN_STRUCTURE, val, building, descr.str(), subtype);
  790. if(prop)
  791. b->addPropagator(prop);
  792. addNewBonus(b);
  793. return true;
  794. }
  795. return false;
  796. }
  797. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  798. {
  799. //if (!(!!visitingHero == !h))
  800. //{
  801. // logGlobal->warnStream() << boost::format("Hero visiting town %s is %s ") % name % (visitingHero.get() ? visitingHero->name : "NULL");
  802. // logGlobal->warnStream() << boost::format("New hero will be %s ") % (h ? h->name : "NULL");
  803. //
  804. //}
  805. assert(!!visitingHero == !h);
  806. if(h)
  807. {
  808. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  809. assert(p);
  810. h->detachFrom(p);
  811. h->attachTo(&townAndVis);
  812. visitingHero = h;
  813. h->visitedTown = this;
  814. h->inTownGarrison = false;
  815. }
  816. else
  817. {
  818. PlayerState *p = cb->gameState()->getPlayer(visitingHero->tempOwner);
  819. visitingHero->visitedTown = nullptr;
  820. visitingHero->detachFrom(&townAndVis);
  821. visitingHero->attachTo(p);
  822. visitingHero = nullptr;
  823. }
  824. }
  825. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  826. {
  827. assert(!!garrisonHero == !h);
  828. if(h)
  829. {
  830. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  831. assert(p);
  832. h->detachFrom(p);
  833. h->attachTo(this);
  834. garrisonHero = h;
  835. h->visitedTown = this;
  836. h->inTownGarrison = true;
  837. }
  838. else
  839. {
  840. PlayerState *p = cb->gameState()->getPlayer(garrisonHero->tempOwner);
  841. garrisonHero->visitedTown = nullptr;
  842. garrisonHero->inTownGarrison = false;
  843. garrisonHero->detachFrom(this);
  844. garrisonHero->attachTo(p);
  845. garrisonHero = nullptr;
  846. }
  847. updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
  848. }
  849. bool CGTownInstance::armedGarrison() const
  850. {
  851. return stacksCount() || garrisonHero;
  852. }
  853. int CGTownInstance::getTownLevel() const
  854. {
  855. // count all buildings that are not upgrades
  856. return boost::range::count_if(builtBuildings, [&](const BuildingID & build)
  857. {
  858. return town->buildings.at(build) && town->buildings.at(build)->upgrade == -1;
  859. });
  860. }
  861. CBonusSystemNode * CGTownInstance::whatShouldBeAttached()
  862. {
  863. return &townAndVis;
  864. }
  865. const CArmedInstance * CGTownInstance::getUpperArmy() const
  866. {
  867. if(garrisonHero)
  868. return garrisonHero;
  869. return this;
  870. }
  871. bool CGTownInstance::hasBuilt(BuildingID buildingID, int townID) const
  872. {
  873. if (townID == town->faction->index || townID == ETownType::ANY)
  874. return hasBuilt(buildingID);
  875. return false;
  876. }
  877. bool CGTownInstance::hasBuilt(BuildingID buildingID) const
  878. {
  879. return vstd::contains(builtBuildings, buildingID);
  880. }
  881. CBuilding::TRequired CGTownInstance::genBuildingRequirements(BuildingID buildID) const
  882. {
  883. const CBuilding * building = town->buildings.at(buildID);
  884. std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
  885. [&](const BuildingID & id) -> CBuilding::TRequired::Variant
  886. {
  887. const CBuilding * build = town->buildings.at(id);
  888. if (!hasBuilt(id))
  889. return id;
  890. if (build->upgrade != BuildingID::NONE && !hasBuilt(build->upgrade))
  891. return build->upgrade;
  892. return build->requirements.morph(dependTest);
  893. };
  894. CBuilding::TRequired::OperatorAll requirements;
  895. if (building->upgrade != BuildingID::NONE)
  896. {
  897. const CBuilding * upgr = town->buildings.at(building->upgrade);
  898. requirements.expressions.push_back(upgr->bid);
  899. requirements.expressions.push_back(upgr->requirements.morph(dependTest));
  900. }
  901. requirements.expressions.push_back(building->requirements.morph(dependTest));
  902. CBuilding::TRequired::Variant variant(requirements);
  903. CBuilding::TRequired ret(variant);
  904. ret.minimize();
  905. return ret;
  906. }
  907. void CGTownInstance::addHeroToStructureVisitors( const CGHeroInstance *h, si32 structureInstanceID ) const
  908. {
  909. if(visitingHero == h)
  910. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  911. else if(garrisonHero == h)
  912. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  913. else
  914. {
  915. //should never ever happen
  916. logGlobal->errorStream() << "Cannot add hero " << h->name << " to visitors of structure #" << structureInstanceID;
  917. assert(0);
  918. }
  919. }
  920. void CGTownInstance::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  921. {
  922. if(result.winner == 0)
  923. {
  924. removeCapitols(hero->getOwner());
  925. cb->setOwner (this, hero->tempOwner); //give control after checkout is done
  926. FoWChange fw;
  927. fw.player = hero->tempOwner;
  928. fw.mode = 1;
  929. cb->getTilesInRange(fw.tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  930. cb->sendAndApply (&fw);
  931. }
  932. }
  933. COPWBonus::COPWBonus (BuildingID index, CGTownInstance *TOWN)
  934. {
  935. ID = index;
  936. town = TOWN;
  937. id = town->bonusingBuildings.size();
  938. }
  939. void COPWBonus::setProperty(ui8 what, ui32 val)
  940. {
  941. switch (what)
  942. {
  943. case ObjProperty::VISITORS:
  944. visitors.insert(val);
  945. break;
  946. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  947. visitors.clear();
  948. break;
  949. }
  950. }
  951. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  952. {
  953. ObjectInstanceID heroID = h->id;
  954. if (town->hasBuilt(ID))
  955. {
  956. InfoWindow iw;
  957. iw.player = h->tempOwner;
  958. switch (town->subID)
  959. {
  960. case ETownType::CASTLE: //Stables
  961. if (!h->hasBonusFrom(Bonus::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
  962. {
  963. GiveBonus gb;
  964. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  965. gb.id = heroID.getNum();
  966. cb->giveHeroBonus(&gb);
  967. iw.text << VLC->generaltexth->allTexts[580];
  968. cb->showInfoDialog(&iw);
  969. }
  970. break;
  971. case ETownType::DUNGEON: //Mana Vortex
  972. if (visitors.empty() && h->mana <= h->manaLimit() * 2)
  973. {
  974. cb->setManaPoints (heroID, 2 * h->manaLimit());
  975. //TODO: investigate line below
  976. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  977. iw.text << VLC->generaltexth->allTexts[579];
  978. cb->showInfoDialog(&iw);
  979. town->addHeroToStructureVisitors(h, id);
  980. }
  981. break;
  982. }
  983. }
  984. }
  985. CTownBonus::CTownBonus (BuildingID index, CGTownInstance *TOWN)
  986. {
  987. ID = index;
  988. town = TOWN;
  989. id = town->bonusingBuildings.size();
  990. }
  991. void CTownBonus::setProperty (ui8 what, ui32 val)
  992. {
  993. if(what == ObjProperty::VISITORS)
  994. visitors.insert(ObjectInstanceID(val));
  995. }
  996. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  997. {
  998. ObjectInstanceID heroID = h->id;
  999. if (town->hasBuilt(ID) && visitors.find(heroID) == visitors.end())
  1000. {
  1001. InfoWindow iw;
  1002. PrimarySkill::PrimarySkill what = PrimarySkill::ATTACK;
  1003. int val=0, mid=0;
  1004. switch (ID)
  1005. {
  1006. case BuildingID::SPECIAL_4:
  1007. switch(town->subID)
  1008. {
  1009. case ETownType::TOWER: //wall
  1010. what = PrimarySkill::KNOWLEDGE;
  1011. val = 1;
  1012. mid = 581;
  1013. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1014. break;
  1015. case ETownType::INFERNO: //order of fire
  1016. what = PrimarySkill::SPELL_POWER;
  1017. val = 1;
  1018. mid = 582;
  1019. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1020. break;
  1021. case ETownType::STRONGHOLD://hall of Valhalla
  1022. what = PrimarySkill::ATTACK;
  1023. val = 1;
  1024. mid = 584;
  1025. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1026. break;
  1027. case ETownType::DUNGEON://academy of battle scholars
  1028. what = PrimarySkill::EXPERIENCE;
  1029. val = h->calculateXp(1000);
  1030. mid = 583;
  1031. iw.components.push_back (Component(Component::EXPERIENCE, 0, val, 0));
  1032. break;
  1033. }
  1034. break;
  1035. case BuildingID::SPECIAL_1:
  1036. switch(town->subID)
  1037. {
  1038. case ETownType::FORTRESS: //cage of warlords
  1039. what = PrimarySkill::DEFENSE;
  1040. val = 1;
  1041. mid = 585;
  1042. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1043. break;
  1044. }
  1045. break;
  1046. }
  1047. assert(mid);
  1048. iw.player = cb->getOwner(heroID);
  1049. iw.text << VLC->generaltexth->allTexts[mid];
  1050. cb->showInfoDialog(&iw);
  1051. cb->changePrimSkill (cb->getHero(heroID), what, val);
  1052. town->addHeroToStructureVisitors(h, id);
  1053. }
  1054. }
  1055. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1056. : count(_count)
  1057. {
  1058. description = boost::str(boost::format(format) % count);
  1059. }
  1060. GrowthInfo::Entry::Entry(int subID, BuildingID building, int _count)
  1061. : count(_count)
  1062. {
  1063. description = boost::str(boost::format("%s %+d") % VLC->townh->factions[subID]->town->buildings.at(building)->Name() % count);
  1064. }
  1065. CTownAndVisitingHero::CTownAndVisitingHero()
  1066. {
  1067. setNodeType(TOWN_AND_VISITOR);
  1068. }
  1069. int GrowthInfo::totalGrowth() const
  1070. {
  1071. int ret = 0;
  1072. for(const Entry &entry : entries)
  1073. ret += entry.count;
  1074. return ret;
  1075. }