CGTownInstance.cpp 36 KB

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