MiscObjects.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. * MiscObjects.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 "MiscObjects.h"
  12. #include "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CSoundBase.h"
  15. #include "../CModHandler.h"
  16. #include "CObjectClassesHandler.h"
  17. #include "../CSpellHandler.h"
  18. #include "../IGameCallback.h"
  19. #include "../CGameState.h"
  20. std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > CGTeleport::objs;
  21. std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > CGTeleport::gates;
  22. std::map <si32, std::vector<ObjectInstanceID> > CGMagi::eyelist;
  23. ui8 CGObelisk::obeliskCount; //how many obelisks are on map
  24. std::map<TeamID, ui8> CGObelisk::visited; //map: team_id => how many obelisks has been visited
  25. ///helpers
  26. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  27. {
  28. OpenWindow ow;
  29. ow.window = type;
  30. ow.id1 = id1;
  31. ow.id2 = id2;
  32. IObjectInterface::cb->sendAndApply(&ow);
  33. }
  34. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  35. {
  36. InfoWindow iw;
  37. iw.soundID = soundID;
  38. iw.player = playerID;
  39. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  40. IObjectInterface::cb->sendAndApply(&iw);
  41. }
  42. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  43. {
  44. const PlayerColor playerID = h->getOwner();
  45. showInfoDialog(playerID,txtID,soundID);
  46. }
  47. static std::string & visitedTxt(const bool visited)
  48. {
  49. int id = visited ? 352 : 353;
  50. return VLC->generaltexth->allTexts[id];
  51. }
  52. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  53. {
  54. if(what == 10)
  55. players.insert(PlayerColor(val));
  56. }
  57. bool CPlayersVisited::wasVisited( PlayerColor player ) const
  58. {
  59. return vstd::contains(players,player);
  60. }
  61. bool CPlayersVisited::wasVisited( TeamID team ) const
  62. {
  63. for(auto i : players)
  64. {
  65. if(cb->getPlayer(i)->team == team)
  66. return true;
  67. }
  68. return false;
  69. }
  70. std::string CGCreature::getHoverText(PlayerColor player) const
  71. {
  72. if(stacks.empty())
  73. {
  74. //should not happen...
  75. logGlobal->errorStream() << "Invalid stack at tile " << pos << ": subID=" << subID << "; id=" << id;
  76. return "!!!INVALID_STACK!!!";
  77. }
  78. std::string hoverName;
  79. MetaString ms;
  80. int pom = stacks.begin()->second->getQuantityID();
  81. pom = 172 + 3*pom;
  82. ms.addTxt(MetaString::ARRAY_TXT,pom);
  83. ms << " " ;
  84. ms.addTxt(MetaString::CRE_PL_NAMES,subID);
  85. ms.toString(hoverName);
  86. return hoverName;
  87. }
  88. std::string CGCreature::getHoverText(const CGHeroInstance * hero) const
  89. {
  90. std::string hoverName = getHoverText(hero->tempOwner);
  91. const JsonNode & texts = VLC->generaltexth->localizedTexts["adventureMap"]["monsterThreat"];
  92. hoverName += texts["title"].String();
  93. int choice;
  94. double ratio = ((double)getArmyStrength() / hero->getTotalStrength());
  95. if (ratio < 0.1) choice = 0;
  96. else if (ratio < 0.25) choice = 1;
  97. else if (ratio < 0.6) choice = 2;
  98. else if (ratio < 0.9) choice = 3;
  99. else if (ratio < 1.1) choice = 4;
  100. else if (ratio < 1.3) choice = 5;
  101. else if (ratio < 1.8) choice = 6;
  102. else if (ratio < 2.5) choice = 7;
  103. else if (ratio < 4) choice = 8;
  104. else if (ratio < 8) choice = 9;
  105. else if (ratio < 20) choice = 10;
  106. else choice = 11;
  107. hoverName += texts["levels"].Vector()[choice].String();
  108. return hoverName;
  109. }
  110. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  111. {
  112. int action = takenAction(h);
  113. switch( action ) //decide what we do...
  114. {
  115. case FIGHT:
  116. fight(h);
  117. break;
  118. case FLEE: //flee
  119. {
  120. flee(h);
  121. break;
  122. }
  123. case JOIN_FOR_FREE: //join for free
  124. {
  125. BlockingDialog ynd(true,false);
  126. ynd.player = h->tempOwner;
  127. ynd.text.addTxt(MetaString::ADVOB_TXT, 86);
  128. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  129. cb->showBlockingDialog(&ynd);
  130. break;
  131. }
  132. default: //join for gold
  133. {
  134. assert(action > 0);
  135. //ask if player agrees to pay gold
  136. BlockingDialog ynd(true,false);
  137. ynd.player = h->tempOwner;
  138. std::string tmp = VLC->generaltexth->advobtxt[90];
  139. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(getStackCount(SlotID(0))));
  140. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  141. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  142. ynd.text << tmp;
  143. cb->showBlockingDialog(&ynd);
  144. break;
  145. }
  146. }
  147. }
  148. void CGCreature::initObj()
  149. {
  150. blockVisit = true;
  151. switch(character)
  152. {
  153. case 0:
  154. character = -4;
  155. break;
  156. case 1:
  157. character = cb->gameState()->getRandomGenerator().nextInt(1, 7);
  158. break;
  159. case 2:
  160. character = cb->gameState()->getRandomGenerator().nextInt(1, 10);
  161. break;
  162. case 3:
  163. character = cb->gameState()->getRandomGenerator().nextInt(4, 10);
  164. break;
  165. case 4:
  166. character = 10;
  167. break;
  168. }
  169. stacks[SlotID(0)]->setType(CreatureID(subID));
  170. TQuantity &amount = stacks[SlotID(0)]->count;
  171. CCreature &c = *VLC->creh->creatures[subID];
  172. if(amount == 0)
  173. {
  174. amount = cb->gameState()->getRandomGenerator().nextInt(c.ammMin, c.ammMax);
  175. if(amount == 0) //armies with 0 creatures are illegal
  176. {
  177. logGlobal->warnStream() << "Problem: stack " << nodeName() << " cannot have 0 creatures. Check properties of " << c.nodeName();
  178. amount = 1;
  179. }
  180. }
  181. formation.randomFormation = cb->gameState()->getRandomGenerator().nextInt();
  182. temppower = stacks[SlotID(0)]->count * 1000;
  183. refusedJoining = false;
  184. }
  185. void CGCreature::newTurn() const
  186. {//Works only for stacks of single type of size up to 2 millions
  187. if (stacks.begin()->second->count < VLC->modh->settings.CREEP_SIZE && cb->getDate(Date::DAY_OF_WEEK) == 1 && cb->getDate(Date::DAY) > 1)
  188. {
  189. ui32 power = temppower * (100 + VLC->modh->settings.WEEKLY_GROWTH)/100;
  190. cb->setObjProperty(id, ObjProperty::MONSTER_COUNT, std::min (power/1000 , (ui32)VLC->modh->settings.CREEP_SIZE)); //set new amount
  191. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  192. }
  193. if (VLC->modh->modules.STACK_EXP)
  194. cb->setObjProperty(id, ObjProperty::MONSTER_EXP, VLC->modh->settings.NEUTRAL_STACK_EXP); //for testing purpose
  195. }
  196. void CGCreature::setPropertyDer(ui8 what, ui32 val)
  197. {
  198. switch (what)
  199. {
  200. case ObjProperty::MONSTER_COUNT:
  201. stacks[SlotID(0)]->count = val;
  202. break;
  203. case ObjProperty::MONSTER_POWER:
  204. temppower = val;
  205. break;
  206. case ObjProperty::MONSTER_EXP:
  207. giveStackExp(val);
  208. break;
  209. case ObjProperty::MONSTER_RESTORE_TYPE:
  210. formation.basicType = val;
  211. break;
  212. case ObjProperty::MONSTER_REFUSED_JOIN:
  213. refusedJoining = val;
  214. break;
  215. }
  216. }
  217. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  218. {
  219. //calculate relative strength of hero and creatures armies
  220. double relStrength = double(h->getTotalStrength()) / getArmyStrength();
  221. int powerFactor;
  222. if(relStrength >= 7)
  223. powerFactor = 11;
  224. else if(relStrength >= 1)
  225. powerFactor = (int)(2*(relStrength-1));
  226. else if(relStrength >= 0.5)
  227. powerFactor = -1;
  228. else if(relStrength >= 0.333)
  229. powerFactor = -2;
  230. else
  231. powerFactor = -3;
  232. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  233. const CCreature * myCreature = VLC->creh->creatures[subID];
  234. myKindCres.insert(myCreature->idNumber); //we
  235. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  236. for(ConstTransitivePtr<CCreature> &crea : VLC->creh->creatures)
  237. {
  238. if(vstd::contains(crea->upgrades, myCreature->idNumber)) //it's our base creatures
  239. myKindCres.insert(crea->idNumber);
  240. }
  241. int count = 0, //how many creatures of similar kind has hero
  242. totalCount = 0;
  243. for (auto & elem : h->Slots())
  244. {
  245. if(vstd::contains(myKindCres,elem.second->type->idNumber))
  246. count += elem.second->count;
  247. totalCount += elem.second->count;
  248. }
  249. int sympathy = 0; // 0 if hero have no similar creatures
  250. if(count)
  251. sympathy++; // 1 - if hero have at least 1 similar creature
  252. if(count*2 > totalCount)
  253. sympathy++; // 2 - hero have similar creatures more that 50%
  254. int charisma = powerFactor + h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy;
  255. if(charisma < character) //creatures will fight
  256. return -2;
  257. if (allowJoin)
  258. {
  259. if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy + 1 >= character)
  260. return 0; //join for free
  261. else if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) * 2 + sympathy + 1 >= character)
  262. return VLC->creh->creatures[subID]->cost[6] * getStackCount(SlotID(0)); //join for gold
  263. }
  264. //we are still here - creatures have not joined hero, flee or fight
  265. if (charisma > character)
  266. return -1; //flee
  267. else
  268. return -2; //fight
  269. }
  270. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  271. {
  272. if(refusedJoining)
  273. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  274. if(pursue)
  275. {
  276. fight(h);
  277. }
  278. else
  279. {
  280. cb->removeObject(this);
  281. }
  282. }
  283. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  284. {
  285. if(!accept)
  286. {
  287. if(takenAction(h,false) == -1) //they flee
  288. {
  289. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  290. flee(h);
  291. }
  292. else //they fight
  293. {
  294. showInfoDialog(h,87,0);//Insulted by your refusal of their offer, the monsters attack!
  295. fight(h);
  296. }
  297. }
  298. else //accepted
  299. {
  300. if (cb->getResource(h->tempOwner, Res::GOLD) < cost) //player don't have enough gold!
  301. {
  302. InfoWindow iw;
  303. iw.player = h->tempOwner;
  304. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  305. cb->showInfoDialog(&iw);
  306. //act as if player refused
  307. joinDecision(h,cost,false);
  308. return;
  309. }
  310. //take gold
  311. if(cost)
  312. cb->giveResource(h->tempOwner,Res::GOLD,-cost);
  313. cb->tryJoiningArmy(this, h, true, true);
  314. }
  315. }
  316. void CGCreature::fight( const CGHeroInstance *h ) const
  317. {
  318. //split stacks
  319. //TODO: multiple creature types in a stack?
  320. int basicType = stacks.begin()->second->type->idNumber;
  321. cb->setObjProperty(id, ObjProperty::MONSTER_RESTORE_TYPE, basicType); //store info about creature stack
  322. double relativePower = static_cast<double>(h->getTotalStrength()) / getArmyStrength();
  323. int stacksCount;
  324. //TODO: number depends on tile type
  325. if (relativePower < 0.5)
  326. {
  327. stacksCount = 7;
  328. }
  329. else if (relativePower < 0.67)
  330. {
  331. stacksCount = 7;
  332. }
  333. else if (relativePower < 1)
  334. {
  335. stacksCount = 6;
  336. }
  337. else if (relativePower < 1.5)
  338. {
  339. stacksCount = 5;
  340. }
  341. else if (relativePower < 2)
  342. {
  343. stacksCount = 4;
  344. }
  345. else
  346. {
  347. stacksCount = 3;
  348. }
  349. SlotID sourceSlot = stacks.begin()->first;
  350. SlotID destSlot;
  351. for (int stacksLeft = stacksCount; stacksLeft > 1; --stacksLeft)
  352. {
  353. int stackSize = stacks.begin()->second->count / stacksLeft;
  354. if (stackSize)
  355. {
  356. if ((destSlot = getFreeSlot()).validSlot())
  357. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, destSlot), stackSize);
  358. else
  359. {
  360. logGlobal->warnStream() <<"Warning! Not enough empty slots to split stack!";
  361. break;
  362. }
  363. }
  364. else break;
  365. }
  366. if (stacksCount > 1)
  367. {
  368. if (formation.randomFormation % 100 < 50) //upgrade
  369. {
  370. SlotID slotId = SlotID(stacks.size() / 2);
  371. const auto & upgrades = getStack(slotId).type->upgrades;
  372. if(!upgrades.empty())
  373. {
  374. auto it = RandomGeneratorUtil::nextItem(upgrades, cb->gameState()->getRandomGenerator());
  375. cb->changeStackType(StackLocation(this, slotId), VLC->creh->creatures[*it]);
  376. }
  377. }
  378. }
  379. cb->startBattleI(h, this);
  380. }
  381. void CGCreature::flee( const CGHeroInstance * h ) const
  382. {
  383. BlockingDialog ynd(true,false);
  384. ynd.player = h->tempOwner;
  385. ynd.text.addTxt(MetaString::ADVOB_TXT,91);
  386. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  387. cb->showBlockingDialog(&ynd);
  388. }
  389. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  390. {
  391. if(result.winner==0)
  392. {
  393. cb->removeObject(this);
  394. }
  395. else
  396. {
  397. //merge stacks into one
  398. TSlots::const_iterator i;
  399. CCreature * cre = VLC->creh->creatures[formation.basicType];
  400. for (i = stacks.begin(); i != stacks.end(); i++)
  401. {
  402. if (cre->isMyUpgrade(i->second->type))
  403. {
  404. cb->changeStackType (StackLocation(this, i->first), cre); //un-upgrade creatures
  405. }
  406. }
  407. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  408. if(!hasStackAtSlot(SlotID(0)))
  409. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  410. while (stacks.size() > 1) //hopefully that's enough
  411. {
  412. // TODO it's either overcomplicated (if we assume there'll be only one stack) or buggy (if we allow multiple stacks... but that'll also cause troubles elsewhere)
  413. i = stacks.end();
  414. i--;
  415. SlotID slot = getSlotFor(i->second->type);
  416. if (slot == i->first) //no reason to move stack to its own slot
  417. break;
  418. else
  419. cb->moveStack (StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  420. }
  421. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  422. }
  423. }
  424. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  425. {
  426. auto action = takenAction(hero);
  427. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  428. joinDecision(hero, action, answer);
  429. else if(action != FIGHT)
  430. fleeDecision(hero, answer);
  431. else
  432. assert(0);
  433. }
  434. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  435. {
  436. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  437. if(relations == 2) //we're visiting our mine
  438. {
  439. cb->showGarrisonDialog(id,h->id,true);
  440. return;
  441. }
  442. else if (relations == 1)//ally
  443. return;
  444. if(stacksCount()) //Mine is guarded
  445. {
  446. BlockingDialog ynd(true,false);
  447. ynd.player = h->tempOwner;
  448. ynd.text.addTxt(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  449. cb->showBlockingDialog(&ynd);
  450. return;
  451. }
  452. flagMine(h->tempOwner);
  453. }
  454. void CGMine::newTurn() const
  455. {
  456. if(cb->getDate() == 1)
  457. return;
  458. if (tempOwner == PlayerColor::NEUTRAL)
  459. return;
  460. cb->giveResource(tempOwner, producedResource, producedQuantity);
  461. }
  462. void CGMine::initObj()
  463. {
  464. if(subID >= 7) //Abandoned Mine
  465. {
  466. //set guardians
  467. int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
  468. auto troglodytes = new CStackInstance(CreatureID::TROGLODYTES, howManyTroglodytes);
  469. putStack(SlotID(0), troglodytes);
  470. //after map reading tempOwner placeholds bitmask for allowed resources
  471. std::vector<Res::ERes> possibleResources;
  472. for (int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  473. if(tempOwner.getNum() & 1<<i) //NOTE: reuse of tempOwner
  474. possibleResources.push_back(static_cast<Res::ERes>(i));
  475. assert(!possibleResources.empty());
  476. producedResource = *RandomGeneratorUtil::nextItem(possibleResources, cb->gameState()->getRandomGenerator());
  477. tempOwner = PlayerColor::NEUTRAL;
  478. }
  479. else
  480. {
  481. producedResource = static_cast<Res::ERes>(subID);
  482. if(tempOwner >= PlayerColor::PLAYER_LIMIT)
  483. tempOwner = PlayerColor::NEUTRAL;
  484. }
  485. producedQuantity = defaultResProduction();
  486. }
  487. std::string CGMine::getObjectName() const
  488. {
  489. return VLC->generaltexth->mines.at(subID).first;
  490. }
  491. std::string CGMine::getHoverText(PlayerColor player) const
  492. {
  493. std::string hoverName = getObjectName(); // Sawmill
  494. if (tempOwner != PlayerColor::NEUTRAL)
  495. {
  496. hoverName += "\n";
  497. hoverName += VLC->generaltexth->arraytxt[23 + tempOwner.getNum()]; // owned by Red Player
  498. hoverName += "\n(" + VLC->generaltexth->restypes[producedResource] + ")";
  499. }
  500. for (auto & slot : Slots()) // guarded by a few Pikeman
  501. {
  502. hoverName += "\n";
  503. hoverName += getRoughAmount(slot.first);
  504. hoverName += getCreature(slot.first)->namePl;
  505. }
  506. return hoverName;
  507. }
  508. void CGMine::flagMine(PlayerColor player) const
  509. {
  510. assert(tempOwner != player);
  511. cb->setOwner(this, player); //not ours? flag it!
  512. InfoWindow iw;
  513. iw.soundID = soundBase::FLAGMINE;
  514. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  515. iw.player = player;
  516. iw.components.push_back(Component(Component::RESOURCE,producedResource,producedQuantity,-1));
  517. cb->showInfoDialog(&iw);
  518. }
  519. ui32 CGMine::defaultResProduction()
  520. {
  521. switch(producedResource)
  522. {
  523. case Res::WOOD:
  524. case Res::ORE:
  525. return 2;
  526. case Res::GOLD:
  527. return 1000;
  528. default:
  529. return 1;
  530. }
  531. }
  532. void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  533. {
  534. if(result.winner == 0) //attacker won
  535. {
  536. if(subID == 7)
  537. {
  538. showInfoDialog(hero->tempOwner, 85, 0);
  539. }
  540. flagMine(hero->tempOwner);
  541. }
  542. }
  543. void CGMine::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  544. {
  545. if(answer)
  546. cb->startBattleI(hero, this);
  547. }
  548. std::string CGResource::getHoverText(PlayerColor player) const
  549. {
  550. return VLC->generaltexth->restypes[subID];
  551. }
  552. CGResource::CGResource()
  553. {
  554. amount = 0;
  555. }
  556. void CGResource::initObj()
  557. {
  558. blockVisit = true;
  559. if(!amount)
  560. {
  561. switch(subID)
  562. {
  563. case 6:
  564. amount = cb->gameState()->getRandomGenerator().nextInt(500, 1000);
  565. break;
  566. case 0: case 2:
  567. amount = cb->gameState()->getRandomGenerator().nextInt(6, 10);
  568. break;
  569. default:
  570. amount = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  571. break;
  572. }
  573. }
  574. }
  575. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  576. {
  577. if(stacksCount())
  578. {
  579. if(message.size())
  580. {
  581. BlockingDialog ynd(true,false);
  582. ynd.player = h->getOwner();
  583. ynd.text << message;
  584. cb->showBlockingDialog(&ynd);
  585. }
  586. else
  587. {
  588. blockingDialogAnswered(h, true); //behave as if player accepted battle
  589. }
  590. }
  591. else
  592. {
  593. if(message.length())
  594. {
  595. InfoWindow iw;
  596. iw.player = h->tempOwner;
  597. iw.text << message;
  598. cb->showInfoDialog(&iw);
  599. }
  600. collectRes(h->getOwner());
  601. }
  602. }
  603. void CGResource::collectRes( PlayerColor player ) const
  604. {
  605. cb->giveResource(player, static_cast<Res::ERes>(subID), amount);
  606. ShowInInfobox sii;
  607. sii.player = player;
  608. sii.c = Component(Component::RESOURCE,subID,amount,0);
  609. sii.text.addTxt(MetaString::ADVOB_TXT,113);
  610. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  611. cb->showCompInfo(&sii);
  612. cb->removeObject(this);
  613. }
  614. void CGResource::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  615. {
  616. if(result.winner == 0) //attacker won
  617. collectRes(hero->getOwner());
  618. }
  619. void CGResource::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  620. {
  621. if(answer)
  622. cb->startBattleI(hero, this);
  623. }
  624. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  625. {
  626. ObjectInstanceID destinationid;
  627. switch(ID)
  628. {
  629. case Obj::MONOLITH_ONE_WAY_ENTRANCE: //one way - find corresponding exit monolith
  630. {
  631. if(vstd::contains(objs,Obj::MONOLITH_ONE_WAY_EXIT) && vstd::contains(objs[Obj::MONOLITH_ONE_WAY_EXIT],subID) && objs[Obj::MONOLITH_ONE_WAY_EXIT][subID].size())
  632. {
  633. destinationid = *RandomGeneratorUtil::nextItem(objs[Obj::MONOLITH_ONE_WAY_EXIT][subID], cb->gameState()->getRandomGenerator());
  634. }
  635. else
  636. {
  637. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  638. }
  639. break;
  640. }
  641. case Obj::MONOLITH_TWO_WAY://two way monolith - pick any other one
  642. case Obj::WHIRLPOOL: //Whirlpool
  643. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  644. {
  645. //choose another exit
  646. do
  647. {
  648. destinationid = *RandomGeneratorUtil::nextItem(objs[ID][subID], cb->gameState()->getRandomGenerator());
  649. } while(destinationid == id);
  650. if (ID == Obj::WHIRLPOOL)
  651. {
  652. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  653. {
  654. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  655. { //we can't remove last unit
  656. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  657. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  658. {
  659. if (h->getPower(targetstack) > h->getPower(i->first))
  660. {
  661. targetstack = (i->first);
  662. }
  663. }
  664. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  665. vstd::amax(countToTake, 1);
  666. InfoWindow iw;
  667. iw.player = h->tempOwner;
  668. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  669. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  670. cb->showInfoDialog(&iw);
  671. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  672. }
  673. }
  674. }
  675. }
  676. else
  677. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  678. break;
  679. case Obj::SUBTERRANEAN_GATE: //find nearest subterranean gate on the other level
  680. {
  681. destinationid = getMatchingGate(id);
  682. if(destinationid == ObjectInstanceID()) //no exit
  683. {
  684. showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  685. }
  686. break;
  687. }
  688. }
  689. if(destinationid == ObjectInstanceID())
  690. {
  691. logGlobal->warnStream() << "Cannot find exit... (obj at " << pos << ") :( ";
  692. return;
  693. }
  694. if (ID == Obj::WHIRLPOOL)
  695. {
  696. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  697. auto & tile = *RandomGeneratorUtil::nextItem(tiles, cb->gameState()->getRandomGenerator());
  698. cb->moveHero(h->id, tile + int3(1,0,0), true);
  699. }
  700. else
  701. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  702. }
  703. void CGTeleport::initObj()
  704. {
  705. int si = subID;
  706. switch (ID)
  707. {
  708. case Obj::SUBTERRANEAN_GATE://ignore subterranean gates subid
  709. case Obj::WHIRLPOOL:
  710. {
  711. si = 0;
  712. break;
  713. }
  714. default:
  715. break;
  716. }
  717. objs[ID][si].push_back(id);
  718. }
  719. void CGTeleport::postInit() //matches subterranean gates into pairs
  720. {
  721. //split on underground and surface gates
  722. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  723. for(auto & elem : objs[Obj::SUBTERRANEAN_GATE][0])
  724. {
  725. const CGObjectInstance *hlp = cb->getObj(elem);
  726. gatesSplit[hlp->pos.z].push_back(hlp);
  727. }
  728. //sort by position
  729. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  730. {
  731. return a->pos < b->pos;
  732. });
  733. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  734. {
  735. const CGObjectInstance *cur = gatesSplit[0][i];
  736. //find nearest underground exit
  737. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  738. for(int j = 0; j < gatesSplit[1].size(); j++)
  739. {
  740. const CGObjectInstance *checked = gatesSplit[1][j];
  741. if(!checked)
  742. continue;
  743. si32 hlp = checked->pos.dist2dSQ(cur->pos);
  744. if(hlp < best.second)
  745. {
  746. best.first = j;
  747. best.second = hlp;
  748. }
  749. }
  750. if(best.first >= 0) //found pair
  751. {
  752. gates.push_back(std::make_pair(cur->id, gatesSplit[1][best.first]->id));
  753. gatesSplit[1][best.first] = nullptr;
  754. }
  755. else
  756. gates.push_back(std::make_pair(cur->id, ObjectInstanceID()));
  757. }
  758. objs.erase(Obj::SUBTERRANEAN_GATE);
  759. }
  760. ObjectInstanceID CGTeleport::getMatchingGate(ObjectInstanceID id)
  761. {
  762. for(auto & gate : gates)
  763. {
  764. if(gate.first == id)
  765. return gate.second;
  766. if(gate.second == id)
  767. return gate.first;
  768. }
  769. return ObjectInstanceID();
  770. }
  771. void CGArtifact::initObj()
  772. {
  773. blockVisit = true;
  774. if(ID == Obj::ARTIFACT)
  775. {
  776. if (!storedArtifact)
  777. {
  778. auto a = new CArtifactInstance();
  779. cb->gameState()->map->addNewArtifactInstance(a);
  780. storedArtifact = a;
  781. }
  782. if(!storedArtifact->artType)
  783. storedArtifact->setType(VLC->arth->artifacts[subID]);
  784. }
  785. if(ID == Obj::SPELL_SCROLL)
  786. subID = 1;
  787. assert(storedArtifact->artType);
  788. assert(storedArtifact->getParentNodes().size());
  789. //assert(storedArtifact->artType->id == subID); //this does not stop desync
  790. }
  791. std::string CGArtifact::getObjectName() const
  792. {
  793. return VLC->arth->artifacts[subID]->Name();
  794. }
  795. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  796. {
  797. if(!stacksCount())
  798. {
  799. InfoWindow iw;
  800. iw.player = h->tempOwner;
  801. switch(ID)
  802. {
  803. case Obj::ARTIFACT:
  804. {
  805. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  806. iw.components.push_back(Component(Component::ARTIFACT,subID,0,0));
  807. if(message.length())
  808. iw.text << message;
  809. else
  810. {
  811. if (VLC->arth->artifacts[subID]->EventText().size())
  812. iw.text << std::pair<ui8, ui32> (MetaString::ART_EVNTS, subID);
  813. else //fix for mod artifacts with no event text
  814. {
  815. iw.text.addTxt (MetaString::ADVOB_TXT, 183); //% has found treasure
  816. iw.text.addReplacement (h->name);
  817. }
  818. }
  819. }
  820. break;
  821. case Obj::SPELL_SCROLL:
  822. {
  823. int spellID = storedArtifact->getGivenSpellID();
  824. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  825. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  826. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  827. }
  828. break;
  829. }
  830. cb->showInfoDialog(&iw);
  831. pick(h);
  832. }
  833. else
  834. {
  835. if(message.size())
  836. {
  837. BlockingDialog ynd(true,false);
  838. ynd.player = h->getOwner();
  839. ynd.text << message;
  840. cb->showBlockingDialog(&ynd);
  841. }
  842. else
  843. {
  844. blockingDialogAnswered(h, true);
  845. }
  846. }
  847. }
  848. void CGArtifact::pick(const CGHeroInstance * h) const
  849. {
  850. cb->giveHeroArtifact(h, storedArtifact, ArtifactPosition::FIRST_AVAILABLE);
  851. cb->removeObject(this);
  852. }
  853. void CGArtifact::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  854. {
  855. if(result.winner == 0) //attacker won
  856. pick(hero);
  857. }
  858. void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  859. {
  860. if(answer)
  861. cb->startBattleI(hero, this);
  862. }
  863. void CGWitchHut::initObj()
  864. {
  865. if (allowedAbilities.empty()) //this can happen for RMG. regular maps load abilities from map file
  866. {
  867. for (int i = 0; i < GameConstants::SKILL_QUANTITY; i++)
  868. allowedAbilities.push_back(i);
  869. }
  870. ability = *RandomGeneratorUtil::nextItem(allowedAbilities, cb->gameState()->getRandomGenerator());
  871. }
  872. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  873. {
  874. InfoWindow iw;
  875. iw.soundID = soundBase::gazebo;
  876. iw.player = h->getOwner();
  877. if(!wasVisited(h->tempOwner))
  878. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  879. ui32 txt_id;
  880. if(h->getSecSkillLevel(SecondarySkill(ability))) //you already know this skill
  881. {
  882. txt_id =172;
  883. }
  884. else if(!h->canLearnSkill()) //already all skills slots used
  885. {
  886. txt_id = 173;
  887. }
  888. else //give sec skill
  889. {
  890. iw.components.push_back(Component(Component::SEC_SKILL, ability, 1, 0));
  891. txt_id = 171;
  892. cb->changeSecSkill(h, SecondarySkill(ability), 1, true);
  893. }
  894. iw.text.addTxt(MetaString::ADVOB_TXT,txt_id);
  895. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  896. cb->showInfoDialog(&iw);
  897. }
  898. std::string CGWitchHut::getHoverText(PlayerColor player) const
  899. {
  900. std::string hoverName = getObjectName();
  901. if(wasVisited(player))
  902. {
  903. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  904. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  905. }
  906. return hoverName;
  907. }
  908. std::string CGWitchHut::getHoverText(const CGHeroInstance * hero) const
  909. {
  910. std::string hoverName = getHoverText(hero->tempOwner);
  911. if(hero->getSecSkillLevel(SecondarySkill(ability))) //hero knows that ability
  912. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  913. return hoverName;
  914. }
  915. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  916. {
  917. int message;
  918. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  919. {
  920. message = 78;//"A second drink at the well in one day will not help you."
  921. }
  922. else if(h->mana < h->manaLimit())
  923. {
  924. giveDummyBonus(h->id);
  925. cb->setManaPoints(h->id,h->manaLimit());
  926. message = 77;
  927. }
  928. else
  929. {
  930. message = 79;
  931. }
  932. showInfoDialog(h,message,soundBase::faerie);
  933. }
  934. std::string CGMagicWell::getHoverText(const CGHeroInstance * hero) const
  935. {
  936. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  937. }
  938. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  939. {
  940. InfoWindow iw;
  941. iw.player = h->tempOwner;
  942. switch (ID)
  943. {
  944. case Obj::REDWOOD_OBSERVATORY:
  945. case Obj::PILLAR_OF_FIRE:
  946. {
  947. iw.soundID = soundBase::LIGHTHOUSE;
  948. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==Obj::PILLAR_OF_FIRE));
  949. FoWChange fw;
  950. fw.player = h->tempOwner;
  951. fw.mode = 1;
  952. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  953. cb->sendAndApply (&fw);
  954. break;
  955. }
  956. case Obj::COVER_OF_DARKNESS:
  957. {
  958. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  959. for (auto & player : cb->gameState()->players)
  960. {
  961. if (cb->getPlayerStatus(player.first) == EPlayerStatus::INGAME &&
  962. cb->getPlayerRelations(player.first, h->tempOwner) == PlayerRelations::ENEMIES)
  963. cb->changeFogOfWar(visitablePos(), 20, player.first, true);
  964. }
  965. break;
  966. }
  967. }
  968. cb->showInfoDialog(&iw);
  969. }
  970. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  971. {
  972. if(spell == SpellID::NONE)
  973. {
  974. logGlobal->errorStream() << "Not initialized shrine visited!";
  975. return;
  976. }
  977. if(!wasVisited(h->tempOwner))
  978. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  979. InfoWindow iw;
  980. iw.soundID = soundBase::temple;
  981. iw.player = h->getOwner();
  982. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  983. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  984. iw.text << ".";
  985. if(!h->getArt(ArtifactPosition::SPELLBOOK))
  986. {
  987. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  988. }
  989. else if(ID == Obj::SHRINE_OF_MAGIC_THOUGHT && !h->getSecSkillLevel(SecondarySkill::WISDOM)) //it's third level spell and hero doesn't have wisdom
  990. {
  991. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  992. }
  993. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  994. {
  995. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  996. }
  997. else //give spell
  998. {
  999. std::set<SpellID> spells;
  1000. spells.insert(spell);
  1001. cb->changeSpells(h, true, spells);
  1002. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1003. }
  1004. cb->showInfoDialog(&iw);
  1005. }
  1006. void CGShrine::initObj()
  1007. {
  1008. if(spell == SpellID::NONE) //spell not set
  1009. {
  1010. int level = ID-87;
  1011. std::vector<SpellID> possibilities;
  1012. cb->getAllowedSpells (possibilities, level);
  1013. if(possibilities.empty())
  1014. {
  1015. logGlobal->errorStream() << "Error: cannot init shrine, no allowed spells!";
  1016. return;
  1017. }
  1018. spell = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1019. }
  1020. }
  1021. std::string CGShrine::getHoverText(PlayerColor player) const
  1022. {
  1023. std::string hoverName = getObjectName();
  1024. if(wasVisited(player))
  1025. {
  1026. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1027. boost::algorithm::replace_first(hoverName,"%s", spell.toSpell()->name);
  1028. }
  1029. return hoverName;
  1030. }
  1031. std::string CGShrine::getHoverText(const CGHeroInstance * hero) const
  1032. {
  1033. std::string hoverName = getHoverText(hero->tempOwner);
  1034. if(vstd::contains(hero->spells, spell)) //hero knows that spell
  1035. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1036. return hoverName;
  1037. }
  1038. void CGSignBottle::initObj()
  1039. {
  1040. //if no text is set than we pick random from the predefined ones
  1041. if(message.empty())
  1042. {
  1043. message = *RandomGeneratorUtil::nextItem(VLC->generaltexth->randsign, cb->gameState()->getRandomGenerator());
  1044. }
  1045. if(ID == Obj::OCEAN_BOTTLE)
  1046. {
  1047. blockVisit = true;
  1048. }
  1049. }
  1050. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  1051. {
  1052. InfoWindow iw;
  1053. iw.soundID = soundBase::STORE;
  1054. iw.player = h->getOwner();
  1055. iw.text << message;
  1056. cb->showInfoDialog(&iw);
  1057. if(ID == Obj::OCEAN_BOTTLE)
  1058. cb->removeObject(this);
  1059. }
  1060. //TODO: remove
  1061. //void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  1062. //{
  1063. //
  1064. //}
  1065. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  1066. {
  1067. EBonusType type = bonusType;
  1068. int bid = bonusID;
  1069. //check if the bonus if applicable, if not - give primary skill (always possible)
  1070. int ssl = h->getSecSkillLevel(SecondarySkill(bid)); //current sec skill level, used if bonusType == 1
  1071. if((type == SECONDARY_SKILL
  1072. && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  1073. || (type == SPELL && (!h->getArt(ArtifactPosition::SPELLBOOK) || vstd::contains(h->spells, (ui32) bid)
  1074. || ( SpellID(bid).toSpell()->level > h->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  1075. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  1076. {
  1077. type = PRIM_SKILL;
  1078. bid = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS - 1);
  1079. }
  1080. InfoWindow iw;
  1081. iw.soundID = soundBase::gazebo;
  1082. iw.player = h->getOwner();
  1083. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  1084. switch (type)
  1085. {
  1086. case PRIM_SKILL:
  1087. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(bid),+1);
  1088. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  1089. break;
  1090. case SECONDARY_SKILL:
  1091. cb->changeSecSkill(h,SecondarySkill(bid),+1);
  1092. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  1093. break;
  1094. case SPELL:
  1095. {
  1096. std::set<SpellID> hlp;
  1097. hlp.insert(SpellID(bid));
  1098. cb->changeSpells(h,true,hlp);
  1099. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  1100. }
  1101. break;
  1102. default:
  1103. logGlobal->errorStream() << "Error: wrong bonus type (" << (int)type << ") for Scholar!\n";
  1104. return;
  1105. }
  1106. cb->showInfoDialog(&iw);
  1107. cb->removeObject(this);
  1108. }
  1109. void CGScholar::initObj()
  1110. {
  1111. blockVisit = true;
  1112. if(bonusType == RANDOM)
  1113. {
  1114. bonusType = static_cast<EBonusType>(cb->gameState()->getRandomGenerator().nextInt(2));
  1115. switch(bonusType)
  1116. {
  1117. case PRIM_SKILL:
  1118. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS -1);
  1119. break;
  1120. case SECONDARY_SKILL:
  1121. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::SKILL_QUANTITY -1);
  1122. break;
  1123. case SPELL:
  1124. std::vector<SpellID> possibilities;
  1125. for (int i = 1; i < 6; ++i)
  1126. cb->getAllowedSpells (possibilities, i);
  1127. bonusID = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1128. break;
  1129. }
  1130. }
  1131. }
  1132. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  1133. {
  1134. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  1135. if (!ally && stacksCount() > 0) {
  1136. //TODO: Find a way to apply magic garrison effects in battle.
  1137. cb->startBattleI(h, this);
  1138. return;
  1139. }
  1140. //New owner.
  1141. if (!ally)
  1142. cb->setOwner(this, h->tempOwner);
  1143. cb->showGarrisonDialog(id, h->id, removableUnits);
  1144. }
  1145. bool CGGarrison::passableFor(PlayerColor player) const
  1146. {
  1147. //FIXME: identical to same method in CGTownInstance
  1148. if ( !stacksCount() )//empty - anyone can visit
  1149. return true;
  1150. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  1151. return false;
  1152. if (cb->getPlayerRelations(tempOwner, player) != PlayerRelations::ENEMIES)
  1153. return true;
  1154. return false;
  1155. }
  1156. void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  1157. {
  1158. if (result.winner == 0)
  1159. onHeroVisit(hero);
  1160. }
  1161. void CGMagi::initObj()
  1162. {
  1163. if (ID == Obj::EYE_OF_MAGI)
  1164. {
  1165. blockVisit = true;
  1166. eyelist[subID].push_back(id);
  1167. }
  1168. }
  1169. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  1170. {
  1171. if (ID == Obj::HUT_OF_MAGI)
  1172. {
  1173. showInfoDialog(h, 61, soundBase::LIGHTHOUSE);
  1174. if (!eyelist[subID].empty())
  1175. {
  1176. CenterView cv;
  1177. cv.player = h->tempOwner;
  1178. cv.focusTime = 2000;
  1179. FoWChange fw;
  1180. fw.player = h->tempOwner;
  1181. fw.mode = 1;
  1182. fw.waitForDialogs = true;
  1183. for(auto it : eyelist[subID])
  1184. {
  1185. const CGObjectInstance *eye = cb->getObj(it);
  1186. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  1187. cb->sendAndApply(&fw);
  1188. cv.pos = eye->pos;
  1189. cb->sendAndApply(&cv);
  1190. }
  1191. cv.pos = h->getPosition(false);
  1192. cb->sendAndApply(&cv);
  1193. }
  1194. }
  1195. else if (ID == Obj::EYE_OF_MAGI)
  1196. {
  1197. showInfoDialog(h,48,soundBase::invalid);
  1198. }
  1199. }
  1200. void CGBoat::initObj()
  1201. {
  1202. hero = nullptr;
  1203. }
  1204. void CGSirens::initObj()
  1205. {
  1206. blockVisit = true;
  1207. }
  1208. std::string CGSirens::getHoverText(const CGHeroInstance * hero) const
  1209. {
  1210. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  1211. }
  1212. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  1213. {
  1214. InfoWindow iw;
  1215. iw.soundID = soundBase::DANGER;
  1216. iw.player = h->tempOwner;
  1217. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  1218. {
  1219. iw.text.addTxt(MetaString::ADVOB_TXT,133);
  1220. }
  1221. else
  1222. {
  1223. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  1224. TExpType xp = 0;
  1225. for (auto i = h->Slots().begin(); i != h->Slots().end(); i++)
  1226. {
  1227. TQuantity drown = i->second->count * 0.3;
  1228. if(drown)
  1229. {
  1230. cb->changeStackCount(StackLocation(h, i->first), -drown);
  1231. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  1232. }
  1233. }
  1234. if(xp)
  1235. {
  1236. xp = h->calculateXp(xp);
  1237. iw.text.addTxt(MetaString::ADVOB_TXT,132);
  1238. iw.text.addReplacement(xp);
  1239. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, xp, false);
  1240. }
  1241. else
  1242. {
  1243. iw.text.addTxt(MetaString::ADVOB_TXT,134);
  1244. }
  1245. }
  1246. cb->showInfoDialog(&iw);
  1247. }
  1248. CGShipyard::CGShipyard()
  1249. :IShipyard(this)
  1250. {
  1251. }
  1252. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  1253. {
  1254. // H J L K I
  1255. // A x S x B
  1256. // C E G F D
  1257. offsets = {
  1258. int3(-3,0,0), int3(1,0,0), //AB
  1259. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  1260. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0) //HIJKL
  1261. };
  1262. }
  1263. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  1264. {
  1265. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  1266. cb->setOwner(this, h->tempOwner);
  1267. auto s = shipyardStatus();
  1268. if(s != IBoatGenerator::GOOD)
  1269. {
  1270. InfoWindow iw;
  1271. iw.player = tempOwner;
  1272. getProblemText(iw.text, h);
  1273. cb->showInfoDialog(&iw);
  1274. }
  1275. else
  1276. {
  1277. openWindow(OpenWindow::SHIPYARD_WINDOW,id.getNum(),h->id.getNum());
  1278. }
  1279. }
  1280. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  1281. {
  1282. //if player has not bought map of this subtype yet and underground exist for stalagmite cartographer
  1283. if (!wasVisited(h->getOwner()) && (subID != 2 || cb->gameState()->map->twoLevel))
  1284. {
  1285. if (cb->getResource(h->tempOwner, Res::GOLD) >= 1000) //if he can afford a map
  1286. {
  1287. //ask if he wants to buy one
  1288. int text=0;
  1289. switch (subID)
  1290. {
  1291. case 0:
  1292. text = 25;
  1293. break;
  1294. case 1:
  1295. text = 26;
  1296. break;
  1297. case 2:
  1298. text = 27;
  1299. break;
  1300. default:
  1301. logGlobal->warnStream() << "Unrecognized subtype of cartographer";
  1302. }
  1303. assert(text);
  1304. BlockingDialog bd (true, false);
  1305. bd.player = h->getOwner();
  1306. bd.soundID = soundBase::LIGHTHOUSE;
  1307. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  1308. cb->showBlockingDialog (&bd);
  1309. }
  1310. else //if he cannot afford
  1311. {
  1312. showInfoDialog(h,28,soundBase::CAVEHEAD);
  1313. }
  1314. }
  1315. else //if he already visited carographer
  1316. {
  1317. showInfoDialog(h,24,soundBase::CAVEHEAD);
  1318. }
  1319. }
  1320. void CCartographer::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1321. {
  1322. if (answer) //if hero wants to buy map
  1323. {
  1324. cb->giveResource (hero->tempOwner, Res::GOLD, -1000);
  1325. FoWChange fw;
  1326. fw.mode = 1;
  1327. fw.player = hero->tempOwner;
  1328. //subIDs of different types of cartographers:
  1329. //water = 0; land = 1; underground = 2;
  1330. cb->getAllTiles (fw.tiles, hero->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  1331. cb->sendAndApply (&fw);
  1332. cb->setObjProperty (id, 10, hero->tempOwner.getNum());
  1333. }
  1334. }
  1335. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  1336. {
  1337. cb->showThievesGuildWindow(h->tempOwner, id);
  1338. }
  1339. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  1340. {
  1341. InfoWindow iw;
  1342. iw.player = h->tempOwner;
  1343. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  1344. assert(ts);
  1345. TeamID team = ts->id;
  1346. if(!wasVisited(team))
  1347. {
  1348. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  1349. cb->sendAndApply(&iw);
  1350. cb->setObjProperty(id, 20, h->tempOwner.getNum()); //increment general visited obelisks counter
  1351. openWindow(OpenWindow::PUZZLE_MAP, h->tempOwner.getNum());
  1352. cb->setObjProperty(id, 10, h->tempOwner.getNum()); //mark that particular obelisk as visited
  1353. }
  1354. else
  1355. {
  1356. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  1357. cb->sendAndApply(&iw);
  1358. }
  1359. }
  1360. void CGObelisk::initObj()
  1361. {
  1362. obeliskCount++;
  1363. }
  1364. std::string CGObelisk::getHoverText(PlayerColor player) const
  1365. {
  1366. return getObjectName() + " " + visitedTxt(wasVisited(player));
  1367. }
  1368. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  1369. {
  1370. CPlayersVisited::setPropertyDer(what, val);
  1371. switch(what)
  1372. {
  1373. case 20:
  1374. assert(val < PlayerColor::PLAYER_LIMIT_I);
  1375. visited[TeamID(val)]++;
  1376. if(visited[TeamID(val)] > obeliskCount)
  1377. {
  1378. logGlobal->errorStream() << "Error: Visited " << visited[TeamID(val)] << "\t\t" << obeliskCount;
  1379. assert(0);
  1380. }
  1381. break;
  1382. }
  1383. }
  1384. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  1385. {
  1386. if(h->tempOwner != tempOwner)
  1387. {
  1388. PlayerColor oldOwner = tempOwner;
  1389. cb->setOwner(this,h->tempOwner); //not ours? flag it!
  1390. showInfoDialog(h,69,soundBase::LIGHTHOUSE);
  1391. giveBonusTo(h->tempOwner);
  1392. if(oldOwner < PlayerColor::PLAYER_LIMIT) //remove bonus from old owner
  1393. {
  1394. RemoveBonus rb(RemoveBonus::PLAYER);
  1395. rb.whoID = oldOwner.getNum();
  1396. rb.source = Bonus::OBJECT;
  1397. rb.id = id.getNum();
  1398. cb->sendAndApply(&rb);
  1399. }
  1400. }
  1401. }
  1402. void CGLighthouse::initObj()
  1403. {
  1404. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  1405. {
  1406. giveBonusTo(tempOwner);
  1407. }
  1408. }
  1409. std::string CGLighthouse::getHoverText(PlayerColor player) const
  1410. {
  1411. //TODO: owned by %s player
  1412. return getObjectName();
  1413. }
  1414. void CGLighthouse::giveBonusTo( PlayerColor player ) const
  1415. {
  1416. GiveBonus gb(GiveBonus::PLAYER);
  1417. gb.bonus.type = Bonus::SEA_MOVEMENT;
  1418. gb.bonus.val = 500;
  1419. gb.id = player.getNum();
  1420. gb.bonus.duration = Bonus::PERMANENT;
  1421. gb.bonus.source = Bonus::OBJECT;
  1422. gb.bonus.sid = id.getNum();
  1423. cb->sendAndApply(&gb);
  1424. }