CObjectHandler.cpp 159 KB

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