CGTownInstance.cpp 34 KB

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