CGTownInstance.cpp 35 KB

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