CObjectHandler.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023
  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()) //town has lookout tower
  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. //support for legs of legion etc.
  1131. if(garrisonHero)
  1132. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1133. if(visitingHero)
  1134. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1135. return ret;
  1136. }
  1137. int CGTownInstance::dailyIncome() const
  1138. {
  1139. int ret = 0;
  1140. if ((builtBuildings.find(26))!=builtBuildings.end())
  1141. ret+=5000;
  1142. if ((builtBuildings.find(13))!=builtBuildings.end())
  1143. ret+=4000;
  1144. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1145. ret+=2000;
  1146. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1147. ret+=1000;
  1148. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1149. ret+=500;
  1150. return ret;
  1151. }
  1152. bool CGTownInstance::hasFort() const
  1153. {
  1154. return (builtBuildings.find(7))!=builtBuildings.end();
  1155. }
  1156. bool CGTownInstance::hasCapitol() const
  1157. {
  1158. return (builtBuildings.find(13))!=builtBuildings.end();
  1159. }
  1160. CGTownInstance::CGTownInstance()
  1161. :IShipyard(this)
  1162. {
  1163. builded=-1;
  1164. destroyed=-1;
  1165. garrisonHero=NULL;
  1166. town=NULL;
  1167. visitingHero = NULL;
  1168. }
  1169. CGTownInstance::~CGTownInstance()
  1170. {}
  1171. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1172. {
  1173. if(checkGuild && mageGuildLevel() < level)
  1174. return 0;
  1175. int ret = 6 - level; //how many spells are available at this level
  1176. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1177. ret++;
  1178. return ret;
  1179. }
  1180. bool CGTownInstance::needsLastStack() const
  1181. {
  1182. if(garrisonHero)
  1183. return true;
  1184. else return false;
  1185. }
  1186. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1187. {
  1188. if(getOwner() != h->getOwner())
  1189. {
  1190. return;
  1191. }
  1192. cb->heroVisitCastle(id,h->id);
  1193. }
  1194. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1195. {
  1196. cb->stopHeroVisitCastle(id,h->id);
  1197. }
  1198. void CGTownInstance::initObj()
  1199. {
  1200. hoverName = name + ", " + town->Name();
  1201. creatures.resize(CREATURES_PER_TOWN);
  1202. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1203. {
  1204. if(creatureDwelling(i,false))
  1205. creatures[i].second.push_back(town->basicCreatures[i]);
  1206. if(creatureDwelling(i,true))
  1207. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1208. }
  1209. }
  1210. int3 CGTownInstance::getSightCenter() const
  1211. {
  1212. return pos - int3(2,0,0);
  1213. }
  1214. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1215. {
  1216. offsets += int3(-1,3,0), int3(-3,3,0);
  1217. }
  1218. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1219. {
  1220. if(visitors.find(h->id)==visitors.end())
  1221. {
  1222. onNAHeroVisit(h->id, false);
  1223. switch(ID)
  1224. {
  1225. case 102: //tree
  1226. case 4: //arena
  1227. case 41://library
  1228. case 47: //School of Magic
  1229. case 107://School of War
  1230. break;
  1231. default:
  1232. cb->setObjProperty(id,4,h->id); //add to the visitors
  1233. break;
  1234. }
  1235. }
  1236. else
  1237. {
  1238. onNAHeroVisit(h->id, true);
  1239. }
  1240. }
  1241. void CGVisitableOPH::initObj()
  1242. {
  1243. if(ID==102)
  1244. ttype = ran()%3;
  1245. else
  1246. ttype = -1;
  1247. }
  1248. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1249. {
  1250. if(result) //player agreed to give res for exp
  1251. {
  1252. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1253. cb->changePrimSkill(heroID,4,expVal); //give exp
  1254. cb->setObjProperty(id,4,heroID); //add to the visitors
  1255. }
  1256. }
  1257. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1258. {
  1259. int id=0, subid=0, ot=0, val=1, sound = 0;
  1260. switch(ID)
  1261. {
  1262. case 4: //arena
  1263. sound = soundBase::NOMAD;
  1264. ot = 0;
  1265. break;
  1266. case 51: //mercenary camp
  1267. sound = soundBase::NOMAD;
  1268. subid=0;
  1269. ot=80;
  1270. break;
  1271. case 23: //marletto tower
  1272. sound = soundBase::NOMAD;
  1273. subid=1;
  1274. ot=39;
  1275. break;
  1276. case 61:
  1277. sound = soundBase::gazebo;
  1278. subid=2;
  1279. ot=100;
  1280. break;
  1281. case 32:
  1282. sound = soundBase::GETPROTECTION;
  1283. subid=3;
  1284. ot=59;
  1285. break;
  1286. case 100:
  1287. sound = soundBase::gazebo;
  1288. id=5;
  1289. ot=143;
  1290. val=1000;
  1291. break;
  1292. case 102:
  1293. sound = soundBase::gazebo;
  1294. id = 5;
  1295. subid = 1;
  1296. ot = 146;
  1297. val = 1;
  1298. break;
  1299. case 41:
  1300. sound = soundBase::gazebo;
  1301. ot = 66;
  1302. break;
  1303. case 47: //School of Magic
  1304. sound = soundBase::faerie;
  1305. ot = 71;
  1306. break;
  1307. case 107://School of War
  1308. sound = soundBase::MILITARY;
  1309. ot = 158;
  1310. break;
  1311. }
  1312. if (!alreadyVisited)
  1313. {
  1314. switch (ID)
  1315. {
  1316. case 4: //arena
  1317. {
  1318. BlockingDialog sd(false,true);
  1319. sd.soundID = sound;
  1320. sd.text << std::pair<ui8,ui32>(11,ot);
  1321. sd.components.push_back(Component(0,0,2,0));
  1322. sd.components.push_back(Component(0,1,2,0));
  1323. sd.player = cb->getOwner(heroID);
  1324. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1325. return;
  1326. }
  1327. case 51:
  1328. case 23:
  1329. case 61:
  1330. case 32:
  1331. {
  1332. cb->changePrimSkill(heroID,subid,val);
  1333. InfoWindow iw;
  1334. iw.soundID = sound;
  1335. iw.components.push_back(Component(0,subid,val,0));
  1336. iw.text << std::pair<ui8,ui32>(11,ot);
  1337. iw.player = cb->getOwner(heroID);
  1338. cb->showInfoDialog(&iw);
  1339. break;
  1340. }
  1341. case 100: //give exp
  1342. {
  1343. InfoWindow iw;
  1344. iw.soundID = sound;
  1345. iw.components.push_back(Component(id,subid,val,0));
  1346. iw.player = cb->getOwner(heroID);
  1347. iw.text << std::pair<ui8,ui32>(11,ot);
  1348. iw.soundID = soundBase::gazebo;
  1349. cb->showInfoDialog(&iw);
  1350. cb->changePrimSkill(heroID,4,val);
  1351. break;
  1352. }
  1353. case 102://tree
  1354. {
  1355. const CGHeroInstance *h = cb->getHero(heroID);
  1356. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1357. if(!ttype)
  1358. {
  1359. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1360. InfoWindow iw;
  1361. iw.soundID = sound;
  1362. iw.components.push_back(Component(id,subid,1,0));
  1363. iw.player = cb->getOwner(heroID);
  1364. iw.text << std::pair<ui8,ui32>(11,148);
  1365. cb->showInfoDialog(&iw);
  1366. cb->changePrimSkill(heroID,4,val);
  1367. break;
  1368. }
  1369. else
  1370. {
  1371. int res, resval;
  1372. if(ttype==1)
  1373. {
  1374. res = 6;
  1375. resval = 2000;
  1376. ot = 149;
  1377. }
  1378. else
  1379. {
  1380. res = 5;
  1381. resval = 10;
  1382. ot = 151;
  1383. }
  1384. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1385. {
  1386. ot++;
  1387. InfoWindow iw;
  1388. iw.soundID = sound;
  1389. iw.player = h->tempOwner;
  1390. iw.text << std::pair<ui8,ui32>(11,ot);
  1391. cb->showInfoDialog(&iw);
  1392. return;
  1393. }
  1394. BlockingDialog sd(true,false);
  1395. sd.soundID = sound;
  1396. sd.player = cb->getOwner(heroID);
  1397. sd.text << std::pair<ui8,ui32>(11,ot);
  1398. sd.components.push_back(Component(id,subid,val,0));
  1399. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1400. }
  1401. break;
  1402. }
  1403. case 41://library of enlightenment
  1404. {
  1405. const CGHeroInstance *h = cb->getHero(heroID);
  1406. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1407. {
  1408. InfoWindow iw;
  1409. iw.soundID = sound;
  1410. iw.player = cb->getOwner(heroID);
  1411. iw.text << std::pair<ui8,ui32>(11,68);
  1412. cb->showInfoDialog(&iw);
  1413. }
  1414. else
  1415. {
  1416. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1417. cb->changePrimSkill(heroID,0,2);
  1418. cb->changePrimSkill(heroID,1,2);
  1419. cb->changePrimSkill(heroID,2,2);
  1420. cb->changePrimSkill(heroID,3,2);
  1421. InfoWindow iw;
  1422. iw.soundID = sound;
  1423. iw.player = cb->getOwner(heroID);
  1424. iw.text << std::pair<ui8,ui32>(11,66);
  1425. cb->showInfoDialog(&iw);
  1426. }
  1427. break;
  1428. }
  1429. case 47: //School of Magic
  1430. case 107://School of War
  1431. {
  1432. int skill = (ID==47 ? 2 : 0);
  1433. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1434. {
  1435. InfoWindow iw;
  1436. iw.soundID = sound;
  1437. iw.player = cb->getOwner(heroID);
  1438. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1439. cb->showInfoDialog(&iw);
  1440. }
  1441. else
  1442. {
  1443. BlockingDialog sd(true,true);
  1444. sd.soundID = sound;
  1445. sd.player = cb->getOwner(heroID);
  1446. sd.text << std::pair<ui8,ui32>(11,ot);
  1447. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1448. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1449. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1450. }
  1451. }
  1452. break;
  1453. }
  1454. }
  1455. else
  1456. {
  1457. ot++;
  1458. InfoWindow iw;
  1459. iw.soundID = sound;
  1460. iw.player = cb->getOwner(heroID);
  1461. iw.text << std::pair<ui8,ui32>(11,ot);
  1462. cb->showInfoDialog(&iw);
  1463. }
  1464. }
  1465. const std::string & CGVisitableOPH::getHoverText() const
  1466. {
  1467. int pom = -1;
  1468. switch(ID)
  1469. {
  1470. case 4:
  1471. pom = -1;
  1472. break;
  1473. case 51:
  1474. pom = 8;
  1475. break;
  1476. case 23:
  1477. pom = 7;
  1478. break;
  1479. case 61:
  1480. pom = 11;
  1481. break;
  1482. case 32:
  1483. pom = 4;
  1484. break;
  1485. case 100:
  1486. pom = 5;
  1487. break;
  1488. case 102:
  1489. pom = 18;
  1490. break;
  1491. case 41:
  1492. break;
  1493. case 47: //School of Magic
  1494. pom = 9;
  1495. break;
  1496. case 107://School of War
  1497. pom = 10;
  1498. break;
  1499. default:
  1500. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1501. }
  1502. hoverName = VLC->generaltexth->names[ID];
  1503. if(pom >= 0)
  1504. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1505. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1506. if(h)
  1507. {
  1508. hoverName += ' ';
  1509. hoverName += (vstd::contains(visitors,h->id))
  1510. ? (VLC->generaltexth->allTexts[352]) //visited
  1511. : ( VLC->generaltexth->allTexts[353]); //not visited
  1512. }
  1513. return hoverName;
  1514. }
  1515. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1516. {
  1517. cb->setObjProperty(id,4,heroID); //add to the visitors
  1518. cb->changePrimSkill(heroID,primSkill-1,2);
  1519. }
  1520. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1521. {
  1522. if(what == 4)
  1523. visitors.insert(val);
  1524. }
  1525. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1526. {
  1527. if(!which) //player refused to pay
  1528. return;
  1529. int base = (ID == 47 ? 2 : 0);
  1530. cb->setObjProperty(id,4,heroID); //add to the visitors
  1531. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1532. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1533. }
  1534. bool CArmedInstance::needsLastStack() const
  1535. {
  1536. return false;
  1537. }
  1538. int CArmedInstance::getArmyStrength() const
  1539. {
  1540. int ret = 0;
  1541. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1542. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1543. return ret;
  1544. }
  1545. /*const std::string & CGCreature::getHoverText() const
  1546. {
  1547. hoverName = VLC->generaltexth->names[ID];
  1548. hoverName += "\n Power rating: ";
  1549. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  1550. if (ratio < 0.1) hoverName += "Effortless";
  1551. else if (ratio < 0.3) hoverName += "Very Weak";
  1552. else if (ratio < 0.6) hoverName += "Weak";
  1553. else if (ratio < 0.9) hoverName += "A bit weaker";
  1554. else if (ratio < 1.1) hoverName += "Equal";
  1555. else if (ratio < 1.3) hoverName += "A bit stronger";
  1556. else if (ratio < 1.8) hoverName += "Strong";
  1557. else if (ratio < 2.5) hoverName += "Very Strong";
  1558. else if (ratio < 4) hoverName += "Challenging";
  1559. else if (ratio < 8) hoverName += "Overpowering";
  1560. else if (ratio < 20) hoverName += "Deadly";
  1561. else hoverName += "Impossible";
  1562. return hoverName;
  1563. }*/
  1564. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1565. {
  1566. int action = takenAction(h);
  1567. switch( action ) //decide what we do...
  1568. {
  1569. case -2: //fight
  1570. fight(h);
  1571. break;
  1572. case -1: //flee
  1573. {
  1574. flee(h);
  1575. break;
  1576. }
  1577. case 0: //join for free
  1578. {
  1579. BlockingDialog ynd(true,false);
  1580. ynd.player = h->tempOwner;
  1581. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1582. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1583. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1584. break;
  1585. }
  1586. default: //join for gold
  1587. {
  1588. assert(action > 0);
  1589. //ask if player agrees to pay gold
  1590. BlockingDialog ynd(true,false);
  1591. ynd.player = h->tempOwner;
  1592. std::string tmp = VLC->generaltexth->advobtxt[90];
  1593. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1594. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1595. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1596. ynd.text << tmp;
  1597. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1598. break;
  1599. }
  1600. }
  1601. }
  1602. void CGCreature::endBattle( BattleResult *result ) const
  1603. {
  1604. if(result->winner==0)
  1605. {
  1606. cb->removeObject(id);
  1607. }
  1608. else
  1609. {
  1610. //int killedAmount=0;
  1611. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1612. // if(i->first == subID)
  1613. // killedAmount += i->second;
  1614. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1615. MetaString ms;
  1616. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1617. pom = 174 + 3*pom + 1;
  1618. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1619. cb->setHoverName(id,&ms);
  1620. }
  1621. }
  1622. void CGCreature::initObj()
  1623. {
  1624. blockVisit = true;
  1625. switch(character)
  1626. {
  1627. case 0:
  1628. character = 0;
  1629. break;
  1630. case 1:
  1631. character = 1 + ran()%7;
  1632. break;
  1633. case 2:
  1634. character = 1 + ran()%10;
  1635. break;
  1636. case 3:
  1637. character = 4 + ran()%7;
  1638. break;
  1639. case 4:
  1640. character = 10;
  1641. break;
  1642. }
  1643. army.slots[0].first = subID;
  1644. si32 &amount = army.slots[0].second;
  1645. CCreature &c = VLC->creh->creatures[subID];
  1646. if(!amount)
  1647. if(c.ammMax == c.ammMin)
  1648. amount = c.ammMax;
  1649. else
  1650. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1651. MetaString ms;
  1652. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1653. pom = 174 + 3*pom + 1;
  1654. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1655. ms.toString(hoverName);
  1656. }
  1657. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1658. {
  1659. double hlp = h->getTotalStrength() / getArmyStrength();
  1660. if(!character) //compliant creatures will always join
  1661. return 0;
  1662. else if(allowJoin)//test for joining
  1663. {
  1664. int factor;
  1665. if(hlp >= 7)
  1666. factor = 11;
  1667. else if(hlp >= 1)
  1668. factor = (int)(2*(hlp-1));
  1669. else if(hlp >= 0.5)
  1670. factor = -1;
  1671. else if(hlp >= 0.333)
  1672. factor = -2;
  1673. else
  1674. factor = -3;
  1675. int sympathy = 0;
  1676. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1677. myKindCres.insert(subID); //we
  1678. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1679. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1680. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1681. myKindCres.insert(i->idNumber);
  1682. int count = 0, //how many creatures of our kind has hero
  1683. totalCount = 0;
  1684. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1685. {
  1686. if(vstd::contains(myKindCres,i->second.first))
  1687. count += i->second.second;
  1688. totalCount += i->second.second;
  1689. }
  1690. if(count*2 > totalCount)
  1691. sympathy++;
  1692. if(count)
  1693. sympathy++;
  1694. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1695. if(charisma >= character) //creatures might join...
  1696. {
  1697. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1698. return 0; //join for free
  1699. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1700. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1701. }
  1702. }
  1703. //we are still here - creatures not joined heroes, test for fleeing
  1704. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1705. //TODO: should be deterministic (will be needed for Vision spell)
  1706. int hlp2 = (int) (hlp - 2)*1000;
  1707. if(!neverFlees
  1708. && hlp2 >= 0
  1709. && rand()%2000 < hlp2
  1710. )
  1711. return -1; //flee
  1712. else
  1713. return -2; //fight
  1714. }
  1715. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1716. {
  1717. if(pursue)
  1718. {
  1719. fight(h);
  1720. }
  1721. else
  1722. {
  1723. cb->removeObject(id);
  1724. }
  1725. }
  1726. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1727. {
  1728. if(!accept)
  1729. {
  1730. if(takenAction(h,false) == -1) //they flee
  1731. {
  1732. flee(h);
  1733. }
  1734. else //they fight
  1735. {
  1736. InfoWindow iw;
  1737. iw.player = h->tempOwner;
  1738. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1739. cb->showInfoDialog(&iw);
  1740. fight(h);
  1741. }
  1742. }
  1743. else //accepted
  1744. {
  1745. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1746. {
  1747. InfoWindow iw;
  1748. iw.player = h->tempOwner;
  1749. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1750. cb->showInfoDialog(&iw);
  1751. //act as if player refused
  1752. joinDecision(h,cost,false);
  1753. return;
  1754. }
  1755. //take gold
  1756. if(cost)
  1757. cb->giveResource(h->tempOwner,6,-cost);
  1758. int slot = h->army.getSlotFor(subID);
  1759. if(slot >= 0) //there is place
  1760. {
  1761. //add creatures
  1762. SetGarrisons sg;
  1763. sg.garrs[h->id] = h->army;
  1764. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1765. {
  1766. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1767. }
  1768. else //add as a new stack
  1769. {
  1770. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1771. }
  1772. cb->sendAndApply(&sg);
  1773. cb->removeObject(id);
  1774. }
  1775. else
  1776. {
  1777. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1778. }
  1779. }
  1780. }
  1781. void CGCreature::fight( const CGHeroInstance *h ) const
  1782. {
  1783. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  1784. }
  1785. void CGCreature::flee( const CGHeroInstance * h ) const
  1786. {
  1787. BlockingDialog ynd(true,false);
  1788. ynd.player = h->tempOwner;
  1789. ynd.text << std::pair<ui8,ui32>(11,91);
  1790. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1791. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1792. }
  1793. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1794. {
  1795. if(subID == 7) //TODO: support for abandoned mine
  1796. return;
  1797. if(h->tempOwner == tempOwner) //we're visiting our mine
  1798. {
  1799. cb->showGarrisonDialog(id,h->id,0);
  1800. return;
  1801. }
  1802. //TODO: check if mine is guarded
  1803. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1804. MetaString ms;
  1805. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1806. cb->setHoverName(id,&ms);
  1807. int vv=1; //amount of resource per turn
  1808. if (subID==0 || subID==2)
  1809. vv++;
  1810. else if (subID==6)
  1811. vv = 1000;
  1812. InfoWindow iw;
  1813. iw.soundID = soundBase::FLAGMINE;
  1814. iw.text << std::pair<ui8,ui32>(10,subID);
  1815. iw.player = h->tempOwner;
  1816. iw.components.push_back(Component(2,subID,vv,-1));
  1817. cb->showInfoDialog(&iw);
  1818. }
  1819. void CGMine::newTurn() const
  1820. {
  1821. if(cb->getDate() == 1)
  1822. return;
  1823. if (tempOwner == NEUTRAL_PLAYER)
  1824. return;
  1825. int vv = 1;
  1826. if (subID==0 || subID==2)
  1827. vv++;
  1828. else if (subID==6)
  1829. vv = 1000;
  1830. cb->giveResource(tempOwner,subID,vv);
  1831. }
  1832. void CGMine::initObj()
  1833. {
  1834. MetaString ms;
  1835. ms << std::pair<ui8,ui32>(9,subID);
  1836. if(tempOwner >= PLAYER_LIMIT)
  1837. tempOwner = NEUTRAL_PLAYER;
  1838. else
  1839. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1840. ms.toString(hoverName);
  1841. }
  1842. void CGResource::initObj()
  1843. {
  1844. blockVisit = true;
  1845. hoverName = VLC->generaltexth->restypes[subID];
  1846. if(!amount)
  1847. {
  1848. switch(subID)
  1849. {
  1850. case 6:
  1851. amount = 500 + (rand()%6)*100;
  1852. break;
  1853. case 0: case 2:
  1854. amount = 6 + (rand()%5);
  1855. break;
  1856. default:
  1857. amount = 3 + (rand()%3);
  1858. break;
  1859. }
  1860. }
  1861. }
  1862. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1863. {
  1864. if(army.slots.size())
  1865. {
  1866. if(message.size())
  1867. {
  1868. BlockingDialog ynd(true,false);
  1869. ynd.player = h->getOwner();
  1870. ynd.text << message;
  1871. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1872. }
  1873. else
  1874. {
  1875. fightForRes(1,h);
  1876. }
  1877. }
  1878. else
  1879. {
  1880. if(message.length())
  1881. {
  1882. InfoWindow iw;
  1883. iw.player = h->tempOwner;
  1884. iw.text << message;
  1885. cb->showInfoDialog(&iw);
  1886. }
  1887. collectRes(h->getOwner());
  1888. }
  1889. }
  1890. void CGResource::collectRes( int player ) const
  1891. {
  1892. cb->giveResource(player,subID,amount);
  1893. ShowInInfobox sii;
  1894. sii.player = player;
  1895. sii.c = Component(2,subID,amount,0);
  1896. sii.text << std::pair<ui8,ui32>(11,113);
  1897. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  1898. cb->showCompInfo(&sii);
  1899. cb->removeObject(id);
  1900. }
  1901. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1902. {
  1903. if(agreed)
  1904. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  1905. }
  1906. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1907. {
  1908. if(result->winner == 0) //attacker won
  1909. collectRes(h->getOwner());
  1910. }
  1911. void CGVisitableOPW::newTurn() const
  1912. {
  1913. if (cb->getDate(1)==1) //first day of week
  1914. {
  1915. cb->setObjProperty(id,5,false);
  1916. MetaString ms; //set text to "not visited"
  1917. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1918. cb->setHoverName(id,&ms);
  1919. }
  1920. }
  1921. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1922. {
  1923. int mid, sound = 0;
  1924. switch (ID)
  1925. {
  1926. case 55: //mystical garden
  1927. sound = soundBase::experience;
  1928. mid = 92;
  1929. break;
  1930. case 112://windmill
  1931. sound = soundBase::GENIE;
  1932. mid = 170;
  1933. break;
  1934. case 109://waterwheel
  1935. sound = soundBase::GENIE;
  1936. mid = 164;
  1937. break;
  1938. }
  1939. if (visited)
  1940. {
  1941. if (ID!=112)
  1942. mid++;
  1943. else
  1944. mid--;
  1945. InfoWindow iw;
  1946. iw.soundID = sound;
  1947. iw.player = h->tempOwner;
  1948. iw.text << std::pair<ui8,ui32>(11,mid);
  1949. cb->showInfoDialog(&iw);
  1950. }
  1951. else
  1952. {
  1953. int type, sub, val;
  1954. type = 2;
  1955. switch (ID)
  1956. {
  1957. case 55:
  1958. if (rand()%2)
  1959. {
  1960. sub = 5;
  1961. val = 5;
  1962. }
  1963. else
  1964. {
  1965. sub = 6;
  1966. val = 500;
  1967. }
  1968. break;
  1969. case 112:
  1970. mid = 170;
  1971. sub = (rand() % 5) + 1;
  1972. val = (rand() % 4) + 3;
  1973. break;
  1974. case 109:
  1975. mid = 164;
  1976. sub = 6;
  1977. if(cb->getDate(0)<8)
  1978. val = 500;
  1979. else
  1980. val = 1000;
  1981. }
  1982. cb->giveResource(h->tempOwner,sub,val);
  1983. InfoWindow iw;
  1984. iw.soundID = sound;
  1985. iw.player = h->tempOwner;
  1986. iw.components.push_back(Component(type,sub,val,0));
  1987. iw.text << std::pair<ui8,ui32>(11,mid);
  1988. cb->showInfoDialog(&iw);
  1989. cb->setObjProperty(id,5,true);
  1990. MetaString ms; //set text to "visited"
  1991. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1992. cb->setHoverName(id,&ms);
  1993. }
  1994. }
  1995. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1996. {
  1997. if(what == 5)
  1998. visited = val;
  1999. }
  2000. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2001. {
  2002. int destinationid=-1;
  2003. switch(ID)
  2004. {
  2005. case 43: //one way - find corresponding exit monolith
  2006. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2007. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2008. else
  2009. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2010. break;
  2011. case 45: //two way monolith - pick any other one
  2012. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2013. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2014. else
  2015. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2016. break;
  2017. case 103: //find nearest subterranean gate on the other level
  2018. {
  2019. std::pair<int,double> best(-1,150000); //pair<id,dist>
  2020. for(int i=0; i<objs[103][0].size(); i++)
  2021. {
  2022. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  2023. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  2024. if(hlp<best.second)
  2025. {
  2026. best.first = objs[103][0][i];
  2027. best.second = hlp;
  2028. }
  2029. }
  2030. if(best.first<0)
  2031. return;
  2032. else
  2033. destinationid = best.first;
  2034. break;
  2035. }
  2036. }
  2037. if(destinationid < 0)
  2038. {
  2039. tlog2 << "Cannot find exit... :( \n";
  2040. return;
  2041. }
  2042. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2043. true);
  2044. }
  2045. void CGTeleport::initObj()
  2046. {
  2047. objs[ID][subID].push_back(id);
  2048. }
  2049. void CGArtifact::initObj()
  2050. {
  2051. blockVisit = true;
  2052. if(ID == 5)
  2053. hoverName = VLC->arth->artifacts[subID].Name();
  2054. }
  2055. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2056. {
  2057. if(!army.slots.size())
  2058. {
  2059. if(ID == 5)
  2060. {
  2061. InfoWindow iw;
  2062. iw.soundID = soundBase::treasure;
  2063. iw.player = h->tempOwner;
  2064. iw.components.push_back(Component(4,subID,0,0));
  2065. if(message.length())
  2066. iw.text << message;
  2067. else
  2068. iw.text << std::pair<ui8,ui32>(12,subID);
  2069. cb->showInfoDialog(&iw);
  2070. }
  2071. pick(h);
  2072. }
  2073. else
  2074. {
  2075. if(message.size())
  2076. {
  2077. BlockingDialog ynd(true,false);
  2078. ynd.player = h->getOwner();
  2079. ynd.text << message;
  2080. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2081. }
  2082. else
  2083. {
  2084. fightForArt(0,h);
  2085. }
  2086. }
  2087. }
  2088. void CGArtifact::pick(const CGHeroInstance * h) const
  2089. {
  2090. if(ID == 5) //Artifact
  2091. {
  2092. cb->giveHeroArtifact(subID,h->id,-2);
  2093. }
  2094. else if(ID == 93) // Spell scroll
  2095. {
  2096. //TODO: support for the spell scroll
  2097. }
  2098. cb->removeObject(id);
  2099. }
  2100. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2101. {
  2102. if(agreed)
  2103. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2104. }
  2105. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2106. {
  2107. if(result->winner == 0) //attacker won
  2108. pick(h);
  2109. }
  2110. void CGPickable::initObj()
  2111. {
  2112. blockVisit = true;
  2113. switch(ID)
  2114. {
  2115. case 12: //campfire
  2116. val2 = (ran()%3) + 4; //4 - 6
  2117. val1 = val2 * 100;
  2118. type = ran()%6; //given resource
  2119. break;
  2120. case 29: //floatsam
  2121. switch(type = ran()%4)
  2122. {
  2123. case 0:
  2124. val1 = val2 = 0;
  2125. break;
  2126. case 1:
  2127. val1 = 5;
  2128. val2 = 0;
  2129. break;
  2130. case 2:
  2131. val1 = 5;
  2132. val2 = 200;
  2133. break;
  2134. case 3:
  2135. val1 = 10;
  2136. val2 = 500;
  2137. break;
  2138. }
  2139. break;
  2140. case 82: //sea chest
  2141. {
  2142. int hlp = ran()%100;
  2143. if(hlp < 20)
  2144. {
  2145. val1 = 0;
  2146. type = 0;
  2147. }
  2148. else if(hlp < 90)
  2149. {
  2150. val1 = 1500;
  2151. type = 2;
  2152. }
  2153. else
  2154. {
  2155. val1 = 1000;
  2156. val2 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2157. type = 1;
  2158. }
  2159. }
  2160. break;
  2161. case 86: //Shipwreck Survivor
  2162. {
  2163. int hlp = ran()%100;
  2164. if(hlp < 55)
  2165. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2166. else if(hlp < 75)
  2167. val1 = VLC->arth->minors[ran()%VLC->arth->minors.size()]->id;
  2168. else if(hlp < 95)
  2169. val1 = VLC->arth->majors[ran()%VLC->arth->majors.size()]->id;
  2170. else
  2171. val1 = VLC->arth->relics[ran()%VLC->arth->relics.size()]->id;
  2172. }
  2173. break;
  2174. case 101: //treasure chest
  2175. {
  2176. int hlp = ran()%100;
  2177. if(hlp >= 95)
  2178. {
  2179. type = 1;
  2180. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2181. return;
  2182. }
  2183. else if (hlp >= 65)
  2184. {
  2185. val1 = 2000;
  2186. }
  2187. else if(hlp >= 33)
  2188. {
  2189. val1 = 1500;
  2190. }
  2191. else
  2192. {
  2193. val1 = 1000;
  2194. }
  2195. val2 = val1 - 500;
  2196. type = 0;
  2197. break;
  2198. }
  2199. }
  2200. }
  2201. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2202. {
  2203. switch(ID)
  2204. {
  2205. case 12: //campfire
  2206. {
  2207. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2208. cb->giveResource(h->tempOwner,6,val1);//gold
  2209. InfoWindow iw;
  2210. iw.soundID = soundBase::experience;
  2211. iw.player = h->tempOwner;
  2212. iw.components.push_back(Component(2,6,val1,0));
  2213. iw.components.push_back(Component(2,type,val2,0));
  2214. iw.text << std::pair<ui8,ui32>(11,23);
  2215. cb->showInfoDialog(&iw);
  2216. break;
  2217. }
  2218. case 29: //flotsam
  2219. {
  2220. cb->giveResource(h->tempOwner,0,val1); //wood
  2221. cb->giveResource(h->tempOwner,6,val2);//gold
  2222. InfoWindow iw;
  2223. iw.soundID = soundBase::GENIE;
  2224. iw.player = h->tempOwner;
  2225. if(val1)
  2226. iw.components.push_back(Component(2,0,val1,0));
  2227. if(val2)
  2228. iw.components.push_back(Component(2,6,val2,0));
  2229. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2230. cb->showInfoDialog(&iw);
  2231. break;
  2232. }
  2233. case 82: //Sea Chest
  2234. {
  2235. InfoWindow iw;
  2236. iw.soundID = soundBase::chest;
  2237. iw.player = h->tempOwner;
  2238. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2239. if(val1) //there is gold
  2240. {
  2241. iw.components.push_back(Component(2,6,val1,0));
  2242. cb->giveResource(h->tempOwner,6,val1);
  2243. }
  2244. if(type == 1) //art
  2245. {
  2246. //TODO: what if no space in backpack?
  2247. iw.components.push_back(Component(4, val2, 1, 0));
  2248. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2249. cb->giveHeroArtifact(val2,h->id,-2);
  2250. }
  2251. cb->showInfoDialog(&iw);
  2252. break;
  2253. }
  2254. case 86: //Shipwreck Survivor
  2255. {
  2256. //TODO: what if no space in backpack?
  2257. InfoWindow iw;
  2258. iw.soundID = soundBase::experience;
  2259. iw.player = h->tempOwner;
  2260. iw.components.push_back(Component(4,val1,1,0));
  2261. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2262. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2263. cb->giveHeroArtifact(val1,h->id,-2);
  2264. cb->showInfoDialog(&iw);
  2265. break;
  2266. }
  2267. case 101: //treasure chest
  2268. {
  2269. if (subID) //not OH3 treasure chest
  2270. {
  2271. tlog2 << "Not supported WoG treasure chest!\n";
  2272. return;
  2273. }
  2274. if(type) //there is an artifact
  2275. {
  2276. cb->giveHeroArtifact(val1,h->id,-2);
  2277. InfoWindow iw;
  2278. iw.soundID = soundBase::treasure;
  2279. iw.player = h->tempOwner;
  2280. iw.components.push_back(Component(4,val1,1,0));
  2281. iw.text << std::pair<ui8,ui32>(11,145);
  2282. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2283. cb->showInfoDialog(&iw);
  2284. break;
  2285. }
  2286. else
  2287. {
  2288. BlockingDialog sd(false,true);
  2289. sd.player = h->tempOwner;
  2290. sd.text << std::pair<ui8,ui32>(11,146);
  2291. sd.components.push_back(Component(2,6,val1,0));
  2292. sd.components.push_back(Component(5,0,val2,0));
  2293. sd.soundID = soundBase::chest;
  2294. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2295. cb->showBlockingDialog(&sd,fun);
  2296. return;
  2297. }
  2298. }
  2299. }
  2300. cb->removeObject(id);
  2301. }
  2302. void CGPickable::chosen( int which, int heroID ) const
  2303. {
  2304. switch(which)
  2305. {
  2306. case 1: //player pick gold
  2307. cb->giveResource(cb->getOwner(heroID),6,val1);
  2308. break;
  2309. case 2: //player pick exp
  2310. cb->changePrimSkill(heroID, 4, val2);
  2311. break;
  2312. default:
  2313. throw std::string("Unhandled treasure choice");
  2314. }
  2315. cb->removeObject(id);
  2316. }
  2317. void CGWitchHut::initObj()
  2318. {
  2319. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2320. }
  2321. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2322. {
  2323. InfoWindow iw;
  2324. iw.soundID = soundBase::gazebo;
  2325. iw.player = h->getOwner();
  2326. if(!hasVisited(h->tempOwner))
  2327. cb->setObjProperty(id,10,h->tempOwner);
  2328. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2329. {
  2330. iw.text << std::pair<ui8,ui32>(11,172);
  2331. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2332. }
  2333. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2334. {
  2335. iw.text << std::pair<ui8,ui32>(11,173);
  2336. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2337. }
  2338. else //give sec skill
  2339. {
  2340. iw.components.push_back(Component(1, ability, 1, 0));
  2341. iw.text << std::pair<ui8,ui32>(11,171);
  2342. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2343. cb->changeSecSkill(h->id,ability,1,true);
  2344. }
  2345. cb->showInfoDialog(&iw);
  2346. }
  2347. const std::string & CGWitchHut::getHoverText() const
  2348. {
  2349. hoverName = VLC->generaltexth->names[ID];
  2350. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2351. {
  2352. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2353. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2354. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2355. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2356. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2357. }
  2358. return hoverName;
  2359. }
  2360. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2361. {
  2362. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2363. int messageID, bonusType, bonusVal;
  2364. int bonusMove = 0, sound = -1;
  2365. InfoWindow iw;
  2366. iw.player = h->tempOwner;
  2367. GiveBonus gbonus;
  2368. gbonus.hid = h->id;
  2369. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2370. gbonus.bonus.source = HeroBonus::OBJECT;
  2371. gbonus.bonus.id = ID;
  2372. switch(ID)
  2373. {
  2374. case 11: //buoy
  2375. messageID = 21;
  2376. sound = soundBase::MORALE;
  2377. gbonus.bonus.type = HeroBonus::MORALE;
  2378. gbonus.bonus.val = +1;
  2379. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2380. break;
  2381. case 14: //swan pond
  2382. messageID = 29;
  2383. sound = soundBase::LUCK;
  2384. gbonus.bonus.type = HeroBonus::LUCK;
  2385. gbonus.bonus.val = 2;
  2386. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2387. bonusMove = -h->movement;
  2388. break;
  2389. case 28: //Faerie Ring
  2390. messageID = 49;
  2391. sound = soundBase::LUCK;
  2392. gbonus.bonus.type = HeroBonus::LUCK;
  2393. gbonus.bonus.val = 1;
  2394. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2395. break;
  2396. case 30: //fountain of fortune
  2397. messageID = 55;
  2398. sound = soundBase::LUCK;
  2399. gbonus.bonus.type = HeroBonus::LUCK;
  2400. gbonus.bonus.val = rand()%5 - 1;
  2401. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2402. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2403. break;
  2404. case 38: //idol of fortune
  2405. messageID = 62;
  2406. iw.soundID = soundBase::experience;
  2407. if(cb->getDate(1) == 7) //7th day of week
  2408. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2409. else
  2410. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2411. gbonus.bonus.val = 1;
  2412. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2413. break;
  2414. case 52: //Mermaid
  2415. messageID = 83;
  2416. sound = soundBase::LUCK;
  2417. gbonus.bonus.type = HeroBonus::LUCK;
  2418. gbonus.bonus.val = 1;
  2419. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2420. break;
  2421. case 64: //Rally Flag
  2422. sound = soundBase::MORALE;
  2423. messageID = 111;
  2424. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2425. gbonus.bonus.val = 1;
  2426. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2427. bonusMove = 400;
  2428. break;
  2429. case 56: //oasis
  2430. messageID = 95;
  2431. gbonus.bonus.type = HeroBonus::MORALE;
  2432. gbonus.bonus.val = 1;
  2433. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2434. bonusMove = 800;
  2435. break;
  2436. case 96: //temple
  2437. messageID = 140;
  2438. iw.soundID = soundBase::temple;
  2439. gbonus.bonus.type = HeroBonus::MORALE;
  2440. if(cb->getDate(1)==7) //sunday
  2441. {
  2442. gbonus.bonus.val = 2;
  2443. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2444. }
  2445. else
  2446. {
  2447. gbonus.bonus.val = 1;
  2448. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2449. }
  2450. break;
  2451. case 110://Watering Hole
  2452. sound = soundBase::MORALE;
  2453. messageID = 166;
  2454. gbonus.bonus.type = HeroBonus::MORALE;
  2455. gbonus.bonus.val = 1;
  2456. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2457. bonusMove = 400;
  2458. break;
  2459. case 31: //Fountain of Youth
  2460. sound = soundBase::MORALE;
  2461. messageID = 57;
  2462. gbonus.bonus.type = HeroBonus::MORALE;
  2463. gbonus.bonus.val = 1;
  2464. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2465. bonusMove = 400;
  2466. break;
  2467. case 94: //Stables TODO: upgrade Cavaliers
  2468. sound = soundBase::horse20;
  2469. std::set<ui32> slots;
  2470. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2471. {
  2472. if(i->second.first == 10)
  2473. slots.insert(i->first);
  2474. }
  2475. if (!slots.empty())
  2476. {
  2477. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2478. {
  2479. UpgradeCreature uc(*i, id, 11);
  2480. //uc.applyGh (&gh);
  2481. }
  2482. }
  2483. messageID = 137;
  2484. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  2485. gbonus.bonus.val = 600;
  2486. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  2487. break;
  2488. }
  2489. if(visited)
  2490. {
  2491. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  2492. messageID--;
  2493. else
  2494. messageID++;
  2495. }
  2496. else
  2497. {
  2498. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2499. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2500. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2501. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2502. cb->giveHeroBonus(&gbonus);
  2503. if(bonusMove) //swan pond - take all move points
  2504. {
  2505. SetMovePoints smp;
  2506. smp.hid = h->id;
  2507. smp.val = h->movement + bonusMove;
  2508. cb->setMovePoints(&smp);
  2509. }
  2510. }
  2511. iw.soundID = sound;
  2512. iw.text << std::pair<ui8,ui32>(11,messageID);
  2513. cb->showInfoDialog(&iw);
  2514. }
  2515. const std::string & CGBonusingObject::getHoverText() const
  2516. {
  2517. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2518. hoverName = VLC->generaltexth->names[ID];
  2519. if(h)
  2520. {
  2521. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2522. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2523. else
  2524. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2525. }
  2526. return hoverName;
  2527. }
  2528. void CGBonusingObject::initObj()
  2529. {
  2530. if(ID == 11 || ID == 52) //Buoy / Mermaid
  2531. {
  2532. blockVisit = true;
  2533. }
  2534. }
  2535. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2536. {
  2537. int message;
  2538. InfoWindow iw;
  2539. iw.soundID = soundBase::faerie;
  2540. iw.player = h->tempOwner;
  2541. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2542. {
  2543. message = 78;
  2544. }
  2545. else if(h->mana < h->manaLimit())
  2546. {
  2547. giveDummyBonus(h->id);
  2548. cb->setManaPoints(h->id,h->manaLimit());
  2549. message = 77;
  2550. }
  2551. else
  2552. {
  2553. message = 79;
  2554. }
  2555. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2556. cb->showInfoDialog(&iw);
  2557. }
  2558. const std::string & CGMagicWell::getHoverText() const
  2559. {
  2560. getNameVis(hoverName);
  2561. return hoverName;
  2562. }
  2563. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2564. {
  2565. if(!(availableFor & (1 << h->tempOwner)))
  2566. return;
  2567. if(cb->getPlayerSettings(h->tempOwner)->human)
  2568. {
  2569. if(humanActivate)
  2570. activated(h);
  2571. }
  2572. else if(computerActivate)
  2573. activated(h);
  2574. }
  2575. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2576. {
  2577. if(result->winner)
  2578. return;
  2579. giveContents(h,true);
  2580. }
  2581. void CGEvent::activated( const CGHeroInstance * h ) const
  2582. {
  2583. if(army)
  2584. {
  2585. InfoWindow iw;
  2586. iw.player = h->tempOwner;
  2587. if(message.size())
  2588. iw.text << message;
  2589. else
  2590. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2591. cb->showInfoDialog(&iw);
  2592. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  2593. }
  2594. else
  2595. {
  2596. giveContents(h,false);
  2597. }
  2598. }
  2599. void CGPandoraBox::initObj()
  2600. {
  2601. blockVisit = true;
  2602. }
  2603. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  2604. {
  2605. BlockingDialog bd (true, false);
  2606. bd.player = h->getOwner();
  2607. bd.soundID = soundBase::QUEST;
  2608. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  2609. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  2610. }
  2611. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  2612. {
  2613. if (accept)
  2614. {
  2615. if (army)
  2616. {
  2617. InfoWindow iw;
  2618. iw.player = h->tempOwner;
  2619. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2620. cb->showInfoDialog(&iw);
  2621. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h,_1));
  2622. }
  2623. else if (message.size() == resources.size() ==
  2624. primskills.size() == abilities.size() ==
  2625. abilityLevels.size() == artifacts.size() ==
  2626. spells.size() == creatures ==
  2627. gainedExp == manaDiff == moraleDiff == luckDiff == 0) //yeaha!
  2628. {
  2629. InfoWindow iw;
  2630. iw.player = h->tempOwner;
  2631. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  2632. cb->showInfoDialog(&iw);
  2633. }
  2634. else
  2635. {
  2636. giveContents (h, false);
  2637. }
  2638. }
  2639. }
  2640. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2641. {
  2642. InfoWindow iw;
  2643. iw.player = h->getOwner();
  2644. bool changesPrimSkill = false;
  2645. for (int i = 0; i < primskills.size(); i++)
  2646. {
  2647. if(primskills[i])
  2648. {
  2649. changesPrimSkill = true;
  2650. break;
  2651. }
  2652. }
  2653. if(gainedExp || changesPrimSkill || abilities.size())
  2654. {
  2655. getText(iw,afterBattle,175,h);
  2656. if(gainedExp)
  2657. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2658. for(int i=0; i<primskills.size(); i++)
  2659. if(primskills[i])
  2660. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2661. for(int i=0; i<abilities.size(); i++)
  2662. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2663. cb->showInfoDialog(&iw);
  2664. //give exp
  2665. if(gainedExp)
  2666. cb->changePrimSkill(h->id,5,gainedExp,false);
  2667. //give prim skills
  2668. for(int i=0; i<primskills.size(); i++)
  2669. if(primskills[i])
  2670. cb->changePrimSkill(h->id,i,primskills[i],false);
  2671. //give sec skills
  2672. for(int i=0; i<abilities.size(); i++)
  2673. {
  2674. int curLev = h->getSecSkillLevel(abilities[i]);
  2675. if(curLev && curLev < abilityLevels[i]
  2676. || h->secSkills.size() < SKILL_PER_HERO )
  2677. {
  2678. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2679. }
  2680. }
  2681. }
  2682. if(spells.size())
  2683. {
  2684. std::set<ui32> spellsToGive;
  2685. iw.components.clear();
  2686. for(int i=0; i<spells.size(); i++)
  2687. {
  2688. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2689. spellsToGive.insert(spells[i]);
  2690. }
  2691. if(spellsToGive.size())
  2692. {
  2693. cb->changeSpells(h->id,true,spellsToGive);
  2694. cb->showInfoDialog(&iw);
  2695. }
  2696. }
  2697. if(manaDiff)
  2698. {
  2699. getText(iw,afterBattle,manaDiff,176,177,h);
  2700. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2701. cb->showInfoDialog(&iw);
  2702. cb->setManaPoints(h->id, h->mana + manaDiff);
  2703. }
  2704. if(moraleDiff)
  2705. {
  2706. getText(iw,afterBattle,moraleDiff,178,179,h);
  2707. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2708. cb->showInfoDialog(&iw);
  2709. GiveBonus gb;
  2710. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2711. gb.hid = h->id;
  2712. cb->giveHeroBonus(&gb);
  2713. }
  2714. if(luckDiff)
  2715. {
  2716. getText(iw,afterBattle,luckDiff,180,181,h);
  2717. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2718. cb->showInfoDialog(&iw);
  2719. GiveBonus gb;
  2720. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2721. gb.hid = h->id;
  2722. cb->giveHeroBonus(&gb);
  2723. }
  2724. iw.components.clear();
  2725. iw.text.clear();
  2726. for(int i=0; i<resources.size(); i++)
  2727. {
  2728. if(resources[i] < 0)
  2729. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2730. }
  2731. if(iw.components.size())
  2732. {
  2733. getText(iw,afterBattle,182,h);
  2734. cb->showInfoDialog(&iw);
  2735. }
  2736. iw.components.clear();
  2737. iw.text.clear();
  2738. for(int i=0; i<resources.size(); i++)
  2739. {
  2740. if(resources[i] > 0)
  2741. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2742. }
  2743. if(iw.components.size())
  2744. {
  2745. getText(iw,afterBattle,183,h);
  2746. cb->showInfoDialog(&iw);
  2747. }
  2748. iw.components.clear();
  2749. getText(iw,afterBattle,183,h);
  2750. for(int i=0; i<artifacts.size(); i++)
  2751. {
  2752. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2753. if(iw.components.size() >= 14)
  2754. {
  2755. cb->showInfoDialog(&iw);
  2756. iw.components.clear();
  2757. }
  2758. }
  2759. if(iw.components.size())
  2760. {
  2761. cb->showInfoDialog(&iw);
  2762. }
  2763. for(int i=0; i<resources.size(); i++)
  2764. if(resources[i])
  2765. cb->giveResource(h->getOwner(),i,resources[i]);
  2766. for(int i=0; i<artifacts.size(); i++)
  2767. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2768. //show dialog with given creatures
  2769. iw.components.clear();
  2770. iw.text.clear();
  2771. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2772. {
  2773. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2774. }
  2775. if(iw.components.size())
  2776. {
  2777. if(afterBattle)
  2778. {
  2779. if(iw.components.front().val == 1)
  2780. {
  2781. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2782. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2783. }
  2784. else
  2785. {
  2786. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2787. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2788. }
  2789. iw.text.addReplacement(h->name);
  2790. }
  2791. else
  2792. {
  2793. iw.text << message;
  2794. afterBattle = true;
  2795. }
  2796. cb->showInfoDialog(&iw);
  2797. }
  2798. //check if creatures can be moved to hero army
  2799. CCreatureSet heroArmy = h->army;
  2800. CCreatureSet ourArmy = creatures;
  2801. while(ourArmy)
  2802. {
  2803. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2804. if(slot < 0)
  2805. break;
  2806. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2807. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2808. ourArmy.slots.erase(ourArmy.slots.begin());
  2809. }
  2810. if(!ourArmy) //all creatures can be moved to hero army - do that
  2811. {
  2812. SetGarrisons sg;
  2813. sg.garrs[h->id] = heroArmy;
  2814. cb->sendAndApply(&sg);
  2815. }
  2816. else //show garrison window and let player pick creatures
  2817. {
  2818. SetGarrisons sg;
  2819. sg.garrs[id] = creatures;
  2820. cb->sendAndApply(&sg);
  2821. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2822. }
  2823. if(!afterBattle && message.size())
  2824. {
  2825. iw.text << message;
  2826. cb->showInfoDialog(&iw);
  2827. }
  2828. cb->removeObject(id);
  2829. }
  2830. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2831. {
  2832. if(afterBattle || !message.size())
  2833. {
  2834. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2835. iw.text.addReplacement(h->name);
  2836. }
  2837. else
  2838. {
  2839. iw.text << message;
  2840. afterBattle = true;
  2841. }
  2842. }
  2843. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  2844. {
  2845. iw.components.clear();
  2846. iw.text.clear();
  2847. if(afterBattle || !message.size())
  2848. {
  2849. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2850. iw.text.addReplacement(h->name);
  2851. }
  2852. else
  2853. {
  2854. iw.text << message;
  2855. afterBattle = true;
  2856. }
  2857. }
  2858. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2859. {
  2860. InfoWindow iw;
  2861. iw.soundID = soundBase::LIGHTHOUSE;
  2862. iw.player = h->tempOwner;
  2863. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2864. cb->showInfoDialog(&iw);
  2865. FoWChange fw;
  2866. fw.player = h->tempOwner;
  2867. fw.mode = 1;
  2868. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2869. cb->sendAndApply(&fw);
  2870. }
  2871. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2872. {
  2873. if(spell == 255)
  2874. {
  2875. tlog1 << "Not initialized shrine visited!\n";
  2876. return;
  2877. }
  2878. if(!hasVisited(h->tempOwner))
  2879. cb->setObjProperty(id,10,h->tempOwner);
  2880. InfoWindow iw;
  2881. iw.soundID = soundBase::temple;
  2882. iw.player = h->getOwner();
  2883. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2884. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2885. iw.text << ".";
  2886. if(!h->getArt(17)) //no spellbook
  2887. {
  2888. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2889. }
  2890. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2891. {
  2892. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2893. }
  2894. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2895. {
  2896. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2897. }
  2898. else //give spell
  2899. {
  2900. std::set<ui32> spells;
  2901. spells.insert(spell);
  2902. cb->changeSpells(h->id,true,spells);
  2903. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2904. }
  2905. cb->showInfoDialog(&iw);
  2906. }
  2907. void CGShrine::initObj()
  2908. {
  2909. if(spell == 255) //spell not set
  2910. {
  2911. int level = ID-87;
  2912. std::vector<ui32> possibilities;
  2913. //add all allowed spells of wanted level
  2914. for(int i=0; i<SPELLS_QUANTITY; i++)
  2915. {
  2916. if(VLC->spellh->spells[i].level == level
  2917. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2918. {
  2919. possibilities.push_back(VLC->spellh->spells[i].id);
  2920. }
  2921. }
  2922. if(!possibilities.size())
  2923. {
  2924. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2925. return;
  2926. }
  2927. spell = possibilities[ran() % possibilities.size()];
  2928. }
  2929. }
  2930. const std::string & CGShrine::getHoverText() const
  2931. {
  2932. hoverName = VLC->generaltexth->names[ID];
  2933. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2934. {
  2935. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2936. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2937. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2938. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2939. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2940. }
  2941. return hoverName;
  2942. }
  2943. void CGSignBottle::initObj()
  2944. {
  2945. //if no text is set than we pick random from the predefined ones
  2946. if(!message.size())
  2947. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2948. if(ID == 59)
  2949. {
  2950. blockVisit = true;
  2951. }
  2952. }
  2953. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2954. {
  2955. InfoWindow iw;
  2956. iw.soundID = soundBase::STORE;
  2957. iw.player = h->getOwner();
  2958. iw.text << message;
  2959. cb->showInfoDialog(&iw);
  2960. if(ID == 59)
  2961. cb->removeObject(id);
  2962. }
  2963. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2964. {
  2965. }
  2966. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2967. {
  2968. int type = bonusType;
  2969. int bid = bonusID;
  2970. //check if the bonus if applicable, if not - give primary skill (always possible)
  2971. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2972. if((type == 1
  2973. && ((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)
  2974. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2975. {
  2976. type = 0;
  2977. bid = ran() % PRIMARY_SKILLS;
  2978. }
  2979. InfoWindow iw;
  2980. iw.soundID = soundBase::gazebo;
  2981. iw.player = h->getOwner();
  2982. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2983. switch (type)
  2984. {
  2985. case 0:
  2986. cb->changePrimSkill(h->id,bid,+1);
  2987. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  2988. break;
  2989. case 1:
  2990. cb->changeSecSkill(h->id,bid,+1);
  2991. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2992. break;
  2993. case 2:
  2994. {
  2995. std::set<ui32> hlp;
  2996. hlp.insert(bid);
  2997. cb->changeSpells(h->id,true,hlp);
  2998. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2999. }
  3000. break;
  3001. default:
  3002. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3003. return;
  3004. }
  3005. cb->showInfoDialog(&iw);
  3006. cb->removeObject(id);
  3007. }
  3008. void CGScholar::initObj()
  3009. {
  3010. blockVisit = true;
  3011. if(bonusType == 255)
  3012. {
  3013. bonusType = ran()%3;
  3014. switch(bonusType)
  3015. {
  3016. case 0:
  3017. bonusID = ran() % PRIMARY_SKILLS;
  3018. break;
  3019. case 1:
  3020. bonusID = ran() % SKILL_QUANTITY;
  3021. break;
  3022. case 2:
  3023. bonusID = ran() % SPELLS_QUANTITY;
  3024. break;
  3025. }
  3026. }
  3027. }
  3028. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3029. {
  3030. int sound = 0;
  3031. int txtid = -1;
  3032. switch(ID)
  3033. {
  3034. case 22: //Corpse
  3035. txtid = 37;
  3036. sound = soundBase::MYSTERY;
  3037. break;
  3038. case 39: //Lean To
  3039. sound = soundBase::GENIE;
  3040. txtid = 64;
  3041. break;
  3042. case 105://Wagon
  3043. sound = soundBase::GENIE;
  3044. txtid = 154;
  3045. break;
  3046. case 108:
  3047. break;
  3048. default:
  3049. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3050. return;
  3051. }
  3052. if(ID == 108)//Warrior's Tomb
  3053. {
  3054. //ask if player wants to search the Tomb
  3055. BlockingDialog bd(true, false);
  3056. sound = soundBase::GRAVEYARD;
  3057. bd.player = h->getOwner();
  3058. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3059. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3060. return;
  3061. }
  3062. InfoWindow iw;
  3063. iw.soundID = sound;
  3064. iw.player = h->getOwner();
  3065. if(players.size()) //we have been already visited...
  3066. {
  3067. txtid++;
  3068. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  3069. txtid++;
  3070. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3071. }
  3072. else //first visit - give bonus!
  3073. {
  3074. switch(artOrRes)
  3075. {
  3076. case 0:
  3077. txtid++;
  3078. break;
  3079. case 1: //art
  3080. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3081. cb->giveHeroArtifact(bonusType,h->id,-2);
  3082. break;
  3083. case 2: //res
  3084. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  3085. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  3086. break;
  3087. }
  3088. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3089. if(ID == 105 && artOrRes == 1)
  3090. {
  3091. iw.text.localStrings.back().second++;
  3092. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3093. }
  3094. }
  3095. cb->showInfoDialog(&iw);
  3096. cb->setObjProperty(id,10,h->getOwner());
  3097. }
  3098. const std::string & CGOnceVisitable::getHoverText() const
  3099. {
  3100. hoverName = VLC->generaltexth->names[ID] + " ";
  3101. hoverName += (hasVisited(cb->getCurrentPlayer())
  3102. ? (VLC->generaltexth->allTexts[352]) //visited
  3103. : ( VLC->generaltexth->allTexts[353])); //not visited
  3104. return hoverName;
  3105. }
  3106. void CGOnceVisitable::initObj()
  3107. {
  3108. switch(ID)
  3109. {
  3110. case 22: //Corpse
  3111. {
  3112. blockVisit = true;
  3113. int hlp = ran()%100;
  3114. if(hlp < 20)
  3115. {
  3116. artOrRes = 1;
  3117. std::vector<CArtifact*> arts;
  3118. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3119. bonusType = arts[ran() % arts.size()]->id;
  3120. }
  3121. else
  3122. {
  3123. artOrRes = 0;
  3124. }
  3125. }
  3126. break;
  3127. case 39: //Lean To
  3128. {
  3129. artOrRes = 2;
  3130. bonusType = ran()%6; //any basic resource without gold
  3131. bonusVal = ran()%4 + 1;
  3132. break;
  3133. }
  3134. case 108://Warrior's Tomb
  3135. {
  3136. artOrRes = 1;
  3137. std::vector<CArtifact*> arts;
  3138. int hlp = ran()%100;
  3139. if(hlp < 30)
  3140. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  3141. else if(hlp < 80)
  3142. cb->getAllowed(arts,CArtifact::ART_MINOR);
  3143. else if(hlp < 95)
  3144. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  3145. else
  3146. cb->getAllowed(arts,CArtifact::ART_RELIC);
  3147. bonusType = arts[ran() % arts.size()]->id;
  3148. }
  3149. break;
  3150. case 105://Wagon
  3151. {
  3152. int hlp = ran()%100;
  3153. if(hlp < 10)
  3154. {
  3155. artOrRes = 0; // nothing... :(
  3156. }
  3157. else if(hlp < 50) //minor or treasure art
  3158. {
  3159. artOrRes = 1;
  3160. std::vector<CArtifact*> arts;
  3161. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3162. bonusType = arts[ran() % arts.size()]->id;
  3163. }
  3164. else //2 - 5 of non-gold resource
  3165. {
  3166. artOrRes = 2;
  3167. bonusType = ran()%6;
  3168. bonusVal = ran()%4 + 2;
  3169. }
  3170. break;
  3171. }
  3172. }
  3173. }
  3174. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3175. {
  3176. if(accept)
  3177. {
  3178. InfoWindow iw;
  3179. iw.player = h->getOwner();
  3180. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3181. if(players.size()) //we've been already visited, player found nothing
  3182. {
  3183. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3184. }
  3185. else //first visit - give artifact
  3186. {
  3187. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3188. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3189. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3190. cb->giveHeroArtifact(bonusType,h->id,-2);
  3191. }
  3192. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3193. {
  3194. //ruin morale
  3195. GiveBonus gb;
  3196. gb.hid = h->id;
  3197. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3198. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3199. cb->giveHeroBonus(&gb);
  3200. }
  3201. }
  3202. }
  3203. void CBank::initObj()
  3204. {
  3205. index = 0;
  3206. switch (ID) //find apriopriate key
  3207. {
  3208. case 16: //bank
  3209. index = subID; break;
  3210. case 24: //derelict ship
  3211. index = 8;
  3212. case 25: //utopia
  3213. index = 10; break;
  3214. case 84: //crypt
  3215. index = 9; break;
  3216. case 85: //shipwreck
  3217. index = 7; break;
  3218. }
  3219. bc = NULL;
  3220. daycounter = 0;
  3221. multiplier = 1;
  3222. }
  3223. void CBank::reset()
  3224. {
  3225. int val1 = ran()%100;
  3226. int chance = 0;
  3227. for (ui8 i = 1; i <= VLC->objh->banksInfo[index].size(); i++)
  3228. {
  3229. if (val1 < (chance += VLC->objh->banksInfo[index][i].chance))
  3230. cb->setObjProperty (id, 13, i);
  3231. }
  3232. artifacts.clear();
  3233. for (ui8 i = 1; i <= 4; i++)
  3234. {
  3235. for (ui8 j = 1; j <= bc->artifacts[i]; j++)
  3236. cb->setObjProperty (id, 18, i);
  3237. }
  3238. }
  3239. void CBank::setPropertyDer (ui8 what, ui32 val)
  3240. {
  3241. switch (what)
  3242. {
  3243. case 11: //daycounter
  3244. daycounter++;
  3245. break;
  3246. case 12: //multiplier
  3247. multiplier *= ((float)val)/100;
  3248. break;
  3249. case 13: //bank preset
  3250. bc = &VLC->objh->banksInfo[index][val];
  3251. break;
  3252. case 18: //Artifacts
  3253. {
  3254. std::vector<CArtifact*> arts;
  3255. switch (val)
  3256. {
  3257. case 1:
  3258. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  3259. break;
  3260. case 2:
  3261. cb->getAllowed (arts, CArtifact::ART_MINOR);
  3262. break;
  3263. case 3:
  3264. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  3265. break;
  3266. case 4:
  3267. cb->getAllowed (arts, CArtifact::ART_RELIC);
  3268. break;
  3269. }
  3270. artifacts.push_back (arts[ran() % arts.size()]->id);
  3271. }
  3272. break;
  3273. }
  3274. }
  3275. void CBank::newTurn()
  3276. {
  3277. if (bc == NULL)
  3278. {
  3279. if (daycounter >= 28 || cb->getDate(0) == 1)
  3280. {
  3281. reset();
  3282. daycounter = 0;
  3283. if (ID == 24 && cb->getDate(0) > 1)
  3284. {
  3285. artifacts.clear(); //derelict ships are usable only once
  3286. }
  3287. }
  3288. else
  3289. daycounter++;
  3290. }
  3291. }
  3292. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3293. {
  3294. if (bc != NULL)
  3295. {
  3296. int banktext = 0;
  3297. switch (ID)
  3298. {
  3299. case 16: //generic bank
  3300. banktext = 32;
  3301. break;
  3302. case 24:
  3303. banktext = 41;
  3304. break;
  3305. case 25: //utopia
  3306. banktext = 47;
  3307. break;
  3308. case 84: //crypt
  3309. banktext = 119;
  3310. break;
  3311. case 85: //shipwreck
  3312. banktext = 122;
  3313. break;
  3314. }
  3315. BlockingDialog bd (true, false);
  3316. bd.player = h->getOwner();
  3317. bd.soundID = soundBase::DANGER;
  3318. std::string desc = VLC->generaltexth->allTexts[banktext];
  3319. boost::algorithm::replace_first (desc, "%s", VLC->generaltexth->names[ID]);
  3320. bd.text << desc;
  3321. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3322. }
  3323. else
  3324. {
  3325. InfoWindow iw;
  3326. if (ID == 85)
  3327. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3328. iw.soundID = soundBase::GRAVEYARD;
  3329. iw.player = h->getOwner();
  3330. iw.text.addTxt (MetaString::ADVOB_TXT, 33);
  3331. cb->showInfoDialog(&iw);
  3332. }
  3333. }
  3334. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3335. {
  3336. if (accept)
  3337. {
  3338. int upgraded = 0;
  3339. if (ran()%100 < bc->upgradeChance) upgraded = 1;
  3340. CCreatureSet ourArmy;
  3341. switch (bc->guards.size())
  3342. {
  3343. case 1:
  3344. for (int i = 1; i <= 5; i++)
  3345. ourArmy.setCreature (i, bc->guards[1].first + upgraded, bc->guards[1].second / 5 );
  3346. break;
  3347. case 4:
  3348. {
  3349. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3350. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3351. ourArmy.setCreature (ourArmy.slots.size() + 1, it->first, it->second );
  3352. }
  3353. break;
  3354. default:
  3355. tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3356. return;
  3357. }
  3358. //TODO: start combat
  3359. }
  3360. }
  3361. void CBank::endBattle (const BattleResult *result)
  3362. {
  3363. if (result->winner == 0)
  3364. {
  3365. int textID = -1;
  3366. InfoWindow iw;
  3367. switch (ID)
  3368. {
  3369. case 16: //generic bank
  3370. textID = 34;
  3371. break;
  3372. case 24: //derelict ship
  3373. if (bc->resources.size() != 0)
  3374. textID = 43;
  3375. else
  3376. {
  3377. textID = 42;
  3378. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3379. }
  3380. break;
  3381. case 25: //utopia
  3382. textID = 47;
  3383. break;
  3384. case 84: //crypt
  3385. textID = 121;
  3386. break;
  3387. case 85: //shipwreck
  3388. if (bc->resources.size() != 0)
  3389. textID = 124;
  3390. else
  3391. {
  3392. textID = 123;
  3393. }
  3394. break;
  3395. }
  3396. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  3397. iw.player = cb->getCurrentPlayer();
  3398. for (std::map<ui8, si32>::iterator it = bc->resources.begin(); it != bc->resources.end(); it++)
  3399. {
  3400. iw.components.push_back (Component (Component::RESOURCE, it->first, it->second, 0));
  3401. cb->giveResource (cb->getCurrentPlayer(), it->first, it->second);
  3402. }
  3403. for (std::vector<si32>::iterator it = artifacts.begin(); it != artifacts.end(); it++)
  3404. {
  3405. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3406. iw.text.addReplacement (MetaString::ART_NAMES, *it);
  3407. cb->giveHeroArtifact (*it, cb->getSelectedHero() ,-2);
  3408. }
  3409. CCreatureSet ourArmy;
  3410. for (std::vector< std::pair <ui16, ui32> >::iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  3411. {
  3412. int slot = ourArmy.getSlotFor (it->second);
  3413. ourArmy.slots[slot] = *it; //assuming we're not going to add multiple stacks of same creature
  3414. }
  3415. cb->giveCreatures (id, cb->getHero (cb->getSelectedHero()), &ourArmy);
  3416. bc = NULL;
  3417. }
  3418. else
  3419. reset();
  3420. }
  3421. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  3422. {
  3423. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  3424. playerKeyMap.find(what-101)->second.insert(val);
  3425. }
  3426. bool CGKeys::wasMyColorVisited (int player) const
  3427. {
  3428. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  3429. return true;
  3430. else
  3431. return false;
  3432. }
  3433. const std::string & CGKeymasterTent::getHoverText() const
  3434. {
  3435. hoverName = VLC->generaltexth->names[ID];
  3436. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3437. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3438. else
  3439. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3440. return hoverName;
  3441. }
  3442. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  3443. {
  3444. InfoWindow iw;
  3445. iw.soundID = soundBase::CAVEHEAD;
  3446. iw.player = h->getOwner();
  3447. if (!wasMyColorVisited (h->getOwner()) )
  3448. {
  3449. cb->setObjProperty(id, h->tempOwner+101, subID);
  3450. iw.text << std::pair<ui8,ui32>(11,19);
  3451. }
  3452. else
  3453. iw.text << std::pair<ui8,ui32>(11,20);
  3454. cb->showInfoDialog(&iw);
  3455. }
  3456. void CGBorderGuard::initObj()
  3457. {
  3458. blockVisit = true;
  3459. }
  3460. const std::string & CGBorderGuard::getHoverText() const
  3461. {
  3462. hoverName = VLC->generaltexth->names[ID];
  3463. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3464. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3465. else
  3466. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3467. return hoverName;
  3468. }
  3469. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  3470. {
  3471. if (wasMyColorVisited (h->getOwner()) )
  3472. {
  3473. BlockingDialog bd (true, false);
  3474. bd.player = h->getOwner();
  3475. bd.soundID = soundBase::QUEST;
  3476. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  3477. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  3478. }
  3479. else
  3480. {
  3481. InfoWindow iw;
  3482. iw.player = h->getOwner();
  3483. iw.soundID = soundBase::CAVEHEAD;
  3484. iw.text << std::pair<ui8,ui32>(11,18);
  3485. cb->showInfoDialog (&iw);
  3486. }
  3487. }
  3488. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  3489. {
  3490. if (accept)
  3491. cb->removeObject(id);
  3492. }
  3493. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  3494. {
  3495. InfoWindow iw;
  3496. iw.player = h->getOwner();
  3497. if (!wasMyColorVisited (h->getOwner()) )
  3498. {
  3499. iw.text << std::pair<ui8,ui32>(11,18);
  3500. cb->showInfoDialog(&iw);
  3501. }
  3502. }
  3503. void CGMagi::initObj()
  3504. {
  3505. if (ID == 27)
  3506. {
  3507. blockVisit = true;
  3508. eyelist[subID].push_back(id);
  3509. }
  3510. }
  3511. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  3512. {
  3513. if (ID == 37)
  3514. {
  3515. InfoWindow iw;
  3516. CenterView cv;
  3517. FoWChange fw;
  3518. cv.player = iw.player = fw.player = h->tempOwner;
  3519. iw.soundID = soundBase::LIGHTHOUSE;
  3520. iw.player = h->tempOwner;
  3521. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  3522. cb->showInfoDialog(&iw);
  3523. fw.mode = 1;
  3524. std::vector<si32>::iterator it;
  3525. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  3526. {
  3527. const CGObjectInstance *eye = cb->getObj(*it);
  3528. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  3529. cb->sendAndApply(&fw);
  3530. cv.pos = eye->pos;
  3531. cv.focusTime = 2000;
  3532. cb->sendAndApply(&cv);
  3533. }
  3534. cv.pos = h->getPosition(false);
  3535. cb->sendAndApply(&cv);
  3536. }
  3537. else if (ID == 27)
  3538. {
  3539. InfoWindow iw;
  3540. iw.player = h->tempOwner;
  3541. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  3542. cb->showInfoDialog(&iw);
  3543. }
  3544. }
  3545. void CGBoat::initObj()
  3546. {
  3547. hero = NULL;
  3548. }
  3549. void CGSirens::initObj()
  3550. {
  3551. blockVisit = true;
  3552. }
  3553. const std::string & CGSirens::getHoverText() const
  3554. {
  3555. getNameVis(hoverName);
  3556. return hoverName;
  3557. }
  3558. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  3559. {
  3560. int message;
  3561. InfoWindow iw;
  3562. iw.soundID = soundBase::DANGER;
  3563. iw.player = h->tempOwner;
  3564. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  3565. {
  3566. iw.text.addTxt(11,133);
  3567. }
  3568. else
  3569. {
  3570. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  3571. int xp = 0;
  3572. SetGarrisons sg;
  3573. sg.garrs[h->id] = h->army;
  3574. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  3575. {
  3576. int drown = (int)(i->second.second * 0.3);
  3577. if(drown)
  3578. {
  3579. sg.garrs[h->id].slots[i->first].second -= drown;
  3580. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  3581. }
  3582. }
  3583. if(xp)
  3584. {
  3585. iw.text.addTxt(11,132);
  3586. iw.text.addReplacement(xp);
  3587. cb->sendAndApply(&sg);
  3588. }
  3589. else
  3590. {
  3591. iw.text.addTxt(11,134);
  3592. }
  3593. }
  3594. cb->showInfoDialog(&iw);
  3595. }
  3596. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  3597. {
  3598. cost.resize(RESOURCE_QUANTITY);
  3599. cost[0] = 10;
  3600. cost[6] = 1000;
  3601. }
  3602. //bool IShipyard::validLocation() const
  3603. //{
  3604. // std::vector<int3> offsets;
  3605. // getOutOffsets(offsets);
  3606. //
  3607. // TerrainTile *tile;
  3608. // for(int i = 0; i < offsets.size(); i++)
  3609. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3610. // return true;
  3611. // return false;
  3612. //}
  3613. int3 IShipyard::bestLocation() const
  3614. {
  3615. std::vector<int3> offsets;
  3616. getOutOffsets(offsets);
  3617. TerrainTile *tile;
  3618. for(int i = 0; i < offsets.size(); i++)
  3619. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3620. return o->pos + offsets[i];
  3621. return int3(-1,-1,-1);
  3622. }
  3623. IShipyard::IShipyard(const CGObjectInstance *O)
  3624. : o(O)
  3625. {
  3626. }
  3627. int IShipyard::state() const
  3628. {
  3629. int3 tile = bestLocation();
  3630. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  3631. if(!t)
  3632. return 3; //no water
  3633. else if(!t->blockingObjects.size())
  3634. return 0; //OK
  3635. else if(t->blockingObjects.front()->ID == 8)
  3636. return 1; //blocked with boat
  3637. else
  3638. return 2; //blocked
  3639. }
  3640. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  3641. {
  3642. if(obj->ID == TOWNI_TYPE)
  3643. {
  3644. return static_cast<CGTownInstance*>(obj);
  3645. }
  3646. else if(obj->ID == 87)
  3647. {
  3648. return static_cast<CGShipyard*>(obj);
  3649. }
  3650. else
  3651. {
  3652. tlog1 << "Cannot cast to IShipyar object with ID " << obj->ID << std::endl;
  3653. return NULL;
  3654. }
  3655. }
  3656. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  3657. {
  3658. return castFrom(const_cast<CGObjectInstance*>(obj));
  3659. }
  3660. CGShipyard::CGShipyard()
  3661. :IShipyard(this)
  3662. {
  3663. }
  3664. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  3665. {
  3666. 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),
  3667. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  3668. }
  3669. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  3670. {
  3671. if(tempOwner != h->tempOwner)
  3672. cb->setOwner(id, h->tempOwner);
  3673. int s = state();
  3674. if(s)
  3675. {
  3676. InfoWindow iw;
  3677. iw.player = tempOwner;
  3678. switch(s)
  3679. {
  3680. case 1:
  3681. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  3682. break;
  3683. case 2:
  3684. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  3685. break;
  3686. case 3:
  3687. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  3688. return;
  3689. }
  3690. cb->showInfoDialog(&iw);
  3691. }
  3692. else
  3693. {
  3694. OpenWindow ow;
  3695. ow.id1 = id;
  3696. ow.id2 = h->id;
  3697. ow.window = OpenWindow::SHIPYARD_WINDOW;
  3698. cb->sendAndApply(&ow);
  3699. }
  3700. }