CObjectHandler.cpp 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908
  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. void CGHeroInstance::recreateArtBonuses()
  1076. {
  1077. //clear all bonuses from artifacts (if present) and give them again
  1078. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  1079. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  1080. {
  1081. CArtifact &art = VLC->arth->artifacts[ari->second];
  1082. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  1083. bonuses.push_back(*i);
  1084. }
  1085. }
  1086. void CGDwelling::initObj()
  1087. {
  1088. switch(ID)
  1089. {
  1090. case 17:
  1091. {
  1092. int crid = VLC->objh->cregens[subID];
  1093. CCreature *crs = &VLC->creh->creatures[crid];
  1094. creatures.resize(1);
  1095. creatures[0].second.push_back(crid);
  1096. hoverName = VLC->generaltexth->creGens[subID];
  1097. if(crs->level > 4)
  1098. {
  1099. army.slots[0].first = crs->idNumber;
  1100. army.slots[0].second = (8 - crs->level) * 3;
  1101. }
  1102. if (getOwner() != 255)
  1103. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1104. }
  1105. break;
  1106. case 20:
  1107. creatures.resize(4);
  1108. if(subID == 1) //Golem Factory
  1109. {
  1110. creatures[0].second.push_back(32); //Stone Golem
  1111. creatures[1].second.push_back(33); //Iron Golem
  1112. creatures[2].second.push_back(116); //Gold Golem
  1113. creatures[3].second.push_back(117); //Diamond Golem
  1114. //guards
  1115. army.slots[0].first = 116;
  1116. army.slots[0].second = 9;
  1117. army.slots[1].first = 117;
  1118. army.slots[1].second = 6;
  1119. }
  1120. else if(subID == 0) // Elemental Conflux
  1121. {
  1122. creatures[0].second.push_back(112); //Air Elemental
  1123. creatures[1].second.push_back(114); //Fire Elemental
  1124. creatures[2].second.push_back(113); //Earth Elemental
  1125. creatures[3].second.push_back(115); //Water Elemental
  1126. //guards
  1127. army.slots[0].first = 113;
  1128. army.slots[0].second = 12;
  1129. }
  1130. else
  1131. {
  1132. assert(0);
  1133. }
  1134. hoverName = VLC->generaltexth->creGens4[subID];
  1135. break;
  1136. default:
  1137. assert(0);
  1138. break;
  1139. }
  1140. }
  1141. void CGDwelling::setProperty(ui8 what, ui32 val)
  1142. {
  1143. switch (what)
  1144. {
  1145. case 1: //change owner
  1146. if (ID == 17) //single generators
  1147. {
  1148. if (tempOwner != NEUTRAL_PLAYER)
  1149. {
  1150. std::vector<CGDwelling *>* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1151. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1152. }
  1153. if (val != NEUTRAL_PLAYER) //can new owner be neutral?
  1154. cb->gameState()->players[val].dwellings.push_back (this);
  1155. }
  1156. break;
  1157. }
  1158. CGObjectInstance::setProperty(what,val);
  1159. }
  1160. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1161. {
  1162. if(h->tempOwner != tempOwner && army.slots.size() > 0) //object is guarded
  1163. {
  1164. BlockingDialog bd;
  1165. bd.player = h->tempOwner;
  1166. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1167. 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?
  1168. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1169. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  1170. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  1171. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1172. return;
  1173. }
  1174. if(h->tempOwner != tempOwner)
  1175. {
  1176. cb->setOwner(id, h->tempOwner);
  1177. }
  1178. BlockingDialog bd;
  1179. bd.player = h->tempOwner;
  1180. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1181. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  1182. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1183. for(size_t i = 0; i < creatures.size(); i++)
  1184. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1185. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1186. }
  1187. void CGDwelling::newTurn() const
  1188. {
  1189. if(cb->getDate(1) != 1) //not first day of week
  1190. return;
  1191. //town growths are handled separately
  1192. if(ID == TOWNI_TYPE)
  1193. return;
  1194. bool change = false;
  1195. SetAvailableCreatures sac;
  1196. sac.creatures = creatures;
  1197. sac.tid = id;
  1198. for (size_t i = 0; i < creatures.size(); i++)
  1199. {
  1200. if(creatures[i].second.size())
  1201. {
  1202. if(false /*accumulate creatures*/)
  1203. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  1204. else
  1205. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  1206. change = true;
  1207. }
  1208. }
  1209. if(change)
  1210. cb->sendAndApply(&sac);
  1211. }
  1212. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1213. {
  1214. if(!answer)
  1215. return;
  1216. int crid = creatures[0].second[0];
  1217. CCreature *crs = &VLC->creh->creatures[crid];
  1218. if(crs->level == 1) //first level - creatures are for free
  1219. {
  1220. if(creatures[0].first) //there are available creatures
  1221. {
  1222. int slot = h->army.getSlotFor(crid);
  1223. if(slot < 0) //no available slot
  1224. {
  1225. InfoWindow iw;
  1226. iw.player = h->tempOwner;
  1227. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1228. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1229. cb->showInfoDialog(&iw);
  1230. }
  1231. else //give creatures
  1232. {
  1233. SetAvailableCreatures sac;
  1234. sac.tid = id;
  1235. sac.creatures = creatures;
  1236. sac.creatures[0].first = 0;
  1237. SetGarrisons sg;
  1238. sg.garrs[h->id] = h->army;
  1239. sg.garrs[h->id].slots[slot].first = crid;
  1240. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1241. InfoWindow iw;
  1242. iw.player = h->tempOwner;
  1243. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1244. iw.text.addReplacement(creatures[0].first);
  1245. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1246. cb->showInfoDialog(&iw);
  1247. cb->sendAndApply(&sac);
  1248. cb->sendAndApply(&sg);
  1249. }
  1250. }
  1251. else //there no creatures
  1252. {
  1253. InfoWindow iw;
  1254. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1255. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1256. iw.player = h->tempOwner;
  1257. cb->sendAndApply(&iw);
  1258. }
  1259. }
  1260. else
  1261. {
  1262. OpenWindow ow;
  1263. ow.id1 = id;
  1264. ow.id2 = h->id;
  1265. ow.window = ID == 17
  1266. ? OpenWindow::RECRUITMENT_FIRST
  1267. : OpenWindow::RECRUITMENT_ALL;
  1268. cb->sendAndApply(&ow);
  1269. }
  1270. }
  1271. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1272. {
  1273. if(answer)
  1274. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1275. }
  1276. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1277. {
  1278. if (result->winner == 0)
  1279. {
  1280. onHeroVisit(h);
  1281. }
  1282. }
  1283. int CGTownInstance::getSightRadious() const //returns sight distance
  1284. {
  1285. if (subID == 2) //tower
  1286. {
  1287. if ((builtBuildings.find(17)) != builtBuildings.end()) //skyship
  1288. return -1; //entire map
  1289. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1290. return 20;
  1291. }
  1292. return 5;
  1293. }
  1294. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1295. {
  1296. ///this is freakin' overcomplicated solution
  1297. switch (what)
  1298. {
  1299. case 11: //add visitor of town building
  1300. bonusingBuildings[val]->setProperty (4, visitingHero->id);
  1301. break;
  1302. case 12:
  1303. bonusingBuildings[val]->setProperty (12, 0);
  1304. break;
  1305. }
  1306. }
  1307. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1308. {
  1309. if((builtBuildings.find(9))!=builtBuildings.end())
  1310. return 3;
  1311. if((builtBuildings.find(8))!=builtBuildings.end())
  1312. return 2;
  1313. if((builtBuildings.find(7))!=builtBuildings.end())
  1314. return 1;
  1315. return 0;
  1316. }
  1317. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1318. {
  1319. if ((builtBuildings.find(13))!=builtBuildings.end())
  1320. return 3;
  1321. if ((builtBuildings.find(12))!=builtBuildings.end())
  1322. return 2;
  1323. if ((builtBuildings.find(11))!=builtBuildings.end())
  1324. return 1;
  1325. if ((builtBuildings.find(10))!=builtBuildings.end())
  1326. return 0;
  1327. return -1;
  1328. }
  1329. int CGTownInstance::mageGuildLevel() const
  1330. {
  1331. if ((builtBuildings.find(4))!=builtBuildings.end())
  1332. return 5;
  1333. if ((builtBuildings.find(3))!=builtBuildings.end())
  1334. return 4;
  1335. if ((builtBuildings.find(2))!=builtBuildings.end())
  1336. return 3;
  1337. if ((builtBuildings.find(1))!=builtBuildings.end())
  1338. return 2;
  1339. if ((builtBuildings.find(0))!=builtBuildings.end())
  1340. return 1;
  1341. return 0;
  1342. }
  1343. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1344. {
  1345. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1346. }
  1347. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1348. {
  1349. return town->hordeLvl[HID];
  1350. }
  1351. int CGTownInstance::creatureGrowth(const int & level) const
  1352. {
  1353. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1354. switch(fortLevel())
  1355. {
  1356. case 3:
  1357. ret*=2;break;
  1358. case 2:
  1359. ret*=(1.5); break;
  1360. }
  1361. if(getHordeLevel(0)==level)
  1362. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1363. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1364. if(getHordeLevel(1)==level)
  1365. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1366. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1367. //support for legs of legion etc.
  1368. if(garrisonHero)
  1369. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1370. if(visitingHero)
  1371. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1372. for (std::vector<CGDwelling*>::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1373. { //+1 for each dwelling
  1374. if (VLC->creh->creatures[town->basicCreatures[level]].idNumber == (*it)->creatures[0].second[0])
  1375. ++ret;
  1376. }
  1377. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1378. ret*=1.5;
  1379. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1380. }
  1381. int CGTownInstance::dailyIncome() const
  1382. {
  1383. int ret = 0;
  1384. if ((builtBuildings.find(26))!=builtBuildings.end())
  1385. ret+=5000;
  1386. if ((builtBuildings.find(13))!=builtBuildings.end())
  1387. ret+=4000;
  1388. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1389. ret+=2000;
  1390. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1391. ret+=1000;
  1392. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1393. ret+=500;
  1394. return ret;
  1395. }
  1396. bool CGTownInstance::hasFort() const
  1397. {
  1398. return (builtBuildings.find(7))!=builtBuildings.end();
  1399. }
  1400. bool CGTownInstance::hasCapitol() const
  1401. {
  1402. return (builtBuildings.find(13))!=builtBuildings.end();
  1403. }
  1404. CGTownInstance::CGTownInstance()
  1405. :IShipyard(this)
  1406. {
  1407. builded=-1;
  1408. destroyed=-1;
  1409. garrisonHero=NULL;
  1410. town=NULL;
  1411. visitingHero = NULL;
  1412. }
  1413. CGTownInstance::~CGTownInstance()
  1414. {
  1415. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1416. delete *i;
  1417. }
  1418. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1419. {
  1420. if(checkGuild && mageGuildLevel() < level)
  1421. return 0;
  1422. int ret = 6 - level; //how many spells are available at this level
  1423. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1424. ret++;
  1425. return ret;
  1426. }
  1427. bool CGTownInstance::needsLastStack() const
  1428. {
  1429. if(garrisonHero)
  1430. return true;
  1431. else return false;
  1432. }
  1433. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1434. {
  1435. if(getOwner() != h->getOwner())
  1436. {
  1437. //TODO ally check
  1438. if(army.slots.size() > 0 || visitingHero)
  1439. {
  1440. const CGHeroInstance *defendingHero = NULL;
  1441. if(visitingHero)
  1442. defendingHero = visitingHero;
  1443. else if(garrisonHero)
  1444. defendingHero = garrisonHero;
  1445. const CArmedInstance *defendingArmy = this;
  1446. if(defendingHero)
  1447. defendingArmy = defendingHero;
  1448. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1449. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1450. }
  1451. else
  1452. {
  1453. cb->setOwner(id, h->tempOwner);
  1454. removeCapitols (h->getOwner(), true);
  1455. cb->heroVisitCastle(id, h->id);
  1456. }
  1457. }
  1458. else
  1459. cb->heroVisitCastle(id, h->id);
  1460. }
  1461. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1462. {
  1463. cb->stopHeroVisitCastle(id,h->id);
  1464. }
  1465. void CGTownInstance::initObj()
  1466. {
  1467. hoverName = name + ", " + town->Name();
  1468. creatures.resize(CREATURES_PER_TOWN);
  1469. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1470. {
  1471. if(creatureDwelling(i,false))
  1472. creatures[i].second.push_back(town->basicCreatures[i]);
  1473. if(creatureDwelling(i,true))
  1474. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1475. }
  1476. switch (subID)
  1477. { //add new visitable objects
  1478. case 0:
  1479. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1480. break;
  1481. case 2: case 3: case 5: case 6:
  1482. bonusingBuildings.push_back (new CTownBonus(23, this));
  1483. if (subID == 5)
  1484. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1485. break;
  1486. case 7:
  1487. bonusingBuildings.push_back (new CTownBonus(17, this));
  1488. break;
  1489. }
  1490. if (getOwner() != 255)
  1491. removeCapitols (getOwner(), false); // destroy other capitols
  1492. }
  1493. void CGTownInstance::newTurn() const
  1494. {
  1495. if (cb->getDate(0)%7 == 1) //reset on new week
  1496. {
  1497. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1498. cb->setObjProperty (id, 12, 0); //reset visitors for Mana Vortex
  1499. }
  1500. }
  1501. int3 CGTownInstance::getSightCenter() const
  1502. {
  1503. return pos - int3(2,0,0);
  1504. }
  1505. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1506. {
  1507. offsets += int3(-1,2,0), int3(-3,2,0);
  1508. }
  1509. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1510. {
  1511. if(result->winner == 0)
  1512. {
  1513. removeCapitols (h->getOwner(), true);
  1514. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1515. }
  1516. }
  1517. void CGTownInstance::removeCapitols (ui8 owner, bool me) const
  1518. {
  1519. if (hasCapitol()) // search for older capitol
  1520. {
  1521. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1522. for (std::vector<CGTownInstance*>::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1523. {
  1524. if (*i != this && (*i)->hasCapitol())
  1525. {
  1526. if (me)
  1527. {
  1528. RazeStructures rs;
  1529. rs.tid = id;
  1530. rs.bid.insert(13);
  1531. rs.destroyed = destroyed;
  1532. cb->sendAndApply(&rs);
  1533. return;
  1534. }
  1535. else
  1536. (*i)->builtBuildings.erase(13); //destroy all other capitols at the beginning of game
  1537. }
  1538. }
  1539. }
  1540. }
  1541. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1542. {
  1543. if(visitors.find(h->id)==visitors.end())
  1544. {
  1545. onNAHeroVisit(h->id, false);
  1546. switch(ID)
  1547. {
  1548. case 102: //tree
  1549. case 4: //arena
  1550. case 41://library
  1551. case 47: //School of Magic
  1552. case 107://School of War
  1553. break;
  1554. default:
  1555. cb->setObjProperty(id,4,h->id); //add to the visitors
  1556. break;
  1557. }
  1558. }
  1559. else
  1560. {
  1561. onNAHeroVisit(h->id, true);
  1562. }
  1563. }
  1564. void CGVisitableOPH::initObj()
  1565. {
  1566. if(ID==102)
  1567. ttype = ran()%3;
  1568. else
  1569. ttype = -1;
  1570. }
  1571. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1572. {
  1573. if(result) //player agreed to give res for exp
  1574. {
  1575. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1576. cb->changePrimSkill(heroID,4,expVal); //give exp
  1577. cb->setObjProperty(id,4,heroID); //add to the visitors
  1578. }
  1579. }
  1580. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1581. {
  1582. int id=0, subid=0, ot=0, val=1, sound = 0;
  1583. switch(ID)
  1584. {
  1585. case 4: //arena
  1586. sound = soundBase::NOMAD;
  1587. ot = 0;
  1588. break;
  1589. case 51: //mercenary camp
  1590. sound = soundBase::NOMAD;
  1591. subid=0;
  1592. ot=80;
  1593. break;
  1594. case 23: //marletto tower
  1595. sound = soundBase::NOMAD;
  1596. subid=1;
  1597. ot=39;
  1598. break;
  1599. case 61:
  1600. sound = soundBase::gazebo;
  1601. subid=2;
  1602. ot=100;
  1603. break;
  1604. case 32:
  1605. sound = soundBase::GETPROTECTION;
  1606. subid=3;
  1607. ot=59;
  1608. break;
  1609. case 100:
  1610. sound = soundBase::gazebo;
  1611. id=5;
  1612. ot=143;
  1613. val=1000;
  1614. break;
  1615. case 102:
  1616. sound = soundBase::gazebo;
  1617. id = 5;
  1618. subid = 1;
  1619. ot = 146;
  1620. val = 1;
  1621. break;
  1622. case 41:
  1623. sound = soundBase::gazebo;
  1624. ot = 66;
  1625. break;
  1626. case 47: //School of Magic
  1627. sound = soundBase::faerie;
  1628. ot = 71;
  1629. break;
  1630. case 107://School of War
  1631. sound = soundBase::MILITARY;
  1632. ot = 158;
  1633. break;
  1634. }
  1635. if (!alreadyVisited)
  1636. {
  1637. switch (ID)
  1638. {
  1639. case 4: //arena
  1640. {
  1641. BlockingDialog sd(false,true);
  1642. sd.soundID = sound;
  1643. sd.text << std::pair<ui8,ui32>(11,ot);
  1644. sd.components.push_back(Component(0,0,2,0));
  1645. sd.components.push_back(Component(0,1,2,0));
  1646. sd.player = cb->getOwner(heroID);
  1647. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1648. return;
  1649. }
  1650. case 51:
  1651. case 23:
  1652. case 61:
  1653. case 32:
  1654. {
  1655. cb->changePrimSkill(heroID,subid,val);
  1656. InfoWindow iw;
  1657. iw.soundID = sound;
  1658. iw.components.push_back(Component(0,subid,val,0));
  1659. iw.text << std::pair<ui8,ui32>(11,ot);
  1660. iw.player = cb->getOwner(heroID);
  1661. cb->showInfoDialog(&iw);
  1662. break;
  1663. }
  1664. case 100: //give exp
  1665. {
  1666. InfoWindow iw;
  1667. iw.soundID = sound;
  1668. iw.components.push_back(Component(id,subid,val,0));
  1669. iw.player = cb->getOwner(heroID);
  1670. iw.text << std::pair<ui8,ui32>(11,ot);
  1671. iw.soundID = soundBase::gazebo;
  1672. cb->showInfoDialog(&iw);
  1673. cb->changePrimSkill(heroID,4,val);
  1674. break;
  1675. }
  1676. case 102://tree
  1677. {
  1678. const CGHeroInstance *h = cb->getHero(heroID);
  1679. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1680. if(!ttype)
  1681. {
  1682. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1683. InfoWindow iw;
  1684. iw.soundID = sound;
  1685. iw.components.push_back(Component(id,subid,1,0));
  1686. iw.player = cb->getOwner(heroID);
  1687. iw.text << std::pair<ui8,ui32>(11,148);
  1688. cb->showInfoDialog(&iw);
  1689. cb->changePrimSkill(heroID,4,val);
  1690. break;
  1691. }
  1692. else
  1693. {
  1694. int res, resval;
  1695. if(ttype==1)
  1696. {
  1697. res = 6;
  1698. resval = 2000;
  1699. ot = 149;
  1700. }
  1701. else
  1702. {
  1703. res = 5;
  1704. resval = 10;
  1705. ot = 151;
  1706. }
  1707. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1708. {
  1709. ot++;
  1710. InfoWindow iw;
  1711. iw.soundID = sound;
  1712. iw.player = h->tempOwner;
  1713. iw.text << std::pair<ui8,ui32>(11,ot);
  1714. cb->showInfoDialog(&iw);
  1715. return;
  1716. }
  1717. BlockingDialog sd (true, false);
  1718. sd.soundID = sound;
  1719. sd.player = cb->getOwner(heroID);
  1720. sd.text << std::pair<ui8,ui32>(11,ot);
  1721. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  1722. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1723. }
  1724. break;
  1725. }
  1726. case 41://library of enlightenment
  1727. {
  1728. const CGHeroInstance *h = cb->getHero(heroID);
  1729. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1730. {
  1731. InfoWindow iw;
  1732. iw.soundID = sound;
  1733. iw.player = cb->getOwner(heroID);
  1734. iw.text << std::pair<ui8,ui32>(11,68);
  1735. cb->showInfoDialog(&iw);
  1736. }
  1737. else
  1738. {
  1739. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1740. cb->changePrimSkill(heroID,0,2);
  1741. cb->changePrimSkill(heroID,1,2);
  1742. cb->changePrimSkill(heroID,2,2);
  1743. cb->changePrimSkill(heroID,3,2);
  1744. InfoWindow iw;
  1745. iw.soundID = sound;
  1746. iw.player = cb->getOwner(heroID);
  1747. iw.text << std::pair<ui8,ui32>(11,66);
  1748. cb->showInfoDialog(&iw);
  1749. }
  1750. break;
  1751. }
  1752. case 47: //School of Magic
  1753. case 107://School of War
  1754. {
  1755. int skill = (ID==47 ? 2 : 0);
  1756. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1757. {
  1758. InfoWindow iw;
  1759. iw.soundID = sound;
  1760. iw.player = cb->getOwner(heroID);
  1761. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1762. cb->showInfoDialog(&iw);
  1763. }
  1764. else
  1765. {
  1766. BlockingDialog sd(true,true);
  1767. sd.soundID = sound;
  1768. sd.player = cb->getOwner(heroID);
  1769. sd.text << std::pair<ui8,ui32>(11,ot);
  1770. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1771. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1772. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1773. }
  1774. }
  1775. break;
  1776. }
  1777. }
  1778. else
  1779. {
  1780. ot++;
  1781. InfoWindow iw;
  1782. iw.soundID = sound;
  1783. iw.player = cb->getOwner(heroID);
  1784. iw.text << std::pair<ui8,ui32>(11,ot);
  1785. cb->showInfoDialog(&iw);
  1786. }
  1787. }
  1788. const std::string & CGVisitableOPH::getHoverText() const
  1789. {
  1790. int pom = -1;
  1791. switch(ID)
  1792. {
  1793. case 4:
  1794. pom = -1;
  1795. break;
  1796. case 51:
  1797. pom = 8;
  1798. break;
  1799. case 23:
  1800. pom = 7;
  1801. break;
  1802. case 61:
  1803. pom = 11;
  1804. break;
  1805. case 32:
  1806. pom = 4;
  1807. break;
  1808. case 100:
  1809. pom = 5;
  1810. break;
  1811. case 102:
  1812. pom = 18;
  1813. break;
  1814. case 41:
  1815. break;
  1816. case 47: //School of Magic
  1817. pom = 9;
  1818. break;
  1819. case 107://School of War
  1820. pom = 10;
  1821. break;
  1822. default:
  1823. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1824. }
  1825. hoverName = VLC->generaltexth->names[ID];
  1826. if(pom >= 0)
  1827. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1828. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1829. if(h)
  1830. {
  1831. hoverName += ' ';
  1832. hoverName += (vstd::contains(visitors,h->id))
  1833. ? (VLC->generaltexth->allTexts[352]) //visited
  1834. : ( VLC->generaltexth->allTexts[353]); //not visited
  1835. }
  1836. return hoverName;
  1837. }
  1838. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1839. {
  1840. cb->setObjProperty(id,4,heroID); //add to the visitors
  1841. cb->changePrimSkill(heroID,primSkill-1,2);
  1842. }
  1843. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1844. {
  1845. if(what == 4)
  1846. visitors.insert(val);
  1847. }
  1848. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1849. {
  1850. if(!which) //player refused to pay
  1851. return;
  1852. int base = (ID == 47 ? 2 : 0);
  1853. cb->setObjProperty(id,4,heroID); //add to the visitors
  1854. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1855. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1856. }
  1857. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  1858. {
  1859. ID = index;
  1860. town = TOWN;
  1861. id = town->bonusingBuildings.size();
  1862. }
  1863. void COPWBonus::setProperty(ui8 what, ui32 val)
  1864. {
  1865. switch (what)
  1866. {
  1867. case 4:
  1868. visitors.insert(val);
  1869. break;
  1870. case 12:
  1871. visitors.clear();
  1872. break;
  1873. }
  1874. }
  1875. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  1876. {
  1877. int heroID = h->id;
  1878. if (town->builtBuildings.find(ID) != town->builtBuildings.end())
  1879. {
  1880. InfoWindow iw;
  1881. iw.player = h->tempOwner;
  1882. switch (town->subID)
  1883. {
  1884. case 0: //Stables
  1885. if (!h->getBonus(HeroBonus::OBJECT, 94)) //no advMap Stables
  1886. {
  1887. GiveBonus gb;
  1888. gb.bonus = HeroBonus(HeroBonus::ONE_WEEK, HeroBonus::LAND_MOVEMENT, HeroBonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  1889. gb.hid = heroID;
  1890. cb->giveHeroBonus(&gb);
  1891. iw.text << VLC->generaltexth->allTexts[580];
  1892. cb->showInfoDialog(&iw);
  1893. }
  1894. break;
  1895. case 5: //Mana Vortex
  1896. if (visitors.find(heroID) == visitors.end() && h->mana <= h->manaLimit())
  1897. {
  1898. cb->setManaPoints (heroID, 2 * h->manaLimit());
  1899. cb->setObjProperty (id, 5, true);
  1900. iw.text << VLC->generaltexth->allTexts[579];
  1901. cb->showInfoDialog(&iw);
  1902. cb->setObjProperty (town->id, 11, id); //add to visitors
  1903. }
  1904. break;
  1905. }
  1906. }
  1907. }
  1908. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  1909. {
  1910. ID = index;
  1911. town = TOWN;
  1912. id = town->bonusingBuildings.size();
  1913. }
  1914. void CTownBonus::setProperty (ui8 what, ui32 val)
  1915. {
  1916. if(what == 4)
  1917. visitors.insert(val);
  1918. }
  1919. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  1920. {
  1921. int heroID = h->id;
  1922. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  1923. {
  1924. InfoWindow iw;
  1925. int what, val, mid;
  1926. switch (ID)
  1927. {
  1928. case 23:
  1929. switch(town->subID)
  1930. {
  1931. case 2: //wall
  1932. what = 3;
  1933. val = 1;
  1934. mid = 581;
  1935. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1936. break;
  1937. case 3: //order of fire
  1938. what = 2;
  1939. val = 1;
  1940. mid = 582;
  1941. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1942. break;
  1943. case 6://hall of valhalla
  1944. what = 0;
  1945. val = 1;
  1946. mid = 584;
  1947. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1948. break;
  1949. case 5://academy of battle scholars
  1950. what = 4;
  1951. val = 1000;
  1952. mid = 583;
  1953. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  1954. break;
  1955. }
  1956. break;
  1957. case 17:
  1958. switch(town->subID)
  1959. {
  1960. case 7: //cage of warlords
  1961. what = 1;
  1962. val = 1;
  1963. mid = 585;
  1964. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1965. break;
  1966. }
  1967. break;
  1968. }
  1969. iw.player = cb->getOwner(heroID);
  1970. iw.text << VLC->generaltexth->allTexts[mid];
  1971. cb->showInfoDialog(&iw);
  1972. cb->changePrimSkill (heroID, what, val);
  1973. cb->setObjProperty (town->id, 11, id); //add to visitors
  1974. }
  1975. }
  1976. bool CArmedInstance::needsLastStack() const
  1977. {
  1978. return false;
  1979. }
  1980. int CArmedInstance::getArmyStrength() const
  1981. {
  1982. int ret = 0;
  1983. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1984. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1985. return ret;
  1986. }
  1987. ui64 CArmedInstance::getPower (TSlot slot) const
  1988. {
  1989. return VLC->creh->creatures[army.getCreature(slot)].AIValue * army.getAmount(slot);
  1990. }
  1991. std::string CArmedInstance::getRoughAmount (TSlot slot) const
  1992. {
  1993. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(army.getAmount(slot))];
  1994. }
  1995. /*const std::string & CGCreature::getHoverText() const
  1996. {
  1997. hoverName = VLC->generaltexth->names[ID];
  1998. hoverName += "\n Power rating: ";
  1999. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  2000. if (ratio < 0.1) hoverName += "Effortless";
  2001. else if (ratio < 0.3) hoverName += "Very Weak";
  2002. else if (ratio < 0.6) hoverName += "Weak";
  2003. else if (ratio < 0.9) hoverName += "A bit weaker";
  2004. else if (ratio < 1.1) hoverName += "Equal";
  2005. else if (ratio < 1.3) hoverName += "A bit stronger";
  2006. else if (ratio < 1.8) hoverName += "Strong";
  2007. else if (ratio < 2.5) hoverName += "Very Strong";
  2008. else if (ratio < 4) hoverName += "Challenging";
  2009. else if (ratio < 8) hoverName += "Overpowering";
  2010. else if (ratio < 20) hoverName += "Deadly";
  2011. else hoverName += "Impossible";
  2012. return hoverName;
  2013. }*/
  2014. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2015. {
  2016. int action = takenAction(h);
  2017. switch( action ) //decide what we do...
  2018. {
  2019. case -2: //fight
  2020. fight(h);
  2021. break;
  2022. case -1: //flee
  2023. {
  2024. flee(h);
  2025. break;
  2026. }
  2027. case 0: //join for free
  2028. {
  2029. BlockingDialog ynd(true,false);
  2030. ynd.player = h->tempOwner;
  2031. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2032. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2033. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2034. break;
  2035. }
  2036. default: //join for gold
  2037. {
  2038. assert(action > 0);
  2039. //ask if player agrees to pay gold
  2040. BlockingDialog ynd(true,false);
  2041. ynd.player = h->tempOwner;
  2042. std::string tmp = VLC->generaltexth->advobtxt[90];
  2043. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  2044. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2045. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  2046. ynd.text << tmp;
  2047. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2048. break;
  2049. }
  2050. }
  2051. }
  2052. void CGCreature::endBattle( BattleResult *result ) const
  2053. {
  2054. if(result->winner==0)
  2055. {
  2056. cb->removeObject(id);
  2057. }
  2058. else
  2059. {
  2060. //int killedAmount=0;
  2061. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2062. // if(i->first == subID)
  2063. // killedAmount += i->second;
  2064. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  2065. MetaString ms;
  2066. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2067. pom = 174 + 3*pom + 1;
  2068. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2069. cb->setHoverName(id,&ms);
  2070. }
  2071. }
  2072. void CGCreature::initObj()
  2073. {
  2074. blockVisit = true;
  2075. switch(character)
  2076. {
  2077. case 0:
  2078. character = 0;
  2079. break;
  2080. case 1:
  2081. character = 1 + ran()%7;
  2082. break;
  2083. case 2:
  2084. character = 1 + ran()%10;
  2085. break;
  2086. case 3:
  2087. character = 4 + ran()%7;
  2088. break;
  2089. case 4:
  2090. character = 10;
  2091. break;
  2092. }
  2093. army.slots[0].first = subID;
  2094. si32 &amount = army.slots[0].second;
  2095. CCreature &c = VLC->creh->creatures[subID];
  2096. if(!amount)
  2097. if(c.ammMax == c.ammMin)
  2098. amount = c.ammMax;
  2099. else
  2100. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2101. MetaString ms;
  2102. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2103. pom = 174 + 3*pom + 1;
  2104. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2105. ms.toString(hoverName);
  2106. }
  2107. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2108. {
  2109. double hlp = h->getTotalStrength() / getArmyStrength();
  2110. if(!character) //compliant creatures will always join
  2111. return 0;
  2112. else if(allowJoin)//test for joining
  2113. {
  2114. int factor;
  2115. if(hlp >= 7)
  2116. factor = 11;
  2117. else if(hlp >= 1)
  2118. factor = (int)(2*(hlp-1));
  2119. else if(hlp >= 0.5)
  2120. factor = -1;
  2121. else if(hlp >= 0.333)
  2122. factor = -2;
  2123. else
  2124. factor = -3;
  2125. int sympathy = 0;
  2126. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2127. myKindCres.insert(subID); //we
  2128. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  2129. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2130. if(vstd::contains(i->upgrades, (ui32) id)) //it's our base creatures
  2131. myKindCres.insert(i->idNumber);
  2132. int count = 0, //how many creatures of our kind has hero
  2133. totalCount = 0;
  2134. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  2135. {
  2136. if(vstd::contains(myKindCres,i->second.first))
  2137. count += i->second.second;
  2138. totalCount += i->second.second;
  2139. }
  2140. if(count*2 > totalCount)
  2141. sympathy++;
  2142. if(count)
  2143. sympathy++;
  2144. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  2145. if(charisma >= character) //creatures might join...
  2146. {
  2147. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  2148. return 0; //join for free
  2149. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  2150. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  2151. }
  2152. }
  2153. //we are still here - creatures not joined heroes, test for fleeing
  2154. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2155. //TODO: should be deterministic (will be needed for Vision spell)
  2156. int hlp2 = (int) (hlp - 2)*1000;
  2157. if(!neverFlees
  2158. && hlp2 >= 0
  2159. && rand()%2000 < hlp2
  2160. )
  2161. return -1; //flee
  2162. else
  2163. return -2; //fight
  2164. }
  2165. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2166. {
  2167. if(pursue)
  2168. {
  2169. fight(h);
  2170. }
  2171. else
  2172. {
  2173. cb->removeObject(id);
  2174. }
  2175. }
  2176. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2177. {
  2178. if(!accept)
  2179. {
  2180. if(takenAction(h,false) == -1) //they flee
  2181. {
  2182. flee(h);
  2183. }
  2184. else //they fight
  2185. {
  2186. InfoWindow iw;
  2187. iw.player = h->tempOwner;
  2188. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2189. cb->showInfoDialog(&iw);
  2190. fight(h);
  2191. }
  2192. }
  2193. else //accepted
  2194. {
  2195. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2196. {
  2197. InfoWindow iw;
  2198. iw.player = h->tempOwner;
  2199. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2200. cb->showInfoDialog(&iw);
  2201. //act as if player refused
  2202. joinDecision(h,cost,false);
  2203. return;
  2204. }
  2205. //take gold
  2206. if(cost)
  2207. cb->giveResource(h->tempOwner,6,-cost);
  2208. int slot = h->army.getSlotFor(subID);
  2209. if(slot >= 0) //there is place
  2210. {
  2211. //add creatures
  2212. SetGarrisons sg;
  2213. sg.garrs[h->id] = h->army;
  2214. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  2215. {
  2216. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  2217. }
  2218. else //add as a new stack
  2219. {
  2220. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  2221. }
  2222. cb->sendAndApply(&sg);
  2223. cb->removeObject(id);
  2224. }
  2225. else
  2226. {
  2227. cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2228. }
  2229. }
  2230. }
  2231. void CGCreature::fight( const CGHeroInstance *h ) const
  2232. {
  2233. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2234. }
  2235. void CGCreature::flee( const CGHeroInstance * h ) const
  2236. {
  2237. BlockingDialog ynd(true,false);
  2238. ynd.player = h->tempOwner;
  2239. ynd.text << std::pair<ui8,ui32>(11,91);
  2240. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2241. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2242. }
  2243. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2244. {
  2245. if(subID == 7) //TODO: support for abandoned mine
  2246. return;
  2247. if(h->tempOwner == tempOwner) //we're visiting our mine
  2248. {
  2249. cb->showGarrisonDialog(id,h->id,true,0);
  2250. return;
  2251. }
  2252. //TODO: check if mine is guarded
  2253. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  2254. MetaString ms;
  2255. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  2256. cb->setHoverName(id,&ms);
  2257. int vv=1; //amount of resource per turn
  2258. if (subID==0 || subID==2)
  2259. vv++;
  2260. else if (subID==6)
  2261. vv = 1000;
  2262. InfoWindow iw;
  2263. iw.soundID = soundBase::FLAGMINE;
  2264. iw.text << std::pair<ui8,ui32>(10,subID);
  2265. iw.player = h->tempOwner;
  2266. iw.components.push_back(Component(2,subID,vv,-1));
  2267. cb->showInfoDialog(&iw);
  2268. }
  2269. void CGMine::newTurn() const
  2270. {
  2271. if(cb->getDate() == 1)
  2272. return;
  2273. if (tempOwner == NEUTRAL_PLAYER)
  2274. return;
  2275. int vv = 1;
  2276. if (subID==0 || subID==2)
  2277. vv++;
  2278. else if (subID==6)
  2279. vv = 1000;
  2280. cb->giveResource(tempOwner,subID,vv);
  2281. }
  2282. void CGMine::initObj()
  2283. {
  2284. MetaString ms;
  2285. ms << std::pair<ui8,ui32>(9,subID);
  2286. if(tempOwner >= PLAYER_LIMIT)
  2287. tempOwner = NEUTRAL_PLAYER;
  2288. else
  2289. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2290. ms.toString(hoverName);
  2291. }
  2292. void CGResource::initObj()
  2293. {
  2294. blockVisit = true;
  2295. hoverName = VLC->generaltexth->restypes[subID];
  2296. if(!amount)
  2297. {
  2298. switch(subID)
  2299. {
  2300. case 6:
  2301. amount = 500 + (rand()%6)*100;
  2302. break;
  2303. case 0: case 2:
  2304. amount = 6 + (rand()%5);
  2305. break;
  2306. default:
  2307. amount = 3 + (rand()%3);
  2308. break;
  2309. }
  2310. }
  2311. }
  2312. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2313. {
  2314. if(army.slots.size())
  2315. {
  2316. if(message.size())
  2317. {
  2318. BlockingDialog ynd(true,false);
  2319. ynd.player = h->getOwner();
  2320. ynd.text << message;
  2321. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2322. }
  2323. else
  2324. {
  2325. fightForRes(1,h);
  2326. }
  2327. }
  2328. else
  2329. {
  2330. if(message.length())
  2331. {
  2332. InfoWindow iw;
  2333. iw.player = h->tempOwner;
  2334. iw.text << message;
  2335. cb->showInfoDialog(&iw);
  2336. }
  2337. collectRes(h->getOwner());
  2338. }
  2339. }
  2340. void CGResource::collectRes( int player ) const
  2341. {
  2342. cb->giveResource(player,subID,amount);
  2343. ShowInInfobox sii;
  2344. sii.player = player;
  2345. sii.c = Component(2,subID,amount,0);
  2346. sii.text << std::pair<ui8,ui32>(11,113);
  2347. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2348. cb->showCompInfo(&sii);
  2349. cb->removeObject(id);
  2350. }
  2351. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2352. {
  2353. if(agreed)
  2354. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  2355. }
  2356. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2357. {
  2358. if(result->winner == 0) //attacker won
  2359. collectRes(h->getOwner());
  2360. }
  2361. void CGVisitableOPW::newTurn() const
  2362. {
  2363. if (cb->getDate(1)==1) //first day of week
  2364. {
  2365. cb->setObjProperty(id,5,false);
  2366. MetaString ms; //set text to "not visited"
  2367. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2368. cb->setHoverName(id,&ms);
  2369. }
  2370. }
  2371. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2372. {
  2373. int mid, sound = 0;
  2374. switch (ID)
  2375. {
  2376. case 55: //mystical garden
  2377. sound = soundBase::experience;
  2378. mid = 92;
  2379. break;
  2380. case 112://windmill
  2381. sound = soundBase::GENIE;
  2382. mid = 170;
  2383. break;
  2384. case 109://waterwheel
  2385. sound = soundBase::GENIE;
  2386. mid = 164;
  2387. break;
  2388. }
  2389. if (visited)
  2390. {
  2391. if (ID!=112)
  2392. mid++;
  2393. else
  2394. mid--;
  2395. InfoWindow iw;
  2396. iw.soundID = sound;
  2397. iw.player = h->tempOwner;
  2398. iw.text << std::pair<ui8,ui32>(11,mid);
  2399. cb->showInfoDialog(&iw);
  2400. }
  2401. else
  2402. {
  2403. int type, sub, val;
  2404. type = 2;
  2405. switch (ID)
  2406. {
  2407. case 55:
  2408. if (rand()%2)
  2409. {
  2410. sub = 5;
  2411. val = 5;
  2412. }
  2413. else
  2414. {
  2415. sub = 6;
  2416. val = 500;
  2417. }
  2418. break;
  2419. case 112:
  2420. mid = 170;
  2421. sub = (rand() % 5) + 1;
  2422. val = (rand() % 4) + 3;
  2423. break;
  2424. case 109:
  2425. mid = 164;
  2426. sub = 6;
  2427. if(cb->getDate(0)<8)
  2428. val = 500;
  2429. else
  2430. val = 1000;
  2431. }
  2432. cb->giveResource(h->tempOwner,sub,val);
  2433. InfoWindow iw;
  2434. iw.soundID = sound;
  2435. iw.player = h->tempOwner;
  2436. iw.components.push_back(Component(type,sub,val,0));
  2437. iw.text << std::pair<ui8,ui32>(11,mid);
  2438. cb->showInfoDialog(&iw);
  2439. cb->setObjProperty(id,5,true);
  2440. MetaString ms; //set text to "visited"
  2441. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2442. cb->setHoverName(id,&ms);
  2443. }
  2444. }
  2445. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2446. {
  2447. if(what == 5)
  2448. visited = val;
  2449. }
  2450. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2451. {
  2452. int destinationid=-1;
  2453. switch(ID)
  2454. {
  2455. case 43: //one way - find corresponding exit monolith
  2456. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2457. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2458. else
  2459. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2460. break;
  2461. case 45: //two way monolith - pick any other one
  2462. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2463. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2464. else
  2465. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2466. break;
  2467. case 103: //find nearest subterranean gate on the other level
  2468. {
  2469. for(int i=0; i < gates.size(); i++)
  2470. {
  2471. if(gates[i].first == id)
  2472. {
  2473. destinationid = gates[i].second;
  2474. break;
  2475. }
  2476. else if(gates[i].second == id)
  2477. {
  2478. destinationid = gates[i].first;
  2479. break;
  2480. }
  2481. }
  2482. break;
  2483. }
  2484. }
  2485. if(destinationid < 0)
  2486. {
  2487. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  2488. return;
  2489. }
  2490. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2491. true);
  2492. }
  2493. void CGTeleport::initObj()
  2494. {
  2495. int si = subID;
  2496. if(ID == 103) //ignore subterranean gates subid
  2497. si = 0;
  2498. objs[ID][si].push_back(id);
  2499. }
  2500. void CGTeleport::postInit() //matches subterranean gates into pairs
  2501. {
  2502. //split on underground and surface gates
  2503. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  2504. for(size_t i = 0; i < objs[103][0].size(); i++)
  2505. {
  2506. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  2507. gatesSplit[hlp->pos.z].push_back(hlp);
  2508. }
  2509. //sort by position
  2510. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  2511. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  2512. {
  2513. const CGObjectInstance *cur = gatesSplit[0][i];
  2514. //find nearest underground exit
  2515. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  2516. for(int j = 0; j < gatesSplit[1].size(); j++)
  2517. {
  2518. const CGObjectInstance *checked = gatesSplit[1][j];
  2519. if(!checked)
  2520. continue;
  2521. double hlp = checked->pos.dist2d(cur->pos);
  2522. if(hlp < best.second)
  2523. {
  2524. best.first = j;
  2525. best.second = hlp;
  2526. }
  2527. }
  2528. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  2529. gatesSplit[1][best.first] = NULL;
  2530. }
  2531. objs.erase(103);
  2532. }
  2533. void CGArtifact::initObj()
  2534. {
  2535. blockVisit = true;
  2536. if(ID == 5)
  2537. hoverName = VLC->arth->artifacts[subID].Name();
  2538. }
  2539. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2540. {
  2541. if(!army.slots.size())
  2542. {
  2543. if(ID == 5)
  2544. {
  2545. InfoWindow iw;
  2546. iw.soundID = soundBase::treasure;
  2547. iw.player = h->tempOwner;
  2548. iw.components.push_back(Component(4,subID,0,0));
  2549. if(message.length())
  2550. iw.text << message;
  2551. else
  2552. iw.text << std::pair<ui8,ui32>(12,subID);
  2553. cb->showInfoDialog(&iw);
  2554. }
  2555. pick(h);
  2556. }
  2557. else
  2558. {
  2559. if(message.size())
  2560. {
  2561. BlockingDialog ynd(true,false);
  2562. ynd.player = h->getOwner();
  2563. ynd.text << message;
  2564. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2565. }
  2566. else
  2567. {
  2568. fightForArt(0,h);
  2569. }
  2570. }
  2571. }
  2572. void CGArtifact::pick(const CGHeroInstance * h) const
  2573. {
  2574. if(ID == 5) //Artifact
  2575. {
  2576. cb->giveHeroArtifact(subID,h->id,-2);
  2577. }
  2578. else if(ID == 93) // Spell scroll
  2579. {
  2580. //TODO: support for the spell scroll
  2581. }
  2582. cb->removeObject(id);
  2583. }
  2584. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2585. {
  2586. if(agreed)
  2587. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2588. }
  2589. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2590. {
  2591. if(result->winner == 0) //attacker won
  2592. pick(h);
  2593. }
  2594. void CGPickable::initObj()
  2595. {
  2596. blockVisit = true;
  2597. switch(ID)
  2598. {
  2599. case 12: //campfire
  2600. val2 = (ran()%3) + 4; //4 - 6
  2601. val1 = val2 * 100;
  2602. type = ran()%6; //given resource
  2603. break;
  2604. case 29: //floatsam
  2605. switch(type = ran()%4)
  2606. {
  2607. case 0:
  2608. val1 = val2 = 0;
  2609. break;
  2610. case 1:
  2611. val1 = 5;
  2612. val2 = 0;
  2613. break;
  2614. case 2:
  2615. val1 = 5;
  2616. val2 = 200;
  2617. break;
  2618. case 3:
  2619. val1 = 10;
  2620. val2 = 500;
  2621. break;
  2622. }
  2623. break;
  2624. case 82: //sea chest
  2625. {
  2626. int hlp = ran()%100;
  2627. if(hlp < 20)
  2628. {
  2629. val1 = 0;
  2630. type = 0;
  2631. }
  2632. else if(hlp < 90)
  2633. {
  2634. val1 = 1500;
  2635. type = 2;
  2636. }
  2637. else
  2638. {
  2639. val1 = 1000;
  2640. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2641. type = 1;
  2642. }
  2643. }
  2644. break;
  2645. case 86: //Shipwreck Survivor
  2646. {
  2647. int hlp = ran()%100;
  2648. if(hlp < 55)
  2649. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2650. else if(hlp < 75)
  2651. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  2652. else if(hlp < 95)
  2653. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  2654. else
  2655. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  2656. }
  2657. break;
  2658. case 101: //treasure chest
  2659. {
  2660. int hlp = ran()%100;
  2661. if(hlp >= 95)
  2662. {
  2663. type = 1;
  2664. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2665. return;
  2666. }
  2667. else if (hlp >= 65)
  2668. {
  2669. val1 = 2000;
  2670. }
  2671. else if(hlp >= 33)
  2672. {
  2673. val1 = 1500;
  2674. }
  2675. else
  2676. {
  2677. val1 = 1000;
  2678. }
  2679. val2 = val1 - 500;
  2680. type = 0;
  2681. break;
  2682. }
  2683. }
  2684. }
  2685. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2686. {
  2687. switch(ID)
  2688. {
  2689. case 12: //campfire
  2690. {
  2691. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2692. cb->giveResource(h->tempOwner,6,val1);//gold
  2693. InfoWindow iw;
  2694. iw.soundID = soundBase::experience;
  2695. iw.player = h->tempOwner;
  2696. iw.components.push_back(Component(2,6,val1,0));
  2697. iw.components.push_back(Component(2,type,val2,0));
  2698. iw.text << std::pair<ui8,ui32>(11,23);
  2699. cb->showInfoDialog(&iw);
  2700. break;
  2701. }
  2702. case 29: //flotsam
  2703. {
  2704. cb->giveResource(h->tempOwner,0,val1); //wood
  2705. cb->giveResource(h->tempOwner,6,val2);//gold
  2706. InfoWindow iw;
  2707. iw.soundID = soundBase::GENIE;
  2708. iw.player = h->tempOwner;
  2709. if(val1)
  2710. iw.components.push_back(Component(2,0,val1,0));
  2711. if(val2)
  2712. iw.components.push_back(Component(2,6,val2,0));
  2713. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2714. cb->showInfoDialog(&iw);
  2715. break;
  2716. }
  2717. case 82: //Sea Chest
  2718. {
  2719. InfoWindow iw;
  2720. iw.soundID = soundBase::chest;
  2721. iw.player = h->tempOwner;
  2722. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2723. if(val1) //there is gold
  2724. {
  2725. iw.components.push_back(Component(2,6,val1,0));
  2726. cb->giveResource(h->tempOwner,6,val1);
  2727. }
  2728. if(type == 1) //art
  2729. {
  2730. //TODO: what if no space in backpack?
  2731. iw.components.push_back(Component(4, val2, 1, 0));
  2732. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2733. cb->giveHeroArtifact(val2,h->id,-2);
  2734. }
  2735. cb->showInfoDialog(&iw);
  2736. break;
  2737. }
  2738. case 86: //Shipwreck Survivor
  2739. {
  2740. //TODO: what if no space in backpack?
  2741. InfoWindow iw;
  2742. iw.soundID = soundBase::experience;
  2743. iw.player = h->tempOwner;
  2744. iw.components.push_back(Component(4,val1,1,0));
  2745. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2746. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2747. cb->giveHeroArtifact(val1,h->id,-2);
  2748. cb->showInfoDialog(&iw);
  2749. break;
  2750. }
  2751. case 101: //treasure chest
  2752. {
  2753. if (subID) //not OH3 treasure chest
  2754. {
  2755. tlog2 << "Not supported WoG treasure chest!\n";
  2756. return;
  2757. }
  2758. if(type) //there is an artifact
  2759. {
  2760. cb->giveHeroArtifact(val1,h->id,-2);
  2761. InfoWindow iw;
  2762. iw.soundID = soundBase::treasure;
  2763. iw.player = h->tempOwner;
  2764. iw.components.push_back(Component(4,val1,1,0));
  2765. iw.text << std::pair<ui8,ui32>(11,145);
  2766. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2767. cb->showInfoDialog(&iw);
  2768. break;
  2769. }
  2770. else
  2771. {
  2772. BlockingDialog sd(false,true);
  2773. sd.player = h->tempOwner;
  2774. sd.text << std::pair<ui8,ui32>(11,146);
  2775. sd.components.push_back(Component(2,6,val1,0));
  2776. sd.components.push_back(Component(5,0,val2,0));
  2777. sd.soundID = soundBase::chest;
  2778. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2779. cb->showBlockingDialog(&sd,fun);
  2780. return;
  2781. }
  2782. }
  2783. }
  2784. cb->removeObject(id);
  2785. }
  2786. void CGPickable::chosen( int which, int heroID ) const
  2787. {
  2788. switch(which)
  2789. {
  2790. case 1: //player pick gold
  2791. cb->giveResource(cb->getOwner(heroID),6,val1);
  2792. break;
  2793. case 2: //player pick exp
  2794. cb->changePrimSkill(heroID, 4, val2);
  2795. break;
  2796. default:
  2797. throw std::string("Unhandled treasure choice");
  2798. }
  2799. cb->removeObject(id);
  2800. }
  2801. void CGWitchHut::initObj()
  2802. {
  2803. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2804. }
  2805. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2806. {
  2807. InfoWindow iw;
  2808. iw.soundID = soundBase::gazebo;
  2809. iw.player = h->getOwner();
  2810. if(!hasVisited(h->tempOwner))
  2811. cb->setObjProperty(id,10,h->tempOwner);
  2812. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2813. {
  2814. iw.text << std::pair<ui8,ui32>(11,172);
  2815. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2816. }
  2817. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2818. {
  2819. iw.text << std::pair<ui8,ui32>(11,173);
  2820. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2821. }
  2822. else //give sec skill
  2823. {
  2824. iw.components.push_back(Component(1, ability, 1, 0));
  2825. iw.text << std::pair<ui8,ui32>(11,171);
  2826. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2827. cb->changeSecSkill(h->id,ability,1,true);
  2828. }
  2829. cb->showInfoDialog(&iw);
  2830. }
  2831. const std::string & CGWitchHut::getHoverText() const
  2832. {
  2833. hoverName = VLC->generaltexth->names[ID];
  2834. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2835. {
  2836. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2837. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2838. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2839. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2840. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2841. }
  2842. return hoverName;
  2843. }
  2844. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2845. {
  2846. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2847. int messageID, bonusType, bonusVal;
  2848. int bonusMove = 0, sound = -1;
  2849. InfoWindow iw;
  2850. iw.player = h->tempOwner;
  2851. GiveBonus gbonus;
  2852. gbonus.hid = h->id;
  2853. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2854. gbonus.bonus.source = HeroBonus::OBJECT;
  2855. gbonus.bonus.id = ID;
  2856. switch(ID)
  2857. {
  2858. case 11: //buoy
  2859. messageID = 21;
  2860. sound = soundBase::MORALE;
  2861. gbonus.bonus.type = HeroBonus::MORALE;
  2862. gbonus.bonus.val = +1;
  2863. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2864. break;
  2865. case 14: //swan pond
  2866. messageID = 29;
  2867. sound = soundBase::LUCK;
  2868. gbonus.bonus.type = HeroBonus::LUCK;
  2869. gbonus.bonus.val = 2;
  2870. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2871. bonusMove = -h->movement;
  2872. break;
  2873. case 28: //Faerie Ring
  2874. messageID = 49;
  2875. sound = soundBase::LUCK;
  2876. gbonus.bonus.type = HeroBonus::LUCK;
  2877. gbonus.bonus.val = 1;
  2878. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2879. break;
  2880. case 30: //fountain of fortune
  2881. messageID = 55;
  2882. sound = soundBase::LUCK;
  2883. gbonus.bonus.type = HeroBonus::LUCK;
  2884. gbonus.bonus.val = rand()%5 - 1;
  2885. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2886. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2887. break;
  2888. case 38: //idol of fortune
  2889. messageID = 62;
  2890. sound = soundBase::experience;
  2891. if(cb->getDate(1) == 7) //7th day of week
  2892. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2893. else
  2894. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2895. gbonus.bonus.val = 1;
  2896. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2897. break;
  2898. case 52: //Mermaid
  2899. messageID = 83;
  2900. sound = soundBase::LUCK;
  2901. gbonus.bonus.type = HeroBonus::LUCK;
  2902. gbonus.bonus.val = 1;
  2903. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2904. break;
  2905. case 64: //Rally Flag
  2906. sound = soundBase::MORALE;
  2907. messageID = 111;
  2908. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2909. gbonus.bonus.val = 1;
  2910. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2911. bonusMove = 400;
  2912. break;
  2913. case 56: //oasis
  2914. messageID = 95;
  2915. gbonus.bonus.type = HeroBonus::MORALE;
  2916. gbonus.bonus.val = 1;
  2917. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2918. bonusMove = 800;
  2919. break;
  2920. case 96: //temple
  2921. messageID = 140;
  2922. iw.soundID = soundBase::temple;
  2923. gbonus.bonus.type = HeroBonus::MORALE;
  2924. if(cb->getDate(1)==7) //sunday
  2925. {
  2926. gbonus.bonus.val = 2;
  2927. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2928. }
  2929. else
  2930. {
  2931. gbonus.bonus.val = 1;
  2932. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2933. }
  2934. break;
  2935. case 110://Watering Hole
  2936. sound = soundBase::MORALE;
  2937. messageID = 166;
  2938. gbonus.bonus.type = HeroBonus::MORALE;
  2939. gbonus.bonus.val = 1;
  2940. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2941. bonusMove = 400;
  2942. break;
  2943. case 31: //Fountain of Youth
  2944. sound = soundBase::MORALE;
  2945. messageID = 57;
  2946. gbonus.bonus.type = HeroBonus::MORALE;
  2947. gbonus.bonus.val = 1;
  2948. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2949. bonusMove = 400;
  2950. break;
  2951. case 94: //Stables TODO: upgrade Cavaliers
  2952. sound = soundBase::horse20;
  2953. std::set<ui32> slots;
  2954. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2955. {
  2956. if(i->second.first == 10)
  2957. slots.insert(i->first);
  2958. }
  2959. if (!slots.empty())
  2960. {
  2961. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2962. {
  2963. UpgradeCreature uc(*i, id, 11);
  2964. //uc.applyGh (&gh);
  2965. }
  2966. }
  2967. messageID = 137;
  2968. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  2969. gbonus.bonus.val = 600;
  2970. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  2971. break;
  2972. }
  2973. if(visited)
  2974. {
  2975. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  2976. messageID--;
  2977. else
  2978. messageID++;
  2979. }
  2980. else
  2981. {
  2982. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2983. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2984. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2985. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2986. cb->giveHeroBonus(&gbonus);
  2987. if(bonusMove) //swan pond - take all move points
  2988. {
  2989. SetMovePoints smp;
  2990. smp.hid = h->id;
  2991. smp.val = h->movement + bonusMove;
  2992. cb->setMovePoints(&smp);
  2993. }
  2994. }
  2995. iw.soundID = sound;
  2996. iw.text << std::pair<ui8,ui32>(11,messageID);
  2997. cb->showInfoDialog(&iw);
  2998. }
  2999. const std::string & CGBonusingObject::getHoverText() const
  3000. {
  3001. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3002. hoverName = VLC->generaltexth->names[ID];
  3003. if(h)
  3004. {
  3005. if(!h->getBonus(HeroBonus::OBJECT,ID))
  3006. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3007. else
  3008. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3009. }
  3010. return hoverName;
  3011. }
  3012. void CGBonusingObject::initObj()
  3013. {
  3014. if(ID == 11 || ID == 52) //Buoy / Mermaid
  3015. {
  3016. blockVisit = true;
  3017. }
  3018. }
  3019. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  3020. {
  3021. int messageID;
  3022. InfoWindow iw;
  3023. iw.player = h->tempOwner;
  3024. iw.soundID = soundBase::GENIE;
  3025. if (!visited)
  3026. {
  3027. if (h->mana > h->manaLimit())
  3028. messageID = 76;
  3029. else
  3030. {
  3031. messageID = 74;
  3032. cb->setManaPoints (h->id, 2 * h->manaLimit());
  3033. cb->setObjProperty (id, 5, true);
  3034. }
  3035. }
  3036. else
  3037. messageID = 75;
  3038. iw.text << std::pair<ui8,ui32>(11,messageID);
  3039. cb->showInfoDialog(&iw);
  3040. }
  3041. const std::string & CGMagicSpring::getHoverText() const
  3042. {
  3043. hoverName = VLC->generaltexth->names[ID];
  3044. if(!visited)
  3045. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3046. else
  3047. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3048. return hoverName;
  3049. }
  3050. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  3051. {
  3052. int message;
  3053. InfoWindow iw;
  3054. iw.soundID = soundBase::faerie;
  3055. iw.player = h->tempOwner;
  3056. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  3057. {
  3058. message = 78;
  3059. }
  3060. else if(h->mana < h->manaLimit())
  3061. {
  3062. giveDummyBonus(h->id);
  3063. cb->setManaPoints(h->id,h->manaLimit());
  3064. message = 77;
  3065. }
  3066. else
  3067. {
  3068. message = 79;
  3069. }
  3070. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  3071. cb->showInfoDialog(&iw);
  3072. }
  3073. const std::string & CGMagicWell::getHoverText() const
  3074. {
  3075. getNameVis(hoverName);
  3076. return hoverName;
  3077. }
  3078. void CGPandoraBox::initObj()
  3079. {
  3080. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  3081. }
  3082. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  3083. {
  3084. BlockingDialog bd (true, false);
  3085. bd.player = h->getOwner();
  3086. bd.soundID = soundBase::QUEST;
  3087. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  3088. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  3089. }
  3090. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  3091. {
  3092. if (accept)
  3093. {
  3094. if (army.slots.size() > 0) //if pandora's box is protected by army
  3095. {
  3096. InfoWindow iw;
  3097. iw.player = h->tempOwner;
  3098. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3099. cb->showInfoDialog(&iw);
  3100. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  3101. }
  3102. else if (message.size() == 0 && resources.size() == 0
  3103. && primskills.size() == 0 && abilities.size() == 0
  3104. && abilityLevels.size() == 0 && artifacts.size() == 0
  3105. && spells.size() == 0 && creatures.slots.size() > 0
  3106. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  3107. {
  3108. InfoWindow iw;
  3109. iw.player = h->tempOwner;
  3110. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  3111. cb->showInfoDialog(&iw);
  3112. }
  3113. else //if it gives something without battle
  3114. {
  3115. giveContents (h, false);
  3116. }
  3117. }
  3118. }
  3119. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  3120. {
  3121. if(result->winner)
  3122. return;
  3123. giveContents(h,true);
  3124. }
  3125. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  3126. {
  3127. InfoWindow iw;
  3128. iw.player = h->getOwner();
  3129. bool changesPrimSkill = false;
  3130. for (int i = 0; i < primskills.size(); i++)
  3131. {
  3132. if(primskills[i])
  3133. {
  3134. changesPrimSkill = true;
  3135. break;
  3136. }
  3137. }
  3138. if(gainedExp || changesPrimSkill || abilities.size())
  3139. {
  3140. getText(iw,afterBattle,175,h);
  3141. if(gainedExp)
  3142. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  3143. for(int i=0; i<primskills.size(); i++)
  3144. if(primskills[i])
  3145. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  3146. for(int i=0; i<abilities.size(); i++)
  3147. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  3148. cb->showInfoDialog(&iw);
  3149. //give exp
  3150. if(gainedExp)
  3151. cb->changePrimSkill(h->id,4,gainedExp,false);
  3152. //give prim skills
  3153. for(int i=0; i<primskills.size(); i++)
  3154. if(primskills[i])
  3155. cb->changePrimSkill(h->id,i,primskills[i],false);
  3156. //give sec skills
  3157. for(int i=0; i<abilities.size(); i++)
  3158. {
  3159. int curLev = h->getSecSkillLevel(abilities[i]);
  3160. if(curLev && curLev < abilityLevels[i]
  3161. || h->secSkills.size() < SKILL_PER_HERO )
  3162. {
  3163. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  3164. }
  3165. }
  3166. }
  3167. if(spells.size())
  3168. {
  3169. std::set<ui32> spellsToGive;
  3170. iw.components.clear();
  3171. for(int i=0; i<spells.size(); i++)
  3172. {
  3173. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  3174. spellsToGive.insert(spells[i]);
  3175. }
  3176. if(spellsToGive.size())
  3177. {
  3178. cb->changeSpells(h->id,true,spellsToGive);
  3179. cb->showInfoDialog(&iw);
  3180. }
  3181. }
  3182. if(manaDiff)
  3183. {
  3184. getText(iw,afterBattle,manaDiff,176,177,h);
  3185. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  3186. cb->showInfoDialog(&iw);
  3187. cb->setManaPoints(h->id, h->mana + manaDiff);
  3188. }
  3189. if(moraleDiff)
  3190. {
  3191. getText(iw,afterBattle,moraleDiff,178,179,h);
  3192. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  3193. cb->showInfoDialog(&iw);
  3194. GiveBonus gb;
  3195. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  3196. gb.hid = h->id;
  3197. cb->giveHeroBonus(&gb);
  3198. }
  3199. if(luckDiff)
  3200. {
  3201. getText(iw,afterBattle,luckDiff,180,181,h);
  3202. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  3203. cb->showInfoDialog(&iw);
  3204. GiveBonus gb;
  3205. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  3206. gb.hid = h->id;
  3207. cb->giveHeroBonus(&gb);
  3208. }
  3209. iw.components.clear();
  3210. iw.text.clear();
  3211. for(int i=0; i<resources.size(); i++)
  3212. {
  3213. if(resources[i] < 0)
  3214. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3215. }
  3216. if(iw.components.size())
  3217. {
  3218. getText(iw,afterBattle,182,h);
  3219. cb->showInfoDialog(&iw);
  3220. }
  3221. iw.components.clear();
  3222. iw.text.clear();
  3223. for(int i=0; i<resources.size(); i++)
  3224. {
  3225. if(resources[i] > 0)
  3226. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3227. }
  3228. if(iw.components.size())
  3229. {
  3230. getText(iw,afterBattle,183,h);
  3231. cb->showInfoDialog(&iw);
  3232. }
  3233. iw.components.clear();
  3234. // getText(iw,afterBattle,183,h);
  3235. for(int i=0; i<artifacts.size(); i++)
  3236. {
  3237. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  3238. if(iw.components.size() >= 14)
  3239. {
  3240. cb->showInfoDialog(&iw);
  3241. iw.components.clear();
  3242. }
  3243. }
  3244. if(iw.components.size())
  3245. {
  3246. cb->showInfoDialog(&iw);
  3247. }
  3248. for(int i=0; i<resources.size(); i++)
  3249. if(resources[i])
  3250. cb->giveResource(h->getOwner(),i,resources[i]);
  3251. for(int i=0; i<artifacts.size(); i++)
  3252. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  3253. iw.components.clear();
  3254. iw.text.clear();
  3255. if (creatures.slots.size())
  3256. { //this part is taken straight from creature bank
  3257. MetaString loot;
  3258. CCreatureSet ourArmy = creatures;
  3259. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  3260. { //build list of joined creatures
  3261. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  3262. loot << "%s";
  3263. if (i->second.second == 1)
  3264. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  3265. else
  3266. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  3267. }
  3268. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  3269. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  3270. else
  3271. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  3272. iw.text.addReplacement (loot.buildList());
  3273. iw.text.addReplacement (h->name);
  3274. cb->showInfoDialog(&iw);
  3275. cb->giveCreatures (id, h, ourArmy);
  3276. }
  3277. if(!afterBattle && message.size())
  3278. {
  3279. iw.text << message;
  3280. cb->showInfoDialog(&iw);
  3281. }
  3282. cb->removeObject(id);
  3283. }
  3284. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  3285. {
  3286. if(afterBattle || !message.size())
  3287. {
  3288. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  3289. iw.text.addReplacement(h->name);
  3290. }
  3291. else
  3292. {
  3293. iw.text << message;
  3294. afterBattle = true;
  3295. }
  3296. }
  3297. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  3298. {
  3299. iw.components.clear();
  3300. iw.text.clear();
  3301. if(afterBattle || !message.size())
  3302. {
  3303. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  3304. iw.text.addReplacement(h->name);
  3305. }
  3306. else
  3307. {
  3308. iw.text << message;
  3309. afterBattle = true;
  3310. }
  3311. }
  3312. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  3313. {
  3314. if(!(availableFor & (1 << h->tempOwner)))
  3315. return;
  3316. if(cb->getPlayerSettings(h->tempOwner)->human)
  3317. {
  3318. if(humanActivate)
  3319. activated(h);
  3320. }
  3321. else if(computerActivate)
  3322. activated(h);
  3323. }
  3324. void CGEvent::activated( const CGHeroInstance * h ) const
  3325. {
  3326. if(army.slots.size() > 0)
  3327. {
  3328. InfoWindow iw;
  3329. iw.player = h->tempOwner;
  3330. if(message.size())
  3331. iw.text << message;
  3332. else
  3333. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3334. cb->showInfoDialog(&iw);
  3335. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  3336. }
  3337. else
  3338. {
  3339. giveContents(h,false);
  3340. }
  3341. }
  3342. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  3343. {
  3344. InfoWindow iw;
  3345. iw.soundID = soundBase::LIGHTHOUSE;
  3346. iw.player = h->tempOwner;
  3347. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  3348. cb->showInfoDialog(&iw);
  3349. FoWChange fw;
  3350. fw.player = h->tempOwner;
  3351. fw.mode = 1;
  3352. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  3353. cb->sendAndApply(&fw);
  3354. }
  3355. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  3356. {
  3357. if(spell == 255)
  3358. {
  3359. tlog1 << "Not initialized shrine visited!\n";
  3360. return;
  3361. }
  3362. if(!hasVisited(h->tempOwner))
  3363. cb->setObjProperty(id,10,h->tempOwner);
  3364. InfoWindow iw;
  3365. iw.soundID = soundBase::temple;
  3366. iw.player = h->getOwner();
  3367. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  3368. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  3369. iw.text << ".";
  3370. if(!h->getArt(17)) //no spellbook
  3371. {
  3372. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  3373. }
  3374. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  3375. {
  3376. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  3377. }
  3378. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  3379. {
  3380. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  3381. }
  3382. else //give spell
  3383. {
  3384. std::set<ui32> spells;
  3385. spells.insert(spell);
  3386. cb->changeSpells(h->id,true,spells);
  3387. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  3388. }
  3389. cb->showInfoDialog(&iw);
  3390. }
  3391. void CGShrine::initObj()
  3392. {
  3393. if(spell == 255) //spell not set
  3394. {
  3395. int level = ID-87;
  3396. std::vector<ui16> possibilities;
  3397. cb->getAllowedSpells (possibilities, level);
  3398. if(!possibilities.size())
  3399. {
  3400. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  3401. return;
  3402. }
  3403. spell = possibilities[ran() % possibilities.size()];
  3404. }
  3405. }
  3406. const std::string & CGShrine::getHoverText() const
  3407. {
  3408. hoverName = VLC->generaltexth->names[ID];
  3409. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3410. {
  3411. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  3412. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  3413. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3414. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  3415. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  3416. }
  3417. return hoverName;
  3418. }
  3419. void CGSignBottle::initObj()
  3420. {
  3421. //if no text is set than we pick random from the predefined ones
  3422. if(!message.size())
  3423. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  3424. if(ID == 59)
  3425. {
  3426. blockVisit = true;
  3427. }
  3428. }
  3429. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  3430. {
  3431. InfoWindow iw;
  3432. iw.soundID = soundBase::STORE;
  3433. iw.player = h->getOwner();
  3434. iw.text << message;
  3435. cb->showInfoDialog(&iw);
  3436. if(ID == 59)
  3437. cb->removeObject(id);
  3438. }
  3439. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  3440. {
  3441. }
  3442. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  3443. {
  3444. int type = bonusType;
  3445. int bid = bonusID;
  3446. //check if the bonus if applicable, if not - give primary skill (always possible)
  3447. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  3448. if((type == 1
  3449. && ((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)
  3450. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)))) //hero doesn't have a spellbook or already knows the spell
  3451. {
  3452. type = 0;
  3453. bid = ran() % PRIMARY_SKILLS;
  3454. }
  3455. InfoWindow iw;
  3456. iw.soundID = soundBase::gazebo;
  3457. iw.player = h->getOwner();
  3458. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  3459. switch (type)
  3460. {
  3461. case 0:
  3462. cb->changePrimSkill(h->id,bid,+1);
  3463. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  3464. break;
  3465. case 1:
  3466. cb->changeSecSkill(h->id,bid,+1);
  3467. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  3468. break;
  3469. case 2:
  3470. {
  3471. std::set<ui32> hlp;
  3472. hlp.insert(bid);
  3473. cb->changeSpells(h->id,true,hlp);
  3474. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  3475. }
  3476. break;
  3477. default:
  3478. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3479. return;
  3480. }
  3481. cb->showInfoDialog(&iw);
  3482. cb->removeObject(id);
  3483. }
  3484. void CGScholar::initObj()
  3485. {
  3486. blockVisit = true;
  3487. if(bonusType == 255)
  3488. {
  3489. bonusType = ran()%3;
  3490. switch(bonusType)
  3491. {
  3492. case 0:
  3493. bonusID = ran() % PRIMARY_SKILLS;
  3494. break;
  3495. case 1:
  3496. bonusID = ran() % SKILL_QUANTITY;
  3497. break;
  3498. case 2:
  3499. std::vector<ui16> possibilities;
  3500. for (int i = 1; i < 6; ++i)
  3501. cb->getAllowedSpells (possibilities, i);
  3502. bonusID = possibilities[ran() % possibilities.size()];
  3503. break;
  3504. }
  3505. }
  3506. }
  3507. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  3508. {
  3509. if (h->tempOwner != tempOwner && army.slots.size() > 0) {
  3510. //TODO: Find a way to apply magic garrison effects in battle.
  3511. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  3512. return;
  3513. }
  3514. //New owner.
  3515. if (h->tempOwner != tempOwner)
  3516. cb->setOwner(id, h->tempOwner);
  3517. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  3518. }
  3519. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  3520. {
  3521. if (result->winner == 0)
  3522. onHeroVisit(h);
  3523. }
  3524. ui8 CGGarrison::getPassableness() const
  3525. {
  3526. return 1<<tempOwner;
  3527. }
  3528. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3529. {
  3530. int sound = 0;
  3531. int txtid = -1;
  3532. switch(ID)
  3533. {
  3534. case 22: //Corpse
  3535. txtid = 37;
  3536. sound = soundBase::MYSTERY;
  3537. break;
  3538. case 39: //Lean To
  3539. sound = soundBase::GENIE;
  3540. txtid = 64;
  3541. break;
  3542. case 105://Wagon
  3543. sound = soundBase::GENIE;
  3544. txtid = 154;
  3545. break;
  3546. case 108:
  3547. break;
  3548. default:
  3549. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3550. return;
  3551. }
  3552. if(ID == 108)//Warrior's Tomb
  3553. {
  3554. //ask if player wants to search the Tomb
  3555. BlockingDialog bd(true, false);
  3556. sound = soundBase::GRAVEYARD;
  3557. bd.player = h->getOwner();
  3558. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3559. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3560. return;
  3561. }
  3562. InfoWindow iw;
  3563. iw.soundID = sound;
  3564. iw.player = h->getOwner();
  3565. if(players.size()) //we have been already visited...
  3566. {
  3567. txtid++;
  3568. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  3569. txtid++;
  3570. }
  3571. else //first visit - give bonus!
  3572. {
  3573. switch(artOrRes)
  3574. {
  3575. case 0: // first visit but empty
  3576. if (ID == 22) //Corpse
  3577. ++txtid;
  3578. else
  3579. txtid+=2;
  3580. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3581. break;
  3582. case 1: //art
  3583. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3584. cb->giveHeroArtifact(bonusType,h->id,-2);
  3585. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3586. if (ID == 22) //Corpse
  3587. {
  3588. iw.text << "%s";
  3589. iw.text.addReplacement (MetaString::ART_NAMES, bonusType);
  3590. }
  3591. break;
  3592. case 2: //res
  3593. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3594. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  3595. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  3596. break;
  3597. }
  3598. if(ID == 105 && artOrRes == 1)
  3599. {
  3600. iw.text.localStrings.back().second++;
  3601. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3602. }
  3603. }
  3604. cb->showInfoDialog(&iw);
  3605. cb->setObjProperty(id,10,h->getOwner());
  3606. }
  3607. const std::string & CGOnceVisitable::getHoverText() const
  3608. {
  3609. hoverName = VLC->generaltexth->names[ID] + " ";
  3610. hoverName += (hasVisited(cb->getCurrentPlayer())
  3611. ? (VLC->generaltexth->allTexts[352]) //visited
  3612. : ( VLC->generaltexth->allTexts[353])); //not visited
  3613. return hoverName;
  3614. }
  3615. void CGOnceVisitable::initObj()
  3616. {
  3617. switch(ID)
  3618. {
  3619. case 22: //Corpse
  3620. {
  3621. blockVisit = true;
  3622. int hlp = ran()%100;
  3623. if(hlp < 20)
  3624. {
  3625. artOrRes = 1;
  3626. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3627. }
  3628. else
  3629. {
  3630. artOrRes = 0;
  3631. }
  3632. }
  3633. break;
  3634. case 39: //Lean To
  3635. {
  3636. artOrRes = 2;
  3637. bonusType = ran()%6; //any basic resource without gold
  3638. bonusVal = ran()%4 + 1;
  3639. }
  3640. break;
  3641. case 108://Warrior's Tomb
  3642. {
  3643. artOrRes = 1;
  3644. int hlp = ran()%100;
  3645. if(hlp < 30)
  3646. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  3647. else if(hlp < 80)
  3648. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  3649. else if(hlp < 95)
  3650. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  3651. else
  3652. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  3653. }
  3654. break;
  3655. case 105://Wagon
  3656. {
  3657. int hlp = ran()%100;
  3658. if(hlp < 10)
  3659. {
  3660. artOrRes = 0; // nothing... :(
  3661. }
  3662. else if(hlp < 50) //minor or treasure art
  3663. {
  3664. artOrRes = 1;
  3665. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3666. }
  3667. else //2 - 5 of non-gold resource
  3668. {
  3669. artOrRes = 2;
  3670. bonusType = ran()%6;
  3671. bonusVal = ran()%4 + 2;
  3672. }
  3673. }
  3674. break;
  3675. }
  3676. }
  3677. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3678. {
  3679. if(accept)
  3680. {
  3681. InfoWindow iw;
  3682. iw.player = h->getOwner();
  3683. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3684. if(players.size()) //we've been already visited, player found nothing
  3685. {
  3686. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3687. }
  3688. else //first visit - give artifact
  3689. {
  3690. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3691. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3692. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3693. cb->giveHeroArtifact(bonusType,h->id,-2);
  3694. }
  3695. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3696. {
  3697. //ruin morale
  3698. GiveBonus gb;
  3699. gb.hid = h->id;
  3700. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3701. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3702. cb->giveHeroBonus(&gb);
  3703. }
  3704. cb->showInfoDialog(&iw);
  3705. cb->setObjProperty(id,10,h->getOwner());
  3706. }
  3707. }
  3708. void CBank::initObj()
  3709. {
  3710. switch (ID) //find apriopriate key
  3711. {
  3712. case 16: //bank
  3713. index = subID; break;
  3714. case 24: //derelict ship
  3715. index = 8; break;
  3716. case 25: //utopia
  3717. index = 10; break;
  3718. case 84: //crypt
  3719. index = 9; break;
  3720. case 85: //shipwreck
  3721. index = 7; break;
  3722. }
  3723. bc = NULL;
  3724. daycounter = 0;
  3725. multiplier = 1;
  3726. }
  3727. const std::string & CBank::getHoverText() const
  3728. {
  3729. hoverName = VLC->objh->creBanksNames[index];
  3730. if (bc == NULL)
  3731. hoverName += " " + VLC->generaltexth->allTexts[352];
  3732. else
  3733. hoverName += " " + VLC->generaltexth->allTexts[353];
  3734. return hoverName;
  3735. }
  3736. void CBank::reset(ui16 var1) //prevents desync
  3737. {
  3738. ui8 chance = 0;
  3739. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  3740. {
  3741. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  3742. {
  3743. bc = VLC->objh->banksInfo[index][i];
  3744. break;
  3745. }
  3746. }
  3747. artifacts.clear();
  3748. }
  3749. void CBank::initialize() const
  3750. {
  3751. cb->setObjProperty (id, 14, ran()); //synchronous reset
  3752. for (ui8 i = 0; i <= 3; i++)
  3753. {
  3754. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  3755. {
  3756. switch (i)
  3757. {
  3758. case 0:
  3759. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
  3760. break;
  3761. case 1:
  3762. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
  3763. break;
  3764. case 2:
  3765. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
  3766. break;
  3767. case 3:
  3768. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
  3769. break;
  3770. }
  3771. }
  3772. }
  3773. }
  3774. void CBank::setPropertyDer (ui8 what, ui32 val)
  3775. /// random values are passed as arguments and processed identically on all clients
  3776. {
  3777. switch (what)
  3778. {
  3779. case 11: //daycounter
  3780. if (val == 0)
  3781. daycounter = 0;
  3782. else
  3783. daycounter++;
  3784. break;
  3785. case 12: //multiplier, in percent
  3786. multiplier = ((float)val)/100;
  3787. break;
  3788. case 13: //bank preset
  3789. bc = VLC->objh->banksInfo[index][val];
  3790. break;
  3791. case 14:
  3792. reset (val%100);
  3793. break;
  3794. case 15:
  3795. bc = NULL;
  3796. break;
  3797. case 16:
  3798. artifacts.clear();
  3799. break;
  3800. case 17: //set ArmedInstance army
  3801. {
  3802. int upgraded = 0;
  3803. if (val%100 < bc->upgradeChance) //once again anti-desync
  3804. upgraded = 1;
  3805. switch (bc->guards.size())
  3806. {
  3807. case 1:
  3808. for (int i = 0; i <= 4; i++)
  3809. army.setCreature (i, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  3810. break;
  3811. case 4:
  3812. {
  3813. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3814. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3815. {
  3816. int n = army.slots.size(); //debug
  3817. army.setCreature (n, it->first, it->second);
  3818. }
  3819. }
  3820. break;
  3821. default:
  3822. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3823. return;
  3824. }
  3825. }
  3826. break;
  3827. case 18: //add Artifact
  3828. artifacts.push_back (val);
  3829. break;
  3830. }
  3831. }
  3832. void CBank::newTurn() const
  3833. {
  3834. if (bc == NULL)
  3835. {
  3836. if (cb->getDate(0) == 1)
  3837. initialize(); //initialize on first day
  3838. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  3839. {
  3840. initialize();
  3841. cb->setObjProperty (id, 11, 0); //daycounter 0
  3842. if (ID == 24 && cb->getDate(0) > 1)
  3843. cb->setObjProperty (id, 16, 0); //derelict ships are usable only once
  3844. }
  3845. else
  3846. cb->setObjProperty (id, 11, 1); //daycounter++
  3847. }
  3848. }
  3849. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3850. {
  3851. if (bc != NULL)
  3852. {
  3853. int banktext = 0;
  3854. switch (ID)
  3855. {
  3856. case 16: //generic bank
  3857. banktext = 32;
  3858. break;
  3859. case 24:
  3860. banktext = 41;
  3861. break;
  3862. case 25: //utopia
  3863. banktext = 47;
  3864. break;
  3865. case 84: //crypt
  3866. banktext = 119;
  3867. break;
  3868. case 85: //shipwreck
  3869. banktext = 122;
  3870. break;
  3871. }
  3872. BlockingDialog bd (true, false);
  3873. bd.player = h->getOwner();
  3874. bd.soundID = soundBase::DANGER;
  3875. bd.text << VLC->generaltexth->advobtxt[banktext];
  3876. if (ID == 16)
  3877. bd.text.addReplacement (VLC->objh->creBanksNames[index]);
  3878. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3879. }
  3880. else
  3881. {
  3882. InfoWindow iw;
  3883. iw.soundID = soundBase::GRAVEYARD;
  3884. iw.player = h->getOwner();
  3885. //if (ID == 16 || ID == 24 || ID == 25 || ID == 84)
  3886. iw.text << VLC->generaltexth->advobtxt[33];
  3887. iw.text.addReplacement (VLC->objh->creBanksNames[index]);
  3888. cb->showInfoDialog(&iw);
  3889. }
  3890. }
  3891. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3892. {
  3893. if (accept)
  3894. {
  3895. cb->setObjProperty (id, 17, ran()); //get army
  3896. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  3897. }
  3898. }
  3899. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  3900. {
  3901. if (result->winner == 0)
  3902. {
  3903. int textID = -1;
  3904. InfoWindow iw;
  3905. iw.player = h->getOwner();
  3906. MetaString loot;
  3907. switch (ID)
  3908. {
  3909. case 16: case 25:
  3910. textID = 34;
  3911. break;
  3912. case 24: //derelict ship
  3913. if (bc->resources.size() != 0)
  3914. textID = 43;
  3915. else
  3916. {
  3917. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3918. GiveBonus gbonus;
  3919. gbonus.hid = h->id;
  3920. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3921. gbonus.bonus.source = HeroBonus::OBJECT;
  3922. gbonus.bonus.id = ID;
  3923. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  3924. gbonus.bonus.type = HeroBonus::MORALE;
  3925. gbonus.bonus.val = -2;
  3926. cb->giveHeroBonus(&gbonus);
  3927. textID = 42;
  3928. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3929. }
  3930. break;
  3931. case 84: //Crypt
  3932. if (bc->resources.size() != 0)
  3933. textID = 121;
  3934. else
  3935. {
  3936. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  3937. GiveBonus gbonus;
  3938. gbonus.hid = h->id;
  3939. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3940. gbonus.bonus.source = HeroBonus::OBJECT;
  3941. gbonus.bonus.id = ID;
  3942. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  3943. gbonus.bonus.type = HeroBonus::MORALE;
  3944. gbonus.bonus.val = -1;
  3945. cb->giveHeroBonus(&gbonus);
  3946. textID = 120;
  3947. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  3948. }
  3949. break;
  3950. case 85: //shipwreck
  3951. if (bc->resources.size() != 0)
  3952. textID = 124;
  3953. else
  3954. {
  3955. textID = 123;
  3956. }
  3957. break;
  3958. }
  3959. //grant resources
  3960. for (int it = 0; it < bc->resources.size(); it++)
  3961. {
  3962. if (bc->resources[it] != 0)
  3963. {
  3964. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  3965. loot << "%d %s";
  3966. loot.addReplacement (iw.components.back().val);
  3967. loot.addReplacement (MetaString::RES_NAMES, iw.components.back().subtype);
  3968. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  3969. }
  3970. }
  3971. //grant artifacts
  3972. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  3973. {
  3974. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3975. loot << "%s";
  3976. loot.addReplacement (MetaString::ART_NAMES, *it);
  3977. cb->giveHeroArtifact (*it, h->id ,-2);
  3978. }
  3979. //display loot
  3980. if (!loot.message.empty())
  3981. {
  3982. if (textID == 34)
  3983. {
  3984. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  3985. iw.text.addReplacement (MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  3986. iw.text.addReplacement (loot.buildList());
  3987. }
  3988. else
  3989. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  3990. cb->showInfoDialog(&iw);
  3991. }
  3992. loot.clear();
  3993. iw.components.clear();
  3994. iw.text.clear();
  3995. //grant creatures
  3996. CCreatureSet ourArmy;
  3997. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  3998. {
  3999. int slot = ourArmy.getSlotFor (it->first);
  4000. ourArmy.slots[slot].first = it->first;
  4001. ourArmy.slots[slot].second += it->second;
  4002. }
  4003. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  4004. {
  4005. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  4006. loot << "%s";
  4007. if (i->second.second == 1)
  4008. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  4009. else
  4010. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  4011. }
  4012. if (ourArmy.slots.size())
  4013. {
  4014. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  4015. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4016. else
  4017. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4018. iw.text.addReplacement (loot.buildList());
  4019. iw.text.addReplacement (h->name);
  4020. cb->showInfoDialog(&iw);
  4021. cb->giveCreatures (id, h, ourArmy);
  4022. }
  4023. cb->setObjProperty (id, 15, 0); //bc = NULL
  4024. }
  4025. else //in case of defeat
  4026. initialize();
  4027. }
  4028. void CGPyramid::initObj()
  4029. {
  4030. //would be nice to do that only once
  4031. if (!pyramidConfig.guards.size())
  4032. {
  4033. pyramidConfig.level = 1;
  4034. pyramidConfig.chance = 100;
  4035. pyramidConfig.upgradeChance = 0;
  4036. for (int i = 0; i < 2; ++i)
  4037. {
  4038. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(116, 20));
  4039. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(117, 10));
  4040. }
  4041. pyramidConfig.combatValue; //how hard are guards of this level
  4042. pyramidConfig.value; //overall value of given things
  4043. pyramidConfig.rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  4044. pyramidConfig.easiest; //?!?
  4045. }
  4046. bc = &pyramidConfig;
  4047. std::vector<ui16> available;
  4048. cb->getAllowedSpells (available, 5);
  4049. spell = (available[rand()%available.size()]);
  4050. }
  4051. const std::string & CGPyramid::getHoverText() const
  4052. {
  4053. hoverName = VLC->generaltexth->names[ID];
  4054. if (bc == NULL)
  4055. hoverName += " " + VLC->generaltexth->allTexts[352];
  4056. else
  4057. hoverName += " " + VLC->generaltexth->allTexts[353];
  4058. return hoverName;
  4059. }
  4060. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  4061. {
  4062. if (bc)
  4063. {
  4064. BlockingDialog bd (true, false);
  4065. bd.player = h->getOwner();
  4066. bd.soundID = soundBase::DANGER;
  4067. bd.text << VLC->generaltexth->advobtxt[105];
  4068. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4069. }
  4070. else
  4071. {
  4072. InfoWindow iw;
  4073. iw.player = h->getOwner();
  4074. iw.text << VLC->generaltexth->advobtxt[107];
  4075. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  4076. GiveBonus gb;
  4077. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  4078. gb.hid = h->id;
  4079. cb->giveHeroBonus(&gb);
  4080. cb->showInfoDialog(&iw);
  4081. }
  4082. }
  4083. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4084. {
  4085. if (result->winner == 0)
  4086. {
  4087. InfoWindow iw;
  4088. iw.player = h->getOwner();
  4089. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  4090. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  4091. if (!h->getArt(17))
  4092. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  4093. else if (h->getSecSkillLevel(7) < 3)
  4094. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  4095. else
  4096. {
  4097. std::set<ui32> spells;
  4098. spells.insert (spell);
  4099. cb->changeSpells (h->id, true, spells);
  4100. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  4101. }
  4102. cb->showInfoDialog(&iw);
  4103. cb->setObjProperty (id, 15, 0);
  4104. }
  4105. }
  4106. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  4107. {
  4108. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  4109. playerKeyMap.find(what-101)->second.insert(val);
  4110. }
  4111. bool CGKeys::wasMyColorVisited (int player) const
  4112. {
  4113. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  4114. return true;
  4115. else
  4116. return false;
  4117. }
  4118. const std::string & CGKeymasterTent::getHoverText() const
  4119. {
  4120. hoverName = VLC->generaltexth->names[ID];
  4121. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4122. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4123. else
  4124. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4125. return hoverName;
  4126. }
  4127. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  4128. {
  4129. InfoWindow iw;
  4130. iw.soundID = soundBase::CAVEHEAD;
  4131. iw.player = h->getOwner();
  4132. if (!wasMyColorVisited (h->getOwner()) )
  4133. {
  4134. cb->setObjProperty(id, h->tempOwner+101, subID);
  4135. iw.text << std::pair<ui8,ui32>(11,19);
  4136. }
  4137. else
  4138. iw.text << std::pair<ui8,ui32>(11,20);
  4139. cb->showInfoDialog(&iw);
  4140. }
  4141. void CGBorderGuard::initObj()
  4142. {
  4143. blockVisit = true;
  4144. }
  4145. const std::string & CGBorderGuard::getHoverText() const
  4146. {
  4147. hoverName = VLC->generaltexth->names[ID];
  4148. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4149. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4150. else
  4151. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4152. return hoverName;
  4153. }
  4154. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  4155. {
  4156. if (wasMyColorVisited (h->getOwner()) )
  4157. {
  4158. BlockingDialog bd (true, false);
  4159. bd.player = h->getOwner();
  4160. bd.soundID = soundBase::QUEST;
  4161. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  4162. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  4163. }
  4164. else
  4165. {
  4166. InfoWindow iw;
  4167. iw.player = h->getOwner();
  4168. iw.soundID = soundBase::CAVEHEAD;
  4169. iw.text << std::pair<ui8,ui32>(11,18);
  4170. cb->showInfoDialog (&iw);
  4171. }
  4172. }
  4173. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  4174. {
  4175. if (accept)
  4176. cb->removeObject(id);
  4177. }
  4178. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  4179. {
  4180. if (!wasMyColorVisited (h->getOwner()) )
  4181. {
  4182. InfoWindow iw;
  4183. iw.player = h->getOwner();
  4184. iw.text << std::pair<ui8,ui32>(11,18);
  4185. cb->showInfoDialog(&iw);
  4186. }
  4187. }
  4188. ui8 CGBorderGate::getPassableness() const
  4189. {
  4190. ui8 ret = 0;
  4191. for (int i = 0; i < PLAYER_LIMIT; i++)
  4192. ret |= wasMyColorVisited(i)<<i;
  4193. return ret;
  4194. }
  4195. void CGMagi::initObj()
  4196. {
  4197. if (ID == 27)
  4198. {
  4199. blockVisit = true;
  4200. eyelist[subID].push_back(id);
  4201. }
  4202. }
  4203. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  4204. {
  4205. if (ID == 37)
  4206. {
  4207. InfoWindow iw;
  4208. CenterView cv;
  4209. FoWChange fw;
  4210. cv.player = iw.player = fw.player = h->tempOwner;
  4211. iw.soundID = soundBase::LIGHTHOUSE;
  4212. iw.player = h->tempOwner;
  4213. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  4214. cb->showInfoDialog(&iw);
  4215. fw.mode = 1;
  4216. std::vector<si32>::iterator it;
  4217. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  4218. {
  4219. const CGObjectInstance *eye = cb->getObj(*it);
  4220. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  4221. cb->sendAndApply(&fw);
  4222. cv.pos = eye->pos;
  4223. cv.focusTime = 2000;
  4224. cb->sendAndApply(&cv);
  4225. }
  4226. cv.pos = h->getPosition(false);
  4227. cb->sendAndApply(&cv);
  4228. }
  4229. else if (ID == 27)
  4230. {
  4231. InfoWindow iw;
  4232. iw.player = h->tempOwner;
  4233. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  4234. cb->showInfoDialog(&iw);
  4235. }
  4236. }
  4237. void CGBoat::initObj()
  4238. {
  4239. hero = NULL;
  4240. }
  4241. void CGSirens::initObj()
  4242. {
  4243. blockVisit = true;
  4244. }
  4245. const std::string & CGSirens::getHoverText() const
  4246. {
  4247. getNameVis(hoverName);
  4248. return hoverName;
  4249. }
  4250. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  4251. {
  4252. int message;
  4253. InfoWindow iw;
  4254. iw.soundID = soundBase::DANGER;
  4255. iw.player = h->tempOwner;
  4256. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  4257. {
  4258. iw.text.addTxt(11,133);
  4259. }
  4260. else
  4261. {
  4262. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  4263. int xp = 0;
  4264. SetGarrisons sg;
  4265. sg.garrs[h->id] = h->army;
  4266. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  4267. {
  4268. int drown = (int)(i->second.second * 0.3);
  4269. if(drown)
  4270. {
  4271. sg.garrs[h->id].slots[i->first].second -= drown;
  4272. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  4273. }
  4274. }
  4275. if(xp)
  4276. {
  4277. iw.text.addTxt(11,132);
  4278. iw.text.addReplacement(xp);
  4279. cb->sendAndApply(&sg);
  4280. }
  4281. else
  4282. {
  4283. iw.text.addTxt(11,134);
  4284. }
  4285. }
  4286. cb->showInfoDialog(&iw);
  4287. }
  4288. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  4289. {
  4290. cost.resize(RESOURCE_QUANTITY);
  4291. cost[0] = 10;
  4292. cost[6] = 1000;
  4293. }
  4294. //bool IShipyard::validLocation() const
  4295. //{
  4296. // std::vector<int3> offsets;
  4297. // getOutOffsets(offsets);
  4298. //
  4299. // TerrainTile *tile;
  4300. // for(int i = 0; i < offsets.size(); i++)
  4301. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4302. // return true;
  4303. // return false;
  4304. //}
  4305. int3 IShipyard::bestLocation() const
  4306. {
  4307. std::vector<int3> offsets;
  4308. getOutOffsets(offsets);
  4309. TerrainTile *tile;
  4310. for(int i = 0; i < offsets.size(); i++)
  4311. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4312. return o->pos + offsets[i];
  4313. return int3(-1,-1,-1);
  4314. }
  4315. IShipyard::IShipyard(const CGObjectInstance *O)
  4316. : o(O)
  4317. {
  4318. }
  4319. int IShipyard::state() const
  4320. {
  4321. int3 tile = bestLocation();
  4322. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  4323. if(!t)
  4324. return 3; //no water
  4325. else if(!t->blockingObjects.size())
  4326. return 0; //OK
  4327. else if(t->blockingObjects.front()->ID == 8)
  4328. return 1; //blocked with boat
  4329. else
  4330. return 2; //blocked
  4331. }
  4332. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  4333. {
  4334. if(obj->ID == TOWNI_TYPE)
  4335. {
  4336. return static_cast<CGTownInstance*>(obj);
  4337. }
  4338. else if(obj->ID == 87)
  4339. {
  4340. return static_cast<CGShipyard*>(obj);
  4341. }
  4342. else
  4343. {
  4344. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  4345. return NULL;
  4346. }
  4347. }
  4348. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  4349. {
  4350. return castFrom(const_cast<CGObjectInstance*>(obj));
  4351. }
  4352. CGShipyard::CGShipyard()
  4353. :IShipyard(this)
  4354. {
  4355. }
  4356. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  4357. {
  4358. 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),
  4359. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  4360. }
  4361. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  4362. {
  4363. if(tempOwner != h->tempOwner)
  4364. cb->setOwner(id, h->tempOwner);
  4365. int s = state();
  4366. if(s)
  4367. {
  4368. InfoWindow iw;
  4369. iw.player = tempOwner;
  4370. switch(s)
  4371. {
  4372. case 1:
  4373. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  4374. break;
  4375. case 2:
  4376. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  4377. break;
  4378. case 3:
  4379. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  4380. return;
  4381. }
  4382. cb->showInfoDialog(&iw);
  4383. }
  4384. else
  4385. {
  4386. OpenWindow ow;
  4387. ow.id1 = id;
  4388. ow.id2 = h->id;
  4389. ow.window = OpenWindow::SHIPYARD_WINDOW;
  4390. cb->sendAndApply(&ow);
  4391. }
  4392. }
  4393. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  4394. {
  4395. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  4396. {
  4397. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  4398. {
  4399. //ask if he wants to buy one
  4400. int text;
  4401. if (cb->getTile(pos)->tertype == 8) //water
  4402. text = 25;
  4403. else
  4404. {
  4405. if (pos.z == 0)
  4406. text = 26;
  4407. else
  4408. text = 27;
  4409. }
  4410. BlockingDialog bd (true, false);
  4411. bd.player = h->getOwner();
  4412. bd.soundID = soundBase::LIGHTHOUSE;
  4413. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  4414. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  4415. }
  4416. else //if he cannot afford
  4417. {
  4418. InfoWindow iw;
  4419. iw.player = h->getOwner();
  4420. iw.soundID = soundBase::CAVEHEAD;
  4421. iw.text << std::pair<ui8,ui32>(11,28);
  4422. cb->showInfoDialog (&iw);
  4423. }
  4424. }
  4425. else //if he already visited carographer
  4426. {
  4427. InfoWindow iw;
  4428. iw.player = h->getOwner();
  4429. iw.soundID = soundBase::CAVEHEAD;
  4430. iw.text << std::pair<ui8,ui32>(11,24);
  4431. cb->showInfoDialog (&iw);
  4432. }
  4433. }
  4434. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  4435. {
  4436. if (accept) //if hero wants to buy map
  4437. {
  4438. cb->giveResource (h->tempOwner, 6, -1000);
  4439. FoWChange fw;
  4440. fw.player = h->tempOwner;
  4441. //subIDs of different types of cartographers:
  4442. //water = 0; land = 1; underground = 2;
  4443. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  4444. cb->sendAndApply (&fw);
  4445. cb->setObjProperty (id, 10, h->tempOwner);
  4446. }
  4447. }
  4448. void CShop::newTurn() const
  4449. {
  4450. switch (ID)
  4451. {
  4452. case 7: //ArtMerchant aka. Black Market
  4453. if (cb->getDate(0)%28 == 1)
  4454. {
  4455. cb->setObjProperty (id, 13, 0);
  4456. cb->setObjProperty (id, 14, rand());
  4457. }
  4458. break;
  4459. case 78: //Refugee Camp
  4460. case 95: //Tavern -- global hero pool?
  4461. if (cb->getDate(0)%7 == 1)
  4462. cb->setObjProperty (id, 14, rand());
  4463. break;
  4464. }
  4465. }
  4466. void CShop::setPropertyDer (ui8 what, ui32 val)
  4467. {
  4468. switch (what)
  4469. {
  4470. case 13: //sweep
  4471. available.clear();
  4472. chosen.clear();
  4473. bought.clear();
  4474. break;
  4475. case 14: //reset - get new items
  4476. reset(val);
  4477. break;
  4478. }
  4479. }
  4480. void CGArtMerchant::reset(ui32 val)
  4481. {
  4482. std::vector<CArtifact*>::iterator index;
  4483. for (ui8 i = 0; i < 4; ++i) //each tier
  4484. {
  4485. int count = 0;
  4486. std::vector<CArtifact*> arts; //to avoid addition of different tiers
  4487. switch (i)
  4488. {
  4489. case 0:
  4490. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  4491. count = 2;
  4492. break;
  4493. case 1:
  4494. cb->getAllowed (arts, CArtifact::ART_MINOR);
  4495. count = 2;
  4496. break;
  4497. case 2:
  4498. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  4499. count = 1;
  4500. break;
  4501. case 3:
  4502. cb->getAllowed (arts, CArtifact::ART_RELIC);
  4503. count = 1;
  4504. break;
  4505. }
  4506. for (ui8 n = 0; n < count; n++)
  4507. {
  4508. index = arts.begin() + val % arts.size();
  4509. available [available.size()] = new Component (Component::ARTIFACT, (*index)->id, 0, 0);
  4510. arts.erase(index);
  4511. val *= (id + n * i); //randomize
  4512. }
  4513. }
  4514. }
  4515. void CGRefugeeCamp::reset(ui32 val)
  4516. {
  4517. /*int creid = creh->creatures[val%creh->creatures.size()].idNumber;
  4518. VLC->creh->creatures[creatures[creid].second[0]].growth;
  4519. available[0] = new Component (Component::CREATURE, creid, 0, 0);
  4520. */
  4521. }