CObjectHandler.cpp 126 KB

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