MiscObjects.cpp 40 KB

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