2
0

MiscObjects.cpp 49 KB

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