CObjectHandler.cpp 47 KB

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