CObjectHandler.cpp 78 KB

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