CGTownInstance.cpp 35 KB

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