CObjectHandler.cpp 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068
  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.find(0)->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. }
  2697. }
  2698. void CGCreature::initObj()
  2699. {
  2700. blockVisit = true;
  2701. switch(character)
  2702. {
  2703. case 0:
  2704. character = 0;
  2705. break;
  2706. case 1:
  2707. character = 1 + ran()%7;
  2708. break;
  2709. case 2:
  2710. character = 1 + ran()%10;
  2711. break;
  2712. case 3:
  2713. character = 4 + ran()%7;
  2714. break;
  2715. case 4:
  2716. character = 10;
  2717. break;
  2718. }
  2719. stacks[0]->setType(subID);
  2720. TQuantity &amount = stacks[0]->count;
  2721. CCreature &c = *VLC->creh->creatures[subID];
  2722. if(!amount)
  2723. {
  2724. if(c.ammMax == c.ammMin)
  2725. amount = c.ammMax;
  2726. else
  2727. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2728. }
  2729. temppower = stacks[0]->count * 1000;
  2730. }
  2731. void CGCreature::newTurn() const
  2732. {//Works only for stacks of single type of size up to 2 millions
  2733. if (stacks.begin()->second->count < CREEP_SIZE && cb->getDate(1) == 1 && cb->getDate(0) > 1)
  2734. {
  2735. ui32 power = temppower * (100 + WEEKLY_GROWTH)/100;
  2736. cb->setObjProperty(id, 10, std::min (power/1000 , (ui32)CREEP_SIZE)); //set new amount
  2737. cb->setObjProperty(id, 11, power); //increase temppower
  2738. }
  2739. if (STACK_EXP)
  2740. cb->setObjProperty(id, 12, 10000); //for testing purpose
  2741. }
  2742. void CGCreature::setPropertyDer(ui8 what, ui32 val)
  2743. {
  2744. switch (what)
  2745. {
  2746. case 10:
  2747. stacks[0]->count = val;
  2748. break;
  2749. case 11:
  2750. temppower = val;
  2751. break;
  2752. case 12:
  2753. giveStackExp(val);
  2754. break;
  2755. }
  2756. }
  2757. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2758. {
  2759. double hlp = h->getTotalStrength() / getArmyStrength();
  2760. if(!character) //compliant creatures will always join
  2761. return 0;
  2762. else if(allowJoin)//test for joining
  2763. {
  2764. int factor;
  2765. if(hlp >= 7)
  2766. factor = 11;
  2767. else if(hlp >= 1)
  2768. factor = (int)(2*(hlp-1));
  2769. else if(hlp >= 0.5)
  2770. factor = -1;
  2771. else if(hlp >= 0.333)
  2772. factor = -2;
  2773. else
  2774. factor = -3;
  2775. int sympathy = 0;
  2776. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2777. myKindCres.insert(subID); //we
  2778. myKindCres.insert(VLC->creh->creatures[subID]->upgrades.begin(),VLC->creh->creatures[subID]->upgrades.end()); //our upgrades
  2779. for(std::vector<ConstTransitivePtr<CCreature> >::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2780. if(vstd::contains((*i)->upgrades, (ui32) id)) //it's our base creatures
  2781. myKindCres.insert((*i)->idNumber);
  2782. int count = 0, //how many creatures of our kind has hero
  2783. totalCount = 0;
  2784. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  2785. {
  2786. if(vstd::contains(myKindCres,i->second->type->idNumber))
  2787. count += i->second->count;
  2788. totalCount += i->second->count;
  2789. }
  2790. if(count*2 > totalCount)
  2791. sympathy++;
  2792. if(count)
  2793. sympathy++;
  2794. int charisma = factor + h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) + sympathy;
  2795. if(charisma >= character) //creatures might join...
  2796. {
  2797. if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) + sympathy + 1 >= character)
  2798. return 0; //join for free
  2799. else if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) * 2 + sympathy + 1 >= character)
  2800. return VLC->creh->creatures[subID]->cost[6] * getStackCount(0); //join for gold
  2801. }
  2802. }
  2803. //we are still here - creatures not joined heroes, test for fleeing
  2804. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2805. //TODO: should be deterministic (will be needed for Vision spell)
  2806. int hlp2 = (int) (hlp - 2)*1000;
  2807. if(!neverFlees
  2808. && hlp2 >= 0
  2809. && rand()%2000 < hlp2
  2810. )
  2811. return -1; //flee
  2812. else
  2813. return -2; //fight
  2814. }
  2815. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2816. {
  2817. if(pursue)
  2818. {
  2819. fight(h);
  2820. }
  2821. else
  2822. {
  2823. cb->removeObject(id);
  2824. }
  2825. }
  2826. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2827. {
  2828. if(!accept)
  2829. {
  2830. if(takenAction(h,false) == -1) //they flee
  2831. {
  2832. flee(h);
  2833. }
  2834. else //they fight
  2835. {
  2836. InfoWindow iw;
  2837. iw.player = h->tempOwner;
  2838. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2839. cb->showInfoDialog(&iw);
  2840. fight(h);
  2841. }
  2842. }
  2843. else //accepted
  2844. {
  2845. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2846. {
  2847. InfoWindow iw;
  2848. iw.player = h->tempOwner;
  2849. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2850. cb->showInfoDialog(&iw);
  2851. //act as if player refused
  2852. joinDecision(h,cost,false);
  2853. return;
  2854. }
  2855. //take gold
  2856. if(cost)
  2857. cb->giveResource(h->tempOwner,6,-cost);
  2858. cb->tryJoiningArmy(this, h, true, false);
  2859. }
  2860. }
  2861. void CGCreature::fight( const CGHeroInstance *h ) const
  2862. {
  2863. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2864. }
  2865. void CGCreature::flee( const CGHeroInstance * h ) const
  2866. {
  2867. BlockingDialog ynd(true,false);
  2868. ynd.player = h->tempOwner;
  2869. ynd.text << std::pair<ui8,ui32>(11,91);
  2870. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2871. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2872. }
  2873. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2874. {
  2875. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  2876. if(relations == 2) //we're visiting our mine
  2877. {
  2878. cb->showGarrisonDialog(id,h->id,true,0);
  2879. return;
  2880. }
  2881. else if (relations == 1)//ally
  2882. return;
  2883. if(stacksCount()) //Mine is guarded
  2884. {
  2885. BlockingDialog ynd(true,false);
  2886. ynd.player = h->tempOwner;
  2887. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  2888. cb->showBlockingDialog(&ynd,boost::bind(&CGMine::fight, this, _1, h));
  2889. return;
  2890. }
  2891. flagMine(h->tempOwner);
  2892. }
  2893. void CGMine::newTurn() const
  2894. {
  2895. if(cb->getDate() == 1)
  2896. return;
  2897. if (tempOwner == NEUTRAL_PLAYER)
  2898. return;
  2899. cb->giveResource(tempOwner, producedResource, producedQuantity);
  2900. }
  2901. void CGMine::initObj()
  2902. {
  2903. if(subID >= 7) //Abandoned Mine
  2904. {
  2905. //set guardians
  2906. int howManyTroglodytes = 100 + ran()%100;
  2907. CStackInstance *troglodytes = new CStackInstance(70, howManyTroglodytes);
  2908. putStack(0, troglodytes);
  2909. //after map reading tempOwner placeholds bitmask for allowed resources
  2910. std::vector<int> possibleResources;
  2911. for (int i = 0; i < 8; i++)
  2912. if(tempOwner & 1<<i)
  2913. possibleResources.push_back(i);
  2914. assert(possibleResources.size());
  2915. producedResource = possibleResources[ran()%possibleResources.size()];
  2916. tempOwner = NEUTRAL_PLAYER;
  2917. hoverName = VLC->generaltexth->mines[7].first + "\n" + VLC->generaltexth->allTexts[202] + " " + troglodytes->getQuantityTXT(false) + " " + troglodytes->type->namePl;
  2918. }
  2919. else
  2920. {
  2921. producedResource = subID;
  2922. MetaString ms;
  2923. ms << std::pair<ui8,ui32>(9,producedResource);
  2924. if(tempOwner >= PLAYER_LIMIT)
  2925. tempOwner = NEUTRAL_PLAYER;
  2926. else
  2927. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2928. ms.toString(hoverName);
  2929. }
  2930. producedQuantity = defaultResProduction();
  2931. }
  2932. void CGMine::fight(ui32 agreed, const CGHeroInstance *h) const
  2933. {
  2934. cb->startBattleI(h, this, boost::bind(&CGMine::endBattle, this, _1, h->tempOwner));
  2935. }
  2936. void CGMine::endBattle(BattleResult *result, ui8 attackingPlayer) const
  2937. {
  2938. if(result->winner == 0) //attacker won
  2939. {
  2940. if(subID == 7)
  2941. {
  2942. InfoWindow iw;
  2943. iw.player = attackingPlayer;
  2944. iw.text.addTxt(MetaString::ADVOB_TXT, 85);
  2945. cb->showInfoDialog(&iw);
  2946. }
  2947. flagMine(attackingPlayer);
  2948. }
  2949. }
  2950. void CGMine::flagMine(ui8 player) const
  2951. {
  2952. assert(tempOwner != player);
  2953. cb->setOwner(id,player); //not ours? flag it!
  2954. MetaString ms;
  2955. ms << std::pair<ui8,ui32>(9,subID) << "\n(" << std::pair<ui8,ui32>(6,23+player) << ")";
  2956. if(subID == 7)
  2957. {
  2958. ms << "(%s)";
  2959. ms.addReplacement(MetaString::RES_NAMES, producedResource);
  2960. }
  2961. cb->setHoverName(id,&ms);
  2962. InfoWindow iw;
  2963. iw.soundID = soundBase::FLAGMINE;
  2964. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  2965. iw.player = player;
  2966. iw.components.push_back(Component(2,producedResource,producedQuantity,-1));
  2967. cb->showInfoDialog(&iw);
  2968. }
  2969. ui32 CGMine::defaultResProduction()
  2970. {
  2971. switch(producedResource)
  2972. {
  2973. case 0: //wood
  2974. case 2: //ore
  2975. return 2;
  2976. case 6: //gold
  2977. return 1000;
  2978. default:
  2979. return 1;
  2980. }
  2981. }
  2982. void CGResource::initObj()
  2983. {
  2984. blockVisit = true;
  2985. hoverName = VLC->generaltexth->restypes[subID];
  2986. if(!amount)
  2987. {
  2988. switch(subID)
  2989. {
  2990. case 6:
  2991. amount = 500 + (rand()%6)*100;
  2992. break;
  2993. case 0: case 2:
  2994. amount = 6 + (rand()%5);
  2995. break;
  2996. default:
  2997. amount = 3 + (rand()%3);
  2998. break;
  2999. }
  3000. }
  3001. }
  3002. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  3003. {
  3004. if(stacksCount())
  3005. {
  3006. if(message.size())
  3007. {
  3008. BlockingDialog ynd(true,false);
  3009. ynd.player = h->getOwner();
  3010. ynd.text << message;
  3011. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  3012. }
  3013. else
  3014. {
  3015. fightForRes(1,h);
  3016. }
  3017. }
  3018. else
  3019. {
  3020. if(message.length())
  3021. {
  3022. InfoWindow iw;
  3023. iw.player = h->tempOwner;
  3024. iw.text << message;
  3025. cb->showInfoDialog(&iw);
  3026. }
  3027. collectRes(h->getOwner());
  3028. }
  3029. }
  3030. void CGResource::collectRes( int player ) const
  3031. {
  3032. cb->giveResource(player,subID,amount);
  3033. ShowInInfobox sii;
  3034. sii.player = player;
  3035. sii.c = Component(2,subID,amount,0);
  3036. sii.text << std::pair<ui8,ui32>(11,113);
  3037. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  3038. cb->showCompInfo(&sii);
  3039. cb->removeObject(id);
  3040. }
  3041. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  3042. {
  3043. if(agreed)
  3044. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  3045. }
  3046. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3047. {
  3048. if(result->winner == 0) //attacker won
  3049. collectRes(h->getOwner());
  3050. }
  3051. void CGVisitableOPW::newTurn() const
  3052. {
  3053. if (cb->getDate(1) == 1) //first day of week = 1
  3054. {
  3055. cb->setObjProperty(id, ObjProperty::VISITED, false);
  3056. MetaString ms; //set text to "not visited"
  3057. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  3058. cb->setHoverName(id,&ms);
  3059. }
  3060. }
  3061. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  3062. {
  3063. int mid, sound = 0;
  3064. switch (ID)
  3065. {
  3066. case 55: //mystical garden
  3067. sound = soundBase::experience;
  3068. mid = 92;
  3069. break;
  3070. case 112://windmill
  3071. sound = soundBase::GENIE;
  3072. mid = 170;
  3073. break;
  3074. case 109://waterwheel
  3075. sound = soundBase::GENIE;
  3076. mid = 164;
  3077. break;
  3078. }
  3079. if (visited)
  3080. {
  3081. if (ID!=112)
  3082. mid++;
  3083. else
  3084. mid--;
  3085. InfoWindow iw;
  3086. iw.soundID = sound;
  3087. iw.player = h->tempOwner;
  3088. iw.text << std::pair<ui8,ui32>(11,mid);
  3089. cb->showInfoDialog(&iw);
  3090. }
  3091. else
  3092. {
  3093. int type, sub, val;
  3094. type = 2;
  3095. switch (ID)
  3096. {
  3097. case 55:
  3098. if (rand()%2)
  3099. {
  3100. sub = 5;
  3101. val = 5;
  3102. }
  3103. else
  3104. {
  3105. sub = 6;
  3106. val = 500;
  3107. }
  3108. break;
  3109. case 112:
  3110. mid = 170;
  3111. sub = (rand() % 5) + 1;
  3112. val = (rand() % 4) + 3;
  3113. break;
  3114. case 109:
  3115. mid = 164;
  3116. sub = 6;
  3117. if(cb->getDate(0)<8)
  3118. val = 500;
  3119. else
  3120. val = 1000;
  3121. }
  3122. cb->giveResource(h->tempOwner,sub,val);
  3123. InfoWindow iw;
  3124. iw.soundID = sound;
  3125. iw.player = h->tempOwner;
  3126. iw.components.push_back(Component(type,sub,val,0));
  3127. iw.text << std::pair<ui8,ui32>(11,mid);
  3128. cb->showInfoDialog(&iw);
  3129. cb->setObjProperty(id, ObjProperty::VISITED, true);
  3130. MetaString ms; //set text to "visited"
  3131. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  3132. cb->setHoverName(id,&ms);
  3133. }
  3134. }
  3135. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  3136. {
  3137. if(what == ObjProperty::VISITED)
  3138. visited = val;
  3139. }
  3140. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  3141. {
  3142. int destinationid=-1;
  3143. switch(ID)
  3144. {
  3145. case 43: //one way - find corresponding exit monolith
  3146. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  3147. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  3148. else
  3149. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3150. break;
  3151. case 45://two way monolith - pick any other one
  3152. case 111: //Whirlpool
  3153. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  3154. {
  3155. while ((destinationid = objs[ID][subID][rand()%objs[ID][subID].size()]) == id); //choose another exit
  3156. if (ID == 111)
  3157. {
  3158. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  3159. {
  3160. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  3161. { //we can't remove last unit
  3162. TSlot targetstack = h->Slots().begin()->first; //slot numbers may vary
  3163. for(TSlots::const_reverse_iterator i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  3164. {
  3165. if (h->getPower(targetstack) > h->getPower(i->first))
  3166. {
  3167. targetstack = (i->first);
  3168. }
  3169. }
  3170. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  3171. amax(countToTake, 1);
  3172. InfoWindow iw;
  3173. iw.player = h->tempOwner;
  3174. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  3175. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  3176. cb->showInfoDialog(&iw);
  3177. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  3178. }
  3179. }
  3180. }
  3181. }
  3182. else
  3183. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3184. break;
  3185. case 103: //find nearest subterranean gate on the other level
  3186. {
  3187. int i=0;
  3188. for(; i < gates.size(); i++)
  3189. {
  3190. if(gates[i].first == id)
  3191. {
  3192. destinationid = gates[i].second;
  3193. break;
  3194. }
  3195. else if(gates[i].second == id)
  3196. {
  3197. destinationid = gates[i].first;
  3198. break;
  3199. }
  3200. }
  3201. if(destinationid < 0 || i == gates.size()) //no exit
  3202. {
  3203. InfoWindow iw;
  3204. iw.player = h->tempOwner;
  3205. iw.text.addTxt(MetaString::ADVOB_TXT, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  3206. cb->sendAndApply(&iw);
  3207. }
  3208. break;
  3209. }
  3210. }
  3211. if(destinationid < 0)
  3212. {
  3213. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  3214. return;
  3215. }
  3216. if (ID == 111)
  3217. {
  3218. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  3219. std::set<int3>::iterator it = tiles.begin();
  3220. std::advance (it, rand() % tiles.size()); //picking random element of set is tiring
  3221. cb->moveHero (h->id, *it + int3(1,0,0), true);
  3222. }
  3223. else
  3224. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  3225. }
  3226. void CGTeleport::initObj()
  3227. {
  3228. int si = subID;
  3229. switch (ID)
  3230. {
  3231. case 103://ignore subterranean gates subid
  3232. case 111:
  3233. {
  3234. si = 0;
  3235. break;
  3236. }
  3237. default:
  3238. break;
  3239. }
  3240. objs[ID][si].push_back(id);
  3241. }
  3242. void CGTeleport::postInit() //matches subterranean gates into pairs
  3243. {
  3244. //split on underground and surface gates
  3245. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  3246. for(size_t i = 0; i < objs[103][0].size(); i++)
  3247. {
  3248. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  3249. gatesSplit[hlp->pos.z].push_back(hlp);
  3250. }
  3251. //sort by position
  3252. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  3253. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  3254. {
  3255. const CGObjectInstance *cur = gatesSplit[0][i];
  3256. //find nearest underground exit
  3257. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  3258. for(int j = 0; j < gatesSplit[1].size(); j++)
  3259. {
  3260. const CGObjectInstance *checked = gatesSplit[1][j];
  3261. if(!checked)
  3262. continue;
  3263. double hlp = checked->pos.dist2d(cur->pos);
  3264. if(hlp < best.second)
  3265. {
  3266. best.first = j;
  3267. best.second = hlp;
  3268. }
  3269. }
  3270. if(best.first >= 0) //found pair
  3271. {
  3272. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  3273. gatesSplit[1][best.first] = NULL;
  3274. }
  3275. else
  3276. {
  3277. gates.push_back(std::pair<int, int>(cur->id, -1));
  3278. }
  3279. }
  3280. objs.erase(103);
  3281. }
  3282. void CGArtifact::initObj()
  3283. {
  3284. blockVisit = true;
  3285. if(ID == 5)
  3286. {
  3287. hoverName = VLC->arth->artifacts[subID]->Name();
  3288. if(!storedArtifact->artType)
  3289. storedArtifact->setType(VLC->arth->artifacts[subID]);
  3290. }
  3291. if(ID == 93)
  3292. subID = 1;
  3293. assert(storedArtifact->artType);
  3294. assert(storedArtifact->parents.size());
  3295. }
  3296. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  3297. {
  3298. if(!stacksCount())
  3299. {
  3300. InfoWindow iw;
  3301. iw.player = h->tempOwner;
  3302. switch(ID)
  3303. {
  3304. case 5:
  3305. {
  3306. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  3307. iw.components.push_back(Component(4,subID,0,0));
  3308. if(message.length())
  3309. iw.text << message;
  3310. else
  3311. iw.text << std::pair<ui8,ui32>(12,subID);
  3312. }
  3313. break;
  3314. case 93:
  3315. {
  3316. int spellID = storedArtifact->getGivenSpellID();
  3317. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  3318. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  3319. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  3320. }
  3321. break;
  3322. }
  3323. cb->showInfoDialog(&iw);
  3324. pick(h);
  3325. }
  3326. else
  3327. {
  3328. if(message.size())
  3329. {
  3330. BlockingDialog ynd(true,false);
  3331. ynd.player = h->getOwner();
  3332. ynd.text << message;
  3333. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  3334. }
  3335. else
  3336. {
  3337. fightForArt(0,h);
  3338. }
  3339. }
  3340. }
  3341. void CGArtifact::pick(const CGHeroInstance * h) const
  3342. {
  3343. cb->giveHeroArtifact(h, storedArtifact, -2);
  3344. cb->removeObject(id);
  3345. }
  3346. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  3347. {
  3348. if(agreed)
  3349. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  3350. }
  3351. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3352. {
  3353. if(result->winner == 0) //attacker won
  3354. pick(h);
  3355. }
  3356. void CGPickable::initObj()
  3357. {
  3358. blockVisit = true;
  3359. switch(ID)
  3360. {
  3361. case 12: //campfire
  3362. val2 = (ran()%3) + 4; //4 - 6
  3363. val1 = val2 * 100;
  3364. type = ran()%6; //given resource
  3365. break;
  3366. case 29: //floatsam
  3367. switch(type = ran()%4)
  3368. {
  3369. case 0:
  3370. val1 = val2 = 0;
  3371. break;
  3372. case 1:
  3373. val1 = 5;
  3374. val2 = 0;
  3375. break;
  3376. case 2:
  3377. val1 = 5;
  3378. val2 = 200;
  3379. break;
  3380. case 3:
  3381. val1 = 10;
  3382. val2 = 500;
  3383. break;
  3384. }
  3385. break;
  3386. case 82: //sea chest
  3387. {
  3388. int hlp = ran()%100;
  3389. if(hlp < 20)
  3390. {
  3391. val1 = 0;
  3392. type = 0;
  3393. }
  3394. else if(hlp < 90)
  3395. {
  3396. val1 = 1500;
  3397. type = 2;
  3398. }
  3399. else
  3400. {
  3401. val1 = 1000;
  3402. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3403. type = 1;
  3404. }
  3405. }
  3406. break;
  3407. case 86: //Shipwreck Survivor
  3408. {
  3409. int hlp = ran()%100;
  3410. if(hlp < 55)
  3411. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3412. else if(hlp < 75)
  3413. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  3414. else if(hlp < 95)
  3415. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  3416. else
  3417. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  3418. }
  3419. break;
  3420. case 101: //treasure chest
  3421. {
  3422. int hlp = ran()%100;
  3423. if(hlp >= 95)
  3424. {
  3425. type = 1;
  3426. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3427. return;
  3428. }
  3429. else if (hlp >= 65)
  3430. {
  3431. val1 = 2000;
  3432. }
  3433. else if(hlp >= 33)
  3434. {
  3435. val1 = 1500;
  3436. }
  3437. else
  3438. {
  3439. val1 = 1000;
  3440. }
  3441. val2 = val1 - 500;
  3442. type = 0;
  3443. break;
  3444. }
  3445. }
  3446. }
  3447. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  3448. {
  3449. switch(ID)
  3450. {
  3451. case 12: //campfire
  3452. {
  3453. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  3454. cb->giveResource(h->tempOwner,6,val1);//gold
  3455. InfoWindow iw;
  3456. iw.soundID = soundBase::experience;
  3457. iw.player = h->tempOwner;
  3458. iw.components.push_back(Component(2,6,val1,0));
  3459. iw.components.push_back(Component(2,type,val2,0));
  3460. iw.text << std::pair<ui8,ui32>(11,23);
  3461. cb->showInfoDialog(&iw);
  3462. break;
  3463. }
  3464. case 29: //flotsam
  3465. {
  3466. cb->giveResource(h->tempOwner,0,val1); //wood
  3467. cb->giveResource(h->tempOwner,6,val2);//gold
  3468. InfoWindow iw;
  3469. iw.soundID = soundBase::GENIE;
  3470. iw.player = h->tempOwner;
  3471. if(val1)
  3472. iw.components.push_back(Component(2,0,val1,0));
  3473. if(val2)
  3474. iw.components.push_back(Component(2,6,val2,0));
  3475. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  3476. cb->showInfoDialog(&iw);
  3477. break;
  3478. }
  3479. case 82: //Sea Chest
  3480. {
  3481. InfoWindow iw;
  3482. iw.soundID = soundBase::chest;
  3483. iw.player = h->tempOwner;
  3484. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  3485. if(val1) //there is gold
  3486. {
  3487. iw.components.push_back(Component(2,6,val1,0));
  3488. cb->giveResource(h->tempOwner,6,val1);
  3489. }
  3490. if(type == 1) //art
  3491. {
  3492. //TODO: what if no space in backpack?
  3493. iw.components.push_back(Component(4, val2, 1, 0));
  3494. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  3495. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val2],-2);
  3496. }
  3497. cb->showInfoDialog(&iw);
  3498. break;
  3499. }
  3500. case 86: //Shipwreck Survivor
  3501. {
  3502. //TODO: what if no space in backpack?
  3503. InfoWindow iw;
  3504. iw.soundID = soundBase::experience;
  3505. iw.player = h->tempOwner;
  3506. iw.components.push_back(Component(4,val1,1,0));
  3507. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  3508. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3509. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3510. cb->showInfoDialog(&iw);
  3511. break;
  3512. }
  3513. case 101: //treasure chest
  3514. {
  3515. if (subID) //not OH3 treasure chest
  3516. {
  3517. tlog2 << "Not supported WoG treasure chest!\n";
  3518. return;
  3519. }
  3520. if(type) //there is an artifact
  3521. {
  3522. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3523. InfoWindow iw;
  3524. iw.soundID = soundBase::treasure;
  3525. iw.player = h->tempOwner;
  3526. iw.components.push_back(Component(4,val1,1,0));
  3527. iw.text << std::pair<ui8,ui32>(11,145);
  3528. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3529. cb->showInfoDialog(&iw);
  3530. break;
  3531. }
  3532. else
  3533. {
  3534. BlockingDialog sd(false,true);
  3535. sd.player = h->tempOwner;
  3536. sd.text << std::pair<ui8,ui32>(11,146);
  3537. sd.components.push_back(Component(2,6,val1,0));
  3538. expType expVal = h->calculateXp(val2);
  3539. sd.components.push_back(Component(5,0,expVal, 0));
  3540. sd.soundID = soundBase::chest;
  3541. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  3542. cb->showBlockingDialog(&sd,fun);
  3543. return;
  3544. }
  3545. }
  3546. }
  3547. cb->removeObject(id);
  3548. }
  3549. void CGPickable::chosen( int which, int heroID ) const
  3550. {
  3551. const CGHeroInstance *h = cb->getHero(heroID);
  3552. switch(which)
  3553. {
  3554. case 1: //player pick gold
  3555. cb->giveResource(cb->getOwner(heroID),6,val1);
  3556. break;
  3557. case 2: //player pick exp
  3558. cb->changePrimSkill(heroID, 4, h->calculateXp(val2));
  3559. break;
  3560. default:
  3561. throw std::string("Unhandled treasure choice");
  3562. }
  3563. cb->removeObject(id);
  3564. }
  3565. bool CQuest::checkQuest (const CGHeroInstance * h) const
  3566. {
  3567. switch (missionType)
  3568. {
  3569. case MISSION_NONE:
  3570. return true;
  3571. case MISSION_LEVEL:
  3572. if (m13489val <= h->level)
  3573. return true;
  3574. return false;
  3575. case MISSION_PRIMARY_STAT:
  3576. for (int i = 0; i < 4; ++i)
  3577. {
  3578. if (h->getPrimSkillLevel(i) < m2stats[i])
  3579. return false;
  3580. }
  3581. return true;
  3582. case MISSION_KILL_HERO:
  3583. case MISSION_KILL_CREATURE:
  3584. if (!h->cb->getObjByQuestIdentifier(m13489val))
  3585. return true;
  3586. return false;
  3587. case MISSION_ART:
  3588. for (int i = 0; i < m5arts.size(); ++i)
  3589. {
  3590. if (h->hasArt(m5arts[i]))
  3591. continue;
  3592. return false; //if the artifact was not found
  3593. }
  3594. return true;
  3595. case MISSION_ARMY:
  3596. {
  3597. std::vector<CStackBasicDescriptor>::const_iterator cre;
  3598. TSlots::const_iterator it;
  3599. ui32 count;
  3600. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  3601. {
  3602. for (count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  3603. {
  3604. if (it->second->type == cre->type)
  3605. count += it->second->count;
  3606. }
  3607. if (count < cre->count) //not enough creatures of this kind
  3608. return false;
  3609. }
  3610. }
  3611. return true;
  3612. case MISSION_RESOURCES:
  3613. for (int i = 0; i < 7; ++i) //including Mithril ?
  3614. { //Quest has no direct access to callback
  3615. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  3616. return false;
  3617. }
  3618. return true;
  3619. case MISSION_HERO:
  3620. if (m13489val == h->type->ID)
  3621. return true;
  3622. return false;
  3623. case MISSION_PLAYER:
  3624. if (m13489val == h->getOwner())
  3625. return true;
  3626. return false;
  3627. default:
  3628. return false;
  3629. }
  3630. }
  3631. void CGSeerHut::initObj()
  3632. {
  3633. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  3634. textOption = ran()%3;
  3635. progress = 0;
  3636. if (missionType)
  3637. {
  3638. if (!isCustom)
  3639. {
  3640. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3641. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3642. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3643. }
  3644. if(missionType == MISSION_KILL_CREATURE)
  3645. {
  3646. stackToKill = getCreatureToKill(false)->getStack(0);
  3647. stackDirection = checkDirection();
  3648. }
  3649. else if(missionType == MISSION_KILL_HERO)
  3650. {
  3651. heroName = getHeroToKill(false)->name;
  3652. heroPortrait = getHeroToKill(false)->portrait;
  3653. }
  3654. }
  3655. else
  3656. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3657. }
  3658. const std::string & CGSeerHut::getHoverText() const
  3659. {
  3660. switch (ID)
  3661. {
  3662. case 83:
  3663. hoverName = VLC->generaltexth->allTexts[347];
  3664. boost::algorithm::replace_first(hoverName,"%s", seerName);
  3665. break;
  3666. case 215:
  3667. hoverName = VLC->generaltexth->names[ID];
  3668. break;
  3669. default:
  3670. tlog5 << "unrecognized quest object\n";
  3671. }
  3672. if (progress & missionType) //rollover when the quest is active
  3673. {
  3674. MetaString ms;
  3675. ms << "\n\n" << VLC->generaltexth->quests[missionType-1][3][textOption];
  3676. std::string str;
  3677. switch (missionType)
  3678. {
  3679. case MISSION_LEVEL:
  3680. ms.addReplacement(m13489val);
  3681. break;
  3682. case MISSION_PRIMARY_STAT:
  3683. {
  3684. MetaString loot;
  3685. for (int i = 0; i < 4; ++i)
  3686. {
  3687. if (m2stats[i])
  3688. {
  3689. loot << "%d %s";
  3690. loot.addReplacement(m2stats[i]);
  3691. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3692. }
  3693. }
  3694. ms.addReplacement(loot.buildList());
  3695. }
  3696. break;
  3697. case MISSION_KILL_HERO:
  3698. ms.addReplacement(heroName);
  3699. break;
  3700. case MISSION_KILL_CREATURE:
  3701. ms.addReplacement(stackToKill);
  3702. break;
  3703. case MISSION_ART:
  3704. {
  3705. MetaString loot;
  3706. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3707. {
  3708. loot << "%s";
  3709. loot.addReplacement(MetaString::ART_NAMES, *it);
  3710. }
  3711. ms.addReplacement(loot.buildList());
  3712. }
  3713. break;
  3714. case MISSION_ARMY:
  3715. {
  3716. MetaString loot;
  3717. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3718. {
  3719. loot << "%s";
  3720. loot.addReplacement(*it);
  3721. }
  3722. ms.addReplacement(loot.buildList());
  3723. }
  3724. break;
  3725. case MISSION_RESOURCES:
  3726. {
  3727. MetaString loot;
  3728. for (int i = 0; i < 7; ++i)
  3729. {
  3730. if (m7resources[i])
  3731. {
  3732. loot << "%d %s";
  3733. loot.addReplacement(m7resources[i]);
  3734. loot.addReplacement(MetaString::RES_NAMES, i);
  3735. }
  3736. }
  3737. ms.addReplacement(loot.buildList());
  3738. }
  3739. break;
  3740. case MISSION_HERO:
  3741. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3742. break;
  3743. case MISSION_PLAYER:
  3744. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  3745. break;
  3746. default:
  3747. break;
  3748. }
  3749. ms.toString(str);
  3750. hoverName += str;
  3751. }
  3752. return hoverName;
  3753. }
  3754. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  3755. {
  3756. switch (what)
  3757. {
  3758. case 10:
  3759. progress = val;
  3760. break;
  3761. case 11:
  3762. missionType = CQuest::MISSION_NONE;
  3763. break;
  3764. }
  3765. }
  3766. void CGSeerHut::newTurn() const
  3767. {
  3768. if (lastDay >= 0 && lastDay <= cb->getDate(0)) //time is up
  3769. {
  3770. cb->setObjProperty (id,11,0);
  3771. cb->setObjProperty (id,10,0);
  3772. }
  3773. }
  3774. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  3775. {
  3776. InfoWindow iw;
  3777. iw.player = h->getOwner();
  3778. if (missionType)
  3779. {
  3780. if (!progress) //propose quest
  3781. {
  3782. iw.text << firstVisitText;
  3783. switch (missionType)
  3784. {
  3785. case MISSION_LEVEL:
  3786. iw.components.push_back(Component (Component::EXPERIENCE, 1, m13489val, 0));
  3787. if (!isCustom)
  3788. iw.text.addReplacement(m13489val);
  3789. break;
  3790. case MISSION_PRIMARY_STAT:
  3791. {
  3792. MetaString loot;
  3793. for (int i = 0; i < 4; ++i)
  3794. {
  3795. if (m2stats[i])
  3796. {
  3797. iw.components.push_back(Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  3798. loot << "%d %s";
  3799. loot.addReplacement(m2stats[i]);
  3800. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3801. }
  3802. }
  3803. if (!isCustom)
  3804. iw.text.addReplacement(loot.buildList());
  3805. }
  3806. break;
  3807. case MISSION_KILL_HERO:
  3808. iw.components.push_back(Component(Component::HERO, heroPortrait, 0, 0));
  3809. if (!isCustom)
  3810. addReplacements(iw.text, firstVisitText);
  3811. break;
  3812. case MISSION_HERO:
  3813. iw.components.push_back(Component (Component::HERO, m13489val, 0, 0));
  3814. if (!isCustom)
  3815. iw.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3816. break;
  3817. case MISSION_KILL_CREATURE:
  3818. {
  3819. iw.components.push_back(Component(stackToKill));
  3820. if (!isCustom)
  3821. addReplacements(iw.text, firstVisitText);
  3822. }
  3823. break;
  3824. case MISSION_ART:
  3825. {
  3826. MetaString loot;
  3827. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3828. {
  3829. iw.components.push_back(Component (Component::ARTIFACT, *it, 0, 0));
  3830. loot << "%s";
  3831. loot.addReplacement(MetaString::ART_NAMES, *it);
  3832. }
  3833. if (!isCustom)
  3834. iw.text.addReplacement(loot.buildList());
  3835. }
  3836. break;
  3837. case MISSION_ARMY:
  3838. {
  3839. MetaString loot;
  3840. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3841. {
  3842. iw.components.push_back(Component(*it));
  3843. loot << "%s";
  3844. loot.addReplacement(*it);
  3845. }
  3846. if (!isCustom)
  3847. iw.text.addReplacement(loot.buildList());
  3848. }
  3849. break;
  3850. case MISSION_RESOURCES:
  3851. {
  3852. MetaString loot;
  3853. for (int i = 0; i < 7; ++i)
  3854. {
  3855. if (m7resources[i])
  3856. {
  3857. iw.components.push_back(Component (Component::RESOURCE, i, m7resources[i], 0));
  3858. loot << "%d %s";
  3859. loot.addReplacement(m7resources[i]);
  3860. loot.addReplacement(MetaString::RES_NAMES, i);
  3861. }
  3862. }
  3863. if (!isCustom)
  3864. iw.text.addReplacement(loot.buildList());
  3865. }
  3866. break;
  3867. case MISSION_PLAYER:
  3868. iw.components.push_back(Component (Component::FLAG, m13489val, 0, 0));
  3869. if (!isCustom)
  3870. iw.text.addReplacement(VLC->generaltexth->colors[m13489val]);
  3871. break;
  3872. }
  3873. cb->setObjProperty (id,10,1);
  3874. cb->showInfoDialog(&iw);
  3875. }
  3876. else if (!checkQuest(h))
  3877. {
  3878. iw.text << nextVisitText;
  3879. if(!isCustom)
  3880. addReplacements(iw.text, nextVisitText);
  3881. cb->showInfoDialog(&iw);
  3882. }
  3883. if (checkQuest(h)) // propose completion, also on first visit
  3884. {
  3885. BlockingDialog bd (true, false);
  3886. bd.player = h->getOwner();
  3887. bd.soundID = soundBase::QUEST;
  3888. bd.text << completedText;
  3889. switch (missionType)
  3890. {
  3891. case CQuest::MISSION_LEVEL:
  3892. if (!isCustom)
  3893. bd.text.addReplacement(m13489val);
  3894. break;
  3895. case CQuest::MISSION_PRIMARY_STAT:
  3896. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  3897. {
  3898. MetaString loot;
  3899. for (int i = 0; i < 4; ++i)
  3900. {
  3901. if (m2stats[i])
  3902. {
  3903. loot << "%d %s";
  3904. loot.addReplacement(m2stats[i]);
  3905. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3906. }
  3907. }
  3908. if (!isCustom)
  3909. bd.text.addReplacement(loot.buildList());
  3910. }
  3911. break;
  3912. case CQuest::MISSION_ART:
  3913. {
  3914. MetaString loot;
  3915. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3916. {
  3917. loot << "%s";
  3918. loot.addReplacement(MetaString::ART_NAMES, *it);
  3919. }
  3920. if (!isCustom)
  3921. bd.text.addReplacement(loot.buildList());
  3922. }
  3923. break;
  3924. case CQuest::MISSION_ARMY:
  3925. {
  3926. MetaString loot;
  3927. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3928. {
  3929. loot << "%s";
  3930. loot.addReplacement(*it);
  3931. }
  3932. if (!isCustom)
  3933. bd.text.addReplacement(loot.buildList());
  3934. }
  3935. break;
  3936. case CQuest::MISSION_RESOURCES:
  3937. {
  3938. MetaString loot;
  3939. for (int i = 0; i < 7; ++i)
  3940. {
  3941. if (m7resources[i])
  3942. {
  3943. loot << "%d %s";
  3944. loot.addReplacement(m7resources[i]);
  3945. loot.addReplacement(MetaString::RES_NAMES, i);
  3946. }
  3947. }
  3948. if (!isCustom)
  3949. bd.text.addReplacement(loot.buildList());
  3950. }
  3951. break;
  3952. case MISSION_KILL_HERO:
  3953. case MISSION_KILL_CREATURE:
  3954. if (!isCustom)
  3955. addReplacements(bd.text, completedText);
  3956. break;
  3957. case MISSION_HERO:
  3958. if (!isCustom)
  3959. bd.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3960. break;
  3961. case MISSION_PLAYER:
  3962. if (!isCustom)
  3963. bd.text.addReplacement(VLC->generaltexth->colors[m13489val]);
  3964. break;
  3965. }
  3966. switch (rewardType)
  3967. {
  3968. case 1: bd.components.push_back(Component (Component::EXPERIENCE, 0, rVal*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f, 0));
  3969. break;
  3970. case 2: bd.components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  3971. break;
  3972. case 3: bd.components.push_back(Component (Component::MORALE, 0, rVal, 0));
  3973. break;
  3974. case 4: bd.components.push_back(Component (Component::LUCK, 0, rVal, 0));
  3975. break;
  3976. case 5: bd.components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  3977. break;
  3978. case 6: bd.components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  3979. break;
  3980. case 7: bd.components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  3981. break;
  3982. case 8: bd.components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  3983. break;
  3984. case 9: bd.components.push_back(Component (Component::SPELL, rID, 0, 0));
  3985. break;
  3986. case 10: bd.components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  3987. break;
  3988. }
  3989. cb->showBlockingDialog (&bd, boost::bind (&CGSeerHut::finishQuest, this, h, _1));
  3990. return;
  3991. }
  3992. }
  3993. else
  3994. {
  3995. iw.text << VLC->generaltexth->seerEmpty[textOption];
  3996. if (ID == 83)
  3997. iw.text.addReplacement(seerName);
  3998. cb->showInfoDialog(&iw);
  3999. }
  4000. }
  4001. int CGSeerHut::checkDirection() const
  4002. {
  4003. int3 cord = getCreatureToKill()->pos;
  4004. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  4005. {
  4006. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  4007. return 8;
  4008. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  4009. return 1;
  4010. else //northeast
  4011. return 2;
  4012. }
  4013. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  4014. {
  4015. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  4016. return 7;
  4017. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  4018. return 9;
  4019. else //east
  4020. return 3;
  4021. }
  4022. else //south
  4023. {
  4024. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  4025. return 6;
  4026. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  4027. return 5;
  4028. else //southeast
  4029. return 4;
  4030. }
  4031. }
  4032. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  4033. {
  4034. if (accept)
  4035. {
  4036. switch (missionType)
  4037. {
  4038. case CQuest::MISSION_ART:
  4039. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  4040. {
  4041. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(*it, false)));
  4042. }
  4043. break;
  4044. case CQuest::MISSION_ARMY:
  4045. cb->takeCreatures(h->id, m6creatures);
  4046. break;
  4047. case CQuest::MISSION_RESOURCES:
  4048. for (int i = 0; i < 7; ++i)
  4049. {
  4050. cb->giveResource(h->getOwner(), i, -m7resources[i]);
  4051. }
  4052. break;
  4053. default:
  4054. break;
  4055. }
  4056. cb->setObjProperty(id,11,0); //no more mission avaliable
  4057. completeQuest(h); //make sure to remove QuestQuard at the very end
  4058. }
  4059. }
  4060. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  4061. {
  4062. switch (rewardType)
  4063. {
  4064. case 1: //experience
  4065. {
  4066. expType expVal = h->calculateXp(rVal);
  4067. cb->changePrimSkill(h->id, 4, expVal, false);
  4068. break;
  4069. }
  4070. case 2: //mana points
  4071. {
  4072. cb->setManaPoints(h->id, h->mana+rVal);
  4073. break;
  4074. }
  4075. case 3: case 4: //morale /luck
  4076. {
  4077. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  4078. Bonus::OBJECT, rVal, h->id, "", -1);
  4079. GiveBonus gb;
  4080. gb.id = h->id;
  4081. gb.bonus = hb;
  4082. cb->giveHeroBonus(&gb);
  4083. }
  4084. break;
  4085. case 5: //resources
  4086. cb->giveResource(h->getOwner(), rID, rVal);
  4087. break;
  4088. case 6: //main ability bonus (attak, defence etd.)
  4089. cb->changePrimSkill(h->id, rID, rVal, false);
  4090. break;
  4091. case 7: // secondary ability gain
  4092. cb->changeSecSkill(h->id, rID, rVal, false);
  4093. break;
  4094. case 8: // artifact
  4095. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],-2);
  4096. break;
  4097. case 9:// spell
  4098. {
  4099. std::set<ui32> spell;
  4100. spell.insert (rID);
  4101. cb->changeSpells(h->id, true, spell);
  4102. }
  4103. break;
  4104. case 10:// creature
  4105. {
  4106. CCreatureSet creatures;
  4107. creatures.setCreature(0, rID, rVal);
  4108. cb->giveCreatures(this, h, creatures, false);
  4109. }
  4110. break;
  4111. default:
  4112. break;
  4113. }
  4114. }
  4115. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  4116. {
  4117. const CGObjectInstance *o = cb->getObjByQuestIdentifier(m13489val);
  4118. if(allowNull && !o)
  4119. return NULL;
  4120. assert(o && o->ID == HEROI_TYPE);
  4121. return static_cast<const CGHeroInstance*>(o);
  4122. }
  4123. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  4124. {
  4125. const CGObjectInstance *o = cb->getObjByQuestIdentifier(m13489val);
  4126. if(allowNull && !o)
  4127. return NULL;
  4128. assert(o && o->ID == 54);
  4129. return static_cast<const CGCreature*>(o);
  4130. }
  4131. void CGSeerHut::addReplacements(MetaString &out, const std::string &base) const
  4132. {
  4133. switch(missionType)
  4134. {
  4135. case MISSION_KILL_CREATURE:
  4136. out.addReplacement(stackToKill);
  4137. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  4138. {
  4139. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  4140. }
  4141. break;
  4142. case MISSION_KILL_HERO:
  4143. out.addReplacement(heroName);
  4144. break;
  4145. }
  4146. }
  4147. void CGQuestGuard::initObj()
  4148. {
  4149. blockVisit = true;
  4150. progress = 0;
  4151. textOption = ran()%3 + 3; //3-5
  4152. if (missionType && !isCustom)
  4153. {
  4154. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  4155. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  4156. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  4157. }
  4158. else
  4159. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  4160. }
  4161. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  4162. {
  4163. cb->removeObject(id);
  4164. }
  4165. void CGWitchHut::initObj()
  4166. {
  4167. ability = allowedAbilities[ran()%allowedAbilities.size()];
  4168. }
  4169. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  4170. {
  4171. InfoWindow iw;
  4172. iw.soundID = soundBase::gazebo;
  4173. iw.player = h->getOwner();
  4174. if(!hasVisited(h->tempOwner))
  4175. cb->setObjProperty(id,10,h->tempOwner);
  4176. if(h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //you alredy know this skill
  4177. {
  4178. iw.text << std::pair<ui8,ui32>(11,172);
  4179. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4180. }
  4181. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  4182. {
  4183. iw.text << std::pair<ui8,ui32>(11,173);
  4184. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4185. }
  4186. else //give sec skill
  4187. {
  4188. iw.components.push_back(Component(1, ability, 1, 0));
  4189. iw.text << std::pair<ui8,ui32>(11,171);
  4190. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4191. cb->changeSecSkill(h->id,ability,1,true);
  4192. }
  4193. cb->showInfoDialog(&iw);
  4194. }
  4195. const std::string & CGWitchHut::getHoverText() const
  4196. {
  4197. hoverName = VLC->generaltexth->names[ID];
  4198. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4199. {
  4200. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  4201. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  4202. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4203. if(h && h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //hero knows that ability
  4204. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  4205. }
  4206. return hoverName;
  4207. }
  4208. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  4209. {
  4210. bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  4211. int messageID;
  4212. int bonusMove = 0, sound = -1;
  4213. InfoWindow iw;
  4214. iw.player = h->tempOwner;
  4215. GiveBonus gbonus;
  4216. gbonus.id = h->id;
  4217. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4218. gbonus.bonus.source = Bonus::OBJECT;
  4219. gbonus.bonus.sid = ID;
  4220. bool second = false;
  4221. Bonus secondBonus;
  4222. switch(ID)
  4223. {
  4224. case 11: //buoy
  4225. messageID = 21;
  4226. sound = soundBase::MORALE;
  4227. gbonus.bonus.type = Bonus::MORALE;
  4228. gbonus.bonus.val = +1;
  4229. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  4230. break;
  4231. case 14: //swan pond
  4232. messageID = 29;
  4233. sound = soundBase::LUCK;
  4234. gbonus.bonus.type = Bonus::LUCK;
  4235. gbonus.bonus.val = 2;
  4236. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  4237. bonusMove = -h->movement;
  4238. break;
  4239. case 28: //Faerie Ring
  4240. messageID = 49;
  4241. sound = soundBase::LUCK;
  4242. gbonus.bonus.type = Bonus::LUCK;
  4243. gbonus.bonus.val = 1;
  4244. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  4245. break;
  4246. case 30: //fountain of fortune
  4247. messageID = 55;
  4248. sound = soundBase::LUCK;
  4249. gbonus.bonus.type = Bonus::LUCK;
  4250. gbonus.bonus.val = rand()%5 - 1;
  4251. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  4252. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  4253. break;
  4254. case 38: //idol of fortune
  4255. messageID = 62;
  4256. sound = soundBase::experience;
  4257. gbonus.bonus.val = 1;
  4258. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  4259. if(cb->getDate(1) == 7) //7th day of week
  4260. {
  4261. gbonus.bonus.type = Bonus::MORALE;
  4262. second = true;
  4263. secondBonus = gbonus.bonus;
  4264. secondBonus.type = Bonus::LUCK;
  4265. }
  4266. else
  4267. {
  4268. gbonus.bonus.type = (cb->getDate(1)%2) ? Bonus::LUCK : Bonus::MORALE;
  4269. }
  4270. break;
  4271. case 52: //Mermaid
  4272. messageID = 83;
  4273. sound = soundBase::LUCK;
  4274. gbonus.bonus.type = Bonus::LUCK;
  4275. gbonus.bonus.val = 1;
  4276. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  4277. break;
  4278. case 64: //Rally Flag
  4279. sound = soundBase::MORALE;
  4280. messageID = 111;
  4281. gbonus.bonus.type = Bonus::MORALE;
  4282. gbonus.bonus.val = 1;
  4283. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  4284. second = true;
  4285. secondBonus = gbonus.bonus;
  4286. secondBonus.type = Bonus::LUCK;
  4287. bonusMove = 400;
  4288. break;
  4289. case 56: //oasis
  4290. messageID = 95;
  4291. gbonus.bonus.type = Bonus::MORALE;
  4292. gbonus.bonus.val = 1;
  4293. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  4294. bonusMove = 800;
  4295. break;
  4296. case 96: //temple
  4297. messageID = 140;
  4298. iw.soundID = soundBase::temple;
  4299. gbonus.bonus.type = Bonus::MORALE;
  4300. if(cb->getDate(1)==7) //sunday
  4301. {
  4302. gbonus.bonus.val = 2;
  4303. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  4304. }
  4305. else
  4306. {
  4307. gbonus.bonus.val = 1;
  4308. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  4309. }
  4310. break;
  4311. case 110://Watering Hole
  4312. sound = soundBase::MORALE;
  4313. messageID = 166;
  4314. gbonus.bonus.type = Bonus::MORALE;
  4315. gbonus.bonus.val = 1;
  4316. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  4317. bonusMove = 400;
  4318. break;
  4319. case 31: //Fountain of Youth
  4320. sound = soundBase::MORALE;
  4321. messageID = 57;
  4322. gbonus.bonus.type = Bonus::MORALE;
  4323. gbonus.bonus.val = 1;
  4324. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  4325. bonusMove = 400;
  4326. break;
  4327. case 94: //Stables
  4328. sound = soundBase::horse20;
  4329. CCreatureSet creatures;
  4330. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); ++i)
  4331. {
  4332. if(i->second->type->idNumber == 10)
  4333. creatures.stacks.insert(*i);
  4334. }
  4335. if (creatures.stacks.size())
  4336. {
  4337. messageID = 138;
  4338. iw.components.push_back(Component(Component::CREATURE,11,0,1));
  4339. for (TSlots::const_iterator i = creatures.stacks.begin(); i != creatures.stacks.end(); ++i)
  4340. {
  4341. cb->changeStackType(StackLocation(h, i->first), VLC->creh->creatures[11]);
  4342. }
  4343. }
  4344. else
  4345. messageID = 137;
  4346. gbonus.bonus.type = Bonus::LAND_MOVEMENT;
  4347. gbonus.bonus.val = 600;
  4348. bonusMove = 600;
  4349. gbonus.bonus.duration = Bonus::ONE_WEEK;
  4350. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  4351. break;
  4352. }
  4353. if(visited)
  4354. {
  4355. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  4356. messageID--;
  4357. else
  4358. messageID++;
  4359. }
  4360. else
  4361. {
  4362. //TODO: fix if second bonus val != main bonus val
  4363. if(gbonus.bonus.type == Bonus::MORALE || secondBonus.type == Bonus::MORALE)
  4364. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  4365. if(gbonus.bonus.type == Bonus::LUCK || secondBonus.type == Bonus::LUCK)
  4366. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  4367. cb->giveHeroBonus(&gbonus);
  4368. if(second)
  4369. {
  4370. gbonus.bonus = secondBonus;
  4371. cb->giveHeroBonus(&gbonus);
  4372. }
  4373. if(bonusMove) //swan pond - take all move points, stables - give move point this day
  4374. {
  4375. SetMovePoints smp;
  4376. smp.hid = h->id;
  4377. smp.val = h->movement + bonusMove;
  4378. cb->setMovePoints(&smp);
  4379. }
  4380. }
  4381. iw.soundID = sound;
  4382. iw.text << std::pair<ui8,ui32>(11,messageID);
  4383. cb->showInfoDialog(&iw);
  4384. }
  4385. const std::string & CGBonusingObject::getHoverText() const
  4386. {
  4387. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4388. hoverName = VLC->generaltexth->names[ID];
  4389. if(h)
  4390. {
  4391. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  4392. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4393. else
  4394. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4395. }
  4396. return hoverName;
  4397. }
  4398. void CGBonusingObject::initObj()
  4399. {
  4400. if(ID == 11 || ID == 52) //Buoy / Mermaid
  4401. {
  4402. blockVisit = true;
  4403. }
  4404. }
  4405. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  4406. {
  4407. int messageID;
  4408. InfoWindow iw;
  4409. iw.player = h->tempOwner;
  4410. iw.soundID = soundBase::GENIE;
  4411. if (!visited)
  4412. {
  4413. if (h->mana > h->manaLimit())
  4414. messageID = 76;
  4415. else
  4416. {
  4417. messageID = 74;
  4418. cb->setManaPoints (h->id, 2 * h->manaLimit());
  4419. cb->setObjProperty (id, ObjProperty::VISITED, true);
  4420. }
  4421. }
  4422. else
  4423. messageID = 75;
  4424. iw.text << std::pair<ui8,ui32>(11,messageID);
  4425. cb->showInfoDialog(&iw);
  4426. }
  4427. const std::string & CGMagicSpring::getHoverText() const
  4428. {
  4429. hoverName = VLC->generaltexth->names[ID];
  4430. if(!visited)
  4431. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4432. else
  4433. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4434. return hoverName;
  4435. }
  4436. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  4437. {
  4438. int message;
  4439. InfoWindow iw;
  4440. iw.soundID = soundBase::faerie;
  4441. iw.player = h->tempOwner;
  4442. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  4443. {
  4444. message = 78;
  4445. }
  4446. else if(h->mana < h->manaLimit())
  4447. {
  4448. giveDummyBonus(h->id);
  4449. cb->setManaPoints(h->id,h->manaLimit());
  4450. message = 77;
  4451. }
  4452. else
  4453. {
  4454. message = 79;
  4455. }
  4456. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  4457. cb->showInfoDialog(&iw);
  4458. }
  4459. const std::string & CGMagicWell::getHoverText() const
  4460. {
  4461. getNameVis(hoverName);
  4462. return hoverName;
  4463. }
  4464. void CGPandoraBox::initObj()
  4465. {
  4466. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  4467. }
  4468. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  4469. {
  4470. BlockingDialog bd (true, false);
  4471. bd.player = h->getOwner();
  4472. bd.soundID = soundBase::QUEST;
  4473. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  4474. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  4475. }
  4476. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  4477. {
  4478. if (accept)
  4479. {
  4480. if (stacksCount() > 0) //if pandora's box is protected by army
  4481. {
  4482. InfoWindow iw;
  4483. iw.player = h->tempOwner;
  4484. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4485. cb->showInfoDialog(&iw);
  4486. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  4487. }
  4488. else if (message.size() == 0 && resources.size() == 0
  4489. && primskills.size() == 0 && abilities.size() == 0
  4490. && abilityLevels.size() == 0 && artifacts.size() == 0
  4491. && spells.size() == 0 && creatures.Slots().size() > 0
  4492. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  4493. {
  4494. InfoWindow iw;
  4495. iw.player = h->tempOwner;
  4496. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  4497. cb->showInfoDialog(&iw);
  4498. }
  4499. else //if it gives something without battle
  4500. {
  4501. giveContents (h, false);
  4502. }
  4503. }
  4504. }
  4505. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  4506. {
  4507. if(result->winner)
  4508. return;
  4509. giveContents(h,true);
  4510. }
  4511. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  4512. {
  4513. InfoWindow iw;
  4514. iw.player = h->getOwner();
  4515. std::string msg = message; //in case box is removed in the meantime
  4516. bool changesPrimSkill = false;
  4517. for (int i = 0; i < primskills.size(); i++)
  4518. {
  4519. if(primskills[i])
  4520. {
  4521. changesPrimSkill = true;
  4522. break;
  4523. }
  4524. }
  4525. if(gainedExp || changesPrimSkill || abilities.size())
  4526. {
  4527. expType expVal = h->calculateXp(gainedExp);
  4528. getText(iw,afterBattle,175,h);
  4529. if(expVal)
  4530. iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
  4531. for(int i=0; i<primskills.size(); i++)
  4532. if(primskills[i])
  4533. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  4534. for(int i=0; i<abilities.size(); i++)
  4535. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  4536. cb->showInfoDialog(&iw);
  4537. //give exp
  4538. if(expVal)
  4539. cb->changePrimSkill(h->id,4,expVal,false);
  4540. //give prim skills
  4541. for(int i=0; i<primskills.size(); i++)
  4542. if(primskills[i])
  4543. cb->changePrimSkill(h->id,i,primskills[i],false);
  4544. //give sec skills
  4545. for(int i=0; i<abilities.size(); i++)
  4546. {
  4547. int curLev = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(abilities[i]));
  4548. if( (curLev && curLev < abilityLevels[i])
  4549. || (h->secSkills.size() < SKILL_PER_HERO) )
  4550. {
  4551. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  4552. }
  4553. }
  4554. }
  4555. if(spells.size())
  4556. {
  4557. std::set<ui32> spellsToGive;
  4558. iw.components.clear();
  4559. std::vector<ConstTransitivePtr<CSpell> > * sp = &VLC->spellh->spells;
  4560. for(std::vector<si32>::const_iterator i=spells.begin(); i != spells.end(); i++)
  4561. {
  4562. if ((*sp)[*i]->level <= h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2) //enough wisdom
  4563. {
  4564. iw.components.push_back(Component(Component::SPELL,*i,0,0));
  4565. spellsToGive.insert(*i);
  4566. }
  4567. }
  4568. if(spellsToGive.size())
  4569. {
  4570. cb->changeSpells(h->id,true,spellsToGive);
  4571. cb->showInfoDialog(&iw);
  4572. }
  4573. }
  4574. if(manaDiff)
  4575. {
  4576. getText(iw,afterBattle,manaDiff,176,177,h);
  4577. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  4578. cb->showInfoDialog(&iw);
  4579. cb->setManaPoints(h->id, h->mana + manaDiff);
  4580. }
  4581. if(moraleDiff)
  4582. {
  4583. getText(iw,afterBattle,moraleDiff,178,179,h);
  4584. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  4585. cb->showInfoDialog(&iw);
  4586. GiveBonus gb;
  4587. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id,"");
  4588. gb.id = h->id;
  4589. cb->giveHeroBonus(&gb);
  4590. }
  4591. if(luckDiff)
  4592. {
  4593. getText(iw,afterBattle,luckDiff,180,181,h);
  4594. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  4595. cb->showInfoDialog(&iw);
  4596. GiveBonus gb;
  4597. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id,"");
  4598. gb.id = h->id;
  4599. cb->giveHeroBonus(&gb);
  4600. }
  4601. iw.components.clear();
  4602. iw.text.clear();
  4603. for(int i=0; i<resources.size(); i++)
  4604. {
  4605. if(resources[i] < 0)
  4606. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4607. }
  4608. if(iw.components.size())
  4609. {
  4610. getText(iw,afterBattle,182,h);
  4611. cb->showInfoDialog(&iw);
  4612. }
  4613. iw.components.clear();
  4614. iw.text.clear();
  4615. for(int i=0; i<resources.size(); i++)
  4616. {
  4617. if(resources[i] > 0)
  4618. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4619. }
  4620. if(iw.components.size())
  4621. {
  4622. getText(iw,afterBattle,183,h);
  4623. cb->showInfoDialog(&iw);
  4624. }
  4625. iw.components.clear();
  4626. // getText(iw,afterBattle,183,h);
  4627. for(int i=0; i<artifacts.size(); i++)
  4628. {
  4629. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  4630. if(iw.components.size() >= 14)
  4631. {
  4632. cb->showInfoDialog(&iw);
  4633. iw.components.clear();
  4634. }
  4635. }
  4636. if(iw.components.size())
  4637. {
  4638. cb->showInfoDialog(&iw);
  4639. }
  4640. for(int i=0; i<resources.size(); i++)
  4641. if(resources[i])
  4642. cb->giveResource(h->getOwner(),i,resources[i]);
  4643. for(int i=0; i<artifacts.size(); i++)
  4644. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[artifacts[i]],-2);
  4645. iw.components.clear();
  4646. iw.text.clear();
  4647. if (creatures.Slots().size())
  4648. { //this part is taken straight from creature bank
  4649. MetaString loot;
  4650. for(TSlots::const_iterator i = creatures.Slots().begin(); i != creatures.Slots().end(); i++)
  4651. { //build list of joined creatures
  4652. iw.components.push_back(Component(*i->second));
  4653. loot << "%s";
  4654. loot.addReplacement(*i->second);
  4655. }
  4656. if (creatures.Slots().size() == 1 && creatures.Slots().begin()->second->count == 1)
  4657. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  4658. else
  4659. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  4660. iw.text.addReplacement(loot.buildList());
  4661. iw.text.addReplacement(h->name);
  4662. cb->showInfoDialog(&iw);
  4663. cb->giveCreatures(this, h, creatures, true);
  4664. }
  4665. if(!afterBattle && msg.size())
  4666. {
  4667. iw.text << msg;
  4668. cb->showInfoDialog(&iw);
  4669. }
  4670. if (!creatures.Slots().size())
  4671. cb->removeObject(id); //only when we don't need to display garrison window
  4672. }
  4673. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  4674. {
  4675. if(afterBattle || !message.size())
  4676. {
  4677. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  4678. iw.text.addReplacement(h->name);
  4679. }
  4680. else
  4681. {
  4682. iw.text << message;
  4683. afterBattle = true;
  4684. }
  4685. }
  4686. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  4687. {
  4688. iw.components.clear();
  4689. iw.text.clear();
  4690. if(afterBattle || !message.size())
  4691. {
  4692. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  4693. iw.text.addReplacement(h->name);
  4694. }
  4695. else
  4696. {
  4697. iw.text << message;
  4698. afterBattle = true;
  4699. }
  4700. }
  4701. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  4702. {
  4703. if(!(availableFor & (1 << h->tempOwner)))
  4704. return;
  4705. if(cb->getPlayerSettings(h->tempOwner)->human)
  4706. {
  4707. if(humanActivate)
  4708. activated(h);
  4709. }
  4710. else if(computerActivate)
  4711. activated(h);
  4712. }
  4713. void CGEvent::activated( const CGHeroInstance * h ) const
  4714. {
  4715. if(stacksCount() > 0)
  4716. {
  4717. InfoWindow iw;
  4718. iw.player = h->tempOwner;
  4719. if(message.size())
  4720. iw.text << message;
  4721. else
  4722. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4723. cb->showInfoDialog(&iw);
  4724. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  4725. }
  4726. else
  4727. {
  4728. giveContents(h,false);
  4729. }
  4730. }
  4731. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  4732. {
  4733. InfoWindow iw;
  4734. iw.player = h->tempOwner;
  4735. switch (ID)
  4736. {
  4737. case 58://redwood observatory
  4738. case 60://pillar of fire
  4739. {
  4740. iw.soundID = soundBase::LIGHTHOUSE;
  4741. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  4742. FoWChange fw;
  4743. fw.player = h->tempOwner;
  4744. fw.mode = 1;
  4745. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  4746. cb->sendAndApply (&fw);
  4747. break;
  4748. }
  4749. case 15://cover of darkness
  4750. {
  4751. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  4752. hideTiles(h->tempOwner, 20);
  4753. break;
  4754. }
  4755. }
  4756. cb->showInfoDialog(&iw);
  4757. }
  4758. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  4759. {
  4760. if(spell == 255)
  4761. {
  4762. tlog1 << "Not initialized shrine visited!\n";
  4763. return;
  4764. }
  4765. if(!hasVisited(h->tempOwner))
  4766. cb->setObjProperty(id,10,h->tempOwner);
  4767. InfoWindow iw;
  4768. iw.soundID = soundBase::temple;
  4769. iw.player = h->getOwner();
  4770. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  4771. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  4772. iw.text << ".";
  4773. if(!h->getArt(17)) //no spellbook
  4774. {
  4775. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  4776. }
  4777. else if(ID == 90 && !h->getSecSkillLevel(CGHeroInstance::WISDOM)) //it's third level spell and hero doesn't have wisdom
  4778. {
  4779. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  4780. }
  4781. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  4782. {
  4783. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  4784. }
  4785. else //give spell
  4786. {
  4787. std::set<ui32> spells;
  4788. spells.insert(spell);
  4789. cb->changeSpells(h->id, true, spells);
  4790. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  4791. }
  4792. cb->showInfoDialog(&iw);
  4793. }
  4794. void CGShrine::initObj()
  4795. {
  4796. if(spell == 255) //spell not set
  4797. {
  4798. int level = ID-87;
  4799. std::vector<ui16> possibilities;
  4800. cb->getAllowedSpells (possibilities, level);
  4801. if(!possibilities.size())
  4802. {
  4803. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  4804. return;
  4805. }
  4806. spell = possibilities[ran() % possibilities.size()];
  4807. }
  4808. }
  4809. const std::string & CGShrine::getHoverText() const
  4810. {
  4811. hoverName = VLC->generaltexth->names[ID];
  4812. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4813. {
  4814. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  4815. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell]->name);
  4816. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4817. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  4818. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  4819. }
  4820. return hoverName;
  4821. }
  4822. void CGSignBottle::initObj()
  4823. {
  4824. //if no text is set than we pick random from the predefined ones
  4825. if(!message.size())
  4826. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  4827. if(ID == 59)
  4828. {
  4829. blockVisit = true;
  4830. }
  4831. }
  4832. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  4833. {
  4834. InfoWindow iw;
  4835. iw.soundID = soundBase::STORE;
  4836. iw.player = h->getOwner();
  4837. iw.text << message;
  4838. cb->showInfoDialog(&iw);
  4839. if(ID == 59)
  4840. cb->removeObject(id);
  4841. }
  4842. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  4843. {
  4844. }
  4845. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  4846. {
  4847. int type = bonusType;
  4848. int bid = bonusID;
  4849. //check if the bonus if applicable, if not - give primary skill (always possible)
  4850. int ssl = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(bid)); //current sec skill level, used if bonusType == 1
  4851. if((type == 1
  4852. && ((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)
  4853. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)
  4854. || (VLC->spellh->spells[bid]->level > h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2)
  4855. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  4856. {
  4857. type = 0;
  4858. bid = ran() % PRIMARY_SKILLS;
  4859. }
  4860. InfoWindow iw;
  4861. iw.soundID = soundBase::gazebo;
  4862. iw.player = h->getOwner();
  4863. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  4864. switch (type)
  4865. {
  4866. case 0:
  4867. cb->changePrimSkill(h->id,bid,+1);
  4868. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  4869. break;
  4870. case 1:
  4871. cb->changeSecSkill(h->id,bid,+1);
  4872. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  4873. break;
  4874. case 2:
  4875. {
  4876. std::set<ui32> hlp;
  4877. hlp.insert(bid);
  4878. cb->changeSpells(h->id,true,hlp);
  4879. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  4880. }
  4881. break;
  4882. default:
  4883. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  4884. return;
  4885. }
  4886. cb->showInfoDialog(&iw);
  4887. cb->removeObject(id);
  4888. }
  4889. void CGScholar::initObj()
  4890. {
  4891. blockVisit = true;
  4892. if(bonusType == 255)
  4893. {
  4894. bonusType = ran()%3;
  4895. switch(bonusType)
  4896. {
  4897. case 0:
  4898. bonusID = ran() % PRIMARY_SKILLS;
  4899. break;
  4900. case 1:
  4901. bonusID = ran() % SKILL_QUANTITY;
  4902. break;
  4903. case 2:
  4904. std::vector<ui16> possibilities;
  4905. for (int i = 1; i < 6; ++i)
  4906. cb->getAllowedSpells (possibilities, i);
  4907. bonusID = possibilities[ran() % possibilities.size()];
  4908. break;
  4909. }
  4910. }
  4911. }
  4912. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  4913. {
  4914. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  4915. if (!ally && stacksCount() > 0) {
  4916. //TODO: Find a way to apply magic garrison effects in battle.
  4917. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  4918. return;
  4919. }
  4920. //New owner.
  4921. if (!ally)
  4922. cb->setOwner(id, h->tempOwner);
  4923. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  4924. }
  4925. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  4926. {
  4927. if (result->winner == 0)
  4928. onHeroVisit(h);
  4929. }
  4930. ui8 CGGarrison::getPassableness() const
  4931. {
  4932. if ( !stacksCount() )//empty - anyone can visit
  4933. return ALL_PLAYERS;
  4934. if ( tempOwner == 255 )//neutral guarded - noone can visit
  4935. return 0;
  4936. ui8 mask = 0;
  4937. TeamState * ts = cb->gameState()->getPlayerTeam(tempOwner);
  4938. BOOST_FOREACH(ui8 it, ts->players)
  4939. mask |= 1<<it;//allies - add to possible visitors
  4940. return mask;
  4941. }
  4942. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  4943. {
  4944. int sound = 0;
  4945. int txtid = -1;
  4946. switch(ID)
  4947. {
  4948. case 22: //Corpse
  4949. txtid = 37;
  4950. sound = soundBase::MYSTERY;
  4951. break;
  4952. case 39: //Lean To
  4953. sound = soundBase::GENIE;
  4954. txtid = 64;
  4955. break;
  4956. case 105://Wagon
  4957. sound = soundBase::GENIE;
  4958. txtid = 154;
  4959. break;
  4960. case 108:
  4961. break;
  4962. default:
  4963. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  4964. return;
  4965. }
  4966. if(ID == 108)//Warrior's Tomb
  4967. {
  4968. //ask if player wants to search the Tomb
  4969. BlockingDialog bd(true, false);
  4970. sound = soundBase::GRAVEYARD;
  4971. bd.player = h->getOwner();
  4972. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  4973. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  4974. return;
  4975. }
  4976. InfoWindow iw;
  4977. iw.soundID = sound;
  4978. iw.player = h->getOwner();
  4979. if(players.size()) //we have been already visited...
  4980. {
  4981. txtid++;
  4982. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  4983. txtid++;
  4984. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4985. }
  4986. else //first visit - give bonus!
  4987. {
  4988. switch(artOrRes)
  4989. {
  4990. case 0: // first visit but empty
  4991. if (ID == 22) //Corpse
  4992. ++txtid;
  4993. else
  4994. txtid+=2;
  4995. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4996. break;
  4997. case 1: //art
  4998. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  4999. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5000. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5001. if (ID == 22) //Corpse
  5002. {
  5003. iw.text << "%s";
  5004. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5005. }
  5006. break;
  5007. case 2: //res
  5008. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5009. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  5010. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  5011. break;
  5012. }
  5013. if(ID == 105 && artOrRes == 1)
  5014. {
  5015. iw.text.localStrings.back().second++;
  5016. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5017. }
  5018. }
  5019. cb->showInfoDialog(&iw);
  5020. cb->setObjProperty(id,10,h->getOwner());
  5021. }
  5022. const std::string & CGOnceVisitable::getHoverText() const
  5023. {
  5024. hoverName = VLC->generaltexth->names[ID] + " ";
  5025. hoverName += (hasVisited(cb->getCurrentPlayer())
  5026. ? (VLC->generaltexth->allTexts[352]) //visited
  5027. : ( VLC->generaltexth->allTexts[353])); //not visited
  5028. return hoverName;
  5029. }
  5030. void CGOnceVisitable::initObj()
  5031. {
  5032. switch(ID)
  5033. {
  5034. case 22: //Corpse
  5035. {
  5036. blockVisit = true;
  5037. int hlp = ran()%100;
  5038. if(hlp < 20)
  5039. {
  5040. artOrRes = 1;
  5041. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  5042. }
  5043. else
  5044. {
  5045. artOrRes = 0;
  5046. }
  5047. }
  5048. break;
  5049. case 39: //Lean To
  5050. {
  5051. artOrRes = 2;
  5052. bonusType = ran()%6; //any basic resource without gold
  5053. bonusVal = ran()%4 + 1;
  5054. }
  5055. break;
  5056. case 108://Warrior's Tomb
  5057. {
  5058. artOrRes = 1;
  5059. int hlp = ran()%100;
  5060. if(hlp < 30)
  5061. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  5062. else if(hlp < 80)
  5063. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  5064. else if(hlp < 95)
  5065. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  5066. else
  5067. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  5068. }
  5069. break;
  5070. case 105://Wagon
  5071. {
  5072. int hlp = ran()%100;
  5073. if(hlp < 10)
  5074. {
  5075. artOrRes = 0; // nothing... :(
  5076. }
  5077. else if(hlp < 50) //minor or treasure art
  5078. {
  5079. artOrRes = 1;
  5080. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  5081. }
  5082. else //2 - 5 of non-gold resource
  5083. {
  5084. artOrRes = 2;
  5085. bonusType = ran()%6;
  5086. bonusVal = ran()%4 + 2;
  5087. }
  5088. }
  5089. break;
  5090. }
  5091. }
  5092. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  5093. {
  5094. if(accept)
  5095. {
  5096. InfoWindow iw;
  5097. iw.player = h->getOwner();
  5098. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  5099. if(players.size()) //we've been already visited, player found nothing
  5100. {
  5101. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  5102. }
  5103. else //first visit - give artifact
  5104. {
  5105. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  5106. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  5107. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5108. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5109. }
  5110. if(!h->hasBonusFrom(Bonus::OBJECT,ID)) //we don't have modifier from this object yet
  5111. {
  5112. //ruin morale
  5113. GiveBonus gb;
  5114. gb.id = h->id;
  5115. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,-3,id,"");
  5116. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  5117. cb->giveHeroBonus(&gb);
  5118. }
  5119. cb->showInfoDialog(&iw);
  5120. cb->setObjProperty(id,10,h->getOwner());
  5121. }
  5122. }
  5123. void CBank::initObj()
  5124. {
  5125. switch (ID) //find apriopriate key
  5126. {
  5127. case 16: //bank
  5128. index = subID; break;
  5129. case 24: //derelict ship
  5130. index = 8; break;
  5131. case 25: //utopia
  5132. index = 10; break;
  5133. case 84: //crypt
  5134. index = 9; break;
  5135. case 85: //shipwreck
  5136. index = 7; break;
  5137. }
  5138. bc = NULL;
  5139. daycounter = 0;
  5140. multiplier = 1;
  5141. }
  5142. const std::string & CBank::getHoverText() const
  5143. {
  5144. hoverName = VLC->objh->creBanksNames[index];
  5145. if (bc == NULL)
  5146. hoverName += " " + VLC->generaltexth->allTexts[352];
  5147. else
  5148. hoverName += " " + VLC->generaltexth->allTexts[353];
  5149. return hoverName;
  5150. }
  5151. void CBank::reset(ui16 var1) //prevents desync
  5152. {
  5153. ui8 chance = 0;
  5154. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  5155. {
  5156. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  5157. {
  5158. bc = VLC->objh->banksInfo[index][i];
  5159. break;
  5160. }
  5161. }
  5162. artifacts.clear();
  5163. }
  5164. void CBank::initialize() const
  5165. {
  5166. cb->setObjProperty (id, 14, ran()); //synchronous reset
  5167. for (ui8 i = 0; i <= 3; i++)
  5168. {
  5169. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  5170. {
  5171. cb->setObjProperty (id, 18 + i, ran()); //synchronic artifacts
  5172. }
  5173. }
  5174. cb->setObjProperty (id, 17, ran()); //get army
  5175. }
  5176. void CBank::setPropertyDer (ui8 what, ui32 val)
  5177. /// random values are passed as arguments and processed identically on all clients
  5178. {
  5179. switch (what)
  5180. {
  5181. case 11: //daycounter
  5182. if (val == 0)
  5183. daycounter = 1; //yes, 1
  5184. else
  5185. daycounter++;
  5186. break;
  5187. case 12: //multiplier, in percent
  5188. multiplier = ((float)val)/100;
  5189. break;
  5190. case 13: //bank preset
  5191. bc = VLC->objh->banksInfo[index][val];
  5192. break;
  5193. case 14:
  5194. reset (val%100);
  5195. break;
  5196. case 15:
  5197. bc = NULL;
  5198. break;
  5199. case 16: //remove rewards from Derelict Ship
  5200. artifacts.clear();
  5201. break;
  5202. case 17: //set ArmedInstance army
  5203. {
  5204. int upgraded = 0;
  5205. if (val%100 < bc->upgradeChance) //once again anti-desync
  5206. upgraded = 1;
  5207. switch (bc->guards.size())
  5208. {
  5209. case 1:
  5210. for (int i = 0; i < 4; ++i)
  5211. setCreature (i, bc->guards[0].first, bc->guards[0].second / 5 );
  5212. setCreature (4, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  5213. break;
  5214. case 4:
  5215. {
  5216. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  5217. if (bc->guards.back().second) //all stacks are present
  5218. {
  5219. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  5220. {
  5221. setCreature (stacksCount(), it->first, it->second);
  5222. }
  5223. }
  5224. else if (bc->guards[2].second)//Wraiths are present, split two stacks in Crypt
  5225. {
  5226. setCreature (0, bc->guards[0].first, bc->guards[0].second / 2 );
  5227. setCreature (1, bc->guards[1].first, bc->guards[1].second / 2);
  5228. setCreature (2, bc->guards[2].first + upgraded, bc->guards[2].second);
  5229. setCreature (3, bc->guards[1].first, bc->guards[1].second / 2 );
  5230. setCreature (4, bc->guards[0].first, bc->guards[0].second - (bc->guards[0].second / 2) );
  5231. }
  5232. else //split both stacks
  5233. {
  5234. for (int i = 0; i < 3; ++i) //skellies
  5235. setCreature (2*i, bc->guards[0].first, bc->guards[0].second / 3);
  5236. for (int i = 0; i < 2; ++i) //zombies
  5237. setCreature (2*i+1, bc->guards[1].first, bc->guards[1].second / 2);
  5238. }
  5239. }
  5240. break;
  5241. default:
  5242. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  5243. return;
  5244. }
  5245. }
  5246. break;
  5247. case 18: //add Artifact
  5248. {
  5249. int id = cb->getArtSync(val, CArtifact::ART_TREASURE);
  5250. artifacts.push_back (id);
  5251. cb->erasePickedArt(id);
  5252. break;
  5253. }
  5254. case 19: //add Artifact
  5255. {
  5256. int id = cb->getArtSync(val, CArtifact::ART_MINOR);
  5257. artifacts.push_back (id);
  5258. cb->erasePickedArt(id);
  5259. break;
  5260. }
  5261. case 20: //add Artifact
  5262. {
  5263. int id = cb->getArtSync(val, CArtifact::ART_MAJOR);
  5264. artifacts.push_back (id);
  5265. cb->erasePickedArt(id);
  5266. break;
  5267. }
  5268. case 21: //add Artifact
  5269. {
  5270. int id = cb->getArtSync(val, CArtifact::ART_RELIC);
  5271. artifacts.push_back (id);
  5272. cb->erasePickedArt(id);
  5273. break;
  5274. }
  5275. }
  5276. }
  5277. void CBank::newTurn() const
  5278. {
  5279. if (bc == NULL)
  5280. {
  5281. if (cb->getDate(0) == 1)
  5282. initialize(); //initialize on first day
  5283. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  5284. {
  5285. initialize();
  5286. cb->setObjProperty (id, 11, 0); //daycounter 0
  5287. if (ID == 24 && cb->getDate(0) > 1)
  5288. {
  5289. cb->setObjProperty (id, 12, 0);//ugly hack to make derelict ships usable only once
  5290. cb->setObjProperty (id, 16, 0);
  5291. }
  5292. }
  5293. else
  5294. cb->setObjProperty (id, 11, 1); //daycounter++
  5295. }
  5296. }
  5297. void CBank::onHeroVisit (const CGHeroInstance * h) const
  5298. {
  5299. if (bc)
  5300. {
  5301. int banktext = 0;
  5302. switch (ID)
  5303. {
  5304. case 16: //generic bank
  5305. banktext = 32;
  5306. break;
  5307. case 24:
  5308. banktext = 41;
  5309. break;
  5310. case 25: //utopia
  5311. banktext = 47;
  5312. break;
  5313. case 84: //crypt
  5314. banktext = 119;
  5315. break;
  5316. case 85: //shipwreck
  5317. banktext = 122;
  5318. break;
  5319. }
  5320. BlockingDialog bd (true, false);
  5321. bd.player = h->getOwner();
  5322. bd.soundID = soundBase::DANGER;
  5323. bd.text << VLC->generaltexth->advobtxt[banktext];
  5324. if (ID == 16)
  5325. bd.text.addReplacement(VLC->objh->creBanksNames[index]);
  5326. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5327. }
  5328. else
  5329. {
  5330. InfoWindow iw;
  5331. iw.soundID = soundBase::GRAVEYARD;
  5332. iw.player = h->getOwner();
  5333. if (ID == 84) //morale penalty for empty Crypt
  5334. {
  5335. GiveBonus gbonus;
  5336. gbonus.id = h->id;
  5337. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5338. gbonus.bonus.source = Bonus::OBJECT;
  5339. gbonus.bonus.sid = ID;
  5340. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[98];
  5341. gbonus.bonus.type = Bonus::MORALE;
  5342. gbonus.bonus.val = -1;
  5343. cb->giveHeroBonus(&gbonus);
  5344. iw.text << VLC->generaltexth->advobtxt[120];
  5345. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5346. }
  5347. else
  5348. {
  5349. iw.text << VLC->generaltexth->advobtxt[33];
  5350. iw.text.addReplacement(VLC->objh->creBanksNames[index]);
  5351. }
  5352. cb->showInfoDialog(&iw);
  5353. }
  5354. }
  5355. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  5356. {
  5357. if (accept)
  5358. {
  5359. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  5360. }
  5361. }
  5362. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5363. {
  5364. if (result->winner == 0)
  5365. {
  5366. int textID = -1;
  5367. InfoWindow iw;
  5368. iw.player = h->getOwner();
  5369. MetaString loot;
  5370. switch (ID)
  5371. {
  5372. case 16: case 25:
  5373. textID = 34;
  5374. break;
  5375. case 24: //derelict ship
  5376. if (multiplier)
  5377. textID = 43;
  5378. else
  5379. {
  5380. GiveBonus gbonus;
  5381. gbonus.id = h->id;
  5382. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5383. gbonus.bonus.source = Bonus::OBJECT;
  5384. gbonus.bonus.sid = ID;
  5385. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  5386. gbonus.bonus.type = Bonus::MORALE;
  5387. gbonus.bonus.val = -1;
  5388. cb->giveHeroBonus(&gbonus);
  5389. textID = 42;
  5390. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5391. }
  5392. break;
  5393. case 84: //Crypt
  5394. if (bc->resources.size() != 0)
  5395. textID = 121;
  5396. else
  5397. {
  5398. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5399. GiveBonus gbonus;
  5400. gbonus.id = h->id;
  5401. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5402. gbonus.bonus.source = Bonus::OBJECT;
  5403. gbonus.bonus.sid = ID;
  5404. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  5405. gbonus.bonus.type = Bonus::MORALE;
  5406. gbonus.bonus.val = -1;
  5407. cb->giveHeroBonus(&gbonus);
  5408. textID = 120;
  5409. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5410. }
  5411. break;
  5412. case 85: //shipwreck
  5413. if (bc->resources.size())
  5414. textID = 124;
  5415. else
  5416. textID = 123;
  5417. break;
  5418. }
  5419. //grant resources
  5420. if (textID != 42) //empty derelict ship gives no cash
  5421. {
  5422. for (int it = 0; it < bc->resources.size(); it++)
  5423. {
  5424. if (bc->resources[it] != 0)
  5425. {
  5426. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  5427. loot << "%d %s";
  5428. loot.addReplacement(iw.components.back().val);
  5429. loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
  5430. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  5431. }
  5432. }
  5433. }
  5434. //grant artifacts
  5435. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  5436. {
  5437. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  5438. loot << "%s";
  5439. loot.addReplacement(MetaString::ART_NAMES, *it);
  5440. cb->giveHeroNewArtifact (h, VLC->arth->artifacts[*it], -2);
  5441. }
  5442. //display loot
  5443. if (!iw.components.empty())
  5444. {
  5445. if (textID == 34)
  5446. {
  5447. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  5448. iw.text.addReplacement(MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  5449. iw.text.addReplacement(loot.buildList());
  5450. }
  5451. else
  5452. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  5453. cb->showInfoDialog(&iw);
  5454. }
  5455. loot.clear();
  5456. iw.components.clear();
  5457. iw.text.clear();
  5458. //grant creatures
  5459. CCreatureSet ourArmy;
  5460. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  5461. {
  5462. int slot = ourArmy.getSlotFor(it->first);
  5463. ourArmy.addToSlot(slot, it->first, it->second);
  5464. }
  5465. for (TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  5466. {
  5467. iw.components.push_back(Component(*i->second));
  5468. loot << "%s";
  5469. loot.addReplacement(*i->second);
  5470. }
  5471. if (ourArmy.Slots().size())
  5472. {
  5473. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second->count == 1)
  5474. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  5475. else
  5476. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  5477. iw.text.addReplacement(loot.buildList());
  5478. iw.text.addReplacement(h->name);
  5479. cb->showInfoDialog(&iw);
  5480. cb->giveCreatures(this, h, ourArmy, false);
  5481. }
  5482. cb->setObjProperty (id, 15, 0); //bc = NULL
  5483. }
  5484. }
  5485. void CGPyramid::initObj()
  5486. {
  5487. std::vector<ui16> available;
  5488. cb->getAllowedSpells (available, 5);
  5489. if (available.size())
  5490. {
  5491. bc = VLC->objh->banksInfo[21].front(); //TODO: remove hardcoded value?
  5492. spell = (available[rand()%available.size()]);
  5493. }
  5494. else
  5495. {
  5496. tlog1 <<"No spells available for Pyramid! Object set to empty.\n";
  5497. }
  5498. setPropertyDer (17,ran()); //set guards at game start
  5499. }
  5500. const std::string & CGPyramid::getHoverText() const
  5501. {
  5502. hoverName = VLC->objh->creBanksNames[21];
  5503. if (bc == NULL)
  5504. hoverName += " " + VLC->generaltexth->allTexts[352];
  5505. else
  5506. hoverName += " " + VLC->generaltexth->allTexts[353];
  5507. return hoverName;
  5508. }
  5509. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  5510. {
  5511. if (bc)
  5512. {
  5513. BlockingDialog bd (true, false);
  5514. bd.player = h->getOwner();
  5515. bd.soundID = soundBase::MYSTERY;
  5516. bd.text << VLC->generaltexth->advobtxt[105];
  5517. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5518. }
  5519. else
  5520. {
  5521. InfoWindow iw;
  5522. iw.player = h->getOwner();
  5523. iw.text << VLC->generaltexth->advobtxt[107];
  5524. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  5525. GiveBonus gb;
  5526. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  5527. gb.id = h->id;
  5528. cb->giveHeroBonus(&gb);
  5529. cb->showInfoDialog(&iw);
  5530. }
  5531. }
  5532. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5533. {
  5534. if (result->winner == 0)
  5535. {
  5536. InfoWindow iw;
  5537. iw.player = h->getOwner();
  5538. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  5539. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  5540. if (!h->getArt(17))
  5541. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  5542. else if (h->getSecSkillLevel(CGHeroInstance::WISDOM) < 3)
  5543. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  5544. else
  5545. {
  5546. std::set<ui32> spells;
  5547. spells.insert (spell);
  5548. cb->changeSpells (h->id, true, spells);
  5549. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  5550. }
  5551. cb->showInfoDialog(&iw);
  5552. cb->setObjProperty (id, 15, 0);
  5553. }
  5554. }
  5555. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  5556. {
  5557. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  5558. playerKeyMap.find(what-101)->second.insert((ui8)val);
  5559. }
  5560. bool CGKeys::wasMyColorVisited (int player) const
  5561. {
  5562. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  5563. return true;
  5564. else
  5565. return false;
  5566. }
  5567. const std::string & CGKeymasterTent::getHoverText() const
  5568. {
  5569. hoverName = VLC->generaltexth->names[ID];
  5570. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5571. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5572. else
  5573. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5574. return hoverName;
  5575. }
  5576. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  5577. {
  5578. InfoWindow iw;
  5579. iw.soundID = soundBase::CAVEHEAD;
  5580. iw.player = h->getOwner();
  5581. if (!wasMyColorVisited (h->getOwner()) )
  5582. {
  5583. cb->setObjProperty(id, h->tempOwner+101, subID);
  5584. iw.text << std::pair<ui8,ui32>(11,19);
  5585. }
  5586. else
  5587. iw.text << std::pair<ui8,ui32>(11,20);
  5588. cb->showInfoDialog(&iw);
  5589. }
  5590. void CGBorderGuard::initObj()
  5591. {
  5592. blockVisit = true;
  5593. }
  5594. const std::string & CGBorderGuard::getHoverText() const
  5595. {
  5596. hoverName = VLC->generaltexth->names[ID];
  5597. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5598. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5599. else
  5600. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5601. return hoverName;
  5602. }
  5603. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  5604. {
  5605. if (wasMyColorVisited (h->getOwner()) )
  5606. {
  5607. BlockingDialog bd (true, false);
  5608. bd.player = h->getOwner();
  5609. bd.soundID = soundBase::QUEST;
  5610. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  5611. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  5612. }
  5613. else
  5614. {
  5615. InfoWindow iw;
  5616. iw.player = h->getOwner();
  5617. iw.soundID = soundBase::CAVEHEAD;
  5618. iw.text << std::pair<ui8,ui32>(11,18);
  5619. cb->showInfoDialog (&iw);
  5620. }
  5621. }
  5622. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  5623. {
  5624. if (accept)
  5625. cb->removeObject(id);
  5626. }
  5627. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  5628. {
  5629. if (!wasMyColorVisited (h->getOwner()) )
  5630. {
  5631. InfoWindow iw;
  5632. iw.player = h->getOwner();
  5633. iw.text << std::pair<ui8,ui32>(11,18);
  5634. cb->showInfoDialog(&iw);
  5635. }
  5636. }
  5637. ui8 CGBorderGate::getPassableness() const
  5638. {
  5639. ui8 ret = 0;
  5640. for (int i = 0; i < PLAYER_LIMIT; i++)
  5641. ret |= wasMyColorVisited(i)<<i;
  5642. return ret;
  5643. }
  5644. void CGMagi::initObj()
  5645. {
  5646. if (ID == 27)
  5647. {
  5648. blockVisit = true;
  5649. eyelist[subID].push_back(id);
  5650. }
  5651. }
  5652. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  5653. {
  5654. if (ID == 37)
  5655. {
  5656. InfoWindow iw;
  5657. CenterView cv;
  5658. FoWChange fw;
  5659. cv.player = iw.player = fw.player = h->tempOwner;
  5660. iw.soundID = soundBase::LIGHTHOUSE;
  5661. iw.player = h->tempOwner;
  5662. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  5663. cb->showInfoDialog(&iw);
  5664. fw.mode = 1;
  5665. std::vector<si32>::iterator it;
  5666. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  5667. {
  5668. const CGObjectInstance *eye = cb->getObj(*it);
  5669. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  5670. cb->sendAndApply(&fw);
  5671. cv.pos = eye->pos;
  5672. cv.focusTime = 2000;
  5673. cb->sendAndApply(&cv);
  5674. }
  5675. cv.pos = h->getPosition(false);
  5676. cb->sendAndApply(&cv);
  5677. }
  5678. else if (ID == 27)
  5679. {
  5680. InfoWindow iw;
  5681. iw.player = h->tempOwner;
  5682. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  5683. cb->showInfoDialog(&iw);
  5684. }
  5685. }
  5686. void CGBoat::initObj()
  5687. {
  5688. hero = NULL;
  5689. }
  5690. void CGSirens::initObj()
  5691. {
  5692. blockVisit = true;
  5693. }
  5694. const std::string & CGSirens::getHoverText() const
  5695. {
  5696. getNameVis(hoverName);
  5697. return hoverName;
  5698. }
  5699. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  5700. {
  5701. InfoWindow iw;
  5702. iw.soundID = soundBase::DANGER;
  5703. iw.player = h->tempOwner;
  5704. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  5705. {
  5706. iw.text.addTxt(11,133);
  5707. }
  5708. else
  5709. {
  5710. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  5711. int xp = 0;
  5712. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  5713. {
  5714. TQuantity drown = i->second->count * 0.3;
  5715. if(drown)
  5716. {
  5717. cb->changeStackCount(StackLocation(h, i->first), -drown);
  5718. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  5719. }
  5720. }
  5721. if(xp)
  5722. {
  5723. xp = h->calculateXp(xp);
  5724. iw.text.addTxt(11,132);
  5725. iw.text.addReplacement(xp);
  5726. cb->changePrimSkill(h->ID, 4, xp, false);
  5727. }
  5728. else
  5729. {
  5730. iw.text.addTxt(11,134);
  5731. }
  5732. }
  5733. cb->showInfoDialog(&iw);
  5734. }
  5735. //bool IShipyard::validLocation() const
  5736. //{
  5737. // std::vector<int3> offsets;
  5738. // getOutOffsets(offsets);
  5739. //
  5740. // TerrainTile *tile;
  5741. // for(int i = 0; i < offsets.size(); i++)
  5742. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5743. // return true;
  5744. // return false;
  5745. //}
  5746. int3 IBoatGenerator::bestLocation() const
  5747. {
  5748. std::vector<int3> offsets;
  5749. getOutOffsets(offsets);
  5750. for (int i = 0; i < offsets.size(); ++i)
  5751. {
  5752. TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offsets[i]);
  5753. if (tile) //tile is in the map
  5754. {
  5755. if (tile->tertype == TerrainTile::water && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  5756. return o->pos + offsets[i];
  5757. }
  5758. }
  5759. return int3 (-1,-1,-1);
  5760. }
  5761. int IBoatGenerator::state() const
  5762. {
  5763. int3 tile = bestLocation();
  5764. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  5765. if(!t)
  5766. return 2; //no available water
  5767. else if(!t->blockingObjects.size())
  5768. return 0; //OK
  5769. else if(t->blockingObjects.front()->ID == 8)
  5770. return 1; //blocked with boat
  5771. else
  5772. return 2; //blocked
  5773. }
  5774. int IBoatGenerator::getBoatType() const
  5775. {
  5776. //We make good ships by default
  5777. return 1;
  5778. }
  5779. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  5780. : o(O)
  5781. {
  5782. }
  5783. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  5784. {
  5785. switch(state())
  5786. {
  5787. case 1:
  5788. out.addTxt(MetaString::GENERAL_TXT, 51);
  5789. break;
  5790. case 2:
  5791. if(visitor)
  5792. {
  5793. out.addTxt(MetaString::GENERAL_TXT, 134);
  5794. out.addReplacement(visitor->name);
  5795. }
  5796. else
  5797. out.addTxt(MetaString::ADVOB_TXT, 189);
  5798. break;
  5799. case 3:
  5800. tlog1 << "Shipyard without water!!! " << o->pos << "\t" << o->id << std::endl;
  5801. return;
  5802. }
  5803. }
  5804. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  5805. {
  5806. cost.resize(RESOURCE_QUANTITY);
  5807. cost[0] = 10;
  5808. cost[6] = 1000;
  5809. }
  5810. IShipyard::IShipyard(const CGObjectInstance *O)
  5811. : IBoatGenerator(O)
  5812. {
  5813. }
  5814. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  5815. {
  5816. if(!obj)
  5817. return NULL;
  5818. if(obj->ID == TOWNI_TYPE)
  5819. {
  5820. return static_cast<CGTownInstance*>(obj);
  5821. }
  5822. else if(obj->ID == 87)
  5823. {
  5824. return static_cast<CGShipyard*>(obj);
  5825. }
  5826. else
  5827. {
  5828. //tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  5829. return NULL;
  5830. }
  5831. }
  5832. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  5833. {
  5834. return castFrom(const_cast<CGObjectInstance*>(obj));
  5835. }
  5836. CGShipyard::CGShipyard()
  5837. :IShipyard(this)
  5838. {
  5839. }
  5840. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  5841. {
  5842. // H J L K I
  5843. // A x S x B
  5844. // C E G F D
  5845. offsets += int3(-3,0,0), int3(1,0,0), //AB
  5846. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  5847. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0); //HIJKL
  5848. }
  5849. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  5850. {
  5851. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  5852. cb->setOwner(id, h->tempOwner);
  5853. int s = state();
  5854. if(s)
  5855. {
  5856. InfoWindow iw;
  5857. iw.player = tempOwner;
  5858. getProblemText(iw.text, h);
  5859. cb->showInfoDialog(&iw);
  5860. }
  5861. else
  5862. {
  5863. OpenWindow ow;
  5864. ow.id1 = id;
  5865. ow.id2 = h->id;
  5866. ow.window = OpenWindow::SHIPYARD_WINDOW;
  5867. cb->sendAndApply(&ow);
  5868. }
  5869. }
  5870. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  5871. {
  5872. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  5873. {
  5874. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  5875. {
  5876. //ask if he wants to buy one
  5877. int text;
  5878. switch (subID)
  5879. {
  5880. case 0:
  5881. text = 25;
  5882. break;
  5883. case 1:
  5884. text = 26;
  5885. break;
  5886. case 2:
  5887. text = 27;
  5888. break;
  5889. default:
  5890. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  5891. }
  5892. BlockingDialog bd (true, false);
  5893. bd.player = h->getOwner();
  5894. bd.soundID = soundBase::LIGHTHOUSE;
  5895. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  5896. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  5897. }
  5898. else //if he cannot afford
  5899. {
  5900. InfoWindow iw;
  5901. iw.player = h->getOwner();
  5902. iw.soundID = soundBase::CAVEHEAD;
  5903. iw.text << std::pair<ui8,ui32>(11,28);
  5904. cb->showInfoDialog (&iw);
  5905. }
  5906. }
  5907. else //if he already visited carographer
  5908. {
  5909. InfoWindow iw;
  5910. iw.player = h->getOwner();
  5911. iw.soundID = soundBase::CAVEHEAD;
  5912. iw.text << std::pair<ui8,ui32>(11,24);
  5913. cb->showInfoDialog (&iw);
  5914. }
  5915. }
  5916. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  5917. {
  5918. if (accept) //if hero wants to buy map
  5919. {
  5920. cb->giveResource (h->tempOwner, 6, -1000);
  5921. FoWChange fw;
  5922. fw.mode = 1;
  5923. fw.player = h->tempOwner;
  5924. //subIDs of different types of cartographers:
  5925. //water = 0; land = 1; underground = 2;
  5926. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  5927. cb->sendAndApply (&fw);
  5928. cb->setObjProperty (id, 10, h->tempOwner);
  5929. }
  5930. }
  5931. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  5932. {
  5933. cb->showThievesGuildWindow(id);
  5934. }
  5935. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  5936. {
  5937. InfoWindow iw;
  5938. iw.player = h->tempOwner;
  5939. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  5940. assert(ts);
  5941. int team = ts->id;
  5942. if(!hasVisited(team))
  5943. {
  5944. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  5945. cb->sendAndApply(&iw);
  5946. cb->setObjProperty(id,20,team); //increment general visited obelisks counter
  5947. OpenWindow ow;
  5948. ow.id1 = h->tempOwner;
  5949. ow.window = OpenWindow::PUZZLE_MAP;
  5950. cb->sendAndApply(&ow);
  5951. cb->setObjProperty(id,10,team); //mark that particular obelisk as visited
  5952. }
  5953. else
  5954. {
  5955. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  5956. cb->sendAndApply(&iw);
  5957. }
  5958. }
  5959. void CGObelisk::initObj()
  5960. {
  5961. obeliskCount++;
  5962. }
  5963. const std::string & CGObelisk::getHoverText() const
  5964. {
  5965. hoverName = VLC->generaltexth->names[ID];
  5966. if(hasVisited(cb->getCurrentPlayer()))
  5967. hoverName += " " + VLC->generaltexth->allTexts[352]; //not visited
  5968. else
  5969. hoverName += " " + VLC->generaltexth->allTexts[353]; //visited
  5970. return hoverName;
  5971. }
  5972. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  5973. {
  5974. CPlayersVisited::setPropertyDer(what, val);
  5975. switch(what)
  5976. {
  5977. case 20:
  5978. assert(val < PLAYER_LIMIT);
  5979. visited[val]++;
  5980. if(visited[val] > obeliskCount)
  5981. {
  5982. tlog0 << "Error: Visited " << visited[val] << "\t\t" << obeliskCount << std::endl;
  5983. assert(0);
  5984. }
  5985. break;
  5986. }
  5987. }
  5988. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  5989. {
  5990. if(h->tempOwner != tempOwner)
  5991. {
  5992. ui8 oldOwner = tempOwner;
  5993. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  5994. InfoWindow iw;
  5995. iw.player = h->tempOwner;
  5996. iw.text.addTxt(MetaString::ADVOB_TXT, 69);
  5997. iw.soundID = soundBase::LIGHTHOUSE;
  5998. cb->sendAndApply(&iw);
  5999. giveBonusTo(h->tempOwner);
  6000. if(oldOwner < PLAYER_LIMIT) //remove bonus from old owner
  6001. {
  6002. RemoveBonus rb(RemoveBonus::PLAYER);
  6003. rb.whoID = oldOwner;
  6004. rb.source = Bonus::OBJECT;
  6005. rb.id = id;
  6006. cb->sendAndApply(&rb);
  6007. }
  6008. }
  6009. }
  6010. void CGLighthouse::initObj()
  6011. {
  6012. if(tempOwner < PLAYER_LIMIT)
  6013. {
  6014. giveBonusTo(tempOwner);
  6015. }
  6016. }
  6017. const std::string & CGLighthouse::getHoverText() const
  6018. {
  6019. hoverName = VLC->generaltexth->names[ID];
  6020. //TODO: owned by %s player
  6021. return hoverName;
  6022. }
  6023. void CGLighthouse::giveBonusTo( ui8 player ) const
  6024. {
  6025. GiveBonus gb(GiveBonus::PLAYER);
  6026. gb.bonus.type = Bonus::SEA_MOVEMENT;
  6027. gb.bonus.val = 500;
  6028. gb.id = player;
  6029. gb.bonus.duration = Bonus::PERMANENT;
  6030. gb.bonus.source = Bonus::OBJECT;
  6031. gb.bonus.sid = id;
  6032. cb->sendAndApply(&gb);
  6033. }
  6034. void CArmedInstance::randomizeArmy(int type)
  6035. {
  6036. int max = VLC->creh->creatures.size();
  6037. for (TSlots::iterator j = stacks.begin(); j != stacks.end();j++)
  6038. {
  6039. int randID = j->second->idRand;
  6040. if(randID > max)
  6041. {
  6042. if(randID % 2)
  6043. j->second->setType(VLC->townh->towns[type].basicCreatures[(randID-197) / 2 -1]);
  6044. else
  6045. j->second->setType(VLC->townh->towns[type].upgradedCreatures[(randID-197) / 2 -1]);
  6046. randID = -1;
  6047. }
  6048. assert(j->second->armyObj == this);
  6049. }
  6050. return;
  6051. }
  6052. CArmedInstance::CArmedInstance()
  6053. {
  6054. battle = NULL;
  6055. }
  6056. int CArmedInstance::valOfGlobalBonuses(CSelector selector) const
  6057. {
  6058. //if (tempOwner != NEUTRAL_PLAYER)
  6059. return cb->gameState()->players[tempOwner].valOfBonuses(selector);
  6060. }
  6061. void CArmedInstance::updateMoraleBonusFromArmy()
  6062. {
  6063. if(!validTypes(false)) //object not randomized, don't bother
  6064. return;
  6065. Bonus *b = bonuses.getFirst(Selector::sourceType(Bonus::ARMY) && Selector::type(Bonus::MORALE));
  6066. if(!b)
  6067. {
  6068. b = new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  6069. addNewBonus(b);
  6070. }
  6071. //number of alignments and presence of undead
  6072. bool canMix = hasBonusOfType(Bonus::NONEVIL_ALIGNMENT_MIX);
  6073. std::set<si8> factions;
  6074. for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  6075. {
  6076. // Take Angelic Alliance troop-mixing freedom of non-evil, non-Conflux units into account.
  6077. const si8 faction = i->second->type->faction;
  6078. if (canMix
  6079. && ((faction >= 0 && faction <= 2) || faction == 6 || faction == 7))
  6080. {
  6081. factions.insert(0); // Insert a single faction of the affected group, Castle will do.
  6082. }
  6083. else
  6084. {
  6085. factions.insert(faction);
  6086. }
  6087. }
  6088. if(factions.size() == 1)
  6089. {
  6090. b->val = +1;
  6091. b->description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
  6092. }
  6093. else
  6094. {
  6095. b->val = 2-factions.size();
  6096. b->description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factions.size() % b->val); //Troops of %d alignments %d
  6097. }
  6098. boost::algorithm::trim(b->description);
  6099. //-1 modifier for any Necropolis unit in army
  6100. const ui8 UNDEAD_MODIFIER_ID = -2;
  6101. Bonus *undeadModifier = bonuses.getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
  6102. if(vstd::contains(factions,4))
  6103. {
  6104. if(!undeadModifier)
  6105. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]));
  6106. }
  6107. else if(undeadModifier)
  6108. removeBonus(undeadModifier);
  6109. }
  6110. void CArmedInstance::armyChanged()
  6111. {
  6112. updateMoraleBonusFromArmy();
  6113. }
  6114. CBonusSystemNode * CArmedInstance::whereShouldBeAttached(CGameState *gs)
  6115. {
  6116. if(tempOwner < PLAYER_LIMIT)
  6117. return gs->getPlayer(tempOwner);
  6118. else
  6119. return &gs->globalEffects;
  6120. }
  6121. CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
  6122. {
  6123. return this;
  6124. }
  6125. bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const
  6126. {
  6127. switch(mode)
  6128. {
  6129. case RESOURCE_RESOURCE:
  6130. {
  6131. float effectiveness = std::min(((float)getMarketEfficiency()+1.0f) / 20.0f, 0.5f);
  6132. float r = VLC->objh->resVals[id1], //value of given resource
  6133. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  6134. if(r>g) //if given resource is more expensive than wanted
  6135. {
  6136. val2 = ceil(r / g);
  6137. val1 = 1;
  6138. }
  6139. else //if wanted resource is more expensive
  6140. {
  6141. val1 = (g / r) + 0.5f;
  6142. val2 = 1;
  6143. }
  6144. }
  6145. break;
  6146. case CREATURE_RESOURCE:
  6147. {
  6148. const float effectivenessArray[] = {0, 0.3, 0.45, 0.50, 0.65, 0.7, 0.85, 0.9, 1};
  6149. float effectiveness = effectivenessArray[std::min(getMarketEfficiency(), 8)];
  6150. float r = VLC->creh->creatures[id1]->cost[6], //value of given creature in gold
  6151. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  6152. if(r>g) //if given resource is more expensive than wanted
  6153. {
  6154. val2 = ceil(r / g);
  6155. val1 = 1;
  6156. }
  6157. else //if wanted resource is more expensive
  6158. {
  6159. val1 = (g / r) + 0.5f;
  6160. val2 = 1;
  6161. }
  6162. }
  6163. break;
  6164. case RESOURCE_PLAYER:
  6165. val1 = 1;
  6166. val2 = 1;
  6167. break;
  6168. case RESOURCE_ARTIFACT:
  6169. {
  6170. float effectiveness = std::min(((float)getMarketEfficiency()+3.0f) / 20.0f, 0.6f);
  6171. float r = VLC->objh->resVals[id1], //value of offered resource
  6172. g = VLC->arth->artifacts[id2]->price / effectiveness; //value of bought artifact in gold
  6173. if(id1 != 6) //non-gold prices are doubled
  6174. r /= 2;
  6175. assert(g >= r); //should we allow artifacts cheaper than unit of resource?
  6176. val1 = (g / r) + 0.5f;
  6177. val2 = 1;
  6178. }
  6179. break;
  6180. case CREATURE_EXP:
  6181. {
  6182. val1 = 1;
  6183. val2 = (VLC->creh->creatures[id1]->AIValue / 40) * 5;
  6184. }
  6185. break;
  6186. case ARTIFACT_EXP:
  6187. {
  6188. val1 = 1;
  6189. int givenClass = VLC->arth->artifacts[id1]->getArtClassSerial();
  6190. if(givenClass < 0 || givenClass > 3)
  6191. {
  6192. val2 = 0;
  6193. return false;
  6194. }
  6195. static const int expPerClass[] = {1000, 1500, 3000, 6000};
  6196. val2 = expPerClass[givenClass];
  6197. }
  6198. break;
  6199. default:
  6200. assert(0);
  6201. return false;
  6202. }
  6203. return true;
  6204. }
  6205. bool IMarket::allowsTrade(EMarketMode mode) const
  6206. {
  6207. return false;
  6208. }
  6209. int IMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  6210. {
  6211. switch(mode)
  6212. {
  6213. case RESOURCE_RESOURCE:
  6214. case ARTIFACT_RESOURCE:
  6215. case CREATURE_RESOURCE:
  6216. return -1;
  6217. default:
  6218. return 1;
  6219. }
  6220. }
  6221. std::vector<int> IMarket::availableItemsIds(EMarketMode mode) const
  6222. {
  6223. std::vector<int> ret;
  6224. switch(mode)
  6225. {
  6226. case RESOURCE_RESOURCE:
  6227. case ARTIFACT_RESOURCE:
  6228. case CREATURE_RESOURCE:
  6229. for (int i = 0; i < 7; i++)
  6230. ret.push_back(i);
  6231. default:
  6232. break;
  6233. }
  6234. return ret;
  6235. }
  6236. const IMarket * IMarket::castFrom(const CGObjectInstance *obj)
  6237. {
  6238. switch(obj->ID)
  6239. {
  6240. case TOWNI_TYPE:
  6241. return static_cast<const CGTownInstance*>(obj);
  6242. case 2: //Altar of Sacrifice
  6243. case 7: //Black Market
  6244. case 99: //Trading Post
  6245. case 221: //Trading Post (snow)
  6246. case 213: //Freelancer's Guild
  6247. return static_cast<const CGMarket*>(obj);
  6248. case 104: //University
  6249. return static_cast<const CGUniversity*>(obj);
  6250. default:
  6251. tlog1 << "Cannot cast to IMarket object with ID " << obj->ID << std::endl;
  6252. return NULL;
  6253. }
  6254. }
  6255. IMarket::IMarket(const CGObjectInstance *O)
  6256. :o(O)
  6257. {
  6258. }
  6259. std::vector<EMarketMode> IMarket::availableModes() const
  6260. {
  6261. std::vector<EMarketMode> ret;
  6262. for (int i = 0; i < MARTKET_AFTER_LAST_PLACEHOLDER; i++)
  6263. if(allowsTrade((EMarketMode)i))
  6264. ret.push_back((EMarketMode)i);
  6265. return ret;
  6266. }
  6267. void CGMarket::onHeroVisit(const CGHeroInstance * h) const
  6268. {
  6269. OpenWindow ow;
  6270. ow.id1 = id;
  6271. ow.id2 = h->id;
  6272. ow.window = OpenWindow::MARKET_WINDOW;
  6273. cb->sendAndApply(&ow);
  6274. }
  6275. int CGMarket::getMarketEfficiency() const
  6276. {
  6277. return 5;
  6278. }
  6279. bool CGMarket::allowsTrade(EMarketMode mode) const
  6280. {
  6281. switch(mode)
  6282. {
  6283. case RESOURCE_RESOURCE:
  6284. case RESOURCE_PLAYER:
  6285. switch(ID)
  6286. {
  6287. case 99: //Trading Post
  6288. case 221: //Trading Post (snow)
  6289. return true;
  6290. default:
  6291. return false;
  6292. }
  6293. case CREATURE_RESOURCE:
  6294. return ID == 213; //Freelancer's Guild
  6295. //case ARTIFACT_RESOURCE:
  6296. case RESOURCE_ARTIFACT:
  6297. return ID == 7; //Black Market
  6298. case ARTIFACT_EXP:
  6299. case CREATURE_EXP:
  6300. return ID == 2; //TODO? check here for alignment of visiting hero? - would not be coherent with other checks here
  6301. case RESOURCE_SKILL:
  6302. return ID == 104;//University
  6303. default:
  6304. return false;
  6305. }
  6306. }
  6307. int CGMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  6308. {
  6309. return -1;
  6310. }
  6311. std::vector<int> CGMarket::availableItemsIds(EMarketMode mode) const
  6312. {
  6313. switch(mode)
  6314. {
  6315. case RESOURCE_RESOURCE:
  6316. case RESOURCE_PLAYER:
  6317. return IMarket::availableItemsIds(mode);
  6318. default:
  6319. return std::vector<int>();
  6320. }
  6321. }
  6322. CGMarket::CGMarket()
  6323. :IMarket(this)
  6324. {
  6325. }
  6326. std::vector<int> CGBlackMarket::availableItemsIds(EMarketMode mode) const
  6327. {
  6328. switch(mode)
  6329. {
  6330. case ARTIFACT_RESOURCE:
  6331. return IMarket::availableItemsIds(mode);
  6332. case RESOURCE_ARTIFACT:
  6333. {
  6334. std::vector<int> ret;
  6335. BOOST_FOREACH(const CArtifact *a, artifacts)
  6336. if(a)
  6337. ret.push_back(a->id);
  6338. else
  6339. ret.push_back(-1);
  6340. return ret;
  6341. }
  6342. default:
  6343. return std::vector<int>();
  6344. }
  6345. }
  6346. void CGBlackMarket::newTurn() const
  6347. {
  6348. if(cb->getDate(2) != 1)
  6349. return;
  6350. SetAvailableArtifacts saa;
  6351. saa.id = id;
  6352. cb->pickAllowedArtsSet(saa.arts);
  6353. cb->sendAndApply(&saa);
  6354. }
  6355. void CGUniversity::initObj()
  6356. {
  6357. std::vector <int> toChoose;
  6358. for (int i=0; i<SKILL_QUANTITY; i++)
  6359. if (cb->isAllowed(2,i))
  6360. toChoose.push_back(i);
  6361. if (toChoose.size() < 4)
  6362. {
  6363. tlog0<<"Warning: less then 4 available skills was found by University initializer!\n";
  6364. return;
  6365. }
  6366. for (int i=0; i<4; i++)//get 4 skills
  6367. {
  6368. int skillPos = ran()%toChoose.size();
  6369. skills.push_back(toChoose[skillPos]);//move it to selected
  6370. toChoose.erase(toChoose.begin()+skillPos);//remove from list
  6371. }
  6372. }
  6373. std::vector<int> CGUniversity::availableItemsIds(EMarketMode mode) const
  6374. {
  6375. switch (mode)
  6376. {
  6377. case RESOURCE_SKILL:
  6378. return skills;
  6379. default:
  6380. return std::vector <int> ();
  6381. }
  6382. }
  6383. void CGUniversity::onHeroVisit(const CGHeroInstance * h) const
  6384. {
  6385. OpenWindow ow;
  6386. ow.id1 = id;
  6387. ow.id2 = h->id;
  6388. ow.window = OpenWindow::UNIVERSITY_WINDOW;
  6389. cb->sendAndApply(&ow);
  6390. }
  6391. const CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/) const
  6392. {
  6393. if(const ArtSlotInfo *si = getSlot(pos))
  6394. {
  6395. if(si->artifact && (!excludeLocked || !si->locked))
  6396. return si->artifact;
  6397. }
  6398. return NULL;
  6399. }
  6400. CArtifactInstance* CArtifactSet::getArt(ui16 pos, bool excludeLocked /*= true*/)
  6401. {
  6402. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  6403. }
  6404. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  6405. {
  6406. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  6407. if(i->second.artifact->artType->id == aid)
  6408. return i->first;
  6409. if(onlyWorn)
  6410. return -1;
  6411. for(int i = 0; i < artifactsInBackpack.size(); i++)
  6412. if(artifactsInBackpack[i].artifact->artType->id == aid)
  6413. return Arts::BACKPACK_START + i;
  6414. return -1;
  6415. }
  6416. si32 CArtifactSet::getArtPos(const CArtifactInstance *art) const
  6417. {
  6418. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  6419. if(i->second.artifact == art)
  6420. return i->first;
  6421. for(int i = 0; i < artifactsInBackpack.size(); i++)
  6422. if(artifactsInBackpack[i].artifact == art)
  6423. return Arts::BACKPACK_START + i;
  6424. return -1;
  6425. }
  6426. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  6427. {
  6428. return getArtPos(aid, onlyWorn) != -1;
  6429. }
  6430. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  6431. {
  6432. if(vstd::contains(artifactsWorn, pos))
  6433. return &artifactsWorn[pos];
  6434. if(pos >= Arts::AFTER_LAST )
  6435. {
  6436. int backpackPos = (int)pos - Arts::BACKPACK_START;
  6437. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  6438. return NULL;
  6439. else
  6440. return &artifactsInBackpack[backpackPos];
  6441. }
  6442. return NULL;
  6443. }
  6444. bool CArtifactSet::isPositionFree(ui16 pos, bool onlyLockCheck /*= false*/) const
  6445. {
  6446. if(const ArtSlotInfo *s = getSlot(pos))
  6447. return (onlyLockCheck || !s->artifact) && !s->locked;
  6448. return true; //no slot means not used
  6449. }
  6450. si32 CArtifactSet::getArtTypeId(ui16 pos) const
  6451. {
  6452. const CArtifactInstance * const a = getArt(pos);
  6453. if(!a)
  6454. {
  6455. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  6456. return -1;
  6457. }
  6458. return a->artType->id;
  6459. }
  6460. CArtifactSet::~CArtifactSet()
  6461. {
  6462. }
  6463. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ui16 slot)
  6464. {
  6465. assert(!vstd::contains(artifactsWorn, slot));
  6466. ArtSlotInfo &ret = slot < Arts::BACKPACK_START
  6467. ? artifactsWorn[slot]
  6468. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - Arts::BACKPACK_START), ArtSlotInfo());
  6469. return ret;
  6470. }
  6471. void CArtifactSet::setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked)
  6472. {
  6473. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  6474. asi.artifact = art;
  6475. asi.locked = locked;
  6476. }
  6477. void CArtifactSet::eraseArtSlot(ui16 slot)
  6478. {
  6479. if(slot < Arts::BACKPACK_START)
  6480. {
  6481. artifactsWorn.erase(slot);
  6482. }
  6483. else
  6484. {
  6485. slot -= Arts::BACKPACK_START;
  6486. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  6487. }
  6488. }