CObjectHandler.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981
  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/assign/std/vector.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/random/linear_congruential.hpp>
  15. #include "CTownHandler.h"
  16. #include "CArtHandler.h"
  17. #include "CSoundBase.h"
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../lib/IGameCallback.h"
  20. #include "../lib/CGameState.h"
  21. #include "../lib/NetPacks.h"
  22. #include "../StartInfo.h"
  23. #include "../lib/map.h"
  24. using namespace boost::assign;
  25. /*
  26. * CObjectHandler.cpp, part of VCMI engine
  27. *
  28. * Authors: listed in file AUTHORS in main folder
  29. *
  30. * License: GNU General Public License v2.0 or later
  31. * Full text of license available in license.txt file, in main folder
  32. *
  33. */
  34. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  35. IGameCallback * IObjectInterface::cb = NULL;
  36. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  37. extern CLodHandler * bitmaph;
  38. extern boost::rand48 ran;
  39. std::map <ui8, std::set <ui8> > CGKeys::playerKeyMap;
  40. std::map <si32, std::vector<si32> > CGMagi::eyelist;
  41. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  42. {};
  43. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  44. {};
  45. void IObjectInterface::newTurn () const
  46. {};
  47. IObjectInterface::~IObjectInterface()
  48. {}
  49. IObjectInterface::IObjectInterface()
  50. {}
  51. void IObjectInterface::initObj()
  52. {}
  53. void IObjectInterface::setProperty( ui8 what, ui32 val )
  54. {}
  55. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  56. {
  57. if(what == 10)
  58. players.insert(val);
  59. }
  60. bool CPlayersVisited::hasVisited( ui8 player ) const
  61. {
  62. return vstd::contains(players,player);
  63. }
  64. void CObjectHandler::loadObjects()
  65. {
  66. {
  67. tlog5 << "\t\tReading cregens \n";
  68. cregens.resize(110); //TODO: hardcoded value - change
  69. for(size_t i=0; i < cregens.size(); ++i)
  70. {
  71. cregens[i]=-1;
  72. }
  73. std::ifstream ifs("config/cregens.txt");
  74. while(!ifs.eof())
  75. {
  76. int dw, cr;
  77. ifs >> dw >> cr;
  78. cregens[dw]=cr;
  79. }
  80. tlog5 << "\t\tDone loading objects!\n";
  81. }
  82. std::string banksConfig = bitmaph->getTextFile("ZCRBANK.TXT");
  83. //TODO: parse to banksInfo
  84. }
  85. int CGObjectInstance::getOwner() const
  86. {
  87. //if (state)
  88. // return state->owner;
  89. //else
  90. return tempOwner; //won't have owner
  91. }
  92. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  93. {
  94. pos = int3(-1,-1,-1);
  95. //std::cout << "Tworze obiekt "<<this<<std::endl;
  96. //state = new CLuaObjectScript();
  97. ID = subID = id = -1;
  98. defInfo = NULL;
  99. info = NULL;
  100. tempOwner = 254;
  101. blockVisit = false;
  102. }
  103. CGObjectInstance::~CGObjectInstance()
  104. {
  105. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  106. //if (state)
  107. // delete state;
  108. //state=NULL;
  109. }
  110. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  111. //{
  112. // pos = right.pos;
  113. // ID = right.ID;
  114. // subID = right.subID;
  115. // id = right.id;
  116. // defInfo = right.defInfo;
  117. // info = right.info;
  118. // blockVisit = right.blockVisit;
  119. // //state = new CLuaObjectScript(right.state->);
  120. // //*state = *right.state;
  121. // //state = right.state;
  122. // tempOwner = right.tempOwner;
  123. //}
  124. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  125. //{
  126. // pos = right.pos;
  127. // ID = right.ID;
  128. // subID = right.subID;
  129. // id = right.id;
  130. // defInfo = right.defInfo;
  131. // info = right.info;
  132. // blockVisit = right.blockVisit;
  133. // //state = new CLuaObjectScript();
  134. // //*state = *right.state;
  135. // tempOwner = right.tempOwner;
  136. // return *this;
  137. //}
  138. const std::string & CGObjectInstance::getHoverText() const
  139. {
  140. return hoverName;
  141. }
  142. void CGObjectInstance::setOwner(int ow)
  143. {
  144. //if (state)
  145. // state->owner = ow;
  146. //else
  147. tempOwner = ow;
  148. }
  149. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  150. {
  151. return defInfo->width;
  152. }
  153. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  154. {
  155. return defInfo->height;
  156. }
  157. 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)
  158. {
  159. if(defInfo==NULL)
  160. {
  161. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  162. return false;
  163. }
  164. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  165. {
  166. return true;
  167. }
  168. return false;
  169. }
  170. bool CGObjectInstance::blockingAt(int x, int y) const
  171. {
  172. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  173. return false;
  174. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  175. return false;
  176. return true;
  177. }
  178. bool CGObjectInstance::coveringAt(int x, int y) const
  179. {
  180. if((defInfo->coverageMap[y] >> (7-(x) )) & 1)
  181. return true;
  182. return false;
  183. }
  184. std::set<int3> CGObjectInstance::getBlockedPos() const
  185. {
  186. std::set<int3> ret;
  187. for(int w=0; w<getWidth(); ++w)
  188. {
  189. for(int h=0; h<getHeight(); ++h)
  190. {
  191. if(blockingAt(w, h))
  192. ret.insert(int3(pos.x - getWidth() + w + 1, pos.y - getHeight() + h + 1, pos.z));
  193. }
  194. }
  195. return ret;
  196. }
  197. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  198. {
  199. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  200. return true;
  201. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  202. return false;
  203. if(this->pos.y<cmp.pos.y)
  204. return true;
  205. if(this->pos.y>cmp.pos.y)
  206. return false;
  207. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  208. return true;
  209. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  210. return false;
  211. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  212. return true;
  213. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  214. return false;
  215. if(this->pos.x<cmp.pos.x)
  216. return true;
  217. return false;
  218. }
  219. void CGObjectInstance::initObj()
  220. {
  221. }
  222. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  223. {
  224. switch(what)
  225. {
  226. case 1:
  227. tempOwner = val;
  228. break;
  229. case 2:
  230. blockVisit = val;
  231. break;
  232. case 6:
  233. ID = val;
  234. break;
  235. }
  236. setPropertyDer(what, val);
  237. }
  238. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  239. {}
  240. int3 CGObjectInstance::getSightCenter() const
  241. {
  242. //return vistiable tile if possible
  243. for(int i=0; i < 8; i++)
  244. for(int j=0; j < 6; j++)
  245. if(visitableAt(i,j))
  246. return(pos + int3(i-7, j-5, 0));
  247. return pos;
  248. }
  249. int CGObjectInstance::getSightRadious() const
  250. {
  251. return 3;
  252. }
  253. int3 CGObjectInstance::getVisitableOffset() const
  254. {
  255. for(int y = 0; y < 6; y++)
  256. for (int x = 0; x < 8; x++)
  257. if((defInfo->visitMap[5-y] >> x) & 1)
  258. return int3(x,y,0);
  259. tlog2 << "Warning: getVisitableOffset called on non-visitable obj!\n";
  260. return int3(-1,-1,-1);
  261. }
  262. void CGObjectInstance::getNameVis( std::string &hname ) const
  263. {
  264. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  265. hname = VLC->generaltexth->names[ID];
  266. if(h)
  267. {
  268. if(!h->getBonus(HeroBonus::OBJECT,ID))
  269. hname += " " + VLC->generaltexth->allTexts[353]; //not visited
  270. else
  271. hname += " " + VLC->generaltexth->allTexts[352]; //visited
  272. }
  273. }
  274. void CGObjectInstance::giveDummyBonus(int heroID, ui8 duration) const
  275. {
  276. GiveBonus gbonus;
  277. gbonus.bonus.type = HeroBonus::NONE;
  278. gbonus.hid = heroID;
  279. gbonus.bonus.duration = duration;
  280. gbonus.bonus.source = HeroBonus::OBJECT;
  281. gbonus.bonus.id = ID;
  282. cb->giveHeroBonus(&gbonus);
  283. }
  284. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  285. {
  286. }
  287. static int lowestSpeed(const CGHeroInstance * chi)
  288. {
  289. if(!chi->army.slots.size())
  290. {
  291. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  292. return 20;
  293. }
  294. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  295. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  296. for (;i!=chi->army.slots.end();i++)
  297. {
  298. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  299. }
  300. return ret;
  301. }
  302. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  303. {
  304. //TODO: check if all creatures are on its native terrain and change cost appropriately
  305. //base move cost
  306. unsigned ret = 100;
  307. //if there is road both on dest and src tiles - use road movement cost
  308. if(dest.malle && from.malle)
  309. {
  310. int road = std::min(dest.malle,from.malle); //used road ID
  311. switch(road)
  312. {
  313. case TerrainTile::dirtRoad:
  314. ret = 75;
  315. break;
  316. case TerrainTile::grazvelRoad:
  317. ret = 65;
  318. break;
  319. case TerrainTile::cobblestoneRoad:
  320. ret = 50;
  321. break;
  322. default:
  323. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  324. break;
  325. }
  326. }
  327. else
  328. {
  329. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  330. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  331. }
  332. return ret;
  333. }
  334. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  335. {
  336. unsigned int sl = 100;
  337. for(size_t h=0; h < army.slots.size(); ++h)
  338. {
  339. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  340. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  341. }
  342. return sl;
  343. }
  344. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  345. {
  346. if (toh3m)
  347. {
  348. src.x+=1;
  349. return src;
  350. }
  351. else
  352. {
  353. src.x-=1;
  354. return src;
  355. }
  356. }
  357. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  358. {
  359. if (h3m)
  360. {
  361. return pos;
  362. }
  363. else
  364. {
  365. return convertPosition(pos,false);
  366. }
  367. }
  368. si32 CGHeroInstance::manaLimit() const
  369. {
  370. double modifier = 1.0;
  371. switch(getSecSkillLevel(24)) //intelligence level
  372. {
  373. case 1: modifier+=0.25; break;
  374. case 2: modifier+=0.5; break;
  375. case 3: modifier+=1.0; break;
  376. }
  377. return si32(10*getPrimSkillLevel(3)*modifier);
  378. }
  379. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  380. //{
  381. // if (h3m)
  382. // pos = Pos;
  383. // else
  384. // pos = convertPosition(Pos,true);
  385. //}
  386. bool CGHeroInstance::canWalkOnSea() const
  387. {
  388. //TODO: write it - it should check if hero is flying, or something similar
  389. return false;
  390. }
  391. int CGHeroInstance::getPrimSkillLevel(int id) const
  392. {
  393. int ret = primSkills[id];
  394. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  395. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  396. ret += i->val;
  397. amax(ret, id/2);//minimum value for attack and defense is 0 and for spell power and knowledge - 1
  398. return ret;
  399. }
  400. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  401. {
  402. for(size_t i=0; i < secSkills.size(); ++i)
  403. if(secSkills[i].first==ID)
  404. return secSkills[i].second;
  405. return 0;
  406. }
  407. int CGHeroInstance::maxMovePoints(bool onLand) const
  408. {
  409. int ret = std::min(2000, 1270+70*lowestSpeed(this)),
  410. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  411. double modifier = 0;
  412. if(onLand)
  413. {
  414. //logistics:
  415. switch(getSecSkillLevel(2))
  416. {
  417. case 1:
  418. modifier = 0.1;
  419. break;
  420. case 2:
  421. modifier = 0.2;
  422. break;
  423. case 3:
  424. modifier = 0.3;
  425. break;
  426. }
  427. }
  428. else
  429. {
  430. //navigation:
  431. switch(getSecSkillLevel(5))
  432. {
  433. case 1:
  434. modifier = 0.5;
  435. break;
  436. case 2:
  437. modifier = 1.0;
  438. break;
  439. case 3:
  440. modifier = 1.5;
  441. break;
  442. }
  443. }
  444. return int(ret + ret*modifier) + bonus;
  445. }
  446. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  447. {
  448. if(pos<19)
  449. if(vstd::contains(artifWorn,pos))
  450. return artifWorn.find(pos)->second;
  451. else
  452. return -1;
  453. else
  454. if(pos-19 < artifacts.size())
  455. return artifacts[pos-19];
  456. else
  457. return -1;
  458. }
  459. const CArtifact * CGHeroInstance::getArt(int pos) const
  460. {
  461. int id = getArtAtPos(pos);
  462. if(id>=0)
  463. return &VLC->arth->artifacts[id];
  464. else
  465. return NULL;
  466. }
  467. int CGHeroInstance::getSpellSecLevel(int spell) const
  468. {
  469. int bestslvl = 0;
  470. if(VLC->spellh->spells[spell].air)
  471. if(getSecSkillLevel(15) >= bestslvl)
  472. {
  473. bestslvl = getSecSkillLevel(15);
  474. }
  475. if(VLC->spellh->spells[spell].fire)
  476. if(getSecSkillLevel(14) >= bestslvl)
  477. {
  478. bestslvl = getSecSkillLevel(14);
  479. }
  480. if(VLC->spellh->spells[spell].water)
  481. if(getSecSkillLevel(16) >= bestslvl)
  482. {
  483. bestslvl = getSecSkillLevel(16);
  484. }
  485. if(VLC->spellh->spells[spell].earth)
  486. if(getSecSkillLevel(17) >= bestslvl)
  487. {
  488. bestslvl = getSecSkillLevel(17);
  489. }
  490. return bestslvl;
  491. }
  492. CGHeroInstance::CGHeroInstance()
  493. {
  494. ID = HEROI_TYPE;
  495. tacticFormationEnabled = inTownGarrison = false;
  496. mana = movement = portrait = level = -1;
  497. isStanding = true;
  498. moveDir = 4;
  499. exp = 0xffffffff;
  500. visitedTown = NULL;
  501. type = NULL;
  502. boat = NULL;
  503. secSkills.push_back(std::make_pair(-1, -1));
  504. }
  505. void CGHeroInstance::initHero(int SUBID)
  506. {
  507. subID = SUBID;
  508. initHero();
  509. }
  510. void CGHeroInstance::initHero()
  511. {
  512. if(ID == HEROI_TYPE)
  513. initHeroDefInfo();
  514. if(!type)
  515. type = VLC->heroh->heroes[subID];
  516. artifWorn[16] = 3;
  517. if(type->startingSpell >= 0) //hero starts with a spell
  518. {
  519. artifWorn[17] = 0; //give him spellbook
  520. spells.insert(type->startingSpell);
  521. }
  522. if(portrait < 0 || portrait == 255)
  523. portrait = subID;
  524. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  525. {
  526. primSkills.resize(4);
  527. primSkills[0] = type->heroClass->initialAttack;
  528. primSkills[1] = type->heroClass->initialDefence;
  529. primSkills[2] = type->heroClass->initialPower;
  530. primSkills[3] = type->heroClass->initialKnowledge;
  531. }
  532. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  533. secSkills = type->secSkillsInit;
  534. if(mana < 0)
  535. mana = manaLimit();
  536. if (!name.length())
  537. name = type->name;
  538. if (exp == 0xffffffff)
  539. {
  540. exp=40+ (ran()) % 50;
  541. level = 1;
  542. }
  543. else
  544. {
  545. level = VLC->heroh->level(exp);
  546. }
  547. if (!army.slots.size()) //standard army//initial army
  548. {
  549. int pom, pom2=0;
  550. int x = 0; //how many stacks will hero receives <1 - 3>
  551. pom = ran()%100;
  552. if(pom < 9)
  553. x = 1;
  554. else if(pom < 79)
  555. x = 2;
  556. else
  557. x = 3;
  558. for(int x=0;x<3;x++)
  559. {
  560. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  561. if(pom>=145 && pom<=149) //war machine
  562. {
  563. pom2++;
  564. switch (pom)
  565. {
  566. case 145: //catapult
  567. artifWorn[16] = 3;
  568. break;
  569. default:
  570. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  571. break;
  572. }
  573. continue;
  574. }
  575. army.slots[x-pom2].first = pom;
  576. pom = type->highStack[x] - type->lowStack[x];
  577. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  578. army.formation = false;
  579. }
  580. }
  581. hoverName = VLC->generaltexth->allTexts[15];
  582. boost::algorithm::replace_first(hoverName,"%s",name);
  583. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  584. recreateArtBonuses();
  585. }
  586. void CGHeroInstance::initHeroDefInfo()
  587. {
  588. if(!defInfo || defInfo->id != HEROI_TYPE)
  589. {
  590. defInfo = new CGDefInfo();
  591. defInfo->id = HEROI_TYPE;
  592. defInfo->subid = subID;
  593. defInfo->printPriority = 0;
  594. defInfo->visitDir = 0xff;
  595. }
  596. for(int i=0;i<6;i++)
  597. {
  598. defInfo->blockMap[i] = 255;
  599. defInfo->visitMap[i] = 0;
  600. defInfo->coverageMap[i] = 0;
  601. }
  602. defInfo->handler=NULL;
  603. defInfo->blockMap[5] = 253;
  604. defInfo->visitMap[5] = 2;
  605. defInfo->coverageMap[4] = defInfo->coverageMap[5] = 224;
  606. }
  607. CGHeroInstance::~CGHeroInstance()
  608. {
  609. }
  610. bool CGHeroInstance::needsLastStack() const
  611. {
  612. return true;
  613. }
  614. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  615. {
  616. if(h == this) return; //exclude potential self-visiting
  617. if (ID == HEROI_TYPE) //hero
  618. {
  619. //TODO: check for allies
  620. if(tempOwner == h->tempOwner) //our hero
  621. {
  622. //exchange
  623. cb->heroExchange(id, h->id);
  624. }
  625. else
  626. {
  627. //battle
  628. cb->startBattleI(h, this);
  629. }
  630. }
  631. else if(ID == 62) //prison
  632. {
  633. InfoWindow iw;
  634. iw.player = h->tempOwner;
  635. iw.soundID = soundBase::ROGUE;
  636. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  637. {
  638. cb->changeObjPos(id,pos+int3(1,0,0),0);
  639. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  640. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  641. iw.text << std::pair<ui8,ui32>(11,102);
  642. }
  643. else //already 8 wandering heroes
  644. {
  645. iw.text << std::pair<ui8,ui32>(11,103);
  646. }
  647. cb->showInfoDialog(&iw);
  648. }
  649. }
  650. const std::string & CGHeroInstance::getBiography() const
  651. {
  652. if (biography.length())
  653. return biography;
  654. else
  655. return VLC->generaltexth->hTxts[subID].biography;
  656. }
  657. void CGHeroInstance::initObj()
  658. {
  659. blockVisit = true;
  660. }
  661. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  662. {
  663. int ret = 0;
  664. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  665. for(int i=0; i < mods.size(); i++)
  666. ret += mods[i].first;
  667. if(ret > 3)
  668. return 3;
  669. if(ret < -3)
  670. return -3;
  671. return ret;
  672. }
  673. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  674. {
  675. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  676. std::vector<std::pair<int,std::string> > ret;
  677. //various morale bonuses (from buildings, artifacts, etc)
  678. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  679. {
  680. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  681. {
  682. ret.push_back(std::make_pair(i->val, i->description));
  683. }
  684. }
  685. //leadership
  686. if(getSecSkillLevel(6))
  687. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  688. //town structures
  689. if(town && visitedTown)
  690. {
  691. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  692. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  693. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  694. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  695. }
  696. //number of alignments and presence of undead
  697. if(stack>=0)
  698. {
  699. bool archangelInArmy = false;
  700. std::set<si8> factions;
  701. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  702. {
  703. factions.insert(VLC->creh->creatures[i->second.first].faction);
  704. if(i->second.first == 13)
  705. archangelInArmy = true;
  706. }
  707. if(factions.size() == 1)
  708. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  709. else
  710. {
  711. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  712. {
  713. char buf[150];
  714. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  715. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  716. }
  717. else
  718. {
  719. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  720. }
  721. }
  722. if(vstd::contains(factions,4))
  723. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  724. if(archangelInArmy)
  725. {
  726. char buf[100];
  727. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  728. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  729. }
  730. }
  731. return ret;
  732. }
  733. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  734. {
  735. int ret = 0;
  736. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  737. for(int i=0; i < mods.size(); i++)
  738. ret += mods[i].first;
  739. if(ret > 3)
  740. return 3;
  741. if(ret < -3)
  742. return -3;
  743. return ret;
  744. }
  745. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  746. {
  747. std::vector<std::pair<int,std::string> > ret;
  748. //various morale bonuses (from buildings, artifacts, etc)
  749. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  750. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  751. ret.push_back(std::make_pair(i->val, i->description));
  752. //luck skill
  753. if(getSecSkillLevel(9))
  754. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  755. return ret;
  756. }
  757. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  758. {
  759. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  760. if(i->source == from && i->id == id)
  761. return &*i;
  762. return NULL;
  763. }
  764. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  765. {
  766. if(what == 3)
  767. army.slots[0].second = val;
  768. }
  769. double CGHeroInstance::getHeroStrength() const
  770. {
  771. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  772. }
  773. int CGHeroInstance::getTotalStrength() const
  774. {
  775. double ret = getHeroStrength() * getArmyStrength();
  776. return (int) ret;
  777. }
  778. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  779. {
  780. ui8 skill = 0; //skill level
  781. if(spell->fire)
  782. skill = std::max(skill,getSecSkillLevel(14));
  783. if(spell->air)
  784. skill = std::max(skill,getSecSkillLevel(15));
  785. if(spell->water)
  786. skill = std::max(skill,getSecSkillLevel(16));
  787. if(spell->earth)
  788. skill = std::max(skill,getSecSkillLevel(17));
  789. //bonuses (eg. from special terrains)
  790. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  791. if(spell->fire)
  792. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 1));
  793. if(spell->air)
  794. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 2));
  795. if(spell->water)
  796. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 4));
  797. if(spell->earth)
  798. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 8));
  799. return skill;
  800. }
  801. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  802. {
  803. if(!getArt(17)) //if hero has no spellbook
  804. return false;
  805. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  806. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  807. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  808. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  809. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  810. )
  811. return true;
  812. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  813. {
  814. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  815. {
  816. return true;
  817. }
  818. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  819. {
  820. return true;
  821. }
  822. }
  823. return false;
  824. }
  825. int3 CGHeroInstance::getSightCenter() const
  826. {
  827. return getPosition(false);
  828. }
  829. int CGHeroInstance::getSightRadious() const
  830. {
  831. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  832. }
  833. si32 CGHeroInstance::manaRegain() const
  834. {
  835. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  836. }
  837. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  838. {
  839. int ret = 0;
  840. if(subtype == -1)
  841. {
  842. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  843. if(i->type == type)
  844. ret += i->val;
  845. }
  846. else
  847. {
  848. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  849. if(i->type == type && i->subtype == subtype)
  850. ret += i->val;
  851. }
  852. return ret;
  853. }
  854. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  855. {
  856. if(subtype == -1) //any subtype
  857. {
  858. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  859. if(i->type == type)
  860. return true;
  861. }
  862. else //given subtype
  863. {
  864. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  865. if(i->type == type && i->subtype == subtype)
  866. return true;
  867. }
  868. return false;
  869. }
  870. si32 CGHeroInstance::getArtPos(int aid) const
  871. {
  872. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  873. if(i->second == aid)
  874. return i->first;
  875. return -1;
  876. }
  877. void CGHeroInstance::recreateArtBonuses()
  878. {
  879. //clear all bonuses from artifacts (if present) and give them again
  880. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  881. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  882. {
  883. CArtifact &art = VLC->arth->artifacts[ari->second];
  884. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  885. bonuses.push_back(*i);
  886. }
  887. }
  888. void CGDwelling::initObj()
  889. {
  890. switch(ID)
  891. {
  892. case 17:
  893. {
  894. int crid = VLC->objh->cregens[subID];
  895. CCreature *crs = &VLC->creh->creatures[crid];
  896. creatures.resize(1);
  897. creatures[0].second.push_back(crid);
  898. hoverName = VLC->generaltexth->creGens[subID];
  899. if(crs->level > 4)
  900. {
  901. army.slots[0].first = crs->idNumber;
  902. army.slots[0].second = (8 - crs->level) * 3;
  903. }
  904. }
  905. break;
  906. case 20:
  907. creatures.resize(4);
  908. if(subID == 1) //Golem Factory
  909. {
  910. creatures[0].second.push_back(32); //Stone Golem
  911. creatures[1].second.push_back(33); //Iron Golem
  912. creatures[2].second.push_back(116); //Gold Golem
  913. creatures[3].second.push_back(117); //Diamond Golem
  914. //guards
  915. army.slots[0].first = 116;
  916. army.slots[0].second = 9;
  917. army.slots[1].first = 117;
  918. army.slots[1].second = 6;
  919. }
  920. else if(subID == 0) // Elemental Conflux
  921. {
  922. creatures[0].second.push_back(112); //Air Elemental
  923. creatures[1].second.push_back(114); //Fire Elemental
  924. creatures[2].second.push_back(113); //Earth Elemental
  925. creatures[3].second.push_back(115); //Water Elemental
  926. //guards
  927. army.slots[0].first = 113;
  928. army.slots[0].second = 12;
  929. }
  930. else
  931. {
  932. assert(0);
  933. }
  934. hoverName = VLC->generaltexth->creGens4[subID];
  935. break;
  936. default:
  937. assert(0);
  938. break;
  939. }
  940. }
  941. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  942. {
  943. if(h->tempOwner != tempOwner && army) //object is guarded
  944. {
  945. BlockingDialog bd;
  946. bd.player = h->tempOwner;
  947. bd.flags = BlockingDialog::ALLOW_CANCEL;
  948. 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?
  949. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  950. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  951. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  952. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  953. return;
  954. }
  955. if(h->tempOwner != tempOwner)
  956. cb->setOwner(id, h->tempOwner);
  957. BlockingDialog bd;
  958. bd.player = h->tempOwner;
  959. bd.flags = BlockingDialog::ALLOW_CANCEL;
  960. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  961. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  962. for(size_t i = 0; i < creatures.size(); i++)
  963. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  964. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  965. }
  966. void CGDwelling::newTurn() const
  967. {
  968. if(cb->getDate(1) != 1) //not first day of week
  969. return;
  970. //town growths are handled separately
  971. if(ID == TOWNI_TYPE)
  972. return;
  973. bool change = false;
  974. SetAvailableCreatures sac;
  975. sac.creatures = creatures;
  976. sac.tid = id;
  977. for (size_t i = 0; i < creatures.size(); i++)
  978. {
  979. if(creatures[i].second.size())
  980. {
  981. if(false /*accumulate creatures*/)
  982. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  983. else
  984. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  985. change = true;
  986. }
  987. }
  988. if(change)
  989. cb->sendAndApply(&sac);
  990. }
  991. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  992. {
  993. if(!answer)
  994. return;
  995. int crid = creatures[0].second[0];
  996. CCreature *crs = &VLC->creh->creatures[crid];
  997. if(crs->level == 1) //first level - creatures are for free
  998. {
  999. if(creatures[0].first) //there are available creatures
  1000. {
  1001. int slot = h->army.getSlotFor(crid);
  1002. if(slot < 0) //no available slot
  1003. {
  1004. InfoWindow iw;
  1005. iw.player = h->tempOwner;
  1006. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1007. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1008. cb->showInfoDialog(&iw);
  1009. }
  1010. else //give creatures
  1011. {
  1012. SetAvailableCreatures sac;
  1013. sac.tid = id;
  1014. sac.creatures = creatures;
  1015. sac.creatures[0].first = 0;
  1016. SetGarrisons sg;
  1017. sg.garrs[h->id] = h->army;
  1018. sg.garrs[h->id].slots[slot].first = crid;
  1019. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1020. InfoWindow iw;
  1021. iw.player = h->tempOwner;
  1022. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1023. iw.text.addReplacement(creatures[0].first);
  1024. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1025. cb->showInfoDialog(&iw);
  1026. cb->sendAndApply(&sac);
  1027. cb->sendAndApply(&sg);
  1028. }
  1029. }
  1030. else //there no creatures
  1031. {
  1032. InfoWindow iw;
  1033. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1034. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1035. iw.player = h->tempOwner;
  1036. cb->sendAndApply(&iw);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. OpenWindow ow;
  1042. ow.id1 = id;
  1043. ow.id2 = h->id;
  1044. ow.window = ID == 17
  1045. ? OpenWindow::RECRUITMENT_FIRST
  1046. : OpenWindow::RECRUITMENT_ALL;
  1047. cb->sendAndApply(&ow);
  1048. }
  1049. }
  1050. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1051. {
  1052. if(answer)
  1053. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1054. }
  1055. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1056. {
  1057. if (result->winner == 0)
  1058. {
  1059. onHeroVisit(h);
  1060. }
  1061. }
  1062. int CGTownInstance::getSightRadious() const //returns sight distance
  1063. {
  1064. if (subID == 2 && (builtBuildings.find(21))!=builtBuildings.end())
  1065. return 20;
  1066. return 5;
  1067. }
  1068. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1069. {
  1070. if((builtBuildings.find(9))!=builtBuildings.end())
  1071. return 3;
  1072. if((builtBuildings.find(8))!=builtBuildings.end())
  1073. return 2;
  1074. if((builtBuildings.find(7))!=builtBuildings.end())
  1075. return 1;
  1076. return 0;
  1077. }
  1078. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1079. {
  1080. if ((builtBuildings.find(13))!=builtBuildings.end())
  1081. return 3;
  1082. if ((builtBuildings.find(12))!=builtBuildings.end())
  1083. return 2;
  1084. if ((builtBuildings.find(11))!=builtBuildings.end())
  1085. return 1;
  1086. if ((builtBuildings.find(10))!=builtBuildings.end())
  1087. return 0;
  1088. return -1;
  1089. }
  1090. int CGTownInstance::mageGuildLevel() const
  1091. {
  1092. if ((builtBuildings.find(4))!=builtBuildings.end())
  1093. return 5;
  1094. if ((builtBuildings.find(3))!=builtBuildings.end())
  1095. return 4;
  1096. if ((builtBuildings.find(2))!=builtBuildings.end())
  1097. return 3;
  1098. if ((builtBuildings.find(1))!=builtBuildings.end())
  1099. return 2;
  1100. if ((builtBuildings.find(0))!=builtBuildings.end())
  1101. return 1;
  1102. return 0;
  1103. }
  1104. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1105. {
  1106. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1107. }
  1108. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1109. {
  1110. return town->hordeLvl[HID];
  1111. }
  1112. int CGTownInstance::creatureGrowth(const int & level) const
  1113. {
  1114. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1115. switch(fortLevel())
  1116. {
  1117. case 3:
  1118. ret*=2;break;
  1119. case 2:
  1120. ret*=(1.5); break;
  1121. }
  1122. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  1123. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  1124. if(getHordeLevel(0)==level)
  1125. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1126. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1127. if(getHordeLevel(1)==level)
  1128. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1129. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1130. return ret;
  1131. }
  1132. int CGTownInstance::dailyIncome() const
  1133. {
  1134. int ret = 0;
  1135. if ((builtBuildings.find(26))!=builtBuildings.end())
  1136. ret+=5000;
  1137. if ((builtBuildings.find(13))!=builtBuildings.end())
  1138. ret+=4000;
  1139. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1140. ret+=2000;
  1141. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1142. ret+=1000;
  1143. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1144. ret+=500;
  1145. return ret;
  1146. }
  1147. bool CGTownInstance::hasFort() const
  1148. {
  1149. return (builtBuildings.find(7))!=builtBuildings.end();
  1150. }
  1151. bool CGTownInstance::hasCapitol() const
  1152. {
  1153. return (builtBuildings.find(13))!=builtBuildings.end();
  1154. }
  1155. CGTownInstance::CGTownInstance()
  1156. :IShipyard(this)
  1157. {
  1158. builded=-1;
  1159. destroyed=-1;
  1160. garrisonHero=NULL;
  1161. town=NULL;
  1162. visitingHero = NULL;
  1163. }
  1164. CGTownInstance::~CGTownInstance()
  1165. {}
  1166. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1167. {
  1168. if(checkGuild && mageGuildLevel() < level)
  1169. return 0;
  1170. int ret = 6 - level; //how many spells are available at this level
  1171. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1172. ret++;
  1173. return ret;
  1174. }
  1175. bool CGTownInstance::needsLastStack() const
  1176. {
  1177. if(garrisonHero)
  1178. return true;
  1179. else return false;
  1180. }
  1181. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1182. {
  1183. if(getOwner() != h->getOwner())
  1184. {
  1185. return;
  1186. }
  1187. cb->heroVisitCastle(id,h->id);
  1188. }
  1189. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1190. {
  1191. cb->stopHeroVisitCastle(id,h->id);
  1192. }
  1193. void CGTownInstance::initObj()
  1194. {
  1195. hoverName = name + ", " + town->Name();
  1196. creatures.resize(CREATURES_PER_TOWN);
  1197. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1198. {
  1199. if(creatureDwelling(i,false))
  1200. creatures[i].second.push_back(town->basicCreatures[i]);
  1201. if(creatureDwelling(i,true))
  1202. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1203. }
  1204. }
  1205. int3 CGTownInstance::getSightCenter() const
  1206. {
  1207. return pos - int3(2,0,0);
  1208. }
  1209. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1210. {
  1211. offsets += int3(-1,3,0), int3(-3,3,0);
  1212. }
  1213. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1214. {
  1215. if(visitors.find(h->id)==visitors.end())
  1216. {
  1217. onNAHeroVisit(h->id, false);
  1218. switch(ID)
  1219. {
  1220. case 102: //tree
  1221. case 4: //arena
  1222. case 41://library
  1223. case 47: //School of Magic
  1224. case 107://School of War
  1225. break;
  1226. default:
  1227. cb->setObjProperty(id,4,h->id); //add to the visitors
  1228. break;
  1229. }
  1230. }
  1231. else
  1232. {
  1233. onNAHeroVisit(h->id, true);
  1234. }
  1235. }
  1236. void CGVisitableOPH::initObj()
  1237. {
  1238. if(ID==102)
  1239. ttype = ran()%3;
  1240. else
  1241. ttype = -1;
  1242. }
  1243. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  1244. {
  1245. if(result) //player agreed to give res for exp
  1246. {
  1247. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1248. cb->changePrimSkill(heroID,4,expVal); //give exp
  1249. cb->setObjProperty(id,4,heroID); //add to the visitors
  1250. }
  1251. }
  1252. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1253. {
  1254. int id=0, subid=0, ot=0, val=1, sound = 0;
  1255. switch(ID)
  1256. {
  1257. case 4: //arena
  1258. sound = soundBase::NOMAD;
  1259. ot = 0;
  1260. break;
  1261. case 51: //mercenary camp
  1262. sound = soundBase::NOMAD;
  1263. subid=0;
  1264. ot=80;
  1265. break;
  1266. case 23: //marletto tower
  1267. sound = soundBase::NOMAD;
  1268. subid=1;
  1269. ot=39;
  1270. break;
  1271. case 61:
  1272. sound = soundBase::gazebo;
  1273. subid=2;
  1274. ot=100;
  1275. break;
  1276. case 32:
  1277. sound = soundBase::GETPROTECTION;
  1278. subid=3;
  1279. ot=59;
  1280. break;
  1281. case 100:
  1282. sound = soundBase::gazebo;
  1283. id=5;
  1284. ot=143;
  1285. val=1000;
  1286. break;
  1287. case 102:
  1288. sound = soundBase::gazebo;
  1289. id = 5;
  1290. subid = 1;
  1291. ot = 146;
  1292. val = 1;
  1293. break;
  1294. case 41:
  1295. sound = soundBase::gazebo;
  1296. ot = 66;
  1297. break;
  1298. case 47: //School of Magic
  1299. sound = soundBase::faerie;
  1300. ot = 71;
  1301. break;
  1302. case 107://School of War
  1303. sound = soundBase::MILITARY;
  1304. ot = 158;
  1305. break;
  1306. }
  1307. if (!alreadyVisited)
  1308. {
  1309. switch (ID)
  1310. {
  1311. case 4: //arena
  1312. {
  1313. BlockingDialog sd(false,true);
  1314. sd.soundID = sound;
  1315. sd.text << std::pair<ui8,ui32>(11,ot);
  1316. sd.components.push_back(Component(0,0,2,0));
  1317. sd.components.push_back(Component(0,1,2,0));
  1318. sd.player = cb->getOwner(heroID);
  1319. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1320. return;
  1321. }
  1322. case 51:
  1323. case 23:
  1324. case 61:
  1325. case 32:
  1326. {
  1327. cb->changePrimSkill(heroID,subid,val);
  1328. InfoWindow iw;
  1329. iw.soundID = sound;
  1330. iw.components.push_back(Component(0,subid,val,0));
  1331. iw.text << std::pair<ui8,ui32>(11,ot);
  1332. iw.player = cb->getOwner(heroID);
  1333. cb->showInfoDialog(&iw);
  1334. break;
  1335. }
  1336. case 100: //give exp
  1337. {
  1338. InfoWindow iw;
  1339. iw.soundID = sound;
  1340. iw.components.push_back(Component(id,subid,val,0));
  1341. iw.player = cb->getOwner(heroID);
  1342. iw.text << std::pair<ui8,ui32>(11,ot);
  1343. iw.soundID = soundBase::gazebo;
  1344. cb->showInfoDialog(&iw);
  1345. cb->changePrimSkill(heroID,4,val);
  1346. break;
  1347. }
  1348. case 102://tree
  1349. {
  1350. const CGHeroInstance *h = cb->getHero(heroID);
  1351. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1352. if(!ttype)
  1353. {
  1354. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1355. InfoWindow iw;
  1356. iw.soundID = sound;
  1357. iw.components.push_back(Component(id,subid,1,0));
  1358. iw.player = cb->getOwner(heroID);
  1359. iw.text << std::pair<ui8,ui32>(11,148);
  1360. cb->showInfoDialog(&iw);
  1361. cb->changePrimSkill(heroID,4,val);
  1362. break;
  1363. }
  1364. else
  1365. {
  1366. int res, resval;
  1367. if(ttype==1)
  1368. {
  1369. res = 6;
  1370. resval = 2000;
  1371. ot = 149;
  1372. }
  1373. else
  1374. {
  1375. res = 5;
  1376. resval = 10;
  1377. ot = 151;
  1378. }
  1379. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1380. {
  1381. ot++;
  1382. InfoWindow iw;
  1383. iw.soundID = sound;
  1384. iw.player = h->tempOwner;
  1385. iw.text << std::pair<ui8,ui32>(11,ot);
  1386. cb->showInfoDialog(&iw);
  1387. return;
  1388. }
  1389. BlockingDialog sd(true,false);
  1390. sd.soundID = sound;
  1391. sd.player = cb->getOwner(heroID);
  1392. sd.text << std::pair<ui8,ui32>(11,ot);
  1393. sd.components.push_back(Component(id,subid,val,0));
  1394. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1395. }
  1396. break;
  1397. }
  1398. case 41://library of enlightenment
  1399. {
  1400. const CGHeroInstance *h = cb->getHero(heroID);
  1401. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1402. {
  1403. InfoWindow iw;
  1404. iw.soundID = sound;
  1405. iw.player = cb->getOwner(heroID);
  1406. iw.text << std::pair<ui8,ui32>(11,68);
  1407. cb->showInfoDialog(&iw);
  1408. }
  1409. else
  1410. {
  1411. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1412. cb->changePrimSkill(heroID,0,2);
  1413. cb->changePrimSkill(heroID,1,2);
  1414. cb->changePrimSkill(heroID,2,2);
  1415. cb->changePrimSkill(heroID,3,2);
  1416. InfoWindow iw;
  1417. iw.soundID = sound;
  1418. iw.player = cb->getOwner(heroID);
  1419. iw.text << std::pair<ui8,ui32>(11,66);
  1420. cb->showInfoDialog(&iw);
  1421. }
  1422. break;
  1423. }
  1424. case 47: //School of Magic
  1425. case 107://School of War
  1426. {
  1427. int skill = (ID==47 ? 2 : 0);
  1428. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1429. {
  1430. InfoWindow iw;
  1431. iw.soundID = sound;
  1432. iw.player = cb->getOwner(heroID);
  1433. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1434. cb->showInfoDialog(&iw);
  1435. }
  1436. else
  1437. {
  1438. BlockingDialog sd(true,true);
  1439. sd.soundID = sound;
  1440. sd.player = cb->getOwner(heroID);
  1441. sd.text << std::pair<ui8,ui32>(11,ot);
  1442. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1443. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1444. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1445. }
  1446. }
  1447. break;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. ot++;
  1453. InfoWindow iw;
  1454. iw.soundID = sound;
  1455. iw.player = cb->getOwner(heroID);
  1456. iw.text << std::pair<ui8,ui32>(11,ot);
  1457. cb->showInfoDialog(&iw);
  1458. }
  1459. }
  1460. const std::string & CGVisitableOPH::getHoverText() const
  1461. {
  1462. int pom = -1;
  1463. switch(ID)
  1464. {
  1465. case 4:
  1466. pom = -1;
  1467. break;
  1468. case 51:
  1469. pom = 8;
  1470. break;
  1471. case 23:
  1472. pom = 7;
  1473. break;
  1474. case 61:
  1475. pom = 11;
  1476. break;
  1477. case 32:
  1478. pom = 4;
  1479. break;
  1480. case 100:
  1481. pom = 5;
  1482. break;
  1483. case 102:
  1484. pom = 18;
  1485. break;
  1486. case 41:
  1487. break;
  1488. case 47: //School of Magic
  1489. pom = 9;
  1490. break;
  1491. case 107://School of War
  1492. pom = 10;
  1493. break;
  1494. default:
  1495. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1496. }
  1497. hoverName = VLC->generaltexth->names[ID];
  1498. if(pom >= 0)
  1499. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1500. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1501. if(h)
  1502. {
  1503. hoverName += ' ';
  1504. hoverName += (vstd::contains(visitors,h->id))
  1505. ? (VLC->generaltexth->allTexts[352]) //visited
  1506. : ( VLC->generaltexth->allTexts[353]); //not visited
  1507. }
  1508. return hoverName;
  1509. }
  1510. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1511. {
  1512. cb->setObjProperty(id,4,heroID); //add to the visitors
  1513. cb->changePrimSkill(heroID,primSkill-1,2);
  1514. }
  1515. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1516. {
  1517. if(what == 4)
  1518. visitors.insert(val);
  1519. }
  1520. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1521. {
  1522. if(!which) //player refused to pay
  1523. return;
  1524. int base = (ID == 47 ? 2 : 0);
  1525. cb->setObjProperty(id,4,heroID); //add to the visitors
  1526. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1527. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1528. }
  1529. bool CArmedInstance::needsLastStack() const
  1530. {
  1531. return false;
  1532. }
  1533. int CArmedInstance::getArmyStrength() const
  1534. {
  1535. int ret = 0;
  1536. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1537. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1538. return ret;
  1539. }
  1540. /*const std::string & CGCreature::getHoverText() const
  1541. {
  1542. hoverName = VLC->generaltexth->names[ID];
  1543. hoverName += "\n Power rating: ";
  1544. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  1545. if (ratio < 0.1) hoverName += "Effortless";
  1546. else if (ratio < 0.3) hoverName += "Very Weak";
  1547. else if (ratio < 0.6) hoverName += "Weak";
  1548. else if (ratio < 0.9) hoverName += "A bit weaker";
  1549. else if (ratio < 1.1) hoverName += "Equal";
  1550. else if (ratio < 1.3) hoverName += "A bit stronger";
  1551. else if (ratio < 1.8) hoverName += "Strong";
  1552. else if (ratio < 2.5) hoverName += "Very Strong";
  1553. else if (ratio < 4) hoverName += "Challenging";
  1554. else if (ratio < 8) hoverName += "Overpowering";
  1555. else if (ratio < 20) hoverName += "Deadly";
  1556. else hoverName += "Impossible";
  1557. return hoverName;
  1558. }*/
  1559. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1560. {
  1561. int action = takenAction(h);
  1562. switch( action ) //decide what we do...
  1563. {
  1564. case -2: //fight
  1565. fight(h);
  1566. break;
  1567. case -1: //flee
  1568. {
  1569. flee(h);
  1570. break;
  1571. }
  1572. case 0: //join for free
  1573. {
  1574. BlockingDialog ynd(true,false);
  1575. ynd.player = h->tempOwner;
  1576. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1577. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1578. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1579. break;
  1580. }
  1581. default: //join for gold
  1582. {
  1583. assert(action > 0);
  1584. //ask if player agrees to pay gold
  1585. BlockingDialog ynd(true,false);
  1586. ynd.player = h->tempOwner;
  1587. std::string tmp = VLC->generaltexth->advobtxt[90];
  1588. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1589. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1590. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1591. ynd.text << tmp;
  1592. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1593. break;
  1594. }
  1595. }
  1596. }
  1597. void CGCreature::endBattle( BattleResult *result ) const
  1598. {
  1599. if(result->winner==0)
  1600. {
  1601. cb->removeObject(id);
  1602. }
  1603. else
  1604. {
  1605. //int killedAmount=0;
  1606. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1607. // if(i->first == subID)
  1608. // killedAmount += i->second;
  1609. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1610. MetaString ms;
  1611. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1612. pom = 174 + 3*pom + 1;
  1613. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1614. cb->setHoverName(id,&ms);
  1615. }
  1616. }
  1617. void CGCreature::initObj()
  1618. {
  1619. blockVisit = true;
  1620. switch(character)
  1621. {
  1622. case 0:
  1623. character = 0;
  1624. break;
  1625. case 1:
  1626. character = 1 + ran()%7;
  1627. break;
  1628. case 2:
  1629. character = 1 + ran()%10;
  1630. break;
  1631. case 3:
  1632. character = 4 + ran()%7;
  1633. break;
  1634. case 4:
  1635. character = 10;
  1636. break;
  1637. }
  1638. army.slots[0].first = subID;
  1639. si32 &amount = army.slots[0].second;
  1640. CCreature &c = VLC->creh->creatures[subID];
  1641. if(!amount)
  1642. if(c.ammMax == c.ammMin)
  1643. amount = c.ammMax;
  1644. else
  1645. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1646. MetaString ms;
  1647. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1648. pom = 174 + 3*pom + 1;
  1649. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1650. ms.toString(hoverName);
  1651. }
  1652. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1653. {
  1654. double hlp = h->getTotalStrength() / getArmyStrength();
  1655. if(!character) //compliant creatures will always join
  1656. return 0;
  1657. else if(allowJoin)//test for joining
  1658. {
  1659. int factor;
  1660. if(hlp >= 7)
  1661. factor = 11;
  1662. else if(hlp >= 1)
  1663. factor = (int)(2*(hlp-1));
  1664. else if(hlp >= 0.5)
  1665. factor = -1;
  1666. else if(hlp >= 0.333)
  1667. factor = -2;
  1668. else
  1669. factor = -3;
  1670. int sympathy = 0;
  1671. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1672. myKindCres.insert(subID); //we
  1673. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1674. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1675. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1676. myKindCres.insert(i->idNumber);
  1677. int count = 0, //how many creatures of our kind has hero
  1678. totalCount = 0;
  1679. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1680. {
  1681. if(vstd::contains(myKindCres,i->second.first))
  1682. count += i->second.second;
  1683. totalCount += i->second.second;
  1684. }
  1685. if(count*2 > totalCount)
  1686. sympathy++;
  1687. if(count)
  1688. sympathy++;
  1689. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1690. if(charisma >= character) //creatures might join...
  1691. {
  1692. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1693. return 0; //join for free
  1694. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1695. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1696. }
  1697. }
  1698. //we are still here - creatures not joined heroes, test for fleeing
  1699. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1700. //TODO: should be deterministic (will be needed for Vision spell)
  1701. int hlp2 = (int) (hlp - 2)*1000;
  1702. if(!neverFlees
  1703. && hlp2 >= 0
  1704. && rand()%2000 < hlp2
  1705. )
  1706. return -1; //flee
  1707. else
  1708. return -2; //fight
  1709. }
  1710. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1711. {
  1712. if(pursue)
  1713. {
  1714. fight(h);
  1715. }
  1716. else
  1717. {
  1718. cb->removeObject(id);
  1719. }
  1720. }
  1721. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1722. {
  1723. if(!accept)
  1724. {
  1725. if(takenAction(h,false) == -1) //they flee
  1726. {
  1727. flee(h);
  1728. }
  1729. else //they fight
  1730. {
  1731. InfoWindow iw;
  1732. iw.player = h->tempOwner;
  1733. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1734. cb->showInfoDialog(&iw);
  1735. fight(h);
  1736. }
  1737. }
  1738. else //accepted
  1739. {
  1740. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1741. {
  1742. InfoWindow iw;
  1743. iw.player = h->tempOwner;
  1744. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1745. cb->showInfoDialog(&iw);
  1746. //act as if player refused
  1747. joinDecision(h,cost,false);
  1748. return;
  1749. }
  1750. //take gold
  1751. if(cost)
  1752. cb->giveResource(h->tempOwner,6,-cost);
  1753. int slot = h->army.getSlotFor(subID);
  1754. if(slot >= 0) //there is place
  1755. {
  1756. //add creatures
  1757. SetGarrisons sg;
  1758. sg.garrs[h->id] = h->army;
  1759. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1760. {
  1761. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1762. }
  1763. else //add as a new stack
  1764. {
  1765. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1766. }
  1767. cb->sendAndApply(&sg);
  1768. cb->removeObject(id);
  1769. }
  1770. else
  1771. {
  1772. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1773. }
  1774. }
  1775. }
  1776. void CGCreature::fight( const CGHeroInstance *h ) const
  1777. {
  1778. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  1779. }
  1780. void CGCreature::flee( const CGHeroInstance * h ) const
  1781. {
  1782. BlockingDialog ynd(true,false);
  1783. ynd.player = h->tempOwner;
  1784. ynd.text << std::pair<ui8,ui32>(11,91);
  1785. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1786. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1787. }
  1788. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1789. {
  1790. if(subID == 7) //TODO: support for abandoned mine
  1791. return;
  1792. if(h->tempOwner == tempOwner) //we're visiting our mine
  1793. {
  1794. cb->showGarrisonDialog(id,h->id,0);
  1795. return;
  1796. }
  1797. //TODO: check if mine is guarded
  1798. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1799. MetaString ms;
  1800. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1801. cb->setHoverName(id,&ms);
  1802. int vv=1; //amount of resource per turn
  1803. if (subID==0 || subID==2)
  1804. vv++;
  1805. else if (subID==6)
  1806. vv = 1000;
  1807. InfoWindow iw;
  1808. iw.soundID = soundBase::FLAGMINE;
  1809. iw.text << std::pair<ui8,ui32>(10,subID);
  1810. iw.player = h->tempOwner;
  1811. iw.components.push_back(Component(2,subID,vv,-1));
  1812. cb->showInfoDialog(&iw);
  1813. }
  1814. void CGMine::newTurn() const
  1815. {
  1816. if(cb->getDate() == 1)
  1817. return;
  1818. if (tempOwner == NEUTRAL_PLAYER)
  1819. return;
  1820. int vv = 1;
  1821. if (subID==0 || subID==2)
  1822. vv++;
  1823. else if (subID==6)
  1824. vv = 1000;
  1825. cb->giveResource(tempOwner,subID,vv);
  1826. }
  1827. void CGMine::initObj()
  1828. {
  1829. MetaString ms;
  1830. ms << std::pair<ui8,ui32>(9,subID);
  1831. if(tempOwner >= PLAYER_LIMIT)
  1832. tempOwner = NEUTRAL_PLAYER;
  1833. else
  1834. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1835. ms.toString(hoverName);
  1836. }
  1837. void CGResource::initObj()
  1838. {
  1839. blockVisit = true;
  1840. hoverName = VLC->generaltexth->restypes[subID];
  1841. if(!amount)
  1842. {
  1843. switch(subID)
  1844. {
  1845. case 6:
  1846. amount = 500 + (rand()%6)*100;
  1847. break;
  1848. case 0: case 2:
  1849. amount = 6 + (rand()%5);
  1850. break;
  1851. default:
  1852. amount = 3 + (rand()%3);
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1858. {
  1859. if(army.slots.size())
  1860. {
  1861. if(message.size())
  1862. {
  1863. BlockingDialog ynd(true,false);
  1864. ynd.player = h->getOwner();
  1865. ynd.text << message;
  1866. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1867. }
  1868. else
  1869. {
  1870. fightForRes(1,h);
  1871. }
  1872. }
  1873. else
  1874. {
  1875. if(message.length())
  1876. {
  1877. InfoWindow iw;
  1878. iw.player = h->tempOwner;
  1879. iw.text << message;
  1880. cb->showInfoDialog(&iw);
  1881. }
  1882. collectRes(h->getOwner());
  1883. }
  1884. }
  1885. void CGResource::collectRes( int player ) const
  1886. {
  1887. cb->giveResource(player,subID,amount);
  1888. ShowInInfobox sii;
  1889. sii.player = player;
  1890. sii.c = Component(2,subID,amount,0);
  1891. sii.text << std::pair<ui8,ui32>(11,113);
  1892. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  1893. cb->showCompInfo(&sii);
  1894. cb->removeObject(id);
  1895. }
  1896. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1897. {
  1898. if(agreed)
  1899. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  1900. }
  1901. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1902. {
  1903. if(result->winner == 0) //attacker won
  1904. collectRes(h->getOwner());
  1905. }
  1906. void CGVisitableOPW::newTurn() const
  1907. {
  1908. if (cb->getDate(1)==1) //first day of week
  1909. {
  1910. cb->setObjProperty(id,5,false);
  1911. MetaString ms; //set text to "not visited"
  1912. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1913. cb->setHoverName(id,&ms);
  1914. }
  1915. }
  1916. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1917. {
  1918. int mid, sound = 0;
  1919. switch (ID)
  1920. {
  1921. case 55: //mystical garden
  1922. sound = soundBase::experience;
  1923. mid = 92;
  1924. break;
  1925. case 112://windmill
  1926. sound = soundBase::GENIE;
  1927. mid = 170;
  1928. break;
  1929. case 109://waterwheel
  1930. sound = soundBase::GENIE;
  1931. mid = 164;
  1932. break;
  1933. }
  1934. if (visited)
  1935. {
  1936. if (ID!=112)
  1937. mid++;
  1938. else
  1939. mid--;
  1940. InfoWindow iw;
  1941. iw.soundID = sound;
  1942. iw.player = h->tempOwner;
  1943. iw.text << std::pair<ui8,ui32>(11,mid);
  1944. cb->showInfoDialog(&iw);
  1945. }
  1946. else
  1947. {
  1948. int type, sub, val;
  1949. type = 2;
  1950. switch (ID)
  1951. {
  1952. case 55:
  1953. if (rand()%2)
  1954. {
  1955. sub = 5;
  1956. val = 5;
  1957. }
  1958. else
  1959. {
  1960. sub = 6;
  1961. val = 500;
  1962. }
  1963. break;
  1964. case 112:
  1965. mid = 170;
  1966. sub = (rand() % 5) + 1;
  1967. val = (rand() % 4) + 3;
  1968. break;
  1969. case 109:
  1970. mid = 164;
  1971. sub = 6;
  1972. if(cb->getDate(0)<8)
  1973. val = 500;
  1974. else
  1975. val = 1000;
  1976. }
  1977. cb->giveResource(h->tempOwner,sub,val);
  1978. InfoWindow iw;
  1979. iw.soundID = sound;
  1980. iw.player = h->tempOwner;
  1981. iw.components.push_back(Component(type,sub,val,0));
  1982. iw.text << std::pair<ui8,ui32>(11,mid);
  1983. cb->showInfoDialog(&iw);
  1984. cb->setObjProperty(id,5,true);
  1985. MetaString ms; //set text to "visited"
  1986. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1987. cb->setHoverName(id,&ms);
  1988. }
  1989. }
  1990. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1991. {
  1992. if(what == 5)
  1993. visited = val;
  1994. }
  1995. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1996. {
  1997. int destinationid=-1;
  1998. switch(ID)
  1999. {
  2000. case 43: //one way - find corresponding exit monolith
  2001. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2002. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2003. else
  2004. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2005. break;
  2006. case 45: //two way monolith - pick any other one
  2007. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2008. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2009. else
  2010. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2011. break;
  2012. case 103: //find nearest subterranean gate on the other level
  2013. {
  2014. std::pair<int,double> best(-1,150000); //pair<id,dist>
  2015. for(int i=0; i<objs[103][0].size(); i++)
  2016. {
  2017. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  2018. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  2019. if(hlp<best.second)
  2020. {
  2021. best.first = objs[103][0][i];
  2022. best.second = hlp;
  2023. }
  2024. }
  2025. if(best.first<0)
  2026. return;
  2027. else
  2028. destinationid = best.first;
  2029. break;
  2030. }
  2031. }
  2032. if(destinationid < 0)
  2033. {
  2034. tlog2 << "Cannot find exit... :( \n";
  2035. return;
  2036. }
  2037. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2038. true);
  2039. }
  2040. void CGTeleport::initObj()
  2041. {
  2042. objs[ID][subID].push_back(id);
  2043. }
  2044. void CGArtifact::initObj()
  2045. {
  2046. blockVisit = true;
  2047. if(ID == 5)
  2048. hoverName = VLC->arth->artifacts[subID].Name();
  2049. }
  2050. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2051. {
  2052. if(!army.slots.size())
  2053. {
  2054. if(ID == 5)
  2055. {
  2056. InfoWindow iw;
  2057. iw.soundID = soundBase::treasure;
  2058. iw.player = h->tempOwner;
  2059. iw.components.push_back(Component(4,subID,0,0));
  2060. if(message.length())
  2061. iw.text << message;
  2062. else
  2063. iw.text << std::pair<ui8,ui32>(12,subID);
  2064. cb->showInfoDialog(&iw);
  2065. }
  2066. pick(h);
  2067. }
  2068. else
  2069. {
  2070. if(message.size())
  2071. {
  2072. BlockingDialog ynd(true,false);
  2073. ynd.player = h->getOwner();
  2074. ynd.text << message;
  2075. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2076. }
  2077. else
  2078. {
  2079. fightForArt(0,h);
  2080. }
  2081. }
  2082. }
  2083. void CGArtifact::pick(const CGHeroInstance * h) const
  2084. {
  2085. if(ID == 5) //Artifact
  2086. {
  2087. cb->giveHeroArtifact(subID,h->id,-2);
  2088. }
  2089. else if(ID == 93) // Spell scroll
  2090. {
  2091. //TODO: support for the spell scroll
  2092. }
  2093. cb->removeObject(id);
  2094. }
  2095. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2096. {
  2097. if(agreed)
  2098. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2099. }
  2100. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2101. {
  2102. if(result->winner == 0) //attacker won
  2103. pick(h);
  2104. }
  2105. void CGPickable::initObj()
  2106. {
  2107. blockVisit = true;
  2108. switch(ID)
  2109. {
  2110. case 12: //campfire
  2111. val2 = (ran()%3) + 4; //4 - 6
  2112. val1 = val2 * 100;
  2113. type = ran()%6; //given resource
  2114. break;
  2115. case 29: //floatsam
  2116. switch(type = ran()%4)
  2117. {
  2118. case 0:
  2119. val1 = val2 = 0;
  2120. break;
  2121. case 1:
  2122. val1 = 5;
  2123. val2 = 0;
  2124. break;
  2125. case 2:
  2126. val1 = 5;
  2127. val2 = 200;
  2128. break;
  2129. case 3:
  2130. val1 = 10;
  2131. val2 = 500;
  2132. break;
  2133. }
  2134. break;
  2135. case 82: //sea chest
  2136. {
  2137. int hlp = ran()%100;
  2138. if(hlp < 20)
  2139. {
  2140. val1 = 0;
  2141. type = 0;
  2142. }
  2143. else if(hlp < 90)
  2144. {
  2145. val1 = 1500;
  2146. type = 2;
  2147. }
  2148. else
  2149. {
  2150. val1 = 1000;
  2151. val2 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2152. type = 1;
  2153. }
  2154. }
  2155. break;
  2156. case 86: //Shipwreck Survivor
  2157. {
  2158. int hlp = ran()%100;
  2159. if(hlp < 55)
  2160. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2161. else if(hlp < 75)
  2162. val1 = VLC->arth->minors[ran()%VLC->arth->minors.size()]->id;
  2163. else if(hlp < 95)
  2164. val1 = VLC->arth->majors[ran()%VLC->arth->majors.size()]->id;
  2165. else
  2166. val1 = VLC->arth->relics[ran()%VLC->arth->relics.size()]->id;
  2167. }
  2168. break;
  2169. case 101: //treasure chest
  2170. {
  2171. int hlp = ran()%100;
  2172. if(hlp >= 95)
  2173. {
  2174. type = 1;
  2175. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2176. return;
  2177. }
  2178. else if (hlp >= 65)
  2179. {
  2180. val1 = 2000;
  2181. }
  2182. else if(hlp >= 33)
  2183. {
  2184. val1 = 1500;
  2185. }
  2186. else
  2187. {
  2188. val1 = 1000;
  2189. }
  2190. val2 = val1 - 500;
  2191. type = 0;
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2197. {
  2198. switch(ID)
  2199. {
  2200. case 12: //campfire
  2201. {
  2202. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2203. cb->giveResource(h->tempOwner,6,val1);//gold
  2204. InfoWindow iw;
  2205. iw.soundID = soundBase::experience;
  2206. iw.player = h->tempOwner;
  2207. iw.components.push_back(Component(2,6,val1,0));
  2208. iw.components.push_back(Component(2,type,val2,0));
  2209. iw.text << std::pair<ui8,ui32>(11,23);
  2210. cb->showInfoDialog(&iw);
  2211. break;
  2212. }
  2213. case 29: //flotsam
  2214. {
  2215. cb->giveResource(h->tempOwner,0,val1); //wood
  2216. cb->giveResource(h->tempOwner,6,val2);//gold
  2217. InfoWindow iw;
  2218. iw.soundID = soundBase::GENIE;
  2219. iw.player = h->tempOwner;
  2220. if(val1)
  2221. iw.components.push_back(Component(2,0,val1,0));
  2222. if(val2)
  2223. iw.components.push_back(Component(2,6,val2,0));
  2224. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2225. cb->showInfoDialog(&iw);
  2226. break;
  2227. }
  2228. case 82: //Sea Chest
  2229. {
  2230. InfoWindow iw;
  2231. iw.soundID = soundBase::chest;
  2232. iw.player = h->tempOwner;
  2233. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2234. if(val1) //there is gold
  2235. {
  2236. iw.components.push_back(Component(2,6,val1,0));
  2237. cb->giveResource(h->tempOwner,6,val1);
  2238. }
  2239. if(type == 1) //art
  2240. {
  2241. //TODO: what if no space in backpack?
  2242. iw.components.push_back(Component(4, val2, 1, 0));
  2243. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2244. cb->giveHeroArtifact(val2,h->id,-2);
  2245. }
  2246. cb->showInfoDialog(&iw);
  2247. break;
  2248. }
  2249. case 86: //Shipwreck Survivor
  2250. {
  2251. //TODO: what if no space in backpack?
  2252. InfoWindow iw;
  2253. iw.soundID = soundBase::experience;
  2254. iw.player = h->tempOwner;
  2255. iw.components.push_back(Component(4,val1,1,0));
  2256. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2257. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2258. cb->giveHeroArtifact(val1,h->id,-2);
  2259. cb->showInfoDialog(&iw);
  2260. break;
  2261. }
  2262. case 101: //treasure chest
  2263. {
  2264. if (subID) //not OH3 treasure chest
  2265. {
  2266. tlog2 << "Not supported WoG treasure chest!\n";
  2267. return;
  2268. }
  2269. if(type) //there is an artifact
  2270. {
  2271. cb->giveHeroArtifact(val1,h->id,-2);
  2272. InfoWindow iw;
  2273. iw.soundID = soundBase::treasure;
  2274. iw.player = h->tempOwner;
  2275. iw.components.push_back(Component(4,val1,1,0));
  2276. iw.text << std::pair<ui8,ui32>(11,145);
  2277. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2278. cb->showInfoDialog(&iw);
  2279. break;
  2280. }
  2281. else
  2282. {
  2283. BlockingDialog sd(false,true);
  2284. sd.player = h->tempOwner;
  2285. sd.text << std::pair<ui8,ui32>(11,146);
  2286. sd.components.push_back(Component(2,6,val1,0));
  2287. sd.components.push_back(Component(5,0,val2,0));
  2288. sd.soundID = soundBase::chest;
  2289. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2290. cb->showBlockingDialog(&sd,fun);
  2291. return;
  2292. }
  2293. }
  2294. }
  2295. cb->removeObject(id);
  2296. }
  2297. void CGPickable::chosen( int which, int heroID ) const
  2298. {
  2299. switch(which)
  2300. {
  2301. case 1: //player pick gold
  2302. cb->giveResource(cb->getOwner(heroID),6,val1);
  2303. break;
  2304. case 2: //player pick exp
  2305. cb->changePrimSkill(heroID, 4, val2);
  2306. break;
  2307. default:
  2308. throw std::string("Unhandled treasure choice");
  2309. }
  2310. cb->removeObject(id);
  2311. }
  2312. void CGWitchHut::initObj()
  2313. {
  2314. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2315. }
  2316. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2317. {
  2318. InfoWindow iw;
  2319. iw.soundID = soundBase::gazebo;
  2320. iw.player = h->getOwner();
  2321. if(!hasVisited(h->tempOwner))
  2322. cb->setObjProperty(id,10,h->tempOwner);
  2323. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2324. {
  2325. iw.text << std::pair<ui8,ui32>(11,172);
  2326. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2327. }
  2328. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2329. {
  2330. iw.text << std::pair<ui8,ui32>(11,173);
  2331. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2332. }
  2333. else //give sec skill
  2334. {
  2335. iw.components.push_back(Component(1, ability, 1, 0));
  2336. iw.text << std::pair<ui8,ui32>(11,171);
  2337. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2338. cb->changeSecSkill(h->id,ability,1,true);
  2339. }
  2340. cb->showInfoDialog(&iw);
  2341. }
  2342. const std::string & CGWitchHut::getHoverText() const
  2343. {
  2344. hoverName = VLC->generaltexth->names[ID];
  2345. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2346. {
  2347. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2348. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2349. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2350. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2351. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2352. }
  2353. return hoverName;
  2354. }
  2355. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2356. {
  2357. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2358. int messageID, bonusType, bonusVal;
  2359. int bonusMove = 0, sound = -1;
  2360. InfoWindow iw;
  2361. iw.player = h->tempOwner;
  2362. GiveBonus gbonus;
  2363. gbonus.hid = h->id;
  2364. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2365. gbonus.bonus.source = HeroBonus::OBJECT;
  2366. gbonus.bonus.id = ID;
  2367. switch(ID)
  2368. {
  2369. case 11: //buoy
  2370. messageID = 21;
  2371. sound = soundBase::MORALE;
  2372. gbonus.bonus.type = HeroBonus::MORALE;
  2373. gbonus.bonus.val = +1;
  2374. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2375. break;
  2376. case 14: //swan pond
  2377. messageID = 29;
  2378. sound = soundBase::LUCK;
  2379. gbonus.bonus.type = HeroBonus::LUCK;
  2380. gbonus.bonus.val = 2;
  2381. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2382. bonusMove = -h->movement;
  2383. break;
  2384. case 28: //Faerie Ring
  2385. messageID = 49;
  2386. sound = soundBase::LUCK;
  2387. gbonus.bonus.type = HeroBonus::LUCK;
  2388. gbonus.bonus.val = 1;
  2389. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2390. break;
  2391. case 30: //fountain of fortune
  2392. messageID = 55;
  2393. sound = soundBase::LUCK;
  2394. gbonus.bonus.type = HeroBonus::LUCK;
  2395. gbonus.bonus.val = rand()%5 - 1;
  2396. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2397. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2398. break;
  2399. case 38: //idol of fortune
  2400. messageID = 62;
  2401. iw.soundID = soundBase::experience;
  2402. if(cb->getDate(1) == 7) //7th day of week
  2403. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2404. else
  2405. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2406. gbonus.bonus.val = 1;
  2407. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2408. break;
  2409. case 52: //Mermaid
  2410. messageID = 83;
  2411. sound = soundBase::LUCK;
  2412. gbonus.bonus.type = HeroBonus::LUCK;
  2413. gbonus.bonus.val = 1;
  2414. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2415. break;
  2416. case 64: //Rally Flag
  2417. sound = soundBase::MORALE;
  2418. messageID = 111;
  2419. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2420. gbonus.bonus.val = 1;
  2421. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2422. bonusMove = 400;
  2423. break;
  2424. case 56: //oasis
  2425. messageID = 95;
  2426. gbonus.bonus.type = HeroBonus::MORALE;
  2427. gbonus.bonus.val = 1;
  2428. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2429. bonusMove = 800;
  2430. break;
  2431. case 96: //temple
  2432. messageID = 140;
  2433. iw.soundID = soundBase::temple;
  2434. gbonus.bonus.type = HeroBonus::MORALE;
  2435. if(cb->getDate(1)==7) //sunday
  2436. {
  2437. gbonus.bonus.val = 2;
  2438. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2439. }
  2440. else
  2441. {
  2442. gbonus.bonus.val = 1;
  2443. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2444. }
  2445. break;
  2446. case 110://Watering Hole
  2447. sound = soundBase::MORALE;
  2448. messageID = 166;
  2449. gbonus.bonus.type = HeroBonus::MORALE;
  2450. gbonus.bonus.val = 1;
  2451. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2452. bonusMove = 400;
  2453. break;
  2454. case 31: //Fountain of Youth
  2455. sound = soundBase::MORALE;
  2456. messageID = 57;
  2457. gbonus.bonus.type = HeroBonus::MORALE;
  2458. gbonus.bonus.val = 1;
  2459. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2460. bonusMove = 400;
  2461. break;
  2462. case 94: //Stables TODO: upgrade Cavaliers
  2463. sound = soundBase::horse20;
  2464. std::set<ui32> slots;
  2465. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2466. {
  2467. if(i->second.first == 10)
  2468. slots.insert(i->first);
  2469. }
  2470. if (!slots.empty())
  2471. {
  2472. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2473. {
  2474. UpgradeCreature uc(*i, id, 11);
  2475. //uc.applyGh (&gh);
  2476. }
  2477. }
  2478. messageID = 137;
  2479. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  2480. gbonus.bonus.val = 600;
  2481. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  2482. break;
  2483. }
  2484. if(visited)
  2485. {
  2486. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  2487. messageID--;
  2488. else
  2489. messageID++;
  2490. }
  2491. else
  2492. {
  2493. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2494. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2495. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2496. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2497. cb->giveHeroBonus(&gbonus);
  2498. if(bonusMove) //swan pond - take all move points
  2499. {
  2500. SetMovePoints smp;
  2501. smp.hid = h->id;
  2502. smp.val = h->movement + bonusMove;
  2503. cb->setMovePoints(&smp);
  2504. }
  2505. }
  2506. iw.soundID = sound;
  2507. iw.text << std::pair<ui8,ui32>(11,messageID);
  2508. cb->showInfoDialog(&iw);
  2509. }
  2510. const std::string & CGBonusingObject::getHoverText() const
  2511. {
  2512. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2513. hoverName = VLC->generaltexth->names[ID];
  2514. if(h)
  2515. {
  2516. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2517. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2518. else
  2519. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2520. }
  2521. return hoverName;
  2522. }
  2523. void CGBonusingObject::initObj()
  2524. {
  2525. if(ID == 11 || ID == 52) //Buoy / Mermaid
  2526. {
  2527. blockVisit = true;
  2528. }
  2529. }
  2530. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2531. {
  2532. int message;
  2533. InfoWindow iw;
  2534. iw.soundID = soundBase::faerie;
  2535. iw.player = h->tempOwner;
  2536. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2537. {
  2538. message = 78;
  2539. }
  2540. else if(h->mana < h->manaLimit())
  2541. {
  2542. giveDummyBonus(h->id);
  2543. cb->setManaPoints(h->id,h->manaLimit());
  2544. message = 77;
  2545. }
  2546. else
  2547. {
  2548. message = 79;
  2549. }
  2550. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2551. cb->showInfoDialog(&iw);
  2552. }
  2553. const std::string & CGMagicWell::getHoverText() const
  2554. {
  2555. getNameVis(hoverName);
  2556. return hoverName;
  2557. }
  2558. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2559. {
  2560. if(!(availableFor & (1 << h->tempOwner)))
  2561. return;
  2562. if(cb->getPlayerSettings(h->tempOwner)->human)
  2563. {
  2564. if(humanActivate)
  2565. activated(h);
  2566. }
  2567. else if(computerActivate)
  2568. activated(h);
  2569. }
  2570. void CGEvent::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2571. {
  2572. if(result->winner)
  2573. return;
  2574. giveContents(h,true);
  2575. }
  2576. void CGEvent::activated( const CGHeroInstance * h ) const
  2577. {
  2578. if(army)
  2579. {
  2580. InfoWindow iw;
  2581. iw.player = h->tempOwner;
  2582. if(message.size())
  2583. iw.text << message;
  2584. else
  2585. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2586. cb->showInfoDialog(&iw);
  2587. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  2588. }
  2589. else
  2590. {
  2591. giveContents(h,false);
  2592. }
  2593. }
  2594. void CGEvent::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2595. {
  2596. InfoWindow iw;
  2597. iw.player = h->getOwner();
  2598. bool changesPrimSkill = false;
  2599. for (int i = 0; i < primskills.size(); i++)
  2600. {
  2601. if(primskills[i])
  2602. {
  2603. changesPrimSkill = true;
  2604. break;
  2605. }
  2606. }
  2607. if(gainedExp || changesPrimSkill || abilities.size())
  2608. {
  2609. getText(iw,afterBattle,175,h);
  2610. if(gainedExp)
  2611. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2612. for(int i=0; i<primskills.size(); i++)
  2613. if(primskills[i])
  2614. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2615. for(int i=0; i<abilities.size(); i++)
  2616. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2617. cb->showInfoDialog(&iw);
  2618. //give exp
  2619. if(gainedExp)
  2620. cb->changePrimSkill(h->id,5,gainedExp,false);
  2621. //give prim skills
  2622. for(int i=0; i<primskills.size(); i++)
  2623. if(primskills[i])
  2624. cb->changePrimSkill(h->id,i,primskills[i],false);
  2625. //give sec skills
  2626. for(int i=0; i<abilities.size(); i++)
  2627. {
  2628. int curLev = h->getSecSkillLevel(abilities[i]);
  2629. if(curLev && curLev < abilityLevels[i]
  2630. || h->secSkills.size() < SKILL_PER_HERO )
  2631. {
  2632. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2633. }
  2634. }
  2635. }
  2636. if(spells.size())
  2637. {
  2638. std::set<ui32> spellsToGive;
  2639. iw.components.clear();
  2640. for(int i=0; i<spells.size(); i++)
  2641. {
  2642. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2643. spellsToGive.insert(spells[i]);
  2644. }
  2645. if(spellsToGive.size())
  2646. {
  2647. cb->changeSpells(h->id,true,spellsToGive);
  2648. cb->showInfoDialog(&iw);
  2649. }
  2650. }
  2651. if(manaDiff)
  2652. {
  2653. getText(iw,afterBattle,manaDiff,176,177,h);
  2654. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2655. cb->showInfoDialog(&iw);
  2656. cb->setManaPoints(h->id, h->mana + manaDiff);
  2657. }
  2658. if(moraleDiff)
  2659. {
  2660. getText(iw,afterBattle,moraleDiff,178,179,h);
  2661. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2662. cb->showInfoDialog(&iw);
  2663. GiveBonus gb;
  2664. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2665. gb.hid = h->id;
  2666. cb->giveHeroBonus(&gb);
  2667. }
  2668. if(luckDiff)
  2669. {
  2670. getText(iw,afterBattle,luckDiff,180,181,h);
  2671. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2672. cb->showInfoDialog(&iw);
  2673. GiveBonus gb;
  2674. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2675. gb.hid = h->id;
  2676. cb->giveHeroBonus(&gb);
  2677. }
  2678. iw.components.clear();
  2679. iw.text.clear();
  2680. for(int i=0; i<resources.size(); i++)
  2681. {
  2682. if(resources[i] < 0)
  2683. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2684. }
  2685. if(iw.components.size())
  2686. {
  2687. getText(iw,afterBattle,182,h);
  2688. cb->showInfoDialog(&iw);
  2689. }
  2690. iw.components.clear();
  2691. iw.text.clear();
  2692. for(int i=0; i<resources.size(); i++)
  2693. {
  2694. if(resources[i] > 0)
  2695. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2696. }
  2697. if(iw.components.size())
  2698. {
  2699. getText(iw,afterBattle,183,h);
  2700. cb->showInfoDialog(&iw);
  2701. }
  2702. iw.components.clear();
  2703. getText(iw,afterBattle,183,h);
  2704. for(int i=0; i<artifacts.size(); i++)
  2705. {
  2706. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2707. if(iw.components.size() >= 14)
  2708. {
  2709. cb->showInfoDialog(&iw);
  2710. iw.components.clear();
  2711. }
  2712. }
  2713. if(iw.components.size())
  2714. {
  2715. cb->showInfoDialog(&iw);
  2716. }
  2717. for(int i=0; i<resources.size(); i++)
  2718. if(resources[i])
  2719. cb->giveResource(h->getOwner(),i,resources[i]);
  2720. for(int i=0; i<artifacts.size(); i++)
  2721. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2722. //show dialog with given creatures
  2723. iw.components.clear();
  2724. iw.text.clear();
  2725. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2726. {
  2727. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2728. }
  2729. if(iw.components.size())
  2730. {
  2731. if(afterBattle)
  2732. {
  2733. if(iw.components.front().val == 1)
  2734. {
  2735. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2736. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2737. }
  2738. else
  2739. {
  2740. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2741. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2742. }
  2743. iw.text.addReplacement(h->name);
  2744. }
  2745. else
  2746. {
  2747. iw.text << message;
  2748. afterBattle = true;
  2749. }
  2750. cb->showInfoDialog(&iw);
  2751. }
  2752. //check if creatures can be moved to hero army
  2753. CCreatureSet heroArmy = h->army;
  2754. CCreatureSet ourArmy = creatures;
  2755. while(ourArmy)
  2756. {
  2757. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2758. if(slot < 0)
  2759. break;
  2760. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2761. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2762. ourArmy.slots.erase(ourArmy.slots.begin());
  2763. }
  2764. if(!ourArmy) //all creatures can be moved to hero army - do that
  2765. {
  2766. SetGarrisons sg;
  2767. sg.garrs[h->id] = heroArmy;
  2768. cb->sendAndApply(&sg);
  2769. }
  2770. else //show garrison window and let player pick creatures
  2771. {
  2772. SetGarrisons sg;
  2773. sg.garrs[id] = creatures;
  2774. cb->sendAndApply(&sg);
  2775. if(removeAfterVisit)
  2776. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2777. else
  2778. cb->showGarrisonDialog(id,h->id,0);
  2779. return;
  2780. }
  2781. if(!afterBattle && message.size())
  2782. {
  2783. iw.text << message;
  2784. cb->showInfoDialog(&iw);
  2785. }
  2786. if(removeAfterVisit)
  2787. cb->removeObject(id);
  2788. }
  2789. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2790. {
  2791. if(afterBattle || !message.size())
  2792. {
  2793. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2794. iw.text.addReplacement(h->name);
  2795. }
  2796. else
  2797. {
  2798. iw.text << message;
  2799. afterBattle = true;
  2800. }
  2801. }
  2802. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  2803. {
  2804. iw.components.clear();
  2805. iw.text.clear();
  2806. if(afterBattle || !message.size())
  2807. {
  2808. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2809. iw.text.addReplacement(h->name);
  2810. }
  2811. else
  2812. {
  2813. iw.text << message;
  2814. afterBattle = true;
  2815. }
  2816. }
  2817. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2818. {
  2819. InfoWindow iw;
  2820. iw.soundID = soundBase::LIGHTHOUSE;
  2821. iw.player = h->tempOwner;
  2822. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2823. cb->showInfoDialog(&iw);
  2824. FoWChange fw;
  2825. fw.player = h->tempOwner;
  2826. fw.mode = 1;
  2827. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2828. cb->sendAndApply(&fw);
  2829. }
  2830. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2831. {
  2832. if(spell == 255)
  2833. {
  2834. tlog1 << "Not initialized shrine visited!\n";
  2835. return;
  2836. }
  2837. if(!hasVisited(h->tempOwner))
  2838. cb->setObjProperty(id,10,h->tempOwner);
  2839. InfoWindow iw;
  2840. iw.soundID = soundBase::temple;
  2841. iw.player = h->getOwner();
  2842. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2843. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2844. iw.text << ".";
  2845. if(!h->getArt(17)) //no spellbook
  2846. {
  2847. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2848. }
  2849. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2850. {
  2851. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2852. }
  2853. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2854. {
  2855. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2856. }
  2857. else //give spell
  2858. {
  2859. std::set<ui32> spells;
  2860. spells.insert(spell);
  2861. cb->changeSpells(h->id,true,spells);
  2862. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2863. }
  2864. cb->showInfoDialog(&iw);
  2865. }
  2866. void CGShrine::initObj()
  2867. {
  2868. if(spell == 255) //spell not set
  2869. {
  2870. int level = ID-87;
  2871. std::vector<ui32> possibilities;
  2872. //add all allowed spells of wanted level
  2873. for(int i=0; i<SPELLS_QUANTITY; i++)
  2874. {
  2875. if(VLC->spellh->spells[i].level == level
  2876. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2877. {
  2878. possibilities.push_back(VLC->spellh->spells[i].id);
  2879. }
  2880. }
  2881. if(!possibilities.size())
  2882. {
  2883. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2884. return;
  2885. }
  2886. spell = possibilities[ran() % possibilities.size()];
  2887. }
  2888. }
  2889. const std::string & CGShrine::getHoverText() const
  2890. {
  2891. hoverName = VLC->generaltexth->names[ID];
  2892. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2893. {
  2894. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2895. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2896. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2897. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2898. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2899. }
  2900. return hoverName;
  2901. }
  2902. void CGSignBottle::initObj()
  2903. {
  2904. //if no text is set than we pick random from the predefined ones
  2905. if(!message.size())
  2906. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2907. if(ID == 59)
  2908. {
  2909. blockVisit = true;
  2910. }
  2911. }
  2912. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2913. {
  2914. InfoWindow iw;
  2915. iw.soundID = soundBase::STORE;
  2916. iw.player = h->getOwner();
  2917. iw.text << message;
  2918. cb->showInfoDialog(&iw);
  2919. if(ID == 59)
  2920. cb->removeObject(id);
  2921. }
  2922. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2923. {
  2924. }
  2925. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2926. {
  2927. int type = bonusType;
  2928. int bid = bonusID;
  2929. //check if the bonus if applicable, if not - give primary skill (always possible)
  2930. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2931. if((type == 1
  2932. && ((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)
  2933. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2934. {
  2935. type = 0;
  2936. bid = ran() % PRIMARY_SKILLS;
  2937. }
  2938. InfoWindow iw;
  2939. iw.soundID = soundBase::gazebo;
  2940. iw.player = h->getOwner();
  2941. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2942. switch (type)
  2943. {
  2944. case 0:
  2945. cb->changePrimSkill(h->id,bid,+1);
  2946. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  2947. break;
  2948. case 1:
  2949. cb->changeSecSkill(h->id,bid,+1);
  2950. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2951. break;
  2952. case 2:
  2953. {
  2954. std::set<ui32> hlp;
  2955. hlp.insert(bid);
  2956. cb->changeSpells(h->id,true,hlp);
  2957. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2958. }
  2959. break;
  2960. default:
  2961. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2962. return;
  2963. }
  2964. cb->showInfoDialog(&iw);
  2965. cb->removeObject(id);
  2966. }
  2967. void CGScholar::initObj()
  2968. {
  2969. blockVisit = true;
  2970. if(bonusType == 255)
  2971. {
  2972. bonusType = ran()%3;
  2973. switch(bonusType)
  2974. {
  2975. case 0:
  2976. bonusID = ran() % PRIMARY_SKILLS;
  2977. break;
  2978. case 1:
  2979. bonusID = ran() % SKILL_QUANTITY;
  2980. break;
  2981. case 2:
  2982. bonusID = ran() % SPELLS_QUANTITY;
  2983. break;
  2984. }
  2985. }
  2986. }
  2987. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2988. {
  2989. int sound = 0;
  2990. int txtid = -1;
  2991. switch(ID)
  2992. {
  2993. case 22: //Corpse
  2994. txtid = 37;
  2995. sound = soundBase::MYSTERY;
  2996. break;
  2997. case 39: //Lean To
  2998. sound = soundBase::GENIE;
  2999. txtid = 64;
  3000. break;
  3001. case 105://Wagon
  3002. sound = soundBase::GENIE;
  3003. txtid = 154;
  3004. break;
  3005. case 108:
  3006. break;
  3007. default:
  3008. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3009. return;
  3010. }
  3011. if(ID == 108)//Warrior's Tomb
  3012. {
  3013. //ask if player wants to search the Tomb
  3014. BlockingDialog bd(true, false);
  3015. sound = soundBase::GRAVEYARD;
  3016. bd.player = h->getOwner();
  3017. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3018. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3019. return;
  3020. }
  3021. InfoWindow iw;
  3022. iw.soundID = sound;
  3023. iw.player = h->getOwner();
  3024. if(players.size()) //we have been already visited...
  3025. {
  3026. txtid++;
  3027. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  3028. txtid++;
  3029. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3030. }
  3031. else //first visit - give bonus!
  3032. {
  3033. switch(artOrRes)
  3034. {
  3035. case 0:
  3036. txtid++;
  3037. break;
  3038. case 1: //art
  3039. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3040. cb->giveHeroArtifact(bonusType,h->id,-2);
  3041. break;
  3042. case 2: //res
  3043. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  3044. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  3045. break;
  3046. }
  3047. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3048. if(ID == 105 && artOrRes == 1)
  3049. {
  3050. iw.text.localStrings.back().second++;
  3051. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3052. }
  3053. }
  3054. cb->showInfoDialog(&iw);
  3055. cb->setObjProperty(id,10,h->getOwner());
  3056. }
  3057. const std::string & CGOnceVisitable::getHoverText() const
  3058. {
  3059. hoverName = VLC->generaltexth->names[ID] + " ";
  3060. hoverName += (hasVisited(cb->getCurrentPlayer())
  3061. ? (VLC->generaltexth->allTexts[352]) //visited
  3062. : ( VLC->generaltexth->allTexts[353])); //not visited
  3063. return hoverName;
  3064. }
  3065. void CGOnceVisitable::initObj()
  3066. {
  3067. switch(ID)
  3068. {
  3069. case 22: //Corpse
  3070. {
  3071. blockVisit = true;
  3072. int hlp = ran()%100;
  3073. if(hlp < 20)
  3074. {
  3075. artOrRes = 1;
  3076. std::vector<CArtifact*> arts;
  3077. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3078. bonusType = arts[ran() % arts.size()]->id;
  3079. }
  3080. else
  3081. {
  3082. artOrRes = 0;
  3083. }
  3084. }
  3085. break;
  3086. case 39: //Lean To
  3087. {
  3088. artOrRes = 2;
  3089. bonusType = ran()%6; //any basic resource without gold
  3090. bonusVal = ran()%4 + 1;
  3091. break;
  3092. }
  3093. case 108://Warrior's Tomb
  3094. {
  3095. artOrRes = 1;
  3096. std::vector<CArtifact*> arts;
  3097. int hlp = ran()%100;
  3098. if(hlp < 30)
  3099. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  3100. else if(hlp < 80)
  3101. cb->getAllowed(arts,CArtifact::ART_MINOR);
  3102. else if(hlp < 95)
  3103. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  3104. else
  3105. cb->getAllowed(arts,CArtifact::ART_RELIC);
  3106. bonusType = arts[ran() % arts.size()]->id;
  3107. }
  3108. break;
  3109. case 105://Wagon
  3110. {
  3111. int hlp = ran()%100;
  3112. if(hlp < 10)
  3113. {
  3114. artOrRes = 0; // nothing... :(
  3115. }
  3116. else if(hlp < 50) //minor or treasure art
  3117. {
  3118. artOrRes = 1;
  3119. std::vector<CArtifact*> arts;
  3120. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3121. bonusType = arts[ran() % arts.size()]->id;
  3122. }
  3123. else //2 - 5 of non-gold resource
  3124. {
  3125. artOrRes = 2;
  3126. bonusType = ran()%6;
  3127. bonusVal = ran()%4 + 2;
  3128. }
  3129. break;
  3130. }
  3131. }
  3132. }
  3133. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3134. {
  3135. if(accept)
  3136. {
  3137. InfoWindow iw;
  3138. iw.player = h->getOwner();
  3139. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3140. if(players.size()) //we've been already visited, player found nothing
  3141. {
  3142. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3143. }
  3144. else //first visit - give artifact
  3145. {
  3146. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3147. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3148. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3149. cb->giveHeroArtifact(bonusType,h->id,-2);
  3150. }
  3151. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3152. {
  3153. //ruin morale
  3154. GiveBonus gb;
  3155. gb.hid = h->id;
  3156. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3157. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3158. cb->giveHeroBonus(&gb);
  3159. }
  3160. }
  3161. }
  3162. void CBank::initObj()
  3163. {
  3164. index = 0;
  3165. switch (ID) //find apriopriate key
  3166. {
  3167. case 16: //bank
  3168. index = subID; break;
  3169. case 24: //derelict ship
  3170. index = 8;
  3171. case 25: //utopia
  3172. index = 10; break;
  3173. case 84: //crypt
  3174. index = 9; break;
  3175. case 85: //shipwreck
  3176. index = 7; break;
  3177. }
  3178. bc = NULL;
  3179. daycounter = 0;
  3180. multiplier = 1;
  3181. }
  3182. void CBank::reset()
  3183. {
  3184. int val1 = ran()%100;
  3185. int chance = 0;
  3186. for (ui8 i = 1; i <= VLC->objh->banksInfo[index].size(); i++)
  3187. {
  3188. if (val1 < (chance += VLC->objh->banksInfo[index][i].chance))
  3189. cb->setObjProperty (id, 13, i);
  3190. }
  3191. artifacts.clear();
  3192. for (ui8 i = 1; i <= 4; i++)
  3193. {
  3194. for (ui8 j = 1; j <= bc->artifacts[i]; j++)
  3195. cb->setObjProperty (id, 18, i);
  3196. }
  3197. }
  3198. void CBank::setPropertyDer (ui8 what, ui32 val)
  3199. {
  3200. switch (what)
  3201. {
  3202. case 11: //daycounter
  3203. daycounter++;
  3204. break;
  3205. case 12: //multiplier
  3206. multiplier *= ((float)val)/100;
  3207. break;
  3208. case 13: //bank preset
  3209. bc = &VLC->objh->banksInfo[index][val];
  3210. break;
  3211. case 18: //Artifacts
  3212. {
  3213. std::vector<CArtifact*> arts;
  3214. switch (val)
  3215. {
  3216. case 1:
  3217. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  3218. break;
  3219. case 2:
  3220. cb->getAllowed (arts, CArtifact::ART_MINOR);
  3221. break;
  3222. case 3:
  3223. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  3224. break;
  3225. case 4:
  3226. cb->getAllowed (arts, CArtifact::ART_RELIC);
  3227. break;
  3228. }
  3229. artifacts.push_back (arts[ran() % arts.size()]->id);
  3230. }
  3231. break;
  3232. }
  3233. }
  3234. void CBank::newTurn()
  3235. {
  3236. if (bc == NULL)
  3237. {
  3238. if (daycounter >= 28 || cb->getDate(0) == 1)
  3239. {
  3240. reset();
  3241. daycounter = 0;
  3242. if (ID == 24 && cb->getDate(0) > 1)
  3243. {
  3244. artifacts.clear(); //derelict ships are usable only once
  3245. }
  3246. }
  3247. else
  3248. daycounter++;
  3249. }
  3250. }
  3251. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3252. {
  3253. if (bc != NULL)
  3254. {
  3255. int banktext = 0;
  3256. switch (ID)
  3257. {
  3258. case 16: //generic bank
  3259. banktext = 32;
  3260. break;
  3261. case 24:
  3262. banktext = 41;
  3263. break;
  3264. case 25: //utopia
  3265. banktext = 47;
  3266. break;
  3267. case 84: //crypt
  3268. banktext = 119;
  3269. break;
  3270. case 85: //shipwreck
  3271. banktext = 122;
  3272. break;
  3273. }
  3274. BlockingDialog bd (true, false);
  3275. bd.player = h->getOwner();
  3276. bd.soundID = soundBase::DANGER;
  3277. std::string desc = VLC->generaltexth->allTexts[banktext];
  3278. boost::algorithm::replace_first (desc, "%s", VLC->generaltexth->names[ID]);
  3279. bd.text << desc;
  3280. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3281. }
  3282. else
  3283. {
  3284. InfoWindow iw;
  3285. if (ID == 85)
  3286. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3287. iw.soundID = soundBase::GRAVEYARD;
  3288. iw.player = h->getOwner();
  3289. iw.text.addTxt (MetaString::ADVOB_TXT, 33);
  3290. cb->showInfoDialog(&iw);
  3291. }
  3292. }
  3293. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3294. {
  3295. if (accept)
  3296. {
  3297. int upgraded = 0;
  3298. if (ran()%100 < bc->upgradeChance) upgraded = 1;
  3299. CCreatureSet ourArmy;
  3300. switch (bc->guards.size())
  3301. {
  3302. case 1:
  3303. for (int i = 1; i <= 5; i++)
  3304. ourArmy.setCreature (i, bc->guards[1].first + upgraded, bc->guards[1].second / 5 );
  3305. break;
  3306. case 4:
  3307. {
  3308. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3309. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3310. ourArmy.setCreature (ourArmy.slots.size() + 1, it->first, it->second );
  3311. }
  3312. break;
  3313. default:
  3314. tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3315. return;
  3316. }
  3317. //TODO: start combat
  3318. }
  3319. }
  3320. void CBank::endBattle (const BattleResult *result)
  3321. {
  3322. if (result->winner == 0)
  3323. {
  3324. int textID = -1;
  3325. InfoWindow iw;
  3326. switch (ID)
  3327. {
  3328. case 16: //generic bank
  3329. textID = 34;
  3330. break;
  3331. case 24: //derelict ship
  3332. if (bc->resources.size() != 0)
  3333. textID = 43;
  3334. else
  3335. {
  3336. textID = 42;
  3337. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3338. }
  3339. break;
  3340. case 25: //utopia
  3341. textID = 47;
  3342. break;
  3343. case 84: //crypt
  3344. textID = 121;
  3345. break;
  3346. case 85: //shipwreck
  3347. if (bc->resources.size() != 0)
  3348. textID = 124;
  3349. else
  3350. {
  3351. textID = 123;
  3352. }
  3353. break;
  3354. }
  3355. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  3356. iw.player = cb->getCurrentPlayer();
  3357. for (std::map<ui8, si32>::iterator it = bc->resources.begin(); it != bc->resources.end(); it++)
  3358. {
  3359. iw.components.push_back (Component (Component::RESOURCE, it->first, it->second, 0));
  3360. cb->giveResource (cb->getCurrentPlayer(), it->first, it->second);
  3361. }
  3362. for (std::vector<si32>::iterator it = artifacts.begin(); it != artifacts.end(); it++)
  3363. {
  3364. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3365. iw.text.addReplacement (MetaString::ART_NAMES, *it);
  3366. cb->giveHeroArtifact (*it, cb->getSelectedHero() ,-2);
  3367. }
  3368. CCreatureSet ourArmy;
  3369. for (std::vector< std::pair <ui16, ui32> >::iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  3370. {
  3371. int slot = ourArmy.getSlotFor (it->second);
  3372. ourArmy.slots[slot] = *it; //assuming we're not going to add multiple stacks of same creature
  3373. }
  3374. cb->giveCreatures (id, cb->getHero (cb->getSelectedHero()), &ourArmy);
  3375. bc = NULL;
  3376. }
  3377. else
  3378. reset();
  3379. }
  3380. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  3381. {
  3382. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  3383. playerKeyMap.find(what-101)->second.insert(val);
  3384. }
  3385. bool CGKeys::wasMyColorVisited (int player) const
  3386. {
  3387. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  3388. return true;
  3389. else
  3390. return false;
  3391. }
  3392. const std::string & CGKeymasterTent::getHoverText() const
  3393. {
  3394. hoverName = VLC->generaltexth->names[ID];
  3395. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3396. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3397. else
  3398. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3399. return hoverName;
  3400. }
  3401. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  3402. {
  3403. InfoWindow iw;
  3404. iw.soundID = soundBase::CAVEHEAD;
  3405. iw.player = h->getOwner();
  3406. if (!wasMyColorVisited (h->getOwner()) )
  3407. {
  3408. cb->setObjProperty(id, h->tempOwner+101, subID);
  3409. iw.text << std::pair<ui8,ui32>(11,19);
  3410. }
  3411. else
  3412. iw.text << std::pair<ui8,ui32>(11,20);
  3413. cb->showInfoDialog(&iw);
  3414. }
  3415. void CGBorderGuard::initObj()
  3416. {
  3417. blockVisit = true;
  3418. }
  3419. const std::string & CGBorderGuard::getHoverText() const
  3420. {
  3421. hoverName = VLC->generaltexth->names[ID];
  3422. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3423. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3424. else
  3425. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3426. return hoverName;
  3427. }
  3428. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  3429. {
  3430. if (wasMyColorVisited (h->getOwner()) )
  3431. {
  3432. BlockingDialog bd (true, false);
  3433. bd.player = h->getOwner();
  3434. bd.soundID = soundBase::QUEST;
  3435. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  3436. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  3437. }
  3438. else
  3439. {
  3440. InfoWindow iw;
  3441. iw.player = h->getOwner();
  3442. iw.soundID = soundBase::CAVEHEAD;
  3443. iw.text << std::pair<ui8,ui32>(11,18);
  3444. cb->showInfoDialog (&iw);
  3445. }
  3446. }
  3447. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  3448. {
  3449. if (accept)
  3450. cb->removeObject(id);
  3451. }
  3452. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  3453. {
  3454. InfoWindow iw;
  3455. iw.player = h->getOwner();
  3456. if (!wasMyColorVisited (h->getOwner()) )
  3457. {
  3458. iw.text << std::pair<ui8,ui32>(11,18);
  3459. cb->showInfoDialog(&iw);
  3460. }
  3461. }
  3462. void CGMagi::initObj()
  3463. {
  3464. if (ID == 27)
  3465. {
  3466. blockVisit = true;
  3467. eyelist[subID].push_back(id);
  3468. }
  3469. }
  3470. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  3471. {
  3472. if (ID == 37)
  3473. {
  3474. InfoWindow iw;
  3475. CenterView cv;
  3476. FoWChange fw;
  3477. cv.player = iw.player = fw.player = h->tempOwner;
  3478. iw.soundID = soundBase::LIGHTHOUSE;
  3479. iw.player = h->tempOwner;
  3480. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  3481. cb->showInfoDialog(&iw);
  3482. fw.mode = 1;
  3483. std::vector<si32>::iterator it;
  3484. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  3485. {
  3486. const CGObjectInstance *eye = cb->getObj(*it);
  3487. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  3488. cb->sendAndApply(&fw);
  3489. cv.pos = eye->pos;
  3490. cv.focusTime = 2000;
  3491. cb->sendAndApply(&cv);
  3492. }
  3493. cv.pos = h->getPosition(false);
  3494. cb->sendAndApply(&cv);
  3495. }
  3496. else if (ID == 27)
  3497. {
  3498. InfoWindow iw;
  3499. iw.player = h->tempOwner;
  3500. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  3501. cb->showInfoDialog(&iw);
  3502. }
  3503. }
  3504. void CGBoat::initObj()
  3505. {
  3506. hero = NULL;
  3507. }
  3508. void CGSirens::initObj()
  3509. {
  3510. blockVisit = true;
  3511. }
  3512. const std::string & CGSirens::getHoverText() const
  3513. {
  3514. getNameVis(hoverName);
  3515. return hoverName;
  3516. }
  3517. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  3518. {
  3519. int message;
  3520. InfoWindow iw;
  3521. iw.soundID = soundBase::DANGER;
  3522. iw.player = h->tempOwner;
  3523. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  3524. {
  3525. iw.text.addTxt(11,133);
  3526. }
  3527. else
  3528. {
  3529. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  3530. int xp = 0;
  3531. SetGarrisons sg;
  3532. sg.garrs[h->id] = h->army;
  3533. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  3534. {
  3535. int drown = (int)(i->second.second * 0.3);
  3536. if(drown)
  3537. {
  3538. sg.garrs[h->id].slots[i->first].second -= drown;
  3539. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  3540. }
  3541. }
  3542. if(xp)
  3543. {
  3544. iw.text.addTxt(11,132);
  3545. iw.text.addReplacement(xp);
  3546. cb->sendAndApply(&sg);
  3547. }
  3548. else
  3549. {
  3550. iw.text.addTxt(11,134);
  3551. }
  3552. }
  3553. cb->showInfoDialog(&iw);
  3554. }
  3555. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  3556. {
  3557. cost.resize(RESOURCE_QUANTITY);
  3558. cost[0] = 10;
  3559. cost[6] = 1000;
  3560. }
  3561. //bool IShipyard::validLocation() const
  3562. //{
  3563. // std::vector<int3> offsets;
  3564. // getOutOffsets(offsets);
  3565. //
  3566. // TerrainTile *tile;
  3567. // for(int i = 0; i < offsets.size(); i++)
  3568. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3569. // return true;
  3570. // return false;
  3571. //}
  3572. int3 IShipyard::bestLocation() const
  3573. {
  3574. std::vector<int3> offsets;
  3575. getOutOffsets(offsets);
  3576. TerrainTile *tile;
  3577. for(int i = 0; i < offsets.size(); i++)
  3578. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3579. return o->pos + offsets[i];
  3580. return int3(-1,-1,-1);
  3581. }
  3582. IShipyard::IShipyard(const CGObjectInstance *O)
  3583. : o(O)
  3584. {
  3585. }
  3586. int IShipyard::state() const
  3587. {
  3588. int3 tile = bestLocation();
  3589. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  3590. if(!t)
  3591. return 3; //no water
  3592. else if(!t->blockingObjects.size())
  3593. return 0; //OK
  3594. else if(t->blockingObjects.front()->ID == 8)
  3595. return 1; //blocked with boat
  3596. else
  3597. return 2; //blocked
  3598. }
  3599. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  3600. {
  3601. if(obj->ID == TOWNI_TYPE)
  3602. {
  3603. return static_cast<CGTownInstance*>(obj);
  3604. }
  3605. else if(obj->ID == 87)
  3606. {
  3607. return static_cast<CGShipyard*>(obj);
  3608. }
  3609. else
  3610. {
  3611. tlog1 << "Cannot cast to IShipyar object with ID " << obj->ID << std::endl;
  3612. return NULL;
  3613. }
  3614. }
  3615. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  3616. {
  3617. return castFrom(const_cast<CGObjectInstance*>(obj));
  3618. }
  3619. CGShipyard::CGShipyard()
  3620. :IShipyard(this)
  3621. {
  3622. }
  3623. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  3624. {
  3625. offsets += int3(1,0,0), int3(-3,0,0), int3(1,1,0), int3(-3,1,0), int3(1,-1,0), int3(-3,-1,0),
  3626. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  3627. }
  3628. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  3629. {
  3630. if(tempOwner != h->tempOwner)
  3631. cb->setOwner(id, h->tempOwner);
  3632. int s = state();
  3633. if(s)
  3634. {
  3635. InfoWindow iw;
  3636. iw.player = tempOwner;
  3637. switch(s)
  3638. {
  3639. case 1:
  3640. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  3641. break;
  3642. case 2:
  3643. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  3644. break;
  3645. case 3:
  3646. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  3647. return;
  3648. }
  3649. cb->showInfoDialog(&iw);
  3650. }
  3651. else
  3652. {
  3653. OpenWindow ow;
  3654. ow.id1 = id;
  3655. ow.id2 = h->id;
  3656. ow.window = OpenWindow::SHIPYARD_WINDOW;
  3657. cb->sendAndApply(&ow);
  3658. }
  3659. }