CObjectHandler.cpp 129 KB

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