MiscObjects.cpp 40 KB

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