MiscObjects.cpp 49 KB

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