CObjectHandler.cpp 137 KB

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