CGTownInstance.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 && ID != Obj::REFUGEE_CAMP)
  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->val, b->Description()));
  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->val * (base + castleBonus) / 100, b->Description()));
  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, bool includeUpgrade) 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. if (includeUpgrade)
  939. requirements.expressions.push_back(upgr->bid);
  940. requirements.expressions.push_back(upgr->requirements.morph(dependTest));
  941. }
  942. requirements.expressions.push_back(building->requirements.morph(dependTest));
  943. CBuilding::TRequired::Variant variant(requirements);
  944. CBuilding::TRequired ret(variant);
  945. ret.minimize();
  946. return ret;
  947. }
  948. void CGTownInstance::addHeroToStructureVisitors( const CGHeroInstance *h, si32 structureInstanceID ) const
  949. {
  950. if(visitingHero == h)
  951. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_VISITING_HERO, structureInstanceID); //add to visitors
  952. else if(garrisonHero == h)
  953. cb->setObjProperty(id, ObjProperty::STRUCTURE_ADD_GARRISONED_HERO, structureInstanceID); //then it must be garrisoned hero
  954. else
  955. {
  956. //should never ever happen
  957. logGlobal->errorStream() << "Cannot add hero " << h->name << " to visitors of structure #" << structureInstanceID;
  958. assert(0);
  959. }
  960. }
  961. void CGTownInstance::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  962. {
  963. if(result.winner == 0)
  964. {
  965. removeCapitols(hero->getOwner());
  966. cb->setOwner (this, hero->tempOwner); //give control after checkout is done
  967. FoWChange fw;
  968. fw.player = hero->tempOwner;
  969. fw.mode = 1;
  970. cb->getTilesInRange(fw.tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  971. cb->sendAndApply (&fw);
  972. }
  973. }
  974. COPWBonus::COPWBonus (BuildingID index, CGTownInstance *TOWN)
  975. {
  976. ID = index;
  977. town = TOWN;
  978. id = town->bonusingBuildings.size();
  979. }
  980. void COPWBonus::setProperty(ui8 what, ui32 val)
  981. {
  982. switch (what)
  983. {
  984. case ObjProperty::VISITORS:
  985. visitors.insert(val);
  986. break;
  987. case ObjProperty::STRUCTURE_CLEAR_VISITORS:
  988. visitors.clear();
  989. break;
  990. }
  991. }
  992. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  993. {
  994. ObjectInstanceID heroID = h->id;
  995. if (town->hasBuilt(ID))
  996. {
  997. InfoWindow iw;
  998. iw.player = h->tempOwner;
  999. switch (town->subID)
  1000. {
  1001. case ETownType::CASTLE: //Stables
  1002. if (!h->hasBonusFrom(Bonus::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
  1003. {
  1004. GiveBonus gb;
  1005. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  1006. gb.id = heroID.getNum();
  1007. cb->giveHeroBonus(&gb);
  1008. iw.text << VLC->generaltexth->allTexts[580];
  1009. cb->showInfoDialog(&iw);
  1010. }
  1011. break;
  1012. case ETownType::DUNGEON: //Mana Vortex
  1013. if (visitors.empty() && h->mana <= h->manaLimit() * 2)
  1014. {
  1015. cb->setManaPoints (heroID, 2 * h->manaLimit());
  1016. //TODO: investigate line below
  1017. //cb->setObjProperty (town->id, ObjProperty::VISITED, true);
  1018. iw.text << VLC->generaltexth->allTexts[579];
  1019. cb->showInfoDialog(&iw);
  1020. town->addHeroToStructureVisitors(h, id);
  1021. }
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. CTownBonus::CTownBonus (BuildingID index, CGTownInstance *TOWN)
  1027. {
  1028. ID = index;
  1029. town = TOWN;
  1030. id = town->bonusingBuildings.size();
  1031. }
  1032. void CTownBonus::setProperty (ui8 what, ui32 val)
  1033. {
  1034. if(what == ObjProperty::VISITORS)
  1035. visitors.insert(ObjectInstanceID(val));
  1036. }
  1037. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  1038. {
  1039. ObjectInstanceID heroID = h->id;
  1040. if (town->hasBuilt(ID) && visitors.find(heroID) == visitors.end())
  1041. {
  1042. InfoWindow iw;
  1043. PrimarySkill::PrimarySkill what = PrimarySkill::ATTACK;
  1044. int val=0, mid=0;
  1045. switch (ID)
  1046. {
  1047. case BuildingID::SPECIAL_4:
  1048. switch(town->subID)
  1049. {
  1050. case ETownType::TOWER: //wall
  1051. what = PrimarySkill::KNOWLEDGE;
  1052. val = 1;
  1053. mid = 581;
  1054. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1055. break;
  1056. case ETownType::INFERNO: //order of fire
  1057. what = PrimarySkill::SPELL_POWER;
  1058. val = 1;
  1059. mid = 582;
  1060. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1061. break;
  1062. case ETownType::STRONGHOLD://hall of Valhalla
  1063. what = PrimarySkill::ATTACK;
  1064. val = 1;
  1065. mid = 584;
  1066. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1067. break;
  1068. case ETownType::DUNGEON://academy of battle scholars
  1069. what = PrimarySkill::EXPERIENCE;
  1070. val = h->calculateXp(1000);
  1071. mid = 583;
  1072. iw.components.push_back (Component(Component::EXPERIENCE, 0, val, 0));
  1073. break;
  1074. }
  1075. break;
  1076. case BuildingID::SPECIAL_1:
  1077. switch(town->subID)
  1078. {
  1079. case ETownType::FORTRESS: //cage of warlords
  1080. what = PrimarySkill::DEFENSE;
  1081. val = 1;
  1082. mid = 585;
  1083. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1084. break;
  1085. }
  1086. break;
  1087. }
  1088. assert(mid);
  1089. iw.player = cb->getOwner(heroID);
  1090. iw.text << VLC->generaltexth->allTexts[mid];
  1091. cb->showInfoDialog(&iw);
  1092. cb->changePrimSkill (cb->getHero(heroID), what, val);
  1093. town->addHeroToStructureVisitors(h, id);
  1094. }
  1095. }
  1096. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  1097. : count(_count)
  1098. {
  1099. description = boost::str(boost::format(format) % count);
  1100. }
  1101. GrowthInfo::Entry::Entry(int subID, BuildingID building, int _count)
  1102. : count(_count)
  1103. {
  1104. description = boost::str(boost::format("%s %+d") % VLC->townh->factions[subID]->town->buildings.at(building)->Name() % count);
  1105. }
  1106. GrowthInfo::Entry::Entry(int _count, const std::string &fullDescription)
  1107. : count(_count)
  1108. {
  1109. description = fullDescription;
  1110. }
  1111. CTownAndVisitingHero::CTownAndVisitingHero()
  1112. {
  1113. setNodeType(TOWN_AND_VISITOR);
  1114. }
  1115. int GrowthInfo::totalGrowth() const
  1116. {
  1117. int ret = 0;
  1118. for(const Entry &entry : entries)
  1119. ret += entry.count;
  1120. return ret;
  1121. }