CObjectHandler.cpp 181 KB

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