CObjectHandler.cpp 186 KB

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