CObjectHandler.cpp 124 KB

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