CObjectHandler.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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. int ret = primSkills[id];
  325. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  326. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  327. ret += i->val;
  328. return ret;
  329. }
  330. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  331. {
  332. for(size_t i=0; i < secSkills.size(); ++i)
  333. if(secSkills[i].first==ID)
  334. return secSkills[i].second;
  335. return 0;
  336. }
  337. int CGHeroInstance::maxMovePoints(bool onLand) const
  338. {
  339. int ret = std::max(2000, 1270+70*lowestSpeed(this)),
  340. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  341. double modifier = 0;
  342. if(onLand)
  343. {
  344. //logistics:
  345. switch(getSecSkillLevel(2))
  346. {
  347. case 1:
  348. modifier = 0.1;
  349. break;
  350. case 2:
  351. modifier = 0.2;
  352. break;
  353. case 3:
  354. modifier = 0.3;
  355. break;
  356. }
  357. }
  358. else
  359. {
  360. //navigation:
  361. switch(getSecSkillLevel(2))
  362. {
  363. case 1:
  364. modifier = 0.5;
  365. break;
  366. case 2:
  367. modifier = 1.0;
  368. break;
  369. case 3:
  370. modifier = 1.5;
  371. break;
  372. }
  373. }
  374. return int(ret + ret*modifier) + bonus;
  375. }
  376. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  377. {
  378. if(pos<19)
  379. if(vstd::contains(artifWorn,pos))
  380. return artifWorn.find(pos)->second;
  381. else
  382. return -1;
  383. else
  384. if(pos-19 < artifacts.size())
  385. return artifacts[pos-19];
  386. else
  387. return -1;
  388. }
  389. const CArtifact * CGHeroInstance::getArt(int pos) const
  390. {
  391. int id = getArtAtPos(pos);
  392. if(id>=0)
  393. return &VLC->arth->artifacts[id];
  394. else
  395. return NULL;
  396. }
  397. int CGHeroInstance::getSpellSecLevel(int spell) const
  398. {
  399. int bestslvl = 0;
  400. if(VLC->spellh->spells[spell].air)
  401. if(getSecSkillLevel(15) >= bestslvl)
  402. {
  403. bestslvl = getSecSkillLevel(15);
  404. }
  405. if(VLC->spellh->spells[spell].fire)
  406. if(getSecSkillLevel(14) >= bestslvl)
  407. {
  408. bestslvl = getSecSkillLevel(14);
  409. }
  410. if(VLC->spellh->spells[spell].water)
  411. if(getSecSkillLevel(16) >= bestslvl)
  412. {
  413. bestslvl = getSecSkillLevel(16);
  414. }
  415. if(VLC->spellh->spells[spell].earth)
  416. if(getSecSkillLevel(17) >= bestslvl)
  417. {
  418. bestslvl = getSecSkillLevel(17);
  419. }
  420. return bestslvl;
  421. }
  422. CGHeroInstance::CGHeroInstance()
  423. {
  424. ID = HEROI_TYPE;
  425. tacticFormationEnabled = inTownGarrison = false;
  426. mana = movement = portrait = level = -1;
  427. isStanding = true;
  428. moveDir = 4;
  429. exp = 0xffffffff;
  430. visitedTown = NULL;
  431. type = NULL;
  432. secSkills.push_back(std::make_pair(-1, -1));
  433. }
  434. void CGHeroInstance::initHero(int SUBID)
  435. {
  436. subID = SUBID;
  437. initHero();
  438. }
  439. void CGHeroInstance::initHero()
  440. {
  441. if(ID == HEROI_TYPE)
  442. initHeroDefInfo();
  443. if(!type)
  444. type = VLC->heroh->heroes[subID];
  445. artifWorn[16] = 3;
  446. if(type->heroType % 2 == 1) //it's a magical hero
  447. {
  448. artifWorn[17] = 0; //give him spellbook
  449. }
  450. if(portrait < 0 || portrait == 255)
  451. portrait = subID;
  452. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  453. {
  454. primSkills.resize(4);
  455. primSkills[0] = type->heroClass->initialAttack;
  456. primSkills[1] = type->heroClass->initialDefence;
  457. primSkills[2] = type->heroClass->initialPower;
  458. primSkills[3] = type->heroClass->initialKnowledge;
  459. }
  460. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  461. secSkills = type->secSkillsInit;
  462. if(mana < 0)
  463. mana = manaLimit();
  464. if (!name.length())
  465. name = type->name;
  466. if (exp == 0xffffffff)
  467. {
  468. exp=40+ (ran()) % 50;
  469. level = 1;
  470. }
  471. else
  472. {
  473. level = VLC->heroh->level(exp);
  474. }
  475. if (!army.slots.size()) //standard army//initial army
  476. {
  477. int pom, pom2=0;
  478. for(int x=0;x<3;x++)
  479. {
  480. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  481. if(pom>=145 && pom<=149) //war machine
  482. {
  483. pom2++;
  484. switch (pom)
  485. {
  486. case 145: //catapult
  487. artifWorn[16] = 3;
  488. break;
  489. default:
  490. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  491. break;
  492. }
  493. continue;
  494. }
  495. army.slots[x-pom2].first = pom;
  496. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  497. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  498. else
  499. army.slots[x-pom2].second = +type->lowStack[x];
  500. army.formation = false;
  501. }
  502. }
  503. hoverName = VLC->generaltexth->allTexts[15];
  504. boost::algorithm::replace_first(hoverName,"%s",name);
  505. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  506. //clear all bonuses from artifacts and give those from artifacts
  507. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  508. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  509. {
  510. CArtifact &art = VLC->arth->artifacts[ari->second];
  511. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  512. bonuses.push_back(*i);
  513. }
  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.c_str());
  656. ret.push_back(std::pair<int,std::string>(1,buf)); //%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) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  704. }
  705. si32 CGHeroInstance::manaRegain() const
  706. {
  707. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  708. }
  709. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type ) const
  710. {
  711. int ret = 0;
  712. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  713. if(i->type == type)
  714. ret += i->val;
  715. return ret;
  716. }
  717. int CGTownInstance::getSightRadious() const //returns sight distance
  718. {
  719. return 5;
  720. }
  721. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  722. {
  723. if((builtBuildings.find(9))!=builtBuildings.end())
  724. return 3;
  725. if((builtBuildings.find(8))!=builtBuildings.end())
  726. return 2;
  727. if((builtBuildings.find(7))!=builtBuildings.end())
  728. return 1;
  729. return 0;
  730. }
  731. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  732. {
  733. if ((builtBuildings.find(13))!=builtBuildings.end())
  734. return 3;
  735. if ((builtBuildings.find(12))!=builtBuildings.end())
  736. return 2;
  737. if ((builtBuildings.find(11))!=builtBuildings.end())
  738. return 1;
  739. if ((builtBuildings.find(10))!=builtBuildings.end())
  740. return 0;
  741. return -1;
  742. }
  743. int CGTownInstance::mageGuildLevel() const
  744. {
  745. if ((builtBuildings.find(4))!=builtBuildings.end())
  746. return 5;
  747. if ((builtBuildings.find(3))!=builtBuildings.end())
  748. return 4;
  749. if ((builtBuildings.find(2))!=builtBuildings.end())
  750. return 3;
  751. if ((builtBuildings.find(1))!=builtBuildings.end())
  752. return 2;
  753. if ((builtBuildings.find(0))!=builtBuildings.end())
  754. return 1;
  755. return 0;
  756. }
  757. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  758. {
  759. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  760. }
  761. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  762. {
  763. return town->hordeLvl[HID];
  764. }
  765. int CGTownInstance::creatureGrowth(const int & level) const
  766. {
  767. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  768. switch(fortLevel())
  769. {
  770. case 3:
  771. ret*=2;break;
  772. case 2:
  773. ret*=(1.5); break;
  774. }
  775. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  776. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  777. if(getHordeLevel(0)==level)
  778. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  779. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  780. if(getHordeLevel(1)==level)
  781. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  782. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  783. return ret;
  784. }
  785. int CGTownInstance::dailyIncome() const
  786. {
  787. int ret = 0;
  788. if ((builtBuildings.find(26))!=builtBuildings.end())
  789. ret+=5000;
  790. if ((builtBuildings.find(13))!=builtBuildings.end())
  791. ret+=4000;
  792. else if ((builtBuildings.find(12))!=builtBuildings.end())
  793. ret+=2000;
  794. else if ((builtBuildings.find(11))!=builtBuildings.end())
  795. ret+=1000;
  796. else if ((builtBuildings.find(10))!=builtBuildings.end())
  797. ret+=500;
  798. return ret;
  799. }
  800. bool CGTownInstance::hasFort() const
  801. {
  802. return (builtBuildings.find(7))!=builtBuildings.end();
  803. }
  804. bool CGTownInstance::hasCapitol() const
  805. {
  806. return (builtBuildings.find(13))!=builtBuildings.end();
  807. }
  808. CGTownInstance::CGTownInstance()
  809. {
  810. builded=-1;
  811. destroyed=-1;
  812. garrisonHero=NULL;
  813. town=NULL;
  814. visitingHero = NULL;
  815. }
  816. CGTownInstance::~CGTownInstance()
  817. {}
  818. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  819. {
  820. if(checkGuild && mageGuildLevel() < level)
  821. return 0;
  822. int ret = 6 - level; //how many spells are available at this level
  823. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  824. ret++;
  825. return ret;
  826. }
  827. bool CGTownInstance::needsLastStack() const
  828. {
  829. if(garrisonHero)
  830. return true;
  831. else return false;
  832. }
  833. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  834. {
  835. if(getOwner() != h->getOwner())
  836. {
  837. return;
  838. }
  839. cb->heroVisitCastle(id,h->id);
  840. }
  841. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  842. {
  843. cb->stopHeroVisitCastle(id,h->id);
  844. }
  845. void CGTownInstance::initObj()
  846. {
  847. MetaString ms;
  848. ms << name << ", " << town->Name();
  849. hoverName = toString(ms);
  850. }
  851. int3 CGTownInstance::getSightCenter() const
  852. {
  853. return pos - int3(2,0,0);
  854. }
  855. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  856. {
  857. if(visitors.find(h->id)==visitors.end())
  858. {
  859. onNAHeroVisit(h->id, false);
  860. if(ID != 102 && ID!=4 && ID!=41) //not tree nor arena nor library of enlightenment
  861. cb->setObjProperty(id,4,h->id); //add to the visitors
  862. }
  863. else
  864. {
  865. onNAHeroVisit(h->id, true);
  866. }
  867. }
  868. void CGVisitableOPH::initObj()
  869. {
  870. if(ID==102)
  871. ttype = ran()%3;
  872. else
  873. ttype = -1;
  874. }
  875. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  876. {
  877. if(result==0) //player agreed to give res for exp
  878. {
  879. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  880. cb->changePrimSkill(heroID,4,expVal); //give exp
  881. cb->setObjProperty(id,4,heroID); //add to the visitors
  882. }
  883. }
  884. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  885. {
  886. int id=0, subid=0, ot=0, val=1;
  887. switch(ID)
  888. {
  889. case 4:
  890. ot = 0;
  891. break;
  892. case 51:
  893. subid=0;
  894. ot=80;
  895. break;
  896. case 23:
  897. subid=1;
  898. ot=39;
  899. break;
  900. case 61:
  901. subid=2;
  902. ot=100;
  903. break;
  904. case 32:
  905. subid=3;
  906. ot=59;
  907. break;
  908. case 100:
  909. id=5;
  910. ot=143;
  911. val=1000;
  912. break;
  913. case 102:
  914. id = 5;
  915. subid = 1;
  916. ot = 146;
  917. val = 1;
  918. break;
  919. case 41:
  920. ot = 66;
  921. break;
  922. }
  923. if (!alreadyVisited)
  924. {
  925. switch (ID)
  926. {
  927. case 4: //arena
  928. {
  929. SelectionDialog sd;
  930. sd.text << std::pair<ui8,ui32>(11,ot);
  931. sd.components.push_back(Component(0,0,2,0));
  932. sd.components.push_back(Component(0,1,2,0));
  933. sd.player = cb->getOwner(heroID);
  934. cb->showSelectionDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  935. return;
  936. }
  937. case 51:
  938. case 23:
  939. case 61:
  940. case 32:
  941. {
  942. cb->changePrimSkill(heroID,subid,val);
  943. InfoWindow iw;
  944. iw.components.push_back(Component(0,subid,val,0));
  945. iw.text << std::pair<ui8,ui32>(11,ot);
  946. iw.player = cb->getOwner(heroID);
  947. cb->showInfoDialog(&iw);
  948. break;
  949. }
  950. case 100: //give exp
  951. {
  952. InfoWindow iw;
  953. iw.components.push_back(Component(id,subid,val,0));
  954. iw.player = cb->getOwner(heroID);
  955. iw.text << std::pair<ui8,ui32>(11,ot);
  956. cb->showInfoDialog(&iw);
  957. cb->changePrimSkill(heroID,4,val);
  958. break;
  959. }
  960. case 102:
  961. {
  962. const CGHeroInstance *h = cb->getHero(heroID);
  963. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  964. if(!ttype)
  965. {
  966. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  967. InfoWindow iw;
  968. iw.components.push_back(Component(id,subid,1,0));
  969. iw.player = cb->getOwner(heroID);
  970. iw.text << std::pair<ui8,ui32>(11,148);
  971. cb->showInfoDialog(&iw);
  972. cb->changePrimSkill(heroID,4,val);
  973. break;
  974. }
  975. else
  976. {
  977. int res, resval;
  978. if(ttype==1)
  979. {
  980. res = 6;
  981. resval = 2000;
  982. ot = 149;
  983. }
  984. else
  985. {
  986. res = 5;
  987. resval = 10;
  988. ot = 151;
  989. }
  990. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  991. {
  992. ot++;
  993. InfoWindow iw;
  994. iw.player = h->tempOwner;
  995. iw.text << std::pair<ui8,ui32>(11,ot);
  996. cb->showInfoDialog(&iw);
  997. return;
  998. }
  999. YesNoDialog sd;
  1000. sd.player = cb->getOwner(heroID);
  1001. sd.text << std::pair<ui8,ui32>(11,ot);
  1002. sd.components.push_back(Component(id,subid,val,0));
  1003. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1004. }
  1005. break;
  1006. }
  1007. case 41:
  1008. {
  1009. const CGHeroInstance *h = cb->getHero(heroID);
  1010. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1011. {
  1012. InfoWindow iw;
  1013. iw.player = cb->getOwner(heroID);
  1014. iw.text << std::pair<ui8,ui32>(11,68);
  1015. cb->showInfoDialog(&iw);
  1016. }
  1017. else
  1018. {
  1019. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1020. cb->changePrimSkill(heroID,0,2);
  1021. cb->changePrimSkill(heroID,1,2);
  1022. cb->changePrimSkill(heroID,2,2);
  1023. cb->changePrimSkill(heroID,3,2);
  1024. InfoWindow iw;
  1025. iw.player = cb->getOwner(heroID);
  1026. iw.text << std::pair<ui8,ui32>(11,66);
  1027. cb->showInfoDialog(&iw);
  1028. }
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. else
  1034. {
  1035. ot++;
  1036. InfoWindow iw;
  1037. iw.player = cb->getOwner(heroID);
  1038. iw.text << std::pair<ui8,ui32>(11,ot);
  1039. cb->showInfoDialog(&iw);
  1040. }
  1041. }
  1042. const std::string & CGVisitableOPH::getHoverText() const
  1043. {
  1044. int pom = -1;
  1045. switch(ID)
  1046. {
  1047. case 4:
  1048. pom = -1;
  1049. break;
  1050. case 51:
  1051. pom = 8;
  1052. break;
  1053. case 23:
  1054. pom = 7;
  1055. break;
  1056. case 61:
  1057. pom = 11;
  1058. break;
  1059. case 32:
  1060. pom = 4;
  1061. break;
  1062. case 100:
  1063. pom = 5;
  1064. break;
  1065. case 102:
  1066. pom = 18;
  1067. break;
  1068. case 41:
  1069. break;
  1070. default:
  1071. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1072. }
  1073. hoverName = VLC->generaltexth->names[ID];
  1074. if(pom >= 0)
  1075. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1076. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1077. if(h)
  1078. {
  1079. hoverName += ' ';
  1080. hoverName += (vstd::contains(visitors,h->id))
  1081. ? (VLC->generaltexth->allTexts[352]) //visited
  1082. : ( VLC->generaltexth->allTexts[353]); //not visited
  1083. }
  1084. return hoverName;
  1085. }
  1086. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1087. {
  1088. cb->setObjProperty(id,4,heroID); //add to the visitors
  1089. cb->changePrimSkill(heroID,primSkill,2);
  1090. }
  1091. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1092. {
  1093. if(what == 4)
  1094. visitors.insert(val);
  1095. }
  1096. bool CArmedInstance::needsLastStack() const
  1097. {
  1098. return false;
  1099. }
  1100. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1101. {
  1102. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1103. }
  1104. void CGCreature::endBattle( BattleResult *result ) const
  1105. {
  1106. if(result->winner==0)
  1107. {
  1108. cb->removeObject(id);
  1109. }
  1110. else
  1111. {
  1112. int killedAmount=0;
  1113. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1114. if(i->first == subID)
  1115. killedAmount += i->second;
  1116. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1117. MetaString ms;
  1118. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1119. pom = 174 + 3*pom + 1;
  1120. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1121. cb->setHoverName(id,&ms);
  1122. }
  1123. }
  1124. void CGCreature::initObj()
  1125. {
  1126. army.slots[0].first = subID;
  1127. si32 &amount = army.slots[0].second;
  1128. CCreature &c = VLC->creh->creatures[subID];
  1129. if(!amount)
  1130. if(c.ammMax == c.ammMin)
  1131. amount = c.ammMax;
  1132. else
  1133. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1134. MetaString ms;
  1135. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1136. pom = 174 + 3*pom + 1;
  1137. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1138. hoverName = toString(ms);
  1139. }
  1140. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1141. {
  1142. if(subID == 7) //TODO: support for abandoned mine
  1143. return;
  1144. if(h->tempOwner == tempOwner) //we're visiting our mine
  1145. return; //TODO: leaving garrison
  1146. //TODO: check if mine is guarded
  1147. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1148. MetaString ms;
  1149. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1150. cb->setHoverName(id,&ms);
  1151. int vv=1; //amount of resource per turn
  1152. if (subID==0 || subID==2)
  1153. vv++;
  1154. else if (subID==6)
  1155. vv = 1000;
  1156. InfoWindow iw;
  1157. iw.text << std::pair<ui8,ui32>(10,subID);
  1158. iw.player = h->tempOwner;
  1159. iw.components.push_back(Component(2,subID,vv,-1));
  1160. cb->showInfoDialog(&iw);
  1161. }
  1162. void CGMine::newTurn() const
  1163. {
  1164. if (tempOwner == NEUTRAL_PLAYER)
  1165. return;
  1166. int vv = 1;
  1167. if (subID==0 || subID==2)
  1168. vv++;
  1169. else if (subID==6)
  1170. vv = 1000;
  1171. cb->giveResource(tempOwner,subID,vv);
  1172. }
  1173. void CGMine::initObj()
  1174. {
  1175. MetaString ms;
  1176. ms << std::pair<ui8,ui32>(9,subID);
  1177. if(tempOwner >= PLAYER_LIMIT)
  1178. tempOwner = NEUTRAL_PLAYER;
  1179. else
  1180. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1181. hoverName = toString(ms);
  1182. }
  1183. void CGResource::initObj()
  1184. {
  1185. blockVisit = true;
  1186. hoverName = VLC->generaltexth->restypes[subID];
  1187. if(!amount)
  1188. {
  1189. switch(subID)
  1190. {
  1191. case 6:
  1192. amount = 500 + (rand()%6)*100;
  1193. break;
  1194. case 0: case 2:
  1195. amount = 6 + (rand()%5);
  1196. break;
  1197. default:
  1198. amount = 3 + (rand()%3);
  1199. break;
  1200. }
  1201. }
  1202. }
  1203. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1204. {
  1205. if(army.slots.size())
  1206. {
  1207. if(message.size())
  1208. {
  1209. YesNoDialog ynd;
  1210. ynd.player = h->getOwner();
  1211. ynd.text << message;
  1212. cb->showYesNoDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1213. }
  1214. else
  1215. {
  1216. fightForRes(0,h);
  1217. }
  1218. }
  1219. else
  1220. {
  1221. if(message.length())
  1222. {
  1223. InfoWindow iw;
  1224. iw.player = h->tempOwner;
  1225. iw.text << message;
  1226. cb->showInfoDialog(&iw);
  1227. }
  1228. collectRes(h->getOwner());
  1229. }
  1230. }
  1231. void CGResource::collectRes( int player ) const
  1232. {
  1233. cb->giveResource(player,subID,amount);
  1234. ShowInInfobox sii;
  1235. sii.player = player;
  1236. sii.c = Component(2,subID,amount,0);
  1237. sii.text << std::pair<ui8,ui32>(11,113);
  1238. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1239. cb->showCompInfo(&sii);
  1240. cb->removeObject(id);
  1241. }
  1242. void CGResource::fightForRes(ui32 refusedFight, const CGHeroInstance *h) const
  1243. {
  1244. if(!refusedFight)
  1245. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1246. }
  1247. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1248. {
  1249. if(result->winner == 0) //attacker won
  1250. collectRes(h->getOwner());
  1251. }
  1252. void CGVisitableOPW::newTurn() const
  1253. {
  1254. if (cb->getDate(1)==1) //first day of week
  1255. {
  1256. cb->setObjProperty(id,5,false);
  1257. MetaString ms; //set text to "not visited"
  1258. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1259. cb->setHoverName(id,&ms);
  1260. }
  1261. }
  1262. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1263. {
  1264. int mid;
  1265. switch (ID)
  1266. {
  1267. case 55:
  1268. mid = 92;
  1269. break;
  1270. case 112:
  1271. mid = 170;
  1272. break;
  1273. case 109:
  1274. mid = 164;
  1275. break;
  1276. }
  1277. if (visited)
  1278. {
  1279. if (ID!=112)
  1280. mid++;
  1281. else
  1282. mid--;
  1283. InfoWindow iw;
  1284. iw.player = h->tempOwner;
  1285. iw.text << std::pair<ui8,ui32>(11,mid);
  1286. cb->showInfoDialog(&iw);
  1287. }
  1288. else
  1289. {
  1290. int type, sub, val;
  1291. type = 2;
  1292. switch (ID)
  1293. {
  1294. case 55:
  1295. if (rand()%2)
  1296. {
  1297. sub = 5;
  1298. val = 5;
  1299. }
  1300. else
  1301. {
  1302. sub = 6;
  1303. val = 500;
  1304. }
  1305. break;
  1306. case 112:
  1307. mid = 170;
  1308. sub = (rand() % 5) + 1;
  1309. val = (rand() % 4) + 3;
  1310. break;
  1311. case 109:
  1312. mid = 164;
  1313. sub = 6;
  1314. if(cb->getDate(0)<8)
  1315. val = 500;
  1316. else
  1317. val = 1000;
  1318. }
  1319. cb->giveResource(h->tempOwner,sub,val);
  1320. InfoWindow iw;
  1321. iw.player = h->tempOwner;
  1322. iw.components.push_back(Component(type,sub,val,0));
  1323. iw.text << std::pair<ui8,ui32>(11,mid);
  1324. cb->showInfoDialog(&iw);
  1325. cb->setObjProperty(id,5,true);
  1326. MetaString ms; //set text to "visited"
  1327. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1328. cb->setHoverName(id,&ms);
  1329. }
  1330. }
  1331. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1332. {
  1333. if(what == 5)
  1334. visited = val;
  1335. }
  1336. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1337. {
  1338. int destinationid=-1;
  1339. switch(ID)
  1340. {
  1341. case 43: //one way - find correspong exit monolith
  1342. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1343. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1344. else
  1345. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1346. break;
  1347. case 45: //two way monolith - pick any other one
  1348. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1349. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1350. else
  1351. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1352. break;
  1353. case 103: //find nearest subterranean gate on the other level
  1354. {
  1355. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1356. for(int i=0; i<objs[103][0].size(); i++)
  1357. {
  1358. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1359. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1360. if(hlp<best.second)
  1361. {
  1362. best.first = objs[103][0][i];
  1363. best.second = hlp;
  1364. }
  1365. }
  1366. if(best.first<0)
  1367. return;
  1368. else
  1369. destinationid = best.first;
  1370. break;
  1371. }
  1372. }
  1373. if(destinationid < 0)
  1374. {
  1375. tlog2 << "Cannot find exit... :( \n";
  1376. return;
  1377. }
  1378. cb->moveHero(h->id,
  1379. (ID!=103)
  1380. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1381. : (cb->getObj(destinationid)->pos),
  1382. true);
  1383. }
  1384. void CGTeleport::initObj()
  1385. {
  1386. objs[ID][subID].push_back(id);
  1387. }
  1388. void CGArtifact::initObj()
  1389. {
  1390. blockVisit = true;
  1391. if(ID == 5)
  1392. hoverName = VLC->arth->artifacts[subID].Name();
  1393. }
  1394. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1395. {
  1396. if(!army.slots.size())
  1397. {
  1398. if(ID == 5)
  1399. {
  1400. InfoWindow iw;
  1401. iw.player = h->tempOwner;
  1402. iw.components.push_back(Component(4,subID,0,0));
  1403. if(message.length())
  1404. iw.text << message;
  1405. else
  1406. iw.text << std::pair<ui8,ui32>(12,subID);
  1407. cb->showInfoDialog(&iw);
  1408. }
  1409. pick(h);
  1410. }
  1411. else
  1412. {
  1413. if(message.size())
  1414. {
  1415. YesNoDialog ynd;
  1416. ynd.player = h->getOwner();
  1417. ynd.text << message;
  1418. cb->showYesNoDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1419. }
  1420. else
  1421. {
  1422. fightForArt(0,h);
  1423. }
  1424. }
  1425. }
  1426. void CGArtifact::pick(const CGHeroInstance * h) const
  1427. {
  1428. if(ID == 5) //Artifact
  1429. {
  1430. cb->giveHeroArtifact(subID,h->id,-2);
  1431. }
  1432. else if(ID == 93) // Spell scroll
  1433. {
  1434. //TODO: support for the spell scroll
  1435. }
  1436. cb->removeObject(id);
  1437. }
  1438. void CGArtifact::fightForArt( ui32 refusedFight, const CGHeroInstance *h ) const
  1439. {
  1440. if(!refusedFight)
  1441. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1442. }
  1443. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1444. {
  1445. if(result->winner == 0) //attacker won
  1446. pick(h);
  1447. }
  1448. void CGPickable::initObj()
  1449. {
  1450. blockVisit = true;
  1451. switch(ID)
  1452. {
  1453. case 12: //campfire
  1454. val2 = (ran()%3) + 4; //4 - 6
  1455. val1 = val2 * 100;
  1456. type = ran()%6; //given resource
  1457. break;
  1458. case 101: //treasure chest
  1459. {
  1460. int hlp = ran()%100;
  1461. if(hlp >= 95)
  1462. {
  1463. type = 1;
  1464. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1465. return;
  1466. }
  1467. else if (hlp >= 65)
  1468. {
  1469. val1 = 2000;
  1470. }
  1471. else if(hlp >= 33)
  1472. {
  1473. val1 = 1500;
  1474. }
  1475. else
  1476. {
  1477. val1 = 1000;
  1478. }
  1479. val2 = val1 - 500;
  1480. type = 0;
  1481. break;
  1482. }
  1483. }
  1484. }
  1485. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1486. {
  1487. switch(ID)
  1488. {
  1489. case 12: //campfire
  1490. {
  1491. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1492. cb->giveResource(h->tempOwner,6,val1);//gold
  1493. InfoWindow iw;
  1494. iw.player = h->tempOwner;
  1495. iw.components.push_back(Component(2,6,val1,0));
  1496. iw.components.push_back(Component(2,type,val2,0));
  1497. iw.text << std::pair<ui8,ui32>(11,23);
  1498. cb->showInfoDialog(&iw);
  1499. break;
  1500. }
  1501. case 101: //treasure chest
  1502. {
  1503. if (subID) //not OH3 treasure chest
  1504. {
  1505. tlog2 << "Not supported WoG treasure chest!\n";
  1506. return;
  1507. }
  1508. if(type) //there is an artifact
  1509. {
  1510. cb->giveHeroArtifact(val1,h->id,-2);
  1511. InfoWindow iw;
  1512. iw.player = h->tempOwner;
  1513. iw.components.push_back(Component(4,val1,1,0));
  1514. iw.text << std::pair<ui8,ui32>(11,145);
  1515. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1516. cb->showInfoDialog(&iw);
  1517. break;
  1518. }
  1519. else
  1520. {
  1521. SelectionDialog sd;
  1522. sd.player = h->tempOwner;
  1523. sd.text << std::pair<ui8,ui32>(11,146);
  1524. sd.components.push_back(Component(2,6,val1,0));
  1525. sd.components.push_back(Component(5,0,val2,0));
  1526. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1527. cb->showSelectionDialog(&sd,fun);
  1528. return;
  1529. }
  1530. }
  1531. }
  1532. cb->removeObject(id);
  1533. }
  1534. void CGPickable::chosen( int which, int heroID ) const
  1535. {
  1536. switch(which)
  1537. {
  1538. case 0: //player pick gold
  1539. cb->giveResource(cb->getOwner(heroID),6,val1);
  1540. break;
  1541. case 1: //player pick exp
  1542. cb->changePrimSkill(heroID, 4, val2);
  1543. break;
  1544. default:
  1545. throw std::string("Unhandled treasure choice");
  1546. }
  1547. cb->removeObject(id);
  1548. }
  1549. void CGWitchHut::initObj()
  1550. {
  1551. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1552. }
  1553. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1554. {
  1555. InfoWindow iw;
  1556. iw.player = h->getOwner();
  1557. if(!hasVisited(h->tempOwner))
  1558. cb->setObjProperty(id,10,h->tempOwner);
  1559. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1560. {
  1561. iw.text << std::pair<ui8,ui32>(11,172);
  1562. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1563. }
  1564. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1565. {
  1566. iw.text << std::pair<ui8,ui32>(11,173);
  1567. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1568. }
  1569. else //give sec skill
  1570. {
  1571. iw.components.push_back(Component(1, ability, 1, 0));
  1572. iw.text << std::pair<ui8,ui32>(11,171);
  1573. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1574. cb->changeSecSkill(h->id,ability,1,true);
  1575. }
  1576. cb->showInfoDialog(&iw);
  1577. }
  1578. const std::string & CGWitchHut::getHoverText() const
  1579. {
  1580. hoverName = VLC->generaltexth->names[ID];
  1581. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1582. {
  1583. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  1584. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  1585. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  1586. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  1587. }
  1588. return hoverName;
  1589. }
  1590. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1591. {
  1592. }
  1593. void CGDwelling::initObj()
  1594. {
  1595. }
  1596. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1597. {
  1598. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1599. int messageID, bonusType, bonusVal;
  1600. int bonusMove = 0;
  1601. InfoWindow iw;
  1602. iw.player = h->tempOwner;
  1603. GiveBonus gbonus;
  1604. gbonus.hid = h->id;
  1605. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1606. gbonus.bonus.source = HeroBonus::OBJECT;
  1607. gbonus.bonus.id = ID;
  1608. switch(ID)
  1609. {
  1610. case 14: //swan pond
  1611. messageID = 29;
  1612. gbonus.bonus.type = HeroBonus::LUCK;
  1613. gbonus.bonus.val = 2;
  1614. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1615. bonusMove = -h->movement;
  1616. break;
  1617. case 28: //Faerie Ring
  1618. messageID = 49;
  1619. gbonus.bonus.type = HeroBonus::LUCK;
  1620. gbonus.bonus.val = 1;
  1621. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1622. break;
  1623. case 30: //fountain of fortune
  1624. messageID = 55;
  1625. gbonus.bonus.type = HeroBonus::LUCK;
  1626. gbonus.bonus.val = rand()%5 - 1;
  1627. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  1628. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  1629. break;
  1630. case 38: //idol of fortune
  1631. messageID = 62;
  1632. if(cb->getDate(1) == 7) //7th day of week
  1633. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1634. else
  1635. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  1636. gbonus.bonus.val = 1;
  1637. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  1638. break;
  1639. case 64: //Rally Flag
  1640. messageID = 111;
  1641. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1642. gbonus.bonus.val = 1;
  1643. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  1644. bonusMove = 400;
  1645. break;
  1646. case 56: //oasis
  1647. messageID = 95;
  1648. gbonus.bonus.type = HeroBonus::MORALE;
  1649. gbonus.bonus.val = 1;
  1650. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  1651. bonusMove = 800;
  1652. break;
  1653. case 96: //temple
  1654. messageID = 140;
  1655. gbonus.bonus.type = HeroBonus::MORALE;
  1656. if(cb->getDate(1)==7) //sunday
  1657. {
  1658. gbonus.bonus.val = 2;
  1659. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  1660. }
  1661. else
  1662. {
  1663. gbonus.bonus.val = 1;
  1664. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  1665. }
  1666. break;
  1667. case 110://Watering Hole
  1668. messageID = 166;
  1669. gbonus.bonus.type = HeroBonus::MORALE;
  1670. gbonus.bonus.val = 1;
  1671. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  1672. bonusMove = 400;
  1673. break;
  1674. case 31: //Fountain of Youth
  1675. messageID = 57;
  1676. gbonus.bonus.type = HeroBonus::MORALE;
  1677. gbonus.bonus.val = 1;
  1678. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  1679. bonusMove = 400;
  1680. break;
  1681. }
  1682. if(visited)
  1683. {
  1684. if(ID==64 || ID==96 || ID==56)
  1685. messageID--;
  1686. else
  1687. messageID++;
  1688. }
  1689. else
  1690. {
  1691. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1692. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  1693. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1694. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  1695. cb->giveHeroBonus(&gbonus);
  1696. if(bonusMove) //swan pond - take all move points
  1697. {
  1698. SetMovePoints smp;
  1699. smp.hid = h->id;
  1700. smp.val = h->movement + bonusMove;
  1701. cb->setMovePoints(&smp);
  1702. }
  1703. }
  1704. iw.text << std::pair<ui8,ui32>(11,messageID);
  1705. cb->showInfoDialog(&iw);
  1706. }
  1707. const std::string & CGBonusingObject::getHoverText() const
  1708. {
  1709. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1710. hoverName = VLC->generaltexth->names[ID];
  1711. if(h)
  1712. {
  1713. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1714. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1715. else
  1716. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1717. }
  1718. return hoverName;
  1719. }
  1720. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  1721. {
  1722. int message;
  1723. InfoWindow iw;
  1724. iw.player = h->tempOwner;
  1725. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  1726. {
  1727. message = 78;
  1728. }
  1729. else if(h->mana < h->manaLimit())
  1730. {
  1731. GiveBonus gbonus;
  1732. gbonus.bonus.type = HeroBonus::NONE;
  1733. gbonus.hid = h->id;
  1734. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  1735. gbonus.bonus.source = HeroBonus::OBJECT;
  1736. gbonus.bonus.id = ID;
  1737. cb->giveHeroBonus(&gbonus);
  1738. cb->setManaPoints(h->id,h->manaLimit());
  1739. message = 77;
  1740. }
  1741. else
  1742. {
  1743. message = 79;
  1744. }
  1745. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  1746. cb->showInfoDialog(&iw);
  1747. }
  1748. const std::string & CGMagicWell::getHoverText() const
  1749. {
  1750. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1751. hoverName = VLC->generaltexth->names[ID];
  1752. if(h)
  1753. {
  1754. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1755. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1756. else
  1757. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1758. }
  1759. return hoverName;
  1760. }
  1761. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  1762. {
  1763. /*if(!(availableFor & (1 << h->tempOwner)))
  1764. return;
  1765. if(cb->getPlayerSettings(h->tempOwner)->human)
  1766. {
  1767. if(humanActivate)
  1768. activated(h);
  1769. }
  1770. else if(computerActivate)
  1771. activated(h);*/
  1772. }
  1773. void CGEvent::endBattle( BattleResult *result ) const
  1774. {
  1775. if(result->winner)
  1776. return;
  1777. //give
  1778. }
  1779. void CGEvent::activated( const CGHeroInstance * h ) const
  1780. {
  1781. InfoWindow iw;
  1782. iw.player = h->tempOwner;
  1783. iw.text << message;
  1784. cb->showInfoDialog(&iw);
  1785. if(guarders)
  1786. cb->startBattleI(h->id,guarders,pos,boost::bind(&CGEvent::endBattle,this,_1));
  1787. cb->removeObject(id);
  1788. }
  1789. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  1790. {
  1791. FoWChange fw;
  1792. fw.player = h->tempOwner;
  1793. fw.mode = 1;
  1794. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  1795. cb->sendAndApply(&fw);
  1796. InfoWindow iw;
  1797. iw.player = h->tempOwner;
  1798. iw.text.addTxt(MetaString::ADVOB_TXT,98);
  1799. cb->showInfoDialog(&iw);
  1800. }
  1801. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  1802. {
  1803. if(spell == 255)
  1804. {
  1805. tlog1 << "Not initialized shrine visited!\n";
  1806. return;
  1807. }
  1808. if(!hasVisited(h->tempOwner))
  1809. cb->setObjProperty(id,10,h->tempOwner);
  1810. InfoWindow iw;
  1811. iw.player = h->getOwner();
  1812. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  1813. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  1814. iw.text << ".";
  1815. if(!h->getArt(17)) //no spellbook
  1816. {
  1817. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  1818. }
  1819. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  1820. {
  1821. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  1822. }
  1823. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  1824. {
  1825. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  1826. }
  1827. else //give spell
  1828. {
  1829. std::set<ui32> spells;
  1830. spells.insert(spell);
  1831. cb->changeSpells(h->id,true,spells);
  1832. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  1833. }
  1834. cb->showInfoDialog(&iw);
  1835. }
  1836. void CGShrine::initObj()
  1837. {
  1838. if(spell == 255) //spell not set
  1839. {
  1840. int level = ID-87;
  1841. std::vector<ui32> possibilities;
  1842. //add all allowed spells of wanted level
  1843. for(int i=0; i<SPELLS_QUANTITY; i++)
  1844. {
  1845. if(VLC->spellh->spells[i].level == level
  1846. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  1847. {
  1848. possibilities.push_back(VLC->spellh->spells[i].id);
  1849. }
  1850. }
  1851. if(!possibilities.size())
  1852. {
  1853. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  1854. return;
  1855. }
  1856. spell = possibilities[ran() % possibilities.size()];
  1857. }
  1858. }
  1859. const std::string & CGShrine::getHoverText() const
  1860. {
  1861. hoverName = VLC->generaltexth->names[ID];
  1862. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1863. {
  1864. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  1865. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  1866. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1867. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  1868. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  1869. }
  1870. return hoverName;
  1871. }
  1872. void CGSignBottle::initObj()
  1873. {
  1874. //if no text is set than we pick random from the predefined ones
  1875. if(!message.size())
  1876. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  1877. }
  1878. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  1879. {
  1880. InfoWindow iw;
  1881. iw.player = h->getOwner();
  1882. iw.text << message;
  1883. cb->showInfoDialog(&iw);
  1884. }