CGTownInstance.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. cb->stopHeroVisitCastle(this, h);
  457. }
  458. std::string CGTownInstance::getObjectName() const
  459. {
  460. return name + ", " + town->faction->name;
  461. }
  462. void CGTownInstance::initObj()
  463. ///initialize town structures
  464. {
  465. blockVisit = true;
  466. if (subID == ETownType::DUNGEON)
  467. creatures.resize(GameConstants::CREATURES_PER_TOWN+1);//extra dwelling for Dungeon
  468. else
  469. creatures.resize(GameConstants::CREATURES_PER_TOWN);
  470. for (int level = 0; level < GameConstants::CREATURES_PER_TOWN; level++)
  471. {
  472. BuildingID buildID = BuildingID(BuildingID::DWELL_FIRST).advance(level);
  473. int upgradeNum = 0;
  474. for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(GameConstants::CREATURES_PER_TOWN))
  475. {
  476. if (hasBuilt(buildID) && town->creatures.at(level).size() > upgradeNum)
  477. creatures[level].second.push_back(town->creatures[level][upgradeNum]);
  478. }
  479. }
  480. switch (subID)
  481. { //add new visitable objects
  482. case 0:
  483. bonusingBuildings.push_back (new COPWBonus(BuildingID::STABLES, this));
  484. break;
  485. case 5:
  486. bonusingBuildings.push_back (new COPWBonus(BuildingID::MANA_VORTEX, this));
  487. //fallthrough
  488. case 2: case 3: case 6:
  489. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_4, this));
  490. break;
  491. case 7:
  492. bonusingBuildings.push_back (new CTownBonus(BuildingID::SPECIAL_1, this));
  493. break;
  494. }
  495. //add special bonuses from buildings
  496. recreateBuildingsBonuses();
  497. updateAppearance();
  498. }
  499. void CGTownInstance::newTurn() const
  500. {
  501. if (cb->getDate(Date::DAY_OF_WEEK) == 1) //reset on new week
  502. {
  503. auto & rand = cb->gameState()->getRandomGenerator();
  504. //give resources for Rampart, Mystic Pond
  505. if (hasBuilt(BuildingID::MYSTIC_POND, ETownType::RAMPART)
  506. && cb->getDate(Date::DAY) != 1 && (tempOwner < PlayerColor::PLAYER_LIMIT))
  507. {
  508. int resID = rand.nextInt(2, 5); //bonus to random rare resource
  509. resID = (resID==2)?1:resID;
  510. int resVal = rand.nextInt(1, 4);//with size 1..4
  511. cb->giveResource(tempOwner, static_cast<Res::ERes>(resID), resVal);
  512. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID);
  513. cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal);
  514. }
  515. if ( subID == ETownType::DUNGEON )
  516. for (auto & elem : bonusingBuildings)
  517. {
  518. if ((elem)->ID == BuildingID::MANA_VORTEX)
  519. cb->setObjProperty (id, ObjProperty::STRUCTURE_CLEAR_VISITORS, (elem)->id); //reset visitors for Mana Vortex
  520. }
  521. if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
  522. {
  523. std::vector<SlotID> nativeCrits; //slots
  524. for (auto & elem : Slots())
  525. {
  526. if (elem.second->type->faction == subID) //native
  527. {
  528. nativeCrits.push_back(elem.first); //collect matching slots
  529. }
  530. }
  531. if (nativeCrits.size())
  532. {
  533. SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
  534. StackLocation sl(this, pos);
  535. const CCreature *c = getCreature(pos);
  536. if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
  537. {
  538. cb->changeStackCount(sl, c->growth);
  539. }
  540. else //upgrade
  541. {
  542. cb->changeStackType(sl, VLC->creh->creatures[*c->upgrades.begin()]);
  543. }
  544. }
  545. if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
  546. {
  547. int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1);
  548. if (!town->creatures[i].empty())
  549. {
  550. CreatureID c = town->creatures[i][0];
  551. SlotID n;
  552. TQuantity count = creatureGrowth(i);
  553. if (!count) // no dwelling
  554. count = VLC->creh->creatures[c]->growth;
  555. {//no lower tiers or above current month
  556. if ((n = getSlotFor(c)).validSlot())
  557. {
  558. StackLocation sl(this, n);
  559. if (slotEmpty(n))
  560. cb->insertNewStack(sl, VLC->creh->creatures[c], count);
  561. else //add to existing
  562. cb->changeStackCount(sl, count);
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. /*
  571. int3 CGTownInstance::getSightCenter() const
  572. {
  573. return pos - int3(2,0,0);
  574. }
  575. */
  576. bool CGTownInstance::passableFor(PlayerColor color) const
  577. {
  578. if (!armedGarrison())//empty castle - anyone can visit
  579. return true;
  580. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  581. return false;
  582. if (cb->getPlayerRelations(tempOwner, color) != PlayerRelations::ENEMIES)
  583. return true;
  584. return false;
  585. }
  586. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  587. {
  588. offsets += int3(-1,2,0), int3(-3,2,0);
  589. }
  590. void CGTownInstance::removeCapitols (PlayerColor owner) const
  591. {
  592. if (hasCapitol()) // search if there's an older capitol
  593. {
  594. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  595. for (auto i = state->towns.cbegin(); i < state->towns.cend(); ++i)
  596. {
  597. if (*i != this && (*i)->hasCapitol())
  598. {
  599. RazeStructures rs;
  600. rs.tid = id;
  601. rs.bid.insert(BuildingID::CAPITOL);
  602. rs.destroyed = destroyed;
  603. cb->sendAndApply(&rs);
  604. return;
  605. }
  606. }
  607. }
  608. }
  609. int CGTownInstance::getBoatType() const
  610. {
  611. switch (town->faction->alignment)
  612. {
  613. case EAlignment::EVIL : return 0;
  614. case EAlignment::GOOD : return 1;
  615. case EAlignment::NEUTRAL : return 2;
  616. }
  617. assert(0);
  618. return -1;
  619. }
  620. int CGTownInstance::getMarketEfficiency() const
  621. {
  622. if (!hasBuilt(BuildingID::MARKETPLACE))
  623. return 0;
  624. const PlayerState *p = cb->getPlayer(tempOwner);
  625. assert(p);
  626. int marketCount = 0;
  627. for(const CGTownInstance *t : p->towns)
  628. if(t->hasBuilt(BuildingID::MARKETPLACE))
  629. marketCount++;
  630. return marketCount;
  631. }
  632. bool CGTownInstance::allowsTrade(EMarketMode::EMarketMode mode) const
  633. {
  634. switch(mode)
  635. {
  636. case EMarketMode::RESOURCE_RESOURCE:
  637. case EMarketMode::RESOURCE_PLAYER:
  638. return hasBuilt(BuildingID::MARKETPLACE);
  639. case EMarketMode::ARTIFACT_RESOURCE:
  640. case EMarketMode::RESOURCE_ARTIFACT:
  641. return hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::TOWER)
  642. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::DUNGEON)
  643. || hasBuilt(BuildingID::ARTIFACT_MERCHANT, ETownType::CONFLUX);
  644. case EMarketMode::CREATURE_RESOURCE:
  645. return hasBuilt(BuildingID::FREELANCERS_GUILD, ETownType::STRONGHOLD);
  646. case EMarketMode::CREATURE_UNDEAD:
  647. return hasBuilt(BuildingID::SKELETON_TRANSFORMER, ETownType::NECROPOLIS);
  648. case EMarketMode::RESOURCE_SKILL:
  649. return hasBuilt(BuildingID::MAGIC_UNIVERSITY, ETownType::CONFLUX);
  650. default:
  651. assert(0);
  652. return false;
  653. }
  654. }
  655. std::vector<int> CGTownInstance::availableItemsIds(EMarketMode::EMarketMode mode) const
  656. {
  657. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  658. {
  659. std::vector<int> ret;
  660. for(const CArtifact *a : merchantArtifacts)
  661. if(a)
  662. ret.push_back(a->id);
  663. else
  664. ret.push_back(-1);
  665. return ret;
  666. }
  667. else if ( mode == EMarketMode::RESOURCE_SKILL )
  668. {
  669. return universitySkills;
  670. }
  671. else
  672. return IMarket::availableItemsIds(mode);
  673. }
  674. void CGTownInstance::setType(si32 ID, si32 subID)
  675. {
  676. assert(ID == Obj::TOWN); // just in case
  677. CGObjectInstance::setType(ID, subID);
  678. town = VLC->townh->factions[subID]->town;
  679. randomizeArmy(subID);
  680. updateAppearance();
  681. }
  682. void CGTownInstance::updateAppearance()
  683. {
  684. //FIXME: not the best way to do this
  685. auto app = VLC->objtypeh->getHandlerFor(ID, subID)->getOverride(cb->gameState()->getTile(visitablePos())->terType, this);
  686. if (app)
  687. appearance = app.get();
  688. }
  689. std::string CGTownInstance::nodeName() const
  690. {
  691. return "Town (" + (town ? town->faction->name : "unknown") + ") of " + name;
  692. }
  693. void CGTownInstance::deserializationFix()
  694. {
  695. attachTo(&townAndVis);
  696. //Hero is already handled by CGameState::attachArmedObjects
  697. // if(visitingHero)
  698. // visitingHero->attachTo(&townAndVis);
  699. // if(garrisonHero)
  700. // garrisonHero->attachTo(this);
  701. }
  702. void CGTownInstance::updateMoraleBonusFromArmy()
  703. {
  704. Bonus *b = getBonusList().getFirst(Selector::sourceType(Bonus::ARMY).And(Selector::type(Bonus::MORALE)));
  705. if(!b)
  706. {
  707. b = new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  708. addNewBonus(b);
  709. }
  710. if (garrisonHero)
  711. b->val = 0;
  712. else
  713. CArmedInstance::updateMoraleBonusFromArmy();
  714. }
  715. void CGTownInstance::recreateBuildingsBonuses()
  716. {
  717. static TPropagatorPtr playerProp(new CPropagatorNodeType(PLAYER));
  718. BonusList bl;
  719. getExportedBonusList().getBonuses(bl, Selector::sourceType(Bonus::TOWN_STRUCTURE));
  720. for(Bonus *b : bl)
  721. removeBonus(b);
  722. //tricky! -> checks tavern only if no bratherhood of sword or not a castle
  723. if(subID != ETownType::CASTLE || !addBonusIfBuilt(BuildingID::BROTHERHOOD, Bonus::MORALE, +2))
  724. addBonusIfBuilt(BuildingID::TAVERN, Bonus::MORALE, +1);
  725. if(subID == ETownType::CASTLE) //castle
  726. {
  727. addBonusIfBuilt(BuildingID::LIGHTHOUSE, Bonus::SEA_MOVEMENT, +500, playerProp);
  728. addBonusIfBuilt(BuildingID::GRAIL, Bonus::MORALE, +2, playerProp); //colossus
  729. }
  730. else if(subID == ETownType::RAMPART) //rampart
  731. {
  732. addBonusIfBuilt(BuildingID::FOUNTAIN_OF_FORTUNE, Bonus::LUCK, +2); //fountain of fortune
  733. addBonusIfBuilt(BuildingID::GRAIL, Bonus::LUCK, +2, playerProp); //guardian spirit
  734. }
  735. else if(subID == ETownType::TOWER) //tower
  736. {
  737. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +15, PrimarySkill::KNOWLEDGE); //grail
  738. }
  739. else if(subID == ETownType::INFERNO) //Inferno
  740. {
  741. addBonusIfBuilt(BuildingID::STORMCLOUDS, Bonus::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); //Brimstone Clouds
  742. }
  743. else if(subID == ETownType::NECROPOLIS) //necropolis
  744. {
  745. addBonusIfBuilt(BuildingID::COVER_OF_DARKNESS, Bonus::DARKNESS, +20);
  746. addBonusIfBuilt(BuildingID::NECROMANCY_AMPLIFIER, Bonus::SECONDARY_SKILL_PREMY, +10, playerProp, SecondarySkill::NECROMANCY); //necromancy amplifier
  747. addBonusIfBuilt(BuildingID::GRAIL, Bonus::SECONDARY_SKILL_PREMY, +20, playerProp, SecondarySkill::NECROMANCY); //Soul prison
  748. }
  749. else if(subID == ETownType::DUNGEON) //Dungeon
  750. {
  751. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +12, PrimarySkill::SPELL_POWER); //grail
  752. }
  753. else if(subID == ETownType::STRONGHOLD) //Stronghold
  754. {
  755. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +20, PrimarySkill::ATTACK); //grail
  756. }
  757. else if(subID == ETownType::FORTRESS) //Fortress
  758. {
  759. addBonusIfBuilt(BuildingID::GLYPHS_OF_FEAR, Bonus::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); //Glyphs of Fear
  760. addBonusIfBuilt(BuildingID::BLOOD_OBELISK, Bonus::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); //Blood Obelisk
  761. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::ATTACK); //grail
  762. addBonusIfBuilt(BuildingID::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::DEFENSE); //grail
  763. }
  764. else if(subID == ETownType::CONFLUX)
  765. {
  766. }
  767. }
  768. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype /*= -1*/)
  769. {
  770. static auto emptyPropagator = TPropagatorPtr();
  771. return addBonusIfBuilt(building, type, val, emptyPropagator, subtype);
  772. }
  773. bool CGTownInstance::addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr & prop, int subtype /*= -1*/)
  774. {
  775. if(hasBuilt(building))
  776. {
  777. std::ostringstream descr;
  778. descr << town->buildings.at(building)->Name() << " ";
  779. if(val > 0)
  780. descr << "+";
  781. else if(val < 0)
  782. descr << "-";
  783. descr << val;
  784. Bonus *b = new Bonus(Bonus::PERMANENT, type, Bonus::TOWN_STRUCTURE, val, building, descr.str(), subtype);
  785. if(prop)
  786. b->addPropagator(prop);
  787. addNewBonus(b);
  788. return true;
  789. }
  790. return false;
  791. }
  792. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  793. {
  794. assert(!!visitingHero == !h);
  795. if(h)
  796. {
  797. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  798. assert(p);
  799. h->detachFrom(p);
  800. h->attachTo(&townAndVis);
  801. visitingHero = h;
  802. h->visitedTown = this;
  803. h->inTownGarrison = false;
  804. }
  805. else
  806. {
  807. PlayerState *p = cb->gameState()->getPlayer(visitingHero->tempOwner);
  808. visitingHero->visitedTown = nullptr;
  809. visitingHero->detachFrom(&townAndVis);
  810. visitingHero->attachTo(p);
  811. visitingHero = nullptr;
  812. }
  813. }
  814. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  815. {
  816. assert(!!garrisonHero == !h);
  817. if(h)
  818. {
  819. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  820. assert(p);
  821. h->detachFrom(p);
  822. h->attachTo(this);
  823. garrisonHero = h;
  824. h->visitedTown = this;
  825. h->inTownGarrison = true;
  826. }
  827. else
  828. {
  829. PlayerState *p = cb->gameState()->getPlayer(garrisonHero->tempOwner);
  830. garrisonHero->visitedTown = nullptr;
  831. garrisonHero->inTownGarrison = false;
  832. garrisonHero->detachFrom(this);
  833. garrisonHero->attachTo(p);
  834. garrisonHero = nullptr;
  835. }
  836. updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
  837. }
  838. bool CGTownInstance::armedGarrison() const
  839. {
  840. return stacksCount() || garrisonHero;
  841. }
  842. int CGTownInstance::getTownLevel() const
  843. {
  844. // count all buildings that are not upgrades
  845. return boost::range::count_if(builtBuildings, [&](const BuildingID & build)
  846. {
  847. return town->buildings.at(build) && town->buildings.at(build)->upgrade == -1;
  848. });
  849. }
  850. CBonusSystemNode * CGTownInstance::whatShouldBeAttached()
  851. {
  852. return &townAndVis;
  853. }
  854. const CArmedInstance * CGTownInstance::getUpperArmy() const
  855. {
  856. if(garrisonHero)
  857. return garrisonHero;
  858. return this;
  859. }
  860. bool CGTownInstance::hasBuilt(BuildingID buildingID, int townID) const
  861. {
  862. if (townID == town->faction->index || townID == ETownType::ANY)
  863. return hasBuilt(buildingID);
  864. return false;
  865. }
  866. bool CGTownInstance::hasBuilt(BuildingID buildingID) const
  867. {
  868. return vstd::contains(builtBuildings, buildingID);
  869. }
  870. void CGTownInstance::addHeroToStructureVisitors( const CGHeroInstance *h, si32 structureInstanceID ) const
  871. {
  872. if(visitingHero == h)
  873. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  874. else if(garrisonHero == h)
  875. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  876. else
  877. {
  878. //should never ever happen
  879. logGlobal->errorStream() << "Cannot add hero " << h->name << " to visitors of structure #" << structureInstanceID;
  880. assert(0);
  881. }
  882. }
  883. void CGTownInstance::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  884. {
  885. if(result.winner == 0)
  886. {
  887. removeCapitols(hero->getOwner());
  888. cb->setOwner (this, hero->tempOwner); //give control after checkout is done
  889. FoWChange fw;
  890. fw.player = hero->tempOwner;
  891. fw.mode = 1;
  892. cb->getTilesInRange(fw.tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  893. cb->sendAndApply (&fw);
  894. }
  895. }
  896. COPWBonus::COPWBonus (BuildingID index, CGTownInstance *TOWN)
  897. {
  898. ID = index;
  899. town = TOWN;
  900. id = town->bonusingBuildings.size();
  901. }
  902. void COPWBonus::setProperty(ui8 what, ui32 val)
  903. {
  904. switch (what)
  905. {
  906. case ObjProperty::VISITORS:
  907. visitors.insert(val);
  908. break;
  909. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  910. visitors.clear();
  911. break;
  912. }
  913. }
  914. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  915. {
  916. ObjectInstanceID heroID = h->id;
  917. if (town->hasBuilt(ID))
  918. {
  919. InfoWindow iw;
  920. iw.player = h->tempOwner;
  921. switch (town->subID)
  922. {
  923. case ETownType::CASTLE: //Stables
  924. if (!h->hasBonusFrom(Bonus::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
  925. {
  926. GiveBonus gb;
  927. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  928. gb.id = heroID.getNum();
  929. cb->giveHeroBonus(&gb);
  930. iw.text << VLC->generaltexth->allTexts[580];
  931. cb->showInfoDialog(&iw);
  932. }
  933. break;
  934. case ETownType::DUNGEON: //Mana Vortex
  935. if (visitors.empty() && h->mana <= h->manaLimit() * 2)
  936. {
  937. cb->setManaPoints (heroID, 2 * h->manaLimit());
  938. //TODO: investigate line below
  939. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  940. iw.text << VLC->generaltexth->allTexts[579];
  941. cb->showInfoDialog(&iw);
  942. town->addHeroToStructureVisitors(h, id);
  943. }
  944. break;
  945. }
  946. }
  947. }
  948. CTownBonus::CTownBonus (BuildingID index, CGTownInstance *TOWN)
  949. {
  950. ID = index;
  951. town = TOWN;
  952. id = town->bonusingBuildings.size();
  953. }
  954. void CTownBonus::setProperty (ui8 what, ui32 val)
  955. {
  956. if(what == ObjProperty::VISITORS)
  957. visitors.insert(ObjectInstanceID(val));
  958. }
  959. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  960. {
  961. ObjectInstanceID heroID = h->id;
  962. if (town->hasBuilt(ID) && visitors.find(heroID) == visitors.end())
  963. {
  964. InfoWindow iw;
  965. PrimarySkill::PrimarySkill what = PrimarySkill::ATTACK;
  966. int val=0, mid=0;
  967. switch (ID)
  968. {
  969. case BuildingID::SPECIAL_4:
  970. switch(town->subID)
  971. {
  972. case ETownType::TOWER: //wall
  973. what = PrimarySkill::KNOWLEDGE;
  974. val = 1;
  975. mid = 581;
  976. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  977. break;
  978. case ETownType::INFERNO: //order of fire
  979. what = PrimarySkill::SPELL_POWER;
  980. val = 1;
  981. mid = 582;
  982. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  983. break;
  984. case ETownType::STRONGHOLD://hall of Valhalla
  985. what = PrimarySkill::ATTACK;
  986. val = 1;
  987. mid = 584;
  988. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  989. break;
  990. case ETownType::DUNGEON://academy of battle scholars
  991. what = PrimarySkill::EXPERIENCE;
  992. val = h->calculateXp(1000);
  993. mid = 583;
  994. iw.components.push_back (Component(Component::EXPERIENCE, 0, val, 0));
  995. break;
  996. }
  997. break;
  998. case BuildingID::SPECIAL_1:
  999. switch(town->subID)
  1000. {
  1001. case ETownType::FORTRESS: //cage of warlords
  1002. what = PrimarySkill::DEFENSE;
  1003. val = 1;
  1004. mid = 585;
  1005. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1006. break;
  1007. }
  1008. break;
  1009. }
  1010. assert(mid);
  1011. iw.player = cb->getOwner(heroID);
  1012. iw.text << VLC->generaltexth->allTexts[mid];
  1013. cb->showInfoDialog(&iw);
  1014. cb->changePrimSkill (cb->getHero(heroID), what, val);
  1015. town->addHeroToStructureVisitors(h, id);
  1016. }
  1017. }
  1018. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1019. : count(_count)
  1020. {
  1021. description = boost::str(boost::format(format) % count);
  1022. }
  1023. GrowthInfo::Entry::Entry(int subID, BuildingID building, int _count)
  1024. : count(_count)
  1025. {
  1026. description = boost::str(boost::format("%s %+d") % VLC->townh->factions[subID]->town->buildings.at(building)->Name() % count);
  1027. }
  1028. CTownAndVisitingHero::CTownAndVisitingHero()
  1029. {
  1030. setNodeType(TOWN_AND_VISITOR);
  1031. }
  1032. int GrowthInfo::totalGrowth() const
  1033. {
  1034. int ret = 0;
  1035. for(const Entry &entry : entries)
  1036. ret += entry.count;
  1037. return ret;
  1038. }