CGTownInstance.cpp 34 KB

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