CObjectHandler.cpp 48 KB

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