2
0

CObjectHandler.cpp 188 KB

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