CObjectHandler.cpp 184 KB

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