CObjectHandler.cpp 120 KB

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