MiscObjects.cpp 41 KB

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