CObjectHandler.cpp 169 KB

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