CObjectHandler.cpp 171 KB

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