CObjectHandler.cpp 161 KB

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