CObjectHandler.cpp 152 KB

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