CGTownInstance.cpp 36 KB

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