CObjectHandler.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CDefObjInfoHandler.h"
  8. #include "CHeroHandler.h"
  9. #include "CSpellHandler.h"
  10. #include <boost/bind.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/random/linear_congruential.hpp>
  14. #include "CTownHandler.h"
  15. #include "CArtHandler.h"
  16. #include "../lib/VCMI_Lib.h"
  17. #include "../lib/IGameCallback.h"
  18. #include "../CGameState.h"
  19. #include "../lib/NetPacks.h"
  20. #include "../StartInfo.h"
  21. #include "../map.h"
  22. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  23. IGameCallback * IObjectInterface::cb = NULL;
  24. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  25. extern CLodHandler * bitmaph;
  26. extern boost::rand48 ran;
  27. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  28. {};
  29. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  30. {};
  31. void IObjectInterface::newTurn () const
  32. {};
  33. IObjectInterface::~IObjectInterface()
  34. {}
  35. IObjectInterface::IObjectInterface()
  36. {}
  37. void IObjectInterface::initObj()
  38. {}
  39. void IObjectInterface::setProperty( ui8 what, ui32 val )
  40. {}
  41. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  42. {
  43. if(what == 10)
  44. players.insert(val);
  45. }
  46. bool CPlayersVisited::hasVisited( ui8 player ) const
  47. {
  48. return vstd::contains(players,player);
  49. }
  50. void CObjectHandler::loadObjects()
  51. {
  52. tlog5 << "\t\tReading cregens \n";
  53. cregens.resize(110); //TODO: hardcoded value - change
  54. for(size_t i=0; i < cregens.size(); ++i)
  55. {
  56. cregens[i]=-1;
  57. }
  58. std::ifstream ifs("config/cregens.txt");
  59. while(!ifs.eof())
  60. {
  61. int dw, cr;
  62. ifs >> dw >> cr;
  63. cregens[dw]=cr;
  64. }
  65. ifs.close();
  66. ifs.clear();
  67. tlog5 << "\t\tDone loading objects!\n";
  68. }
  69. int CGObjectInstance::getOwner() const
  70. {
  71. //if (state)
  72. // return state->owner;
  73. //else
  74. return tempOwner; //won't have owner
  75. }
  76. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  77. {
  78. pos = int3(-1,-1,-1);
  79. //std::cout << "Tworze obiekt "<<this<<std::endl;
  80. //state = new CLuaObjectScript();
  81. ID = subID = id = -1;
  82. defInfo = NULL;
  83. info = NULL;
  84. tempOwner = 254;
  85. blockVisit = false;
  86. }
  87. CGObjectInstance::~CGObjectInstance()
  88. {
  89. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  90. //if (state)
  91. // delete state;
  92. //state=NULL;
  93. }
  94. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  95. //{
  96. // pos = right.pos;
  97. // ID = right.ID;
  98. // subID = right.subID;
  99. // id = right.id;
  100. // defInfo = right.defInfo;
  101. // info = right.info;
  102. // blockVisit = right.blockVisit;
  103. // //state = new CLuaObjectScript(right.state->);
  104. // //*state = *right.state;
  105. // //state = right.state;
  106. // tempOwner = right.tempOwner;
  107. //}
  108. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  109. //{
  110. // pos = right.pos;
  111. // ID = right.ID;
  112. // subID = right.subID;
  113. // id = right.id;
  114. // defInfo = right.defInfo;
  115. // info = right.info;
  116. // blockVisit = right.blockVisit;
  117. // //state = new CLuaObjectScript();
  118. // //*state = *right.state;
  119. // tempOwner = right.tempOwner;
  120. // return *this;
  121. //}
  122. const std::string & CGObjectInstance::getHoverText() const
  123. {
  124. return hoverName;
  125. }
  126. void CGObjectInstance::setOwner(int ow)
  127. {
  128. //if (state)
  129. // state->owner = ow;
  130. //else
  131. tempOwner = ow;
  132. }
  133. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  134. {
  135. return defInfo->width;
  136. }
  137. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  138. {
  139. return defInfo->height;
  140. }
  141. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  142. {
  143. if(defInfo==NULL)
  144. {
  145. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  146. return false;
  147. }
  148. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  149. {
  150. return true;
  151. }
  152. return false;
  153. }
  154. bool CGObjectInstance::blockingAt(int x, int y) const
  155. {
  156. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  157. return false;
  158. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  159. return true;
  160. return false;
  161. }
  162. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  163. {
  164. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  165. return true;
  166. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  167. return false;
  168. if(this->pos.y<cmp.pos.y)
  169. return true;
  170. if(this->pos.y>cmp.pos.y)
  171. return false;
  172. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  173. return true;
  174. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  175. return false;
  176. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  177. return true;
  178. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  179. return false;
  180. if(this->pos.x<cmp.pos.x)
  181. return true;
  182. return false;
  183. }
  184. void CGObjectInstance::initObj()
  185. {
  186. }
  187. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  188. {
  189. switch(what)
  190. {
  191. case 1:
  192. tempOwner = val;
  193. break;
  194. case 2:
  195. blockVisit = val;
  196. break;
  197. case 6:
  198. ID = val;
  199. break;
  200. }
  201. setPropertyDer(what, val);
  202. }
  203. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  204. {}
  205. int3 CGObjectInstance::getSightCenter() const
  206. {
  207. //return vistiable tile if possible
  208. for(int i=0; i < 8; i++)
  209. for(int j=0; j < 6; j++)
  210. if(visitableAt(i,j))
  211. return(pos + int3(i-7, j-5, 0));
  212. return pos;
  213. }
  214. int CGObjectInstance::getSightRadious() const
  215. {
  216. return 3;
  217. }
  218. int lowestSpeed(const CGHeroInstance * chi)
  219. {
  220. if(!chi->army.slots.size())
  221. {
  222. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  223. return 20;
  224. }
  225. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  226. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  227. for (;i!=chi->army.slots.end();i++)
  228. {
  229. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  230. }
  231. return ret;
  232. }
  233. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  234. {
  235. //TODO: check if all creatures are on its native terrain and change cost appropriately
  236. //base move cost
  237. unsigned ret = 100;
  238. //if there is road both on dest and src tiles - use road movement cost
  239. if(dest.malle && from.malle)
  240. {
  241. int road = std::min(dest.malle,from.malle); //used road ID
  242. switch(road)
  243. {
  244. case dirtRoad:
  245. ret = 75;
  246. break;
  247. case grazvelRoad:
  248. ret = 65;
  249. break;
  250. case cobblestoneRoad:
  251. ret = 50;
  252. break;
  253. default:
  254. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  255. break;
  256. }
  257. }
  258. else
  259. {
  260. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  261. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  262. }
  263. return ret;
  264. }
  265. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  266. {
  267. unsigned int sl = 100;
  268. for(size_t h=0; h < army.slots.size(); ++h)
  269. {
  270. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  271. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  272. }
  273. return sl;
  274. }
  275. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  276. {
  277. if (toh3m)
  278. {
  279. src.x+=1;
  280. return src;
  281. }
  282. else
  283. {
  284. src.x-=1;
  285. return src;
  286. }
  287. }
  288. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  289. {
  290. if (h3m)
  291. {
  292. return pos;
  293. }
  294. else
  295. {
  296. return convertPosition(pos,false);
  297. }
  298. }
  299. si32 CGHeroInstance::manaLimit() const
  300. {
  301. double modifier = 1.0;
  302. switch(getSecSkillLevel(24)) //intelligence level
  303. {
  304. case 1: modifier+=0.25; break;
  305. case 2: modifier+=0.5; break;
  306. case 3: modifier+=1.0; break;
  307. }
  308. return si32(10*getPrimSkillLevel(3)*modifier);
  309. }
  310. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  311. //{
  312. // if (h3m)
  313. // pos = Pos;
  314. // else
  315. // pos = convertPosition(Pos,true);
  316. //}
  317. bool CGHeroInstance::canWalkOnSea() const
  318. {
  319. //TODO: write it - it should check if hero is flying, or something similiar
  320. return false;
  321. }
  322. int CGHeroInstance::getPrimSkillLevel(int id) const
  323. {
  324. return primSkills[id];
  325. }
  326. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  327. {
  328. for(size_t i=0; i < secSkills.size(); ++i)
  329. if(secSkills[i].first==ID)
  330. return secSkills[i].second;
  331. return 0;
  332. }
  333. int CGHeroInstance::maxMovePoints(bool onLand) const
  334. {
  335. int ret = 1270+70*lowestSpeed(this);
  336. if (ret>2000)
  337. ret=2000;
  338. double bonus = 0;
  339. if(onLand)
  340. {
  341. //logistics:
  342. switch(getSecSkillLevel(2))
  343. {
  344. case 1:
  345. bonus = 0.1;
  346. break;
  347. case 2:
  348. bonus = 0.2;
  349. break;
  350. case 3:
  351. bonus = 0.3;
  352. break;
  353. }
  354. }
  355. else
  356. {
  357. //navigation:
  358. switch(getSecSkillLevel(2))
  359. {
  360. case 1:
  361. bonus = 0.5;
  362. break;
  363. case 2:
  364. bonus = 1.0;
  365. break;
  366. case 3:
  367. bonus = 1.5;
  368. break;
  369. }
  370. }
  371. return int(ret + ret*bonus);
  372. }
  373. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  374. {
  375. if(pos<19)
  376. if(vstd::contains(artifWorn,pos))
  377. return artifWorn.find(pos)->second;
  378. else
  379. return -1;
  380. else
  381. if(pos-19 < artifacts.size())
  382. return artifacts[pos-19];
  383. else
  384. return -1;
  385. }
  386. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  387. {
  388. if(art<0)
  389. {
  390. if(pos<19)
  391. artifWorn.erase(pos);
  392. else
  393. artifacts -= artifacts[pos-19];
  394. }
  395. else
  396. {
  397. if(pos<19)
  398. artifWorn[pos] = art;
  399. else
  400. if(pos-19 < artifacts.size())
  401. artifacts[pos-19] = art;
  402. else
  403. artifacts.push_back(art);
  404. }
  405. }
  406. const CArtifact * CGHeroInstance::getArt(int pos) const
  407. {
  408. int id = getArtAtPos(pos);
  409. if(id>=0)
  410. return &VLC->arth->artifacts[id];
  411. else
  412. return NULL;
  413. }
  414. int CGHeroInstance::getSpellSecLevel(int spell) const
  415. {
  416. int bestslvl = 0;
  417. if(VLC->spellh->spells[spell].air)
  418. if(getSecSkillLevel(15) >= bestslvl)
  419. {
  420. bestslvl = getSecSkillLevel(15);
  421. }
  422. if(VLC->spellh->spells[spell].fire)
  423. if(getSecSkillLevel(14) >= bestslvl)
  424. {
  425. bestslvl = getSecSkillLevel(14);
  426. }
  427. if(VLC->spellh->spells[spell].water)
  428. if(getSecSkillLevel(16) >= bestslvl)
  429. {
  430. bestslvl = getSecSkillLevel(16);
  431. }
  432. if(VLC->spellh->spells[spell].earth)
  433. if(getSecSkillLevel(17) >= bestslvl)
  434. {
  435. bestslvl = getSecSkillLevel(17);
  436. }
  437. return bestslvl;
  438. }
  439. CGHeroInstance::CGHeroInstance()
  440. {
  441. ID = HEROI_TYPE;
  442. tacticFormationEnabled = inTownGarrison = false;
  443. mana = movement = portrait = level = -1;
  444. isStanding = true;
  445. moveDir = 4;
  446. exp = 0xffffffff;
  447. visitedTown = NULL;
  448. type = NULL;
  449. secSkills.push_back(std::make_pair(-1, -1));
  450. }
  451. void CGHeroInstance::initHero(int SUBID)
  452. {
  453. subID = SUBID;
  454. initHero();
  455. }
  456. void CGHeroInstance::initHero()
  457. {
  458. if(ID == HEROI_TYPE)
  459. initHeroDefInfo();
  460. if(!type)
  461. type = VLC->heroh->heroes[subID];
  462. artifWorn[16] = 3;
  463. if(type->heroType % 2 == 1) //it's a magical hero
  464. {
  465. artifWorn[17] = 0; //give him spellbook
  466. }
  467. if(portrait < 0 || portrait == 255)
  468. portrait = subID;
  469. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  470. {
  471. primSkills.resize(4);
  472. primSkills[0] = type->heroClass->initialAttack;
  473. primSkills[1] = type->heroClass->initialDefence;
  474. primSkills[2] = type->heroClass->initialPower;
  475. primSkills[3] = type->heroClass->initialKnowledge;
  476. }
  477. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  478. secSkills = type->secSkillsInit;
  479. if(mana < 0)
  480. mana = manaLimit();
  481. if (!name.length())
  482. name = type->name;
  483. if (exp == 0xffffffff)
  484. {
  485. exp=40+ (ran()) % 50;
  486. level = 1;
  487. }
  488. else
  489. {
  490. level = VLC->heroh->level(exp);
  491. }
  492. if (!army.slots.size()) //standard army//initial army
  493. {
  494. int pom, pom2=0;
  495. for(int x=0;x<3;x++)
  496. {
  497. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  498. if(pom>=145 && pom<=149) //war machine
  499. {
  500. pom2++;
  501. switch (pom)
  502. {
  503. case 145: //catapult
  504. artifWorn[16] = 3;
  505. break;
  506. default:
  507. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  508. break;
  509. }
  510. continue;
  511. }
  512. army.slots[x-pom2].first = pom;
  513. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  514. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  515. else
  516. army.slots[x-pom2].second = +type->lowStack[x];
  517. army.formation = false;
  518. }
  519. }
  520. hoverName = VLC->generaltexth->allTexts[15];
  521. boost::algorithm::replace_first(hoverName,"%s",name);
  522. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  523. }
  524. void CGHeroInstance::initHeroDefInfo()
  525. {
  526. if(!defInfo || defInfo->id != HEROI_TYPE)
  527. {
  528. defInfo = new CGDefInfo();
  529. defInfo->id = HEROI_TYPE;
  530. defInfo->subid = subID;
  531. defInfo->printPriority = 0;
  532. defInfo->visitDir = 0xff;
  533. }
  534. for(int i=0;i<6;i++)
  535. {
  536. defInfo->blockMap[i]=255;
  537. defInfo->visitMap[i]=0;
  538. }
  539. defInfo->handler=NULL;
  540. defInfo->blockMap[5] = 253;
  541. defInfo->visitMap[5] = 2;
  542. }
  543. CGHeroInstance::~CGHeroInstance()
  544. {
  545. }
  546. bool CGHeroInstance::needsLastStack() const
  547. {
  548. return true;
  549. }
  550. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  551. {
  552. if (ID == HEROI_TYPE) //hero
  553. {
  554. //TODO: check for allies
  555. if(tempOwner == h->tempOwner) //our hero
  556. {
  557. //exchange
  558. }
  559. else
  560. {
  561. cb->startBattleI(
  562. &h->army,
  563. &army,
  564. h->pos,
  565. h,
  566. this,
  567. 0);
  568. }
  569. }
  570. else if(ID == 62) //prison
  571. {
  572. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  573. {
  574. cb->changeObjPos(id,pos+int3(1,0,0),0);
  575. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  576. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  577. InfoWindow iw;
  578. iw.player = h->tempOwner;
  579. iw.text << std::pair<ui8,ui32>(11,102);
  580. cb->showInfoDialog(&iw);
  581. }
  582. else //already 8 wandering heroes
  583. {
  584. InfoWindow iw;
  585. iw.player = h->tempOwner;
  586. iw.text << std::pair<ui8,ui32>(11,103);
  587. cb->showInfoDialog(&iw);
  588. }
  589. }
  590. }
  591. const std::string & CGHeroInstance::getBiography() const
  592. {
  593. if (biography.length())
  594. return biography;
  595. else
  596. return VLC->generaltexth->hTxts[subID].biography;
  597. }
  598. void CGHeroInstance::initObj()
  599. {
  600. blockVisit = true;
  601. }
  602. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  603. {
  604. int ret = 0;
  605. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  606. for(int i=0; i < mods.size(); i++)
  607. ret += mods[i].first;
  608. if(ret > 3)
  609. return 3;
  610. if(ret < -3)
  611. return -3;
  612. return ret;
  613. }
  614. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  615. {
  616. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  617. std::vector<std::pair<int,std::string> > ret;
  618. //various morale bonuses (from buildings, artifacts, etc)
  619. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  620. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  621. ret.push_back(std::make_pair(i->val, i->description));
  622. //leadership
  623. if(getSecSkillLevel(6))
  624. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  625. //town structures
  626. if(town && visitedTown)
  627. {
  628. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  629. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  630. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  631. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  632. }
  633. //number of alignments and presence of undead
  634. if(stack>=0)
  635. {
  636. bool archangelInArmy = false;
  637. std::set<si8> factions;
  638. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  639. {
  640. factions.insert(VLC->creh->creatures[i->second.first].faction);
  641. if(i->second.first == 13)
  642. archangelInArmy = true;
  643. }
  644. if(factions.size() == 1)
  645. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  646. else
  647. {
  648. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  649. {
  650. char buf[150];
  651. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  652. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  653. }
  654. else
  655. {
  656. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  657. }
  658. }
  659. if(vstd::contains(factions,4))
  660. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  661. if(archangelInArmy)
  662. {
  663. char buf[100];
  664. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl);
  665. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //%s in group +1
  666. }
  667. }
  668. return ret;
  669. }
  670. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  671. {
  672. int ret = 0;
  673. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  674. for(int i=0; i < mods.size(); i++)
  675. ret += mods[i].first;
  676. if(ret > 3)
  677. return 3;
  678. if(ret < -3)
  679. return -3;
  680. return ret;
  681. }
  682. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  683. {
  684. std::vector<std::pair<int,std::string> > ret;
  685. //various morale bonuses (from buildings, artifacts, etc)
  686. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  687. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  688. ret.push_back(std::make_pair(i->val, i->description));
  689. //luck skill
  690. if(getSecSkillLevel(9))
  691. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  692. return ret;
  693. }
  694. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  695. {
  696. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  697. if(i->source == from && i->id == id)
  698. return &*i;
  699. return NULL;
  700. }
  701. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  702. {
  703. if(what == 3)
  704. army.slots[0].second = val;
  705. }
  706. int3 CGHeroInstance::getSightCenter() const
  707. {
  708. return getPosition(false);
  709. }
  710. int CGHeroInstance::getSightRadious() const
  711. {
  712. return 5 + getSecSkillLevel(3); //default + scouting
  713. }
  714. int CGTownInstance::getSightRadious() const //returns sight distance
  715. {
  716. return 5;
  717. }
  718. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  719. {
  720. if((builtBuildings.find(9))!=builtBuildings.end())
  721. return 3;
  722. if((builtBuildings.find(8))!=builtBuildings.end())
  723. return 2;
  724. if((builtBuildings.find(7))!=builtBuildings.end())
  725. return 1;
  726. return 0;
  727. }
  728. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  729. {
  730. if ((builtBuildings.find(13))!=builtBuildings.end())
  731. return 3;
  732. if ((builtBuildings.find(12))!=builtBuildings.end())
  733. return 2;
  734. if ((builtBuildings.find(11))!=builtBuildings.end())
  735. return 1;
  736. if ((builtBuildings.find(10))!=builtBuildings.end())
  737. return 0;
  738. return -1;
  739. }
  740. int CGTownInstance::mageGuildLevel() const
  741. {
  742. if ((builtBuildings.find(4))!=builtBuildings.end())
  743. return 5;
  744. if ((builtBuildings.find(3))!=builtBuildings.end())
  745. return 4;
  746. if ((builtBuildings.find(2))!=builtBuildings.end())
  747. return 3;
  748. if ((builtBuildings.find(1))!=builtBuildings.end())
  749. return 2;
  750. if ((builtBuildings.find(0))!=builtBuildings.end())
  751. return 1;
  752. return 0;
  753. }
  754. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  755. {
  756. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  757. }
  758. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  759. {
  760. return town->hordeLvl[HID];
  761. }
  762. int CGTownInstance::creatureGrowth(const int & level) const
  763. {
  764. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  765. switch(fortLevel())
  766. {
  767. case 3:
  768. ret*=2;break;
  769. case 2:
  770. ret*=(1.5); break;
  771. }
  772. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  773. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  774. if(getHordeLevel(0)==level)
  775. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  776. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  777. if(getHordeLevel(1)==level)
  778. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  779. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  780. return ret;
  781. }
  782. int CGTownInstance::dailyIncome() const
  783. {
  784. int ret = 0;
  785. if ((builtBuildings.find(26))!=builtBuildings.end())
  786. ret+=5000;
  787. if ((builtBuildings.find(13))!=builtBuildings.end())
  788. ret+=4000;
  789. else if ((builtBuildings.find(12))!=builtBuildings.end())
  790. ret+=2000;
  791. else if ((builtBuildings.find(11))!=builtBuildings.end())
  792. ret+=1000;
  793. else if ((builtBuildings.find(10))!=builtBuildings.end())
  794. ret+=500;
  795. return ret;
  796. }
  797. bool CGTownInstance::hasFort() const
  798. {
  799. return (builtBuildings.find(7))!=builtBuildings.end();
  800. }
  801. bool CGTownInstance::hasCapitol() const
  802. {
  803. return (builtBuildings.find(13))!=builtBuildings.end();
  804. }
  805. CGTownInstance::CGTownInstance()
  806. {
  807. builded=-1;
  808. destroyed=-1;
  809. garrisonHero=NULL;
  810. town=NULL;
  811. visitingHero = NULL;
  812. }
  813. CGTownInstance::~CGTownInstance()
  814. {}
  815. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  816. {
  817. if(checkGuild && mageGuildLevel() < level)
  818. return 0;
  819. int ret = 6 - level; //how many spells are available at this level
  820. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  821. ret++;
  822. return ret;
  823. }
  824. bool CGTownInstance::needsLastStack() const
  825. {
  826. if(garrisonHero)
  827. return true;
  828. else return false;
  829. }
  830. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  831. {
  832. if(getOwner() != h->getOwner())
  833. {
  834. return;
  835. }
  836. cb->heroVisitCastle(id,h->id);
  837. }
  838. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  839. {
  840. cb->stopHeroVisitCastle(id,h->id);
  841. }
  842. void CGTownInstance::initObj()
  843. {
  844. MetaString ms;
  845. ms << name << ", " << town->Name();
  846. hoverName = toString(ms);
  847. }
  848. int3 CGTownInstance::getSightCenter() const
  849. {
  850. return pos - int3(2,0,0);
  851. }
  852. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  853. {
  854. if(visitors.find(h->id)==visitors.end())
  855. {
  856. onNAHeroVisit(h->id, false);
  857. if(ID != 102 && ID!=4 && ID!=41) //not tree nor arena nor library of enlightenment
  858. cb->setObjProperty(id,4,h->id); //add to the visitors
  859. }
  860. else
  861. {
  862. onNAHeroVisit(h->id, true);
  863. }
  864. }
  865. void CGVisitableOPH::initObj()
  866. {
  867. if(ID==102)
  868. ttype = ran()%3;
  869. else
  870. ttype = -1;
  871. }
  872. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  873. {
  874. if(result==0) //player agreed to give res for exp
  875. {
  876. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  877. cb->changePrimSkill(heroID,4,expVal); //give exp
  878. cb->setObjProperty(id,4,heroID); //add to the visitors
  879. }
  880. }
  881. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  882. {
  883. int id=0, subid=0, ot=0, val=1;
  884. switch(ID)
  885. {
  886. case 4:
  887. ot = 0;
  888. break;
  889. case 51:
  890. subid=0;
  891. ot=80;
  892. break;
  893. case 23:
  894. subid=1;
  895. ot=39;
  896. break;
  897. case 61:
  898. subid=2;
  899. ot=100;
  900. break;
  901. case 32:
  902. subid=3;
  903. ot=59;
  904. break;
  905. case 100:
  906. id=5;
  907. ot=143;
  908. val=1000;
  909. break;
  910. case 102:
  911. id = 5;
  912. subid = 1;
  913. ot = 146;
  914. val = 1;
  915. break;
  916. case 41:
  917. ot = 66;
  918. break;
  919. }
  920. if (!alreadyVisited)
  921. {
  922. switch (ID)
  923. {
  924. case 4: //arena
  925. {
  926. SelectionDialog sd;
  927. sd.text << std::pair<ui8,ui32>(11,ot);
  928. sd.components.push_back(Component(0,0,2,0));
  929. sd.components.push_back(Component(0,1,2,0));
  930. sd.player = cb->getOwner(heroID);
  931. cb->showSelectionDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  932. return;
  933. }
  934. case 51:
  935. case 23:
  936. case 61:
  937. case 32:
  938. {
  939. cb->changePrimSkill(heroID,subid,val);
  940. InfoWindow iw;
  941. iw.components.push_back(Component(0,subid,val,0));
  942. iw.text << std::pair<ui8,ui32>(11,ot);
  943. iw.player = cb->getOwner(heroID);
  944. cb->showInfoDialog(&iw);
  945. break;
  946. }
  947. case 100: //give exp
  948. {
  949. InfoWindow iw;
  950. iw.components.push_back(Component(id,subid,val,0));
  951. iw.player = cb->getOwner(heroID);
  952. iw.text << std::pair<ui8,ui32>(11,ot);
  953. cb->showInfoDialog(&iw);
  954. cb->changePrimSkill(heroID,4,val);
  955. break;
  956. }
  957. case 102:
  958. {
  959. const CGHeroInstance *h = cb->getHero(heroID);
  960. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  961. if(!ttype)
  962. {
  963. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  964. InfoWindow iw;
  965. iw.components.push_back(Component(id,subid,1,0));
  966. iw.player = cb->getOwner(heroID);
  967. iw.text << std::pair<ui8,ui32>(11,148);
  968. cb->showInfoDialog(&iw);
  969. cb->changePrimSkill(heroID,4,val);
  970. break;
  971. }
  972. else
  973. {
  974. int res, resval;
  975. if(ttype==1)
  976. {
  977. res = 6;
  978. resval = 2000;
  979. ot = 149;
  980. }
  981. else
  982. {
  983. res = 5;
  984. resval = 10;
  985. ot = 151;
  986. }
  987. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  988. {
  989. ot++;
  990. InfoWindow iw;
  991. iw.player = h->tempOwner;
  992. iw.text << std::pair<ui8,ui32>(11,ot);
  993. cb->showInfoDialog(&iw);
  994. return;
  995. }
  996. YesNoDialog sd;
  997. sd.player = cb->getOwner(heroID);
  998. sd.text << std::pair<ui8,ui32>(11,ot);
  999. sd.components.push_back(Component(id,subid,val,0));
  1000. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1001. }
  1002. break;
  1003. }
  1004. case 41:
  1005. {
  1006. const CGHeroInstance *h = cb->getHero(heroID);
  1007. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1008. {
  1009. InfoWindow iw;
  1010. iw.player = cb->getOwner(heroID);
  1011. iw.text << std::pair<ui8,ui32>(11,68);
  1012. cb->showInfoDialog(&iw);
  1013. }
  1014. else
  1015. {
  1016. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1017. cb->changePrimSkill(heroID,0,2);
  1018. cb->changePrimSkill(heroID,1,2);
  1019. cb->changePrimSkill(heroID,2,2);
  1020. cb->changePrimSkill(heroID,3,2);
  1021. InfoWindow iw;
  1022. iw.player = cb->getOwner(heroID);
  1023. iw.text << std::pair<ui8,ui32>(11,66);
  1024. cb->showInfoDialog(&iw);
  1025. }
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. else
  1031. {
  1032. ot++;
  1033. InfoWindow iw;
  1034. iw.player = cb->getOwner(heroID);
  1035. iw.text << std::pair<ui8,ui32>(11,ot);
  1036. cb->showInfoDialog(&iw);
  1037. }
  1038. }
  1039. const std::string & CGVisitableOPH::getHoverText() const
  1040. {
  1041. int pom = -1;
  1042. switch(ID)
  1043. {
  1044. case 4:
  1045. pom = -1;
  1046. break;
  1047. case 51:
  1048. pom = 8;
  1049. break;
  1050. case 23:
  1051. pom = 7;
  1052. break;
  1053. case 61:
  1054. pom = 11;
  1055. break;
  1056. case 32:
  1057. pom = 4;
  1058. break;
  1059. case 100:
  1060. pom = 5;
  1061. break;
  1062. case 102:
  1063. pom = 18;
  1064. break;
  1065. case 41:
  1066. break;
  1067. default:
  1068. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1069. }
  1070. hoverName = VLC->generaltexth->names[ID];
  1071. if(pom >= 0)
  1072. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1073. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1074. if(h)
  1075. {
  1076. hoverName += ' ';
  1077. hoverName += (vstd::contains(visitors,h->id))
  1078. ? (VLC->generaltexth->allTexts[352]) //visited
  1079. : ( VLC->generaltexth->allTexts[353]); //not visited
  1080. }
  1081. return hoverName;
  1082. }
  1083. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1084. {
  1085. cb->setObjProperty(id,4,heroID); //add to the visitors
  1086. cb->changePrimSkill(heroID,primSkill,2);
  1087. }
  1088. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1089. {
  1090. if(what == 4)
  1091. visitors.insert(val);
  1092. }
  1093. bool CArmedInstance::needsLastStack() const
  1094. {
  1095. return false;
  1096. }
  1097. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1098. {
  1099. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1100. }
  1101. void CGCreature::endBattle( BattleResult *result ) const
  1102. {
  1103. if(result->winner==0)
  1104. {
  1105. cb->removeObject(id);
  1106. }
  1107. else
  1108. {
  1109. int killedAmount=0;
  1110. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1111. if(i->first == subID)
  1112. killedAmount += i->second;
  1113. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1114. MetaString ms;
  1115. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1116. pom = 174 + 3*pom + 1;
  1117. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1118. cb->setHoverName(id,&ms);
  1119. }
  1120. }
  1121. void CGCreature::initObj()
  1122. {
  1123. army.slots[0].first = subID;
  1124. si32 &amount = army.slots[0].second;
  1125. CCreature &c = VLC->creh->creatures[subID];
  1126. if(!amount)
  1127. if(c.ammMax == c.ammMin)
  1128. amount = c.ammMax;
  1129. else
  1130. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1131. MetaString ms;
  1132. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1133. pom = 174 + 3*pom + 1;
  1134. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1135. hoverName = toString(ms);
  1136. }
  1137. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1138. {
  1139. if(subID == 7) //TODO: support for abandoned mine
  1140. return;
  1141. if(h->tempOwner == tempOwner) //we're visiting our mine
  1142. return; //TODO: leaving garrison
  1143. //TODO: check if mine is guarded
  1144. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1145. MetaString ms;
  1146. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1147. cb->setHoverName(id,&ms);
  1148. int vv=1; //amount of resource per turn
  1149. if (subID==0 || subID==2)
  1150. vv++;
  1151. else if (subID==6)
  1152. vv = 1000;
  1153. InfoWindow iw;
  1154. iw.text << std::pair<ui8,ui32>(10,subID);
  1155. iw.player = h->tempOwner;
  1156. iw.components.push_back(Component(2,subID,vv,-1));
  1157. cb->showInfoDialog(&iw);
  1158. }
  1159. void CGMine::newTurn() const
  1160. {
  1161. if (tempOwner == NEUTRAL_PLAYER)
  1162. return;
  1163. int vv = 1;
  1164. if (subID==0 || subID==2)
  1165. vv++;
  1166. else if (subID==6)
  1167. vv = 1000;
  1168. cb->giveResource(tempOwner,subID,vv);
  1169. }
  1170. void CGMine::initObj()
  1171. {
  1172. MetaString ms;
  1173. ms << std::pair<ui8,ui32>(9,subID);
  1174. if(tempOwner >= PLAYER_LIMIT)
  1175. tempOwner = NEUTRAL_PLAYER;
  1176. else
  1177. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1178. hoverName = toString(ms);
  1179. }
  1180. void CGResource::initObj()
  1181. {
  1182. blockVisit = true;
  1183. hoverName = VLC->generaltexth->restypes[subID];
  1184. if(!amount)
  1185. {
  1186. switch(subID)
  1187. {
  1188. case 6:
  1189. amount = 500 + (rand()%6)*100;
  1190. break;
  1191. case 0: case 2:
  1192. amount = 6 + (rand()%5);
  1193. break;
  1194. default:
  1195. amount = 3 + (rand()%3);
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1201. {
  1202. if(army.slots.size())
  1203. {
  1204. if(message.size())
  1205. {
  1206. YesNoDialog ynd;
  1207. ynd.player = h->getOwner();
  1208. ynd.text << message;
  1209. cb->showYesNoDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1210. }
  1211. else
  1212. {
  1213. fightForRes(0,h);
  1214. }
  1215. }
  1216. else
  1217. {
  1218. if(message.length())
  1219. {
  1220. InfoWindow iw;
  1221. iw.player = h->tempOwner;
  1222. iw.text << message;
  1223. cb->showInfoDialog(&iw);
  1224. }
  1225. collectRes(h->getOwner());
  1226. }
  1227. }
  1228. void CGResource::collectRes( int player ) const
  1229. {
  1230. cb->giveResource(player,subID,amount);
  1231. ShowInInfobox sii;
  1232. sii.player = player;
  1233. sii.c = Component(2,subID,amount,0);
  1234. sii.text << std::pair<ui8,ui32>(11,113);
  1235. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1236. cb->showCompInfo(&sii);
  1237. cb->removeObject(id);
  1238. }
  1239. void CGResource::fightForRes(ui32 refusedFight, const CGHeroInstance *h) const
  1240. {
  1241. if(!refusedFight)
  1242. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1243. }
  1244. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1245. {
  1246. if(result->winner == 0) //attacker won
  1247. collectRes(h->getOwner());
  1248. }
  1249. void CGVisitableOPW::newTurn() const
  1250. {
  1251. if (cb->getDate(1)==1) //first day of week
  1252. {
  1253. cb->setObjProperty(id,5,false);
  1254. MetaString ms; //set text to "not visited"
  1255. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1256. cb->setHoverName(id,&ms);
  1257. }
  1258. }
  1259. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1260. {
  1261. int mid;
  1262. switch (ID)
  1263. {
  1264. case 55:
  1265. mid = 92;
  1266. break;
  1267. case 112:
  1268. mid = 170;
  1269. break;
  1270. case 109:
  1271. mid = 164;
  1272. break;
  1273. }
  1274. if (visited)
  1275. {
  1276. if (ID!=112)
  1277. mid++;
  1278. else
  1279. mid--;
  1280. InfoWindow iw;
  1281. iw.player = h->tempOwner;
  1282. iw.text << std::pair<ui8,ui32>(11,mid);
  1283. cb->showInfoDialog(&iw);
  1284. }
  1285. else
  1286. {
  1287. int type, sub, val;
  1288. type = 2;
  1289. switch (ID)
  1290. {
  1291. case 55:
  1292. if (rand()%2)
  1293. {
  1294. sub = 5;
  1295. val = 5;
  1296. }
  1297. else
  1298. {
  1299. sub = 6;
  1300. val = 500;
  1301. }
  1302. break;
  1303. case 112:
  1304. mid = 170;
  1305. sub = (rand() % 5) + 1;
  1306. val = (rand() % 4) + 3;
  1307. break;
  1308. case 109:
  1309. mid = 164;
  1310. sub = 6;
  1311. if(cb->getDate(0)<8)
  1312. val = 500;
  1313. else
  1314. val = 1000;
  1315. }
  1316. cb->giveResource(h->tempOwner,sub,val);
  1317. InfoWindow iw;
  1318. iw.player = h->tempOwner;
  1319. iw.components.push_back(Component(type,sub,val,0));
  1320. iw.text << std::pair<ui8,ui32>(11,mid);
  1321. cb->showInfoDialog(&iw);
  1322. cb->setObjProperty(id,5,true);
  1323. MetaString ms; //set text to "visited"
  1324. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1325. cb->setHoverName(id,&ms);
  1326. }
  1327. }
  1328. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1329. {
  1330. if(what == 5)
  1331. visited = val;
  1332. }
  1333. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1334. {
  1335. int destinationid=-1;
  1336. switch(ID)
  1337. {
  1338. case 43: //one way - find correspong exit monolith
  1339. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1340. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1341. else
  1342. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1343. break;
  1344. case 45: //two way monolith - pick any other one
  1345. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1346. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1347. else
  1348. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1349. break;
  1350. case 103: //find nearest subterranean gate on the other level
  1351. {
  1352. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1353. for(int i=0; i<objs[103][0].size(); i++)
  1354. {
  1355. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1356. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1357. if(hlp<best.second)
  1358. {
  1359. best.first = objs[103][0][i];
  1360. best.second = hlp;
  1361. }
  1362. }
  1363. if(best.first<0)
  1364. return;
  1365. else
  1366. destinationid = best.first;
  1367. break;
  1368. }
  1369. }
  1370. if(destinationid < 0)
  1371. {
  1372. tlog2 << "Cannot find exit... :( \n";
  1373. return;
  1374. }
  1375. cb->moveHero(h->id,
  1376. (ID!=103)
  1377. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1378. : (cb->getObj(destinationid)->pos),
  1379. true);
  1380. }
  1381. void CGTeleport::initObj()
  1382. {
  1383. objs[ID][subID].push_back(id);
  1384. }
  1385. void CGArtifact::initObj()
  1386. {
  1387. blockVisit = true;
  1388. if(ID == 5)
  1389. hoverName = VLC->arth->artifacts[subID].Name();
  1390. }
  1391. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1392. {
  1393. if(!army.slots.size())
  1394. {
  1395. InfoWindow iw;
  1396. iw.player = h->tempOwner;
  1397. iw.components.push_back(Component(4,subID,0,0));
  1398. if(message.length())
  1399. iw.text << message;
  1400. else
  1401. iw.text << std::pair<ui8,ui32>(12,subID);
  1402. cb->showInfoDialog(&iw);
  1403. pick(h);
  1404. }
  1405. else
  1406. {
  1407. if(message.size())
  1408. {
  1409. YesNoDialog ynd;
  1410. ynd.player = h->getOwner();
  1411. ynd.text << message;
  1412. cb->showYesNoDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1413. }
  1414. else
  1415. {
  1416. fightForArt(0,h);
  1417. }
  1418. }
  1419. }
  1420. void CGArtifact::pick(const CGHeroInstance * h) const
  1421. {
  1422. cb->giveHeroArtifact(subID,h->id,-2);
  1423. cb->removeObject(id);
  1424. }
  1425. void CGArtifact::fightForArt( ui32 refusedFight, const CGHeroInstance *h ) const
  1426. {
  1427. if(!refusedFight)
  1428. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1429. }
  1430. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1431. {
  1432. if(result->winner == 0) //attacker won
  1433. pick(h);
  1434. }
  1435. void CGPickable::initObj()
  1436. {
  1437. blockVisit = true;
  1438. switch(ID)
  1439. {
  1440. case 12: //campfire
  1441. val2 = (ran()%3) + 4; //4 - 6
  1442. val1 = val2 * 100;
  1443. type = ran()%6; //given resource
  1444. break;
  1445. case 101: //treasure chest
  1446. {
  1447. int hlp = ran()%100;
  1448. if(hlp >= 95)
  1449. {
  1450. type = 1;
  1451. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1452. return;
  1453. }
  1454. else if (hlp >= 65)
  1455. {
  1456. val1 = 2000;
  1457. }
  1458. else if(hlp >= 33)
  1459. {
  1460. val1 = 1500;
  1461. }
  1462. else
  1463. {
  1464. val1 = 1000;
  1465. }
  1466. val2 = val1 - 500;
  1467. type = 0;
  1468. break;
  1469. }
  1470. }
  1471. }
  1472. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1473. {
  1474. switch(ID)
  1475. {
  1476. case 12: //campfire
  1477. {
  1478. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1479. cb->giveResource(h->tempOwner,6,val1);//gold
  1480. InfoWindow iw;
  1481. iw.player = h->tempOwner;
  1482. iw.components.push_back(Component(2,6,val1,0));
  1483. iw.components.push_back(Component(2,type,val2,0));
  1484. iw.text << std::pair<ui8,ui32>(11,23);
  1485. cb->showInfoDialog(&iw);
  1486. break;
  1487. }
  1488. case 101: //treasure chest
  1489. {
  1490. if (subID) //not OH3 treasure chest
  1491. {
  1492. tlog2 << "Not supported WoG treasure chest!\n";
  1493. return;
  1494. }
  1495. if(type) //there is an artifact
  1496. {
  1497. cb->giveHeroArtifact(val1,h->id,-2);
  1498. InfoWindow iw;
  1499. iw.player = h->tempOwner;
  1500. iw.components.push_back(Component(4,val1,1,0));
  1501. iw.text << std::pair<ui8,ui32>(11,145);
  1502. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1503. cb->showInfoDialog(&iw);
  1504. break;
  1505. }
  1506. else
  1507. {
  1508. SelectionDialog sd;
  1509. sd.player = h->tempOwner;
  1510. sd.text << std::pair<ui8,ui32>(11,146);
  1511. sd.components.push_back(Component(2,6,val1,0));
  1512. sd.components.push_back(Component(5,0,val2,0));
  1513. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1514. cb->showSelectionDialog(&sd,fun);
  1515. return;
  1516. }
  1517. }
  1518. }
  1519. cb->removeObject(id);
  1520. }
  1521. void CGPickable::chosen( int which, int heroID ) const
  1522. {
  1523. switch(which)
  1524. {
  1525. case 0: //player pick gold
  1526. cb->giveResource(cb->getOwner(heroID),6,val1);
  1527. break;
  1528. case 1: //player pick exp
  1529. cb->changePrimSkill(heroID, 4, val2);
  1530. break;
  1531. default:
  1532. throw std::string("Unhandled treasure choice");
  1533. }
  1534. cb->removeObject(id);
  1535. }
  1536. void CGWitchHut::initObj()
  1537. {
  1538. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1539. }
  1540. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1541. {
  1542. InfoWindow iw;
  1543. iw.player = h->getOwner();
  1544. if(!hasVisited(h->tempOwner))
  1545. cb->setObjProperty(id,10,h->tempOwner);
  1546. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1547. {
  1548. iw.text << std::pair<ui8,ui32>(11,172);
  1549. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1550. }
  1551. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1552. {
  1553. iw.text << std::pair<ui8,ui32>(11,173);
  1554. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1555. }
  1556. else //give sec skill
  1557. {
  1558. iw.components.push_back(Component(1, ability, 1, 0));
  1559. iw.text << std::pair<ui8,ui32>(11,171);
  1560. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1561. cb->changeSecSkill(h->id,ability,1,true);
  1562. }
  1563. cb->showInfoDialog(&iw);
  1564. }
  1565. const std::string & CGWitchHut::getHoverText() const
  1566. {
  1567. hoverName = VLC->generaltexth->names[ID];
  1568. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1569. {
  1570. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  1571. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  1572. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  1573. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  1574. }
  1575. return hoverName;
  1576. }
  1577. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1578. {
  1579. }
  1580. void CGDwelling::initObj()
  1581. {
  1582. }
  1583. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1584. {
  1585. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1586. int messageID, bonusType, bonusVal;
  1587. int bonusMove = 0;
  1588. InfoWindow iw;
  1589. iw.player = h->tempOwner;
  1590. GiveBonus gbonus;
  1591. gbonus.hid = h->id;
  1592. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1593. gbonus.bonus.source = HeroBonus::OBJECT;
  1594. gbonus.bonus.id = ID;
  1595. switch(ID)
  1596. {
  1597. case 14: //swan pond
  1598. messageID = 29;
  1599. gbonus.bonus.type = HeroBonus::LUCK;
  1600. gbonus.bonus.val = 2;
  1601. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1602. bonusMove = -h->movement;
  1603. break;
  1604. case 28: //Faerie Ring
  1605. messageID = 49;
  1606. gbonus.bonus.type = HeroBonus::LUCK;
  1607. gbonus.bonus.val = 1;
  1608. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1609. break;
  1610. case 30: //fountain of fortune
  1611. messageID = 55;
  1612. gbonus.bonus.type = HeroBonus::LUCK;
  1613. gbonus.bonus.val = rand()%5 - 1;
  1614. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  1615. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  1616. break;
  1617. case 38: //idol of fortune
  1618. messageID = 62;
  1619. if(cb->getDate(1) == 7) //7th day of week
  1620. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1621. else
  1622. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  1623. gbonus.bonus.val = 1;
  1624. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  1625. break;
  1626. case 64: //Rally Flag
  1627. messageID = 111;
  1628. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1629. gbonus.bonus.val = 1;
  1630. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  1631. bonusMove = 400;
  1632. break;
  1633. case 56: //oasis
  1634. messageID = 95;
  1635. gbonus.bonus.type = HeroBonus::MORALE;
  1636. gbonus.bonus.val = 1;
  1637. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  1638. bonusMove = 800;
  1639. break;
  1640. case 96: //temple
  1641. messageID = 140;
  1642. gbonus.bonus.type = HeroBonus::MORALE;
  1643. if(cb->getDate(1)==7) //sunday
  1644. {
  1645. gbonus.bonus.val = 2;
  1646. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  1647. }
  1648. else
  1649. {
  1650. gbonus.bonus.val = 1;
  1651. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  1652. }
  1653. break;
  1654. case 110://Watering Hole
  1655. messageID = 166;
  1656. gbonus.bonus.type = HeroBonus::MORALE;
  1657. gbonus.bonus.val = 1;
  1658. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  1659. bonusMove = 400;
  1660. break;
  1661. case 31: //Fountain of Youth
  1662. messageID = 57;
  1663. gbonus.bonus.type = HeroBonus::MORALE;
  1664. gbonus.bonus.val = 1;
  1665. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  1666. bonusMove = 400;
  1667. break;
  1668. }
  1669. if(visited)
  1670. {
  1671. if(ID==64 || ID==96 || ID==56)
  1672. messageID--;
  1673. else
  1674. messageID++;
  1675. }
  1676. else
  1677. {
  1678. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1679. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  1680. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1681. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  1682. cb->giveHeroBonus(&gbonus);
  1683. if(bonusMove) //swan pond - take all move points
  1684. {
  1685. SetMovePoints smp;
  1686. smp.hid = h->id;
  1687. smp.val = h->movement + bonusMove;
  1688. cb->setMovePoints(&smp);
  1689. }
  1690. }
  1691. iw.text << std::pair<ui8,ui32>(11,messageID);
  1692. cb->showInfoDialog(&iw);
  1693. }
  1694. const std::string & CGBonusingObject::getHoverText() const
  1695. {
  1696. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1697. hoverName = VLC->generaltexth->names[ID];
  1698. if(h)
  1699. {
  1700. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1701. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1702. else
  1703. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1704. }
  1705. return hoverName;
  1706. }
  1707. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  1708. {
  1709. int message;
  1710. InfoWindow iw;
  1711. iw.player = h->tempOwner;
  1712. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  1713. {
  1714. message = 78;
  1715. }
  1716. else if(h->mana < h->manaLimit())
  1717. {
  1718. GiveBonus gbonus;
  1719. gbonus.bonus.type = HeroBonus::NONE;
  1720. gbonus.hid = h->id;
  1721. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  1722. gbonus.bonus.source = HeroBonus::OBJECT;
  1723. gbonus.bonus.id = ID;
  1724. cb->giveHeroBonus(&gbonus);
  1725. cb->setManaPoints(h->id,h->manaLimit());
  1726. message = 77;
  1727. }
  1728. else
  1729. {
  1730. message = 79;
  1731. }
  1732. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  1733. cb->showInfoDialog(&iw);
  1734. }
  1735. const std::string & CGMagicWell::getHoverText() const
  1736. {
  1737. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1738. hoverName = VLC->generaltexth->names[ID];
  1739. if(h)
  1740. {
  1741. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1742. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1743. else
  1744. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1745. }
  1746. return hoverName;
  1747. }
  1748. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  1749. {
  1750. /*if(!(availableFor & (1 << h->tempOwner)))
  1751. return;
  1752. if(cb->getPlayerSettings(h->tempOwner)->human)
  1753. {
  1754. if(humanActivate)
  1755. activated(h);
  1756. }
  1757. else if(computerActivate)
  1758. activated(h);*/
  1759. }
  1760. void CGEvent::endBattle( BattleResult *result ) const
  1761. {
  1762. if(result->winner)
  1763. return;
  1764. //give
  1765. }
  1766. void CGEvent::activated( const CGHeroInstance * h ) const
  1767. {
  1768. InfoWindow iw;
  1769. iw.player = h->tempOwner;
  1770. iw.text << message;
  1771. cb->showInfoDialog(&iw);
  1772. if(guarders)
  1773. cb->startBattleI(h->id,guarders,pos,boost::bind(&CGEvent::endBattle,this,_1));
  1774. cb->removeObject(id);
  1775. }
  1776. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  1777. {
  1778. FoWChange fw;
  1779. fw.player = h->tempOwner;
  1780. fw.mode = 1;
  1781. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  1782. cb->sendAndApply(&fw);
  1783. InfoWindow iw;
  1784. iw.player = h->tempOwner;
  1785. iw.text.addTxt(MetaString::ADVOB_TXT,98);
  1786. cb->showInfoDialog(&iw);
  1787. }
  1788. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  1789. {
  1790. if(spell == 255)
  1791. {
  1792. tlog1 << "Not initialized shrine visited!\n";
  1793. return;
  1794. }
  1795. if(!hasVisited(h->tempOwner))
  1796. cb->setObjProperty(id,10,h->tempOwner);
  1797. InfoWindow iw;
  1798. iw.player = h->getOwner();
  1799. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  1800. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  1801. iw.text << ".";
  1802. if(!h->getArt(17)) //no spellbook
  1803. {
  1804. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  1805. }
  1806. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  1807. {
  1808. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  1809. }
  1810. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  1811. {
  1812. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  1813. }
  1814. else //give spell
  1815. {
  1816. std::set<ui32> spells;
  1817. spells.insert(spell);
  1818. cb->changeSpells(h->id,true,spells);
  1819. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1820. }
  1821. cb->showInfoDialog(&iw);
  1822. }
  1823. void CGShrine::initObj()
  1824. {
  1825. if(spell == 255) //spell not set
  1826. {
  1827. int level = ID-87;
  1828. std::vector<ui32> possibilities;
  1829. //add all allowed spells of wanted level
  1830. for(int i=0; i<SPELLS_QUANTITY; i++)
  1831. {
  1832. if(VLC->spellh->spells[i].level == level
  1833. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  1834. {
  1835. possibilities.push_back(VLC->spellh->spells[i].id);
  1836. }
  1837. }
  1838. if(!possibilities.size())
  1839. {
  1840. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  1841. return;
  1842. }
  1843. spell = possibilities[ran() % possibilities.size()];
  1844. }
  1845. }
  1846. const std::string & CGShrine::getHoverText() const
  1847. {
  1848. hoverName = VLC->generaltexth->names[ID];
  1849. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1850. {
  1851. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1852. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  1853. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1854. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  1855. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1856. }
  1857. return hoverName;
  1858. }