CObjectHandler.cpp 79 KB

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