2
0

CObjectHandler.cpp 183 KB

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