CObjectHandler.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627
  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. /*
  23. * CObjectHandler.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  32. IGameCallback * IObjectInterface::cb = NULL;
  33. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  34. extern CLodHandler * bitmaph;
  35. extern boost::rand48 ran;
  36. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  37. {};
  38. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  39. {};
  40. void IObjectInterface::newTurn () const
  41. {};
  42. IObjectInterface::~IObjectInterface()
  43. {}
  44. IObjectInterface::IObjectInterface()
  45. {}
  46. void IObjectInterface::initObj()
  47. {}
  48. void IObjectInterface::setProperty( ui8 what, ui32 val )
  49. {}
  50. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  51. {
  52. if(what == 10)
  53. players.insert(val);
  54. }
  55. bool CPlayersVisited::hasVisited( ui8 player ) const
  56. {
  57. return vstd::contains(players,player);
  58. }
  59. void CObjectHandler::loadObjects()
  60. {
  61. tlog5 << "\t\tReading cregens \n";
  62. cregens.resize(110); //TODO: hardcoded value - change
  63. for(size_t i=0; i < cregens.size(); ++i)
  64. {
  65. cregens[i]=-1;
  66. }
  67. std::ifstream ifs("config/cregens.txt");
  68. while(!ifs.eof())
  69. {
  70. int dw, cr;
  71. ifs >> dw >> cr;
  72. cregens[dw]=cr;
  73. }
  74. ifs.close();
  75. ifs.clear();
  76. tlog5 << "\t\tDone loading objects!\n";
  77. }
  78. int CGObjectInstance::getOwner() const
  79. {
  80. //if (state)
  81. // return state->owner;
  82. //else
  83. return tempOwner; //won't have owner
  84. }
  85. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  86. {
  87. pos = int3(-1,-1,-1);
  88. //std::cout << "Tworze obiekt "<<this<<std::endl;
  89. //state = new CLuaObjectScript();
  90. ID = subID = id = -1;
  91. defInfo = NULL;
  92. info = NULL;
  93. tempOwner = 254;
  94. blockVisit = false;
  95. }
  96. CGObjectInstance::~CGObjectInstance()
  97. {
  98. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  99. //if (state)
  100. // delete state;
  101. //state=NULL;
  102. }
  103. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  104. //{
  105. // pos = right.pos;
  106. // ID = right.ID;
  107. // subID = right.subID;
  108. // id = right.id;
  109. // defInfo = right.defInfo;
  110. // info = right.info;
  111. // blockVisit = right.blockVisit;
  112. // //state = new CLuaObjectScript(right.state->);
  113. // //*state = *right.state;
  114. // //state = right.state;
  115. // tempOwner = right.tempOwner;
  116. //}
  117. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  118. //{
  119. // pos = right.pos;
  120. // ID = right.ID;
  121. // subID = right.subID;
  122. // id = right.id;
  123. // defInfo = right.defInfo;
  124. // info = right.info;
  125. // blockVisit = right.blockVisit;
  126. // //state = new CLuaObjectScript();
  127. // //*state = *right.state;
  128. // tempOwner = right.tempOwner;
  129. // return *this;
  130. //}
  131. const std::string & CGObjectInstance::getHoverText() const
  132. {
  133. return hoverName;
  134. }
  135. void CGObjectInstance::setOwner(int ow)
  136. {
  137. //if (state)
  138. // state->owner = ow;
  139. //else
  140. tempOwner = ow;
  141. }
  142. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  143. {
  144. return defInfo->width;
  145. }
  146. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  147. {
  148. return defInfo->height;
  149. }
  150. 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)
  151. {
  152. if(defInfo==NULL)
  153. {
  154. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  155. return false;
  156. }
  157. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  158. {
  159. return true;
  160. }
  161. return false;
  162. }
  163. bool CGObjectInstance::blockingAt(int x, int y) const
  164. {
  165. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  166. return false;
  167. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  168. return true;
  169. return false;
  170. }
  171. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  172. {
  173. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  174. return true;
  175. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  176. return false;
  177. if(this->pos.y<cmp.pos.y)
  178. return true;
  179. if(this->pos.y>cmp.pos.y)
  180. return false;
  181. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  182. return true;
  183. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  184. return false;
  185. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  186. return true;
  187. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  188. return false;
  189. if(this->pos.x<cmp.pos.x)
  190. return true;
  191. return false;
  192. }
  193. void CGObjectInstance::initObj()
  194. {
  195. }
  196. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  197. {
  198. switch(what)
  199. {
  200. case 1:
  201. tempOwner = val;
  202. break;
  203. case 2:
  204. blockVisit = val;
  205. break;
  206. case 6:
  207. ID = val;
  208. break;
  209. }
  210. setPropertyDer(what, val);
  211. }
  212. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  213. {}
  214. int3 CGObjectInstance::getSightCenter() const
  215. {
  216. //return vistiable tile if possible
  217. for(int i=0; i < 8; i++)
  218. for(int j=0; j < 6; j++)
  219. if(visitableAt(i,j))
  220. return(pos + int3(i-7, j-5, 0));
  221. return pos;
  222. }
  223. int CGObjectInstance::getSightRadious() const
  224. {
  225. return 3;
  226. }
  227. int lowestSpeed(const CGHeroInstance * chi)
  228. {
  229. if(!chi->army.slots.size())
  230. {
  231. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  232. return 20;
  233. }
  234. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  235. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  236. for (;i!=chi->army.slots.end();i++)
  237. {
  238. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  239. }
  240. return ret;
  241. }
  242. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  243. {
  244. //TODO: check if all creatures are on its native terrain and change cost appropriately
  245. //base move cost
  246. unsigned ret = 100;
  247. //if there is road both on dest and src tiles - use road movement cost
  248. if(dest.malle && from.malle)
  249. {
  250. int road = std::min(dest.malle,from.malle); //used road ID
  251. switch(road)
  252. {
  253. case dirtRoad:
  254. ret = 75;
  255. break;
  256. case grazvelRoad:
  257. ret = 65;
  258. break;
  259. case cobblestoneRoad:
  260. ret = 50;
  261. break;
  262. default:
  263. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  264. break;
  265. }
  266. }
  267. else
  268. {
  269. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  270. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  271. }
  272. return ret;
  273. }
  274. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  275. {
  276. unsigned int sl = 100;
  277. for(size_t h=0; h < army.slots.size(); ++h)
  278. {
  279. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  280. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  281. }
  282. return sl;
  283. }
  284. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  285. {
  286. if (toh3m)
  287. {
  288. src.x+=1;
  289. return src;
  290. }
  291. else
  292. {
  293. src.x-=1;
  294. return src;
  295. }
  296. }
  297. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  298. {
  299. if (h3m)
  300. {
  301. return pos;
  302. }
  303. else
  304. {
  305. return convertPosition(pos,false);
  306. }
  307. }
  308. si32 CGHeroInstance::manaLimit() const
  309. {
  310. double modifier = 1.0;
  311. switch(getSecSkillLevel(24)) //intelligence level
  312. {
  313. case 1: modifier+=0.25; break;
  314. case 2: modifier+=0.5; break;
  315. case 3: modifier+=1.0; break;
  316. }
  317. return si32(10*getPrimSkillLevel(3)*modifier);
  318. }
  319. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  320. //{
  321. // if (h3m)
  322. // pos = Pos;
  323. // else
  324. // pos = convertPosition(Pos,true);
  325. //}
  326. bool CGHeroInstance::canWalkOnSea() const
  327. {
  328. //TODO: write it - it should check if hero is flying, or something similiar
  329. return false;
  330. }
  331. int CGHeroInstance::getPrimSkillLevel(int id) const
  332. {
  333. int ret = primSkills[id];
  334. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  335. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  336. ret += i->val;
  337. return ret;
  338. }
  339. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  340. {
  341. for(size_t i=0; i < secSkills.size(); ++i)
  342. if(secSkills[i].first==ID)
  343. return secSkills[i].second;
  344. return 0;
  345. }
  346. int CGHeroInstance::maxMovePoints(bool onLand) const
  347. {
  348. int ret = std::min(2000, 1270+70*lowestSpeed(this)),
  349. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  350. double modifier = 0;
  351. if(onLand)
  352. {
  353. //logistics:
  354. switch(getSecSkillLevel(2))
  355. {
  356. case 1:
  357. modifier = 0.1;
  358. break;
  359. case 2:
  360. modifier = 0.2;
  361. break;
  362. case 3:
  363. modifier = 0.3;
  364. break;
  365. }
  366. }
  367. else
  368. {
  369. //navigation:
  370. switch(getSecSkillLevel(2))
  371. {
  372. case 1:
  373. modifier = 0.5;
  374. break;
  375. case 2:
  376. modifier = 1.0;
  377. break;
  378. case 3:
  379. modifier = 1.5;
  380. break;
  381. }
  382. }
  383. return int(ret + ret*modifier) + bonus;
  384. }
  385. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  386. {
  387. if(pos<19)
  388. if(vstd::contains(artifWorn,pos))
  389. return artifWorn.find(pos)->second;
  390. else
  391. return -1;
  392. else
  393. if(pos-19 < artifacts.size())
  394. return artifacts[pos-19];
  395. else
  396. return -1;
  397. }
  398. const CArtifact * CGHeroInstance::getArt(int pos) const
  399. {
  400. int id = getArtAtPos(pos);
  401. if(id>=0)
  402. return &VLC->arth->artifacts[id];
  403. else
  404. return NULL;
  405. }
  406. int CGHeroInstance::getSpellSecLevel(int spell) const
  407. {
  408. int bestslvl = 0;
  409. if(VLC->spellh->spells[spell].air)
  410. if(getSecSkillLevel(15) >= bestslvl)
  411. {
  412. bestslvl = getSecSkillLevel(15);
  413. }
  414. if(VLC->spellh->spells[spell].fire)
  415. if(getSecSkillLevel(14) >= bestslvl)
  416. {
  417. bestslvl = getSecSkillLevel(14);
  418. }
  419. if(VLC->spellh->spells[spell].water)
  420. if(getSecSkillLevel(16) >= bestslvl)
  421. {
  422. bestslvl = getSecSkillLevel(16);
  423. }
  424. if(VLC->spellh->spells[spell].earth)
  425. if(getSecSkillLevel(17) >= bestslvl)
  426. {
  427. bestslvl = getSecSkillLevel(17);
  428. }
  429. return bestslvl;
  430. }
  431. CGHeroInstance::CGHeroInstance()
  432. {
  433. ID = HEROI_TYPE;
  434. tacticFormationEnabled = inTownGarrison = false;
  435. mana = movement = portrait = level = -1;
  436. isStanding = true;
  437. moveDir = 4;
  438. exp = 0xffffffff;
  439. visitedTown = NULL;
  440. type = NULL;
  441. secSkills.push_back(std::make_pair(-1, -1));
  442. }
  443. void CGHeroInstance::initHero(int SUBID)
  444. {
  445. subID = SUBID;
  446. initHero();
  447. }
  448. void CGHeroInstance::initHero()
  449. {
  450. if(ID == HEROI_TYPE)
  451. initHeroDefInfo();
  452. if(!type)
  453. type = VLC->heroh->heroes[subID];
  454. artifWorn[16] = 3;
  455. if(type->heroType % 2 == 1) //it's a magical hero
  456. {
  457. artifWorn[17] = 0; //give him spellbook
  458. }
  459. if(portrait < 0 || portrait == 255)
  460. portrait = subID;
  461. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  462. {
  463. primSkills.resize(4);
  464. primSkills[0] = type->heroClass->initialAttack;
  465. primSkills[1] = type->heroClass->initialDefence;
  466. primSkills[2] = type->heroClass->initialPower;
  467. primSkills[3] = type->heroClass->initialKnowledge;
  468. }
  469. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  470. secSkills = type->secSkillsInit;
  471. if(mana < 0)
  472. mana = manaLimit();
  473. if (!name.length())
  474. name = type->name;
  475. if (exp == 0xffffffff)
  476. {
  477. exp=40+ (ran()) % 50;
  478. level = 1;
  479. }
  480. else
  481. {
  482. level = VLC->heroh->level(exp);
  483. }
  484. if (!army.slots.size()) //standard army//initial army
  485. {
  486. int pom, pom2=0;
  487. int x = 0; //how many stacks will hero receives <1 - 3>
  488. pom = ran()%100;
  489. if(pom < 5)
  490. x = 1;
  491. else if(pom < 67)
  492. x = 2;
  493. else
  494. x = 3;
  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. pom = type->highStack[x] - type->lowStack[x];
  514. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  515. army.formation = false;
  516. }
  517. }
  518. hoverName = VLC->generaltexth->allTexts[15];
  519. boost::algorithm::replace_first(hoverName,"%s",name);
  520. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  521. //clear all bonuses from artifacts (if present) and give them again
  522. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  523. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  524. {
  525. CArtifact &art = VLC->arth->artifacts[ari->second];
  526. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  527. bonuses.push_back(*i);
  528. }
  529. }
  530. void CGHeroInstance::initHeroDefInfo()
  531. {
  532. if(!defInfo || defInfo->id != HEROI_TYPE)
  533. {
  534. defInfo = new CGDefInfo();
  535. defInfo->id = HEROI_TYPE;
  536. defInfo->subid = subID;
  537. defInfo->printPriority = 0;
  538. defInfo->visitDir = 0xff;
  539. }
  540. for(int i=0;i<6;i++)
  541. {
  542. defInfo->blockMap[i]=255;
  543. defInfo->visitMap[i]=0;
  544. }
  545. defInfo->handler=NULL;
  546. defInfo->blockMap[5] = 253;
  547. defInfo->visitMap[5] = 2;
  548. }
  549. CGHeroInstance::~CGHeroInstance()
  550. {
  551. }
  552. bool CGHeroInstance::needsLastStack() const
  553. {
  554. return true;
  555. }
  556. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  557. {
  558. if (ID == HEROI_TYPE) //hero
  559. {
  560. //TODO: check for allies
  561. if(tempOwner == h->tempOwner) //our hero
  562. {
  563. //exchange
  564. }
  565. else
  566. {
  567. cb->startBattleI(
  568. &h->army,
  569. &army,
  570. h->pos,
  571. h,
  572. this,
  573. 0);
  574. }
  575. }
  576. else if(ID == 62) //prison
  577. {
  578. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  579. {
  580. cb->changeObjPos(id,pos+int3(1,0,0),0);
  581. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  582. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  583. InfoWindow iw;
  584. iw.player = h->tempOwner;
  585. iw.text << std::pair<ui8,ui32>(11,102);
  586. cb->showInfoDialog(&iw);
  587. }
  588. else //already 8 wandering heroes
  589. {
  590. InfoWindow iw;
  591. iw.player = h->tempOwner;
  592. iw.text << std::pair<ui8,ui32>(11,103);
  593. cb->showInfoDialog(&iw);
  594. }
  595. }
  596. }
  597. const std::string & CGHeroInstance::getBiography() const
  598. {
  599. if (biography.length())
  600. return biography;
  601. else
  602. return VLC->generaltexth->hTxts[subID].biography;
  603. }
  604. void CGHeroInstance::initObj()
  605. {
  606. blockVisit = true;
  607. }
  608. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  609. {
  610. int ret = 0;
  611. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  612. for(int i=0; i < mods.size(); i++)
  613. ret += mods[i].first;
  614. if(ret > 3)
  615. return 3;
  616. if(ret < -3)
  617. return -3;
  618. return ret;
  619. }
  620. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  621. {
  622. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  623. std::vector<std::pair<int,std::string> > ret;
  624. //various morale bonuses (from buildings, artifacts, etc)
  625. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  626. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  627. ret.push_back(std::make_pair(i->val, i->description));
  628. //leadership
  629. if(getSecSkillLevel(6))
  630. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  631. //town structures
  632. if(town && visitedTown)
  633. {
  634. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  635. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  636. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  637. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  638. }
  639. //number of alignments and presence of undead
  640. if(stack>=0)
  641. {
  642. bool archangelInArmy = false;
  643. std::set<si8> factions;
  644. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  645. {
  646. factions.insert(VLC->creh->creatures[i->second.first].faction);
  647. if(i->second.first == 13)
  648. archangelInArmy = true;
  649. }
  650. if(factions.size() == 1)
  651. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  652. else
  653. {
  654. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  655. {
  656. char buf[150];
  657. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  658. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  659. }
  660. else
  661. {
  662. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  663. }
  664. }
  665. if(vstd::contains(factions,4))
  666. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  667. if(archangelInArmy)
  668. {
  669. char buf[100];
  670. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  671. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  672. }
  673. }
  674. return ret;
  675. }
  676. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  677. {
  678. int ret = 0;
  679. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  680. for(int i=0; i < mods.size(); i++)
  681. ret += mods[i].first;
  682. if(ret > 3)
  683. return 3;
  684. if(ret < -3)
  685. return -3;
  686. return ret;
  687. }
  688. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  689. {
  690. std::vector<std::pair<int,std::string> > ret;
  691. //various morale bonuses (from buildings, artifacts, etc)
  692. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  693. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  694. ret.push_back(std::make_pair(i->val, i->description));
  695. //luck skill
  696. if(getSecSkillLevel(9))
  697. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  698. return ret;
  699. }
  700. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  701. {
  702. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  703. if(i->source == from && i->id == id)
  704. return &*i;
  705. return NULL;
  706. }
  707. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  708. {
  709. if(what == 3)
  710. army.slots[0].second = val;
  711. }
  712. double CGHeroInstance::getHeroStrength() const
  713. {
  714. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  715. }
  716. int CGHeroInstance::getTotalStrength() const
  717. {
  718. double ret = getHeroStrength() * getArmyStrength();
  719. return (int) ret;
  720. }
  721. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  722. {
  723. //TODO: skill level may be different on special terrain
  724. ui8 skill = 0; //skill level
  725. if(spell->fire)
  726. skill = std::max(skill,getSecSkillLevel(14));
  727. if(spell->air)
  728. skill = std::max(skill,getSecSkillLevel(15));
  729. if(spell->water)
  730. skill = std::max(skill,getSecSkillLevel(16));
  731. if(spell->earth)
  732. skill = std::max(skill,getSecSkillLevel(17));
  733. return skill;
  734. }
  735. int3 CGHeroInstance::getSightCenter() const
  736. {
  737. return getPosition(false);
  738. }
  739. int CGHeroInstance::getSightRadious() const
  740. {
  741. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  742. }
  743. si32 CGHeroInstance::manaRegain() const
  744. {
  745. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  746. }
  747. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type ) const
  748. {
  749. int ret = 0;
  750. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  751. if(i->type == type)
  752. ret += i->val;
  753. return ret;
  754. }
  755. int CGTownInstance::getSightRadious() const //returns sight distance
  756. {
  757. return 5;
  758. }
  759. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  760. {
  761. if((builtBuildings.find(9))!=builtBuildings.end())
  762. return 3;
  763. if((builtBuildings.find(8))!=builtBuildings.end())
  764. return 2;
  765. if((builtBuildings.find(7))!=builtBuildings.end())
  766. return 1;
  767. return 0;
  768. }
  769. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  770. {
  771. if ((builtBuildings.find(13))!=builtBuildings.end())
  772. return 3;
  773. if ((builtBuildings.find(12))!=builtBuildings.end())
  774. return 2;
  775. if ((builtBuildings.find(11))!=builtBuildings.end())
  776. return 1;
  777. if ((builtBuildings.find(10))!=builtBuildings.end())
  778. return 0;
  779. return -1;
  780. }
  781. int CGTownInstance::mageGuildLevel() const
  782. {
  783. if ((builtBuildings.find(4))!=builtBuildings.end())
  784. return 5;
  785. if ((builtBuildings.find(3))!=builtBuildings.end())
  786. return 4;
  787. if ((builtBuildings.find(2))!=builtBuildings.end())
  788. return 3;
  789. if ((builtBuildings.find(1))!=builtBuildings.end())
  790. return 2;
  791. if ((builtBuildings.find(0))!=builtBuildings.end())
  792. return 1;
  793. return 0;
  794. }
  795. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  796. {
  797. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  798. }
  799. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  800. {
  801. return town->hordeLvl[HID];
  802. }
  803. int CGTownInstance::creatureGrowth(const int & level) const
  804. {
  805. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  806. switch(fortLevel())
  807. {
  808. case 3:
  809. ret*=2;break;
  810. case 2:
  811. ret*=(1.5); break;
  812. }
  813. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  814. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  815. if(getHordeLevel(0)==level)
  816. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  817. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  818. if(getHordeLevel(1)==level)
  819. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  820. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  821. return ret;
  822. }
  823. int CGTownInstance::dailyIncome() const
  824. {
  825. int ret = 0;
  826. if ((builtBuildings.find(26))!=builtBuildings.end())
  827. ret+=5000;
  828. if ((builtBuildings.find(13))!=builtBuildings.end())
  829. ret+=4000;
  830. else if ((builtBuildings.find(12))!=builtBuildings.end())
  831. ret+=2000;
  832. else if ((builtBuildings.find(11))!=builtBuildings.end())
  833. ret+=1000;
  834. else if ((builtBuildings.find(10))!=builtBuildings.end())
  835. ret+=500;
  836. return ret;
  837. }
  838. bool CGTownInstance::hasFort() const
  839. {
  840. return (builtBuildings.find(7))!=builtBuildings.end();
  841. }
  842. bool CGTownInstance::hasCapitol() const
  843. {
  844. return (builtBuildings.find(13))!=builtBuildings.end();
  845. }
  846. CGTownInstance::CGTownInstance()
  847. {
  848. builded=-1;
  849. destroyed=-1;
  850. garrisonHero=NULL;
  851. town=NULL;
  852. visitingHero = NULL;
  853. }
  854. CGTownInstance::~CGTownInstance()
  855. {}
  856. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  857. {
  858. if(checkGuild && mageGuildLevel() < level)
  859. return 0;
  860. int ret = 6 - level; //how many spells are available at this level
  861. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  862. ret++;
  863. return ret;
  864. }
  865. bool CGTownInstance::needsLastStack() const
  866. {
  867. if(garrisonHero)
  868. return true;
  869. else return false;
  870. }
  871. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  872. {
  873. if(getOwner() != h->getOwner())
  874. {
  875. return;
  876. }
  877. cb->heroVisitCastle(id,h->id);
  878. }
  879. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  880. {
  881. cb->stopHeroVisitCastle(id,h->id);
  882. }
  883. void CGTownInstance::initObj()
  884. {
  885. MetaString ms;
  886. ms << name << ", " << town->Name();
  887. hoverName = toString(ms);
  888. }
  889. int3 CGTownInstance::getSightCenter() const
  890. {
  891. return pos - int3(2,0,0);
  892. }
  893. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  894. {
  895. if(visitors.find(h->id)==visitors.end())
  896. {
  897. onNAHeroVisit(h->id, false);
  898. switch(ID)
  899. {
  900. case 102: //tree
  901. case 4: //arena
  902. case 41://library
  903. case 47: //School of Magic
  904. case 107://School of War
  905. break;
  906. default:
  907. cb->setObjProperty(id,4,h->id); //add to the visitors
  908. break;
  909. }
  910. }
  911. else
  912. {
  913. onNAHeroVisit(h->id, true);
  914. }
  915. }
  916. void CGVisitableOPH::initObj()
  917. {
  918. if(ID==102)
  919. ttype = ran()%3;
  920. else
  921. ttype = -1;
  922. }
  923. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  924. {
  925. if(result) //player agreed to give res for exp
  926. {
  927. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  928. cb->changePrimSkill(heroID,4,expVal); //give exp
  929. cb->setObjProperty(id,4,heroID); //add to the visitors
  930. }
  931. }
  932. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  933. {
  934. int id=0, subid=0, ot=0, val=1;
  935. switch(ID)
  936. {
  937. case 4:
  938. ot = 0;
  939. break;
  940. case 51:
  941. subid=0;
  942. ot=80;
  943. break;
  944. case 23:
  945. subid=1;
  946. ot=39;
  947. break;
  948. case 61:
  949. subid=2;
  950. ot=100;
  951. break;
  952. case 32:
  953. subid=3;
  954. ot=59;
  955. break;
  956. case 100:
  957. id=5;
  958. ot=143;
  959. val=1000;
  960. break;
  961. case 102:
  962. id = 5;
  963. subid = 1;
  964. ot = 146;
  965. val = 1;
  966. break;
  967. case 41:
  968. ot = 66;
  969. break;
  970. case 47: //School of Magic
  971. ot = 71;
  972. break;
  973. case 107://School of War
  974. ot = 158;
  975. break;
  976. }
  977. if (!alreadyVisited)
  978. {
  979. switch (ID)
  980. {
  981. case 4: //arena
  982. {
  983. BlockingDialog sd(false,true);
  984. sd.text << std::pair<ui8,ui32>(11,ot);
  985. sd.components.push_back(Component(0,0,2,0));
  986. sd.components.push_back(Component(0,1,2,0));
  987. sd.player = cb->getOwner(heroID);
  988. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  989. return;
  990. }
  991. case 51:
  992. case 23:
  993. case 61:
  994. case 32:
  995. {
  996. cb->changePrimSkill(heroID,subid,val);
  997. InfoWindow iw;
  998. iw.components.push_back(Component(0,subid,val,0));
  999. iw.text << std::pair<ui8,ui32>(11,ot);
  1000. iw.player = cb->getOwner(heroID);
  1001. cb->showInfoDialog(&iw);
  1002. break;
  1003. }
  1004. case 100: //give exp
  1005. {
  1006. InfoWindow iw;
  1007. iw.components.push_back(Component(id,subid,val,0));
  1008. iw.player = cb->getOwner(heroID);
  1009. iw.text << std::pair<ui8,ui32>(11,ot);
  1010. cb->showInfoDialog(&iw);
  1011. cb->changePrimSkill(heroID,4,val);
  1012. break;
  1013. }
  1014. case 102://tree
  1015. {
  1016. const CGHeroInstance *h = cb->getHero(heroID);
  1017. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1018. if(!ttype)
  1019. {
  1020. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1021. InfoWindow iw;
  1022. iw.components.push_back(Component(id,subid,1,0));
  1023. iw.player = cb->getOwner(heroID);
  1024. iw.text << std::pair<ui8,ui32>(11,148);
  1025. cb->showInfoDialog(&iw);
  1026. cb->changePrimSkill(heroID,4,val);
  1027. break;
  1028. }
  1029. else
  1030. {
  1031. int res, resval;
  1032. if(ttype==1)
  1033. {
  1034. res = 6;
  1035. resval = 2000;
  1036. ot = 149;
  1037. }
  1038. else
  1039. {
  1040. res = 5;
  1041. resval = 10;
  1042. ot = 151;
  1043. }
  1044. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1045. {
  1046. ot++;
  1047. InfoWindow iw;
  1048. iw.player = h->tempOwner;
  1049. iw.text << std::pair<ui8,ui32>(11,ot);
  1050. cb->showInfoDialog(&iw);
  1051. return;
  1052. }
  1053. BlockingDialog sd(true,false);
  1054. sd.player = cb->getOwner(heroID);
  1055. sd.text << std::pair<ui8,ui32>(11,ot);
  1056. sd.components.push_back(Component(id,subid,val,0));
  1057. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1058. }
  1059. break;
  1060. }
  1061. case 41://library of enlightenment
  1062. {
  1063. const CGHeroInstance *h = cb->getHero(heroID);
  1064. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1065. {
  1066. InfoWindow iw;
  1067. iw.player = cb->getOwner(heroID);
  1068. iw.text << std::pair<ui8,ui32>(11,68);
  1069. cb->showInfoDialog(&iw);
  1070. }
  1071. else
  1072. {
  1073. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1074. cb->changePrimSkill(heroID,0,2);
  1075. cb->changePrimSkill(heroID,1,2);
  1076. cb->changePrimSkill(heroID,2,2);
  1077. cb->changePrimSkill(heroID,3,2);
  1078. InfoWindow iw;
  1079. iw.player = cb->getOwner(heroID);
  1080. iw.text << std::pair<ui8,ui32>(11,66);
  1081. cb->showInfoDialog(&iw);
  1082. }
  1083. break;
  1084. }
  1085. case 47: //School of Magic
  1086. case 107://School of War
  1087. {
  1088. int skill = (ID==47 ? 2 : 0);
  1089. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1090. {
  1091. InfoWindow iw;
  1092. iw.player = cb->getOwner(heroID);
  1093. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1094. cb->showInfoDialog(&iw);
  1095. }
  1096. else
  1097. {
  1098. BlockingDialog sd(true,true);
  1099. sd.player = cb->getOwner(heroID);
  1100. sd.text << std::pair<ui8,ui32>(11,ot);
  1101. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1102. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1103. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1104. }
  1105. }
  1106. break;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. ot++;
  1112. InfoWindow iw;
  1113. iw.player = cb->getOwner(heroID);
  1114. iw.text << std::pair<ui8,ui32>(11,ot);
  1115. cb->showInfoDialog(&iw);
  1116. }
  1117. }
  1118. const std::string & CGVisitableOPH::getHoverText() const
  1119. {
  1120. int pom = -1;
  1121. switch(ID)
  1122. {
  1123. case 4:
  1124. pom = -1;
  1125. break;
  1126. case 51:
  1127. pom = 8;
  1128. break;
  1129. case 23:
  1130. pom = 7;
  1131. break;
  1132. case 61:
  1133. pom = 11;
  1134. break;
  1135. case 32:
  1136. pom = 4;
  1137. break;
  1138. case 100:
  1139. pom = 5;
  1140. break;
  1141. case 102:
  1142. pom = 18;
  1143. break;
  1144. case 41:
  1145. break;
  1146. case 47: //School of Magic
  1147. pom = 9;
  1148. break;
  1149. case 107://School of War
  1150. pom = 10;
  1151. break;
  1152. default:
  1153. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1154. }
  1155. hoverName = VLC->generaltexth->names[ID];
  1156. if(pom >= 0)
  1157. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1158. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1159. if(h)
  1160. {
  1161. hoverName += ' ';
  1162. hoverName += (vstd::contains(visitors,h->id))
  1163. ? (VLC->generaltexth->allTexts[352]) //visited
  1164. : ( VLC->generaltexth->allTexts[353]); //not visited
  1165. }
  1166. return hoverName;
  1167. }
  1168. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1169. {
  1170. cb->setObjProperty(id,4,heroID); //add to the visitors
  1171. cb->changePrimSkill(heroID,primSkill-1,2);
  1172. }
  1173. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1174. {
  1175. if(what == 4)
  1176. visitors.insert(val);
  1177. }
  1178. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1179. {
  1180. if(!which) //player refused to pay
  1181. return;
  1182. int base = (ID == 47 ? 2 : 0);
  1183. cb->setObjProperty(id,4,heroID); //add to the visitors
  1184. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1185. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1186. }
  1187. bool CArmedInstance::needsLastStack() const
  1188. {
  1189. return false;
  1190. }
  1191. int CArmedInstance::getArmyStrength() const
  1192. {
  1193. int ret = 0;
  1194. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1195. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1196. return ret;
  1197. }
  1198. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1199. {
  1200. int action = takenAction(h);
  1201. switch( action ) //decide what we do...
  1202. {
  1203. case -2: //fight
  1204. fight(h);
  1205. break;
  1206. case -1: //flee
  1207. {
  1208. flee(h);
  1209. break;
  1210. }
  1211. case 0: //join for free
  1212. {
  1213. BlockingDialog ynd(true,false);
  1214. ynd.player = h->tempOwner;
  1215. ynd.text << std::pair<ui8,ui32>(11,86);
  1216. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1217. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1218. break;
  1219. }
  1220. default: //join for gold
  1221. {
  1222. assert(action > 0);
  1223. //ask if player agrees to pay gold
  1224. BlockingDialog ynd(true,false);
  1225. ynd.player = h->tempOwner;
  1226. std::string tmp = VLC->generaltexth->advobtxt[90];
  1227. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1228. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1229. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1230. ynd.text << tmp;
  1231. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1232. break;
  1233. }
  1234. }
  1235. }
  1236. void CGCreature::endBattle( BattleResult *result ) const
  1237. {
  1238. if(result->winner==0)
  1239. {
  1240. cb->removeObject(id);
  1241. }
  1242. else
  1243. {
  1244. int killedAmount=0;
  1245. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1246. if(i->first == subID)
  1247. killedAmount += i->second;
  1248. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1249. MetaString ms;
  1250. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1251. pom = 174 + 3*pom + 1;
  1252. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1253. cb->setHoverName(id,&ms);
  1254. }
  1255. }
  1256. void CGCreature::initObj()
  1257. {
  1258. blockVisit = true;
  1259. switch(character)
  1260. {
  1261. case 0:
  1262. character = 0;
  1263. break;
  1264. case 1:
  1265. character = 1 + ran()%7;
  1266. break;
  1267. case 2:
  1268. character = 1 + ran()%10;
  1269. break;
  1270. case 3:
  1271. character = 4 + ran()%7;
  1272. break;
  1273. case 4:
  1274. character = 10;
  1275. break;
  1276. }
  1277. army.slots[0].first = subID;
  1278. si32 &amount = army.slots[0].second;
  1279. CCreature &c = VLC->creh->creatures[subID];
  1280. if(!amount)
  1281. if(c.ammMax == c.ammMin)
  1282. amount = c.ammMax;
  1283. else
  1284. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1285. MetaString ms;
  1286. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1287. pom = 174 + 3*pom + 1;
  1288. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1289. hoverName = toString(ms);
  1290. }
  1291. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1292. {
  1293. double hlp = h->getTotalStrength() / getArmyStrength();
  1294. if(!character) //compliant creatures will always join
  1295. return 0;
  1296. else if(allowJoin)//test for joining
  1297. {
  1298. int factor;
  1299. if(hlp >= 7)
  1300. factor = 11;
  1301. else if(hlp >= 1)
  1302. factor = 2*(hlp-1);
  1303. else if(hlp >= 0.5)
  1304. factor = -1;
  1305. else if(hlp >= 0.333)
  1306. factor = -2;
  1307. else
  1308. factor = -3;
  1309. int sympathy = 0;
  1310. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1311. myKindCres.insert(subID); //we
  1312. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1313. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1314. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1315. myKindCres.insert(i->idNumber);
  1316. int count = 0, //how many creatures of our kind has hero
  1317. totalCount = 0;
  1318. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1319. {
  1320. if(vstd::contains(myKindCres,i->second.first))
  1321. count += i->second.second;
  1322. totalCount += i->second.second;
  1323. }
  1324. if(count*2 > totalCount)
  1325. sympathy++;
  1326. if(count)
  1327. sympathy++;
  1328. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1329. if(charisma >= character) //creatures might join...
  1330. {
  1331. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1332. return 0; //join for free
  1333. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1334. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1335. }
  1336. }
  1337. //we are still here - creatures not joined heroes, test for fleeing
  1338. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1339. //TODO: should be deterministic (will be needed for Vision spell)
  1340. int hlp2 = (int) (hlp - 2)*1000;
  1341. if(!neverFlees
  1342. && hlp2 >= 0
  1343. && rand()%2000 < hlp2
  1344. )
  1345. return -1; //flee
  1346. else
  1347. return -2; //fight
  1348. }
  1349. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1350. {
  1351. if(pursue)
  1352. {
  1353. fight(h);
  1354. }
  1355. else
  1356. {
  1357. cb->removeObject(id);
  1358. }
  1359. }
  1360. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1361. {
  1362. if(!accept)
  1363. {
  1364. if(takenAction(h,false) == -1) //they flee
  1365. {
  1366. flee(h);
  1367. }
  1368. else //they fight
  1369. {
  1370. InfoWindow iw;
  1371. iw.player = h->tempOwner;
  1372. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1373. cb->showInfoDialog(&iw);
  1374. fight(h);
  1375. }
  1376. }
  1377. else //accepted
  1378. {
  1379. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1380. {
  1381. InfoWindow iw;
  1382. iw.player = h->tempOwner;
  1383. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1384. cb->showInfoDialog(&iw);
  1385. //act as if player refused
  1386. joinDecision(h,cost,true);
  1387. return;
  1388. }
  1389. //take gold
  1390. if(cost)
  1391. cb->giveResource(h->tempOwner,6,-cost);
  1392. int slot = h->army.getSlotFor(subID);
  1393. if(slot >= 0) //there is place
  1394. {
  1395. //add creatures
  1396. SetGarrisons sg;
  1397. sg.garrs[h->id] = h->army;
  1398. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1399. {
  1400. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1401. }
  1402. else //add as a new stack
  1403. {
  1404. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1405. }
  1406. cb->sendAndApply(&sg);
  1407. cb->removeObject(id);
  1408. }
  1409. else
  1410. {
  1411. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1412. }
  1413. }
  1414. }
  1415. void CGCreature::fight( const CGHeroInstance *h ) const
  1416. {
  1417. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1418. }
  1419. void CGCreature::flee( const CGHeroInstance * h ) const
  1420. {
  1421. BlockingDialog ynd(true,false);
  1422. ynd.player = h->tempOwner;
  1423. ynd.text << std::pair<ui8,ui32>(11,91);
  1424. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1425. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1426. }
  1427. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1428. {
  1429. if(subID == 7) //TODO: support for abandoned mine
  1430. return;
  1431. if(h->tempOwner == tempOwner) //we're visiting our mine
  1432. {
  1433. cb->showGarrisonDialog(id,h->id,0);
  1434. return;
  1435. }
  1436. //TODO: check if mine is guarded
  1437. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1438. MetaString ms;
  1439. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1440. cb->setHoverName(id,&ms);
  1441. int vv=1; //amount of resource per turn
  1442. if (subID==0 || subID==2)
  1443. vv++;
  1444. else if (subID==6)
  1445. vv = 1000;
  1446. InfoWindow iw;
  1447. iw.text << std::pair<ui8,ui32>(10,subID);
  1448. iw.player = h->tempOwner;
  1449. iw.components.push_back(Component(2,subID,vv,-1));
  1450. cb->showInfoDialog(&iw);
  1451. }
  1452. void CGMine::newTurn() const
  1453. {
  1454. if (tempOwner == NEUTRAL_PLAYER)
  1455. return;
  1456. int vv = 1;
  1457. if (subID==0 || subID==2)
  1458. vv++;
  1459. else if (subID==6)
  1460. vv = 1000;
  1461. cb->giveResource(tempOwner,subID,vv);
  1462. }
  1463. void CGMine::initObj()
  1464. {
  1465. MetaString ms;
  1466. ms << std::pair<ui8,ui32>(9,subID);
  1467. if(tempOwner >= PLAYER_LIMIT)
  1468. tempOwner = NEUTRAL_PLAYER;
  1469. else
  1470. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1471. hoverName = toString(ms);
  1472. }
  1473. void CGResource::initObj()
  1474. {
  1475. blockVisit = true;
  1476. hoverName = VLC->generaltexth->restypes[subID];
  1477. if(!amount)
  1478. {
  1479. switch(subID)
  1480. {
  1481. case 6:
  1482. amount = 500 + (rand()%6)*100;
  1483. break;
  1484. case 0: case 2:
  1485. amount = 6 + (rand()%5);
  1486. break;
  1487. default:
  1488. amount = 3 + (rand()%3);
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1494. {
  1495. if(army.slots.size())
  1496. {
  1497. if(message.size())
  1498. {
  1499. BlockingDialog ynd(true,false);
  1500. ynd.player = h->getOwner();
  1501. ynd.text << message;
  1502. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1503. }
  1504. else
  1505. {
  1506. fightForRes(0,h);
  1507. }
  1508. }
  1509. else
  1510. {
  1511. if(message.length())
  1512. {
  1513. InfoWindow iw;
  1514. iw.player = h->tempOwner;
  1515. iw.text << message;
  1516. cb->showInfoDialog(&iw);
  1517. }
  1518. collectRes(h->getOwner());
  1519. }
  1520. }
  1521. void CGResource::collectRes( int player ) const
  1522. {
  1523. cb->giveResource(player,subID,amount);
  1524. ShowInInfobox sii;
  1525. sii.player = player;
  1526. sii.c = Component(2,subID,amount,0);
  1527. sii.text << std::pair<ui8,ui32>(11,113);
  1528. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1529. cb->showCompInfo(&sii);
  1530. cb->removeObject(id);
  1531. }
  1532. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1533. {
  1534. if(agreed)
  1535. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1536. }
  1537. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1538. {
  1539. if(result->winner == 0) //attacker won
  1540. collectRes(h->getOwner());
  1541. }
  1542. void CGVisitableOPW::newTurn() const
  1543. {
  1544. if (cb->getDate(1)==1) //first day of week
  1545. {
  1546. cb->setObjProperty(id,5,false);
  1547. MetaString ms; //set text to "not visited"
  1548. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1549. cb->setHoverName(id,&ms);
  1550. }
  1551. }
  1552. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1553. {
  1554. int mid;
  1555. switch (ID)
  1556. {
  1557. case 55:
  1558. mid = 92;
  1559. break;
  1560. case 112:
  1561. mid = 170;
  1562. break;
  1563. case 109:
  1564. mid = 164;
  1565. break;
  1566. }
  1567. if (visited)
  1568. {
  1569. if (ID!=112)
  1570. mid++;
  1571. else
  1572. mid--;
  1573. InfoWindow iw;
  1574. iw.player = h->tempOwner;
  1575. iw.text << std::pair<ui8,ui32>(11,mid);
  1576. cb->showInfoDialog(&iw);
  1577. }
  1578. else
  1579. {
  1580. int type, sub, val;
  1581. type = 2;
  1582. switch (ID)
  1583. {
  1584. case 55:
  1585. if (rand()%2)
  1586. {
  1587. sub = 5;
  1588. val = 5;
  1589. }
  1590. else
  1591. {
  1592. sub = 6;
  1593. val = 500;
  1594. }
  1595. break;
  1596. case 112:
  1597. mid = 170;
  1598. sub = (rand() % 5) + 1;
  1599. val = (rand() % 4) + 3;
  1600. break;
  1601. case 109:
  1602. mid = 164;
  1603. sub = 6;
  1604. if(cb->getDate(0)<8)
  1605. val = 500;
  1606. else
  1607. val = 1000;
  1608. }
  1609. cb->giveResource(h->tempOwner,sub,val);
  1610. InfoWindow iw;
  1611. iw.player = h->tempOwner;
  1612. iw.components.push_back(Component(type,sub,val,0));
  1613. iw.text << std::pair<ui8,ui32>(11,mid);
  1614. cb->showInfoDialog(&iw);
  1615. cb->setObjProperty(id,5,true);
  1616. MetaString ms; //set text to "visited"
  1617. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1618. cb->setHoverName(id,&ms);
  1619. }
  1620. }
  1621. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1622. {
  1623. if(what == 5)
  1624. visited = val;
  1625. }
  1626. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1627. {
  1628. int destinationid=-1;
  1629. switch(ID)
  1630. {
  1631. case 43: //one way - find correspong exit monolith
  1632. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1633. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1634. else
  1635. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1636. break;
  1637. case 45: //two way monolith - pick any other one
  1638. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1639. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1640. else
  1641. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1642. break;
  1643. case 103: //find nearest subterranean gate on the other level
  1644. {
  1645. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1646. for(int i=0; i<objs[103][0].size(); i++)
  1647. {
  1648. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1649. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1650. if(hlp<best.second)
  1651. {
  1652. best.first = objs[103][0][i];
  1653. best.second = hlp;
  1654. }
  1655. }
  1656. if(best.first<0)
  1657. return;
  1658. else
  1659. destinationid = best.first;
  1660. break;
  1661. }
  1662. }
  1663. if(destinationid < 0)
  1664. {
  1665. tlog2 << "Cannot find exit... :( \n";
  1666. return;
  1667. }
  1668. cb->moveHero(h->id,
  1669. (ID!=103)
  1670. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1671. : (cb->getObj(destinationid)->pos),
  1672. true);
  1673. }
  1674. void CGTeleport::initObj()
  1675. {
  1676. objs[ID][subID].push_back(id);
  1677. }
  1678. void CGArtifact::initObj()
  1679. {
  1680. blockVisit = true;
  1681. if(ID == 5)
  1682. hoverName = VLC->arth->artifacts[subID].Name();
  1683. }
  1684. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1685. {
  1686. if(!army.slots.size())
  1687. {
  1688. if(ID == 5)
  1689. {
  1690. InfoWindow iw;
  1691. iw.player = h->tempOwner;
  1692. iw.components.push_back(Component(4,subID,0,0));
  1693. if(message.length())
  1694. iw.text << message;
  1695. else
  1696. iw.text << std::pair<ui8,ui32>(12,subID);
  1697. cb->showInfoDialog(&iw);
  1698. }
  1699. pick(h);
  1700. }
  1701. else
  1702. {
  1703. if(message.size())
  1704. {
  1705. BlockingDialog ynd(true,false);
  1706. ynd.player = h->getOwner();
  1707. ynd.text << message;
  1708. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1709. }
  1710. else
  1711. {
  1712. fightForArt(0,h);
  1713. }
  1714. }
  1715. }
  1716. void CGArtifact::pick(const CGHeroInstance * h) const
  1717. {
  1718. if(ID == 5) //Artifact
  1719. {
  1720. cb->giveHeroArtifact(subID,h->id,-2);
  1721. }
  1722. else if(ID == 93) // Spell scroll
  1723. {
  1724. //TODO: support for the spell scroll
  1725. }
  1726. cb->removeObject(id);
  1727. }
  1728. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  1729. {
  1730. if(agreed)
  1731. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1732. }
  1733. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1734. {
  1735. if(result->winner == 0) //attacker won
  1736. pick(h);
  1737. }
  1738. void CGPickable::initObj()
  1739. {
  1740. blockVisit = true;
  1741. switch(ID)
  1742. {
  1743. case 12: //campfire
  1744. val2 = (ran()%3) + 4; //4 - 6
  1745. val1 = val2 * 100;
  1746. type = ran()%6; //given resource
  1747. break;
  1748. case 101: //treasure chest
  1749. {
  1750. int hlp = ran()%100;
  1751. if(hlp >= 95)
  1752. {
  1753. type = 1;
  1754. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1755. return;
  1756. }
  1757. else if (hlp >= 65)
  1758. {
  1759. val1 = 2000;
  1760. }
  1761. else if(hlp >= 33)
  1762. {
  1763. val1 = 1500;
  1764. }
  1765. else
  1766. {
  1767. val1 = 1000;
  1768. }
  1769. val2 = val1 - 500;
  1770. type = 0;
  1771. break;
  1772. }
  1773. }
  1774. }
  1775. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1776. {
  1777. switch(ID)
  1778. {
  1779. case 12: //campfire
  1780. {
  1781. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1782. cb->giveResource(h->tempOwner,6,val1);//gold
  1783. InfoWindow iw;
  1784. iw.player = h->tempOwner;
  1785. iw.components.push_back(Component(2,6,val1,0));
  1786. iw.components.push_back(Component(2,type,val2,0));
  1787. iw.text << std::pair<ui8,ui32>(11,23);
  1788. cb->showInfoDialog(&iw);
  1789. break;
  1790. }
  1791. case 101: //treasure chest
  1792. {
  1793. if (subID) //not OH3 treasure chest
  1794. {
  1795. tlog2 << "Not supported WoG treasure chest!\n";
  1796. return;
  1797. }
  1798. if(type) //there is an artifact
  1799. {
  1800. cb->giveHeroArtifact(val1,h->id,-2);
  1801. InfoWindow iw;
  1802. iw.player = h->tempOwner;
  1803. iw.components.push_back(Component(4,val1,1,0));
  1804. iw.text << std::pair<ui8,ui32>(11,145);
  1805. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1806. cb->showInfoDialog(&iw);
  1807. break;
  1808. }
  1809. else
  1810. {
  1811. BlockingDialog sd(false,true);
  1812. sd.player = h->tempOwner;
  1813. sd.text << std::pair<ui8,ui32>(11,146);
  1814. sd.components.push_back(Component(2,6,val1,0));
  1815. sd.components.push_back(Component(5,0,val2,0));
  1816. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1817. cb->showBlockingDialog(&sd,fun);
  1818. return;
  1819. }
  1820. }
  1821. }
  1822. cb->removeObject(id);
  1823. }
  1824. void CGPickable::chosen( int which, int heroID ) const
  1825. {
  1826. switch(which)
  1827. {
  1828. case 1: //player pick gold
  1829. cb->giveResource(cb->getOwner(heroID),6,val1);
  1830. break;
  1831. case 2: //player pick exp
  1832. cb->changePrimSkill(heroID, 4, val2);
  1833. break;
  1834. default:
  1835. throw std::string("Unhandled treasure choice");
  1836. }
  1837. cb->removeObject(id);
  1838. }
  1839. void CGWitchHut::initObj()
  1840. {
  1841. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1842. }
  1843. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1844. {
  1845. InfoWindow iw;
  1846. iw.player = h->getOwner();
  1847. if(!hasVisited(h->tempOwner))
  1848. cb->setObjProperty(id,10,h->tempOwner);
  1849. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1850. {
  1851. iw.text << std::pair<ui8,ui32>(11,172);
  1852. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1853. }
  1854. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1855. {
  1856. iw.text << std::pair<ui8,ui32>(11,173);
  1857. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1858. }
  1859. else //give sec skill
  1860. {
  1861. iw.components.push_back(Component(1, ability, 1, 0));
  1862. iw.text << std::pair<ui8,ui32>(11,171);
  1863. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1864. cb->changeSecSkill(h->id,ability,1,true);
  1865. }
  1866. cb->showInfoDialog(&iw);
  1867. }
  1868. const std::string & CGWitchHut::getHoverText() const
  1869. {
  1870. hoverName = VLC->generaltexth->names[ID];
  1871. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1872. {
  1873. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  1874. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  1875. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  1876. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  1877. }
  1878. return hoverName;
  1879. }
  1880. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1881. {
  1882. }
  1883. void CGDwelling::initObj()
  1884. {
  1885. }
  1886. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1887. {
  1888. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1889. int messageID, bonusType, bonusVal;
  1890. int bonusMove = 0;
  1891. InfoWindow iw;
  1892. iw.player = h->tempOwner;
  1893. GiveBonus gbonus;
  1894. gbonus.hid = h->id;
  1895. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1896. gbonus.bonus.source = HeroBonus::OBJECT;
  1897. gbonus.bonus.id = ID;
  1898. switch(ID)
  1899. {
  1900. case 14: //swan pond
  1901. messageID = 29;
  1902. gbonus.bonus.type = HeroBonus::LUCK;
  1903. gbonus.bonus.val = 2;
  1904. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1905. bonusMove = -h->movement;
  1906. break;
  1907. case 28: //Faerie Ring
  1908. messageID = 49;
  1909. gbonus.bonus.type = HeroBonus::LUCK;
  1910. gbonus.bonus.val = 1;
  1911. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1912. break;
  1913. case 30: //fountain of fortune
  1914. messageID = 55;
  1915. gbonus.bonus.type = HeroBonus::LUCK;
  1916. gbonus.bonus.val = rand()%5 - 1;
  1917. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  1918. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  1919. break;
  1920. case 38: //idol of fortune
  1921. messageID = 62;
  1922. if(cb->getDate(1) == 7) //7th day of week
  1923. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1924. else
  1925. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  1926. gbonus.bonus.val = 1;
  1927. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  1928. break;
  1929. case 64: //Rally Flag
  1930. messageID = 111;
  1931. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1932. gbonus.bonus.val = 1;
  1933. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  1934. bonusMove = 400;
  1935. break;
  1936. case 56: //oasis
  1937. messageID = 95;
  1938. gbonus.bonus.type = HeroBonus::MORALE;
  1939. gbonus.bonus.val = 1;
  1940. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  1941. bonusMove = 800;
  1942. break;
  1943. case 96: //temple
  1944. messageID = 140;
  1945. gbonus.bonus.type = HeroBonus::MORALE;
  1946. if(cb->getDate(1)==7) //sunday
  1947. {
  1948. gbonus.bonus.val = 2;
  1949. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  1950. }
  1951. else
  1952. {
  1953. gbonus.bonus.val = 1;
  1954. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  1955. }
  1956. break;
  1957. case 110://Watering Hole
  1958. messageID = 166;
  1959. gbonus.bonus.type = HeroBonus::MORALE;
  1960. gbonus.bonus.val = 1;
  1961. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  1962. bonusMove = 400;
  1963. break;
  1964. case 31: //Fountain of Youth
  1965. messageID = 57;
  1966. gbonus.bonus.type = HeroBonus::MORALE;
  1967. gbonus.bonus.val = 1;
  1968. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  1969. bonusMove = 400;
  1970. break;
  1971. }
  1972. if(visited)
  1973. {
  1974. if(ID==64 || ID==96 || ID==56)
  1975. messageID--;
  1976. else
  1977. messageID++;
  1978. }
  1979. else
  1980. {
  1981. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1982. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  1983. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1984. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  1985. cb->giveHeroBonus(&gbonus);
  1986. if(bonusMove) //swan pond - take all move points
  1987. {
  1988. SetMovePoints smp;
  1989. smp.hid = h->id;
  1990. smp.val = h->movement + bonusMove;
  1991. cb->setMovePoints(&smp);
  1992. }
  1993. }
  1994. iw.text << std::pair<ui8,ui32>(11,messageID);
  1995. cb->showInfoDialog(&iw);
  1996. }
  1997. const std::string & CGBonusingObject::getHoverText() const
  1998. {
  1999. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2000. hoverName = VLC->generaltexth->names[ID];
  2001. if(h)
  2002. {
  2003. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2004. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2005. else
  2006. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2007. }
  2008. return hoverName;
  2009. }
  2010. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2011. {
  2012. int message;
  2013. InfoWindow iw;
  2014. iw.player = h->tempOwner;
  2015. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2016. {
  2017. message = 78;
  2018. }
  2019. else if(h->mana < h->manaLimit())
  2020. {
  2021. GiveBonus gbonus;
  2022. gbonus.bonus.type = HeroBonus::NONE;
  2023. gbonus.hid = h->id;
  2024. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  2025. gbonus.bonus.source = HeroBonus::OBJECT;
  2026. gbonus.bonus.id = ID;
  2027. cb->giveHeroBonus(&gbonus);
  2028. cb->setManaPoints(h->id,h->manaLimit());
  2029. message = 77;
  2030. }
  2031. else
  2032. {
  2033. message = 79;
  2034. }
  2035. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2036. cb->showInfoDialog(&iw);
  2037. }
  2038. const std::string & CGMagicWell::getHoverText() const
  2039. {
  2040. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2041. hoverName = VLC->generaltexth->names[ID];
  2042. if(h)
  2043. {
  2044. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2045. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2046. else
  2047. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2048. }
  2049. return hoverName;
  2050. }
  2051. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2052. {
  2053. if(!(availableFor & (1 << h->tempOwner)))
  2054. return;
  2055. if(cb->getPlayerSettings(h->tempOwner)->human)
  2056. {
  2057. if(humanActivate)
  2058. activated(h);
  2059. }
  2060. else if(computerActivate)
  2061. activated(h);
  2062. }
  2063. void CGEvent::endBattle( BattleResult *result ) const
  2064. {
  2065. if(result->winner)
  2066. return;
  2067. //give
  2068. }
  2069. void CGEvent::activated( const CGHeroInstance * h ) const
  2070. {
  2071. InfoWindow iw;
  2072. iw.player = h->tempOwner;
  2073. iw.text << message;
  2074. cb->showInfoDialog(&iw);
  2075. if(guarders)
  2076. cb->startBattleI(h->id,guarders,pos,boost::bind(&CGEvent::endBattle,this,_1));
  2077. cb->removeObject(id);
  2078. }
  2079. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2080. {
  2081. InfoWindow iw;
  2082. iw.player = h->tempOwner;
  2083. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2084. cb->showInfoDialog(&iw);
  2085. FoWChange fw;
  2086. fw.player = h->tempOwner;
  2087. fw.mode = 1;
  2088. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2089. cb->sendAndApply(&fw);
  2090. }
  2091. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2092. {
  2093. if(spell == 255)
  2094. {
  2095. tlog1 << "Not initialized shrine visited!\n";
  2096. return;
  2097. }
  2098. if(!hasVisited(h->tempOwner))
  2099. cb->setObjProperty(id,10,h->tempOwner);
  2100. InfoWindow iw;
  2101. iw.player = h->getOwner();
  2102. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2103. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2104. iw.text << ".";
  2105. if(!h->getArt(17)) //no spellbook
  2106. {
  2107. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2108. }
  2109. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2110. {
  2111. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2112. }
  2113. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2114. {
  2115. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2116. }
  2117. else //give spell
  2118. {
  2119. std::set<ui32> spells;
  2120. spells.insert(spell);
  2121. cb->changeSpells(h->id,true,spells);
  2122. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2123. }
  2124. cb->showInfoDialog(&iw);
  2125. }
  2126. void CGShrine::initObj()
  2127. {
  2128. if(spell == 255) //spell not set
  2129. {
  2130. int level = ID-87;
  2131. std::vector<ui32> possibilities;
  2132. //add all allowed spells of wanted level
  2133. for(int i=0; i<SPELLS_QUANTITY; i++)
  2134. {
  2135. if(VLC->spellh->spells[i].level == level
  2136. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2137. {
  2138. possibilities.push_back(VLC->spellh->spells[i].id);
  2139. }
  2140. }
  2141. if(!possibilities.size())
  2142. {
  2143. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2144. return;
  2145. }
  2146. spell = possibilities[ran() % possibilities.size()];
  2147. }
  2148. }
  2149. const std::string & CGShrine::getHoverText() const
  2150. {
  2151. hoverName = VLC->generaltexth->names[ID];
  2152. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2153. {
  2154. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2155. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2156. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2157. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2158. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2159. }
  2160. return hoverName;
  2161. }
  2162. void CGSignBottle::initObj()
  2163. {
  2164. //if no text is set than we pick random from the predefined ones
  2165. if(!message.size())
  2166. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2167. }
  2168. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2169. {
  2170. InfoWindow iw;
  2171. iw.player = h->getOwner();
  2172. iw.text << message;
  2173. cb->showInfoDialog(&iw);
  2174. }
  2175. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2176. {
  2177. }
  2178. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2179. {
  2180. int type = bonusType;
  2181. int bid = bonusID;
  2182. //check if the bonus if applicable, if not - give primary skill (always possible)
  2183. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2184. if((type == 1
  2185. && ((ssl == 3) || (!ssl && h->secSkills.size() == SKILL_PER_HERO))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  2186. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2187. {
  2188. type = 0;
  2189. bid = ran() % PRIMARY_SKILLS;
  2190. }
  2191. InfoWindow iw;
  2192. iw.player = h->getOwner();
  2193. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2194. switch (type)
  2195. {
  2196. case 0:
  2197. cb->changePrimSkill(h->id,bid,+1);
  2198. iw.components.push_back(Component(Component::PRIM_SKILL,bid,1,0));
  2199. break;
  2200. case 1:
  2201. {
  2202. cb->changeSecSkill(h->id,bid,ssl+1);
  2203. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2204. }
  2205. break;
  2206. case 2:
  2207. {
  2208. std::set<ui32> hlp;
  2209. hlp.insert(bid);
  2210. cb->changeSpells(h->id,true,hlp);
  2211. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2212. }
  2213. break;
  2214. default:
  2215. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2216. return;
  2217. }
  2218. cb->showInfoDialog(&iw);
  2219. cb->removeObject(id);
  2220. }
  2221. void CGScholar::initObj()
  2222. {
  2223. blockVisit = true;
  2224. if(bonusType == 255)
  2225. {
  2226. bonusType = ran()%3;
  2227. switch(bonusType)
  2228. {
  2229. case 0:
  2230. bonusID = ran() % PRIMARY_SKILLS;
  2231. break;
  2232. case 1:
  2233. bonusID = ran() % SKILL_QUANTITY;
  2234. break;
  2235. case 2:
  2236. bonusID = ran() % SPELLS_QUANTITY;
  2237. break;
  2238. }
  2239. }
  2240. }
  2241. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2242. {
  2243. int txtid = -1;
  2244. switch(ID)
  2245. {
  2246. case 22: //Corpse
  2247. txtid = 37;
  2248. break;
  2249. case 39: //Lean To
  2250. txtid = 64;
  2251. break;
  2252. case 105://Wagon
  2253. txtid = 154;
  2254. break;
  2255. case 108:
  2256. break;
  2257. default:
  2258. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  2259. return;
  2260. }
  2261. if(ID == 108)//Warrior's Tomb
  2262. {
  2263. //ask if player wants to search the Tomb
  2264. BlockingDialog bd(true, false);
  2265. bd.player = h->getOwner();
  2266. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  2267. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  2268. return;
  2269. }
  2270. InfoWindow iw;
  2271. iw.player = h->getOwner();
  2272. if(players.size()) //we have been already visited...
  2273. {
  2274. txtid++;
  2275. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  2276. txtid++;
  2277. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2278. }
  2279. else //first visit - give bonus!
  2280. {
  2281. if(ID == 105 && artOrRes == 1)
  2282. {
  2283. txtid++;
  2284. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2285. }
  2286. switch(artOrRes)
  2287. {
  2288. case 0:
  2289. txtid++;
  2290. break;
  2291. case 1: //art
  2292. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2293. cb->giveHeroArtifact(bonusType,h->id,-2);
  2294. break;
  2295. case 2: //res
  2296. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  2297. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  2298. break;
  2299. }
  2300. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2301. }
  2302. cb->showInfoDialog(&iw);
  2303. cb->setObjProperty(id,10,h->getOwner());
  2304. }
  2305. const std::string & CGOnceVisitable::getHoverText() const
  2306. {
  2307. hoverName = VLC->generaltexth->names[ID] + " ";
  2308. hoverName += (hasVisited(cb->getCurrentPlayer())
  2309. ? (VLC->generaltexth->allTexts[352]) //visited
  2310. : ( VLC->generaltexth->allTexts[353])); //not visited
  2311. return hoverName;
  2312. }
  2313. void CGOnceVisitable::initObj()
  2314. {
  2315. switch(ID)
  2316. {
  2317. case 22: //Corpse
  2318. {
  2319. blockVisit = true;
  2320. int hlp = ran()%100;
  2321. if(hlp < 20)
  2322. {
  2323. artOrRes = 1;
  2324. std::vector<CArtifact*> arts;
  2325. cb->getAllowed(arts, ART_TREASURE | ART_MINOR | ART_MAJOR);
  2326. bonusType = arts[ran() % arts.size()]->id;
  2327. }
  2328. else
  2329. {
  2330. artOrRes = 0;
  2331. }
  2332. }
  2333. break;
  2334. case 39: //Lean To
  2335. {
  2336. artOrRes = 2;
  2337. bonusType = ran()%6; //any basic resource without gold
  2338. bonusVal = ran()%4 + 1;
  2339. break;
  2340. }
  2341. case 108://Warrior's Tomb
  2342. {
  2343. artOrRes = 1;
  2344. std::vector<CArtifact*> arts;
  2345. int hlp = ran()%100;
  2346. if(hlp < 30)
  2347. cb->getAllowed(arts,ART_TREASURE);
  2348. else if(hlp < 80)
  2349. cb->getAllowed(arts,ART_MINOR);
  2350. else if(hlp < 95)
  2351. cb->getAllowed(arts,ART_MAJOR);
  2352. else
  2353. cb->getAllowed(arts,ART_RELIC);
  2354. bonusType = arts[ran() % arts.size()]->id;
  2355. }
  2356. break;
  2357. case 105://Wagon
  2358. {
  2359. int hlp = ran()%100;
  2360. if(hlp < 10)
  2361. {
  2362. artOrRes = 0; // nothing... :(
  2363. }
  2364. else if(hlp < 50) //minor or treasure art
  2365. {
  2366. artOrRes = 1;
  2367. std::vector<CArtifact*> arts;
  2368. cb->getAllowed(arts, ART_TREASURE | ART_MINOR);
  2369. bonusType = arts[ran() % arts.size()]->id;
  2370. }
  2371. else //2 - 5 of non-gold resource
  2372. {
  2373. artOrRes = 2;
  2374. bonusType = ran()%6;
  2375. bonusVal = ran()%4 + 2;
  2376. }
  2377. break;
  2378. }
  2379. }
  2380. }
  2381. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  2382. {
  2383. if(accept)
  2384. {
  2385. InfoWindow iw;
  2386. iw.player = h->getOwner();
  2387. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  2388. if(players.size()) //we've been already visited, player found nothing
  2389. {
  2390. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  2391. }
  2392. else //first visit - give artifact
  2393. {
  2394. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  2395. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2396. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2397. cb->giveHeroArtifact(bonusType,h->id,-2);
  2398. }
  2399. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  2400. {
  2401. //ruin morale
  2402. GiveBonus gb;
  2403. gb.hid = h->id;
  2404. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  2405. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  2406. cb->giveHeroBonus(&gb);
  2407. }
  2408. cb->showInfoDialog(&iw);
  2409. //add player to the visitors (for visited tooltop)
  2410. cb->setObjProperty(id,10,h->getOwner());
  2411. }
  2412. }