CGTownInstance.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. assert(!creatures.empty());
  32. assert(!creatures[0].second.empty());
  33. break;
  34. }
  35. case Obj::REFUGEE_CAMP:
  36. //is handled within newturn func
  37. break;
  38. case Obj::WAR_MACHINE_FACTORY:
  39. creatures.resize(3);
  40. creatures[0].second.push_back(CreatureID::BALLISTA);
  41. creatures[1].second.push_back(CreatureID::FIRST_AID_TENT);
  42. creatures[2].second.push_back(CreatureID::AMMO_CART);
  43. break;
  44. default:
  45. assert(0);
  46. break;
  47. }
  48. }
  49. void CGDwelling::setPropertyDer(ui8 what, ui32 val)
  50. {
  51. switch (what)
  52. {
  53. case ObjProperty::OWNER: //change owner
  54. if (ID == Obj::CREATURE_GENERATOR1) //single generators
  55. {
  56. if (tempOwner != PlayerColor::NEUTRAL)
  57. {
  58. std::vector<ConstTransitivePtr<CGDwelling> >* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  59. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  60. }
  61. if (PlayerColor(val) != PlayerColor::NEUTRAL) //can new owner be neutral?
  62. cb->gameState()->players[PlayerColor(val)].dwellings.push_back (this);
  63. }
  64. break;
  65. case ObjProperty::AVAILABLE_CREATURE:
  66. creatures.resize(1);
  67. creatures[0].second.resize(1);
  68. creatures[0].second[0] = CreatureID(val);
  69. break;
  70. }
  71. }
  72. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  73. {
  74. if(ID == Obj::REFUGEE_CAMP && !creatures[0].first) //Refugee Camp, no available cres
  75. {
  76. InfoWindow iw;
  77. iw.player = h->tempOwner;
  78. iw.text.addTxt(MetaString::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  79. iw.text.addReplacement(MetaString::OBJ_NAMES, ID);
  80. cb->sendAndApply(&iw);
  81. return;
  82. }
  83. PlayerRelations::PlayerRelations relations = cb->gameState()->getPlayerRelations( h->tempOwner, tempOwner );
  84. if ( relations == PlayerRelations::ALLIES )
  85. return;//do not allow recruiting or capturing
  86. if( !relations && stacksCount() > 0) //object is guarded, owned by enemy
  87. {
  88. BlockingDialog bd(true,false);
  89. bd.player = h->tempOwner;
  90. 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?
  91. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  92. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + Slots().begin()->second->getQuantityID()*3);
  93. bd.text.addReplacement(*Slots().begin()->second);
  94. cb->showBlockingDialog(&bd);
  95. return;
  96. }
  97. if(!relations && ID != Obj::WAR_MACHINE_FACTORY)
  98. {
  99. cb->setOwner(this, h->tempOwner);
  100. }
  101. BlockingDialog bd (true,false);
  102. bd.player = h->tempOwner;
  103. if(ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4)
  104. {
  105. 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?
  106. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  107. for(auto & elem : creatures)
  108. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  109. }
  110. else if(ID == Obj::REFUGEE_CAMP)
  111. {
  112. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  113. bd.text.addReplacement(MetaString::OBJ_NAMES, ID);
  114. for(auto & elem : creatures)
  115. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  116. }
  117. else if(ID == Obj::WAR_MACHINE_FACTORY)
  118. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  119. else
  120. throw std::runtime_error("Illegal dwelling!");
  121. cb->showBlockingDialog(&bd);
  122. }
  123. void CGDwelling::newTurn() const
  124. {
  125. if(cb->getDate(Date::DAY_OF_WEEK) != 1) //not first day of week
  126. return;
  127. //town growths and War Machines Factories are handled separately
  128. if(ID == Obj::TOWN || ID == Obj::WAR_MACHINE_FACTORY)
  129. return;
  130. if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
  131. {
  132. cb->setObjProperty(id, ObjProperty::AVAILABLE_CREATURE, VLC->creh->pickRandomMonster(cb->gameState()->getRandomGenerator()));
  133. }
  134. bool change = false;
  135. SetAvailableCreatures sac;
  136. sac.creatures = creatures;
  137. sac.tid = id;
  138. for (size_t i = 0; i < creatures.size(); i++)
  139. {
  140. if(creatures[i].second.size())
  141. {
  142. CCreature *cre = VLC->creh->creatures[creatures[i].second[0]];
  143. TQuantity amount = cre->growth * (1 + cre->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(Bonus::CREATURE_GROWTH);
  144. if (VLC->modh->settings.DWELLINGS_ACCUMULATE_CREATURES && ID != Obj::REFUGEE_CAMP) //camp should not try to accumulate different kinds of creatures
  145. sac.creatures[i].first += amount;
  146. else
  147. sac.creatures[i].first = amount;
  148. change = true;
  149. }
  150. }
  151. if(change)
  152. cb->sendAndApply(&sac);
  153. }
  154. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h) const
  155. {
  156. CreatureID crid = creatures[0].second[0];
  157. CCreature *crs = VLC->creh->creatures[crid];
  158. TQuantity count = creatures[0].first;
  159. if(crs->level == 1 && ID != Obj::REFUGEE_CAMP) //first level - creatures are for free
  160. {
  161. if(count) //there are available creatures
  162. {
  163. SlotID slot = h->getSlotFor(crid);
  164. if(!slot.validSlot()) //no available slot
  165. {
  166. InfoWindow iw;
  167. iw.player = h->tempOwner;
  168. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  169. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  170. cb->showInfoDialog(&iw);
  171. }
  172. else //give creatures
  173. {
  174. SetAvailableCreatures sac;
  175. sac.tid = id;
  176. sac.creatures = creatures;
  177. sac.creatures[0].first = 0;
  178. InfoWindow iw;
  179. iw.player = h->tempOwner;
  180. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  181. iw.text.addReplacement(count);
  182. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  183. cb->showInfoDialog(&iw);
  184. cb->sendAndApply(&sac);
  185. cb->addToSlot(StackLocation(h, slot), crs, count);
  186. }
  187. }
  188. else //there no creatures
  189. {
  190. InfoWindow iw;
  191. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  192. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  193. iw.player = h->tempOwner;
  194. cb->sendAndApply(&iw);
  195. }
  196. }
  197. else
  198. {
  199. if(ID == Obj::WAR_MACHINE_FACTORY) //pick available War Machines
  200. {
  201. //there is 1 war machine available to recruit if hero doesn't have one
  202. SetAvailableCreatures sac;
  203. sac.tid = id;
  204. sac.creatures = creatures;
  205. sac.creatures[0].first = !h->getArt(ArtifactPosition::MACH1); //ballista
  206. sac.creatures[1].first = !h->getArt(ArtifactPosition::MACH3); //first aid tent
  207. sac.creatures[2].first = !h->getArt(ArtifactPosition::MACH2); //ammo cart
  208. cb->sendAndApply(&sac);
  209. }
  210. OpenWindow ow;
  211. ow.id1 = id.getNum();
  212. ow.id2 = h->id.getNum();
  213. ow.window = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP)
  214. ? OpenWindow::RECRUITMENT_FIRST
  215. : OpenWindow::RECRUITMENT_ALL;
  216. cb->sendAndApply(&ow);
  217. }
  218. }
  219. void CGDwelling::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  220. {
  221. if (result.winner == 0)
  222. {
  223. onHeroVisit(hero);
  224. }
  225. }
  226. void CGDwelling::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  227. {
  228. auto relations = cb->getPlayerRelations(getOwner(), hero->getOwner());
  229. if(stacksCount() > 0 && relations == PlayerRelations::ENEMIES) //guards present
  230. {
  231. if(answer)
  232. cb->startBattleI(hero, this);
  233. }
  234. else if(answer)
  235. {
  236. heroAcceptsCreatures(hero);
  237. }
  238. }
  239. int CGTownInstance::getSightRadious() const //returns sight distance
  240. {
  241. if (subID == ETownType::TOWER)
  242. {
  243. if (hasBuilt(BuildingID::GRAIL)) //skyship
  244. return -1; //entire map
  245. if (hasBuilt(BuildingID::LOOKOUT_TOWER)) //lookout tower
  246. return 20;
  247. }
  248. return 5;
  249. }
  250. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  251. {
  252. ///this is freakin' overcomplicated solution
  253. switch (what)
  254. {
  255. case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
  256. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id.getNum());
  257. break;
  258. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  259. bonusingBuildings[val]->setProperty (ObjProperty::STRUCTURE_CLEAR_VISITORS, 0);
  260. break;
  261. case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
  262. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id.getNum());
  263. break;
  264. case ObjProperty::BONUS_VALUE_FIRST:
  265. bonusValue.first = val;
  266. break;
  267. case ObjProperty::BONUS_VALUE_SECOND:
  268. bonusValue.second = val;
  269. break;
  270. }
  271. }
  272. CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  273. {
  274. if (hasBuilt(BuildingID::CASTLE))
  275. return CASTLE;
  276. if (hasBuilt(BuildingID::CITADEL))
  277. return CITADEL;
  278. if (hasBuilt(BuildingID::FORT))
  279. return FORT;
  280. return NONE;
  281. }
  282. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  283. {
  284. if (hasBuilt(BuildingID::CAPITOL))
  285. return 3;
  286. if (hasBuilt(BuildingID::CITY_HALL))
  287. return 2;
  288. if (hasBuilt(BuildingID::TOWN_HALL))
  289. return 1;
  290. if (hasBuilt(BuildingID::VILLAGE_HALL))
  291. return 0;
  292. return -1;
  293. }
  294. int CGTownInstance::mageGuildLevel() const
  295. {
  296. if (hasBuilt(BuildingID::MAGES_GUILD_5))
  297. return 5;
  298. if (hasBuilt(BuildingID::MAGES_GUILD_4))
  299. return 4;
  300. if (hasBuilt(BuildingID::MAGES_GUILD_3))
  301. return 3;
  302. if (hasBuilt(BuildingID::MAGES_GUILD_2))
  303. return 2;
  304. if (hasBuilt(BuildingID::MAGES_GUILD_1))
  305. return 1;
  306. return 0;
  307. }
  308. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  309. {
  310. return town->hordeLvl.at(HID);
  311. }
  312. int CGTownInstance::creatureGrowth(const int & level) const
  313. {
  314. return getGrowthInfo(level).totalGrowth();
  315. }
  316. GrowthInfo CGTownInstance::getGrowthInfo(int level) const
  317. {
  318. GrowthInfo ret;
  319. if (level<0 || level >=GameConstants::CREATURES_PER_TOWN)
  320. return ret;
  321. if (creatures[level].second.empty())
  322. return ret; //no dwelling
  323. const CCreature *creature = VLC->creh->creatures[creatures[level].second.back()];
  324. const int base = creature->growth;
  325. int castleBonus = 0;
  326. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[590], base));// \n\nBasic growth %d"
  327. if (hasBuilt(BuildingID::CASTLE))
  328. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::CASTLE, castleBonus = base));
  329. else if (hasBuilt(BuildingID::CITADEL))
  330. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::CITADEL, castleBonus = base / 2));
  331. if(town->hordeLvl.at(0) == level)//horde 1
  332. if(hasBuilt(BuildingID::HORDE_1))
  333. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::HORDE_1, creature->hordeGrowth));
  334. if(town->hordeLvl.at(1) == level)//horde 2
  335. if(hasBuilt(BuildingID::HORDE_2))
  336. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::HORDE_2, creature->hordeGrowth));
  337. int dwellingBonus = 0;
  338. if(const PlayerState *p = cb->getPlayer(tempOwner, false))
  339. {
  340. for(const CGDwelling *dwelling : p->dwellings)
  341. if(vstd::contains(creatures[level].second, dwelling->creatures[0].second[0]))
  342. dwellingBonus++;
  343. }
  344. if(dwellingBonus)
  345. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[591], dwellingBonus));// \nExternal dwellings %+d
  346. //other *-of-legion-like bonuses (%d to growth cumulative with grail)
  347. TBonusListPtr bonuses = getBonuses(Selector::type(Bonus::CREATURE_GROWTH).And(Selector::subtype(level)));
  348. for(const Bonus *b : *bonuses)
  349. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val));
  350. //statue-of-legion-like bonus: % to base+castle
  351. TBonusListPtr bonuses2 = getBonuses(Selector::type(Bonus::CREATURE_GROWTH_PERCENT));
  352. for(const Bonus *b : *bonuses2)
  353. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val * (base + castleBonus) / 100));
  354. if(hasBuilt(BuildingID::GRAIL)) //grail - +50% to ALL (so far added) growth
  355. ret.entries.push_back(GrowthInfo::Entry(subID, BuildingID::GRAIL, ret.totalGrowth() / 2));
  356. return ret;
  357. }
  358. TResources CGTownInstance::dailyIncome() const
  359. {
  360. TResources ret;
  361. for (auto & p : town->buildings)
  362. {
  363. BuildingID buildingUpgrade;
  364. for (auto & p2 : town->buildings)
  365. {
  366. if (p2.second->upgrade == p.first)
  367. {
  368. buildingUpgrade = p2.first;
  369. }
  370. }
  371. if (!hasBuilt(buildingUpgrade)&&(hasBuilt(p.first)))
  372. {
  373. ret += p.second->produce;
  374. }
  375. }
  376. return ret;
  377. }
  378. bool CGTownInstance::hasFort() const
  379. {
  380. return hasBuilt(BuildingID::FORT);
  381. }
  382. bool CGTownInstance::hasCapitol() const
  383. {
  384. return hasBuilt(BuildingID::CAPITOL);
  385. }
  386. CGTownInstance::CGTownInstance()
  387. :IShipyard(this), IMarket(this), town(nullptr), builded(0), destroyed(0), identifier(0), alignment(0xff)
  388. {
  389. }
  390. CGTownInstance::~CGTownInstance()
  391. {
  392. for (auto & elem : bonusingBuildings)
  393. delete elem;
  394. }
  395. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  396. {
  397. if(checkGuild && mageGuildLevel() < level)
  398. return 0;
  399. int ret = 6 - level; //how many spells are available at this level
  400. if (hasBuilt(BuildingID::LIBRARY, ETownType::TOWER))
  401. ret++;
  402. return ret;
  403. }
  404. bool CGTownInstance::needsLastStack() const
  405. {
  406. if(garrisonHero)
  407. return true;
  408. else return false;
  409. }
  410. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  411. {
  412. if( !cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ))//if this is enemy
  413. {
  414. if(armedGarrison() || visitingHero)
  415. {
  416. const CGHeroInstance *defendingHero = nullptr;
  417. const CArmedInstance *defendingArmy = this;
  418. if(visitingHero)
  419. defendingHero = visitingHero;
  420. else if(garrisonHero)
  421. defendingHero = garrisonHero;
  422. if(defendingHero)
  423. defendingArmy = defendingHero;
  424. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  425. //TODO
  426. //"borrowing" army from garrison to visiting hero
  427. cb->startBattlePrimary(h, defendingArmy, getSightCenter(), h, defendingHero, false, (outsideTown ? nullptr : this));
  428. }
  429. else
  430. {
  431. cb->setOwner(this, h->tempOwner);
  432. removeCapitols(h->getOwner());
  433. cb->heroVisitCastle(this, h);
  434. }
  435. }
  436. else if(h->visitablePos() == visitablePos())
  437. {
  438. if (h->commander && !h->commander->alive) //rise commander. TODO: interactive script
  439. {
  440. SetCommanderProperty scp;
  441. scp.heroid = h->id;
  442. scp.which = SetCommanderProperty::ALIVE;
  443. scp.amount = 1;
  444. cb->sendAndApply (&scp);
  445. }
  446. cb->heroVisitCastle(this, h);
  447. }
  448. else
  449. {
  450. logGlobal->errorStream() << h->name << " visits allied town of " << name << " from different pos?";
  451. }
  452. }
  453. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  454. {
  455. //FIXME: find out why this issue appears on random maps
  456. if (visitingHero == h)
  457. {
  458. cb->stopHeroVisitCastle(this, h);
  459. logGlobal->warnStream() << h->name << " correctly left town " << name;
  460. }
  461. else
  462. logGlobal->warnStream() << "Warning, " << h->name << " tries to leave the town " << name << " but hero is not inside.";
  463. }
  464. std::string CGTownInstance::getObjectName() const
  465. {
  466. return name + ", " + town->faction->name;
  467. }
  468. void CGTownInstance::initObj()
  469. ///initialize town structures
  470. {
  471. blockVisit = true;
  472. if (subID == ETownType::DUNGEON)
  473. creatures.resize(GameConstants::CREATURES_PER_TOWN+1);//extra dwelling for Dungeon
  474. else
  475. creatures.resize(GameConstants::CREATURES_PER_TOWN);
  476. for (int level = 0; level < GameConstants::CREATURES_PER_TOWN; level++)
  477. {
  478. BuildingID buildID = BuildingID(BuildingID::DWELL_FIRST).advance(level);
  479. int upgradeNum = 0;
  480. for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(GameConstants::CREATURES_PER_TOWN))
  481. {
  482. if (hasBuilt(buildID) && town->creatures.at(level).size() > upgradeNum)
  483. creatures[level].second.push_back(town->creatures[level][upgradeNum]);
  484. }
  485. }
  486. switch (subID)
  487. { //add new visitable objects
  488. case 0:
  489. bonusingBuildings.push_back (new COPWBonus(BuildingID::STABLES, this));
  490. break;
  491. case 5:
  492. bonusingBuildings.push_back (new COPWBonus(BuildingID::MANA_VORTEX, this));
  493. //fallthrough
  494. case 2: case 3: case 6:
  495. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_4, this));
  496. break;
  497. case 7:
  498. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_1, this));
  499. break;
  500. }
  501. //add special bonuses from buildings
  502. recreateBuildingsBonuses();
  503. updateAppearance();
  504. }
  505. void CGTownInstance::newTurn() const
  506. {
  507. if (cb->getDate(Date::DAY_OF_WEEK) == 1) //reset on new week
  508. {
  509. auto & rand = cb->gameState()->getRandomGenerator();
  510. //give resources for Rampart, Mystic Pond
  511. if (hasBuilt(BuildingID::MYSTIC_POND, ETownType::RAMPART)
  512. && cb->getDate(Date::DAY) != 1 && (tempOwner < PlayerColor::PLAYER_LIMIT))
  513. {
  514. int resID = rand.nextInt(2, 5); //bonus to random rare resource
  515. resID = (resID==2)?1:resID;
  516. int resVal = rand.nextInt(1, 4);//with size 1..4
  517. cb->giveResource(tempOwner, static_cast<Res::ERes>(resID), resVal);
  518. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID);
  519. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal);
  520. }
  521. if ( subID == ETownType::DUNGEON )
  522. for (auto & elem : bonusingBuildings)
  523. {
  524. if ((elem)->ID == BuildingID::MANA_VORTEX)
  525. cb->setObjProperty (id, ObjProperty::STRUCTURE_CLEAR_VISITORS, (elem)->id); //reset visitors for Mana Vortex
  526. }
  527. if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
  528. {
  529. std::vector<SlotID> nativeCrits; //slots
  530. for (auto & elem : Slots())
  531. {
  532. if (elem.second->type->faction == subID) //native
  533. {
  534. nativeCrits.push_back(elem.first); //collect matching slots
  535. }
  536. }
  537. if (nativeCrits.size())
  538. {
  539. SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
  540. StackLocation sl(this, pos);
  541. const CCreature *c = getCreature(pos);
  542. if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
  543. {
  544. cb->changeStackCount(sl, c->growth);
  545. }
  546. else //upgrade
  547. {
  548. cb->changeStackType(sl, VLC->creh->creatures[*c->upgrades.begin()]);
  549. }
  550. }
  551. if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
  552. {
  553. int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1);
  554. if (!town->creatures[i].empty())
  555. {
  556. CreatureID c = town->creatures[i][0];
  557. SlotID n;
  558. TQuantity count = creatureGrowth(i);
  559. if (!count) // no dwelling
  560. count = VLC->creh->creatures[c]->growth;
  561. {//no lower tiers or above current month
  562. if ((n = getSlotFor(c)).validSlot())
  563. {
  564. StackLocation sl(this, n);
  565. if (slotEmpty(n))
  566. cb->insertNewStack(sl, VLC->creh->creatures[c], count);
  567. else //add to existing
  568. cb->changeStackCount(sl, count);
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. /*
  577. int3 CGTownInstance::getSightCenter() const
  578. {
  579. return pos - int3(2,0,0);
  580. }
  581. */
  582. bool CGTownInstance::passableFor(PlayerColor color) const
  583. {
  584. if (!armedGarrison())//empty castle - anyone can visit
  585. return true;
  586. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  587. return false;
  588. if (cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES)
  589. return true;
  590. return false;
  591. }
  592. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  593. {
  594. offsets += int3(-1,2,0), int3(-3,2,0);
  595. }
  596. void CGTownInstance::removeCapitols (PlayerColor owner) const
  597. {
  598. if (hasCapitol()) // search if there's an older capitol
  599. {
  600. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  601. for (auto i = state->towns.cbegin(); i < state->towns.cend(); ++i)
  602. {
  603. if (*i != this && (*i)->hasCapitol())
  604. {
  605. RazeStructures rs;
  606. rs.tid = id;
  607. rs.bid.insert(BuildingID::CAPITOL);
  608. rs.destroyed = destroyed;
  609. cb->sendAndApply(&rs);
  610. return;
  611. }
  612. }
  613. }
  614. }
  615. int CGTownInstance::getBoatType() const
  616. {
  617. switch (town->faction->alignment)
  618. {
  619. case EAlignment::EVIL : return 0;
  620. case EAlignment::GOOD : return 1;
  621. case EAlignment::NEUTRAL : return 2;
  622. }
  623. assert(0);
  624. return -1;
  625. }
  626. int CGTownInstance::getMarketEfficiency() const
  627. {
  628. if (!hasBuilt(BuildingID::MARKETPLACE))
  629. return 0;
  630. const PlayerState *p = cb->getPlayer(tempOwner);
  631. assert(p);
  632. int marketCount = 0;
  633. for(const CGTownInstance *t : p->towns)
  634. if(t->hasBuilt(BuildingID::MARKETPLACE))
  635. marketCount++;
  636. return marketCount;
  637. }
  638. bool CGTownInstance::allowsTrade(EMarketMode::EMarketMode mode) const
  639. {
  640. switch(mode)
  641. {
  642. case EMarketMode::RESOURCE_RESOURCE:
  643. case EMarketMode::RESOURCE_PLAYER:
  644. return hasBuilt(BuildingID::MARKETPLACE);
  645. case EMarketMode::ARTIFACT_RESOURCE:
  646. case EMarketMode::RESOURCE_ARTIFACT:
  647. return hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::TOWER)
  648. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::DUNGEON)
  649. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::CONFLUX);
  650. case EMarketMode::CREATURE_RESOURCE:
  651. return hasBuilt(BuildingID::FREELANCERS_GUILD, ETownType::STRONGHOLD);
  652. case EMarketMode::CREATURE_UNDEAD:
  653. return hasBuilt(BuildingID::SKELETON_TRANSFORMER, ETownType::NECROPOLIS);
  654. case EMarketMode::RESOURCE_SKILL:
  655. return hasBuilt(BuildingID::MAGIC_UNIVERSITY, ETownType::CONFLUX);
  656. default:
  657. assert(0);
  658. return false;
  659. }
  660. }
  661. std::vector<int> CGTownInstance::availableItemsIds(EMarketMode::EMarketMode mode) const
  662. {
  663. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  664. {
  665. std::vector<int> ret;
  666. for(const CArtifact *a : merchantArtifacts)
  667. if(a)
  668. ret.push_back(a->id);
  669. else
  670. ret.push_back(-1);
  671. return ret;
  672. }
  673. else if ( mode == EMarketMode::RESOURCE_SKILL )
  674. {
  675. return universitySkills;
  676. }
  677. else
  678. return IMarket::availableItemsIds(mode);
  679. }
  680. void CGTownInstance::setType(si32 ID, si32 subID)
  681. {
  682. assert(ID == Obj::TOWN); // just in case
  683. CGObjectInstance::setType(ID, subID);
  684. town = VLC->townh->factions[subID]->town;
  685. randomizeArmy(subID);
  686. updateAppearance();
  687. }
  688. void CGTownInstance::updateAppearance()
  689. {
  690. //FIXME: not the best way to do this
  691. auto app = VLC->objtypeh->getHandlerFor(ID, subID)->getOverride(cb->gameState()->getTile(visitablePos())->terType, this);
  692. if (app)
  693. appearance = app.get();
  694. }
  695. std::string CGTownInstance::nodeName() const
  696. {
  697. return "Town (" + (town ? town->faction->name : "unknown") + ") of " + name;
  698. }
  699. void CGTownInstance::deserializationFix()
  700. {
  701. attachTo(&townAndVis);
  702. //Hero is already handled by CGameState::attachArmedObjects
  703. // if(visitingHero)
  704. // visitingHero->attachTo(&townAndVis);
  705. // if(garrisonHero)
  706. // garrisonHero->attachTo(this);
  707. }
  708. void CGTownInstance::updateMoraleBonusFromArmy()
  709. {
  710. Bonus *b = getBonusList().getFirst(Selector::sourceType(Bonus::ARMY).And(Selector::type(Bonus::MORALE)));
  711. if(!b)
  712. {
  713. b = new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  714. addNewBonus(b);
  715. }
  716. if (garrisonHero)
  717. b->val = 0;
  718. else
  719. CArmedInstance::updateMoraleBonusFromArmy();
  720. }
  721. void CGTownInstance::recreateBuildingsBonuses()
  722. {
  723. static TPropagatorPtr playerProp(new CPropagatorNodeType(PLAYER));
  724. BonusList bl;
  725. getExportedBonusList().getBonuses(bl, Selector::sourceType(Bonus::TOWN_STRUCTURE));
  726. for(Bonus *b : bl)
  727. removeBonus(b);
  728. //tricky! -> checks tavern only if no bratherhood of sword or not a castle
  729. if(subID != ETownType::CASTLE || !addBonusIfBuilt(BuildingID::BROTHERHOOD, Bonus::MORALE, +2))
  730. addBonusIfBuilt(BuildingID::TAVERN, Bonus::MORALE, +1);
  731. if(subID == ETownType::CASTLE) //castle
  732. {
  733. addBonusIfBuilt(BuildingID::LIGHTHOUSE, Bonus::SEA_MOVEMENT, +500, playerProp);
  734. addBonusIfBuilt(BuildingID::GRAIL, Bonus::MORALE, +2, playerProp); //colossus
  735. }
  736. else if(subID == ETownType::RAMPART) //rampart
  737. {
  738. addBonusIfBuilt(BuildingID::FOUNTAIN_OF_FORTUNE, Bonus::LUCK, +2); //fountain of fortune
  739. addBonusIfBuilt(BuildingID::GRAIL, Bonus::LUCK, +2, playerProp); //guardian spirit
  740. }
  741. else if(subID == ETownType::TOWER) //tower
  742. {
  743. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +15, PrimarySkill::KNOWLEDGE); //grail
  744. }
  745. else if(subID == ETownType::INFERNO) //Inferno
  746. {
  747. addBonusIfBuilt(BuildingID::STORMCLOUDS, Bonus::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); //Brimstone Clouds
  748. }
  749. else if(subID == ETownType::NECROPOLIS) //necropolis
  750. {
  751. addBonusIfBuilt(BuildingID::COVER_OF_DARKNESS, Bonus::DARKNESS, +20);
  752. addBonusIfBuilt(BuildingID::NECROMANCY_AMPLIFIER, Bonus::SECONDARY_SKILL_PREMY, +10, playerProp, SecondarySkill::NECROMANCY); //necromancy amplifier
  753. addBonusIfBuilt(BuildingID::GRAIL, Bonus::SECONDARY_SKILL_PREMY, +20, playerProp, SecondarySkill::NECROMANCY); //Soul prison
  754. }
  755. else if(subID == ETownType::DUNGEON) //Dungeon
  756. {
  757. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +12, PrimarySkill::SPELL_POWER); //grail
  758. }
  759. else if(subID == ETownType::STRONGHOLD) //Stronghold
  760. {
  761. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +20, PrimarySkill::ATTACK); //grail
  762. }
  763. else if(subID == ETownType::FORTRESS) //Fortress
  764. {
  765. addBonusIfBuilt(BuildingID::GLYPHS_OF_FEAR, Bonus::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); //Glyphs of Fear
  766. addBonusIfBuilt(BuildingID::BLOOD_OBELISK, Bonus::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); //Blood Obelisk
  767. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::ATTACK); //grail
  768. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::DEFENSE); //grail
  769. }
  770. else if(subID == ETownType::CONFLUX)
  771. {
  772. }
  773. }
  774. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype /*= -1*/)
  775. {
  776. static auto emptyPropagator = TPropagatorPtr();
  777. return addBonusIfBuilt(building, type, val, emptyPropagator, subtype);
  778. }
  779. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype /*= -1*/)
  780. {
  781. if(hasBuilt(building))
  782. {
  783. std::ostringstream descr;
  784. descr << town->buildings.at(building)->Name() << " ";
  785. if(val > 0)
  786. descr << "+";
  787. else if(val < 0)
  788. descr << "-";
  789. descr << val;
  790. Bonus *b = new Bonus(Bonus::PERMANENT, type, Bonus::TOWN_STRUCTURE, val, building, descr.str(), subtype);
  791. if(prop)
  792. b->addPropagator(prop);
  793. addNewBonus(b);
  794. return true;
  795. }
  796. return false;
  797. }
  798. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  799. {
  800. if (!(!!visitingHero == !h))
  801. {
  802. logGlobal->warnStream() << boost::format("Hero visiting town %s is %s ") % name % (visitingHero.get() ? visitingHero->name : "NULL");
  803. logGlobal->warnStream() << boost::format("New hero will be %s ") % (h ? h->name : "NULL");
  804. assert(!!visitingHero == !h);
  805. }
  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. }