CGTownInstance.cpp 33 KB

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