CObjectHandler.cpp 163 KB

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