MiscObjects.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /*
  2. * MiscObjects.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MiscObjects.h"
  12. #include "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CSoundBase.h"
  15. #include "../CModHandler.h"
  16. #include "CObjectClassesHandler.h"
  17. #include "../spells/CSpellHandler.h"
  18. #include "../IGameCallback.h"
  19. #include "../CGameState.h"
  20. std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > CGTeleport::objs;
  21. std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > CGTeleport::gates;
  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: //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) //creatures will fight
  288. return -2;
  289. if (allowJoin)
  290. {
  291. if(h->getSecSkillLevel(SecondarySkill::DIPLOMACY) + sympathy + 1 >= character)
  292. return 0; //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)
  298. return -1; //flee
  299. else
  300. return -2; //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) == -1) //they 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. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  676. {
  677. ObjectInstanceID destinationid;
  678. switch(ID)
  679. {
  680. case Obj::MONOLITH_ONE_WAY_ENTRANCE: //one way - find corresponding exit monolith
  681. {
  682. if(vstd::contains(objs,Obj::MONOLITH_ONE_WAY_EXIT) && vstd::contains(objs[Obj::MONOLITH_ONE_WAY_EXIT],subID) && objs[Obj::MONOLITH_ONE_WAY_EXIT][subID].size())
  683. {
  684. destinationid = *RandomGeneratorUtil::nextItem(objs[Obj::MONOLITH_ONE_WAY_EXIT][subID], cb->gameState()->getRandomGenerator());
  685. }
  686. else
  687. {
  688. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  689. }
  690. break;
  691. }
  692. case Obj::MONOLITH_TWO_WAY://two way monolith - pick any other one
  693. case Obj::WHIRLPOOL: //Whirlpool
  694. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  695. {
  696. //choose another exit
  697. do
  698. {
  699. destinationid = *RandomGeneratorUtil::nextItem(objs[ID][subID], cb->gameState()->getRandomGenerator());
  700. } while(destinationid == id);
  701. if (ID == Obj::WHIRLPOOL)
  702. {
  703. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  704. {
  705. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  706. { //we can't remove last unit
  707. SlotID targetstack = h->Slots().begin()->first; //slot numbers may vary
  708. for(auto i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  709. {
  710. if (h->getPower(targetstack) > h->getPower(i->first))
  711. {
  712. targetstack = (i->first);
  713. }
  714. }
  715. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  716. vstd::amax(countToTake, 1);
  717. InfoWindow iw;
  718. iw.player = h->tempOwner;
  719. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  720. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  721. cb->showInfoDialog(&iw);
  722. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  723. }
  724. }
  725. }
  726. }
  727. else
  728. logGlobal->warnStream() << "Cannot find corresponding exit monolith for "<< id;
  729. break;
  730. case Obj::SUBTERRANEAN_GATE: //find nearest subterranean gate on the other level
  731. {
  732. destinationid = getMatchingGate(id);
  733. if(destinationid == ObjectInstanceID()) //no exit
  734. {
  735. showInfoDialog(h,153,0);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  736. }
  737. break;
  738. }
  739. }
  740. if(destinationid == ObjectInstanceID())
  741. {
  742. logGlobal->warnStream() << "Cannot find exit... (obj at " << pos << ") :( ";
  743. return;
  744. }
  745. if (ID == Obj::WHIRLPOOL)
  746. {
  747. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  748. auto & tile = *RandomGeneratorUtil::nextItem(tiles, cb->gameState()->getRandomGenerator());
  749. cb->moveHero(h->id, tile + int3(1,0,0), true);
  750. }
  751. else
  752. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  753. }
  754. void CGTeleport::initObj()
  755. {
  756. int si = subID;
  757. switch (ID)
  758. {
  759. case Obj::SUBTERRANEAN_GATE://ignore subterranean gates subid
  760. case Obj::WHIRLPOOL:
  761. {
  762. si = 0;
  763. break;
  764. }
  765. default:
  766. break;
  767. }
  768. objs[ID][si].push_back(id);
  769. }
  770. void CGTeleport::postInit() //matches subterranean gates into pairs
  771. {
  772. //split on underground and surface gates
  773. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  774. for(auto & elem : objs[Obj::SUBTERRANEAN_GATE][0])
  775. {
  776. const CGObjectInstance *hlp = cb->getObj(elem);
  777. gatesSplit[hlp->pos.z].push_back(hlp);
  778. }
  779. //sort by position
  780. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), [](const CGObjectInstance * a, const CGObjectInstance * b)
  781. {
  782. return a->pos < b->pos;
  783. });
  784. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  785. {
  786. const CGObjectInstance *cur = gatesSplit[0][i];
  787. //find nearest underground exit
  788. std::pair<int, si32> best(-1, std::numeric_limits<si32>::max()); //pair<pos_in_vector, distance^2>
  789. for(int j = 0; j < gatesSplit[1].size(); j++)
  790. {
  791. const CGObjectInstance *checked = gatesSplit[1][j];
  792. if(!checked)
  793. continue;
  794. si32 hlp = checked->pos.dist2dSQ(cur->pos);
  795. if(hlp < best.second)
  796. {
  797. best.first = j;
  798. best.second = hlp;
  799. }
  800. }
  801. if(best.first >= 0) //found pair
  802. {
  803. gates.push_back(std::make_pair(cur->id, gatesSplit[1][best.first]->id));
  804. gatesSplit[1][best.first] = nullptr;
  805. }
  806. else
  807. gates.push_back(std::make_pair(cur->id, ObjectInstanceID()));
  808. }
  809. objs.erase(Obj::SUBTERRANEAN_GATE);
  810. }
  811. ObjectInstanceID CGTeleport::getMatchingGate(ObjectInstanceID id)
  812. {
  813. for(auto & gate : gates)
  814. {
  815. if(gate.first == id)
  816. return gate.second;
  817. if(gate.second == id)
  818. return gate.first;
  819. }
  820. return ObjectInstanceID();
  821. }
  822. void CGArtifact::initObj()
  823. {
  824. blockVisit = true;
  825. if(ID == Obj::ARTIFACT)
  826. {
  827. if (!storedArtifact)
  828. {
  829. auto a = new CArtifactInstance();
  830. cb->gameState()->map->addNewArtifactInstance(a);
  831. storedArtifact = a;
  832. }
  833. if(!storedArtifact->artType)
  834. storedArtifact->setType(VLC->arth->artifacts[subID]);
  835. }
  836. if(ID == Obj::SPELL_SCROLL)
  837. subID = 1;
  838. assert(storedArtifact->artType);
  839. assert(storedArtifact->getParentNodes().size());
  840. //assert(storedArtifact->artType->id == subID); //this does not stop desync
  841. }
  842. std::string CGArtifact::getObjectName() const
  843. {
  844. return VLC->arth->artifacts[subID]->Name();
  845. }
  846. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  847. {
  848. if(!stacksCount())
  849. {
  850. InfoWindow iw;
  851. iw.player = h->tempOwner;
  852. switch(ID)
  853. {
  854. case Obj::ARTIFACT:
  855. {
  856. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  857. iw.components.push_back(Component(Component::ARTIFACT,subID,0,0));
  858. if(message.length())
  859. iw.text << message;
  860. else
  861. {
  862. if (VLC->arth->artifacts[subID]->EventText().size())
  863. iw.text << std::pair<ui8, ui32> (MetaString::ART_EVNTS, subID);
  864. else //fix for mod artifacts with no event text
  865. {
  866. iw.text.addTxt (MetaString::ADVOB_TXT, 183); //% has found treasure
  867. iw.text.addReplacement (h->name);
  868. }
  869. }
  870. }
  871. break;
  872. case Obj::SPELL_SCROLL:
  873. {
  874. int spellID = storedArtifact->getGivenSpellID();
  875. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  876. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  877. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  878. }
  879. break;
  880. }
  881. cb->showInfoDialog(&iw);
  882. pick(h);
  883. }
  884. else
  885. {
  886. if(message.size())
  887. {
  888. BlockingDialog ynd(true,false);
  889. ynd.player = h->getOwner();
  890. ynd.text << message;
  891. cb->showBlockingDialog(&ynd);
  892. }
  893. else
  894. {
  895. blockingDialogAnswered(h, true);
  896. }
  897. }
  898. }
  899. void CGArtifact::pick(const CGHeroInstance * h) const
  900. {
  901. cb->giveHeroArtifact(h, storedArtifact, ArtifactPosition::FIRST_AVAILABLE);
  902. cb->removeObject(this);
  903. }
  904. void CGArtifact::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  905. {
  906. if(result.winner == 0) //attacker won
  907. pick(hero);
  908. }
  909. void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  910. {
  911. if(answer)
  912. cb->startBattleI(hero, this);
  913. }
  914. void CGWitchHut::initObj()
  915. {
  916. if (allowedAbilities.empty()) //this can happen for RMG. regular maps load abilities from map file
  917. {
  918. for (int i = 0; i < GameConstants::SKILL_QUANTITY; i++)
  919. allowedAbilities.push_back(i);
  920. }
  921. ability = *RandomGeneratorUtil::nextItem(allowedAbilities, cb->gameState()->getRandomGenerator());
  922. }
  923. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  924. {
  925. InfoWindow iw;
  926. iw.soundID = soundBase::gazebo;
  927. iw.player = h->getOwner();
  928. if(!wasVisited(h->tempOwner))
  929. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  930. ui32 txt_id;
  931. if(h->getSecSkillLevel(SecondarySkill(ability))) //you already know this skill
  932. {
  933. txt_id =172;
  934. }
  935. else if(!h->canLearnSkill()) //already all skills slots used
  936. {
  937. txt_id = 173;
  938. }
  939. else //give sec skill
  940. {
  941. iw.components.push_back(Component(Component::SEC_SKILL, ability, 1, 0));
  942. txt_id = 171;
  943. cb->changeSecSkill(h, SecondarySkill(ability), 1, true);
  944. }
  945. iw.text.addTxt(MetaString::ADVOB_TXT,txt_id);
  946. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  947. cb->showInfoDialog(&iw);
  948. }
  949. std::string CGWitchHut::getHoverText(PlayerColor player) const
  950. {
  951. std::string hoverName = getObjectName();
  952. if(wasVisited(player))
  953. {
  954. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  955. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  956. }
  957. return hoverName;
  958. }
  959. std::string CGWitchHut::getHoverText(const CGHeroInstance * hero) const
  960. {
  961. std::string hoverName = getHoverText(hero->tempOwner);
  962. if(hero->getSecSkillLevel(SecondarySkill(ability))) //hero knows that ability
  963. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  964. return hoverName;
  965. }
  966. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  967. {
  968. int message;
  969. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  970. {
  971. message = 78;//"A second drink at the well in one day will not help you."
  972. }
  973. else if(h->mana < h->manaLimit())
  974. {
  975. giveDummyBonus(h->id);
  976. cb->setManaPoints(h->id,h->manaLimit());
  977. message = 77;
  978. }
  979. else
  980. {
  981. message = 79;
  982. }
  983. showInfoDialog(h,message,soundBase::faerie);
  984. }
  985. std::string CGMagicWell::getHoverText(const CGHeroInstance * hero) const
  986. {
  987. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  988. }
  989. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  990. {
  991. InfoWindow iw;
  992. iw.player = h->tempOwner;
  993. switch (ID)
  994. {
  995. case Obj::REDWOOD_OBSERVATORY:
  996. case Obj::PILLAR_OF_FIRE:
  997. {
  998. iw.soundID = soundBase::LIGHTHOUSE;
  999. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==Obj::PILLAR_OF_FIRE));
  1000. FoWChange fw;
  1001. fw.player = h->tempOwner;
  1002. fw.mode = 1;
  1003. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  1004. cb->sendAndApply (&fw);
  1005. break;
  1006. }
  1007. case Obj::COVER_OF_DARKNESS:
  1008. {
  1009. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  1010. for (auto & player : cb->gameState()->players)
  1011. {
  1012. if (cb->getPlayerStatus(player.first) == EPlayerStatus::INGAME &&
  1013. cb->getPlayerRelations(player.first, h->tempOwner) == PlayerRelations::ENEMIES)
  1014. cb->changeFogOfWar(visitablePos(), 20, player.first, true);
  1015. }
  1016. break;
  1017. }
  1018. }
  1019. cb->showInfoDialog(&iw);
  1020. }
  1021. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  1022. {
  1023. if(spell == SpellID::NONE)
  1024. {
  1025. logGlobal->errorStream() << "Not initialized shrine visited!";
  1026. return;
  1027. }
  1028. if(!wasVisited(h->tempOwner))
  1029. cb->setObjProperty(id, 10, h->tempOwner.getNum());
  1030. InfoWindow iw;
  1031. iw.soundID = soundBase::temple;
  1032. iw.player = h->getOwner();
  1033. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  1034. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  1035. iw.text << ".";
  1036. if(!h->getArt(ArtifactPosition::SPELLBOOK))
  1037. {
  1038. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  1039. }
  1040. else if(ID == Obj::SHRINE_OF_MAGIC_THOUGHT && !h->getSecSkillLevel(SecondarySkill::WISDOM)) //it's third level spell and hero doesn't have wisdom
  1041. {
  1042. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  1043. }
  1044. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  1045. {
  1046. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  1047. }
  1048. else //give spell
  1049. {
  1050. std::set<SpellID> spells;
  1051. spells.insert(spell);
  1052. cb->changeSpells(h, true, spells);
  1053. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1054. }
  1055. cb->showInfoDialog(&iw);
  1056. }
  1057. void CGShrine::initObj()
  1058. {
  1059. if(spell == SpellID::NONE) //spell not set
  1060. {
  1061. int level = ID-87;
  1062. std::vector<SpellID> possibilities;
  1063. cb->getAllowedSpells (possibilities, level);
  1064. if(possibilities.empty())
  1065. {
  1066. logGlobal->errorStream() << "Error: cannot init shrine, no allowed spells!";
  1067. return;
  1068. }
  1069. spell = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1070. }
  1071. }
  1072. std::string CGShrine::getHoverText(PlayerColor player) const
  1073. {
  1074. std::string hoverName = getObjectName();
  1075. if(wasVisited(player))
  1076. {
  1077. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1078. boost::algorithm::replace_first(hoverName,"%s", spell.toSpell()->name);
  1079. }
  1080. return hoverName;
  1081. }
  1082. std::string CGShrine::getHoverText(const CGHeroInstance * hero) const
  1083. {
  1084. std::string hoverName = getHoverText(hero->tempOwner);
  1085. if(vstd::contains(hero->spells, spell)) //hero knows that spell
  1086. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1087. return hoverName;
  1088. }
  1089. void CGSignBottle::initObj()
  1090. {
  1091. //if no text is set than we pick random from the predefined ones
  1092. if(message.empty())
  1093. {
  1094. message = *RandomGeneratorUtil::nextItem(VLC->generaltexth->randsign, cb->gameState()->getRandomGenerator());
  1095. }
  1096. if(ID == Obj::OCEAN_BOTTLE)
  1097. {
  1098. blockVisit = true;
  1099. }
  1100. }
  1101. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  1102. {
  1103. InfoWindow iw;
  1104. iw.soundID = soundBase::STORE;
  1105. iw.player = h->getOwner();
  1106. iw.text << message;
  1107. cb->showInfoDialog(&iw);
  1108. if(ID == Obj::OCEAN_BOTTLE)
  1109. cb->removeObject(this);
  1110. }
  1111. //TODO: remove
  1112. //void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  1113. //{
  1114. //
  1115. //}
  1116. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  1117. {
  1118. EBonusType type = bonusType;
  1119. int bid = bonusID;
  1120. //check if the bonus if applicable, if not - give primary skill (always possible)
  1121. int ssl = h->getSecSkillLevel(SecondarySkill(bid)); //current sec skill level, used if bonusType == 1
  1122. if((type == SECONDARY_SKILL
  1123. && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  1124. || (type == SPELL && (!h->getArt(ArtifactPosition::SPELLBOOK) || vstd::contains(h->spells, (ui32) bid)
  1125. || ( SpellID(bid).toSpell()->level > h->getSecSkillLevel(SecondarySkill::WISDOM) + 2)
  1126. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  1127. {
  1128. type = PRIM_SKILL;
  1129. bid = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS - 1);
  1130. }
  1131. InfoWindow iw;
  1132. iw.soundID = soundBase::gazebo;
  1133. iw.player = h->getOwner();
  1134. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  1135. switch (type)
  1136. {
  1137. case PRIM_SKILL:
  1138. cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(bid),+1);
  1139. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  1140. break;
  1141. case SECONDARY_SKILL:
  1142. cb->changeSecSkill(h,SecondarySkill(bid),+1);
  1143. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  1144. break;
  1145. case SPELL:
  1146. {
  1147. std::set<SpellID> hlp;
  1148. hlp.insert(SpellID(bid));
  1149. cb->changeSpells(h,true,hlp);
  1150. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  1151. }
  1152. break;
  1153. default:
  1154. logGlobal->errorStream() << "Error: wrong bonus type (" << (int)type << ") for Scholar!\n";
  1155. return;
  1156. }
  1157. cb->showInfoDialog(&iw);
  1158. cb->removeObject(this);
  1159. }
  1160. void CGScholar::initObj()
  1161. {
  1162. blockVisit = true;
  1163. if(bonusType == RANDOM)
  1164. {
  1165. bonusType = static_cast<EBonusType>(cb->gameState()->getRandomGenerator().nextInt(2));
  1166. switch(bonusType)
  1167. {
  1168. case PRIM_SKILL:
  1169. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::PRIMARY_SKILLS -1);
  1170. break;
  1171. case SECONDARY_SKILL:
  1172. bonusID = cb->gameState()->getRandomGenerator().nextInt(GameConstants::SKILL_QUANTITY -1);
  1173. break;
  1174. case SPELL:
  1175. std::vector<SpellID> possibilities;
  1176. for (int i = 1; i < 6; ++i)
  1177. cb->getAllowedSpells (possibilities, i);
  1178. bonusID = *RandomGeneratorUtil::nextItem(possibilities, cb->gameState()->getRandomGenerator());
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  1184. {
  1185. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  1186. if (!ally && stacksCount() > 0) {
  1187. //TODO: Find a way to apply magic garrison effects in battle.
  1188. cb->startBattleI(h, this);
  1189. return;
  1190. }
  1191. //New owner.
  1192. if (!ally)
  1193. cb->setOwner(this, h->tempOwner);
  1194. cb->showGarrisonDialog(id, h->id, removableUnits);
  1195. }
  1196. bool CGGarrison::passableFor(PlayerColor player) const
  1197. {
  1198. //FIXME: identical to same method in CGTownInstance
  1199. if ( !stacksCount() )//empty - anyone can visit
  1200. return true;
  1201. if ( tempOwner == PlayerColor::NEUTRAL )//neutral guarded - no one can visit
  1202. return false;
  1203. if (cb->getPlayerRelations(tempOwner, player) != PlayerRelations::ENEMIES)
  1204. return true;
  1205. return false;
  1206. }
  1207. void CGGarrison::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  1208. {
  1209. if (result.winner == 0)
  1210. onHeroVisit(hero);
  1211. }
  1212. void CGMagi::initObj()
  1213. {
  1214. if (ID == Obj::EYE_OF_MAGI)
  1215. {
  1216. blockVisit = true;
  1217. eyelist[subID].push_back(id);
  1218. }
  1219. }
  1220. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  1221. {
  1222. if (ID == Obj::HUT_OF_MAGI)
  1223. {
  1224. showInfoDialog(h, 61, soundBase::LIGHTHOUSE);
  1225. if (!eyelist[subID].empty())
  1226. {
  1227. CenterView cv;
  1228. cv.player = h->tempOwner;
  1229. cv.focusTime = 2000;
  1230. FoWChange fw;
  1231. fw.player = h->tempOwner;
  1232. fw.mode = 1;
  1233. fw.waitForDialogs = true;
  1234. for(auto it : eyelist[subID])
  1235. {
  1236. const CGObjectInstance *eye = cb->getObj(it);
  1237. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  1238. cb->sendAndApply(&fw);
  1239. cv.pos = eye->pos;
  1240. cb->sendAndApply(&cv);
  1241. }
  1242. cv.pos = h->getPosition(false);
  1243. cb->sendAndApply(&cv);
  1244. }
  1245. }
  1246. else if (ID == Obj::EYE_OF_MAGI)
  1247. {
  1248. showInfoDialog(h,48,soundBase::invalid);
  1249. }
  1250. }
  1251. void CGBoat::initObj()
  1252. {
  1253. hero = nullptr;
  1254. }
  1255. void CGSirens::initObj()
  1256. {
  1257. blockVisit = true;
  1258. }
  1259. std::string CGSirens::getHoverText(const CGHeroInstance * hero) const
  1260. {
  1261. return getObjectName() + " " + visitedTxt(hero->hasBonusFrom(Bonus::OBJECT,ID));
  1262. }
  1263. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  1264. {
  1265. InfoWindow iw;
  1266. iw.soundID = soundBase::DANGER;
  1267. iw.player = h->tempOwner;
  1268. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  1269. {
  1270. iw.text.addTxt(MetaString::ADVOB_TXT,133);
  1271. }
  1272. else
  1273. {
  1274. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  1275. TExpType xp = 0;
  1276. for (auto i = h->Slots().begin(); i != h->Slots().end(); i++)
  1277. {
  1278. TQuantity drown = i->second->count * 0.3;
  1279. if(drown)
  1280. {
  1281. cb->changeStackCount(StackLocation(h, i->first), -drown);
  1282. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  1283. }
  1284. }
  1285. if(xp)
  1286. {
  1287. xp = h->calculateXp(xp);
  1288. iw.text.addTxt(MetaString::ADVOB_TXT,132);
  1289. iw.text.addReplacement(xp);
  1290. cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, xp, false);
  1291. }
  1292. else
  1293. {
  1294. iw.text.addTxt(MetaString::ADVOB_TXT,134);
  1295. }
  1296. }
  1297. cb->showInfoDialog(&iw);
  1298. }
  1299. CGShipyard::CGShipyard()
  1300. :IShipyard(this)
  1301. {
  1302. }
  1303. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  1304. {
  1305. // H J L K I
  1306. // A x S x B
  1307. // C E G F D
  1308. offsets = {
  1309. int3(-3,0,0), int3(1,0,0), //AB
  1310. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  1311. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0) //HIJKL
  1312. };
  1313. }
  1314. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  1315. {
  1316. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  1317. cb->setOwner(this, h->tempOwner);
  1318. auto s = shipyardStatus();
  1319. if(s != IBoatGenerator::GOOD)
  1320. {
  1321. InfoWindow iw;
  1322. iw.player = tempOwner;
  1323. getProblemText(iw.text, h);
  1324. cb->showInfoDialog(&iw);
  1325. }
  1326. else
  1327. {
  1328. openWindow(OpenWindow::SHIPYARD_WINDOW,id.getNum(),h->id.getNum());
  1329. }
  1330. }
  1331. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  1332. {
  1333. //if player has not bought map of this subtype yet and underground exist for stalagmite cartographer
  1334. if (!wasVisited(h->getOwner()) && (subID != 2 || cb->gameState()->map->twoLevel))
  1335. {
  1336. if (cb->getResource(h->tempOwner, Res::GOLD) >= 1000) //if he can afford a map
  1337. {
  1338. //ask if he wants to buy one
  1339. int text=0;
  1340. switch (subID)
  1341. {
  1342. case 0:
  1343. text = 25;
  1344. break;
  1345. case 1:
  1346. text = 26;
  1347. break;
  1348. case 2:
  1349. text = 27;
  1350. break;
  1351. default:
  1352. logGlobal->warnStream() << "Unrecognized subtype of cartographer";
  1353. }
  1354. assert(text);
  1355. BlockingDialog bd (true, false);
  1356. bd.player = h->getOwner();
  1357. bd.soundID = soundBase::LIGHTHOUSE;
  1358. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  1359. cb->showBlockingDialog (&bd);
  1360. }
  1361. else //if he cannot afford
  1362. {
  1363. showInfoDialog(h,28,soundBase::CAVEHEAD);
  1364. }
  1365. }
  1366. else //if he already visited carographer
  1367. {
  1368. showInfoDialog(h,24,soundBase::CAVEHEAD);
  1369. }
  1370. }
  1371. void CCartographer::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  1372. {
  1373. if (answer) //if hero wants to buy map
  1374. {
  1375. cb->giveResource (hero->tempOwner, Res::GOLD, -1000);
  1376. FoWChange fw;
  1377. fw.mode = 1;
  1378. fw.player = hero->tempOwner;
  1379. //subIDs of different types of cartographers:
  1380. //water = 0; land = 1; underground = 2;
  1381. cb->getAllTiles (fw.tiles, hero->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  1382. cb->sendAndApply (&fw);
  1383. cb->setObjProperty (id, 10, hero->tempOwner.getNum());
  1384. }
  1385. }
  1386. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  1387. {
  1388. cb->showThievesGuildWindow(h->tempOwner, id);
  1389. }
  1390. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  1391. {
  1392. InfoWindow iw;
  1393. iw.player = h->tempOwner;
  1394. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  1395. assert(ts);
  1396. TeamID team = ts->id;
  1397. if(!wasVisited(team))
  1398. {
  1399. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  1400. cb->sendAndApply(&iw);
  1401. cb->setObjProperty(id, 20, h->tempOwner.getNum()); //increment general visited obelisks counter
  1402. openWindow(OpenWindow::PUZZLE_MAP, h->tempOwner.getNum());
  1403. cb->setObjProperty(id, 10, h->tempOwner.getNum()); //mark that particular obelisk as visited
  1404. }
  1405. else
  1406. {
  1407. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  1408. cb->sendAndApply(&iw);
  1409. }
  1410. }
  1411. void CGObelisk::initObj()
  1412. {
  1413. obeliskCount++;
  1414. }
  1415. std::string CGObelisk::getHoverText(PlayerColor player) const
  1416. {
  1417. return getObjectName() + " " + visitedTxt(wasVisited(player));
  1418. }
  1419. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  1420. {
  1421. CPlayersVisited::setPropertyDer(what, val);
  1422. switch(what)
  1423. {
  1424. case 20:
  1425. assert(val < PlayerColor::PLAYER_LIMIT_I);
  1426. visited[TeamID(val)]++;
  1427. if(visited[TeamID(val)] > obeliskCount)
  1428. {
  1429. logGlobal->errorStream() << "Error: Visited " << visited[TeamID(val)] << "\t\t" << obeliskCount;
  1430. assert(0);
  1431. }
  1432. break;
  1433. }
  1434. }
  1435. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  1436. {
  1437. if(h->tempOwner != tempOwner)
  1438. {
  1439. PlayerColor oldOwner = tempOwner;
  1440. cb->setOwner(this,h->tempOwner); //not ours? flag it!
  1441. showInfoDialog(h,69,soundBase::LIGHTHOUSE);
  1442. giveBonusTo(h->tempOwner);
  1443. if(oldOwner < PlayerColor::PLAYER_LIMIT) //remove bonus from old owner
  1444. {
  1445. RemoveBonus rb(RemoveBonus::PLAYER);
  1446. rb.whoID = oldOwner.getNum();
  1447. rb.source = Bonus::OBJECT;
  1448. rb.id = id.getNum();
  1449. cb->sendAndApply(&rb);
  1450. }
  1451. }
  1452. }
  1453. void CGLighthouse::initObj()
  1454. {
  1455. if(tempOwner < PlayerColor::PLAYER_LIMIT)
  1456. {
  1457. giveBonusTo(tempOwner);
  1458. }
  1459. }
  1460. std::string CGLighthouse::getHoverText(PlayerColor player) const
  1461. {
  1462. //TODO: owned by %s player
  1463. return getObjectName();
  1464. }
  1465. void CGLighthouse::giveBonusTo( PlayerColor player ) const
  1466. {
  1467. GiveBonus gb(GiveBonus::PLAYER);
  1468. gb.bonus.type = Bonus::SEA_MOVEMENT;
  1469. gb.bonus.val = 500;
  1470. gb.id = player.getNum();
  1471. gb.bonus.duration = Bonus::PERMANENT;
  1472. gb.bonus.source = Bonus::OBJECT;
  1473. gb.bonus.sid = id.getNum();
  1474. cb->sendAndApply(&gb);
  1475. }