MiscObjects.cpp 47 KB

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