MiscObjects.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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. std::map <si32, std::vector<ObjectInstanceID> > CGMagi::eyelist;
  22. ui8 CGObelisk::obeliskCount; //how many obelisks are on map
  23. std::map<TeamID, ui8> CGObelisk::visited; //map: team_id => how many obelisks has been visited
  24. ///helpers
  25. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  26. {
  27. OpenWindow ow;
  28. ow.window = type;
  29. ow.id1 = id1;
  30. ow.id2 = id2;
  31. IObjectInterface::cb->sendAndApply(&ow);
  32. }
  33. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  34. {
  35. InfoWindow iw;
  36. iw.soundID = soundID;
  37. iw.player = playerID;
  38. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  39. IObjectInterface::cb->sendAndApply(&iw);
  40. }
  41. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  42. {
  43. const PlayerColor playerID = h->getOwner();
  44. showInfoDialog(playerID,txtID,soundID);
  45. }
  46. static std::string & visitedTxt(const bool visited)
  47. {
  48. int id = visited ? 352 : 353;
  49. return VLC->generaltexth->allTexts[id];
  50. }
  51. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  52. {
  53. if(what == 10)
  54. players.insert(PlayerColor(val));
  55. }
  56. bool CPlayersVisited::wasVisited( PlayerColor player ) const
  57. {
  58. return vstd::contains(players,player);
  59. }
  60. bool CPlayersVisited::wasVisited( TeamID team ) const
  61. {
  62. for(auto i : players)
  63. {
  64. if(cb->getPlayer(i)->team == team)
  65. return true;
  66. }
  67. return false;
  68. }
  69. std::string CGCreature::getHoverText(PlayerColor player) const
  70. {
  71. if(stacks.empty())
  72. {
  73. //should not happen...
  74. logGlobal->errorStream() << "Invalid stack at tile " << pos << ": subID=" << subID << "; id=" << id;
  75. return "!!!INVALID_STACK!!!";
  76. }
  77. std::string hoverName;
  78. MetaString ms;
  79. int pom = stacks.begin()->second->getQuantityID();
  80. pom = 172 + 3*pom;
  81. ms.addTxt(MetaString::ARRAY_TXT,pom);
  82. ms << " " ;
  83. ms.addTxt(MetaString::CRE_PL_NAMES,subID);
  84. ms.toString(hoverName);
  85. return hoverName;
  86. }
  87. std::string CGCreature::getHoverText(const CGHeroInstance * hero) const
  88. {
  89. std::string hoverName;
  90. if(hero->hasVisions(this, 0))
  91. {
  92. MetaString ms;
  93. ms << stacks.begin()->second->count;
  94. ms << " " ;
  95. ms.addTxt(MetaString::CRE_PL_NAMES,subID);
  96. ms << "\n";
  97. int decision = takenAction(hero, true);
  98. switch (decision)
  99. {
  100. case FIGHT:
  101. ms.addTxt(MetaString::GENERAL_TXT,246);
  102. break;
  103. case FLEE:
  104. ms.addTxt(MetaString::GENERAL_TXT,245);
  105. break;
  106. case JOIN_FOR_FREE:
  107. ms.addTxt(MetaString::GENERAL_TXT,243);
  108. break;
  109. default: //decision = cost in gold
  110. VLC->generaltexth->allTexts[244];
  111. ms << boost::to_string(boost::format(VLC->generaltexth->allTexts[244]) % decision);
  112. break;
  113. }
  114. ms.toString(hoverName);
  115. }
  116. else
  117. {
  118. hoverName = getHoverText(hero->tempOwner);
  119. }
  120. const JsonNode & texts = VLC->generaltexth->localizedTexts["adventureMap"]["monsterThreat"];
  121. hoverName += texts["title"].String();
  122. int choice;
  123. double ratio = ((double)getArmyStrength() / hero->getTotalStrength());
  124. if (ratio < 0.1) choice = 0;
  125. else if (ratio < 0.25) choice = 1;
  126. else if (ratio < 0.6) choice = 2;
  127. else if (ratio < 0.9) choice = 3;
  128. else if (ratio < 1.1) choice = 4;
  129. else if (ratio < 1.3) choice = 5;
  130. else if (ratio < 1.8) choice = 6;
  131. else if (ratio < 2.5) choice = 7;
  132. else if (ratio < 4) choice = 8;
  133. else if (ratio < 8) choice = 9;
  134. else if (ratio < 20) choice = 10;
  135. else choice = 11;
  136. hoverName += texts["levels"].Vector()[choice].String();
  137. return hoverName;
  138. }
  139. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  140. {
  141. int action = takenAction(h);
  142. switch( action ) //decide what we do...
  143. {
  144. case FIGHT:
  145. fight(h);
  146. break;
  147. case FLEE:
  148. {
  149. flee(h);
  150. break;
  151. }
  152. case JOIN_FOR_FREE: //join for free
  153. {
  154. BlockingDialog ynd(true,false);
  155. ynd.player = h->tempOwner;
  156. ynd.text.addTxt(MetaString::ADVOB_TXT, 86);
  157. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  158. cb->showBlockingDialog(&ynd);
  159. break;
  160. }
  161. default: //join for gold
  162. {
  163. assert(action > 0);
  164. //ask if player agrees to pay gold
  165. BlockingDialog ynd(true,false);
  166. ynd.player = h->tempOwner;
  167. std::string tmp = VLC->generaltexth->advobtxt[90];
  168. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(getStackCount(SlotID(0))));
  169. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  170. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  171. ynd.text << tmp;
  172. cb->showBlockingDialog(&ynd);
  173. break;
  174. }
  175. }
  176. }
  177. void CGCreature::initObj()
  178. {
  179. blockVisit = true;
  180. switch(character)
  181. {
  182. case 0:
  183. character = -4;
  184. break;
  185. case 1:
  186. character = cb->gameState()->getRandomGenerator().nextInt(1, 7);
  187. break;
  188. case 2:
  189. character = cb->gameState()->getRandomGenerator().nextInt(1, 10);
  190. break;
  191. case 3:
  192. character = cb->gameState()->getRandomGenerator().nextInt(4, 10);
  193. break;
  194. case 4:
  195. character = 10;
  196. break;
  197. }
  198. stacks[SlotID(0)]->setType(CreatureID(subID));
  199. TQuantity &amount = stacks[SlotID(0)]->count;
  200. CCreature &c = *VLC->creh->creatures[subID];
  201. if(amount == 0)
  202. {
  203. amount = cb->gameState()->getRandomGenerator().nextInt(c.ammMin, c.ammMax);
  204. if(amount == 0) //armies with 0 creatures are illegal
  205. {
  206. logGlobal->warnStream() << "Problem: stack " << nodeName() << " cannot have 0 creatures. Check properties of " << c.nodeName();
  207. amount = 1;
  208. }
  209. }
  210. temppower = stacks[SlotID(0)]->count * 1000;
  211. refusedJoining = false;
  212. }
  213. void CGCreature::newTurn() const
  214. {//Works only for stacks of single type of size up to 2 millions
  215. if (!notGrowingTeam)
  216. {
  217. if (stacks.begin()->second->count < VLC->modh->settings.CREEP_SIZE && cb->getDate(Date::DAY_OF_WEEK) == 1 && cb->getDate(Date::DAY) > 1)
  218. {
  219. ui32 power = temppower * (100 + VLC->modh->settings.WEEKLY_GROWTH) / 100;
  220. cb->setObjProperty(id, ObjProperty::MONSTER_COUNT, std::min(power / 1000, (ui32)VLC->modh->settings.CREEP_SIZE)); //set new amount
  221. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  222. }
  223. }
  224. if (VLC->modh->modules.STACK_EXP)
  225. cb->setObjProperty(id, ObjProperty::MONSTER_EXP, VLC->modh->settings.NEUTRAL_STACK_EXP); //for testing purpose
  226. }
  227. void CGCreature::setPropertyDer(ui8 what, ui32 val)
  228. {
  229. switch (what)
  230. {
  231. case ObjProperty::MONSTER_COUNT:
  232. stacks[SlotID(0)]->count = val;
  233. break;
  234. case ObjProperty::MONSTER_POWER:
  235. temppower = val;
  236. break;
  237. case ObjProperty::MONSTER_EXP:
  238. giveStackExp(val);
  239. break;
  240. case ObjProperty::MONSTER_RESTORE_TYPE:
  241. formation.basicType = val;
  242. break;
  243. case ObjProperty::MONSTER_REFUSED_JOIN:
  244. refusedJoining = val;
  245. break;
  246. }
  247. }
  248. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  249. {
  250. //calculate relative strength of hero and creatures armies
  251. double relStrength = double(h->getTotalStrength()) / getArmyStrength();
  252. int powerFactor;
  253. if(relStrength >= 7)
  254. powerFactor = 11;
  255. else if(relStrength >= 1)
  256. powerFactor = (int)(2*(relStrength-1));
  257. else if(relStrength >= 0.5)
  258. powerFactor = -1;
  259. else if(relStrength >= 0.333)
  260. powerFactor = -2;
  261. else
  262. powerFactor = -3;
  263. std::set<CreatureID> myKindCres; //what creatures are the same kind as we
  264. const CCreature * myCreature = VLC->creh->creatures[subID];
  265. myKindCres.insert(myCreature->idNumber); //we
  266. myKindCres.insert(myCreature->upgrades.begin(), myCreature->upgrades.end()); //our upgrades
  267. for(ConstTransitivePtr<CCreature> &crea : VLC->creh->creatures)
  268. {
  269. if(vstd::contains(crea->upgrades, myCreature->idNumber)) //it's our base creatures
  270. myKindCres.insert(crea->idNumber);
  271. }
  272. int count = 0, //how many creatures of similar kind has hero
  273. totalCount = 0;
  274. for (auto & elem : h->Slots())
  275. {
  276. if(vstd::contains(myKindCres,elem.second->type->idNumber))
  277. count += elem.second->count;
  278. totalCount += elem.second->count;
  279. }
  280. int sympathy = 0; // 0 if hero have no similar creatures
  281. if(count)
  282. sympathy++; // 1 - if hero have at least 1 similar creature
  283. if(count*2 > totalCount)
  284. sympathy++; // 2 - hero have similar creatures more that 50%
  285. int charisma = powerFactor + h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy;
  286. if(charisma < character)
  287. return FIGHT;
  288. if (allowJoin)
  289. {
  290. if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy + 1 >= character)
  291. return JOIN_FOR_FREE;
  292. else if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) * 2 + sympathy + 1 >= character)
  293. return VLC->creh->creatures[subID]->cost[6] * getStackCount(SlotID(0)); //join for gold
  294. }
  295. //we are still here - creatures have not joined hero, flee or fight
  296. if (charisma > character && !neverFlees)
  297. return FLEE;
  298. else
  299. return FIGHT;
  300. }
  301. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  302. {
  303. if(refusedJoining)
  304. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, false);
  305. if(pursue)
  306. {
  307. fight(h);
  308. }
  309. else
  310. {
  311. cb->removeObject(this);
  312. }
  313. }
  314. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  315. {
  316. if(!accept)
  317. {
  318. if(takenAction(h,false) == FLEE)
  319. {
  320. cb->setObjProperty(id, ObjProperty::MONSTER_REFUSED_JOIN, true);
  321. flee(h);
  322. }
  323. else //they fight
  324. {
  325. showInfoDialog(h,87,0);//Insulted by your refusal of their offer, the monsters attack!
  326. fight(h);
  327. }
  328. }
  329. else //accepted
  330. {
  331. if (cb->getResource(h->tempOwner, Res::GOLD) < cost) //player don't have enough gold!
  332. {
  333. InfoWindow iw;
  334. iw.player = h->tempOwner;
  335. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  336. cb->showInfoDialog(&iw);
  337. //act as if player refused
  338. joinDecision(h,cost,false);
  339. return;
  340. }
  341. //take gold
  342. if(cost)
  343. cb->giveResource(h->tempOwner,Res::GOLD,-cost);
  344. cb->tryJoiningArmy(this, h, true, true);
  345. }
  346. }
  347. void CGCreature::fight( const CGHeroInstance *h ) const
  348. {
  349. //split stacks
  350. //TODO: multiple creature types in a stack?
  351. int basicType = stacks.begin()->second->type->idNumber;
  352. cb->setObjProperty(id, ObjProperty::MONSTER_RESTORE_TYPE, basicType); //store info about creature stack
  353. int stacksCount = getNumberOfStacks(h);
  354. //source: http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266335#focus
  355. int amount = getStackCount(SlotID(0));
  356. int m = amount / stacksCount;
  357. int b = stacksCount * (m + 1) - amount;
  358. int a = stacksCount - b;
  359. SlotID sourceSlot = stacks.begin()->first;
  360. for (int slotID = 1; slotID < a; ++slotID)
  361. {
  362. int stackSize = m + 1;
  363. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  364. }
  365. for (int slotID = a; slotID < stacksCount; ++slotID)
  366. {
  367. int stackSize = m;
  368. if (slotID) //don't do this when a = 0 -> stack is single
  369. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, SlotID(slotID)), stackSize);
  370. }
  371. if (stacksCount > 1)
  372. {
  373. if (containsUpgradedStack()) //upgrade
  374. {
  375. SlotID slotID = SlotID(std::floor((float)stacks.size() / 2));
  376. const auto & upgrades = getStack(slotID).type->upgrades;
  377. if(!upgrades.empty())
  378. {
  379. auto it = RandomGeneratorUtil::nextItem(upgrades, cb->gameState()->getRandomGenerator());
  380. cb->changeStackType(StackLocation(this, slotID), VLC->creh->creatures[*it]);
  381. }
  382. }
  383. }
  384. cb->startBattleI(h, this);
  385. }
  386. void CGCreature::flee( const CGHeroInstance * h ) const
  387. {
  388. BlockingDialog ynd(true,false);
  389. ynd.player = h->tempOwner;
  390. ynd.text.addTxt(MetaString::ADVOB_TXT,91);
  391. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  392. cb->showBlockingDialog(&ynd);
  393. }
  394. void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  395. {
  396. if(result.winner==0)
  397. {
  398. cb->removeObject(this);
  399. }
  400. else
  401. {
  402. //merge stacks into one
  403. TSlots::const_iterator i;
  404. CCreature * cre = VLC->creh->creatures[formation.basicType];
  405. for (i = stacks.begin(); i != stacks.end(); i++)
  406. {
  407. if (cre->isMyUpgrade(i->second->type))
  408. {
  409. cb->changeStackType (StackLocation(this, i->first), cre); //un-upgrade creatures
  410. }
  411. }
  412. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  413. if(!hasStackAtSlot(SlotID(0)))
  414. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count);
  415. while (stacks.size() > 1) //hopefully that's enough
  416. {
  417. // 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)
  418. i = stacks.end();
  419. i--;
  420. SlotID slot = getSlotFor(i->second->type);
  421. if (slot == i->first) //no reason to move stack to its own slot
  422. break;
  423. else
  424. cb->moveStack (StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  425. }
  426. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  427. }
  428. }
  429. void CGCreature::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  430. {
  431. auto action = takenAction(hero);
  432. if(!refusedJoining && action >= JOIN_FOR_FREE) //higher means price
  433. joinDecision(hero, action, answer);
  434. else if(action != FIGHT)
  435. fleeDecision(hero, answer);
  436. else
  437. assert(0);
  438. }
  439. bool CGCreature::containsUpgradedStack() const
  440. {
  441. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=830557#focus
  442. float a = 2992.911117;
  443. float b = 14174.264968;
  444. float c = 5325.181015;
  445. float d = 32788.727920;
  446. int val = std::floor (a*pos.x + b*pos.y + c*pos.z + d);
  447. return ((val % 32768) % 100) < 50;
  448. }
  449. int CGCreature::getNumberOfStacks(const CGHeroInstance *hero) const
  450. {
  451. //source http://heroescommunity.com/viewthread.php3?TID=27539&PID=1266094#focus
  452. double strengthRatio = (double)hero->getArmyStrength() / getArmyStrength();
  453. int split = 1;
  454. if (strengthRatio < 0.5f)
  455. split = 7;
  456. else if (strengthRatio < 0.67f)
  457. split = 6;
  458. else if (strengthRatio < 1)
  459. split = 5;
  460. else if (strengthRatio < 1.5f)
  461. split = 4;
  462. else if (strengthRatio < 2)
  463. split = 3;
  464. else
  465. split = 2;
  466. int a = 1550811371;
  467. int b = -935900487;
  468. int c = 1943276003;
  469. int d = -1120346418;
  470. int R1 = a * pos.x + b * pos.y + c * pos.z + d;
  471. int R2 = R1 / 65536;
  472. int R3 = R2 % 32768;
  473. if (R3 < 0)
  474. R3 += 32767; //is it ever needed if we do modulo calculus?
  475. int R4 = R3 % 100 + 1;
  476. if (R4 <= 20)
  477. split -= 1;
  478. else if (R4 >= 80)
  479. split += 1;
  480. vstd::amin(split, getStack(SlotID(0)).count); //can't divide into more stacks than creatures total
  481. vstd::amin(split, 7); //can't have more than 7 stacks
  482. return split;
  483. }
  484. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  485. {
  486. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  487. if(relations == 2) //we're visiting our mine
  488. {
  489. cb->showGarrisonDialog(id,h->id,true);
  490. return;
  491. }
  492. else if (relations == 1)//ally
  493. return;
  494. if(stacksCount()) //Mine is guarded
  495. {
  496. BlockingDialog ynd(true,false);
  497. ynd.player = h->tempOwner;
  498. ynd.text.addTxt(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  499. cb->showBlockingDialog(&ynd);
  500. return;
  501. }
  502. flagMine(h->tempOwner);
  503. }
  504. void CGMine::newTurn() const
  505. {
  506. if(cb->getDate() == 1)
  507. return;
  508. if (tempOwner == PlayerColor::NEUTRAL)
  509. return;
  510. cb->giveResource(tempOwner, producedResource, producedQuantity);
  511. }
  512. void CGMine::initObj()
  513. {
  514. if(subID >= 7) //Abandoned Mine
  515. {
  516. //set guardians
  517. int howManyTroglodytes = cb->gameState()->getRandomGenerator().nextInt(100, 199);
  518. auto troglodytes = new CStackInstance(CreatureID::TROGLODYTES, howManyTroglodytes);
  519. putStack(SlotID(0), troglodytes);
  520. //after map reading tempOwner placeholds bitmask for allowed resources
  521. std::vector<Res::ERes> possibleResources;
  522. for (int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  523. if(tempOwner.getNum() & 1<<i) //NOTE: reuse of tempOwner
  524. possibleResources.push_back(static_cast<Res::ERes>(i));
  525. assert(!possibleResources.empty());
  526. producedResource = *RandomGeneratorUtil::nextItem(possibleResources, cb->gameState()->getRandomGenerator());
  527. tempOwner = PlayerColor::NEUTRAL;
  528. }
  529. else
  530. {
  531. producedResource = static_cast<Res::ERes>(subID);
  532. if(tempOwner >= PlayerColor::PLAYER_LIMIT)
  533. tempOwner = PlayerColor::NEUTRAL;
  534. }
  535. producedQuantity = defaultResProduction();
  536. }
  537. std::string CGMine::getObjectName() const
  538. {
  539. return VLC->generaltexth->mines.at(subID).first;
  540. }
  541. std::string CGMine::getHoverText(PlayerColor player) const
  542. {
  543. std::string hoverName = getObjectName(); // Sawmill
  544. if (tempOwner != PlayerColor::NEUTRAL)
  545. {
  546. hoverName += "\n";
  547. hoverName += VLC->generaltexth->arraytxt[23 + tempOwner.getNum()]; // owned by Red Player
  548. hoverName += "\n(" + VLC->generaltexth->restypes[producedResource] + ")";
  549. }
  550. for (auto & slot : Slots()) // guarded by a few Pikeman
  551. {
  552. hoverName += "\n";
  553. hoverName += getRoughAmount(slot.first);
  554. hoverName += getCreature(slot.first)->namePl;
  555. }
  556. return hoverName;
  557. }
  558. void CGMine::flagMine(PlayerColor player) const
  559. {
  560. assert(tempOwner != player);
  561. cb->setOwner(this, player); //not ours? flag it!
  562. InfoWindow iw;
  563. iw.soundID = soundBase::FLAGMINE;
  564. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  565. iw.player = player;
  566. iw.components.push_back(Component(Component::RESOURCE,producedResource,producedQuantity,-1));
  567. cb->showInfoDialog(&iw);
  568. }
  569. ui32 CGMine::defaultResProduction()
  570. {
  571. switch(producedResource)
  572. {
  573. case Res::WOOD:
  574. case Res::ORE:
  575. return 2;
  576. case Res::GOLD:
  577. return 1000;
  578. default:
  579. return 1;
  580. }
  581. }
  582. void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  583. {
  584. if(result.winner == 0) //attacker won
  585. {
  586. if(subID == 7)
  587. {
  588. showInfoDialog(hero->tempOwner, 85, 0);
  589. }
  590. flagMine(hero->tempOwner);
  591. }
  592. }
  593. void CGMine::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  594. {
  595. if(answer)
  596. cb->startBattleI(hero, this);
  597. }
  598. std::string CGResource::getHoverText(PlayerColor player) const
  599. {
  600. return VLC->generaltexth->restypes[subID];
  601. }
  602. CGResource::CGResource()
  603. {
  604. amount = 0;
  605. }
  606. void CGResource::initObj()
  607. {
  608. blockVisit = true;
  609. if(!amount)
  610. {
  611. switch(subID)
  612. {
  613. case 6:
  614. amount = cb->gameState()->getRandomGenerator().nextInt(500, 1000);
  615. break;
  616. case 0: case 2:
  617. amount = cb->gameState()->getRandomGenerator().nextInt(6, 10);
  618. break;
  619. default:
  620. amount = cb->gameState()->getRandomGenerator().nextInt(3, 5);
  621. break;
  622. }
  623. }
  624. }
  625. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  626. {
  627. if(stacksCount())
  628. {
  629. if(message.size())
  630. {
  631. BlockingDialog ynd(true,false);
  632. ynd.player = h->getOwner();
  633. ynd.text << message;
  634. cb->showBlockingDialog(&ynd);
  635. }
  636. else
  637. {
  638. blockingDialogAnswered(h, true); //behave as if player accepted battle
  639. }
  640. }
  641. else
  642. {
  643. if(message.length())
  644. {
  645. InfoWindow iw;
  646. iw.player = h->tempOwner;
  647. iw.text << message;
  648. cb->showInfoDialog(&iw);
  649. }
  650. collectRes(h->getOwner());
  651. }
  652. }
  653. void CGResource::collectRes( PlayerColor player ) const
  654. {
  655. cb->giveResource(player, static_cast<Res::ERes>(subID), amount);
  656. ShowInInfobox sii;
  657. sii.player = player;
  658. sii.c = Component(Component::RESOURCE,subID,amount,0);
  659. sii.text.addTxt(MetaString::ADVOB_TXT,113);
  660. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  661. cb->showCompInfo(&sii);
  662. cb->removeObject(this);
  663. }
  664. void CGResource::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  665. {
  666. if(result.winner == 0) //attacker won
  667. collectRes(hero->getOwner());
  668. }
  669. void CGResource::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  670. {
  671. if(answer)
  672. cb->startBattleI(hero, this);
  673. }
  674. CGTeleport::CGTeleport() :
  675. type(UNKNOWN), channel(TeleportChannelID())
  676. {
  677. }
  678. bool CGTeleport::isEntrance() const
  679. {
  680. return type == BOTH || type == ENTRANCE;
  681. }
  682. bool CGTeleport::isExit() const
  683. {
  684. return type == BOTH || type == EXIT;
  685. }
  686. bool CGTeleport::isChannelEntrance(ObjectInstanceID id) const
  687. {
  688. return vstd::contains(getAllEntrances(), id);
  689. }
  690. bool CGTeleport::isChannelExit(ObjectInstanceID id) const
  691. {
  692. return vstd::contains(getAllExits(), id);
  693. }
  694. std::vector<ObjectInstanceID> CGTeleport::getAllEntrances(bool excludeCurrent) const
  695. {
  696. auto ret = cb->getTeleportChannelEntraces(channel);
  697. if(excludeCurrent)
  698. vstd::erase_if_present(ret, id);
  699. return ret;
  700. }
  701. std::vector<ObjectInstanceID> CGTeleport::getAllExits(bool excludeCurrent) const
  702. {
  703. auto ret = cb->getTeleportChannelExits(channel);
  704. if(excludeCurrent)
  705. vstd::erase_if_present(ret, id);
  706. return ret;
  707. }
  708. ObjectInstanceID CGTeleport::getRandomExit(const CGHeroInstance * h) const
  709. {
  710. auto passableExits = getPassableExits(cb->gameState(), h, getAllExits(true));
  711. if(passableExits.size())
  712. return *RandomGeneratorUtil::nextItem(passableExits, cb->gameState()->getRandomGenerator());
  713. return ObjectInstanceID();
  714. }
  715. bool CGTeleport::isTeleport(const CGObjectInstance * obj)
  716. {
  717. return ((dynamic_cast<const CGTeleport *>(obj)));
  718. }
  719. bool CGTeleport::isConnected(const CGTeleport * src, const CGTeleport * dst)
  720. {
  721. return src && dst && src != dst && src->isChannelExit(dst->id);
  722. }
  723. bool CGTeleport::isConnected(const CGObjectInstance * src, const CGObjectInstance * dst)
  724. {
  725. auto srcObj = dynamic_cast<const CGTeleport *>(src);
  726. auto dstObj = dynamic_cast<const CGTeleport *>(dst);
  727. return isConnected(srcObj, dstObj);
  728. }
  729. bool CGTeleport::isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj)
  730. {
  731. auto objTopVisObj = gs->map->getTile(obj->visitablePos()).topVisitableObj();
  732. if(objTopVisObj->ID == Obj::HERO)
  733. {
  734. if(h->id == objTopVisObj->id) // Just to be sure it's won't happen.
  735. return false;
  736. // Check if it's friendly hero or not
  737. if(gs->getPlayerRelations(h->tempOwner, objTopVisObj->tempOwner))
  738. {
  739. // Exchange between heroes only possible via subterranean gates
  740. if(!dynamic_cast<const CGSubterraneanGate *>(obj))
  741. return false;
  742. }
  743. }
  744. return true;
  745. }
  746. std::vector<ObjectInstanceID> CGTeleport::getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits)
  747. {
  748. vstd::erase_if(exits, [&](ObjectInstanceID exit) -> bool
  749. {
  750. return !isExitPassable(gs, h, gs->getObj(exit));
  751. });
  752. return exits;
  753. }
  754. void CGTeleport::addToChannel(std::map<TeleportChannelID, shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj)
  755. {
  756. shared_ptr<TeleportChannel> tc;
  757. if(channelsList.find(obj->channel) == channelsList.end())
  758. {
  759. tc = make_shared<TeleportChannel>();
  760. channelsList.insert(std::make_pair(obj->channel, tc));
  761. }
  762. else
  763. tc = channelsList[obj->channel];
  764. if(obj->isEntrance() && !vstd::contains(tc->entrances, obj->id))
  765. tc->entrances.push_back(obj->id);
  766. if(obj->isExit() && !vstd::contains(tc->exits, obj->id))
  767. tc->exits.push_back(obj->id);
  768. if(tc->entrances.size() && tc->exits.size()
  769. && (tc->entrances.size() != 1 || tc->entrances != tc->exits))
  770. {
  771. tc->passability = TeleportChannel::PASSABLE;
  772. }
  773. }
  774. TeleportChannelID CGMonolith::findMeChannel(std::vector<Obj> IDs, int SubID) const
  775. {
  776. for(auto obj : cb->gameState()->map->objects)
  777. {
  778. auto teleportObj = dynamic_cast<const CGTeleport *>(cb->getObj(obj->id));
  779. if(teleportObj && vstd::contains(IDs, teleportObj->ID) && teleportObj->subID == SubID)
  780. return teleportObj->channel;
  781. }
  782. return TeleportChannelID();
  783. }
  784. void CGMonolith::onHeroVisit( const CGHeroInstance * h ) const
  785. {
  786. TeleportDialog td(h, channel);
  787. if(isEntrance())
  788. {
  789. if(cb->isTeleportChannelBidirectional(channel) && 1 < cb->getTeleportChannelExits(channel).size())
  790. td.exits = cb->getTeleportChannelExits(channel);
  791. if(cb->isTeleportChannelImpassable(channel))
  792. {
  793. logGlobal->debugStream() << "Cannot find corresponding exit monolith for "<< id << " (obj at " << pos << ") :(";
  794. td.impassable = true;
  795. }
  796. else if(getRandomExit(h) == ObjectInstanceID())
  797. logGlobal->debugStream() << "All exits blocked for monolith "<< id << " (obj at " << pos << ") :(";
  798. }
  799. else
  800. showInfoDialog(h, 70, 0);
  801. cb->showTeleportDialog(&td);
  802. }
  803. void CGMonolith::teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, std::vector<ObjectInstanceID> exits) const
  804. {
  805. ObjectInstanceID objId(answer);
  806. auto realExits = getAllExits(true);
  807. if(!isEntrance() // Do nothing if hero visited exit only object
  808. || (!exits.size() && !realExits.size()) // Do nothing if there no exits on this channel
  809. || (!exits.size() && ObjectInstanceID() == getRandomExit(hero))) // Do nothing if all exits are blocked by friendly hero and it's not subterranean gate
  810. {
  811. return;
  812. }
  813. else if(objId == ObjectInstanceID())
  814. objId = getRandomExit(hero);
  815. else
  816. assert(vstd::contains(exits, objId)); // Likely cheating attempt: not random teleporter choosen, but it's not from provided list
  817. auto obj = cb->getObj(objId);
  818. if(obj)
  819. cb->moveHero(hero->id,CGHeroInstance::convertPosition(obj->pos,true) - getVisitableOffset(), true);
  820. }
  821. void CGMonolith::initObj()
  822. {
  823. std::vector<Obj> IDs;
  824. IDs.push_back(ID);
  825. switch(ID)
  826. {
  827. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  828. type = ENTRANCE;
  829. IDs.push_back(Obj::MONOLITH_ONE_WAY_EXIT);
  830. break;
  831. case Obj::MONOLITH_ONE_WAY_EXIT:
  832. type = EXIT;
  833. IDs.push_back(Obj::MONOLITH_ONE_WAY_ENTRANCE);
  834. break;
  835. case Obj::MONOLITH_TWO_WAY:
  836. default:
  837. type = BOTH;
  838. break;
  839. }
  840. channel = findMeChannel(IDs, subID);
  841. if(channel == TeleportChannelID())
  842. channel = TeleportChannelID(cb->gameState()->map->teleportChannels.size());
  843. addToChannel(cb->gameState()->map->teleportChannels, this);
  844. }
  845. void CGSubterraneanGate::onHeroVisit( const CGHeroInstance * h ) const
  846. {
  847. TeleportDialog td(h, channel);
  848. if(cb->isTeleportChannelImpassable(channel))
  849. {
  850. showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  851. logGlobal->debugStream() << "Cannot find exit subterranean gate for "<< id << " (obj at " << pos << ") :(";
  852. td.impassable = true;
  853. }
  854. else
  855. td.exits.push_back(getRandomExit(h));
  856. cb->showTeleportDialog(&td);
  857. }
  858. void CGSubterraneanGate::initObj()
  859. {
  860. type = BOTH;
  861. }
  862. void CGSubterraneanGate::postInit() //matches subterranean gates into pairs
  863. {
  864. //split on underground and surface gates
  865. std::vector<CGSubterraneanGate *> gatesSplit[2]; //surface and underground gates
  866. for(auto & obj : cb->gameState()->map->objects)
  867. {
  868. auto hlp = dynamic_cast<CGSubterraneanGate *>(cb->gameState()->getObjInstance(obj->id));
  869. if(hlp)
  870. gatesSplit[hlp->pos.z].push_back(hlp);
  871. }
  872. //sort by position
  873. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  874. {
  875. return a->pos < b->pos;
  876. });
  877. auto assignToChannel = [&](CGSubterraneanGate * obj)
  878. {
  879. if(obj->channel == TeleportChannelID())
  880. { // if object not linked to channel then create new channel
  881. obj->channel = TeleportChannelID(cb->gameState()->map->teleportChannels.size());
  882. addToChannel(cb->gameState()->map->teleportChannels, obj);
  883. }
  884. };
  885. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  886. {
  887. CGSubterraneanGate * objCurrent = gatesSplit[0][i];
  888. //find nearest underground exit
  889. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  890. for(int j = 0; j < gatesSplit[1].size(); j++)
  891. {
  892. CGSubterraneanGate *checked = gatesSplit[1][j];
  893. if(checked->channel != TeleportChannelID())
  894. continue;
  895. si32 hlp = checked->pos.dist2dSQ(objCurrent->pos);
  896. if(hlp < best.second)
  897. {
  898. best.first = j;
  899. best.second = hlp;
  900. }
  901. }
  902. assignToChannel(objCurrent);
  903. if(best.first >= 0) //found pair
  904. {
  905. gatesSplit[1][best.first]->channel = objCurrent->channel;
  906. addToChannel(cb->gameState()->map->teleportChannels, gatesSplit[1][best.first]);
  907. }
  908. }
  909. // we should assign empty channels to underground gates if they don't have matching overground gates
  910. for(size_t i = 0; i < gatesSplit[1].size(); i++)
  911. assignToChannel(gatesSplit[1][i]);
  912. }
  913. void CGWhirlpool::onHeroVisit( const CGHeroInstance * h ) const
  914. {
  915. TeleportDialog td(h, channel);
  916. if(cb->isTeleportChannelImpassable(channel))
  917. {
  918. logGlobal->debugStream() << "Cannot find exit whirlpool for "<< id << " (obj at " << pos << ") :(";
  919. td.impassable = true;
  920. }
  921. else if(getRandomExit(h) == ObjectInstanceID())
  922. logGlobal->debugStream() << "All exits are blocked for whirlpool "<< id << " (obj at " << pos << ") :(";
  923. if(!isProtected(h))
  924. {
  925. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  926. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  927. {
  928. if(h->getPower(targetstack) > h->getPower(i->first))
  929. targetstack = (i->first);
  930. }
  931. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  932. vstd::amax(countToTake, 1);
  933. InfoWindow iw;
  934. iw.player = h->tempOwner;
  935. iw.text.addTxt(MetaString::ADVOB_TXT, 168);
  936. iw.components.push_back(Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  937. cb->showInfoDialog(&iw);
  938. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  939. }
  940. else
  941. td.exits = getAllExits(true);
  942. cb->showTeleportDialog(&td);
  943. }
  944. void CGWhirlpool::teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, std::vector<ObjectInstanceID> exits) const
  945. {
  946. ObjectInstanceID objId(answer);
  947. auto realExits = getAllExits();
  948. if(!exits.size() && !realExits.size())
  949. return;
  950. else if(objId == ObjectInstanceID())
  951. objId = getRandomExit(hero);
  952. else
  953. assert(vstd::contains(exits, objId)); // Likely cheating attempt: not random teleporter choosen, but it's not from provided list
  954. auto obj = cb->getObj(objId);
  955. if(obj)
  956. {
  957. std::set<int3> tiles = obj->getBlockedPos();
  958. auto & tile = *RandomGeneratorUtil::nextItem(tiles, cb->gameState()->getRandomGenerator());
  959. cb->moveHero(hero->id, CGHeroInstance::convertPosition(tile, true), 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. }