CObjectHandler.cpp 185 KB

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