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