CGTownInstance.cpp 36 KB

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