CGTownInstance.cpp 35 KB

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