CGTownInstance.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. //
  805. //}
  806. assert(!!visitingHero == !h);
  807. if(h)
  808. {
  809. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  810. assert(p);
  811. h->detachFrom(p);
  812. h->attachTo(&townAndVis);
  813. visitingHero = h;
  814. h->visitedTown = this;
  815. h->inTownGarrison = false;
  816. }
  817. else
  818. {
  819. PlayerState *p = cb->gameState()->getPlayer(visitingHero->tempOwner);
  820. visitingHero->visitedTown = nullptr;
  821. visitingHero->detachFrom(&townAndVis);
  822. visitingHero->attachTo(p);
  823. visitingHero = nullptr;
  824. }
  825. }
  826. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  827. {
  828. assert(!!garrisonHero == !h);
  829. if(h)
  830. {
  831. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  832. assert(p);
  833. h->detachFrom(p);
  834. h->attachTo(this);
  835. garrisonHero = h;
  836. h->visitedTown = this;
  837. h->inTownGarrison = true;
  838. }
  839. else
  840. {
  841. PlayerState *p = cb->gameState()->getPlayer(garrisonHero->tempOwner);
  842. garrisonHero->visitedTown = nullptr;
  843. garrisonHero->inTownGarrison = false;
  844. garrisonHero->detachFrom(this);
  845. garrisonHero->attachTo(p);
  846. garrisonHero = nullptr;
  847. }
  848. updateMoraleBonusFromArmy(); //avoid giving morale bonus for same army twice
  849. }
  850. bool CGTownInstance::armedGarrison() const
  851. {
  852. return stacksCount() || garrisonHero;
  853. }
  854. int CGTownInstance::getTownLevel() const
  855. {
  856. // count all buildings that are not upgrades
  857. return boost::range::count_if(builtBuildings, [&](const BuildingID & build)
  858. {
  859. return town->buildings.at(build) && town->buildings.at(build)->upgrade == -1;
  860. });
  861. }
  862. CBonusSystemNode * CGTownInstance::whatShouldBeAttached()
  863. {
  864. return &townAndVis;
  865. }
  866. const CArmedInstance * CGTownInstance::getUpperArmy() const
  867. {
  868. if(garrisonHero)
  869. return garrisonHero;
  870. return this;
  871. }
  872. bool CGTownInstance::hasBuilt(BuildingID buildingID, int townID) const
  873. {
  874. if (townID == town->faction->index || townID == ETownType::ANY)
  875. return hasBuilt(buildingID);
  876. return false;
  877. }
  878. bool CGTownInstance::hasBuilt(BuildingID buildingID) const
  879. {
  880. return vstd::contains(builtBuildings, buildingID);
  881. }
  882. CBuilding::TRequired CGTownInstance::genBuildingRequirements(BuildingID buildID) const
  883. {
  884. const CBuilding * building = town->buildings.at(buildID);
  885. std::function<CBuilding::TRequired::Variant(const BuildingID &)> dependTest =
  886. [&](const BuildingID & id) -> CBuilding::TRequired::Variant
  887. {
  888. const CBuilding * build = town->buildings.at(id);
  889. if (!hasBuilt(id))
  890. return id;
  891. if (build->upgrade != BuildingID::NONE && !hasBuilt(build->upgrade))
  892. return build->upgrade;
  893. return build->requirements.morph(dependTest);
  894. };
  895. CBuilding::TRequired::OperatorAll requirements;
  896. if (building->upgrade != BuildingID::NONE)
  897. {
  898. const CBuilding * upgr = town->buildings.at(building->upgrade);
  899. requirements.expressions.push_back(upgr->bid);
  900. requirements.expressions.push_back(upgr->requirements.morph(dependTest));
  901. }
  902. requirements.expressions.push_back(building->requirements.morph(dependTest));
  903. CBuilding::TRequired::Variant variant(requirements);
  904. CBuilding::TRequired ret(variant);
  905. ret.minimize();
  906. return ret;
  907. }
  908. void CGTownInstance::addHeroToStructureVisitors( const CGHeroInstance *h, si32 structureInstanceID ) const
  909. {
  910. if(visitingHero == h)
  911. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  912. else if(garrisonHero == h)
  913. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  914. else
  915. {
  916. //should never ever happen
  917. logGlobal->errorStream() << "Cannot add hero " << h->name << " to visitors of structure #" << structureInstanceID;
  918. assert(0);
  919. }
  920. }
  921. void CGTownInstance::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  922. {
  923. if(result.winner == 0)
  924. {
  925. removeCapitols(hero->getOwner());
  926. cb->setOwner (this, hero->tempOwner); //give control after checkout is done
  927. FoWChange fw;
  928. fw.player = hero->tempOwner;
  929. fw.mode = 1;
  930. cb->getTilesInRange(fw.tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  931. cb->sendAndApply (&fw);
  932. }
  933. }
  934. COPWBonus::COPWBonus (BuildingID index, CGTownInstance *TOWN)
  935. {
  936. ID = index;
  937. town = TOWN;
  938. id = town->bonusingBuildings.size();
  939. }
  940. void COPWBonus::setProperty(ui8 what, ui32 val)
  941. {
  942. switch (what)
  943. {
  944. case ObjProperty::VISITORS:
  945. visitors.insert(val);
  946. break;
  947. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  948. visitors.clear();
  949. break;
  950. }
  951. }
  952. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  953. {
  954. ObjectInstanceID heroID = h->id;
  955. if (town->hasBuilt(ID))
  956. {
  957. InfoWindow iw;
  958. iw.player = h->tempOwner;
  959. switch (town->subID)
  960. {
  961. case ETownType::CASTLE: //Stables
  962. if (!h->hasBonusFrom(Bonus::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
  963. {
  964. GiveBonus gb;
  965. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  966. gb.id = heroID.getNum();
  967. cb->giveHeroBonus(&gb);
  968. iw.text << VLC->generaltexth->allTexts[580];
  969. cb->showInfoDialog(&iw);
  970. }
  971. break;
  972. case ETownType::DUNGEON: //Mana Vortex
  973. if (visitors.empty() && h->mana <= h->manaLimit() * 2)
  974. {
  975. cb->setManaPoints (heroID, 2 * h->manaLimit());
  976. //TODO: investigate line below
  977. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  978. iw.text << VLC->generaltexth->allTexts[579];
  979. cb->showInfoDialog(&iw);
  980. town->addHeroToStructureVisitors(h, id);
  981. }
  982. break;
  983. }
  984. }
  985. }
  986. CTownBonus::CTownBonus (BuildingID index, CGTownInstance *TOWN)
  987. {
  988. ID = index;
  989. town = TOWN;
  990. id = town->bonusingBuildings.size();
  991. }
  992. void CTownBonus::setProperty (ui8 what, ui32 val)
  993. {
  994. if(what == ObjProperty::VISITORS)
  995. visitors.insert(ObjectInstanceID(val));
  996. }
  997. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  998. {
  999. ObjectInstanceID heroID = h->id;
  1000. if (town->hasBuilt(ID) && visitors.find(heroID) == visitors.end())
  1001. {
  1002. InfoWindow iw;
  1003. PrimarySkill::PrimarySkill what = PrimarySkill::ATTACK;
  1004. int val=0, mid=0;
  1005. switch (ID)
  1006. {
  1007. case BuildingID::SPECIAL_4:
  1008. switch(town->subID)
  1009. {
  1010. case ETownType::TOWER: //wall
  1011. what = PrimarySkill::KNOWLEDGE;
  1012. val = 1;
  1013. mid = 581;
  1014. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1015. break;
  1016. case ETownType::INFERNO: //order of fire
  1017. what = PrimarySkill::SPELL_POWER;
  1018. val = 1;
  1019. mid = 582;
  1020. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1021. break;
  1022. case ETownType::STRONGHOLD://hall of Valhalla
  1023. what = PrimarySkill::ATTACK;
  1024. val = 1;
  1025. mid = 584;
  1026. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1027. break;
  1028. case ETownType::DUNGEON://academy of battle scholars
  1029. what = PrimarySkill::EXPERIENCE;
  1030. val = h->calculateXp(1000);
  1031. mid = 583;
  1032. iw.components.push_back (Component(Component::EXPERIENCE, 0, val, 0));
  1033. break;
  1034. }
  1035. break;
  1036. case BuildingID::SPECIAL_1:
  1037. switch(town->subID)
  1038. {
  1039. case ETownType::FORTRESS: //cage of warlords
  1040. what = PrimarySkill::DEFENSE;
  1041. val = 1;
  1042. mid = 585;
  1043. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1044. break;
  1045. }
  1046. break;
  1047. }
  1048. assert(mid);
  1049. iw.player = cb->getOwner(heroID);
  1050. iw.text << VLC->generaltexth->allTexts[mid];
  1051. cb->showInfoDialog(&iw);
  1052. cb->changePrimSkill (cb->getHero(heroID), what, val);
  1053. town->addHeroToStructureVisitors(h, id);
  1054. }
  1055. }
  1056. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1057. : count(_count)
  1058. {
  1059. description = boost::str(boost::format(format) % count);
  1060. }
  1061. GrowthInfo::Entry::Entry(int subID, BuildingID building, int _count)
  1062. : count(_count)
  1063. {
  1064. description = boost::str(boost::format("%s %+d") % VLC->townh->factions[subID]->town->buildings.at(building)->Name() % count);
  1065. }
  1066. CTownAndVisitingHero::CTownAndVisitingHero()
  1067. {
  1068. setNodeType(TOWN_AND_VISITOR);
  1069. }
  1070. int GrowthInfo::totalGrowth() const
  1071. {
  1072. int ret = 0;
  1073. for(const Entry &entry : entries)
  1074. ret += entry.count;
  1075. return ret;
  1076. }