CObjectHandler.cpp 69 KB

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