CObjectHandler.cpp 188 KB

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