CObjectHandler.cpp 186 KB

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