CObjectHandler.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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/random/linear_congruential.hpp>
  13. #include "CTownHandler.h"
  14. #include "CArtHandler.h"
  15. #include "../lib/VCMI_Lib.h"
  16. #include "../lib/IGameCallback.h"
  17. #include "../CGameState.h"
  18. #include "../lib/NetPacks.h"
  19. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  20. IGameCallback * IObjectInterface::cb = NULL;
  21. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  22. extern CLodHandler * bitmaph;
  23. extern boost::rand48 ran;
  24. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  25. {};
  26. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  27. {};
  28. void IObjectInterface::newTurn () const
  29. {};
  30. IObjectInterface::~IObjectInterface()
  31. {}
  32. IObjectInterface::IObjectInterface()
  33. {}
  34. void IObjectInterface::initObj()
  35. {}
  36. void CObjectHandler::loadObjects()
  37. {
  38. tlog5 << "\t\tReading cregens \n";
  39. cregens.resize(110); //TODO: hardcoded value - change
  40. for(size_t i=0; i < cregens.size(); ++i)
  41. {
  42. cregens[i]=-1;
  43. }
  44. std::ifstream ifs("config/cregens.txt");
  45. while(!ifs.eof())
  46. {
  47. int dw, cr;
  48. ifs >> dw >> cr;
  49. cregens[dw]=cr;
  50. }
  51. ifs.close();
  52. ifs.clear();
  53. tlog5 << "\t\tDone loading objects!\n";
  54. }
  55. int CGObjectInstance::getOwner() const
  56. {
  57. //if (state)
  58. // return state->owner;
  59. //else
  60. return tempOwner; //won't have owner
  61. }
  62. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  63. {
  64. pos = int3(-1,-1,-1);
  65. //std::cout << "Tworze obiekt "<<this<<std::endl;
  66. //state = new CLuaObjectScript();
  67. ID = subID = id = -1;
  68. defInfo = NULL;
  69. info = NULL;
  70. tempOwner = 254;
  71. blockVisit = false;
  72. }
  73. CGObjectInstance::~CGObjectInstance()
  74. {
  75. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  76. //if (state)
  77. // delete state;
  78. //state=NULL;
  79. }
  80. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  81. //{
  82. // pos = right.pos;
  83. // ID = right.ID;
  84. // subID = right.subID;
  85. // id = right.id;
  86. // defInfo = right.defInfo;
  87. // info = right.info;
  88. // blockVisit = right.blockVisit;
  89. // //state = new CLuaObjectScript(right.state->);
  90. // //*state = *right.state;
  91. // //state = right.state;
  92. // tempOwner = right.tempOwner;
  93. //}
  94. //CGObjectInstance& CGObjectInstance::operator=(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();
  104. // //*state = *right.state;
  105. // tempOwner = right.tempOwner;
  106. // return *this;
  107. //}
  108. const std::string & CGObjectInstance::getHoverText() const
  109. {
  110. return hoverName;
  111. }
  112. void CGObjectInstance::setOwner(int ow)
  113. {
  114. //if (state)
  115. // state->owner = ow;
  116. //else
  117. tempOwner = ow;
  118. }
  119. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  120. {
  121. return defInfo->width;
  122. }
  123. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  124. {
  125. return defInfo->width;
  126. }
  127. 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)
  128. {
  129. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  130. return false;
  131. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  132. return true;
  133. return false;
  134. }
  135. bool CGObjectInstance::blockingAt(int x, int y) const
  136. {
  137. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  138. return false;
  139. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  140. return true;
  141. return false;
  142. }
  143. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  144. {
  145. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  146. return true;
  147. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  148. return false;
  149. if(this->pos.y<cmp.pos.y)
  150. return true;
  151. if(this->pos.y>cmp.pos.y)
  152. return false;
  153. if(cmp.ID==34 && ID!=34)
  154. return true;
  155. if(cmp.ID!=34 && ID==34)
  156. return false;
  157. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  158. return true;
  159. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  160. return false;
  161. if(this->pos.x<cmp.pos.x)
  162. return true;
  163. return false;
  164. }
  165. void CGObjectInstance::initObj()
  166. {
  167. }
  168. int lowestSpeed(const CGHeroInstance * chi)
  169. {
  170. if(!chi->army.slots.size())
  171. {
  172. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  173. return 20;
  174. }
  175. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  176. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  177. for (;i!=chi->army.slots.end();i++)
  178. {
  179. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  180. }
  181. return ret;
  182. }
  183. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  184. {
  185. unsigned int ret = type->heroClass->terrCosts[ttype];
  186. //applying pathfinding skill
  187. switch(getSecSkillLevel(0))
  188. {
  189. case 1: //basic
  190. switch(ttype)
  191. {
  192. case rough:
  193. ret = 100;
  194. break;
  195. case sand: case snow:
  196. if(ret>125)
  197. ret = 125;
  198. break;
  199. case swamp:
  200. if(ret>150)
  201. ret = 150;
  202. break;
  203. default:
  204. //TODO do something nasty here throw maybe? or some def value asing
  205. break;
  206. }
  207. break;
  208. case 2: //advanced
  209. switch(ttype)
  210. {
  211. case rough:
  212. case sand:
  213. case snow:
  214. ret = 100;
  215. break;
  216. case swamp:
  217. if(ret>125)
  218. ret = 125;
  219. break;
  220. default:
  221. //TODO look up
  222. break;
  223. }
  224. break;
  225. case 3: //expert
  226. ret = 100;
  227. break;
  228. default:
  229. //TODO look up
  230. break;
  231. }
  232. //calculating road influence
  233. switch(rdtype)
  234. {
  235. case dirtRoad:
  236. ret*=0.75;
  237. break;
  238. case grazvelRoad:
  239. ret*=0.667;
  240. break;
  241. case cobblestoneRoad:
  242. ret*=0.5;
  243. break;
  244. default:
  245. //TODO killllll me
  246. break;
  247. }
  248. return ret;
  249. }
  250. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  251. {
  252. unsigned int sl = 100;
  253. for(size_t h=0; h < army.slots.size(); ++h)
  254. {
  255. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  256. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  257. }
  258. return sl;
  259. }
  260. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  261. {
  262. if (toh3m)
  263. {
  264. src.x+=1;
  265. return src;
  266. }
  267. else
  268. {
  269. src.x-=1;
  270. return src;
  271. }
  272. }
  273. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  274. {
  275. if (h3m)
  276. {
  277. return pos;
  278. }
  279. else
  280. {
  281. return convertPosition(pos,false);
  282. }
  283. }
  284. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  285. {
  286. return 6 + getSecSkillLevel(3); //default + scouting
  287. }
  288. si32 CGHeroInstance::manaLimit() const
  289. {
  290. double modifier = 1.0;
  291. switch(getSecSkillLevel(24)) //intelligence level
  292. {
  293. case 1: modifier+=0.25; break;
  294. case 2: modifier+=0.5; break;
  295. case 3: modifier+=1.0; break;
  296. }
  297. return 10*getPrimSkillLevel(3)*modifier;
  298. }
  299. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  300. //{
  301. // if (h3m)
  302. // pos = Pos;
  303. // else
  304. // pos = convertPosition(Pos,true);
  305. //}
  306. bool CGHeroInstance::canWalkOnSea() const
  307. {
  308. //TODO: write it - it should check if hero is flying, or something similiar
  309. return false;
  310. }
  311. int CGHeroInstance::getCurrentLuck() const
  312. {
  313. //TODO: write it
  314. return 0;
  315. }
  316. int CGHeroInstance::getCurrentMorale() const
  317. {
  318. //TODO: write it
  319. return 0;
  320. }
  321. int CGHeroInstance::getPrimSkillLevel(int id) const
  322. {
  323. return primSkills[id];
  324. }
  325. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  326. {
  327. for(size_t i=0; i < secSkills.size(); ++i)
  328. if(secSkills[i].first==ID)
  329. return secSkills[i].second;
  330. return 0;
  331. }
  332. int CGHeroInstance::maxMovePoints(bool onLand) const
  333. {
  334. int ret = 1270+70*lowestSpeed(this);
  335. if (ret>2000)
  336. ret=2000;
  337. if(onLand)
  338. {
  339. //logistics:
  340. switch(getSecSkillLevel(2))
  341. {
  342. case 1:
  343. ret *= 1.1f;
  344. break;
  345. case 2:
  346. ret *= 1.2f;
  347. break;
  348. case 3:
  349. ret *= 1.3f;
  350. break;
  351. }
  352. }
  353. else
  354. {
  355. //navigation:
  356. switch(getSecSkillLevel(2))
  357. {
  358. case 1:
  359. ret *= 1.5f;
  360. break;
  361. case 2:
  362. ret *= 2.0f;
  363. break;
  364. case 3:
  365. ret *= 2.5f;
  366. break;
  367. }
  368. }
  369. return ret;
  370. }
  371. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  372. {
  373. if(pos<19)
  374. if(vstd::contains(artifWorn,pos))
  375. return artifWorn.find(pos)->second;
  376. else
  377. return -1;
  378. else
  379. if(pos-19 < artifacts.size())
  380. return artifacts[pos-19];
  381. else
  382. return -1;
  383. }
  384. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  385. {
  386. if(art<0)
  387. {
  388. if(pos<19)
  389. artifWorn.erase(pos);
  390. else
  391. artifacts -= artifacts[pos-19];
  392. }
  393. else
  394. {
  395. if(pos<19)
  396. artifWorn[pos] = art;
  397. else
  398. if(pos-19 < artifacts.size())
  399. artifacts[pos-19] = art;
  400. else
  401. artifacts.push_back(art);
  402. }
  403. }
  404. const CArtifact * CGHeroInstance::getArt(int pos) const
  405. {
  406. int id = getArtAtPos(pos);
  407. if(id>=0)
  408. return &VLC->arth->artifacts[id];
  409. else
  410. return NULL;
  411. }
  412. int CGHeroInstance::getSpellSecLevel(int spell) const
  413. {
  414. int bestslvl = 0;
  415. if(VLC->spellh->spells[spell].air)
  416. if(getSecSkillLevel(15) >= bestslvl)
  417. {
  418. bestslvl = getSecSkillLevel(15);
  419. }
  420. if(VLC->spellh->spells[spell].fire)
  421. if(getSecSkillLevel(14) >= bestslvl)
  422. {
  423. bestslvl = getSecSkillLevel(14);
  424. }
  425. if(VLC->spellh->spells[spell].water)
  426. if(getSecSkillLevel(16) >= bestslvl)
  427. {
  428. bestslvl = getSecSkillLevel(16);
  429. }
  430. if(VLC->spellh->spells[spell].earth)
  431. if(getSecSkillLevel(17) >= bestslvl)
  432. {
  433. bestslvl = getSecSkillLevel(17);
  434. }
  435. return bestslvl;
  436. }
  437. CGHeroInstance::CGHeroInstance()
  438. {
  439. ID = 34;
  440. tacticFormationEnabled = inTownGarrison = false;
  441. mana = movement = portrait = level = -1;
  442. isStanding = true;
  443. moveDir = 4;
  444. exp = 0xffffffff;
  445. visitedTown = NULL;
  446. type = NULL;
  447. secSkills.push_back(std::make_pair(-1, -1));
  448. }
  449. void CGHeroInstance::initHero(int SUBID)
  450. {
  451. subID = SUBID;
  452. initHero();
  453. }
  454. void CGHeroInstance::initHero()
  455. {
  456. if(!defInfo)
  457. {
  458. defInfo = new CGDefInfo();
  459. defInfo->id = 34;
  460. defInfo->subid = subID;
  461. defInfo->printPriority = 0;
  462. defInfo->visitDir = 0xff;
  463. }
  464. if(!type)
  465. type = VLC->heroh->heroes[subID];
  466. for(int i=0;i<6;i++)
  467. {
  468. defInfo->blockMap[i]=255;
  469. defInfo->visitMap[i]=0;
  470. }
  471. defInfo->handler=NULL;
  472. defInfo->blockMap[5] = 253;
  473. defInfo->visitMap[5] = 2;
  474. artifWorn[16] = 3;
  475. if(type->heroType % 2 == 1) //it's a magical hero
  476. {
  477. artifWorn[17] = 0; //give him spellbook
  478. }
  479. if(portrait < 0 || portrait == 255)
  480. portrait = subID;
  481. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  482. {
  483. primSkills.resize(4);
  484. primSkills[0] = type->heroClass->initialAttack;
  485. primSkills[1] = type->heroClass->initialDefence;
  486. primSkills[2] = type->heroClass->initialPower;
  487. primSkills[3] = type->heroClass->initialKnowledge;
  488. }
  489. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  490. secSkills = type->secSkillsInit;
  491. if(mana < 0)
  492. mana = manaLimit();
  493. if (!name.length())
  494. name = type->name;
  495. if (exp == 0xffffffff)
  496. {
  497. exp=40+ (ran()) % 50;
  498. level = 1;
  499. }
  500. else
  501. {
  502. level = VLC->heroh->level(exp);
  503. }
  504. if (!army.slots.size()) //standard army//initial army
  505. {
  506. int pom, pom2=0;
  507. for(int x=0;x<3;x++)
  508. {
  509. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  510. if(pom>=145 && pom<=149) //war machine
  511. {
  512. pom2++;
  513. switch (pom)
  514. {
  515. case 145: //catapult
  516. artifWorn[16] = 3;
  517. break;
  518. default:
  519. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  520. break;
  521. }
  522. continue;
  523. }
  524. army.slots[x-pom2].first = pom;
  525. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  526. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  527. else
  528. army.slots[x-pom2].second = +type->lowStack[x];
  529. army.formation = false;
  530. }
  531. }
  532. hoverName = VLC->generaltexth->allTexts[15];
  533. boost::algorithm::replace_first(hoverName,"%s",name);
  534. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  535. }
  536. CGHeroInstance::~CGHeroInstance()
  537. {
  538. }
  539. bool CGHeroInstance::needsLastStack() const
  540. {
  541. return true;
  542. }
  543. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  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. const std::string & CGHeroInstance::getBiography() const
  562. {
  563. if (biography.length())
  564. return biography;
  565. else
  566. return VLC->generaltexth->hTxts[subID].biography;
  567. }
  568. void CGHeroInstance::initObj()
  569. {
  570. blockVisit = true;
  571. }
  572. int CGTownInstance::getSightDistance() const //returns sight distance
  573. {
  574. return 10;
  575. }
  576. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  577. {
  578. if((builtBuildings.find(9))!=builtBuildings.end())
  579. return 3;
  580. if((builtBuildings.find(8))!=builtBuildings.end())
  581. return 2;
  582. if((builtBuildings.find(7))!=builtBuildings.end())
  583. return 1;
  584. return 0;
  585. }
  586. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  587. {
  588. if ((builtBuildings.find(13))!=builtBuildings.end())
  589. return 3;
  590. if ((builtBuildings.find(12))!=builtBuildings.end())
  591. return 2;
  592. if ((builtBuildings.find(11))!=builtBuildings.end())
  593. return 1;
  594. if ((builtBuildings.find(10))!=builtBuildings.end())
  595. return 0;
  596. return -1;
  597. }
  598. int CGTownInstance::mageGuildLevel() const
  599. {
  600. if ((builtBuildings.find(4))!=builtBuildings.end())
  601. return 5;
  602. if ((builtBuildings.find(3))!=builtBuildings.end())
  603. return 4;
  604. if ((builtBuildings.find(2))!=builtBuildings.end())
  605. return 3;
  606. if ((builtBuildings.find(1))!=builtBuildings.end())
  607. return 2;
  608. if ((builtBuildings.find(0))!=builtBuildings.end())
  609. return 1;
  610. return 0;
  611. }
  612. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  613. {
  614. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  615. }
  616. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  617. {
  618. return town->hordeLvl[HID];
  619. }
  620. int CGTownInstance::creatureGrowth(const int & level) const
  621. {
  622. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  623. switch(fortLevel())
  624. {
  625. case 3:
  626. ret*=2;break;
  627. case 2:
  628. ret*=(1.5); break;
  629. }
  630. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  631. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  632. if(getHordeLevel(0)==level)
  633. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  634. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  635. if(getHordeLevel(1)==level)
  636. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  637. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  638. return ret;
  639. }
  640. int CGTownInstance::dailyIncome() const
  641. {
  642. int ret = 0;
  643. if ((builtBuildings.find(26))!=builtBuildings.end())
  644. ret+=5000;
  645. if ((builtBuildings.find(13))!=builtBuildings.end())
  646. ret+=4000;
  647. else if ((builtBuildings.find(12))!=builtBuildings.end())
  648. ret+=2000;
  649. else if ((builtBuildings.find(11))!=builtBuildings.end())
  650. ret+=1000;
  651. else if ((builtBuildings.find(10))!=builtBuildings.end())
  652. ret+=500;
  653. return ret;
  654. }
  655. bool CGTownInstance::hasFort() const
  656. {
  657. return (builtBuildings.find(7))!=builtBuildings.end();
  658. }
  659. bool CGTownInstance::hasCapitol() const
  660. {
  661. return (builtBuildings.find(13))!=builtBuildings.end();
  662. }
  663. CGTownInstance::CGTownInstance()
  664. {
  665. builded=-1;
  666. destroyed=-1;
  667. garrisonHero=NULL;
  668. town=NULL;
  669. visitingHero = NULL;
  670. }
  671. CGTownInstance::~CGTownInstance()
  672. {}
  673. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  674. {
  675. if(checkGuild && mageGuildLevel() < level)
  676. return 0;
  677. int ret = 6 - level; //how many spells are available at this level
  678. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  679. ret++;
  680. return ret;
  681. }
  682. bool CGTownInstance::needsLastStack() const
  683. {
  684. if(garrisonHero)
  685. return true;
  686. else return false;
  687. }
  688. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  689. {
  690. if(getOwner() != h->getOwner())
  691. {
  692. return;
  693. }
  694. cb->heroVisitCastle(id,h->id);
  695. }
  696. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  697. {
  698. cb->stopHeroVisitCastle(id,h->id);
  699. }
  700. void CGTownInstance::initObj()
  701. {
  702. MetaString ms;
  703. ms << name << ", " << town->Name();
  704. hoverName = toString(ms);
  705. }
  706. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  707. {
  708. if(visitors.find(h->id)==visitors.end())
  709. {
  710. onNAHeroVisit(h->id, false);
  711. if(ID != 102 && ID!=4) //not tree nor arena
  712. cb->setObjProperty(id,4,h->id); //add to the visitors
  713. }
  714. else
  715. {
  716. onNAHeroVisit(h->id, true);
  717. }
  718. }
  719. void CGVisitableOPH::initObj()
  720. {
  721. if(ID==102)
  722. ttype = ran()%3;
  723. else
  724. ttype = -1;
  725. }
  726. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  727. {
  728. if(result==0) //player agreed to give res for exp
  729. {
  730. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  731. cb->changePrimSkill(heroID,4,expVal); //give exp
  732. cb->setObjProperty(id,4,heroID); //add to the visitors
  733. }
  734. }
  735. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  736. {
  737. int id=0, subid=0, ot=0, val=1;
  738. switch(ID)
  739. {
  740. case 4:
  741. ot = 0;
  742. break;
  743. case 51:
  744. subid=0;
  745. ot=80;
  746. break;
  747. case 23:
  748. subid=1;
  749. ot=39;
  750. break;
  751. case 61:
  752. subid=2;
  753. ot=100;
  754. break;
  755. case 32:
  756. subid=3;
  757. ot=59;
  758. break;
  759. case 100:
  760. id=5;
  761. ot=143;
  762. val=1000;
  763. break;
  764. case 102:
  765. id = 5;
  766. subid = 1;
  767. ot = 146;
  768. val = 1;
  769. break;
  770. }
  771. if (!alreadyVisited)
  772. {
  773. switch (ID)
  774. {
  775. case 4: //arena
  776. {
  777. SelectionDialog sd;
  778. sd.text << std::pair<ui8,ui32>(11,ot);
  779. sd.components.push_back(Component(0,0,2,0));
  780. sd.components.push_back(Component(0,1,2,0));
  781. sd.player = cb->getOwner(heroID);
  782. cb->showSelectionDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  783. return;
  784. }
  785. case 51:
  786. case 23:
  787. case 61:
  788. case 32:
  789. {
  790. cb->changePrimSkill(heroID,subid,val);
  791. InfoWindow iw;
  792. iw.components.push_back(Component(0,subid,val,0));
  793. iw.text << std::pair<ui8,ui32>(11,ot);
  794. iw.player = cb->getOwner(heroID);
  795. cb->showInfoDialog(&iw);
  796. break;
  797. }
  798. case 100: //give exp
  799. {
  800. InfoWindow iw;
  801. iw.components.push_back(Component(id,subid,val,0));
  802. iw.player = cb->getOwner(heroID);
  803. iw.text << std::pair<ui8,ui32>(11,ot);
  804. cb->showInfoDialog(&iw);
  805. cb->changePrimSkill(heroID,4,val);
  806. break;
  807. }
  808. case 102:
  809. {
  810. const CGHeroInstance *h = cb->getHero(heroID);
  811. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  812. if(!ttype)
  813. {
  814. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  815. InfoWindow iw;
  816. iw.components.push_back(Component(id,subid,1,0));
  817. iw.player = cb->getOwner(heroID);
  818. iw.text << std::pair<ui8,ui32>(11,148);
  819. cb->showInfoDialog(&iw);
  820. cb->changePrimSkill(heroID,4,val);
  821. break;
  822. }
  823. else
  824. {
  825. int res, resval;
  826. if(ttype==1)
  827. {
  828. res = 6;
  829. resval = 2000;
  830. ot = 149;
  831. }
  832. else
  833. {
  834. res = 5;
  835. resval = 10;
  836. ot = 151;
  837. }
  838. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  839. {
  840. ot++;
  841. InfoWindow iw;
  842. iw.player = h->tempOwner;
  843. iw.text << std::pair<ui8,ui32>(11,ot);
  844. cb->showInfoDialog(&iw);
  845. return;
  846. }
  847. YesNoDialog sd;
  848. sd.player = cb->getOwner(heroID);
  849. sd.text << std::pair<ui8,ui32>(11,ot);
  850. sd.components.push_back(Component(id,subid,val,0));
  851. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  852. }
  853. break;
  854. }
  855. }
  856. }
  857. else
  858. {
  859. ot++;
  860. InfoWindow iw;
  861. iw.player = cb->getOwner(heroID);
  862. iw.text << std::pair<ui8,ui32>(11,ot);
  863. cb->showInfoDialog(&iw);
  864. }
  865. }
  866. const std::string & CGVisitableOPH::getHoverText() const
  867. {
  868. int pom = -1;
  869. switch(ID)
  870. {
  871. case 4:
  872. pom = -1;
  873. break;
  874. case 51:
  875. pom = 8;
  876. break;
  877. case 23:
  878. pom = 7;
  879. break;
  880. case 61:
  881. pom = 11;
  882. break;
  883. case 32:
  884. pom = 4;
  885. break;
  886. case 100:
  887. pom = 5;
  888. break;
  889. case 102:
  890. pom = 18;
  891. break;
  892. default:
  893. throw std::string("Wrong CGVisitableOPH object ID!\n");
  894. }
  895. hoverName = VLC->generaltexth->names[ID];
  896. if(pom >= 0)
  897. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  898. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  899. if(h)
  900. {
  901. hoverName += ' ';
  902. hoverName += (vstd::contains(visitors,h->id))
  903. ? (VLC->generaltexth->allTexts[352]) //visited
  904. : ( VLC->generaltexth->allTexts[353]); //not visited
  905. }
  906. return hoverName;
  907. }
  908. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  909. {
  910. cb->setObjProperty(id,4,heroID); //add to the visitors
  911. cb->changePrimSkill(heroID,primSkill,2);
  912. }
  913. bool CArmedInstance::needsLastStack() const
  914. {
  915. return false;
  916. }
  917. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  918. {
  919. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  920. }
  921. void CGCreature::endBattle( BattleResult *result ) const
  922. {
  923. if(result->winner==0)
  924. {
  925. cb->removeObject(id);
  926. }
  927. else
  928. {
  929. int killedAmount=0;
  930. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  931. if(i->first == subID)
  932. killedAmount += i->second;
  933. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  934. MetaString ms;
  935. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  936. pom = 174 + 3*pom + 1;
  937. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  938. cb->setHoverName(id,&ms);
  939. }
  940. }
  941. void CGCreature::initObj()
  942. {
  943. army.slots[0].first = subID;
  944. si32 &amount = army.slots[0].second;
  945. CCreature &c = VLC->creh->creatures[subID];
  946. if(!amount)
  947. if(c.ammMax == c.ammMin)
  948. amount = c.ammMax;
  949. else
  950. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  951. MetaString ms;
  952. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  953. pom = 174 + 3*pom + 1;
  954. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  955. hoverName = toString(ms);
  956. }
  957. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  958. {
  959. if(subID == 7) //TODO: support for abandoned mine
  960. return;
  961. if(h->tempOwner == tempOwner) //we're visiting our mine
  962. return; //TODO: leaving garrison
  963. //TODO: check if mine is guarded
  964. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  965. MetaString ms;
  966. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  967. cb->setHoverName(id,&ms);
  968. int vv=1; //amount of resource per turn
  969. if (subID==0 || subID==2)
  970. vv++;
  971. else if (subID==6)
  972. vv = 1000;
  973. InfoWindow iw;
  974. iw.text << std::pair<ui8,ui32>(10,subID);
  975. iw.player = h->tempOwner;
  976. iw.components.push_back(Component(2,subID,vv,-1));
  977. cb->showInfoDialog(&iw);
  978. }
  979. void CGMine::newTurn() const
  980. {
  981. if (tempOwner == NEUTRAL_PLAYER)
  982. return;
  983. int vv = 1;
  984. if (subID==0 || subID==2)
  985. vv++;
  986. else if (subID==6)
  987. vv = 1000;
  988. cb->giveResource(tempOwner,subID,vv);
  989. }
  990. void CGMine::initObj()
  991. {
  992. MetaString ms;
  993. ms << std::pair<ui8,ui32>(9,subID);
  994. if(tempOwner >= PLAYER_LIMIT)
  995. tempOwner = NEUTRAL_PLAYER;
  996. else
  997. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  998. hoverName = toString(ms);
  999. }
  1000. void CGResource::initObj()
  1001. {
  1002. blockVisit = true;
  1003. hoverName = VLC->generaltexth->restypes[subID];
  1004. if(!amount)
  1005. {
  1006. switch(subID)
  1007. {
  1008. case 6:
  1009. amount = 500 + (rand()%6)*100;
  1010. break;
  1011. case 0: case 2:
  1012. amount = 6 + (rand()%5);
  1013. break;
  1014. default:
  1015. amount = 3 + (rand()%3);
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1021. {
  1022. if(army.slots.size())
  1023. {
  1024. if(message.size())
  1025. {
  1026. YesNoDialog ynd;
  1027. ynd.player = h->getOwner();
  1028. ynd.text << message;
  1029. cb->showYesNoDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1030. }
  1031. else
  1032. {
  1033. fightForRes(0,h);
  1034. }
  1035. }
  1036. else
  1037. {
  1038. if(message.length())
  1039. {
  1040. InfoWindow iw;
  1041. iw.player = h->tempOwner;
  1042. iw.text << message;
  1043. cb->showInfoDialog(&iw);
  1044. }
  1045. collectRes(h->getOwner());
  1046. }
  1047. }
  1048. void CGResource::collectRes( int player ) const
  1049. {
  1050. cb->giveResource(player,subID,amount);
  1051. ShowInInfobox sii;
  1052. sii.player = player;
  1053. sii.c = Component(2,subID,amount,0);
  1054. sii.text << std::pair<ui8,ui32>(11,113);
  1055. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1056. cb->showCompInfo(&sii);
  1057. cb->removeObject(id);
  1058. }
  1059. void CGResource::fightForRes(ui32 refusedFight, const CGHeroInstance *h) const
  1060. {
  1061. if(refusedFight)
  1062. return;
  1063. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1064. }
  1065. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1066. {
  1067. if(result->winner == 0) //attacker won
  1068. collectRes(h->getOwner());
  1069. }
  1070. void CGVisitableOPW::newTurn() const
  1071. {
  1072. if (cb->getDate(1)==1) //first day of week
  1073. {
  1074. cb->setObjProperty(id,5,false);
  1075. MetaString ms; //set text to "not visited"
  1076. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1077. cb->setHoverName(id,&ms);
  1078. }
  1079. }
  1080. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1081. {
  1082. int mid;
  1083. switch (ID)
  1084. {
  1085. case 55:
  1086. mid = 92;
  1087. break;
  1088. case 112:
  1089. mid = 170;
  1090. break;
  1091. case 109:
  1092. mid = 164;
  1093. break;
  1094. }
  1095. if (visited)
  1096. {
  1097. if (ID!=112)
  1098. mid++;
  1099. else
  1100. mid--;
  1101. InfoWindow iw;
  1102. iw.player = h->tempOwner;
  1103. iw.text << std::pair<ui8,ui32>(11,mid);
  1104. cb->showInfoDialog(&iw);
  1105. }
  1106. else
  1107. {
  1108. int type, sub, val;
  1109. type = 2;
  1110. switch (ID)
  1111. {
  1112. case 55:
  1113. if (rand()%2)
  1114. {
  1115. sub = 5;
  1116. val = 5;
  1117. }
  1118. else
  1119. {
  1120. sub = 6;
  1121. val = 500;
  1122. }
  1123. break;
  1124. case 112:
  1125. mid = 170;
  1126. sub = (rand() % 5) + 1;
  1127. val = (rand() % 4) + 3;
  1128. break;
  1129. case 109:
  1130. mid = 164;
  1131. sub = 6;
  1132. if(cb->getDate(2)<2)
  1133. val = 500;
  1134. else
  1135. val = 1000;
  1136. }
  1137. cb->giveResource(h->tempOwner,sub,val);
  1138. InfoWindow iw;
  1139. iw.player = h->tempOwner;
  1140. iw.components.push_back(Component(type,sub,val,0));
  1141. iw.text << std::pair<ui8,ui32>(11,mid);
  1142. cb->showInfoDialog(&iw);
  1143. cb->setObjProperty(id,5,true);
  1144. MetaString ms; //set text to "visited"
  1145. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1146. cb->setHoverName(id,&ms);
  1147. }
  1148. }
  1149. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1150. {
  1151. int destinationid=-1;
  1152. switch(ID)
  1153. {
  1154. case 43: //one way - find correspong exit monolith
  1155. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1156. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1157. else
  1158. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1159. break;
  1160. case 45: //two way monolith - pick any other one
  1161. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1162. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1163. else
  1164. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1165. break;
  1166. case 103: //find nearest subterranean gate on the other level
  1167. {
  1168. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1169. for(int i=0; i<objs[103][0].size(); i++)
  1170. {
  1171. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1172. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1173. if(hlp<best.second)
  1174. {
  1175. best.first = objs[103][0][i];
  1176. best.second = hlp;
  1177. }
  1178. }
  1179. if(best.first<0)
  1180. return;
  1181. else
  1182. destinationid = best.first;
  1183. break;
  1184. }
  1185. }
  1186. if(destinationid < 0)
  1187. {
  1188. tlog2 << "Cannot find exit... :( \n";
  1189. return;
  1190. }
  1191. cb->moveHero(h->id,
  1192. (ID!=103)
  1193. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1194. : (cb->getObj(destinationid)->pos),
  1195. true);
  1196. }
  1197. void CGTeleport::initObj()
  1198. {
  1199. objs[ID][subID].push_back(id);
  1200. }
  1201. void CGArtifact::initObj()
  1202. {
  1203. blockVisit = true;
  1204. if(ID == 5)
  1205. hoverName = VLC->arth->artifacts[subID].Name();
  1206. }
  1207. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1208. {
  1209. cb->giveHeroArtifact(subID,h->id,-2);
  1210. InfoWindow iw;
  1211. iw.player = h->tempOwner;
  1212. iw.components.push_back(Component(4,subID,0,0));
  1213. iw.text << std::pair<ui8,ui32>(12,subID);
  1214. cb->showInfoDialog(&iw);
  1215. }
  1216. void CGPickable::initObj()
  1217. {
  1218. blockVisit = true;
  1219. switch(ID)
  1220. {
  1221. case 12: //campfire
  1222. val2 = (ran()%3) + 4; //4 - 6
  1223. val1 = val2 * 100;
  1224. type = ran()%6; //given resource
  1225. break;
  1226. case 101: //treasure chest
  1227. {
  1228. int hlp = ran()%100;
  1229. if(hlp >= 95)
  1230. {
  1231. type = 1;
  1232. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1233. return;
  1234. }
  1235. else if (hlp >= 65)
  1236. {
  1237. val1 = 2000;
  1238. }
  1239. else if(hlp >= 33)
  1240. {
  1241. val1 = 1500;
  1242. }
  1243. else
  1244. {
  1245. val1 = 1000;
  1246. }
  1247. val2 = val1 - 500;
  1248. type = 0;
  1249. break;
  1250. }
  1251. }
  1252. }
  1253. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1254. {
  1255. switch(ID)
  1256. {
  1257. case 12: //campfire
  1258. {
  1259. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1260. cb->giveResource(h->tempOwner,6,val1);//gold
  1261. InfoWindow iw;
  1262. iw.player = h->tempOwner;
  1263. iw.components.push_back(Component(2,6,val1,0));
  1264. iw.components.push_back(Component(2,type,val2,0));
  1265. iw.text << std::pair<ui8,ui32>(11,23);
  1266. cb->showInfoDialog(&iw);
  1267. break;
  1268. }
  1269. case 101: //treasure chest
  1270. {
  1271. if (subID) //not OH3 treasure chest
  1272. {
  1273. tlog2 << "Not supported WoG treasure chest!\n";
  1274. return;
  1275. }
  1276. if(type) //there is an artifact
  1277. {
  1278. cb->giveHeroArtifact(val1,h->id,-2);
  1279. InfoWindow iw;
  1280. iw.player = h->tempOwner;
  1281. iw.components.push_back(Component(4,val1,1,0));
  1282. iw.text << std::pair<ui8,ui32>(11,145);
  1283. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1284. cb->showInfoDialog(&iw);
  1285. break;
  1286. }
  1287. else
  1288. {
  1289. SelectionDialog sd;
  1290. sd.player = h->tempOwner;
  1291. sd.text << std::pair<ui8,ui32>(11,146);
  1292. sd.components.push_back(Component(2,6,val1,0));
  1293. sd.components.push_back(Component(5,0,val2,0));
  1294. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1295. cb->showSelectionDialog(&sd,fun);
  1296. return;
  1297. }
  1298. }
  1299. }
  1300. cb->removeObject(id);
  1301. }
  1302. void CGPickable::chosen( int which, int heroID ) const
  1303. {
  1304. switch(which)
  1305. {
  1306. case 0: //player pick gold
  1307. cb->giveResource(cb->getOwner(heroID),6,val1);
  1308. break;
  1309. case 1: //player pick exp
  1310. cb->changePrimSkill(heroID, 4, val2);
  1311. break;
  1312. default:
  1313. throw std::string("Unhandled treasure choice");
  1314. }
  1315. cb->removeObject(id);
  1316. }
  1317. void CGWitchHut::initObj()
  1318. {
  1319. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1320. }
  1321. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1322. {
  1323. InfoWindow iw;
  1324. iw.player = h->getOwner();
  1325. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1326. {
  1327. iw.text << std::pair<ui8,ui32>(11,172);
  1328. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1329. }
  1330. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1331. {
  1332. iw.text << std::pair<ui8,ui32>(11,173);
  1333. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1334. }
  1335. else //give sec skill
  1336. {
  1337. iw.components.push_back(Component(1, ability, 1, 0));
  1338. iw.text << std::pair<ui8,ui32>(11,171);
  1339. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1340. cb->changeSecSkill(h->id,ability,1,true);
  1341. }
  1342. cb->showInfoDialog(&iw);
  1343. }