CObjectHandler.cpp 184 KB

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