CObjectHandler.cpp 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198
  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(getHordeLevel(0)==level)
  1402. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1403. ret+=VLC->creh->creatures[town->basicCreatures[level]]->hordeGrowth;
  1404. if(getHordeLevel(1)==level)
  1405. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1406. ret+=VLC->creh->creatures[town->basicCreatures[level]]->hordeGrowth;
  1407. //support for legs of legion etc.
  1408. if(garrisonHero)
  1409. ret += garrisonHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1410. if(visitingHero)
  1411. ret += visitingHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1412. for (std::vector<CGDwelling*>::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1413. { //+1 for each dwelling
  1414. if (VLC->creh->creatures[town->basicCreatures[level]]->idNumber == (*it)->creatures[0].second[0])
  1415. ++ret;
  1416. }
  1417. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1418. ret*=1.5;
  1419. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1420. }
  1421. int CGTownInstance::dailyIncome() const
  1422. {
  1423. int ret = 0;
  1424. if ((builtBuildings.find(26))!=builtBuildings.end())
  1425. ret+=5000;
  1426. if ((builtBuildings.find(13))!=builtBuildings.end())
  1427. ret+=4000;
  1428. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1429. ret+=2000;
  1430. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1431. ret+=1000;
  1432. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1433. ret+=500;
  1434. return ret;
  1435. }
  1436. bool CGTownInstance::hasFort() const
  1437. {
  1438. return (builtBuildings.find(7))!=builtBuildings.end();
  1439. }
  1440. bool CGTownInstance::hasCapitol() const
  1441. {
  1442. return (builtBuildings.find(13))!=builtBuildings.end();
  1443. }
  1444. CGTownInstance::CGTownInstance()
  1445. :IShipyard(this), IMarket(this)
  1446. {
  1447. builded=-1;
  1448. destroyed=-1;
  1449. garrisonHero=NULL;
  1450. town=NULL;
  1451. visitingHero = NULL;
  1452. }
  1453. CGTownInstance::~CGTownInstance()
  1454. {
  1455. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1456. delete *i;
  1457. }
  1458. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1459. {
  1460. if(checkGuild && mageGuildLevel() < level)
  1461. return 0;
  1462. int ret = 6 - level; //how many spells are available at this level
  1463. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1464. ret++;
  1465. return ret;
  1466. }
  1467. int CGTownInstance::defenceBonus(int type) const
  1468. {
  1469. int ret=0;
  1470. switch (type)
  1471. {
  1472. /*attack*/ case 0:
  1473. if (subID == 6 && vstd::contains(builtBuildings,26))//Stronghold, grail
  1474. ret += 12;
  1475. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, grail
  1476. ret += 10;
  1477. if (subID == 7 && vstd::contains(builtBuildings,22))//Fortress, Blood Obelisk
  1478. ret += 2;
  1479. return ret;
  1480. /*defence*/ case 1:
  1481. if (subID == 7 && vstd::contains(builtBuildings,21))//Fortress, Glyphs of Fear
  1482. ret += 2;
  1483. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, Grail
  1484. ret += 10;
  1485. return ret;
  1486. /*spellpower*/ case 2:
  1487. if (subID == 3 && vstd::contains(builtBuildings,21))//Inferno, Brimstone Clouds
  1488. ret += 2;
  1489. if (subID == 5 && vstd::contains(builtBuildings,26))//Dungeon, Grail
  1490. ret += 12;
  1491. return ret;
  1492. /*knowledge*/ case 3:
  1493. if (subID == 2 && vstd::contains(builtBuildings,26))//Tower, Grail
  1494. ret += 15;
  1495. return ret;
  1496. }
  1497. return 0;//Why we are here? wrong type?
  1498. }
  1499. bool CGTownInstance::needsLastStack() const
  1500. {
  1501. if(garrisonHero)
  1502. return true;
  1503. else return false;
  1504. }
  1505. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1506. {
  1507. if(getOwner() != h->getOwner())
  1508. {
  1509. //TODO ally check
  1510. if(stacksCount() > 0 || visitingHero)
  1511. {
  1512. const CGHeroInstance *defendingHero = NULL;
  1513. if(visitingHero)
  1514. defendingHero = visitingHero;
  1515. else if(garrisonHero)
  1516. defendingHero = garrisonHero;
  1517. const CArmedInstance *defendingArmy = this;
  1518. if(defendingHero)
  1519. defendingArmy = defendingHero;
  1520. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1521. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1522. }
  1523. else
  1524. {
  1525. cb->setOwner(id, h->tempOwner);
  1526. removeCapitols (h->getOwner());
  1527. cb->heroVisitCastle(id, h->id);
  1528. }
  1529. }
  1530. else
  1531. cb->heroVisitCastle(id, h->id);
  1532. }
  1533. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1534. {
  1535. cb->stopHeroVisitCastle(id,h->id);
  1536. }
  1537. void CGTownInstance::initObj()
  1538. ///initialize town structures
  1539. {
  1540. blockVisit = true;
  1541. hoverName = name + ", " + town->Name();
  1542. creatures.resize(CREATURES_PER_TOWN);
  1543. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1544. {
  1545. if(creatureDwelling(i,false))
  1546. creatures[i].second.push_back(town->basicCreatures[i]);
  1547. if(creatureDwelling(i,true))
  1548. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1549. }
  1550. switch (subID)
  1551. { //add new visitable objects
  1552. case 0:
  1553. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1554. break;
  1555. case 2: case 3: case 5: case 6:
  1556. bonusingBuildings.push_back (new CTownBonus(23, this));
  1557. if (subID == 5)
  1558. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1559. break;
  1560. case 7:
  1561. bonusingBuildings.push_back (new CTownBonus(17, this));
  1562. break;
  1563. }
  1564. //add special bonuses from buildings
  1565. if(subID == 4 && vstd::contains(builtBuildings, 17))
  1566. {
  1567. bonuses.push_back( Bonus(Bonus::PERMANENT, Bonus::DARKNESS, Bonus::TOWN_STRUCTURE, 20, 17) );
  1568. }
  1569. }
  1570. void CGTownInstance::newTurn() const
  1571. {
  1572. if (cb->getDate(1) == 1) //reset on new week
  1573. {
  1574. if (vstd::contains(builtBuildings,17) && subID == 1 && cb->getDate(0) != 1 && (tempOwner < PLAYER_LIMIT) )//give resources for Rampart, Mystic Pond
  1575. {
  1576. int resID = rand()%4+2;//bonus to random rare resource
  1577. resID = (resID==2)?1:resID;
  1578. int resVal = rand()%4+1;//with size 1..4
  1579. cb->giveResource(tempOwner, resID, resVal);
  1580. cb->setObjProperty (id, 14, resID);
  1581. cb->setObjProperty (id, 15, resVal);
  1582. }
  1583. if ( subID == 5 )
  1584. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1585. {
  1586. if ((*i)->ID == 21)
  1587. cb->setObjProperty (id, 12, (*i)->id); //reset visitors for Mana Vortex
  1588. }
  1589. }
  1590. }
  1591. int3 CGTownInstance::getSightCenter() const
  1592. {
  1593. return pos - int3(2,0,0);
  1594. }
  1595. ui8 CGTownInstance::getPassableness() const
  1596. {
  1597. return stacksCount() ? 1<<tempOwner : ALL_PLAYERS; //if there is garrison army, castle be entered only by owner //TODO: allies
  1598. }
  1599. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1600. {
  1601. offsets += int3(-1,2,0), int3(-3,2,0);
  1602. }
  1603. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1604. {
  1605. if(result->winner == 0)
  1606. {
  1607. if (hasBonusOfType(Bonus::DARKNESS))
  1608. {
  1609. //TODO: Make some 'change owner' function for bonus, or bonuses independent of player
  1610. /*
  1611. RemoveBonus rb(RemoveBonus::PLAYER);
  1612. rb.whoID = getOwner();
  1613. rb.source = Bonus::TOWN_STRUCTURE;
  1614. rb.id = id;
  1615. cb->sendAndApply(&rb);
  1616. GiveBonus gb(GiveBonus::PLAYER);
  1617. gb.bonus.type = Bonus::DARKNESS;
  1618. gb.bonus.val = 20;
  1619. gb.id = h->tempOwner;
  1620. gb.bonus.duration = Bonus::PERMANENT;
  1621. gb.bonus.source = Bonus::TOWN_STRUCTURE;
  1622. gb.bonus.id = id;
  1623. cb->sendAndApply(&gb);
  1624. */
  1625. }
  1626. removeCapitols (h->getOwner());
  1627. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1628. FoWChange fw;
  1629. fw.player = h->tempOwner;
  1630. fw.mode = 1;
  1631. getSightTiles (fw.tiles); //update visibility for castle structures
  1632. cb->sendAndApply (&fw);
  1633. }
  1634. }
  1635. void CGTownInstance::removeCapitols (ui8 owner) const
  1636. {
  1637. if (hasCapitol()) // search if there's an older capitol
  1638. {
  1639. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1640. for (std::vector<CGTownInstance*>::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1641. {
  1642. if (*i != this && (*i)->hasCapitol())
  1643. {
  1644. RazeStructures rs;
  1645. rs.tid = id;
  1646. rs.bid.insert(13);
  1647. rs.destroyed = destroyed;
  1648. cb->sendAndApply(&rs);
  1649. return;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. int CGTownInstance::getBoatType() const
  1655. {
  1656. const CCreature *c = VLC->creh->creatures[town->basicCreatures.front()];
  1657. if (c->isGood())
  1658. return 1;
  1659. else if (c->isEvil())
  1660. return 0;
  1661. else //neutral
  1662. return 2;
  1663. }
  1664. void CGTownInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  1665. {
  1666. CArmedInstance::getParents(out, root);
  1667. if(root == this && visitingHero && visitingHero != root)
  1668. out.insert(visitingHero);
  1669. }
  1670. void CGTownInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  1671. {
  1672. CArmedInstance::getBonuses(out, selector, root);
  1673. //TODO eliminate by moving structures effects to bonus system
  1674. if(Selector::matchesType(selector, Bonus::LUCK))
  1675. {
  1676. if(subID == 1 && vstd::contains(builtBuildings,21)) //rampart, fountain of fortune
  1677. out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::TOWN_STRUCTURE, +2, 21, VLC->generaltexth->buildings[1][21].first + " +2"));
  1678. }
  1679. if(Selector::matchesType(selector, Bonus::MORALE))
  1680. {
  1681. if(subID == 0 && vstd::contains(builtBuildings,22)) //castle, brotherhood of sword built
  1682. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +2, 22, VLC->generaltexth->buildings[0][22].first + " +2"));
  1683. else if(vstd::contains(builtBuildings,5)) //tavern is built
  1684. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +1, 5, VLC->generaltexth->buildings[0][5].first + " +1"));
  1685. }
  1686. }
  1687. int CGTownInstance::getMarketEfficiency() const
  1688. {
  1689. if(!vstd::contains(builtBuildings, 14))
  1690. return 0;
  1691. const PlayerState *p = cb->getPlayerState(tempOwner);
  1692. assert(p);
  1693. int marketCount = 0;
  1694. BOOST_FOREACH(const CGTownInstance *t, p->towns)
  1695. if(vstd::contains(t->builtBuildings, 14))
  1696. marketCount++;
  1697. return marketCount;
  1698. }
  1699. bool CGTownInstance::allowsTrade(EMarketMode mode) const
  1700. {
  1701. switch(mode)
  1702. {
  1703. case RESOURCE_RESOURCE:
  1704. case RESOURCE_PLAYER:
  1705. return vstd::contains(builtBuildings, 14); // marketplace
  1706. case ARTIFACT_RESOURCE:
  1707. return (subID == 2 || subID == 5 || subID == 8) && vstd::contains(builtBuildings, 17);//artifact merchants
  1708. case CREATURE_RESOURCE:
  1709. return subID == 6 && vstd::contains(builtBuildings, 21); //Freelancer's guild
  1710. default:
  1711. assert(0);
  1712. return false;
  1713. }
  1714. }
  1715. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1716. {
  1717. if(visitors.find(h->id)==visitors.end())
  1718. {
  1719. onNAHeroVisit(h->id, false);
  1720. switch(ID)
  1721. {
  1722. case 102: //tree
  1723. case 4: //arena
  1724. case 41://library
  1725. case 47: //School of Magic
  1726. case 107://School of War
  1727. break;
  1728. default:
  1729. cb->setObjProperty(id, ObjProperty::VISITORS, h->id); //add to the visitors
  1730. break;
  1731. }
  1732. }
  1733. else
  1734. {
  1735. onNAHeroVisit(h->id, true);
  1736. }
  1737. }
  1738. void CGVisitableOPH::initObj()
  1739. {
  1740. if(ID==102)
  1741. ttype = ran()%3;
  1742. else
  1743. ttype = -1;
  1744. }
  1745. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1746. {
  1747. if(result) //player agreed to give res for exp
  1748. {
  1749. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1750. cb->changePrimSkill(heroID,4,expVal); //give exp
  1751. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  1752. }
  1753. }
  1754. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1755. {
  1756. int id=0, subid=0, ot=0, val=1, sound = 0;
  1757. switch(ID)
  1758. {
  1759. case 4: //arena
  1760. sound = soundBase::NOMAD;
  1761. ot = 0;
  1762. break;
  1763. case 51: //mercenary camp
  1764. sound = soundBase::NOMAD;
  1765. subid=0;
  1766. ot=80;
  1767. break;
  1768. case 23: //marletto tower
  1769. sound = soundBase::NOMAD;
  1770. subid=1;
  1771. ot=39;
  1772. break;
  1773. case 61:
  1774. sound = soundBase::gazebo;
  1775. subid=2;
  1776. ot=100;
  1777. break;
  1778. case 32:
  1779. sound = soundBase::GETPROTECTION;
  1780. subid=3;
  1781. ot=59;
  1782. break;
  1783. case 100:
  1784. sound = soundBase::gazebo;
  1785. id=5;
  1786. ot=143;
  1787. val=1000;
  1788. break;
  1789. case 102:
  1790. sound = soundBase::gazebo;
  1791. id = 5;
  1792. subid = 1;
  1793. ot = 146;
  1794. val = 1;
  1795. break;
  1796. case 41:
  1797. sound = soundBase::gazebo;
  1798. ot = 66;
  1799. break;
  1800. case 47: //School of Magic
  1801. sound = soundBase::faerie;
  1802. ot = 71;
  1803. break;
  1804. case 107://School of War
  1805. sound = soundBase::MILITARY;
  1806. ot = 158;
  1807. break;
  1808. }
  1809. if (!alreadyVisited)
  1810. {
  1811. switch (ID)
  1812. {
  1813. case 4: //arena
  1814. {
  1815. BlockingDialog sd(false,true);
  1816. sd.soundID = sound;
  1817. sd.text << std::pair<ui8,ui32>(11,ot);
  1818. sd.components.push_back(Component(0,0,2,0));
  1819. sd.components.push_back(Component(0,1,2,0));
  1820. sd.player = cb->getOwner(heroID);
  1821. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1822. return;
  1823. }
  1824. case 51:
  1825. case 23:
  1826. case 61:
  1827. case 32:
  1828. {
  1829. cb->changePrimSkill(heroID,subid,val);
  1830. InfoWindow iw;
  1831. iw.soundID = sound;
  1832. iw.components.push_back(Component(0,subid,val,0));
  1833. iw.text << std::pair<ui8,ui32>(11,ot);
  1834. iw.player = cb->getOwner(heroID);
  1835. cb->showInfoDialog(&iw);
  1836. break;
  1837. }
  1838. case 100: //give exp
  1839. {
  1840. InfoWindow iw;
  1841. iw.soundID = sound;
  1842. iw.components.push_back(Component(id,subid,val,0));
  1843. iw.player = cb->getOwner(heroID);
  1844. iw.text << std::pair<ui8,ui32>(11,ot);
  1845. iw.soundID = soundBase::gazebo;
  1846. cb->showInfoDialog(&iw);
  1847. cb->changePrimSkill(heroID,4,val);
  1848. break;
  1849. }
  1850. case 102://tree
  1851. {
  1852. const CGHeroInstance *h = cb->getHero(heroID);
  1853. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1854. if(!ttype)
  1855. {
  1856. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  1857. InfoWindow iw;
  1858. iw.soundID = sound;
  1859. iw.components.push_back(Component(id,subid,1,0));
  1860. iw.player = cb->getOwner(heroID);
  1861. iw.text << std::pair<ui8,ui32>(11,148);
  1862. cb->showInfoDialog(&iw);
  1863. cb->changePrimSkill(heroID,4,val);
  1864. break;
  1865. }
  1866. else
  1867. {
  1868. int res, resval;
  1869. if(ttype==1)
  1870. {
  1871. res = 6;
  1872. resval = 2000;
  1873. ot = 149;
  1874. }
  1875. else
  1876. {
  1877. res = 5;
  1878. resval = 10;
  1879. ot = 151;
  1880. }
  1881. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1882. {
  1883. ot++;
  1884. InfoWindow iw;
  1885. iw.soundID = sound;
  1886. iw.player = h->tempOwner;
  1887. iw.text << std::pair<ui8,ui32>(11,ot);
  1888. cb->showInfoDialog(&iw);
  1889. return;
  1890. }
  1891. BlockingDialog sd (true, false);
  1892. sd.soundID = sound;
  1893. sd.player = cb->getOwner(heroID);
  1894. sd.text << std::pair<ui8,ui32>(11,ot);
  1895. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  1896. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1897. }
  1898. break;
  1899. }
  1900. case 41://library of enlightenment
  1901. {
  1902. const CGHeroInstance *h = cb->getHero(heroID);
  1903. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1904. {
  1905. InfoWindow iw;
  1906. iw.soundID = sound;
  1907. iw.player = cb->getOwner(heroID);
  1908. iw.text << std::pair<ui8,ui32>(11,68);
  1909. cb->showInfoDialog(&iw);
  1910. }
  1911. else
  1912. {
  1913. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  1914. cb->changePrimSkill(heroID,0,2);
  1915. cb->changePrimSkill(heroID,1,2);
  1916. cb->changePrimSkill(heroID,2,2);
  1917. cb->changePrimSkill(heroID,3,2);
  1918. InfoWindow iw;
  1919. iw.soundID = sound;
  1920. iw.player = cb->getOwner(heroID);
  1921. iw.text << std::pair<ui8,ui32>(11,66);
  1922. cb->showInfoDialog(&iw);
  1923. }
  1924. break;
  1925. }
  1926. case 47: //School of Magic
  1927. case 107://School of War
  1928. {
  1929. int skill = (ID==47 ? 2 : 0);
  1930. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1931. {
  1932. InfoWindow iw;
  1933. iw.soundID = sound;
  1934. iw.player = cb->getOwner(heroID);
  1935. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1936. cb->showInfoDialog(&iw);
  1937. }
  1938. else
  1939. {
  1940. BlockingDialog sd(true,true);
  1941. sd.soundID = sound;
  1942. sd.player = cb->getOwner(heroID);
  1943. sd.text << std::pair<ui8,ui32>(11,ot);
  1944. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1945. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1946. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1947. }
  1948. }
  1949. break;
  1950. }
  1951. }
  1952. else
  1953. {
  1954. ot++;
  1955. InfoWindow iw;
  1956. iw.soundID = sound;
  1957. iw.player = cb->getOwner(heroID);
  1958. iw.text << std::pair<ui8,ui32>(11,ot);
  1959. cb->showInfoDialog(&iw);
  1960. }
  1961. }
  1962. const std::string & CGVisitableOPH::getHoverText() const
  1963. {
  1964. int pom = -1;
  1965. switch(ID)
  1966. {
  1967. case 4:
  1968. pom = -1;
  1969. break;
  1970. case 51:
  1971. pom = 8;
  1972. break;
  1973. case 23:
  1974. pom = 7;
  1975. break;
  1976. case 61:
  1977. pom = 11;
  1978. break;
  1979. case 32:
  1980. pom = 4;
  1981. break;
  1982. case 100:
  1983. pom = 5;
  1984. break;
  1985. case 102:
  1986. pom = 18;
  1987. break;
  1988. case 41:
  1989. break;
  1990. case 47: //School of Magic
  1991. pom = 9;
  1992. break;
  1993. case 107://School of War
  1994. pom = 10;
  1995. break;
  1996. default:
  1997. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1998. }
  1999. hoverName = VLC->generaltexth->names[ID];
  2000. if(pom >= 0)
  2001. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  2002. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2003. if(h)
  2004. {
  2005. hoverName += ' ';
  2006. hoverName += (vstd::contains(visitors,h->id))
  2007. ? (VLC->generaltexth->allTexts[352]) //visited
  2008. : ( VLC->generaltexth->allTexts[353]); //not visited
  2009. }
  2010. return hoverName;
  2011. }
  2012. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  2013. {
  2014. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2015. cb->changePrimSkill(heroID,primSkill-1,2);
  2016. }
  2017. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  2018. {
  2019. if(what == ObjProperty::VISITORS)
  2020. visitors.insert(val);
  2021. }
  2022. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  2023. {
  2024. if(!which) //player refused to pay
  2025. return;
  2026. int base = (ID == 47 ? 2 : 0);
  2027. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2028. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  2029. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  2030. }
  2031. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  2032. {
  2033. ID = index;
  2034. town = TOWN;
  2035. id = town->bonusingBuildings.size();
  2036. }
  2037. void COPWBonus::setProperty(ui8 what, ui32 val)
  2038. {
  2039. switch (what)
  2040. {
  2041. case 4:
  2042. visitors.insert(val);
  2043. break;
  2044. case 12:
  2045. visitors.clear();
  2046. break;
  2047. }
  2048. }
  2049. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  2050. {
  2051. int heroID = h->id;
  2052. if (town->builtBuildings.find(ID) != town->builtBuildings.end())
  2053. {
  2054. InfoWindow iw;
  2055. iw.player = h->tempOwner;
  2056. switch (town->subID)
  2057. {
  2058. case 0: //Stables
  2059. if (!h->hasBonusFrom(Bonus::OBJECT, 94)) //does not stack with advMap Stables
  2060. {
  2061. GiveBonus gb;
  2062. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  2063. gb.id = heroID;
  2064. cb->giveHeroBonus(&gb);
  2065. iw.text << VLC->generaltexth->allTexts[580];
  2066. cb->showInfoDialog(&iw);
  2067. }
  2068. break;
  2069. case 5: //Mana Vortex
  2070. if (visitors.empty() && h->mana <= h->manaLimit())
  2071. {
  2072. cb->setManaPoints (heroID, 2 * h->manaLimit());
  2073. cb->setObjProperty (id, ObjProperty::VISITED, true);
  2074. iw.text << VLC->generaltexth->allTexts[579];
  2075. cb->showInfoDialog(&iw);
  2076. cb->setObjProperty (town->id, 11, id); //add to visitors
  2077. }
  2078. break;
  2079. }
  2080. }
  2081. }
  2082. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  2083. {
  2084. ID = index;
  2085. town = TOWN;
  2086. id = town->bonusingBuildings.size();
  2087. }
  2088. void CTownBonus::setProperty (ui8 what, ui32 val)
  2089. {
  2090. if(what == 4)
  2091. visitors.insert(val);
  2092. }
  2093. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  2094. {
  2095. int heroID = h->id;
  2096. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  2097. {
  2098. InfoWindow iw;
  2099. int what, val, mid;
  2100. switch (ID)
  2101. {
  2102. case 23:
  2103. switch(town->subID)
  2104. {
  2105. case 2: //wall
  2106. what = 3;
  2107. val = 1;
  2108. mid = 581;
  2109. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  2110. break;
  2111. case 3: //order of fire
  2112. what = 2;
  2113. val = 1;
  2114. mid = 582;
  2115. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  2116. break;
  2117. case 6://hall of valhalla
  2118. what = 0;
  2119. val = 1;
  2120. mid = 584;
  2121. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  2122. break;
  2123. case 5://academy of battle scholars
  2124. what = 4;
  2125. val = 1000;
  2126. mid = 583;
  2127. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  2128. break;
  2129. }
  2130. break;
  2131. case 17:
  2132. switch(town->subID)
  2133. {
  2134. case 7: //cage of warlords
  2135. what = 1;
  2136. val = 1;
  2137. mid = 585;
  2138. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  2139. break;
  2140. }
  2141. break;
  2142. }
  2143. iw.player = cb->getOwner(heroID);
  2144. iw.text << VLC->generaltexth->allTexts[mid];
  2145. cb->showInfoDialog(&iw);
  2146. cb->changePrimSkill (heroID, what, val);
  2147. if (town->visitingHero == h)
  2148. cb->setObjProperty (town->id, 11, id); //add to visitors
  2149. else
  2150. cb->setObjProperty (town->id, 13, id); //then it must be garrisoned hero
  2151. }
  2152. }
  2153. /*const std::string & CGCreature::getHoverText() const
  2154. {
  2155. hoverName = VLC->generaltexth->names[ID];
  2156. hoverName += "\n Power rating: ";
  2157. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  2158. if (ratio < 0.1) hoverName += "Effortless";
  2159. else if (ratio < 0.3) hoverName += "Very Weak";
  2160. else if (ratio < 0.6) hoverName += "Weak";
  2161. else if (ratio < 0.9) hoverName += "A bit weaker";
  2162. else if (ratio < 1.1) hoverName += "Equal";
  2163. else if (ratio < 1.3) hoverName += "A bit stronger";
  2164. else if (ratio < 1.8) hoverName += "Strong";
  2165. else if (ratio < 2.5) hoverName += "Very Strong";
  2166. else if (ratio < 4) hoverName += "Challenging";
  2167. else if (ratio < 8) hoverName += "Overpowering";
  2168. else if (ratio < 20) hoverName += "Deadly";
  2169. else hoverName += "Impossible";
  2170. return hoverName;
  2171. }*/
  2172. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2173. {
  2174. int action = takenAction(h);
  2175. switch( action ) //decide what we do...
  2176. {
  2177. case -2: //fight
  2178. fight(h);
  2179. break;
  2180. case -1: //flee
  2181. {
  2182. flee(h);
  2183. break;
  2184. }
  2185. case 0: //join for free
  2186. {
  2187. BlockingDialog ynd(true,false);
  2188. ynd.player = h->tempOwner;
  2189. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2190. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2191. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2192. break;
  2193. }
  2194. default: //join for gold
  2195. {
  2196. assert(action > 0);
  2197. //ask if player agrees to pay gold
  2198. BlockingDialog ynd(true,false);
  2199. ynd.player = h->tempOwner;
  2200. std::string tmp = VLC->generaltexth->advobtxt[90];
  2201. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(slots.find(0)->second.count));
  2202. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2203. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  2204. ynd.text << tmp;
  2205. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2206. break;
  2207. }
  2208. }
  2209. }
  2210. void CGCreature::endBattle( BattleResult *result ) const
  2211. {
  2212. if(result->winner==0)
  2213. {
  2214. cb->removeObject(id);
  2215. }
  2216. else
  2217. {
  2218. //int killedAmount=0;
  2219. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2220. // if(i->first == subID)
  2221. // killedAmount += i->second;
  2222. //cb->setAmount(id, slots.find(0)->second.second - killedAmount);
  2223. MetaString ms;
  2224. int pom = slots.find(0)->second.getQuantityID();
  2225. pom = 174 + 3*pom + 1;
  2226. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2227. cb->setHoverName(id,&ms);
  2228. }
  2229. }
  2230. void CGCreature::initObj()
  2231. {
  2232. blockVisit = true;
  2233. switch(character)
  2234. {
  2235. case 0:
  2236. character = 0;
  2237. break;
  2238. case 1:
  2239. character = 1 + ran()%7;
  2240. break;
  2241. case 2:
  2242. character = 1 + ran()%10;
  2243. break;
  2244. case 3:
  2245. character = 4 + ran()%7;
  2246. break;
  2247. case 4:
  2248. character = 10;
  2249. break;
  2250. }
  2251. slots[0].setType(subID);
  2252. si32 &amount = slots[0].count;
  2253. CCreature &c = *VLC->creh->creatures[subID];
  2254. if(!amount)
  2255. if(c.ammMax == c.ammMin)
  2256. amount = c.ammMax;
  2257. else
  2258. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2259. MetaString ms;
  2260. int pom = slots.find(0)->second.getQuantityID();
  2261. pom = 174 + 3*pom + 1;
  2262. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2263. ms.toString(hoverName);
  2264. }
  2265. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2266. {
  2267. double hlp = h->getTotalStrength() / getArmyStrength();
  2268. if(!character) //compliant creatures will always join
  2269. return 0;
  2270. else if(allowJoin)//test for joining
  2271. {
  2272. int factor;
  2273. if(hlp >= 7)
  2274. factor = 11;
  2275. else if(hlp >= 1)
  2276. factor = (int)(2*(hlp-1));
  2277. else if(hlp >= 0.5)
  2278. factor = -1;
  2279. else if(hlp >= 0.333)
  2280. factor = -2;
  2281. else
  2282. factor = -3;
  2283. int sympathy = 0;
  2284. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2285. myKindCres.insert(subID); //we
  2286. myKindCres.insert(VLC->creh->creatures[subID]->upgrades.begin(),VLC->creh->creatures[subID]->upgrades.end()); //our upgrades
  2287. for(std::vector<CCreature*>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2288. if(vstd::contains((*i)->upgrades, (ui32) id)) //it's our base creatures
  2289. myKindCres.insert((*i)->idNumber);
  2290. int count = 0, //how many creatures of our kind has hero
  2291. totalCount = 0;
  2292. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  2293. {
  2294. if(vstd::contains(myKindCres,i->second.type->idNumber))
  2295. count += i->second.count;
  2296. totalCount += i->second.count;
  2297. }
  2298. if(count*2 > totalCount)
  2299. sympathy++;
  2300. if(count)
  2301. sympathy++;
  2302. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  2303. if(charisma >= character) //creatures might join...
  2304. {
  2305. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  2306. return 0; //join for free
  2307. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  2308. return VLC->creh->creatures[subID]->cost[6] * Slots().find(0)->second.count; //join for gold
  2309. }
  2310. }
  2311. //we are still here - creatures not joined heroes, test for fleeing
  2312. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2313. //TODO: should be deterministic (will be needed for Vision spell)
  2314. int hlp2 = (int) (hlp - 2)*1000;
  2315. if(!neverFlees
  2316. && hlp2 >= 0
  2317. && rand()%2000 < hlp2
  2318. )
  2319. return -1; //flee
  2320. else
  2321. return -2; //fight
  2322. }
  2323. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2324. {
  2325. if(pursue)
  2326. {
  2327. fight(h);
  2328. }
  2329. else
  2330. {
  2331. cb->removeObject(id);
  2332. }
  2333. }
  2334. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2335. {
  2336. if(!accept)
  2337. {
  2338. if(takenAction(h,false) == -1) //they flee
  2339. {
  2340. flee(h);
  2341. }
  2342. else //they fight
  2343. {
  2344. InfoWindow iw;
  2345. iw.player = h->tempOwner;
  2346. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2347. cb->showInfoDialog(&iw);
  2348. fight(h);
  2349. }
  2350. }
  2351. else //accepted
  2352. {
  2353. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2354. {
  2355. InfoWindow iw;
  2356. iw.player = h->tempOwner;
  2357. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2358. cb->showInfoDialog(&iw);
  2359. //act as if player refused
  2360. joinDecision(h,cost,false);
  2361. return;
  2362. }
  2363. //take gold
  2364. if(cost)
  2365. cb->giveResource(h->tempOwner,6,-cost);
  2366. int slot = h->getSlotFor(subID);
  2367. if(slot >= 0) //there is place
  2368. {
  2369. //add creatures
  2370. SetGarrisons sg;
  2371. sg.garrs[h->id] = h->getArmy();
  2372. sg.garrs[h->id].addToSlot(slot, subID, getAmount(0));
  2373. cb->sendAndApply(&sg);
  2374. cb->removeObject(id);
  2375. }
  2376. else
  2377. {
  2378. cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2379. }
  2380. }
  2381. }
  2382. void CGCreature::fight( const CGHeroInstance *h ) const
  2383. {
  2384. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2385. }
  2386. void CGCreature::flee( const CGHeroInstance * h ) const
  2387. {
  2388. BlockingDialog ynd(true,false);
  2389. ynd.player = h->tempOwner;
  2390. ynd.text << std::pair<ui8,ui32>(11,91);
  2391. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2392. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2393. }
  2394. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2395. {
  2396. if(subID == 7) //TODO: support for abandoned mine
  2397. return;
  2398. if(h->tempOwner == tempOwner) //we're visiting our mine
  2399. {
  2400. cb->showGarrisonDialog(id,h->id,true,0);
  2401. return;
  2402. }
  2403. //TODO: check if mine is guarded
  2404. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  2405. MetaString ms;
  2406. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  2407. cb->setHoverName(id,&ms);
  2408. int vv=1; //amount of resource per turn
  2409. if (subID==0 || subID==2)
  2410. vv++;
  2411. else if (subID==6)
  2412. vv = 1000;
  2413. InfoWindow iw;
  2414. iw.soundID = soundBase::FLAGMINE;
  2415. iw.text << std::pair<ui8,ui32>(10,subID);
  2416. iw.player = h->tempOwner;
  2417. iw.components.push_back(Component(2,subID,vv,-1));
  2418. cb->showInfoDialog(&iw);
  2419. }
  2420. void CGMine::newTurn() const
  2421. {
  2422. if(cb->getDate() == 1)
  2423. return;
  2424. if (tempOwner == NEUTRAL_PLAYER)
  2425. return;
  2426. int vv = 1;
  2427. if (subID==0 || subID==2)
  2428. vv++;
  2429. else if (subID==6)
  2430. vv = 1000;
  2431. cb->giveResource(tempOwner,subID,vv);
  2432. }
  2433. void CGMine::initObj()
  2434. {
  2435. MetaString ms;
  2436. ms << std::pair<ui8,ui32>(9,subID);
  2437. if(tempOwner >= PLAYER_LIMIT)
  2438. tempOwner = NEUTRAL_PLAYER;
  2439. else
  2440. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2441. ms.toString(hoverName);
  2442. }
  2443. void CGResource::initObj()
  2444. {
  2445. blockVisit = true;
  2446. hoverName = VLC->generaltexth->restypes[subID];
  2447. if(!amount)
  2448. {
  2449. switch(subID)
  2450. {
  2451. case 6:
  2452. amount = 500 + (rand()%6)*100;
  2453. break;
  2454. case 0: case 2:
  2455. amount = 6 + (rand()%5);
  2456. break;
  2457. default:
  2458. amount = 3 + (rand()%3);
  2459. break;
  2460. }
  2461. }
  2462. }
  2463. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2464. {
  2465. if(stacksCount())
  2466. {
  2467. if(message.size())
  2468. {
  2469. BlockingDialog ynd(true,false);
  2470. ynd.player = h->getOwner();
  2471. ynd.text << message;
  2472. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2473. }
  2474. else
  2475. {
  2476. fightForRes(1,h);
  2477. }
  2478. }
  2479. else
  2480. {
  2481. if(message.length())
  2482. {
  2483. InfoWindow iw;
  2484. iw.player = h->tempOwner;
  2485. iw.text << message;
  2486. cb->showInfoDialog(&iw);
  2487. }
  2488. collectRes(h->getOwner());
  2489. }
  2490. }
  2491. void CGResource::collectRes( int player ) const
  2492. {
  2493. cb->giveResource(player,subID,amount);
  2494. ShowInInfobox sii;
  2495. sii.player = player;
  2496. sii.c = Component(2,subID,amount,0);
  2497. sii.text << std::pair<ui8,ui32>(11,113);
  2498. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2499. cb->showCompInfo(&sii);
  2500. cb->removeObject(id);
  2501. }
  2502. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2503. {
  2504. if(agreed)
  2505. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  2506. }
  2507. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2508. {
  2509. if(result->winner == 0) //attacker won
  2510. collectRes(h->getOwner());
  2511. }
  2512. void CGVisitableOPW::newTurn() const
  2513. {
  2514. if (cb->getDate(1)==1) //first day of week
  2515. {
  2516. cb->setObjProperty(id, ObjProperty::VISITED, false);
  2517. MetaString ms; //set text to "not visited"
  2518. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2519. cb->setHoverName(id,&ms);
  2520. }
  2521. }
  2522. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2523. {
  2524. int mid, sound = 0;
  2525. switch (ID)
  2526. {
  2527. case 55: //mystical garden
  2528. sound = soundBase::experience;
  2529. mid = 92;
  2530. break;
  2531. case 112://windmill
  2532. sound = soundBase::GENIE;
  2533. mid = 170;
  2534. break;
  2535. case 109://waterwheel
  2536. sound = soundBase::GENIE;
  2537. mid = 164;
  2538. break;
  2539. }
  2540. if (visited)
  2541. {
  2542. if (ID!=112)
  2543. mid++;
  2544. else
  2545. mid--;
  2546. InfoWindow iw;
  2547. iw.soundID = sound;
  2548. iw.player = h->tempOwner;
  2549. iw.text << std::pair<ui8,ui32>(11,mid);
  2550. cb->showInfoDialog(&iw);
  2551. }
  2552. else
  2553. {
  2554. int type, sub, val;
  2555. type = 2;
  2556. switch (ID)
  2557. {
  2558. case 55:
  2559. if (rand()%2)
  2560. {
  2561. sub = 5;
  2562. val = 5;
  2563. }
  2564. else
  2565. {
  2566. sub = 6;
  2567. val = 500;
  2568. }
  2569. break;
  2570. case 112:
  2571. mid = 170;
  2572. sub = (rand() % 5) + 1;
  2573. val = (rand() % 4) + 3;
  2574. break;
  2575. case 109:
  2576. mid = 164;
  2577. sub = 6;
  2578. if(cb->getDate(0)<8)
  2579. val = 500;
  2580. else
  2581. val = 1000;
  2582. }
  2583. cb->giveResource(h->tempOwner,sub,val);
  2584. InfoWindow iw;
  2585. iw.soundID = sound;
  2586. iw.player = h->tempOwner;
  2587. iw.components.push_back(Component(type,sub,val,0));
  2588. iw.text << std::pair<ui8,ui32>(11,mid);
  2589. cb->showInfoDialog(&iw);
  2590. cb->setObjProperty(id, ObjProperty::VISITED, true);
  2591. MetaString ms; //set text to "visited"
  2592. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2593. cb->setHoverName(id,&ms);
  2594. }
  2595. }
  2596. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2597. {
  2598. if(what == ObjProperty::VISITED)
  2599. visited = val;
  2600. }
  2601. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2602. {
  2603. int destinationid=-1;
  2604. switch(ID)
  2605. {
  2606. case 43: //one way - find corresponding exit monolith
  2607. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2608. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2609. else
  2610. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2611. break;
  2612. case 45://two way monolith - pick any other one
  2613. case 111: //Whirlpool
  2614. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  2615. {
  2616. while ((destinationid = objs[ID][subID][rand()%objs[ID][subID].size()]) == id); //choose another exit
  2617. if (ID == 111)
  2618. {
  2619. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  2620. {
  2621. CCreatureSet army = h->getArmy();
  2622. int targetstack = army.Slots().size()-1;
  2623. for(TSlots::const_reverse_iterator i = army.Slots().rbegin(); i != army.Slots().rend(); i++)
  2624. {
  2625. if (army.getPower(targetstack) > army.getPower(i->first))
  2626. {
  2627. targetstack = (i->first);
  2628. }
  2629. }
  2630. CCreatureSet ourArmy;
  2631. ourArmy.addStack (targetstack, army.getStack(targetstack));
  2632. TQuantity tq = (TQuantity)((double)(ourArmy.getAmount(targetstack))*0.3);
  2633. if (tq) //casualties > 0
  2634. {
  2635. ourArmy.setStackCount (targetstack, tq);
  2636. InfoWindow iw;
  2637. iw.player = h->tempOwner;
  2638. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  2639. iw.components.push_back (Component(ourArmy.getStack(targetstack)));
  2640. cb->showInfoDialog(&iw);
  2641. cb->takeCreatures (h->id, ourArmy.Slots());
  2642. }
  2643. }
  2644. }
  2645. }
  2646. else
  2647. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2648. break;
  2649. case 103: //find nearest subterranean gate on the other level
  2650. {
  2651. int i=0;
  2652. for(; i < gates.size(); i++)
  2653. {
  2654. if(gates[i].first == id)
  2655. {
  2656. destinationid = gates[i].second;
  2657. break;
  2658. }
  2659. else if(gates[i].second == id)
  2660. {
  2661. destinationid = gates[i].first;
  2662. break;
  2663. }
  2664. }
  2665. if(destinationid < 0 || i == gates.size()) //no exit
  2666. {
  2667. InfoWindow iw;
  2668. iw.player = h->tempOwner;
  2669. iw.text.addTxt(MetaString::ADVOB_TXT, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  2670. cb->sendAndApply(&iw);
  2671. }
  2672. break;
  2673. }
  2674. }
  2675. if(destinationid < 0)
  2676. {
  2677. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  2678. return;
  2679. }
  2680. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  2681. }
  2682. void CGTeleport::initObj()
  2683. {
  2684. int si = subID;
  2685. switch (ID)
  2686. {
  2687. case 103://ignore subterranean gates subid
  2688. case 111:
  2689. {
  2690. si = 0;
  2691. break;
  2692. }
  2693. default:
  2694. break;
  2695. }
  2696. objs[ID][si].push_back(id);
  2697. }
  2698. void CGTeleport::postInit() //matches subterranean gates into pairs
  2699. {
  2700. //split on underground and surface gates
  2701. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  2702. for(size_t i = 0; i < objs[103][0].size(); i++)
  2703. {
  2704. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  2705. gatesSplit[hlp->pos.z].push_back(hlp);
  2706. }
  2707. //sort by position
  2708. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  2709. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  2710. {
  2711. const CGObjectInstance *cur = gatesSplit[0][i];
  2712. //find nearest underground exit
  2713. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  2714. for(int j = 0; j < gatesSplit[1].size(); j++)
  2715. {
  2716. const CGObjectInstance *checked = gatesSplit[1][j];
  2717. if(!checked)
  2718. continue;
  2719. double hlp = checked->pos.dist2d(cur->pos);
  2720. if(hlp < best.second)
  2721. {
  2722. best.first = j;
  2723. best.second = hlp;
  2724. }
  2725. }
  2726. if(best.first >= 0) //found pair
  2727. {
  2728. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  2729. gatesSplit[1][best.first] = NULL;
  2730. }
  2731. else
  2732. {
  2733. gates.push_back(std::pair<int, int>(cur->id, -1));
  2734. }
  2735. }
  2736. objs.erase(103);
  2737. }
  2738. void CGArtifact::initObj()
  2739. {
  2740. blockVisit = true;
  2741. if(ID == 5)
  2742. hoverName = VLC->arth->artifacts[subID].Name();
  2743. }
  2744. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2745. {
  2746. if(!stacksCount())
  2747. {
  2748. if(ID == 5)
  2749. {
  2750. InfoWindow iw;
  2751. iw.soundID = soundBase::treasure;
  2752. iw.player = h->tempOwner;
  2753. iw.components.push_back(Component(4,subID,0,0));
  2754. if(message.length())
  2755. iw.text << message;
  2756. else
  2757. iw.text << std::pair<ui8,ui32>(12,subID);
  2758. cb->showInfoDialog(&iw);
  2759. }
  2760. pick(h);
  2761. }
  2762. else
  2763. {
  2764. if(message.size())
  2765. {
  2766. BlockingDialog ynd(true,false);
  2767. ynd.player = h->getOwner();
  2768. ynd.text << message;
  2769. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2770. }
  2771. else
  2772. {
  2773. fightForArt(0,h);
  2774. }
  2775. }
  2776. }
  2777. void CGArtifact::pick(const CGHeroInstance * h) const
  2778. {
  2779. if(ID == 5) //Artifact
  2780. {
  2781. cb->giveHeroArtifact(subID,h->id,-2);
  2782. }
  2783. else if(ID == 93) // Spell scroll
  2784. {
  2785. //TODO: support for the spell scroll
  2786. }
  2787. cb->removeObject(id);
  2788. }
  2789. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2790. {
  2791. if(agreed)
  2792. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2793. }
  2794. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2795. {
  2796. if(result->winner == 0) //attacker won
  2797. pick(h);
  2798. }
  2799. void CGPickable::initObj()
  2800. {
  2801. blockVisit = true;
  2802. switch(ID)
  2803. {
  2804. case 12: //campfire
  2805. val2 = (ran()%3) + 4; //4 - 6
  2806. val1 = val2 * 100;
  2807. type = ran()%6; //given resource
  2808. break;
  2809. case 29: //floatsam
  2810. switch(type = ran()%4)
  2811. {
  2812. case 0:
  2813. val1 = val2 = 0;
  2814. break;
  2815. case 1:
  2816. val1 = 5;
  2817. val2 = 0;
  2818. break;
  2819. case 2:
  2820. val1 = 5;
  2821. val2 = 200;
  2822. break;
  2823. case 3:
  2824. val1 = 10;
  2825. val2 = 500;
  2826. break;
  2827. }
  2828. break;
  2829. case 82: //sea chest
  2830. {
  2831. int hlp = ran()%100;
  2832. if(hlp < 20)
  2833. {
  2834. val1 = 0;
  2835. type = 0;
  2836. }
  2837. else if(hlp < 90)
  2838. {
  2839. val1 = 1500;
  2840. type = 2;
  2841. }
  2842. else
  2843. {
  2844. val1 = 1000;
  2845. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2846. type = 1;
  2847. }
  2848. }
  2849. break;
  2850. case 86: //Shipwreck Survivor
  2851. {
  2852. int hlp = ran()%100;
  2853. if(hlp < 55)
  2854. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2855. else if(hlp < 75)
  2856. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  2857. else if(hlp < 95)
  2858. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  2859. else
  2860. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  2861. }
  2862. break;
  2863. case 101: //treasure chest
  2864. {
  2865. int hlp = ran()%100;
  2866. if(hlp >= 95)
  2867. {
  2868. type = 1;
  2869. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2870. return;
  2871. }
  2872. else if (hlp >= 65)
  2873. {
  2874. val1 = 2000;
  2875. }
  2876. else if(hlp >= 33)
  2877. {
  2878. val1 = 1500;
  2879. }
  2880. else
  2881. {
  2882. val1 = 1000;
  2883. }
  2884. val2 = val1 - 500;
  2885. type = 0;
  2886. break;
  2887. }
  2888. }
  2889. }
  2890. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2891. {
  2892. switch(ID)
  2893. {
  2894. case 12: //campfire
  2895. {
  2896. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2897. cb->giveResource(h->tempOwner,6,val1);//gold
  2898. InfoWindow iw;
  2899. iw.soundID = soundBase::experience;
  2900. iw.player = h->tempOwner;
  2901. iw.components.push_back(Component(2,6,val1,0));
  2902. iw.components.push_back(Component(2,type,val2,0));
  2903. iw.text << std::pair<ui8,ui32>(11,23);
  2904. cb->showInfoDialog(&iw);
  2905. break;
  2906. }
  2907. case 29: //flotsam
  2908. {
  2909. cb->giveResource(h->tempOwner,0,val1); //wood
  2910. cb->giveResource(h->tempOwner,6,val2);//gold
  2911. InfoWindow iw;
  2912. iw.soundID = soundBase::GENIE;
  2913. iw.player = h->tempOwner;
  2914. if(val1)
  2915. iw.components.push_back(Component(2,0,val1,0));
  2916. if(val2)
  2917. iw.components.push_back(Component(2,6,val2,0));
  2918. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2919. cb->showInfoDialog(&iw);
  2920. break;
  2921. }
  2922. case 82: //Sea Chest
  2923. {
  2924. InfoWindow iw;
  2925. iw.soundID = soundBase::chest;
  2926. iw.player = h->tempOwner;
  2927. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2928. if(val1) //there is gold
  2929. {
  2930. iw.components.push_back(Component(2,6,val1,0));
  2931. cb->giveResource(h->tempOwner,6,val1);
  2932. }
  2933. if(type == 1) //art
  2934. {
  2935. //TODO: what if no space in backpack?
  2936. iw.components.push_back(Component(4, val2, 1, 0));
  2937. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2938. cb->giveHeroArtifact(val2,h->id,-2);
  2939. }
  2940. cb->showInfoDialog(&iw);
  2941. break;
  2942. }
  2943. case 86: //Shipwreck Survivor
  2944. {
  2945. //TODO: what if no space in backpack?
  2946. InfoWindow iw;
  2947. iw.soundID = soundBase::experience;
  2948. iw.player = h->tempOwner;
  2949. iw.components.push_back(Component(4,val1,1,0));
  2950. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2951. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2952. cb->giveHeroArtifact(val1,h->id,-2);
  2953. cb->showInfoDialog(&iw);
  2954. break;
  2955. }
  2956. case 101: //treasure chest
  2957. {
  2958. if (subID) //not OH3 treasure chest
  2959. {
  2960. tlog2 << "Not supported WoG treasure chest!\n";
  2961. return;
  2962. }
  2963. if(type) //there is an artifact
  2964. {
  2965. cb->giveHeroArtifact(val1,h->id,-2);
  2966. InfoWindow iw;
  2967. iw.soundID = soundBase::treasure;
  2968. iw.player = h->tempOwner;
  2969. iw.components.push_back(Component(4,val1,1,0));
  2970. iw.text << std::pair<ui8,ui32>(11,145);
  2971. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2972. cb->showInfoDialog(&iw);
  2973. break;
  2974. }
  2975. else
  2976. {
  2977. BlockingDialog sd(false,true);
  2978. sd.player = h->tempOwner;
  2979. sd.text << std::pair<ui8,ui32>(11,146);
  2980. sd.components.push_back(Component(2,6,val1,0));
  2981. sd.components.push_back(Component(5,0,val2,0));
  2982. sd.soundID = soundBase::chest;
  2983. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2984. cb->showBlockingDialog(&sd,fun);
  2985. return;
  2986. }
  2987. }
  2988. }
  2989. cb->removeObject(id);
  2990. }
  2991. void CGPickable::chosen( int which, int heroID ) const
  2992. {
  2993. switch(which)
  2994. {
  2995. case 1: //player pick gold
  2996. cb->giveResource(cb->getOwner(heroID),6,val1);
  2997. break;
  2998. case 2: //player pick exp
  2999. cb->changePrimSkill(heroID, 4, val2);
  3000. break;
  3001. default:
  3002. throw std::string("Unhandled treasure choice");
  3003. }
  3004. cb->removeObject(id);
  3005. }
  3006. bool CQuest::checkQuest (const CGHeroInstance * h) const
  3007. {
  3008. switch (missionType)
  3009. {
  3010. case MISSION_NONE:
  3011. return true;
  3012. break;
  3013. case MISSION_LEVEL:
  3014. if (m13489val <= h->level)
  3015. return true;
  3016. return false;
  3017. break;
  3018. case MISSION_PRIMARY_STAT:
  3019. for (int i = 0; i < 4; ++i)
  3020. {
  3021. if (h->getPrimSkillLevel(i) < m2stats[i])
  3022. return false;
  3023. }
  3024. return true;
  3025. break;
  3026. case MISSION_KILL_HERO:
  3027. if (h->cb->gameState()->map->heroesToBeat[m13489val]->tempOwner < PLAYER_LIMIT)
  3028. return false; //if the pointer is not NULL
  3029. return true;
  3030. break;
  3031. case MISSION_KILL_CREATURE:
  3032. if (h->cb->gameState()->map->monsters[m13489val]->pos == int3(-1,-1,-1))
  3033. return true;
  3034. return false;
  3035. break;
  3036. case MISSION_ART:
  3037. for (int i = 0; i < m5arts.size(); ++i)
  3038. {
  3039. if (h->hasArt(m5arts[i]))
  3040. continue;
  3041. return false; //if the artifact was not found
  3042. }
  3043. return true;
  3044. break;
  3045. case MISSION_ARMY:
  3046. {
  3047. TSlots::const_iterator it, cre;
  3048. ui32 count;
  3049. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  3050. {
  3051. for (count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  3052. {
  3053. if (it->second.type == cre->second.type)
  3054. count += it->second.count;
  3055. }
  3056. if (count < cre->second.count) //not enough creatures of this kind
  3057. return false;
  3058. }
  3059. }
  3060. return true;
  3061. break;
  3062. case MISSION_RESOURCES:
  3063. for (int i = 0; i < 7; ++i) //including Mithril ?
  3064. { //Quest has no direct access to callback
  3065. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  3066. return false;
  3067. }
  3068. return true;
  3069. break;
  3070. case MISSION_HERO:
  3071. if (m13489val == h->type->ID)
  3072. return true;
  3073. return false;
  3074. break;
  3075. case MISSION_PLAYER:
  3076. if (m13489val == h->getOwner())
  3077. return true;
  3078. return false;
  3079. break;
  3080. default:
  3081. return false;
  3082. }
  3083. }
  3084. void CGSeerHut::initObj()
  3085. {
  3086. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  3087. textOption = ran()%3;
  3088. progress = 0;
  3089. if (missionType)
  3090. {
  3091. if (!isCustom)
  3092. {
  3093. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3094. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3095. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3096. }
  3097. }
  3098. else
  3099. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3100. }
  3101. const std::string & CGSeerHut::getHoverText() const
  3102. {
  3103. switch (ID)
  3104. {
  3105. case 83:
  3106. hoverName = VLC->generaltexth->allTexts[347];
  3107. boost::algorithm::replace_first(hoverName,"%s", seerName);
  3108. break;
  3109. case 215:
  3110. hoverName = VLC->generaltexth->names[ID];
  3111. break;
  3112. default:
  3113. tlog5 << "unrecognized quest object\n";
  3114. }
  3115. if (progress & missionType) //rollover when the quest is active
  3116. {
  3117. MetaString ms;
  3118. ms << "\n\n" << VLC->generaltexth->quests[missionType-1][3][textOption];
  3119. std::string str;
  3120. switch (missionType)
  3121. {
  3122. case MISSION_LEVEL:
  3123. ms.addReplacement (m13489val);
  3124. break;
  3125. case MISSION_PRIMARY_STAT:
  3126. {
  3127. MetaString loot;
  3128. for (int i = 0; i < 4; ++i)
  3129. {
  3130. if (m2stats[i])
  3131. {
  3132. loot << "%d %s";
  3133. loot.addReplacement (m2stats[i]);
  3134. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3135. }
  3136. }
  3137. ms.addReplacement(loot.buildList());
  3138. }
  3139. break;
  3140. case MISSION_KILL_HERO:
  3141. ms.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3142. break;
  3143. case MISSION_HERO:
  3144. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3145. break;
  3146. case MISSION_KILL_CREATURE:
  3147. {
  3148. ms.addReplacement(cb->gameState()->map->monsters[m13489val]->getStack(0));
  3149. }
  3150. break;
  3151. case MISSION_ART:
  3152. {
  3153. MetaString loot;
  3154. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3155. {
  3156. loot << "%s";
  3157. loot.addReplacement (MetaString::ART_NAMES, *it);
  3158. }
  3159. ms.addReplacement(loot.buildList());
  3160. }
  3161. break;
  3162. case MISSION_ARMY:
  3163. {
  3164. MetaString loot;
  3165. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3166. {
  3167. loot << "%s";
  3168. loot.addReplacement(it->second);
  3169. }
  3170. ms.addReplacement (loot.buildList());
  3171. }
  3172. break;
  3173. case MISSION_RESOURCES:
  3174. {
  3175. MetaString loot;
  3176. for (int i = 0; i < 7; ++i)
  3177. {
  3178. if (m7resources[i])
  3179. {
  3180. loot << "%d %s";
  3181. loot.addReplacement (m7resources[i]);
  3182. loot.addReplacement (MetaString::RES_NAMES, i);
  3183. }
  3184. }
  3185. ms.addReplacement (loot.buildList());
  3186. }
  3187. break;
  3188. case MISSION_PLAYER:
  3189. ms.addReplacement (VLC->generaltexth->colors[m13489val]);
  3190. break;
  3191. default:
  3192. break;
  3193. }
  3194. ms.toString(str);
  3195. hoverName += str;
  3196. }
  3197. return hoverName;
  3198. }
  3199. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  3200. {
  3201. switch (what)
  3202. {
  3203. case 10:
  3204. progress = val;
  3205. break;
  3206. case 11:
  3207. missionType = CQuest::MISSION_NONE;
  3208. break;
  3209. }
  3210. }
  3211. void CGSeerHut::newTurn() const
  3212. {
  3213. if (lastDay >= 0 && lastDay <= cb->getDate(0)) //time is up
  3214. {
  3215. cb->setObjProperty (id,11,0);
  3216. cb->setObjProperty (id,10,0);
  3217. }
  3218. }
  3219. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  3220. {
  3221. InfoWindow iw;
  3222. iw.player = h->getOwner();
  3223. if (missionType)
  3224. {
  3225. if (!progress) //propose quest
  3226. {
  3227. iw.text << firstVisitText;
  3228. switch (missionType)
  3229. {
  3230. case MISSION_LEVEL:
  3231. iw.components.push_back (Component (Component::EXPERIENCE, 1, m13489val, 0));
  3232. if (!isCustom)
  3233. iw.text.addReplacement(m13489val);
  3234. break;
  3235. case MISSION_PRIMARY_STAT:
  3236. {
  3237. MetaString loot;
  3238. for (int i = 0; i < 4; ++i)
  3239. {
  3240. if (m2stats[i])
  3241. {
  3242. iw.components.push_back (Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  3243. loot << "%d %s";
  3244. loot.addReplacement (m2stats[i]);
  3245. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3246. }
  3247. }
  3248. if (!isCustom)
  3249. iw.text.addReplacement (loot.buildList());
  3250. }
  3251. break;
  3252. case MISSION_KILL_HERO:
  3253. iw.components.push_back (Component (Component::HERO,
  3254. cb->gameState()->map->heroesToBeat[m13489val]->type->ID, 0, 0));
  3255. if (!isCustom)
  3256. iw.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3257. break;
  3258. case MISSION_HERO:
  3259. iw.components.push_back (Component (Component::HERO, m13489val, 0, 0));
  3260. if (!isCustom)
  3261. iw.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3262. break;
  3263. case MISSION_KILL_CREATURE:
  3264. {
  3265. CStackInstance stack = cb->gameState()->map->monsters[m13489val]->getStack(0);
  3266. iw.components.push_back (Component(stack));
  3267. if (!isCustom)
  3268. {
  3269. iw.text.addReplacement(stack);
  3270. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3271. {
  3272. iw.text.addReplacement (VLC->generaltexth->arraytxt[147+checkDirection()]);
  3273. }
  3274. }
  3275. }
  3276. break;
  3277. case MISSION_ART:
  3278. {
  3279. MetaString loot;
  3280. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3281. {
  3282. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3283. loot << "%s";
  3284. loot.addReplacement (MetaString::ART_NAMES, *it);
  3285. }
  3286. if (!isCustom)
  3287. iw.text.addReplacement (loot.buildList());
  3288. }
  3289. break;
  3290. case MISSION_ARMY:
  3291. {
  3292. MetaString loot;
  3293. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3294. {
  3295. iw.components.push_back(Component(it->second));
  3296. loot << "%s";
  3297. loot.addReplacement(it->second);
  3298. }
  3299. if (!isCustom)
  3300. iw.text.addReplacement (loot.buildList());
  3301. }
  3302. break;
  3303. case MISSION_RESOURCES:
  3304. {
  3305. MetaString loot;
  3306. for (int i = 0; i < 7; ++i)
  3307. {
  3308. if (m7resources[i])
  3309. {
  3310. iw.components.push_back (Component (Component::RESOURCE, i, m7resources[i], 0));
  3311. loot << "%d %s";
  3312. loot.addReplacement (m7resources[i]);
  3313. loot.addReplacement (MetaString::RES_NAMES, i);
  3314. }
  3315. }
  3316. if (!isCustom)
  3317. iw.text.addReplacement (loot.buildList());
  3318. }
  3319. break;
  3320. case MISSION_PLAYER:
  3321. iw.components.push_back (Component (Component::FLAG, m13489val, 0, 0));
  3322. if (!isCustom)
  3323. iw.text.addReplacement (VLC->generaltexth->colors[m13489val]);
  3324. break;
  3325. }
  3326. cb->setObjProperty (id,10,1);
  3327. cb->showInfoDialog(&iw);
  3328. }
  3329. else if (!checkQuest(h))
  3330. {
  3331. iw.text << nextVisitText;
  3332. cb->showInfoDialog(&iw);
  3333. }
  3334. if (checkQuest(h)) // propose completion, also on first visit
  3335. {
  3336. BlockingDialog bd (true, false);
  3337. bd.player = h->getOwner();
  3338. bd.soundID = soundBase::QUEST;
  3339. bd.text << completedText;
  3340. switch (missionType)
  3341. {
  3342. case CQuest::MISSION_LEVEL:
  3343. if (!isCustom)
  3344. bd.text.addReplacement(m13489val);
  3345. break;
  3346. case CQuest::MISSION_PRIMARY_STAT:
  3347. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  3348. {
  3349. MetaString loot;
  3350. for (int i = 0; i < 4; ++i)
  3351. {
  3352. if (m2stats[i])
  3353. {
  3354. loot << "%d %s";
  3355. loot.addReplacement (m2stats[i]);
  3356. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3357. }
  3358. }
  3359. if (!isCustom)
  3360. bd.text.addReplacement (loot.buildList());
  3361. }
  3362. break;
  3363. case CQuest::MISSION_ART:
  3364. {
  3365. MetaString loot;
  3366. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3367. {
  3368. bd.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3369. loot << "%s";
  3370. loot.addReplacement (MetaString::ART_NAMES, *it);
  3371. }
  3372. if (!isCustom)
  3373. bd.text.addReplacement (loot.buildList());
  3374. }
  3375. break;
  3376. case CQuest::MISSION_ARMY:
  3377. {
  3378. MetaString loot;
  3379. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3380. {
  3381. bd.components.push_back(Component(it->second));
  3382. loot << "%s";
  3383. loot.addReplacement(it->second);
  3384. }
  3385. if (!isCustom)
  3386. bd.text.addReplacement (loot.buildList());
  3387. }
  3388. break;
  3389. case CQuest::MISSION_RESOURCES:
  3390. {
  3391. MetaString loot;
  3392. for (int i = 0; i < 7; ++i)
  3393. {
  3394. if (m7resources[i])
  3395. {
  3396. bd.components.push_back (Component (Component::RESOURCE, i, m7resources[i], 0));
  3397. loot << "%d %s";
  3398. loot.addReplacement (m7resources[i]);
  3399. loot.addReplacement (MetaString::RES_NAMES, i);
  3400. }
  3401. }
  3402. if (!isCustom)
  3403. bd.text.addReplacement (loot.buildList());
  3404. }
  3405. break;
  3406. case MISSION_KILL_HERO:
  3407. if (!isCustom)
  3408. bd.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3409. break;
  3410. case MISSION_HERO:
  3411. if (!isCustom)
  3412. bd.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3413. break;
  3414. case MISSION_KILL_CREATURE:
  3415. {
  3416. {
  3417. bd.text.addReplacement(cb->gameState()->map->monsters[m13489val]->getArmy()[0]);
  3418. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3419. {
  3420. bd.text.addReplacement (VLC->generaltexth->arraytxt[147+checkDirection()]);
  3421. }
  3422. }
  3423. }
  3424. case MISSION_PLAYER:
  3425. bd.components.push_back (Component (Component::FLAG, m13489val, 0, 0));
  3426. if (!isCustom)
  3427. bd.text.addReplacement (VLC->generaltexth->colors[m13489val]);
  3428. break;
  3429. }
  3430. cb->showBlockingDialog (&bd, boost::bind (&CGSeerHut::finishQuest, this, h, _1));
  3431. return;
  3432. }
  3433. }
  3434. else
  3435. {
  3436. iw.text << VLC->generaltexth->seerEmpty[textOption];
  3437. if (ID == 83)
  3438. iw.text.addReplacement(seerName);
  3439. cb->showInfoDialog(&iw);
  3440. }
  3441. }
  3442. int CGSeerHut::checkDirection() const
  3443. {
  3444. int3 cord = cb->gameState()->map->monsters[m13489val]->pos;
  3445. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  3446. {
  3447. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  3448. return 8;
  3449. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  3450. return 1;
  3451. else //northeast
  3452. return 2;
  3453. }
  3454. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  3455. {
  3456. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  3457. return 7;
  3458. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  3459. return 9;
  3460. else //east
  3461. return 3;
  3462. }
  3463. else //south
  3464. {
  3465. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  3466. return 6;
  3467. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  3468. return 5;
  3469. else //southeast
  3470. return 4;
  3471. }
  3472. }
  3473. void CGSeerHut::finishQuest (const CGHeroInstance * h, ui32 accept) const
  3474. {
  3475. if (accept)
  3476. {
  3477. switch (missionType)
  3478. {
  3479. case CQuest::MISSION_ART:
  3480. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3481. {
  3482. cb->removeArtifact(*it, h->id);
  3483. }
  3484. break;
  3485. case CQuest::MISSION_ARMY:
  3486. cb->takeCreatures (h->id, m6creatures);
  3487. break;
  3488. case CQuest::MISSION_RESOURCES:
  3489. for (int i = 0; i < 7; ++i)
  3490. {
  3491. cb->giveResource(h->getOwner(), i, -m7resources[i]);
  3492. }
  3493. break;
  3494. default:
  3495. break;
  3496. }
  3497. cb->setObjProperty (id,11,0); //no more mission avaliable
  3498. completeQuest(h); //make sure to remove QuestQuard at the very end
  3499. }
  3500. }
  3501. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  3502. {
  3503. InfoWindow iw;
  3504. iw.player = h->getOwner();
  3505. switch (rewardType)
  3506. {
  3507. case 1: //experience
  3508. cb->changePrimSkill(h->id, 4, rVal, false);
  3509. iw.components.push_back (Component (Component::EXPERIENCE, 0, rVal, 0));
  3510. break;
  3511. case 2: //mana points
  3512. cb->setManaPoints(h->id, h->mana+rVal);
  3513. iw.components.push_back (Component (Component::PRIM_SKILL, 5, rVal, 0));
  3514. break;
  3515. case 3: case 4: //morale /luck
  3516. {
  3517. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  3518. Bonus::OBJECT, rVal, h->id, "", -1);
  3519. GiveBonus gb;
  3520. gb.id = h->id;
  3521. gb.bonus = hb;
  3522. //gb.descr = "";
  3523. cb->giveHeroBonus(&gb);
  3524. iw.components.push_back (Component (
  3525. (rewardType == 3 ? Component::MORALE : Component::LUCK), 0, rVal, 0));
  3526. }
  3527. break;
  3528. case 5: //resources
  3529. cb->giveResource(h->getOwner(), rID, rVal);
  3530. iw.components.push_back (Component (Component::RESOURCE, rID, rVal, 0));
  3531. break;
  3532. case 6: //main ability bonus (attak, defence etd.)
  3533. cb->changePrimSkill(h->id, rID, rVal, false);
  3534. iw.components.push_back (Component (Component::PRIM_SKILL, rID, rVal, 0));
  3535. break;
  3536. case 7: // secondary ability gain
  3537. cb->changeSecSkill(h->id, rID, rVal, false);
  3538. iw.components.push_back (Component (Component::SEC_SKILL, rID, rVal, 0));
  3539. break;
  3540. case 8: // artifact
  3541. cb->giveHeroArtifact(rID, h->id, -2);
  3542. iw.components.push_back (Component (Component::ARTIFACT, rID, 0, 0));
  3543. break;
  3544. case 9:// spell
  3545. {
  3546. std::set<ui32> spell;
  3547. spell.insert (rID);
  3548. cb->changeSpells(h->id, true, spell);
  3549. iw.components.push_back (Component (Component::SPELL, rID, 0, 0));
  3550. }
  3551. break;
  3552. case 10:// creature
  3553. {
  3554. CCreatureSet creatures;
  3555. creatures.setCreature (0, rID, rVal);
  3556. cb->giveCreatures (id, h, creatures);
  3557. iw.components.push_back (Component (Component::CREATURE, rID, rVal, 0));
  3558. }
  3559. break;
  3560. default:
  3561. break;
  3562. }
  3563. cb->showInfoDialog(&iw);
  3564. }
  3565. void CGQuestGuard::initObj()
  3566. {
  3567. blockVisit = true;
  3568. progress = 0;
  3569. textOption = ran()%3 + 3; //3-5
  3570. if (missionType && !isCustom)
  3571. {
  3572. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3573. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3574. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3575. }
  3576. else
  3577. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3578. }
  3579. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  3580. {
  3581. cb->removeObject(id);
  3582. }
  3583. void CGWitchHut::initObj()
  3584. {
  3585. ability = allowedAbilities[ran()%allowedAbilities.size()];
  3586. }
  3587. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  3588. {
  3589. InfoWindow iw;
  3590. iw.soundID = soundBase::gazebo;
  3591. iw.player = h->getOwner();
  3592. if(!hasVisited(h->tempOwner))
  3593. cb->setObjProperty(id,10,h->tempOwner);
  3594. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  3595. {
  3596. iw.text << std::pair<ui8,ui32>(11,172);
  3597. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3598. }
  3599. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  3600. {
  3601. iw.text << std::pair<ui8,ui32>(11,173);
  3602. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3603. }
  3604. else //give sec skill
  3605. {
  3606. iw.components.push_back(Component(1, ability, 1, 0));
  3607. iw.text << std::pair<ui8,ui32>(11,171);
  3608. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3609. cb->changeSecSkill(h->id,ability,1,true);
  3610. }
  3611. cb->showInfoDialog(&iw);
  3612. }
  3613. const std::string & CGWitchHut::getHoverText() const
  3614. {
  3615. hoverName = VLC->generaltexth->names[ID];
  3616. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3617. {
  3618. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  3619. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  3620. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3621. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  3622. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  3623. }
  3624. return hoverName;
  3625. }
  3626. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  3627. {
  3628. bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  3629. int messageID, bonusType, bonusVal;
  3630. int bonusMove = 0, sound = -1;
  3631. InfoWindow iw;
  3632. iw.player = h->tempOwner;
  3633. GiveBonus gbonus;
  3634. gbonus.id = h->id;
  3635. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  3636. gbonus.bonus.source = Bonus::OBJECT;
  3637. gbonus.bonus.id = ID;
  3638. bool second = false;
  3639. Bonus secondBonus;
  3640. switch(ID)
  3641. {
  3642. case 11: //buoy
  3643. messageID = 21;
  3644. sound = soundBase::MORALE;
  3645. gbonus.bonus.type = Bonus::MORALE;
  3646. gbonus.bonus.val = +1;
  3647. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  3648. break;
  3649. case 14: //swan pond
  3650. messageID = 29;
  3651. sound = soundBase::LUCK;
  3652. gbonus.bonus.type = Bonus::LUCK;
  3653. gbonus.bonus.val = 2;
  3654. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  3655. bonusMove = -h->movement;
  3656. break;
  3657. case 28: //Faerie Ring
  3658. messageID = 49;
  3659. sound = soundBase::LUCK;
  3660. gbonus.bonus.type = Bonus::LUCK;
  3661. gbonus.bonus.val = 1;
  3662. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  3663. break;
  3664. case 30: //fountain of fortune
  3665. messageID = 55;
  3666. sound = soundBase::LUCK;
  3667. gbonus.bonus.type = Bonus::LUCK;
  3668. gbonus.bonus.val = rand()%5 - 1;
  3669. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  3670. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  3671. break;
  3672. case 38: //idol of fortune
  3673. messageID = 62;
  3674. sound = soundBase::experience;
  3675. gbonus.bonus.val = 1;
  3676. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  3677. if(cb->getDate(1) == 7) //7th day of week
  3678. {
  3679. gbonus.bonus.type = Bonus::MORALE;
  3680. second = true;
  3681. secondBonus = gbonus.bonus;
  3682. secondBonus.type = Bonus::LUCK;
  3683. }
  3684. else
  3685. {
  3686. gbonus.bonus.type = (cb->getDate(1)%2) ? Bonus::LUCK : Bonus::MORALE;
  3687. }
  3688. break;
  3689. case 52: //Mermaid
  3690. messageID = 83;
  3691. sound = soundBase::LUCK;
  3692. gbonus.bonus.type = Bonus::LUCK;
  3693. gbonus.bonus.val = 1;
  3694. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  3695. break;
  3696. case 64: //Rally Flag
  3697. sound = soundBase::MORALE;
  3698. messageID = 111;
  3699. gbonus.bonus.type = Bonus::MORALE;
  3700. gbonus.bonus.val = 1;
  3701. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  3702. second = true;
  3703. secondBonus = gbonus.bonus;
  3704. secondBonus.type = Bonus::LUCK;
  3705. bonusMove = 400;
  3706. break;
  3707. case 56: //oasis
  3708. messageID = 95;
  3709. gbonus.bonus.type = Bonus::MORALE;
  3710. gbonus.bonus.val = 1;
  3711. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  3712. bonusMove = 800;
  3713. break;
  3714. case 96: //temple
  3715. messageID = 140;
  3716. iw.soundID = soundBase::temple;
  3717. gbonus.bonus.type = Bonus::MORALE;
  3718. if(cb->getDate(1)==7) //sunday
  3719. {
  3720. gbonus.bonus.val = 2;
  3721. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  3722. }
  3723. else
  3724. {
  3725. gbonus.bonus.val = 1;
  3726. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  3727. }
  3728. break;
  3729. case 110://Watering Hole
  3730. sound = soundBase::MORALE;
  3731. messageID = 166;
  3732. gbonus.bonus.type = Bonus::MORALE;
  3733. gbonus.bonus.val = 1;
  3734. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  3735. bonusMove = 400;
  3736. break;
  3737. case 31: //Fountain of Youth
  3738. sound = soundBase::MORALE;
  3739. messageID = 57;
  3740. gbonus.bonus.type = Bonus::MORALE;
  3741. gbonus.bonus.val = 1;
  3742. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  3743. bonusMove = 400;
  3744. break;
  3745. case 94: //Stables TODO: upgrade Cavaliers
  3746. sound = soundBase::horse20;
  3747. std::set<ui32> slots;
  3748. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); ++i)
  3749. {
  3750. if(i->second.type->idNumber == 10)
  3751. slots.insert(i->first);
  3752. }
  3753. if (!slots.empty())
  3754. {
  3755. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  3756. {
  3757. UpgradeCreature uc(*i, id, 11);
  3758. //uc.applyGh (&gh);
  3759. }
  3760. }
  3761. messageID = 137;
  3762. gbonus.bonus.type = Bonus::LAND_MOVEMENT;
  3763. gbonus.bonus.val = 600;
  3764. bonusMove = 600;
  3765. gbonus.bonus.duration = Bonus::ONE_WEEK;
  3766. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  3767. break;
  3768. }
  3769. if(visited)
  3770. {
  3771. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  3772. messageID--;
  3773. else
  3774. messageID++;
  3775. }
  3776. else
  3777. {
  3778. //TODO: fix if second bonus val != main bonus val
  3779. if(gbonus.bonus.type == Bonus::MORALE || secondBonus.type == Bonus::MORALE)
  3780. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  3781. if(gbonus.bonus.type == Bonus::LUCK || secondBonus.type == Bonus::LUCK)
  3782. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  3783. cb->giveHeroBonus(&gbonus);
  3784. if(second)
  3785. {
  3786. gbonus.bonus = secondBonus;
  3787. cb->giveHeroBonus(&gbonus);
  3788. }
  3789. if(bonusMove) //swan pond - take all move points, stables - give move point this day
  3790. {
  3791. SetMovePoints smp;
  3792. smp.hid = h->id;
  3793. smp.val = h->movement + bonusMove;
  3794. cb->setMovePoints(&smp);
  3795. }
  3796. }
  3797. iw.soundID = sound;
  3798. iw.text << std::pair<ui8,ui32>(11,messageID);
  3799. cb->showInfoDialog(&iw);
  3800. }
  3801. const std::string & CGBonusingObject::getHoverText() const
  3802. {
  3803. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3804. hoverName = VLC->generaltexth->names[ID];
  3805. if(h)
  3806. {
  3807. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  3808. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3809. else
  3810. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3811. }
  3812. return hoverName;
  3813. }
  3814. void CGBonusingObject::initObj()
  3815. {
  3816. if(ID == 11 || ID == 52) //Buoy / Mermaid
  3817. {
  3818. blockVisit = true;
  3819. }
  3820. }
  3821. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  3822. {
  3823. int messageID;
  3824. InfoWindow iw;
  3825. iw.player = h->tempOwner;
  3826. iw.soundID = soundBase::GENIE;
  3827. if (!visited)
  3828. {
  3829. if (h->mana > h->manaLimit())
  3830. messageID = 76;
  3831. else
  3832. {
  3833. messageID = 74;
  3834. cb->setManaPoints (h->id, 2 * h->manaLimit());
  3835. cb->setObjProperty (id, ObjProperty::VISITED, true);
  3836. }
  3837. }
  3838. else
  3839. messageID = 75;
  3840. iw.text << std::pair<ui8,ui32>(11,messageID);
  3841. cb->showInfoDialog(&iw);
  3842. }
  3843. const std::string & CGMagicSpring::getHoverText() const
  3844. {
  3845. hoverName = VLC->generaltexth->names[ID];
  3846. if(!visited)
  3847. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3848. else
  3849. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3850. return hoverName;
  3851. }
  3852. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  3853. {
  3854. int message;
  3855. InfoWindow iw;
  3856. iw.soundID = soundBase::faerie;
  3857. iw.player = h->tempOwner;
  3858. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  3859. {
  3860. message = 78;
  3861. }
  3862. else if(h->mana < h->manaLimit())
  3863. {
  3864. giveDummyBonus(h->id);
  3865. cb->setManaPoints(h->id,h->manaLimit());
  3866. message = 77;
  3867. }
  3868. else
  3869. {
  3870. message = 79;
  3871. }
  3872. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  3873. cb->showInfoDialog(&iw);
  3874. }
  3875. const std::string & CGMagicWell::getHoverText() const
  3876. {
  3877. getNameVis(hoverName);
  3878. return hoverName;
  3879. }
  3880. void CGPandoraBox::initObj()
  3881. {
  3882. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  3883. }
  3884. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  3885. {
  3886. BlockingDialog bd (true, false);
  3887. bd.player = h->getOwner();
  3888. bd.soundID = soundBase::QUEST;
  3889. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  3890. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  3891. }
  3892. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  3893. {
  3894. if (accept)
  3895. {
  3896. if (stacksCount() > 0) //if pandora's box is protected by army
  3897. {
  3898. InfoWindow iw;
  3899. iw.player = h->tempOwner;
  3900. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3901. cb->showInfoDialog(&iw);
  3902. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  3903. }
  3904. else if (message.size() == 0 && resources.size() == 0
  3905. && primskills.size() == 0 && abilities.size() == 0
  3906. && abilityLevels.size() == 0 && artifacts.size() == 0
  3907. && spells.size() == 0 && creatures.Slots().size() > 0
  3908. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  3909. {
  3910. InfoWindow iw;
  3911. iw.player = h->tempOwner;
  3912. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  3913. cb->showInfoDialog(&iw);
  3914. }
  3915. else //if it gives something without battle
  3916. {
  3917. giveContents (h, false);
  3918. }
  3919. }
  3920. }
  3921. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  3922. {
  3923. if(result->winner)
  3924. return;
  3925. giveContents(h,true);
  3926. }
  3927. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  3928. {
  3929. InfoWindow iw;
  3930. iw.player = h->getOwner();
  3931. bool changesPrimSkill = false;
  3932. for (int i = 0; i < primskills.size(); i++)
  3933. {
  3934. if(primskills[i])
  3935. {
  3936. changesPrimSkill = true;
  3937. break;
  3938. }
  3939. }
  3940. if(gainedExp || changesPrimSkill || abilities.size())
  3941. {
  3942. getText(iw,afterBattle,175,h);
  3943. if(gainedExp)
  3944. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  3945. for(int i=0; i<primskills.size(); i++)
  3946. if(primskills[i])
  3947. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  3948. for(int i=0; i<abilities.size(); i++)
  3949. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  3950. cb->showInfoDialog(&iw);
  3951. //give exp
  3952. if(gainedExp)
  3953. cb->changePrimSkill(h->id,4,gainedExp,false);
  3954. //give prim skills
  3955. for(int i=0; i<primskills.size(); i++)
  3956. if(primskills[i])
  3957. cb->changePrimSkill(h->id,i,primskills[i],false);
  3958. //give sec skills
  3959. for(int i=0; i<abilities.size(); i++)
  3960. {
  3961. int curLev = h->getSecSkillLevel(abilities[i]);
  3962. if(curLev && curLev < abilityLevels[i]
  3963. || h->secSkills.size() < SKILL_PER_HERO )
  3964. {
  3965. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  3966. }
  3967. }
  3968. }
  3969. if(spells.size())
  3970. {
  3971. std::set<ui32> spellsToGive;
  3972. iw.components.clear();
  3973. for(int i=0; i<spells.size(); i++)
  3974. {
  3975. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  3976. spellsToGive.insert(spells[i]);
  3977. }
  3978. if(spellsToGive.size())
  3979. {
  3980. cb->changeSpells(h->id,true,spellsToGive);
  3981. cb->showInfoDialog(&iw);
  3982. }
  3983. }
  3984. if(manaDiff)
  3985. {
  3986. getText(iw,afterBattle,manaDiff,176,177,h);
  3987. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  3988. cb->showInfoDialog(&iw);
  3989. cb->setManaPoints(h->id, h->mana + manaDiff);
  3990. }
  3991. if(moraleDiff)
  3992. {
  3993. getText(iw,afterBattle,moraleDiff,178,179,h);
  3994. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  3995. cb->showInfoDialog(&iw);
  3996. GiveBonus gb;
  3997. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id,"");
  3998. gb.id = h->id;
  3999. cb->giveHeroBonus(&gb);
  4000. }
  4001. if(luckDiff)
  4002. {
  4003. getText(iw,afterBattle,luckDiff,180,181,h);
  4004. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  4005. cb->showInfoDialog(&iw);
  4006. GiveBonus gb;
  4007. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id,"");
  4008. gb.id = h->id;
  4009. cb->giveHeroBonus(&gb);
  4010. }
  4011. iw.components.clear();
  4012. iw.text.clear();
  4013. for(int i=0; i<resources.size(); i++)
  4014. {
  4015. if(resources[i] < 0)
  4016. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4017. }
  4018. if(iw.components.size())
  4019. {
  4020. getText(iw,afterBattle,182,h);
  4021. cb->showInfoDialog(&iw);
  4022. }
  4023. iw.components.clear();
  4024. iw.text.clear();
  4025. for(int i=0; i<resources.size(); i++)
  4026. {
  4027. if(resources[i] > 0)
  4028. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4029. }
  4030. if(iw.components.size())
  4031. {
  4032. getText(iw,afterBattle,183,h);
  4033. cb->showInfoDialog(&iw);
  4034. }
  4035. iw.components.clear();
  4036. // getText(iw,afterBattle,183,h);
  4037. for(int i=0; i<artifacts.size(); i++)
  4038. {
  4039. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  4040. if(iw.components.size() >= 14)
  4041. {
  4042. cb->showInfoDialog(&iw);
  4043. iw.components.clear();
  4044. }
  4045. }
  4046. if(iw.components.size())
  4047. {
  4048. cb->showInfoDialog(&iw);
  4049. }
  4050. for(int i=0; i<resources.size(); i++)
  4051. if(resources[i])
  4052. cb->giveResource(h->getOwner(),i,resources[i]);
  4053. for(int i=0; i<artifacts.size(); i++)
  4054. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  4055. iw.components.clear();
  4056. iw.text.clear();
  4057. if (creatures.Slots().size())
  4058. { //this part is taken straight from creature bank
  4059. MetaString loot;
  4060. CCreatureSet ourArmy = creatures;
  4061. for(TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  4062. { //build list of joined creatures
  4063. iw.components.push_back(Component(i->second));
  4064. loot << "%s";
  4065. loot.addReplacement(i->second);
  4066. }
  4067. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second.count == 1)
  4068. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4069. else
  4070. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4071. iw.text.addReplacement (loot.buildList());
  4072. iw.text.addReplacement (h->name);
  4073. cb->showInfoDialog(&iw);
  4074. cb->giveCreatures (id, h, ourArmy);
  4075. }
  4076. if(!afterBattle && message.size())
  4077. {
  4078. iw.text << message;
  4079. cb->showInfoDialog(&iw);
  4080. }
  4081. cb->removeObject(id);
  4082. }
  4083. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  4084. {
  4085. if(afterBattle || !message.size())
  4086. {
  4087. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  4088. iw.text.addReplacement(h->name);
  4089. }
  4090. else
  4091. {
  4092. iw.text << message;
  4093. afterBattle = true;
  4094. }
  4095. }
  4096. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  4097. {
  4098. iw.components.clear();
  4099. iw.text.clear();
  4100. if(afterBattle || !message.size())
  4101. {
  4102. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  4103. iw.text.addReplacement(h->name);
  4104. }
  4105. else
  4106. {
  4107. iw.text << message;
  4108. afterBattle = true;
  4109. }
  4110. }
  4111. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  4112. {
  4113. if(!(availableFor & (1 << h->tempOwner)))
  4114. return;
  4115. if(cb->getPlayerSettings(h->tempOwner)->human)
  4116. {
  4117. if(humanActivate)
  4118. activated(h);
  4119. }
  4120. else if(computerActivate)
  4121. activated(h);
  4122. }
  4123. void CGEvent::activated( const CGHeroInstance * h ) const
  4124. {
  4125. if(stacksCount() > 0)
  4126. {
  4127. InfoWindow iw;
  4128. iw.player = h->tempOwner;
  4129. if(message.size())
  4130. iw.text << message;
  4131. else
  4132. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4133. cb->showInfoDialog(&iw);
  4134. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  4135. }
  4136. else
  4137. {
  4138. giveContents(h,false);
  4139. }
  4140. }
  4141. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  4142. {
  4143. InfoWindow iw;
  4144. iw.player = h->tempOwner;
  4145. switch (ID)
  4146. {
  4147. case 58://redwood observatory
  4148. case 60://pillar of fire
  4149. {
  4150. iw.soundID = soundBase::LIGHTHOUSE;
  4151. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  4152. FoWChange fw;
  4153. fw.player = h->tempOwner;
  4154. fw.mode = 1;
  4155. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  4156. cb->sendAndApply (&fw);
  4157. break;
  4158. }
  4159. case 15://cover of darkness
  4160. {
  4161. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  4162. hideTiles(h->tempOwner, 20);
  4163. break;
  4164. }
  4165. }
  4166. cb->showInfoDialog(&iw);
  4167. }
  4168. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  4169. {
  4170. if(spell == 255)
  4171. {
  4172. tlog1 << "Not initialized shrine visited!\n";
  4173. return;
  4174. }
  4175. if(!hasVisited(h->tempOwner))
  4176. cb->setObjProperty(id,10,h->tempOwner);
  4177. InfoWindow iw;
  4178. iw.soundID = soundBase::temple;
  4179. iw.player = h->getOwner();
  4180. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  4181. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  4182. iw.text << ".";
  4183. if(!h->getArt(17)) //no spellbook
  4184. {
  4185. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  4186. }
  4187. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  4188. {
  4189. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  4190. }
  4191. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  4192. {
  4193. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  4194. }
  4195. else //give spell
  4196. {
  4197. std::set<ui32> spells;
  4198. spells.insert(spell);
  4199. cb->changeSpells(h->id,true,spells);
  4200. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  4201. }
  4202. cb->showInfoDialog(&iw);
  4203. }
  4204. void CGShrine::initObj()
  4205. {
  4206. if(spell == 255) //spell not set
  4207. {
  4208. int level = ID-87;
  4209. std::vector<ui16> possibilities;
  4210. cb->getAllowedSpells (possibilities, level);
  4211. if(!possibilities.size())
  4212. {
  4213. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  4214. return;
  4215. }
  4216. spell = possibilities[ran() % possibilities.size()];
  4217. }
  4218. }
  4219. const std::string & CGShrine::getHoverText() const
  4220. {
  4221. hoverName = VLC->generaltexth->names[ID];
  4222. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4223. {
  4224. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  4225. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  4226. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4227. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  4228. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  4229. }
  4230. return hoverName;
  4231. }
  4232. void CGSignBottle::initObj()
  4233. {
  4234. //if no text is set than we pick random from the predefined ones
  4235. if(!message.size())
  4236. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  4237. if(ID == 59)
  4238. {
  4239. blockVisit = true;
  4240. }
  4241. }
  4242. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  4243. {
  4244. InfoWindow iw;
  4245. iw.soundID = soundBase::STORE;
  4246. iw.player = h->getOwner();
  4247. iw.text << message;
  4248. cb->showInfoDialog(&iw);
  4249. if(ID == 59)
  4250. cb->removeObject(id);
  4251. }
  4252. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  4253. {
  4254. }
  4255. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  4256. {
  4257. int type = bonusType;
  4258. int bid = bonusID;
  4259. //check if the bonus if applicable, if not - give primary skill (always possible)
  4260. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  4261. if((type == 1
  4262. && ((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)
  4263. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)))) //hero doesn't have a spellbook or already knows the spell
  4264. {
  4265. type = 0;
  4266. bid = ran() % PRIMARY_SKILLS;
  4267. }
  4268. InfoWindow iw;
  4269. iw.soundID = soundBase::gazebo;
  4270. iw.player = h->getOwner();
  4271. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  4272. switch (type)
  4273. {
  4274. case 0:
  4275. cb->changePrimSkill(h->id,bid,+1);
  4276. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  4277. break;
  4278. case 1:
  4279. cb->changeSecSkill(h->id,bid,+1);
  4280. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  4281. break;
  4282. case 2:
  4283. {
  4284. std::set<ui32> hlp;
  4285. hlp.insert(bid);
  4286. cb->changeSpells(h->id,true,hlp);
  4287. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  4288. }
  4289. break;
  4290. default:
  4291. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  4292. return;
  4293. }
  4294. cb->showInfoDialog(&iw);
  4295. cb->removeObject(id);
  4296. }
  4297. void CGScholar::initObj()
  4298. {
  4299. blockVisit = true;
  4300. if(bonusType == 255)
  4301. {
  4302. bonusType = ran()%3;
  4303. switch(bonusType)
  4304. {
  4305. case 0:
  4306. bonusID = ran() % PRIMARY_SKILLS;
  4307. break;
  4308. case 1:
  4309. bonusID = ran() % SKILL_QUANTITY;
  4310. break;
  4311. case 2:
  4312. std::vector<ui16> possibilities;
  4313. for (int i = 1; i < 6; ++i)
  4314. cb->getAllowedSpells (possibilities, i);
  4315. bonusID = possibilities[ran() % possibilities.size()];
  4316. break;
  4317. }
  4318. }
  4319. }
  4320. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  4321. {
  4322. if (h->tempOwner != tempOwner && stacksCount() > 0) {
  4323. //TODO: Find a way to apply magic garrison effects in battle.
  4324. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  4325. return;
  4326. }
  4327. //New owner.
  4328. if (h->tempOwner != tempOwner)
  4329. cb->setOwner(id, h->tempOwner);
  4330. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  4331. }
  4332. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  4333. {
  4334. if (result->winner == 0)
  4335. onHeroVisit(h);
  4336. }
  4337. ui8 CGGarrison::getPassableness() const
  4338. {
  4339. return 1<<tempOwner;
  4340. }
  4341. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  4342. {
  4343. int sound = 0;
  4344. int txtid = -1;
  4345. switch(ID)
  4346. {
  4347. case 22: //Corpse
  4348. txtid = 37;
  4349. sound = soundBase::MYSTERY;
  4350. break;
  4351. case 39: //Lean To
  4352. sound = soundBase::GENIE;
  4353. txtid = 64;
  4354. break;
  4355. case 105://Wagon
  4356. sound = soundBase::GENIE;
  4357. txtid = 154;
  4358. break;
  4359. case 108:
  4360. break;
  4361. default:
  4362. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  4363. return;
  4364. }
  4365. if(ID == 108)//Warrior's Tomb
  4366. {
  4367. //ask if player wants to search the Tomb
  4368. BlockingDialog bd(true, false);
  4369. sound = soundBase::GRAVEYARD;
  4370. bd.player = h->getOwner();
  4371. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  4372. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  4373. return;
  4374. }
  4375. InfoWindow iw;
  4376. iw.soundID = sound;
  4377. iw.player = h->getOwner();
  4378. if(players.size()) //we have been already visited...
  4379. {
  4380. txtid++;
  4381. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  4382. txtid++;
  4383. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4384. }
  4385. else //first visit - give bonus!
  4386. {
  4387. switch(artOrRes)
  4388. {
  4389. case 0: // first visit but empty
  4390. if (ID == 22) //Corpse
  4391. ++txtid;
  4392. else
  4393. txtid+=2;
  4394. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4395. break;
  4396. case 1: //art
  4397. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  4398. cb->giveHeroArtifact(bonusType,h->id,-2);
  4399. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4400. if (ID == 22) //Corpse
  4401. {
  4402. iw.text << "%s";
  4403. iw.text.addReplacement (MetaString::ART_NAMES, bonusType);
  4404. }
  4405. break;
  4406. case 2: //res
  4407. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4408. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  4409. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  4410. break;
  4411. }
  4412. if(ID == 105 && artOrRes == 1)
  4413. {
  4414. iw.text.localStrings.back().second++;
  4415. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  4416. }
  4417. }
  4418. cb->showInfoDialog(&iw);
  4419. cb->setObjProperty(id,10,h->getOwner());
  4420. }
  4421. const std::string & CGOnceVisitable::getHoverText() const
  4422. {
  4423. hoverName = VLC->generaltexth->names[ID] + " ";
  4424. hoverName += (hasVisited(cb->getCurrentPlayer())
  4425. ? (VLC->generaltexth->allTexts[352]) //visited
  4426. : ( VLC->generaltexth->allTexts[353])); //not visited
  4427. return hoverName;
  4428. }
  4429. void CGOnceVisitable::initObj()
  4430. {
  4431. switch(ID)
  4432. {
  4433. case 22: //Corpse
  4434. {
  4435. blockVisit = true;
  4436. int hlp = ran()%100;
  4437. if(hlp < 20)
  4438. {
  4439. artOrRes = 1;
  4440. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  4441. }
  4442. else
  4443. {
  4444. artOrRes = 0;
  4445. }
  4446. }
  4447. break;
  4448. case 39: //Lean To
  4449. {
  4450. artOrRes = 2;
  4451. bonusType = ran()%6; //any basic resource without gold
  4452. bonusVal = ran()%4 + 1;
  4453. }
  4454. break;
  4455. case 108://Warrior's Tomb
  4456. {
  4457. artOrRes = 1;
  4458. int hlp = ran()%100;
  4459. if(hlp < 30)
  4460. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  4461. else if(hlp < 80)
  4462. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  4463. else if(hlp < 95)
  4464. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  4465. else
  4466. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  4467. }
  4468. break;
  4469. case 105://Wagon
  4470. {
  4471. int hlp = ran()%100;
  4472. if(hlp < 10)
  4473. {
  4474. artOrRes = 0; // nothing... :(
  4475. }
  4476. else if(hlp < 50) //minor or treasure art
  4477. {
  4478. artOrRes = 1;
  4479. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  4480. }
  4481. else //2 - 5 of non-gold resource
  4482. {
  4483. artOrRes = 2;
  4484. bonusType = ran()%6;
  4485. bonusVal = ran()%4 + 2;
  4486. }
  4487. }
  4488. break;
  4489. }
  4490. }
  4491. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  4492. {
  4493. if(accept)
  4494. {
  4495. InfoWindow iw;
  4496. iw.player = h->getOwner();
  4497. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  4498. if(players.size()) //we've been already visited, player found nothing
  4499. {
  4500. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  4501. }
  4502. else //first visit - give artifact
  4503. {
  4504. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  4505. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  4506. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  4507. cb->giveHeroArtifact(bonusType,h->id,-2);
  4508. }
  4509. if(!h->hasBonusFrom(Bonus::OBJECT,ID)) //we don't have modifier from this object yet
  4510. {
  4511. //ruin morale
  4512. GiveBonus gb;
  4513. gb.id = h->id;
  4514. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,-3,id,"");
  4515. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  4516. cb->giveHeroBonus(&gb);
  4517. }
  4518. cb->showInfoDialog(&iw);
  4519. cb->setObjProperty(id,10,h->getOwner());
  4520. }
  4521. }
  4522. void CBank::initObj()
  4523. {
  4524. switch (ID) //find apriopriate key
  4525. {
  4526. case 16: //bank
  4527. index = subID; break;
  4528. case 24: //derelict ship
  4529. index = 8; break;
  4530. case 25: //utopia
  4531. index = 10; break;
  4532. case 84: //crypt
  4533. index = 9; break;
  4534. case 85: //shipwreck
  4535. index = 7; break;
  4536. }
  4537. bc = NULL;
  4538. daycounter = 0;
  4539. multiplier = 1;
  4540. }
  4541. const std::string & CBank::getHoverText() const
  4542. {
  4543. hoverName = VLC->objh->creBanksNames[index];
  4544. if (bc == NULL)
  4545. hoverName += " " + VLC->generaltexth->allTexts[352];
  4546. else
  4547. hoverName += " " + VLC->generaltexth->allTexts[353];
  4548. return hoverName;
  4549. }
  4550. void CBank::reset(ui16 var1) //prevents desync
  4551. {
  4552. ui8 chance = 0;
  4553. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  4554. {
  4555. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  4556. {
  4557. bc = VLC->objh->banksInfo[index][i];
  4558. break;
  4559. }
  4560. }
  4561. artifacts.clear();
  4562. }
  4563. void CBank::initialize() const
  4564. {
  4565. cb->setObjProperty (id, 14, ran()); //synchronous reset
  4566. for (ui8 i = 0; i <= 3; i++)
  4567. {
  4568. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  4569. {
  4570. switch (i)
  4571. {
  4572. case 0:
  4573. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
  4574. break;
  4575. case 1:
  4576. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
  4577. break;
  4578. case 2:
  4579. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
  4580. break;
  4581. case 3:
  4582. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
  4583. break;
  4584. }
  4585. }
  4586. }
  4587. }
  4588. void CBank::setPropertyDer (ui8 what, ui32 val)
  4589. /// random values are passed as arguments and processed identically on all clients
  4590. {
  4591. switch (what)
  4592. {
  4593. case 11: //daycounter
  4594. if (val == 0)
  4595. daycounter = 0;
  4596. else
  4597. daycounter++;
  4598. break;
  4599. case 12: //multiplier, in percent
  4600. multiplier = ((float)val)/100;
  4601. break;
  4602. case 13: //bank preset
  4603. bc = VLC->objh->banksInfo[index][val];
  4604. break;
  4605. case 14:
  4606. reset (val%100);
  4607. break;
  4608. case 15:
  4609. bc = NULL;
  4610. break;
  4611. case 16: //remove rewards from Derelict Ship
  4612. artifacts.clear();
  4613. break;
  4614. case 17: //set ArmedInstance army
  4615. {
  4616. int upgraded = 0;
  4617. if (val%100 < bc->upgradeChance) //once again anti-desync
  4618. upgraded = 1;
  4619. switch (bc->guards.size())
  4620. {
  4621. case 1:
  4622. for (int i = 0; i < 4; ++i)
  4623. setCreature (i, bc->guards[0].first, bc->guards[0].second / 5 );
  4624. setCreature (4, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  4625. break;
  4626. case 4:
  4627. {
  4628. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  4629. if (bc->guards.back().second) //all stacks are present
  4630. {
  4631. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  4632. {
  4633. setCreature (stacksCount(), it->first, it->second);
  4634. }
  4635. }
  4636. else //split first stack, as in Crypt
  4637. {
  4638. setCreature (0, bc->guards[0].first, bc->guards[0].second / 2 );
  4639. setCreature (1, bc->guards[0].first, bc->guards[0].second - (bc->guards[0].second / 2) );
  4640. setCreature (2, bc->guards[1].first, bc->guards[1].second);
  4641. setCreature (3, bc->guards[2].first + upgraded, bc->guards[2].second);
  4642. }
  4643. }
  4644. break;
  4645. default:
  4646. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  4647. return;
  4648. }
  4649. }
  4650. break;
  4651. case 18: //add Artifact
  4652. artifacts.push_back (val);
  4653. break;
  4654. }
  4655. }
  4656. void CBank::newTurn() const
  4657. {
  4658. if (bc == NULL)
  4659. {
  4660. if (cb->getDate(0) == 1)
  4661. initialize(); //initialize on first day
  4662. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  4663. {
  4664. initialize();
  4665. cb->setObjProperty (id, 11, 0); //daycounter 0
  4666. if (ID == 24 && cb->getDate(0) > 1)
  4667. {
  4668. cb->setObjProperty (id, 12, 0);//ugly hack to make derelict ships usable only once
  4669. cb->setObjProperty (id, 16, 0);
  4670. }
  4671. }
  4672. else
  4673. cb->setObjProperty (id, 11, 1); //daycounter++
  4674. }
  4675. }
  4676. void CBank::onHeroVisit (const CGHeroInstance * h) const
  4677. {
  4678. if (bc)
  4679. {
  4680. int banktext = 0;
  4681. switch (ID)
  4682. {
  4683. case 16: //generic bank
  4684. banktext = 32;
  4685. break;
  4686. case 24:
  4687. banktext = 41;
  4688. break;
  4689. case 25: //utopia
  4690. banktext = 47;
  4691. break;
  4692. case 84: //crypt
  4693. banktext = 119;
  4694. break;
  4695. case 85: //shipwreck
  4696. banktext = 122;
  4697. break;
  4698. }
  4699. BlockingDialog bd (true, false);
  4700. bd.player = h->getOwner();
  4701. bd.soundID = soundBase::DANGER;
  4702. bd.text << VLC->generaltexth->advobtxt[banktext];
  4703. if (ID == 16)
  4704. bd.text.addReplacement (VLC->objh->creBanksNames[index]);
  4705. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4706. }
  4707. else
  4708. {
  4709. InfoWindow iw;
  4710. iw.soundID = soundBase::GRAVEYARD;
  4711. iw.player = h->getOwner();
  4712. if (ID == 84) //morale penalty for empty Crypt
  4713. {
  4714. GiveBonus gbonus;
  4715. gbonus.id = h->id;
  4716. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4717. gbonus.bonus.source = Bonus::OBJECT;
  4718. gbonus.bonus.id = ID;
  4719. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[98];
  4720. gbonus.bonus.type = Bonus::MORALE;
  4721. gbonus.bonus.val = -1;
  4722. cb->giveHeroBonus(&gbonus);
  4723. iw.text << VLC->generaltexth->advobtxt[120];
  4724. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4725. }
  4726. else
  4727. {
  4728. iw.text << VLC->generaltexth->advobtxt[33];
  4729. iw.text.addReplacement (VLC->objh->creBanksNames[index]);
  4730. }
  4731. cb->showInfoDialog(&iw);
  4732. }
  4733. }
  4734. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  4735. {
  4736. if (accept)
  4737. {
  4738. cb->setObjProperty (id, 17, ran()); //get army
  4739. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  4740. }
  4741. }
  4742. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4743. {
  4744. if (result->winner == 0)
  4745. {
  4746. int textID = -1;
  4747. InfoWindow iw;
  4748. iw.player = h->getOwner();
  4749. MetaString loot;
  4750. switch (ID)
  4751. {
  4752. case 16: case 25:
  4753. textID = 34;
  4754. break;
  4755. case 24: //derelict ship
  4756. if (multiplier)
  4757. textID = 43;
  4758. else
  4759. {
  4760. GiveBonus gbonus;
  4761. gbonus.id = h->id;
  4762. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4763. gbonus.bonus.source = Bonus::OBJECT;
  4764. gbonus.bonus.id = ID;
  4765. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  4766. gbonus.bonus.type = Bonus::MORALE;
  4767. gbonus.bonus.val = -1;
  4768. cb->giveHeroBonus(&gbonus);
  4769. textID = 42;
  4770. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4771. }
  4772. break;
  4773. case 84: //Crypt
  4774. if (bc->resources.size() != 0)
  4775. textID = 121;
  4776. else
  4777. {
  4778. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4779. GiveBonus gbonus;
  4780. gbonus.id = h->id;
  4781. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4782. gbonus.bonus.source = Bonus::OBJECT;
  4783. gbonus.bonus.id = ID;
  4784. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  4785. gbonus.bonus.type = Bonus::MORALE;
  4786. gbonus.bonus.val = -1;
  4787. cb->giveHeroBonus(&gbonus);
  4788. textID = 120;
  4789. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4790. }
  4791. break;
  4792. case 85: //shipwreck
  4793. if (bc->resources.size())
  4794. textID = 124;
  4795. else
  4796. textID = 123;
  4797. break;
  4798. }
  4799. //grant resources
  4800. if (textID != 42) //empty derelict ship gives no cash
  4801. {
  4802. for (int it = 0; it < bc->resources.size(); it++)
  4803. {
  4804. if (bc->resources[it] != 0)
  4805. {
  4806. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  4807. loot << "%d %s";
  4808. loot.addReplacement (iw.components.back().val);
  4809. loot.addReplacement (MetaString::RES_NAMES, iw.components.back().subtype);
  4810. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  4811. }
  4812. }
  4813. }
  4814. //grant artifacts
  4815. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  4816. {
  4817. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  4818. loot << "%s";
  4819. loot.addReplacement (MetaString::ART_NAMES, *it);
  4820. cb->giveHeroArtifact (*it, h->id ,-2);
  4821. }
  4822. //display loot
  4823. if (!iw.components.empty())
  4824. {
  4825. if (textID == 34)
  4826. {
  4827. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  4828. iw.text.addReplacement (MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  4829. iw.text.addReplacement (loot.buildList());
  4830. }
  4831. else
  4832. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  4833. cb->showInfoDialog(&iw);
  4834. }
  4835. loot.clear();
  4836. iw.components.clear();
  4837. iw.text.clear();
  4838. //grant creatures
  4839. CCreatureSet ourArmy;
  4840. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  4841. {
  4842. int slot = ourArmy.getSlotFor(it->first);
  4843. ourArmy.addToSlot(slot, it->first, it->second);
  4844. }
  4845. for (TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  4846. {
  4847. iw.components.push_back(Component(i->second));
  4848. loot << "%s";
  4849. loot.addReplacement (i->second);
  4850. }
  4851. if (ourArmy.Slots().size())
  4852. {
  4853. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second.count == 1)
  4854. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4855. else
  4856. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4857. iw.text.addReplacement (loot.buildList());
  4858. iw.text.addReplacement (h->name);
  4859. cb->showInfoDialog(&iw);
  4860. cb->giveCreatures (id, h, ourArmy);
  4861. }
  4862. cb->setObjProperty (id, 15, 0); //bc = NULL
  4863. }
  4864. else //in case of defeat
  4865. initialize();
  4866. }
  4867. void CGPyramid::initObj()
  4868. {
  4869. //would be nice to do that only once
  4870. if (!pyramidConfig.guards.size())
  4871. {
  4872. pyramidConfig.level = 1;
  4873. pyramidConfig.chance = 100;
  4874. pyramidConfig.upgradeChance = 0;
  4875. for (int i = 0; i < 2; ++i)
  4876. {
  4877. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(116, 20));
  4878. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(117, 10));
  4879. }
  4880. pyramidConfig.combatValue; //how hard are guards of this level
  4881. pyramidConfig.value; //overall value of given things
  4882. pyramidConfig.rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  4883. pyramidConfig.easiest; //?!?
  4884. }
  4885. bc = &pyramidConfig;
  4886. std::vector<ui16> available;
  4887. cb->getAllowedSpells (available, 5);
  4888. spell = (available[rand()%available.size()]);
  4889. }
  4890. const std::string & CGPyramid::getHoverText() const
  4891. {
  4892. hoverName = VLC->generaltexth->names[ID];
  4893. if (bc == NULL)
  4894. hoverName += " " + VLC->generaltexth->allTexts[352];
  4895. else
  4896. hoverName += " " + VLC->generaltexth->allTexts[353];
  4897. return hoverName;
  4898. }
  4899. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  4900. {
  4901. if (bc)
  4902. {
  4903. BlockingDialog bd (true, false);
  4904. bd.player = h->getOwner();
  4905. bd.soundID = soundBase::DANGER;
  4906. bd.text << VLC->generaltexth->advobtxt[105];
  4907. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4908. }
  4909. else
  4910. {
  4911. InfoWindow iw;
  4912. iw.player = h->getOwner();
  4913. iw.text << VLC->generaltexth->advobtxt[107];
  4914. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  4915. GiveBonus gb;
  4916. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  4917. gb.id = h->id;
  4918. cb->giveHeroBonus(&gb);
  4919. cb->showInfoDialog(&iw);
  4920. }
  4921. }
  4922. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4923. {
  4924. if (result->winner == 0)
  4925. {
  4926. InfoWindow iw;
  4927. iw.player = h->getOwner();
  4928. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  4929. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  4930. if (!h->getArt(17))
  4931. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  4932. else if (h->getSecSkillLevel(7) < 3)
  4933. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  4934. else
  4935. {
  4936. std::set<ui32> spells;
  4937. spells.insert (spell);
  4938. cb->changeSpells (h->id, true, spells);
  4939. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  4940. }
  4941. cb->showInfoDialog(&iw);
  4942. cb->setObjProperty (id, 15, 0);
  4943. }
  4944. }
  4945. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  4946. {
  4947. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  4948. playerKeyMap.find(what-101)->second.insert(val);
  4949. }
  4950. bool CGKeys::wasMyColorVisited (int player) const
  4951. {
  4952. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  4953. return true;
  4954. else
  4955. return false;
  4956. }
  4957. const std::string & CGKeymasterTent::getHoverText() const
  4958. {
  4959. hoverName = VLC->generaltexth->names[ID];
  4960. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4961. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4962. else
  4963. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4964. return hoverName;
  4965. }
  4966. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  4967. {
  4968. InfoWindow iw;
  4969. iw.soundID = soundBase::CAVEHEAD;
  4970. iw.player = h->getOwner();
  4971. if (!wasMyColorVisited (h->getOwner()) )
  4972. {
  4973. cb->setObjProperty(id, h->tempOwner+101, subID);
  4974. iw.text << std::pair<ui8,ui32>(11,19);
  4975. }
  4976. else
  4977. iw.text << std::pair<ui8,ui32>(11,20);
  4978. cb->showInfoDialog(&iw);
  4979. }
  4980. void CGBorderGuard::initObj()
  4981. {
  4982. blockVisit = true;
  4983. }
  4984. const std::string & CGBorderGuard::getHoverText() const
  4985. {
  4986. hoverName = VLC->generaltexth->names[ID];
  4987. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4988. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4989. else
  4990. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4991. return hoverName;
  4992. }
  4993. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  4994. {
  4995. if (wasMyColorVisited (h->getOwner()) )
  4996. {
  4997. BlockingDialog bd (true, false);
  4998. bd.player = h->getOwner();
  4999. bd.soundID = soundBase::QUEST;
  5000. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  5001. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  5002. }
  5003. else
  5004. {
  5005. InfoWindow iw;
  5006. iw.player = h->getOwner();
  5007. iw.soundID = soundBase::CAVEHEAD;
  5008. iw.text << std::pair<ui8,ui32>(11,18);
  5009. cb->showInfoDialog (&iw);
  5010. }
  5011. }
  5012. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  5013. {
  5014. if (accept)
  5015. cb->removeObject(id);
  5016. }
  5017. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  5018. {
  5019. if (!wasMyColorVisited (h->getOwner()) )
  5020. {
  5021. InfoWindow iw;
  5022. iw.player = h->getOwner();
  5023. iw.text << std::pair<ui8,ui32>(11,18);
  5024. cb->showInfoDialog(&iw);
  5025. }
  5026. }
  5027. ui8 CGBorderGate::getPassableness() const
  5028. {
  5029. ui8 ret = 0;
  5030. for (int i = 0; i < PLAYER_LIMIT; i++)
  5031. ret |= wasMyColorVisited(i)<<i;
  5032. return ret;
  5033. }
  5034. void CGMagi::initObj()
  5035. {
  5036. if (ID == 27)
  5037. {
  5038. blockVisit = true;
  5039. eyelist[subID].push_back(id);
  5040. }
  5041. }
  5042. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  5043. {
  5044. if (ID == 37)
  5045. {
  5046. InfoWindow iw;
  5047. CenterView cv;
  5048. FoWChange fw;
  5049. cv.player = iw.player = fw.player = h->tempOwner;
  5050. iw.soundID = soundBase::LIGHTHOUSE;
  5051. iw.player = h->tempOwner;
  5052. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  5053. cb->showInfoDialog(&iw);
  5054. fw.mode = 1;
  5055. std::vector<si32>::iterator it;
  5056. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  5057. {
  5058. const CGObjectInstance *eye = cb->getObj(*it);
  5059. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  5060. cb->sendAndApply(&fw);
  5061. cv.pos = eye->pos;
  5062. cv.focusTime = 2000;
  5063. cb->sendAndApply(&cv);
  5064. }
  5065. cv.pos = h->getPosition(false);
  5066. cb->sendAndApply(&cv);
  5067. }
  5068. else if (ID == 27)
  5069. {
  5070. InfoWindow iw;
  5071. iw.player = h->tempOwner;
  5072. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  5073. cb->showInfoDialog(&iw);
  5074. }
  5075. }
  5076. void CGBoat::initObj()
  5077. {
  5078. hero = NULL;
  5079. }
  5080. void CGSirens::initObj()
  5081. {
  5082. blockVisit = true;
  5083. }
  5084. const std::string & CGSirens::getHoverText() const
  5085. {
  5086. getNameVis(hoverName);
  5087. return hoverName;
  5088. }
  5089. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  5090. {
  5091. int message;
  5092. InfoWindow iw;
  5093. iw.soundID = soundBase::DANGER;
  5094. iw.player = h->tempOwner;
  5095. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  5096. {
  5097. iw.text.addTxt(11,133);
  5098. }
  5099. else
  5100. {
  5101. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  5102. int xp = 0;
  5103. SetGarrisons sg;
  5104. sg.garrs[h->id] = h->getArmy();
  5105. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  5106. {
  5107. int drown = (int)(i->second.count * 0.3);
  5108. if(drown)
  5109. {
  5110. sg.garrs[h->id].setStackCount(i->first, i->second.count - drown);
  5111. xp += drown * i->second.type->valOfBonuses(Bonus::STACK_HEALTH);
  5112. }
  5113. }
  5114. if(xp)
  5115. {
  5116. iw.text.addTxt(11,132);
  5117. iw.text.addReplacement(xp);
  5118. cb->sendAndApply(&sg);
  5119. }
  5120. else
  5121. {
  5122. iw.text.addTxt(11,134);
  5123. }
  5124. }
  5125. cb->showInfoDialog(&iw);
  5126. }
  5127. //bool IShipyard::validLocation() const
  5128. //{
  5129. // std::vector<int3> offsets;
  5130. // getOutOffsets(offsets);
  5131. //
  5132. // TerrainTile *tile;
  5133. // for(int i = 0; i < offsets.size(); i++)
  5134. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5135. // return true;
  5136. // return false;
  5137. //}
  5138. int3 IBoatGenerator::bestLocation() const
  5139. {
  5140. std::vector<int3> offsets;
  5141. getOutOffsets(offsets);
  5142. TerrainTile *tile;
  5143. for(int i = 0; i < offsets.size(); i++)
  5144. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5145. return o->pos + offsets[i];
  5146. return int3(-1,-1,-1);
  5147. }
  5148. int IBoatGenerator::state() const
  5149. {
  5150. int3 tile = bestLocation();
  5151. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  5152. if(!t)
  5153. return 3; //no water
  5154. else if(!t->blockingObjects.size())
  5155. return 0; //OK
  5156. else if(t->blockingObjects.front()->ID == 8)
  5157. return 1; //blocked with boat
  5158. else
  5159. return 2; //blocked
  5160. }
  5161. int IBoatGenerator::getBoatType() const
  5162. {
  5163. //We make good ships by default
  5164. return 1;
  5165. }
  5166. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  5167. : o(O)
  5168. {
  5169. }
  5170. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  5171. {
  5172. cost.resize(RESOURCE_QUANTITY);
  5173. cost[0] = 10;
  5174. cost[6] = 1000;
  5175. }
  5176. IShipyard::IShipyard(const CGObjectInstance *O)
  5177. : IBoatGenerator(O)
  5178. {
  5179. }
  5180. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  5181. {
  5182. if(obj->ID == TOWNI_TYPE)
  5183. {
  5184. return static_cast<CGTownInstance*>(obj);
  5185. }
  5186. else if(obj->ID == 87)
  5187. {
  5188. return static_cast<CGShipyard*>(obj);
  5189. }
  5190. else
  5191. {
  5192. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  5193. return NULL;
  5194. }
  5195. }
  5196. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  5197. {
  5198. return castFrom(const_cast<CGObjectInstance*>(obj));
  5199. }
  5200. CGShipyard::CGShipyard()
  5201. :IShipyard(this)
  5202. {
  5203. }
  5204. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  5205. {
  5206. 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),
  5207. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  5208. }
  5209. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  5210. {
  5211. if(tempOwner != h->tempOwner)
  5212. cb->setOwner(id, h->tempOwner);
  5213. int s = state();
  5214. if(s)
  5215. {
  5216. InfoWindow iw;
  5217. iw.player = tempOwner;
  5218. switch(s)
  5219. {
  5220. case 1:
  5221. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  5222. break;
  5223. case 2:
  5224. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  5225. break;
  5226. case 3:
  5227. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  5228. return;
  5229. }
  5230. cb->showInfoDialog(&iw);
  5231. }
  5232. else
  5233. {
  5234. OpenWindow ow;
  5235. ow.id1 = id;
  5236. ow.id2 = h->id;
  5237. ow.window = OpenWindow::SHIPYARD_WINDOW;
  5238. cb->sendAndApply(&ow);
  5239. }
  5240. }
  5241. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  5242. {
  5243. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  5244. {
  5245. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  5246. {
  5247. //ask if he wants to buy one
  5248. int text;
  5249. switch (subID)
  5250. {
  5251. case 0:
  5252. text = 25;
  5253. break;
  5254. case 1:
  5255. text = 26;
  5256. break;
  5257. case 2:
  5258. text = 27;
  5259. break;
  5260. default:
  5261. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  5262. }
  5263. BlockingDialog bd (true, false);
  5264. bd.player = h->getOwner();
  5265. bd.soundID = soundBase::LIGHTHOUSE;
  5266. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  5267. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  5268. }
  5269. else //if he cannot afford
  5270. {
  5271. InfoWindow iw;
  5272. iw.player = h->getOwner();
  5273. iw.soundID = soundBase::CAVEHEAD;
  5274. iw.text << std::pair<ui8,ui32>(11,28);
  5275. cb->showInfoDialog (&iw);
  5276. }
  5277. }
  5278. else //if he already visited carographer
  5279. {
  5280. InfoWindow iw;
  5281. iw.player = h->getOwner();
  5282. iw.soundID = soundBase::CAVEHEAD;
  5283. iw.text << std::pair<ui8,ui32>(11,24);
  5284. cb->showInfoDialog (&iw);
  5285. }
  5286. }
  5287. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  5288. {
  5289. if (accept) //if hero wants to buy map
  5290. {
  5291. cb->giveResource (h->tempOwner, 6, -1000);
  5292. FoWChange fw;
  5293. fw.player = h->tempOwner;
  5294. //subIDs of different types of cartographers:
  5295. //water = 0; land = 1; underground = 2;
  5296. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  5297. cb->sendAndApply (&fw);
  5298. cb->setObjProperty (id, 10, h->tempOwner);
  5299. }
  5300. }
  5301. void CShop::newTurn() const
  5302. {
  5303. switch (ID)
  5304. {
  5305. case 7: //ArtMerchant aka. Black Market
  5306. if (cb->getDate(0)%28 == 1)
  5307. {
  5308. cb->setObjProperty (id, 13, 0);
  5309. cb->setObjProperty (id, 14, rand());
  5310. }
  5311. break;
  5312. case 78: //Refugee Camp
  5313. case 95: //Tavern -- global hero pool?
  5314. if (cb->getDate(0)%7 == 1)
  5315. cb->setObjProperty (id, 14, rand());
  5316. break;
  5317. }
  5318. }
  5319. void CShop::setPropertyDer (ui8 what, ui32 val)
  5320. {
  5321. switch (what)
  5322. {
  5323. case 14: //reset - get new items
  5324. reset(val);
  5325. break;
  5326. }
  5327. }
  5328. void CGArtMerchant::reset(ui32 val)
  5329. {//TODO: it should have 2 global pools instead of unique for each merchant:
  5330. // 1) for town merchants - resets every month,
  5331. // 2) for adv. map - resets only on game start
  5332. std::vector<CArtifact*>::iterator index;
  5333. for (ui8 i = 0; i < 3; ++i) //each tier
  5334. {
  5335. int count = 0;
  5336. std::vector<CArtifact*> arts; //to avoid addition of different tiers
  5337. switch (i)
  5338. {
  5339. case 0:
  5340. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  5341. count = 3; // first row - three treasures,
  5342. break;
  5343. case 1:
  5344. cb->getAllowed (arts, CArtifact::ART_MINOR);
  5345. count = 3; // second row three minors
  5346. break;
  5347. case 2:
  5348. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  5349. count = 1; // and a third row - one major
  5350. break;
  5351. }
  5352. for (ui8 n = 0; n < count; n++)
  5353. {
  5354. index = arts.begin() + val % arts.size();
  5355. advMapArts [advMapArts.size()] = new Component (Component::ARTIFACT, (*index)->id, 0, 0);
  5356. arts.erase(index);
  5357. val *= (id + n * i); //randomize
  5358. }
  5359. }
  5360. }
  5361. void CGRefugeeCamp::reset(ui32 val)
  5362. {
  5363. creatureID = VLC->creh->creatures[val%VLC->creh->creatures.size()]->idNumber;
  5364. }
  5365. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  5366. {
  5367. cb->showThievesGuildWindow(id);
  5368. }
  5369. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  5370. {
  5371. InfoWindow iw;
  5372. iw.player = h->tempOwner;
  5373. if(!hasVisited(h->tempOwner))
  5374. {
  5375. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  5376. cb->sendAndApply(&iw);
  5377. cb->setObjProperty(id,20,h->tempOwner); //increment general visited obelisks counter
  5378. OpenWindow ow;
  5379. ow.id1 = h->tempOwner;
  5380. ow.window = OpenWindow::PUZZLE_MAP;
  5381. cb->sendAndApply(&ow);
  5382. cb->setObjProperty(id,10,h->tempOwner); //mark that particular obelisk as visited
  5383. }
  5384. else
  5385. {
  5386. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  5387. cb->sendAndApply(&iw);
  5388. }
  5389. }
  5390. void CGObelisk::initObj()
  5391. {
  5392. obeliskCount++;
  5393. }
  5394. const std::string & CGObelisk::getHoverText() const
  5395. {
  5396. hoverName = VLC->generaltexth->names[ID];
  5397. if(hasVisited(cb->getCurrentPlayer()))
  5398. hoverName += " " + VLC->generaltexth->allTexts[352]; //not visited
  5399. else
  5400. hoverName += " " + VLC->generaltexth->allTexts[353]; //visited
  5401. return hoverName;
  5402. }
  5403. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  5404. {
  5405. CPlayersVisited::setPropertyDer(what, val);
  5406. switch(what)
  5407. {
  5408. case 20:
  5409. assert(val < PLAYER_LIMIT);
  5410. visited[val]++;
  5411. assert(visited[val] <= obeliskCount);
  5412. break;
  5413. }
  5414. }
  5415. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  5416. {
  5417. if(h->tempOwner != tempOwner)
  5418. {
  5419. ui8 oldOwner = tempOwner;
  5420. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  5421. InfoWindow iw;
  5422. iw.player = h->tempOwner;
  5423. iw.text.addTxt(MetaString::ADVOB_TXT, 69);
  5424. iw.soundID = soundBase::LIGHTHOUSE;
  5425. cb->sendAndApply(&iw);
  5426. giveBonusTo(h->tempOwner);
  5427. if(oldOwner < PLAYER_LIMIT) //remove bonus from old owner
  5428. {
  5429. RemoveBonus rb(RemoveBonus::PLAYER);
  5430. rb.whoID = oldOwner;
  5431. rb.source = Bonus::OBJECT;
  5432. rb.id = id;
  5433. cb->sendAndApply(&rb);
  5434. }
  5435. }
  5436. }
  5437. void CGLighthouse::initObj()
  5438. {
  5439. if(tempOwner < PLAYER_LIMIT)
  5440. {
  5441. giveBonusTo(tempOwner);
  5442. }
  5443. }
  5444. const std::string & CGLighthouse::getHoverText() const
  5445. {
  5446. hoverName = VLC->generaltexth->names[ID];
  5447. //TODO: owned by %s player
  5448. return hoverName;
  5449. }
  5450. void CGLighthouse::giveBonusTo( ui8 player ) const
  5451. {
  5452. GiveBonus gb(GiveBonus::PLAYER);
  5453. gb.bonus.type = Bonus::SEA_MOVEMENT;
  5454. gb.bonus.val = 500;
  5455. gb.id = player;
  5456. gb.bonus.duration = Bonus::PERMANENT;
  5457. gb.bonus.source = Bonus::OBJECT;
  5458. gb.bonus.id = id;
  5459. cb->sendAndApply(&gb);
  5460. }
  5461. void CArmedInstance::setArmy(const CCreatureSet &src)
  5462. {
  5463. slots.clear();
  5464. for(TSlots::const_iterator i = src.Slots().begin(); i != src.Slots().end(); i++)
  5465. {
  5466. CStackInstance &inserted = slots[i->first];
  5467. inserted = i->second;
  5468. inserted.armyObj = this;
  5469. }
  5470. }
  5471. CCreatureSet CArmedInstance::getArmy() const
  5472. {
  5473. return *this;
  5474. }
  5475. void CArmedInstance::randomizeArmy(int type)
  5476. {
  5477. int max = VLC->creh->creatures.size();
  5478. for (TSlots::iterator j = slots.begin(); j != slots.end();j++)
  5479. {
  5480. if(j->second.idRand > max)
  5481. {
  5482. if(j->second.idRand % 2)
  5483. j->second.setType(VLC->townh->towns[type].basicCreatures[(j->second.idRand-197) / 2 -1]);
  5484. else
  5485. j->second.setType(VLC->townh->towns[type].upgradedCreatures[(j->second.idRand-197) / 2 -1]);
  5486. j->second.idRand = -1;
  5487. }
  5488. assert(j->second.armyObj == this);
  5489. }
  5490. return;
  5491. }
  5492. void CArmedInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  5493. {
  5494. const PlayerState *p = cb->getPlayerState(tempOwner);
  5495. if(!p) //occurs when initializing starting hero and heroes from the pool, also for neutral armed objects
  5496. out.insert(&cb->gameState()->globalEffects);
  5497. else
  5498. out.insert(p); //hero always inherits bonuses from player
  5499. if(battle)
  5500. out.insert(battle);
  5501. }
  5502. CArmedInstance::CArmedInstance()
  5503. {
  5504. battle = NULL;
  5505. }
  5506. void CArmedInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  5507. {
  5508. CBonusSystemNode::getBonuses(out, selector, root);
  5509. if(!battle)
  5510. {
  5511. //TODO do it clean, unify with BattleInfo version
  5512. if(Selector::matchesType(selector, Bonus::MORALE) || Selector::matchesType(selector, Bonus::LUCK))
  5513. {
  5514. for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  5515. i->second.getBonuses(out, selector, Selector::effectRange(Bonus::ONLY_ALLIED_ARMY), this);
  5516. }
  5517. }
  5518. if(Selector::matchesType(selector, Bonus::MORALE))
  5519. {
  5520. //number of alignments and presence of undead
  5521. if(contains(dynamic_cast<const CStackInstance*>(root)))
  5522. {
  5523. bool archangelInArmy = false;
  5524. bool canMix = hasBonusOfType(Bonus::NONEVIL_ALIGNMENT_MIX);
  5525. std::set<si8> factions;
  5526. for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  5527. {
  5528. // Take Angelic Alliance troop-mixing freedom of non-evil, non-Conflux units into account.
  5529. const si8 faction = i->second.type->faction;
  5530. if (canMix
  5531. && ((faction >= 0 && faction <= 2) || faction == 6 || faction == 7))
  5532. {
  5533. factions.insert(0); // Insert a single faction of the affected group, Castle will do.
  5534. }
  5535. else
  5536. {
  5537. factions.insert(faction);
  5538. }
  5539. if(i->second.type->idNumber == 13)
  5540. archangelInArmy = true;
  5541. }
  5542. if(factions.size() == 1)
  5543. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, +1, id, VLC->generaltexth->arraytxt[115]));//All troops of one alignment +1
  5544. else
  5545. {
  5546. int fcountModifier = 2-factions.size();
  5547. 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
  5548. }
  5549. if(vstd::contains(factions,4))
  5550. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, id, VLC->generaltexth->arraytxt[116]));//Undead in group -1
  5551. }
  5552. }
  5553. }
  5554. bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const
  5555. {
  5556. switch(mode)
  5557. {
  5558. case RESOURCE_RESOURCE:
  5559. {
  5560. float effectiveness = std::min(((float)getMarketEfficiency()+1.0f) / 20.0f, 0.5f);
  5561. float r = VLC->objh->resVals[id1], //value of given resource
  5562. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  5563. if(r>g) //if given resource is more expensive than wanted
  5564. {
  5565. val2 = ceil(r / g);
  5566. val1 = 1;
  5567. }
  5568. else //if wanted resource is more expensive
  5569. {
  5570. val1 = (g / r) + 0.5f;
  5571. val2 = 1;
  5572. }
  5573. }
  5574. break;
  5575. case CREATURE_RESOURCE:
  5576. {
  5577. const float effectivenessArray[] = {0, 0.3, 0.45, 0.50, 0.65, 0.7, 0.85, 0.9, 1};
  5578. float effectiveness = effectivenessArray[std::min(getMarketEfficiency(), 8)];
  5579. float r = VLC->creh->creatures[id1]->cost[6], //value of given creature in gold
  5580. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  5581. if(r>g) //if given resource is more expensive than wanted
  5582. {
  5583. val2 = ceil(r / g);
  5584. val1 = 1;
  5585. }
  5586. else //if wanted resource is more expensive
  5587. {
  5588. val1 = (g / r) + 0.5f;
  5589. val2 = 1;
  5590. }
  5591. }
  5592. break;
  5593. case RESOURCE_PLAYER:
  5594. val1 = 1;
  5595. val2 = 1;
  5596. break;
  5597. default:
  5598. assert(0);
  5599. return false;
  5600. }
  5601. return true;
  5602. }
  5603. bool IMarket::allowsTrade(EMarketMode mode) const
  5604. {
  5605. return false;
  5606. }
  5607. int IMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  5608. {
  5609. switch(mode)
  5610. {
  5611. case RESOURCE_RESOURCE:
  5612. case ARTIFACT_RESOURCE:
  5613. case CREATURE_RESOURCE:
  5614. return -1;
  5615. default:
  5616. return 1;
  5617. }
  5618. }
  5619. std::vector<int> IMarket::availableItemsIds(EMarketMode mode) const
  5620. {
  5621. std::vector<int> ret;
  5622. switch(mode)
  5623. {
  5624. case RESOURCE_RESOURCE:
  5625. case ARTIFACT_RESOURCE:
  5626. case CREATURE_RESOURCE:
  5627. for (int i = 0; i < 7; i++)
  5628. ret.push_back(i);
  5629. break;
  5630. }
  5631. return ret;
  5632. }
  5633. const IMarket * IMarket::castFrom(const CGObjectInstance *obj)
  5634. {
  5635. switch(obj->ID)
  5636. {
  5637. case TOWNI_TYPE:
  5638. return static_cast<const CGTownInstance*>(obj);
  5639. case 7: //Black Market
  5640. case 99: //Trading Post
  5641. case 221: //Trading Post (snow)
  5642. case 213: //Freelancer's Guild
  5643. return static_cast<const CGMarket*>(obj);
  5644. default:
  5645. tlog1 << "Cannot cast to IMarket object with ID " << obj->ID << std::endl;
  5646. return NULL;
  5647. }
  5648. }
  5649. IMarket::IMarket(const CGObjectInstance *O)
  5650. :o(O)
  5651. {
  5652. }
  5653. std::vector<EMarketMode> IMarket::availableModes() const
  5654. {
  5655. std::vector<EMarketMode> ret;
  5656. for (int i = 0; i < MARTKET_AFTER_LAST_PLACEHOLDER; i++)
  5657. if(allowsTrade((EMarketMode)i))
  5658. ret.push_back((EMarketMode)i);
  5659. return ret;
  5660. }
  5661. void CGMarket::onHeroVisit(const CGHeroInstance * h) const
  5662. {
  5663. OpenWindow ow;
  5664. ow.id1 = id;
  5665. ow.id2 = h->id;
  5666. ow.window = OpenWindow::MARKET_WINDOW;
  5667. cb->sendAndApply(&ow);
  5668. }
  5669. void CGMarket::initObj()
  5670. {
  5671. }
  5672. void CGMarket::newTurn() const
  5673. {
  5674. }
  5675. int CGMarket::getMarketEfficiency() const
  5676. {
  5677. return 5;
  5678. }
  5679. bool CGMarket::allowsTrade(EMarketMode mode) const
  5680. {
  5681. switch(mode)
  5682. {
  5683. case RESOURCE_RESOURCE:
  5684. case RESOURCE_PLAYER:
  5685. switch(ID)
  5686. {
  5687. case 99: //Trading Post
  5688. case 221: //Trading Post (snow)
  5689. return true;
  5690. default:
  5691. return false;
  5692. }
  5693. case CREATURE_RESOURCE:
  5694. return ID == 213; //Freelancer's Guild
  5695. }
  5696. return false;
  5697. }
  5698. int CGMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  5699. {
  5700. return -1;
  5701. }
  5702. std::vector<int> CGMarket::availableItemsIds(EMarketMode mode) const
  5703. {
  5704. switch(mode)
  5705. {
  5706. case RESOURCE_RESOURCE:
  5707. case RESOURCE_PLAYER:
  5708. return IMarket::availableItemsIds(mode);
  5709. default:
  5710. return std::vector<int>();
  5711. }
  5712. }
  5713. CGMarket::CGMarket()
  5714. :IMarket(this)
  5715. {
  5716. }