CGTownInstance.cpp 36 KB

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