CObjectHandler.cpp 184 KB

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