CObjectHandler.cpp 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CDefObjInfoHandler.h"
  8. #include "CHeroHandler.h"
  9. #include "CSpellHandler.h"
  10. #include "../client/CSoundBase.h"
  11. #include <boost/bind.hpp>
  12. #include <boost/algorithm/string/replace.hpp>
  13. #include <boost/assign/std/vector.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/random/linear_congruential.hpp>
  16. #include "CTownHandler.h"
  17. #include "CArtHandler.h"
  18. #include "CCreatureHandler.h"
  19. #include "VCMI_Lib.h"
  20. #include "IGameCallback.h"
  21. #include "CGameState.h"
  22. #include "NetPacks.h"
  23. #include "../StartInfo.h"
  24. #include "map.h"
  25. #include <sstream>
  26. #include <SDL_stdinc.h>
  27. #include <boost/foreach.hpp>
  28. #include <boost/format.hpp>
  29. 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(CGHeroInstance::PATHFINDING)), 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(SecondarySkill skill) 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(SecondarySkill 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. const 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. nodeType = HERO;
  709. ID = HEROI_TYPE;
  710. tacticFormationEnabled = inTownGarrison = false;
  711. mana = movement = portrait = level = -1;
  712. isStanding = true;
  713. moveDir = 4;
  714. exp = 0xffffffff;
  715. visitedTown = NULL;
  716. type = NULL;
  717. boat = NULL;
  718. secSkills.push_back(std::make_pair(-1, -1));
  719. speciality.nodeType = CBonusSystemNode::SPECIALITY;
  720. }
  721. void CGHeroInstance::initHero(int SUBID)
  722. {
  723. subID = SUBID;
  724. initHero();
  725. }
  726. void CGHeroInstance::initHero()
  727. {
  728. assert(validTypes(true));
  729. if(ID == HEROI_TYPE)
  730. initHeroDefInfo();
  731. if(!type)
  732. type = VLC->heroh->heroes[subID];
  733. if(!vstd::contains(spells, 0xffffffff) && type->startingSpell >= 0) //hero starts with a spell
  734. spells.insert(type->startingSpell);
  735. else //remove placeholder
  736. spells -= 0xffffffff;
  737. if(!vstd::contains(artifWorn, 16) && type->startingSpell >= 0) //no catapult means we haven't read pre-existant set
  738. {
  739. VLC->arth->equipArtifact(artifWorn, 17, VLC->arth->artifacts[0]); //give spellbook
  740. }
  741. VLC->arth->equipArtifact(artifWorn, 16, VLC->arth->artifacts[3]); //everyone has a catapult
  742. if(portrait < 0 || portrait == 255)
  743. portrait = subID;
  744. if(!hasBonus(Selector::sourceType(Bonus::HERO_BASE_SKILL)))
  745. {
  746. pushPrimSkill(PrimarySkill::ATTACK, type->heroClass->initialAttack);
  747. pushPrimSkill(PrimarySkill::DEFENSE, type->heroClass->initialDefence);
  748. pushPrimSkill(PrimarySkill::SPELL_POWER, type->heroClass->initialPower);
  749. pushPrimSkill(PrimarySkill::KNOWLEDGE, type->heroClass->initialKnowledge);
  750. }
  751. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  752. secSkills = type->secSkillsInit;
  753. if (!name.length())
  754. name = type->name;
  755. if (exp == 0xffffffff)
  756. {
  757. initExp();
  758. }
  759. else
  760. {
  761. level = VLC->heroh->level(exp);
  762. }
  763. if (sex == 0xFF)//sex is default
  764. sex = type->sex;
  765. setFormation(false);
  766. if (!stacksCount()) //standard army//initial army
  767. {
  768. initArmy();
  769. }
  770. assert(validTypes());
  771. hoverName = VLC->generaltexth->allTexts[15];
  772. boost::algorithm::replace_first(hoverName,"%s",name);
  773. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  774. if (mana < 0)
  775. mana = manaLimit();
  776. }
  777. void CGHeroInstance::initArmy(CCreatureSet *dst /*= NULL*/)
  778. {
  779. if(!dst)
  780. dst = this;
  781. int howManyStacks = 0; //how many stacks will hero receives <1 - 3>
  782. int pom = ran()%100;
  783. int warMachinesGiven = 0;
  784. if(pom < 9)
  785. howManyStacks = 1;
  786. else if(pom < 79)
  787. howManyStacks = 2;
  788. else
  789. howManyStacks = 3;
  790. for(int stackNo=0; stackNo < howManyStacks; stackNo++)
  791. {
  792. int creID = (VLC->creh->nameToID[type->refTypeStack[stackNo]]);
  793. int range = type->highStack[stackNo] - type->lowStack[stackNo];
  794. int count = ran()%(range+1) + type->lowStack[stackNo];
  795. if(creID>=145 && creID<=149) //war machine
  796. {
  797. warMachinesGiven++;
  798. switch (creID)
  799. {
  800. case 145: //catapult
  801. VLC->arth->equipArtifact(artifWorn, 16, VLC->arth->artifacts[3]);
  802. break;
  803. default:
  804. VLC->arth->equipArtifact(
  805. artifWorn,
  806. 9+CArtHandler::convertMachineID(creID,true),
  807. VLC->arth->artifacts[CArtHandler::convertMachineID(creID,true)]);
  808. break;
  809. }
  810. }
  811. else
  812. dst->putStack(stackNo-warMachinesGiven, new CStackInstance(creID, count));
  813. }
  814. }
  815. void CGHeroInstance::initHeroDefInfo()
  816. {
  817. if(!defInfo || defInfo->id != HEROI_TYPE)
  818. {
  819. defInfo = new CGDefInfo();
  820. defInfo->id = HEROI_TYPE;
  821. defInfo->subid = subID;
  822. defInfo->printPriority = 0;
  823. defInfo->visitDir = 0xff;
  824. }
  825. for(int i=0;i<6;i++)
  826. {
  827. defInfo->blockMap[i] = 255;
  828. defInfo->visitMap[i] = 0;
  829. defInfo->coverageMap[i] = 0;
  830. defInfo->shadowCoverage[i] = 0;
  831. }
  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< ConstTransitivePtr<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< ConstTransitivePtr<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( \
  1174. static_cast<CGHeroInstance::SecondarySkill>(14 + (schoolMechanicsId))), \
  1175. valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1 << (schoolMechanicsId))); \
  1176. if(thisSchool > skill) \
  1177. { \
  1178. skill = thisSchool; \
  1179. if(outSelectedSchool) \
  1180. *outSelectedSchool = schoolOutId; \
  1181. } \
  1182. }
  1183. TRY_SCHOOL(fire, 0, 1)
  1184. TRY_SCHOOL(air, 1, 0)
  1185. TRY_SCHOOL(water, 2, 2)
  1186. TRY_SCHOOL(earth, 3, 3)
  1187. #undef TRY_SCHOOL
  1188. amax(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  1189. amax(skill, valOfBonuses(Bonus::SPELL, spell->id)); //given by artifact or other effect
  1190. if (hasBonusOfType(Bonus::MAXED_SPELL, spell->id))//hero speciality (Daremyth, Melodia)
  1191. skill = 3;
  1192. assert(skill >= 0 && skill <= 3);
  1193. return skill;
  1194. }
  1195. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  1196. {
  1197. if(!getArt(17)) //if hero has no spellbook
  1198. return false;
  1199. if(vstd::contains(spells, spell->id) //hero has this spell in spellbook
  1200. || (spell->air && hasBonusOfType(Bonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  1201. || (spell->fire && hasBonusOfType(Bonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  1202. || (spell->water && hasBonusOfType(Bonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  1203. || (spell->earth && hasBonusOfType(Bonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  1204. || hasBonusOfType(Bonus::SPELL, spell->id)
  1205. || hasBonusOfType(Bonus::SPELLS_OF_LEVEL, spell->level)
  1206. )
  1207. return true;
  1208. return false;
  1209. }
  1210. /**
  1211. * Calculates what creatures and how many to be raised from a battle.
  1212. * @param battleResult The results of the battle.
  1213. * @return Returns a pair with the first value indicating the ID of the creature
  1214. * type and second value the amount. Both values are returned as -1 if necromancy
  1215. * could not be applied.
  1216. */
  1217. CStackBasicDescriptor CGHeroInstance::calculateNecromancy (const BattleResult &battleResult) const
  1218. {
  1219. const ui8 necromancyLevel = getSecSkillLevel(CGHeroInstance::NECROMANCY);
  1220. // Hero knows necromancy.
  1221. if (necromancyLevel > 0)
  1222. {
  1223. double necromancySkill = valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 12)/100.0;
  1224. amin(necromancySkill, 1.0); //it's impossible to raise more creatures than all...
  1225. const std::map<ui32,si32> &casualties = battleResult.casualties[!battleResult.winner];
  1226. ui32 raisedUnits = 0;
  1227. // Figure out what to raise and how many.
  1228. const ui32 creatureTypes[] = {56, 58, 60, 64}; // IDs for Skeletons, Walking Dead, Wights and Liches respectively.
  1229. const bool improvedNecromancy = hasBonusOfType(Bonus::IMPROVED_NECROMANCY);
  1230. const CCreature *raisedUnitType = VLC->creh->creatures[creatureTypes[improvedNecromancy ? necromancyLevel : 0]];
  1231. const ui32 raisedUnitHP = raisedUnitType->valOfBonuses(Bonus::STACK_HEALTH);
  1232. //calculate creatures raised from each defeated stack
  1233. for (std::map<ui32,si32>::const_iterator it = casualties.begin(); it != casualties.end(); it++)
  1234. {
  1235. // Get lost enemy hit points convertible to units.
  1236. const ui32 raisedHP = VLC->creh->creatures[it->first]->valOfBonuses(Bonus::STACK_HEALTH) * it->second * necromancySkill;
  1237. raisedUnits += std::min<ui32>(raisedHP / raisedUnitHP, it->second * necromancySkill); //limit to % of HP and % of original stack count
  1238. }
  1239. // Make room for new units.
  1240. int slot = getSlotFor(raisedUnitType->idNumber);
  1241. if (slot == -1)
  1242. {
  1243. // If there's no room for unit, try it's upgraded version 2/3rds the size.
  1244. raisedUnitType = VLC->creh->creatures[*raisedUnitType->upgrades.begin()];
  1245. raisedUnits = (raisedUnits*2)/3;
  1246. slot = getSlotFor(raisedUnitType->idNumber);
  1247. }
  1248. if (raisedUnits <= 0)
  1249. raisedUnits = 1;
  1250. return CStackBasicDescriptor(raisedUnitType->idNumber, raisedUnits);
  1251. }
  1252. return CStackBasicDescriptor();
  1253. }
  1254. /**
  1255. * Show the necromancy dialog with information about units raised.
  1256. * @param raisedStack Pair where the first element represents ID of the raised creature
  1257. * and the second element the amount.
  1258. */
  1259. void CGHeroInstance::showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const
  1260. {
  1261. InfoWindow iw;
  1262. iw.soundID = soundBase::GENIE;
  1263. iw.player = tempOwner;
  1264. iw.components.push_back(Component(raisedStack));
  1265. if (raisedStack.count > 1) // Practicing the dark arts of necromancy, ... (plural)
  1266. {
  1267. iw.text.addTxt(MetaString::GENERAL_TXT, 145);
  1268. iw.text.addReplacement(raisedStack.count);
  1269. iw.text.addReplacement(MetaString::CRE_PL_NAMES, raisedStack.type->idNumber);
  1270. }
  1271. else // Practicing the dark arts of necromancy, ... (singular)
  1272. {
  1273. iw.text.addTxt(MetaString::GENERAL_TXT, 146);
  1274. iw.text.addReplacement(MetaString::CRE_SING_NAMES, raisedStack.type->idNumber);
  1275. }
  1276. cb->showInfoDialog(&iw);
  1277. }
  1278. int3 CGHeroInstance::getSightCenter() const
  1279. {
  1280. return getPosition(false);
  1281. }
  1282. int CGHeroInstance::getSightRadious() const
  1283. {
  1284. return 5 + getSecSkillLevel(CGHeroInstance::SCOUTING) + valOfBonuses(Bonus::SIGHT_RADIOUS); //default + scouting
  1285. }
  1286. si32 CGHeroInstance::manaRegain() const
  1287. {
  1288. if (hasBonusOfType(Bonus::FULL_MANA_REGENERATION))
  1289. return manaLimit();
  1290. return 1 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 8) + valOfBonuses(Bonus::MANA_REGENERATION); //1 + Mysticism level
  1291. }
  1292. si32 CGHeroInstance::getArtPos(int aid) const
  1293. {
  1294. for(std::map<ui16, const CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1295. if(i->second->id == aid)
  1296. return i->first;
  1297. return -1;
  1298. }
  1299. /**
  1300. * Places an artifact in hero's backpack. If it's a big artifact equips it
  1301. * or discards it if it cannot be equipped.
  1302. */
  1303. void CGHeroInstance::giveArtifact (ui32 aid) //use only for fixed artifacts
  1304. {
  1305. CArtifact * const artifact = VLC->arth->artifacts[aid]; //pointer to constant object
  1306. if (artifact->isBig())
  1307. {
  1308. for (std::vector<ui16>::const_iterator it = artifact->possibleSlots.begin(); it != artifact->possibleSlots.end(); ++it)
  1309. {
  1310. if (!vstd::contains(artifWorn, *it))
  1311. {
  1312. VLC->arth->equipArtifact(artifWorn, *it, artifact);
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. else
  1318. {
  1319. artifacts.push_back(artifact);
  1320. }
  1321. }
  1322. bool CGHeroInstance::hasArt( ui32 aid ) const
  1323. {
  1324. for(std::vector<const CArtifact*>::const_iterator i = artifacts.begin(); i != artifacts.end(); i++)
  1325. if((*i)->id == aid)
  1326. return true;
  1327. for(std::map<ui16, const CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1328. if(i->second->id == aid)
  1329. return true;
  1330. return false;
  1331. }
  1332. int CGHeroInstance::getBoatType() const
  1333. {
  1334. int alignment = type->heroType / 6;
  1335. switch(alignment)
  1336. {
  1337. case 0:
  1338. return 1; //good
  1339. case 1:
  1340. return 0; //evil
  1341. case 2:
  1342. return 2;
  1343. default:
  1344. throw std::string("Wrong alignment!");
  1345. }
  1346. }
  1347. void CGHeroInstance::getOutOffsets(std::vector<int3> &offsets) const
  1348. {
  1349. 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) };
  1350. for (size_t i = 0; i < ARRAY_COUNT(dirs); i++)
  1351. offsets += dirs[i];
  1352. }
  1353. int CGHeroInstance::getSpellCost(const CSpell *sp) const
  1354. {
  1355. return sp->costs[getSpellSchoolLevel(sp)];
  1356. }
  1357. // void CGHeroInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  1358. // {
  1359. // CArmedInstance::getParents(out, root);
  1360. //
  1361. // if((root == this || contains(static_cast<const CStackInstance *>(root))) && visitedTown && !dynamic_cast<const PlayerState*>(root))
  1362. // {
  1363. // out.insert(visitedTown);
  1364. // }
  1365. //
  1366. // for (std::map<ui16,CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1367. // out.insert(i->second);
  1368. //
  1369. // out.insert(&speciality);
  1370. // }
  1371. void CGHeroInstance::pushPrimSkill(int which, int val)
  1372. {
  1373. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::HERO_BASE_SKILL, val, id, which));
  1374. }
  1375. // void CGHeroInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  1376. // {
  1377. // #define FOREACH_OWNER_TOWN(town) if(const PlayerState *p = cb->getPlayerState(tempOwner)) BOOST_FOREACH(const CGTownInstance *town, p->towns)
  1378. //
  1379. // CArmedInstance::getBonuses(out, selector, root); ///that's not part of macro!
  1380. //
  1381. // //TODO eliminate by moving secondary skills effects to bonus system
  1382. // if(Selector::matchesType(selector, Bonus::LUCK))
  1383. // {
  1384. // //luck skill
  1385. // if(int luckSkill = getSecSkillLevel(9))
  1386. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::SECONDARY_SKILL, luckSkill, 9, VLC->generaltexth->arraytxt[73+luckSkill]));
  1387. //
  1388. // //guardian spirit
  1389. // FOREACH_OWNER_TOWN(t)
  1390. // if(t->subID ==1 && vstd::contains(t->builtBuildings,26)) //rampart with grail
  1391. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::TOWN_STRUCTURE, +2, 26, VLC->generaltexth->buildings[1][26].first + " +2"));
  1392. // }
  1393. //
  1394. // if(Selector::matchesType(selector, Bonus::SEA_MOVEMENT))
  1395. // {
  1396. // //lighthouses
  1397. // FOREACH_OWNER_TOWN(t)
  1398. // if(t->subID == 0 && vstd::contains(t->builtBuildings,17)) //castle
  1399. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::SEA_MOVEMENT, Bonus::TOWN_STRUCTURE, +500, 17, VLC->generaltexth->buildings[0][17].first + " +500"));
  1400. // }
  1401. //
  1402. // if(Selector::matchesType(selector, Bonus::MORALE))
  1403. // {
  1404. // //leadership
  1405. // if(int moraleSkill = getSecSkillLevel(6))
  1406. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::SECONDARY_SKILL, moraleSkill, 6, VLC->generaltexth->arraytxt[104+moraleSkill]));
  1407. //
  1408. // //colossus
  1409. // FOREACH_OWNER_TOWN(t)
  1410. // if(t->subID == 0 && vstd::contains(t->builtBuildings,26)) //castle
  1411. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +2, 26, VLC->generaltexth->buildings[0][26].first + " +2"));
  1412. // }
  1413. //
  1414. // if(Selector::matchesTypeSubtype(selector, Bonus::SECONDARY_SKILL_PREMY, 12)) //necromancy
  1415. // {
  1416. // FOREACH_OWNER_TOWN(t)
  1417. // {
  1418. // if(t->subID == 4) //necropolis
  1419. // {
  1420. // if(vstd::contains(t->builtBuildings,21)) //necromancy amplifier
  1421. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::SECONDARY_SKILL_PREMY, Bonus::TOWN_STRUCTURE, +10, 21, VLC->generaltexth->buildings[4][21].first + " +10%", 12));
  1422. // if(vstd::contains(t->builtBuildings,26)) //grail - Soul prison
  1423. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::SECONDARY_SKILL_PREMY, Bonus::TOWN_STRUCTURE, +20, 26, VLC->generaltexth->buildings[4][26].first + " +20%", 12));
  1424. // }
  1425. // }
  1426. // }
  1427. // }
  1428. EAlignment CGHeroInstance::getAlignment() const
  1429. {
  1430. return type->heroClass->getAlignment();
  1431. }
  1432. void CGHeroInstance::initExp()
  1433. {
  1434. exp=40+ (ran()) % 50;
  1435. level = 1;
  1436. }
  1437. std::string CGHeroInstance::nodeName() const
  1438. {
  1439. return "Hero " + name;
  1440. }
  1441. void CGDwelling::initObj()
  1442. {
  1443. switch(ID)
  1444. {
  1445. case 17:
  1446. {
  1447. int crid = VLC->objh->cregens[subID];
  1448. const CCreature *crs = VLC->creh->creatures[crid];
  1449. creatures.resize(1);
  1450. creatures[0].second.push_back(crid);
  1451. hoverName = VLC->generaltexth->creGens[subID];
  1452. if(crs->level > 4)
  1453. putStack(0, new CStackInstance(crs, (crs->growth) * 3));
  1454. if (getOwner() != 255)
  1455. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1456. }
  1457. break;
  1458. case 20:
  1459. creatures.resize(4);
  1460. if(subID == 1) //Golem Factory
  1461. {
  1462. creatures[0].second.push_back(32); //Stone Golem
  1463. creatures[1].second.push_back(33); //Iron Golem
  1464. creatures[2].second.push_back(116); //Gold Golem
  1465. creatures[3].second.push_back(117); //Diamond Golem
  1466. //guards
  1467. putStack(0, new CStackInstance(116, 9));
  1468. putStack(1, new CStackInstance(117, 6));
  1469. }
  1470. else if(subID == 0) // Elemental Conflux
  1471. {
  1472. creatures[0].second.push_back(112); //Air Elemental
  1473. creatures[1].second.push_back(114); //Fire Elemental
  1474. creatures[2].second.push_back(113); //Earth Elemental
  1475. creatures[3].second.push_back(115); //Water Elemental
  1476. //guards
  1477. putStack(0, new CStackInstance(113, 12));
  1478. }
  1479. else
  1480. {
  1481. assert(0);
  1482. }
  1483. hoverName = VLC->generaltexth->creGens4[subID];
  1484. break;
  1485. case 78: //Refugee Camp
  1486. //is handled within newturn func
  1487. break;
  1488. case 106: //War Machine Factory
  1489. creatures.resize(3);
  1490. creatures[0].second.push_back(146); //Ballista
  1491. creatures[1].second.push_back(147); //First Aid Tent
  1492. creatures[2].second.push_back(148); //Ammo Cart
  1493. break;
  1494. default:
  1495. assert(0);
  1496. break;
  1497. }
  1498. }
  1499. void CGDwelling::setProperty(ui8 what, ui32 val)
  1500. {
  1501. switch (what)
  1502. {
  1503. case ObjProperty::OWNER: //change owner
  1504. if (ID == 17) //single generators
  1505. {
  1506. if (tempOwner != NEUTRAL_PLAYER)
  1507. {
  1508. std::vector<ConstTransitivePtr<CGDwelling> >* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1509. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1510. }
  1511. if (val != NEUTRAL_PLAYER) //can new owner be neutral?
  1512. cb->gameState()->players[val].dwellings.push_back (this);
  1513. }
  1514. break;
  1515. case ObjProperty::AVAILABLE_CREATURE:
  1516. creatures.resize(1);
  1517. creatures[0].second.resize(1);
  1518. creatures[0].second[0] = val;
  1519. break;
  1520. }
  1521. CGObjectInstance::setProperty(what,val);
  1522. }
  1523. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1524. {
  1525. if(ID == 78 && !creatures[0].first) //Refugee Camp, no available cres
  1526. {
  1527. InfoWindow iw;
  1528. iw.player = h->tempOwner;
  1529. iw.text.addTxt(MetaString::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  1530. iw.text.addReplacement(MetaString::OBJ_NAMES, ID);
  1531. cb->sendAndApply(&iw);
  1532. return;
  1533. }
  1534. int relations = cb->gameState()->getPlayerRelations( h->tempOwner, tempOwner );
  1535. if ( relations == 1 )//ally
  1536. return;//do not allow recruiting or capturing
  1537. if( !relations && stacksCount() > 0) //object is guarded, owned by enemy
  1538. {
  1539. BlockingDialog bd;
  1540. bd.player = h->tempOwner;
  1541. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1542. 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?
  1543. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1544. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + Slots().begin()->second->getQuantityID()*3);
  1545. bd.text.addReplacement(*Slots().begin()->second);
  1546. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1547. return;
  1548. }
  1549. if(!relations && ID != 106)
  1550. {
  1551. cb->setOwner(id, h->tempOwner);
  1552. }
  1553. BlockingDialog bd;
  1554. bd.player = h->tempOwner;
  1555. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1556. if(ID == 17 || ID == 20)
  1557. {
  1558. 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?
  1559. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1560. for(size_t i = 0; i < creatures.size(); i++)
  1561. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1562. }
  1563. else if(ID == 78)
  1564. {
  1565. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  1566. bd.text.addReplacement(MetaString::OBJ_NAMES, ID);
  1567. for(size_t i = 0; i < creatures.size(); i++)
  1568. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1569. }
  1570. else if(ID == 106)
  1571. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  1572. else
  1573. throw std::string("Illegal dwelling!");
  1574. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1575. }
  1576. void CGDwelling::newTurn() const
  1577. {
  1578. if(cb->getDate(1) != 1) //not first day of week
  1579. return;
  1580. //town growths and War Machines Factories are handled separately
  1581. if(ID == TOWNI_TYPE || ID == 106)
  1582. return;
  1583. if(ID == 78) //if it's a refugee camp, we need to pick an available creature
  1584. {
  1585. cb->setObjProperty(id, ObjProperty::AVAILABLE_CREATURE, VLC->creh->pickRandomMonster());
  1586. }
  1587. bool change = false;
  1588. SetAvailableCreatures sac;
  1589. sac.creatures = creatures;
  1590. sac.tid = id;
  1591. for (size_t i = 0; i < creatures.size(); i++)
  1592. {
  1593. if(creatures[i].second.size())
  1594. {
  1595. CCreature *cre = VLC->creh->creatures[creatures[i].second[0]];
  1596. TQuantity amount = cre->growth * (1 + cre->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(Bonus::CREATURE_GROWTH);
  1597. if (DWELLINGS_ACCUMULATE_CREATURES)
  1598. sac.creatures[i].first += amount;
  1599. else
  1600. sac.creatures[i].first = amount;
  1601. change = true;
  1602. }
  1603. }
  1604. if(change)
  1605. cb->sendAndApply(&sac);
  1606. }
  1607. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1608. {
  1609. if(!answer)
  1610. return;
  1611. int crid = creatures[0].second[0];
  1612. CCreature *crs = VLC->creh->creatures[crid];
  1613. TQuantity count = creatures[0].first;
  1614. if(crs->level == 1 && ID != 78) //first level - creatures are for free
  1615. {
  1616. if(count) //there are available creatures
  1617. {
  1618. int slot = h->getSlotFor(crid);
  1619. if(slot < 0) //no available slot
  1620. {
  1621. InfoWindow iw;
  1622. iw.player = h->tempOwner;
  1623. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1624. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1625. cb->showInfoDialog(&iw);
  1626. }
  1627. else //give creatures
  1628. {
  1629. SetAvailableCreatures sac;
  1630. sac.tid = id;
  1631. sac.creatures = creatures;
  1632. sac.creatures[0].first = 0;
  1633. InfoWindow iw;
  1634. iw.player = h->tempOwner;
  1635. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1636. iw.text.addReplacement(count);
  1637. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1638. cb->showInfoDialog(&iw);
  1639. cb->sendAndApply(&sac);
  1640. cb->addToSlot(StackLocation(h, slot), crs, count);
  1641. }
  1642. }
  1643. else //there no creatures
  1644. {
  1645. InfoWindow iw;
  1646. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1647. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1648. iw.player = h->tempOwner;
  1649. cb->sendAndApply(&iw);
  1650. }
  1651. }
  1652. else
  1653. {
  1654. if(ID == 106) //pick available War Machines
  1655. {
  1656. //there is 1 war machine available to recruit if hero doesn't have one
  1657. SetAvailableCreatures sac;
  1658. sac.tid = id;
  1659. sac.creatures = creatures;
  1660. sac.creatures[0].first = !h->getArt(13); //ballista
  1661. sac.creatures[1].first = !h->getArt(15); //first aid tent
  1662. sac.creatures[2].first = !h->getArt(14); //ammo cart
  1663. cb->sendAndApply(&sac);
  1664. }
  1665. OpenWindow ow;
  1666. ow.id1 = id;
  1667. ow.id2 = h->id;
  1668. ow.window = (ID == 17 || ID == 78)
  1669. ? OpenWindow::RECRUITMENT_FIRST
  1670. : OpenWindow::RECRUITMENT_ALL;
  1671. cb->sendAndApply(&ow);
  1672. }
  1673. }
  1674. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1675. {
  1676. if(answer)
  1677. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1678. }
  1679. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1680. {
  1681. if (result->winner == 0)
  1682. {
  1683. onHeroVisit(h);
  1684. }
  1685. }
  1686. int CGTownInstance::getSightRadious() const //returns sight distance
  1687. {
  1688. if (subID == 2) //tower
  1689. {
  1690. if ((builtBuildings.find(26)) != builtBuildings.end()) //skyship
  1691. return -1; //entire map
  1692. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1693. return 20;
  1694. }
  1695. return 5;
  1696. }
  1697. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1698. {
  1699. ///this is freakin' overcomplicated solution
  1700. switch (what)
  1701. {
  1702. case 11: //add visitor of town building
  1703. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id);
  1704. break;
  1705. case 12:
  1706. bonusingBuildings[val]->setProperty (12, 0);
  1707. break;
  1708. case 13: //add garrisoned hero to visitors
  1709. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id);
  1710. break;
  1711. case 14:
  1712. bonusValue.first = val;
  1713. break;
  1714. case 15:
  1715. bonusValue.second = val;
  1716. break;
  1717. }
  1718. }
  1719. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1720. {
  1721. if((builtBuildings.find(9))!=builtBuildings.end())
  1722. return 3;
  1723. if((builtBuildings.find(8))!=builtBuildings.end())
  1724. return 2;
  1725. if((builtBuildings.find(7))!=builtBuildings.end())
  1726. return 1;
  1727. return 0;
  1728. }
  1729. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1730. {
  1731. if ((builtBuildings.find(13))!=builtBuildings.end())
  1732. return 3;
  1733. if ((builtBuildings.find(12))!=builtBuildings.end())
  1734. return 2;
  1735. if ((builtBuildings.find(11))!=builtBuildings.end())
  1736. return 1;
  1737. if ((builtBuildings.find(10))!=builtBuildings.end())
  1738. return 0;
  1739. return -1;
  1740. }
  1741. int CGTownInstance::mageGuildLevel() const
  1742. {
  1743. if ((builtBuildings.find(4))!=builtBuildings.end())
  1744. return 5;
  1745. if ((builtBuildings.find(3))!=builtBuildings.end())
  1746. return 4;
  1747. if ((builtBuildings.find(2))!=builtBuildings.end())
  1748. return 3;
  1749. if ((builtBuildings.find(1))!=builtBuildings.end())
  1750. return 2;
  1751. if ((builtBuildings.find(0))!=builtBuildings.end())
  1752. return 1;
  1753. return 0;
  1754. }
  1755. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1756. {
  1757. if ( level<0 || level >= CREATURES_PER_TOWN )
  1758. return false;
  1759. return vstd::contains(builtBuildings, 30+level+upgraded*CREATURES_PER_TOWN);
  1760. }
  1761. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1762. {
  1763. return town->hordeLvl[HID];
  1764. }
  1765. int CGTownInstance::creatureGrowth(const int & level) const
  1766. {
  1767. if (level<0 || level >=CREATURES_PER_TOWN)
  1768. return 0;
  1769. TCreature creid = town->basicCreatures[level];
  1770. int ret = VLC->creh->creatures[creid]->growth;
  1771. switch(fortLevel())
  1772. {
  1773. case 3:
  1774. ret*=2;break;
  1775. case 2:
  1776. ret*=(1.5); break;
  1777. }
  1778. ret *= (1 + VLC->creh->creatures[creid]->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100); // double growth or plague
  1779. if(tempOwner != NEUTRAL_PLAYER)
  1780. {
  1781. ret *= (100.0f + cb->gameState()->players[tempOwner].valOfBonuses
  1782. (Selector::type(Bonus::CREATURE_GROWTH_PERCENT) && Selector::sourceType(Bonus::ARTIFACT)))/100; //Statue of Legion
  1783. for (std::vector<ConstTransitivePtr<CGDwelling> >::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1784. { //+1 for each dwelling
  1785. if (VLC->creh->creatures[creid]->idNumber == (*it)->creatures[0].second[0])
  1786. ++ret;
  1787. }
  1788. }
  1789. if(getHordeLevel(0)==level)
  1790. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1791. ret+=VLC->creh->creatures[creid]->hordeGrowth;
  1792. if(getHordeLevel(1)==level)
  1793. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1794. ret+=VLC->creh->creatures[creid]->hordeGrowth;
  1795. //support for legs of legion etc.
  1796. if(garrisonHero)
  1797. ret += garrisonHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1798. if(visitingHero)
  1799. ret += visitingHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1800. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1801. ret*=1.5;
  1802. //spcecial week unaffected by grail (bug in original game?)
  1803. ret += VLC->creh->creatures[creid]->valOfBonuses(Bonus::CREATURE_GROWTH);
  1804. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1805. }
  1806. int CGTownInstance::dailyIncome() const
  1807. {
  1808. int ret = 0;
  1809. if ((builtBuildings.find(26))!=builtBuildings.end())
  1810. ret+=5000;
  1811. if ((builtBuildings.find(13))!=builtBuildings.end())
  1812. ret+=4000;
  1813. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1814. ret+=2000;
  1815. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1816. ret+=1000;
  1817. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1818. ret+=500;
  1819. return ret;
  1820. }
  1821. bool CGTownInstance::hasFort() const
  1822. {
  1823. return (builtBuildings.find(7))!=builtBuildings.end();
  1824. }
  1825. bool CGTownInstance::hasCapitol() const
  1826. {
  1827. return (builtBuildings.find(13))!=builtBuildings.end();
  1828. }
  1829. CGTownInstance::CGTownInstance()
  1830. :IShipyard(this), IMarket(this)
  1831. {
  1832. builded=-1;
  1833. destroyed=-1;
  1834. garrisonHero=NULL;
  1835. town=NULL;
  1836. visitingHero = NULL;
  1837. }
  1838. CGTownInstance::~CGTownInstance()
  1839. {
  1840. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1841. delete *i;
  1842. }
  1843. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1844. {
  1845. if(checkGuild && mageGuildLevel() < level)
  1846. return 0;
  1847. int ret = 6 - level; //how many spells are available at this level
  1848. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1849. ret++;
  1850. return ret;
  1851. }
  1852. int CGTownInstance::defenceBonus(int type) const
  1853. {
  1854. int ret=0;
  1855. switch (type)
  1856. {
  1857. /*attack*/ case 0:
  1858. if (subID == 6 && vstd::contains(builtBuildings,26))//Stronghold, grail
  1859. ret += 12;
  1860. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, grail
  1861. ret += 10;
  1862. if (subID == 7 && vstd::contains(builtBuildings,22))//Fortress, Blood Obelisk
  1863. ret += 2;
  1864. return ret;
  1865. /*defence*/ case 1:
  1866. if (subID == 7 && vstd::contains(builtBuildings,21))//Fortress, Glyphs of Fear
  1867. ret += 2;
  1868. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, Grail
  1869. ret += 10;
  1870. return ret;
  1871. /*spellpower*/ case 2:
  1872. if (subID == 3 && vstd::contains(builtBuildings,21))//Inferno, Brimstone Clouds
  1873. ret += 2;
  1874. if (subID == 5 && vstd::contains(builtBuildings,26))//Dungeon, Grail
  1875. ret += 12;
  1876. return ret;
  1877. /*knowledge*/ case 3:
  1878. if (subID == 2 && vstd::contains(builtBuildings,26))//Tower, Grail
  1879. ret += 15;
  1880. return ret;
  1881. }
  1882. return 0;//Why we are here? wrong type?
  1883. }
  1884. bool CGTownInstance::needsLastStack() const
  1885. {
  1886. if(garrisonHero)
  1887. return true;
  1888. else return false;
  1889. }
  1890. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1891. {
  1892. if( !cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ))//if this is enemy
  1893. {
  1894. if(stacksCount() > 0 || visitingHero)
  1895. {
  1896. const CGHeroInstance *defendingHero = NULL;
  1897. if(visitingHero)
  1898. defendingHero = visitingHero;
  1899. else if(garrisonHero)
  1900. defendingHero = garrisonHero;
  1901. const CArmedInstance *defendingArmy = this;
  1902. if(defendingHero)
  1903. defendingArmy = defendingHero;
  1904. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1905. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1906. }
  1907. else
  1908. {
  1909. cb->setOwner(id, h->tempOwner);
  1910. removeCapitols (h->getOwner());
  1911. cb->heroVisitCastle(id, h->id);
  1912. }
  1913. }
  1914. else
  1915. cb->heroVisitCastle(id, h->id);
  1916. }
  1917. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1918. {
  1919. cb->stopHeroVisitCastle(id,h->id);
  1920. }
  1921. void CGTownInstance::initObj()
  1922. ///initialize town structures
  1923. {
  1924. blockVisit = true;
  1925. hoverName = name + ", " + town->Name();
  1926. if (subID == 5)
  1927. creatures.resize(CREATURES_PER_TOWN+1);//extra dwelling for Dungeon
  1928. else
  1929. creatures.resize(CREATURES_PER_TOWN);
  1930. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1931. {
  1932. if(creatureDwelling(i,false))
  1933. creatures[i].second.push_back(town->basicCreatures[i]);
  1934. if(creatureDwelling(i,true))
  1935. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1936. }
  1937. switch (subID)
  1938. { //add new visitable objects
  1939. case 0:
  1940. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1941. break;
  1942. case 5:
  1943. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1944. case 2: case 3: case 6:
  1945. bonusingBuildings.push_back (new CTownBonus(23, this));
  1946. break;
  1947. case 7:
  1948. bonusingBuildings.push_back (new CTownBonus(17, this));
  1949. break;
  1950. }
  1951. //add special bonuses from buildings
  1952. if(subID == 4 && vstd::contains(builtBuildings, 17))
  1953. {
  1954. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::DARKNESS, Bonus::TOWN_STRUCTURE, 20, 17) );
  1955. }
  1956. }
  1957. void CGTownInstance::newTurn() const
  1958. {
  1959. if (cb->getDate(1) == 1) //reset on new week
  1960. {
  1961. if (vstd::contains(builtBuildings,17) && subID == 1 && cb->getDate(0) != 1 && (tempOwner < PLAYER_LIMIT) )//give resources for Rampart, Mystic Pond
  1962. {
  1963. int resID = rand()%4+2;//bonus to random rare resource
  1964. resID = (resID==2)?1:resID;
  1965. int resVal = rand()%4+1;//with size 1..4
  1966. cb->giveResource(tempOwner, resID, resVal);
  1967. cb->setObjProperty (id, 14, resID);
  1968. cb->setObjProperty (id, 15, resVal);
  1969. }
  1970. if ( subID == 5 )
  1971. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1972. {
  1973. if ((*i)->ID == 21)
  1974. cb->setObjProperty (id, 12, (*i)->id); //reset visitors for Mana Vortex
  1975. }
  1976. if (tempOwner == NEUTRAL_PLAYER) //garrison growth for neutral towns
  1977. {
  1978. std::vector<ui8> nativeCrits; //slots
  1979. for (TSlots::const_iterator it = Slots().begin(); it != Slots().end(); it++)
  1980. {
  1981. if (it->second->type->faction == subID) //native
  1982. {
  1983. nativeCrits.push_back(it->first); //collect matching slots
  1984. }
  1985. }
  1986. if (nativeCrits.size())
  1987. {
  1988. TSlot pos = nativeCrits[rand() % nativeCrits.size()];
  1989. StackLocation sl(this, pos);
  1990. const CCreature *c = getCreature(pos);
  1991. if (rand()%100 < 90 || c->upgrades.empty()) //increase number if no upgrade avaliable
  1992. {
  1993. cb->changeStackCount(sl, c->growth);
  1994. }
  1995. else //upgrade
  1996. {
  1997. cb->changeStackType(sl, VLC->creh->creatures[*c->upgrades.begin()]);
  1998. }
  1999. }
  2000. if ((stacksCount() < ARMY_SIZE && rand()%100 < 25) || Slots().empty()) //add new stack
  2001. {
  2002. int i = rand() % std::min (ARMY_SIZE, cb->getDate(3)<<1);
  2003. TCreature c = town->basicCreatures[i];
  2004. TSlot n = -1;
  2005. TQuantity count = creatureGrowth(i);
  2006. {//no lower tiers or above current month
  2007. if ((n = getSlotFor(c))>=0)
  2008. {
  2009. StackLocation sl(this, n);
  2010. if (slotEmpty(n))
  2011. cb->insertNewStack(sl, VLC->creh->creatures[c], count);
  2012. else //add to existing
  2013. cb->changeStackCount(sl, count);
  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<ConstTransitivePtr<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(CGHeroInstance::LEARNING)*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(CGHeroInstance::DIPLOMACY)) //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(CGHeroInstance::LEARNING)*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>(getStackCount(0)));
  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. TQuantity &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<ConstTransitivePtr<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(CGHeroInstance::DIPLOMACY) + sympathy;
  2799. if(charisma >= character) //creatures might join...
  2800. {
  2801. if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) + sympathy + 1 >= character)
  2802. return 0; //join for free
  2803. else if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) * 2 + sympathy + 1 >= character)
  2804. return VLC->creh->creatures[subID]->cost[6] * getStackCount(0); //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. cb->tryJoiningArmy(this, h, true, false);
  2863. // int slot = h->getSlotFor(subID);
  2864. // if(slot >= 0) //there is place
  2865. // {
  2866. // //add creatures
  2867. // SetGarrisons sg;
  2868. // sg.garrs[h->id] = h->getArmy();
  2869. // sg.garrs[h->id].addToSlot(slot, subID, getStackCount(0));
  2870. // cb->sendAndApply(&sg);
  2871. // cb->removeObject(id);
  2872. // }
  2873. // else
  2874. // {
  2875. // cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2876. // }
  2877. }
  2878. }
  2879. void CGCreature::fight( const CGHeroInstance *h ) const
  2880. {
  2881. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2882. }
  2883. void CGCreature::flee( const CGHeroInstance * h ) const
  2884. {
  2885. BlockingDialog ynd(true,false);
  2886. ynd.player = h->tempOwner;
  2887. ynd.text << std::pair<ui8,ui32>(11,91);
  2888. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2889. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2890. }
  2891. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2892. {
  2893. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  2894. if(relations == 2) //we're visiting our mine
  2895. {
  2896. cb->showGarrisonDialog(id,h->id,true,0);
  2897. return;
  2898. }
  2899. else if (relations == 1)//ally
  2900. return;
  2901. if(stacksCount()) //Mine is guarded
  2902. {
  2903. BlockingDialog ynd(true,false);
  2904. ynd.player = h->tempOwner;
  2905. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  2906. cb->showBlockingDialog(&ynd,boost::bind(&CGMine::fight, this, _1, h));
  2907. return;
  2908. }
  2909. flagMine(h->tempOwner);
  2910. }
  2911. void CGMine::newTurn() const
  2912. {
  2913. if(cb->getDate() == 1)
  2914. return;
  2915. if (tempOwner == NEUTRAL_PLAYER)
  2916. return;
  2917. cb->giveResource(tempOwner, producedResource, producedQuantity);
  2918. }
  2919. void CGMine::initObj()
  2920. {
  2921. if(subID >= 7) //Abandoned Mine
  2922. {
  2923. //set guardians
  2924. int howManyTroglodytes = 100 + ran()%100;
  2925. CStackInstance *troglodytes = new CStackInstance(70, howManyTroglodytes);
  2926. putStack(0, troglodytes);
  2927. //after map reading tempOwner placeholds bitmask for allowed resources
  2928. std::vector<int> possibleResources;
  2929. for (int i = 0; i < 8; i++)
  2930. if(tempOwner & 1<<i)
  2931. possibleResources.push_back(i);
  2932. assert(possibleResources.size());
  2933. producedResource = possibleResources[ran()%possibleResources.size()];
  2934. tempOwner = NEUTRAL_PLAYER;
  2935. hoverName = VLC->generaltexth->mines[7].first + "\n" + VLC->generaltexth->allTexts[202] + " " + troglodytes->getQuantityTXT(false) + " " + troglodytes->type->namePl;
  2936. }
  2937. else
  2938. {
  2939. producedResource = subID;
  2940. MetaString ms;
  2941. ms << std::pair<ui8,ui32>(9,producedResource);
  2942. if(tempOwner >= PLAYER_LIMIT)
  2943. tempOwner = NEUTRAL_PLAYER;
  2944. else
  2945. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2946. ms.toString(hoverName);
  2947. }
  2948. producedQuantity = defaultResProduction();
  2949. }
  2950. void CGMine::fight(ui32 agreed, const CGHeroInstance *h) const
  2951. {
  2952. cb->startBattleI(h, this, boost::bind(&CGMine::endBattle, this, _1, h->tempOwner));
  2953. }
  2954. void CGMine::endBattle(BattleResult *result, ui8 attackingPlayer) const
  2955. {
  2956. if(result->winner == 0) //attacker won
  2957. {
  2958. if(subID == 7)
  2959. {
  2960. InfoWindow iw;
  2961. iw.player = attackingPlayer;
  2962. iw.text.addTxt(MetaString::ADVOB_TXT, 85);
  2963. cb->showInfoDialog(&iw);
  2964. }
  2965. flagMine(attackingPlayer);
  2966. }
  2967. }
  2968. void CGMine::flagMine(ui8 player) const
  2969. {
  2970. assert(tempOwner != player);
  2971. cb->setOwner(id,player); //not ours? flag it!
  2972. MetaString ms;
  2973. ms << std::pair<ui8,ui32>(9,subID) << "\n(" << std::pair<ui8,ui32>(6,23+player) << ")";
  2974. if(subID == 7)
  2975. {
  2976. ms << "(%s)";
  2977. ms.addReplacement(MetaString::RES_NAMES, producedResource);
  2978. }
  2979. cb->setHoverName(id,&ms);
  2980. InfoWindow iw;
  2981. iw.soundID = soundBase::FLAGMINE;
  2982. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  2983. iw.player = player;
  2984. iw.components.push_back(Component(2,producedResource,producedQuantity,-1));
  2985. cb->showInfoDialog(&iw);
  2986. }
  2987. ui32 CGMine::defaultResProduction()
  2988. {
  2989. switch(producedResource)
  2990. {
  2991. case 0: //wood
  2992. case 2: //ore
  2993. return 2;
  2994. case 6: //gold
  2995. return 1000;
  2996. default:
  2997. return 1;
  2998. }
  2999. }
  3000. void CGResource::initObj()
  3001. {
  3002. blockVisit = true;
  3003. hoverName = VLC->generaltexth->restypes[subID];
  3004. if(!amount)
  3005. {
  3006. switch(subID)
  3007. {
  3008. case 6:
  3009. amount = 500 + (rand()%6)*100;
  3010. break;
  3011. case 0: case 2:
  3012. amount = 6 + (rand()%5);
  3013. break;
  3014. default:
  3015. amount = 3 + (rand()%3);
  3016. break;
  3017. }
  3018. }
  3019. }
  3020. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  3021. {
  3022. if(stacksCount())
  3023. {
  3024. if(message.size())
  3025. {
  3026. BlockingDialog ynd(true,false);
  3027. ynd.player = h->getOwner();
  3028. ynd.text << message;
  3029. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  3030. }
  3031. else
  3032. {
  3033. fightForRes(1,h);
  3034. }
  3035. }
  3036. else
  3037. {
  3038. if(message.length())
  3039. {
  3040. InfoWindow iw;
  3041. iw.player = h->tempOwner;
  3042. iw.text << message;
  3043. cb->showInfoDialog(&iw);
  3044. }
  3045. collectRes(h->getOwner());
  3046. }
  3047. }
  3048. void CGResource::collectRes( int player ) const
  3049. {
  3050. cb->giveResource(player,subID,amount);
  3051. ShowInInfobox sii;
  3052. sii.player = player;
  3053. sii.c = Component(2,subID,amount,0);
  3054. sii.text << std::pair<ui8,ui32>(11,113);
  3055. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  3056. cb->showCompInfo(&sii);
  3057. cb->removeObject(id);
  3058. }
  3059. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  3060. {
  3061. if(agreed)
  3062. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  3063. }
  3064. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3065. {
  3066. if(result->winner == 0) //attacker won
  3067. collectRes(h->getOwner());
  3068. }
  3069. void CGVisitableOPW::newTurn() const
  3070. {
  3071. if (cb->getDate(1) == 1) //first day of week = 1
  3072. {
  3073. cb->setObjProperty(id, ObjProperty::VISITED, false);
  3074. MetaString ms; //set text to "not visited"
  3075. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  3076. cb->setHoverName(id,&ms);
  3077. }
  3078. }
  3079. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  3080. {
  3081. int mid, sound = 0;
  3082. switch (ID)
  3083. {
  3084. case 55: //mystical garden
  3085. sound = soundBase::experience;
  3086. mid = 92;
  3087. break;
  3088. case 112://windmill
  3089. sound = soundBase::GENIE;
  3090. mid = 170;
  3091. break;
  3092. case 109://waterwheel
  3093. sound = soundBase::GENIE;
  3094. mid = 164;
  3095. break;
  3096. }
  3097. if (visited)
  3098. {
  3099. if (ID!=112)
  3100. mid++;
  3101. else
  3102. mid--;
  3103. InfoWindow iw;
  3104. iw.soundID = sound;
  3105. iw.player = h->tempOwner;
  3106. iw.text << std::pair<ui8,ui32>(11,mid);
  3107. cb->showInfoDialog(&iw);
  3108. }
  3109. else
  3110. {
  3111. int type, sub, val;
  3112. type = 2;
  3113. switch (ID)
  3114. {
  3115. case 55:
  3116. if (rand()%2)
  3117. {
  3118. sub = 5;
  3119. val = 5;
  3120. }
  3121. else
  3122. {
  3123. sub = 6;
  3124. val = 500;
  3125. }
  3126. break;
  3127. case 112:
  3128. mid = 170;
  3129. sub = (rand() % 5) + 1;
  3130. val = (rand() % 4) + 3;
  3131. break;
  3132. case 109:
  3133. mid = 164;
  3134. sub = 6;
  3135. if(cb->getDate(0)<8)
  3136. val = 500;
  3137. else
  3138. val = 1000;
  3139. }
  3140. cb->giveResource(h->tempOwner,sub,val);
  3141. InfoWindow iw;
  3142. iw.soundID = sound;
  3143. iw.player = h->tempOwner;
  3144. iw.components.push_back(Component(type,sub,val,0));
  3145. iw.text << std::pair<ui8,ui32>(11,mid);
  3146. cb->showInfoDialog(&iw);
  3147. cb->setObjProperty(id, ObjProperty::VISITED, true);
  3148. MetaString ms; //set text to "visited"
  3149. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  3150. cb->setHoverName(id,&ms);
  3151. }
  3152. }
  3153. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  3154. {
  3155. if(what == ObjProperty::VISITED)
  3156. visited = val;
  3157. }
  3158. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  3159. {
  3160. int destinationid=-1;
  3161. switch(ID)
  3162. {
  3163. case 43: //one way - find corresponding exit monolith
  3164. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  3165. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  3166. else
  3167. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3168. break;
  3169. case 45://two way monolith - pick any other one
  3170. case 111: //Whirlpool
  3171. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  3172. {
  3173. while ((destinationid = objs[ID][subID][rand()%objs[ID][subID].size()]) == id); //choose another exit
  3174. if (ID == 111)
  3175. {
  3176. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  3177. {
  3178. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  3179. { //we can't remove last unit
  3180. TSlot targetstack = h->Slots().begin()->first; //slot numbers may vary
  3181. for(TSlots::const_reverse_iterator i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  3182. {
  3183. if (h->getPower(targetstack) > h->getPower(i->first))
  3184. {
  3185. targetstack = (i->first);
  3186. }
  3187. }
  3188. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  3189. amax(countToTake, 1);
  3190. InfoWindow iw;
  3191. iw.player = h->tempOwner;
  3192. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  3193. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  3194. cb->showInfoDialog(&iw);
  3195. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  3196. }
  3197. }
  3198. }
  3199. }
  3200. else
  3201. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3202. break;
  3203. case 103: //find nearest subterranean gate on the other level
  3204. {
  3205. int i=0;
  3206. for(; i < gates.size(); i++)
  3207. {
  3208. if(gates[i].first == id)
  3209. {
  3210. destinationid = gates[i].second;
  3211. break;
  3212. }
  3213. else if(gates[i].second == id)
  3214. {
  3215. destinationid = gates[i].first;
  3216. break;
  3217. }
  3218. }
  3219. if(destinationid < 0 || i == gates.size()) //no exit
  3220. {
  3221. InfoWindow iw;
  3222. iw.player = h->tempOwner;
  3223. iw.text.addTxt(MetaString::ADVOB_TXT, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  3224. cb->sendAndApply(&iw);
  3225. }
  3226. break;
  3227. }
  3228. }
  3229. if(destinationid < 0)
  3230. {
  3231. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  3232. return;
  3233. }
  3234. if (ID == 111)
  3235. {
  3236. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  3237. std::set<int3>::iterator it = tiles.begin();
  3238. std::advance (it, rand() % tiles.size()); //picking random element of set is tiring
  3239. cb->moveHero (h->id, *it + int3(1,0,0), true);
  3240. }
  3241. else
  3242. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  3243. }
  3244. void CGTeleport::initObj()
  3245. {
  3246. int si = subID;
  3247. switch (ID)
  3248. {
  3249. case 103://ignore subterranean gates subid
  3250. case 111:
  3251. {
  3252. si = 0;
  3253. break;
  3254. }
  3255. default:
  3256. break;
  3257. }
  3258. objs[ID][si].push_back(id);
  3259. }
  3260. void CGTeleport::postInit() //matches subterranean gates into pairs
  3261. {
  3262. //split on underground and surface gates
  3263. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  3264. for(size_t i = 0; i < objs[103][0].size(); i++)
  3265. {
  3266. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  3267. gatesSplit[hlp->pos.z].push_back(hlp);
  3268. }
  3269. //sort by position
  3270. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  3271. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  3272. {
  3273. const CGObjectInstance *cur = gatesSplit[0][i];
  3274. //find nearest underground exit
  3275. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  3276. for(int j = 0; j < gatesSplit[1].size(); j++)
  3277. {
  3278. const CGObjectInstance *checked = gatesSplit[1][j];
  3279. if(!checked)
  3280. continue;
  3281. double hlp = checked->pos.dist2d(cur->pos);
  3282. if(hlp < best.second)
  3283. {
  3284. best.first = j;
  3285. best.second = hlp;
  3286. }
  3287. }
  3288. if(best.first >= 0) //found pair
  3289. {
  3290. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  3291. gatesSplit[1][best.first] = NULL;
  3292. }
  3293. else
  3294. {
  3295. gates.push_back(std::pair<int, int>(cur->id, -1));
  3296. }
  3297. }
  3298. objs.erase(103);
  3299. }
  3300. void CGArtifact::initObj()
  3301. {
  3302. blockVisit = true;
  3303. if(ID == 5)
  3304. {
  3305. hoverName = VLC->arth->artifacts[subID]->Name();
  3306. if(!storedArtifact->artType)
  3307. storedArtifact->setType(VLC->arth->artifacts[subID]);
  3308. }
  3309. if(ID == 93)
  3310. subID = 1;
  3311. assert(storedArtifact->artType);
  3312. assert(storedArtifact->parents.size());
  3313. }
  3314. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  3315. {
  3316. if(!stacksCount())
  3317. {
  3318. InfoWindow iw;
  3319. iw.soundID = soundBase::treasure;
  3320. iw.player = h->tempOwner;
  3321. switch(ID)
  3322. {
  3323. case 5:
  3324. {
  3325. iw.components.push_back(Component(4,subID,0,0));
  3326. if(message.length())
  3327. iw.text << message;
  3328. else
  3329. iw.text << std::pair<ui8,ui32>(12,subID);
  3330. }
  3331. break;
  3332. case 93:
  3333. {
  3334. int spellID = storedArtifact->getBonus(Selector::type(Bonus::SPELL))->subtype;
  3335. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  3336. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  3337. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  3338. }
  3339. break;
  3340. }
  3341. cb->showInfoDialog(&iw);
  3342. pick(h);
  3343. }
  3344. else
  3345. {
  3346. if(message.size())
  3347. {
  3348. BlockingDialog ynd(true,false);
  3349. ynd.player = h->getOwner();
  3350. ynd.text << message;
  3351. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  3352. }
  3353. else
  3354. {
  3355. fightForArt(0,h);
  3356. }
  3357. }
  3358. }
  3359. void CGArtifact::pick(const CGHeroInstance * h) const
  3360. {
  3361. cb->giveHeroArtifact(h, storedArtifact, -2);
  3362. cb->removeObject(id);
  3363. }
  3364. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  3365. {
  3366. if(agreed)
  3367. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  3368. }
  3369. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3370. {
  3371. if(result->winner == 0) //attacker won
  3372. pick(h);
  3373. }
  3374. void CGPickable::initObj()
  3375. {
  3376. blockVisit = true;
  3377. switch(ID)
  3378. {
  3379. case 12: //campfire
  3380. val2 = (ran()%3) + 4; //4 - 6
  3381. val1 = val2 * 100;
  3382. type = ran()%6; //given resource
  3383. break;
  3384. case 29: //floatsam
  3385. switch(type = ran()%4)
  3386. {
  3387. case 0:
  3388. val1 = val2 = 0;
  3389. break;
  3390. case 1:
  3391. val1 = 5;
  3392. val2 = 0;
  3393. break;
  3394. case 2:
  3395. val1 = 5;
  3396. val2 = 200;
  3397. break;
  3398. case 3:
  3399. val1 = 10;
  3400. val2 = 500;
  3401. break;
  3402. }
  3403. break;
  3404. case 82: //sea chest
  3405. {
  3406. int hlp = ran()%100;
  3407. if(hlp < 20)
  3408. {
  3409. val1 = 0;
  3410. type = 0;
  3411. }
  3412. else if(hlp < 90)
  3413. {
  3414. val1 = 1500;
  3415. type = 2;
  3416. }
  3417. else
  3418. {
  3419. val1 = 1000;
  3420. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3421. type = 1;
  3422. }
  3423. }
  3424. break;
  3425. case 86: //Shipwreck Survivor
  3426. {
  3427. int hlp = ran()%100;
  3428. if(hlp < 55)
  3429. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3430. else if(hlp < 75)
  3431. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  3432. else if(hlp < 95)
  3433. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  3434. else
  3435. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  3436. }
  3437. break;
  3438. case 101: //treasure chest
  3439. {
  3440. int hlp = ran()%100;
  3441. if(hlp >= 95)
  3442. {
  3443. type = 1;
  3444. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3445. return;
  3446. }
  3447. else if (hlp >= 65)
  3448. {
  3449. val1 = 2000;
  3450. }
  3451. else if(hlp >= 33)
  3452. {
  3453. val1 = 1500;
  3454. }
  3455. else
  3456. {
  3457. val1 = 1000;
  3458. }
  3459. val2 = val1 - 500;
  3460. type = 0;
  3461. break;
  3462. }
  3463. }
  3464. }
  3465. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  3466. {
  3467. switch(ID)
  3468. {
  3469. case 12: //campfire
  3470. {
  3471. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  3472. cb->giveResource(h->tempOwner,6,val1);//gold
  3473. InfoWindow iw;
  3474. iw.soundID = soundBase::experience;
  3475. iw.player = h->tempOwner;
  3476. iw.components.push_back(Component(2,6,val1,0));
  3477. iw.components.push_back(Component(2,type,val2,0));
  3478. iw.text << std::pair<ui8,ui32>(11,23);
  3479. cb->showInfoDialog(&iw);
  3480. break;
  3481. }
  3482. case 29: //flotsam
  3483. {
  3484. cb->giveResource(h->tempOwner,0,val1); //wood
  3485. cb->giveResource(h->tempOwner,6,val2);//gold
  3486. InfoWindow iw;
  3487. iw.soundID = soundBase::GENIE;
  3488. iw.player = h->tempOwner;
  3489. if(val1)
  3490. iw.components.push_back(Component(2,0,val1,0));
  3491. if(val2)
  3492. iw.components.push_back(Component(2,6,val2,0));
  3493. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  3494. cb->showInfoDialog(&iw);
  3495. break;
  3496. }
  3497. case 82: //Sea Chest
  3498. {
  3499. InfoWindow iw;
  3500. iw.soundID = soundBase::chest;
  3501. iw.player = h->tempOwner;
  3502. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  3503. if(val1) //there is gold
  3504. {
  3505. iw.components.push_back(Component(2,6,val1,0));
  3506. cb->giveResource(h->tempOwner,6,val1);
  3507. }
  3508. if(type == 1) //art
  3509. {
  3510. //TODO: what if no space in backpack?
  3511. iw.components.push_back(Component(4, val2, 1, 0));
  3512. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  3513. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val2],-2);
  3514. }
  3515. cb->showInfoDialog(&iw);
  3516. break;
  3517. }
  3518. case 86: //Shipwreck Survivor
  3519. {
  3520. //TODO: what if no space in backpack?
  3521. InfoWindow iw;
  3522. iw.soundID = soundBase::experience;
  3523. iw.player = h->tempOwner;
  3524. iw.components.push_back(Component(4,val1,1,0));
  3525. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  3526. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3527. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3528. cb->showInfoDialog(&iw);
  3529. break;
  3530. }
  3531. case 101: //treasure chest
  3532. {
  3533. if (subID) //not OH3 treasure chest
  3534. {
  3535. tlog2 << "Not supported WoG treasure chest!\n";
  3536. return;
  3537. }
  3538. if(type) //there is an artifact
  3539. {
  3540. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3541. InfoWindow iw;
  3542. iw.soundID = soundBase::treasure;
  3543. iw.player = h->tempOwner;
  3544. iw.components.push_back(Component(4,val1,1,0));
  3545. iw.text << std::pair<ui8,ui32>(11,145);
  3546. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3547. cb->showInfoDialog(&iw);
  3548. break;
  3549. }
  3550. else
  3551. {
  3552. BlockingDialog sd(false,true);
  3553. sd.player = h->tempOwner;
  3554. sd.text << std::pair<ui8,ui32>(11,146);
  3555. sd.components.push_back(Component(2,6,val1,0));
  3556. int expVal = val2*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f;
  3557. sd.components.push_back(Component(5,0,expVal, 0));
  3558. sd.soundID = soundBase::chest;
  3559. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  3560. cb->showBlockingDialog(&sd,fun);
  3561. return;
  3562. }
  3563. }
  3564. }
  3565. cb->removeObject(id);
  3566. }
  3567. void CGPickable::chosen( int which, int heroID ) const
  3568. {
  3569. const CGHeroInstance *h = cb->getHero(heroID);
  3570. switch(which)
  3571. {
  3572. case 1: //player pick gold
  3573. cb->giveResource(cb->getOwner(heroID),6,val1);
  3574. break;
  3575. case 2: //player pick exp
  3576. cb->changePrimSkill(heroID, 4, val2*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f);
  3577. break;
  3578. default:
  3579. throw std::string("Unhandled treasure choice");
  3580. }
  3581. cb->removeObject(id);
  3582. }
  3583. bool CQuest::checkQuest (const CGHeroInstance * h) const
  3584. {
  3585. switch (missionType)
  3586. {
  3587. case MISSION_NONE:
  3588. return true;
  3589. break;
  3590. case MISSION_LEVEL:
  3591. if (m13489val <= h->level)
  3592. return true;
  3593. return false;
  3594. break;
  3595. case MISSION_PRIMARY_STAT:
  3596. for (int i = 0; i < 4; ++i)
  3597. {
  3598. if (h->getPrimSkillLevel(i) < m2stats[i])
  3599. return false;
  3600. }
  3601. return true;
  3602. break;
  3603. case MISSION_KILL_HERO:
  3604. if (h->cb->gameState()->map->heroesToBeat[m13489val]->tempOwner < PLAYER_LIMIT)
  3605. return false; //if the pointer is not NULL
  3606. return true;
  3607. break;
  3608. case MISSION_KILL_CREATURE:
  3609. if (h->cb->gameState()->map->monsters[m13489val]->pos == int3(-1,-1,-1))
  3610. return true;
  3611. return false;
  3612. break;
  3613. case MISSION_ART:
  3614. for (int i = 0; i < m5arts.size(); ++i)
  3615. {
  3616. if (h->hasArt(m5arts[i]))
  3617. continue;
  3618. return false; //if the artifact was not found
  3619. }
  3620. return true;
  3621. break;
  3622. case MISSION_ARMY:
  3623. {
  3624. std::vector<CStackBasicDescriptor>::const_iterator cre;
  3625. TSlots::const_iterator it;
  3626. ui32 count;
  3627. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  3628. {
  3629. for (count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  3630. {
  3631. if (it->second->type == cre->type)
  3632. count += it->second->count;
  3633. }
  3634. if (count < cre->count) //not enough creatures of this kind
  3635. return false;
  3636. }
  3637. }
  3638. return true;
  3639. break;
  3640. case MISSION_RESOURCES:
  3641. for (int i = 0; i < 7; ++i) //including Mithril ?
  3642. { //Quest has no direct access to callback
  3643. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  3644. return false;
  3645. }
  3646. return true;
  3647. break;
  3648. case MISSION_HERO:
  3649. if (m13489val == h->type->ID)
  3650. return true;
  3651. return false;
  3652. break;
  3653. case MISSION_PLAYER:
  3654. if (m13489val == h->getOwner())
  3655. return true;
  3656. return false;
  3657. break;
  3658. default:
  3659. return false;
  3660. }
  3661. }
  3662. void CGSeerHut::initObj()
  3663. {
  3664. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  3665. textOption = ran()%3;
  3666. progress = 0;
  3667. if (missionType)
  3668. {
  3669. if (!isCustom)
  3670. {
  3671. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3672. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3673. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3674. }
  3675. }
  3676. else
  3677. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3678. }
  3679. const std::string & CGSeerHut::getHoverText() const
  3680. {
  3681. switch (ID)
  3682. {
  3683. case 83:
  3684. hoverName = VLC->generaltexth->allTexts[347];
  3685. boost::algorithm::replace_first(hoverName,"%s", seerName);
  3686. break;
  3687. case 215:
  3688. hoverName = VLC->generaltexth->names[ID];
  3689. break;
  3690. default:
  3691. tlog5 << "unrecognized quest object\n";
  3692. }
  3693. if (progress & missionType) //rollover when the quest is active
  3694. {
  3695. MetaString ms;
  3696. ms << "\n\n" << VLC->generaltexth->quests[missionType-1][3][textOption];
  3697. std::string str;
  3698. switch (missionType)
  3699. {
  3700. case MISSION_LEVEL:
  3701. ms.addReplacement(m13489val);
  3702. break;
  3703. case MISSION_PRIMARY_STAT:
  3704. {
  3705. MetaString loot;
  3706. for (int i = 0; i < 4; ++i)
  3707. {
  3708. if (m2stats[i])
  3709. {
  3710. loot << "%d %s";
  3711. loot.addReplacement(m2stats[i]);
  3712. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3713. }
  3714. }
  3715. ms.addReplacement(loot.buildList());
  3716. }
  3717. break;
  3718. case MISSION_KILL_HERO:
  3719. ms.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3720. break;
  3721. case MISSION_HERO:
  3722. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3723. break;
  3724. case MISSION_KILL_CREATURE:
  3725. {
  3726. ms.addReplacement(cb->gameState()->map->monsters[m13489val]->getStack(0));
  3727. }
  3728. break;
  3729. case MISSION_ART:
  3730. {
  3731. MetaString loot;
  3732. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3733. {
  3734. loot << "%s";
  3735. loot.addReplacement(MetaString::ART_NAMES, *it);
  3736. }
  3737. ms.addReplacement(loot.buildList());
  3738. }
  3739. break;
  3740. case MISSION_ARMY:
  3741. {
  3742. MetaString loot;
  3743. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3744. {
  3745. loot << "%s";
  3746. loot.addReplacement(*it);
  3747. }
  3748. ms.addReplacement(loot.buildList());
  3749. }
  3750. break;
  3751. case MISSION_RESOURCES:
  3752. {
  3753. MetaString loot;
  3754. for (int i = 0; i < 7; ++i)
  3755. {
  3756. if (m7resources[i])
  3757. {
  3758. loot << "%d %s";
  3759. loot.addReplacement(m7resources[i]);
  3760. loot.addReplacement(MetaString::RES_NAMES, i);
  3761. }
  3762. }
  3763. ms.addReplacement(loot.buildList());
  3764. }
  3765. break;
  3766. case MISSION_PLAYER:
  3767. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  3768. break;
  3769. default:
  3770. break;
  3771. }
  3772. ms.toString(str);
  3773. hoverName += str;
  3774. }
  3775. return hoverName;
  3776. }
  3777. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  3778. {
  3779. switch (what)
  3780. {
  3781. case 10:
  3782. progress = val;
  3783. break;
  3784. case 11:
  3785. missionType = CQuest::MISSION_NONE;
  3786. break;
  3787. }
  3788. }
  3789. void CGSeerHut::newTurn() const
  3790. {
  3791. if (lastDay >= 0 && lastDay <= cb->getDate(0)) //time is up
  3792. {
  3793. cb->setObjProperty (id,11,0);
  3794. cb->setObjProperty (id,10,0);
  3795. }
  3796. }
  3797. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  3798. {
  3799. InfoWindow iw;
  3800. iw.player = h->getOwner();
  3801. if (missionType)
  3802. {
  3803. if (!progress) //propose quest
  3804. {
  3805. iw.text << firstVisitText;
  3806. switch (missionType)
  3807. {
  3808. case MISSION_LEVEL:
  3809. iw.components.push_back (Component (Component::EXPERIENCE, 1, m13489val, 0));
  3810. if (!isCustom)
  3811. iw.text.addReplacement(m13489val);
  3812. break;
  3813. case MISSION_PRIMARY_STAT:
  3814. {
  3815. MetaString loot;
  3816. for (int i = 0; i < 4; ++i)
  3817. {
  3818. if (m2stats[i])
  3819. {
  3820. iw.components.push_back (Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  3821. loot << "%d %s";
  3822. loot.addReplacement(m2stats[i]);
  3823. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3824. }
  3825. }
  3826. if (!isCustom)
  3827. iw.text.addReplacement(loot.buildList());
  3828. }
  3829. break;
  3830. case MISSION_KILL_HERO:
  3831. iw.components.push_back (Component (Component::HERO,
  3832. cb->gameState()->map->heroesToBeat[m13489val]->type->ID, 0, 0));
  3833. if (!isCustom)
  3834. iw.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3835. break;
  3836. case MISSION_HERO:
  3837. iw.components.push_back (Component (Component::HERO, m13489val, 0, 0));
  3838. if (!isCustom)
  3839. iw.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3840. break;
  3841. case MISSION_KILL_CREATURE:
  3842. {
  3843. CStackInstance stack = cb->gameState()->map->monsters[m13489val]->getStack(0);
  3844. iw.components.push_back (Component(stack));
  3845. if (!isCustom)
  3846. {
  3847. iw.text.addReplacement(stack);
  3848. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3849. {
  3850. iw.text.addReplacement(VLC->generaltexth->arraytxt[147+checkDirection()]);
  3851. }
  3852. }
  3853. }
  3854. break;
  3855. case MISSION_ART:
  3856. {
  3857. MetaString loot;
  3858. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3859. {
  3860. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3861. loot << "%s";
  3862. loot.addReplacement(MetaString::ART_NAMES, *it);
  3863. }
  3864. if (!isCustom)
  3865. iw.text.addReplacement (loot.buildList());
  3866. }
  3867. break;
  3868. case MISSION_ARMY:
  3869. {
  3870. MetaString loot;
  3871. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3872. {
  3873. iw.components.push_back(Component(*it));
  3874. loot << "%s";
  3875. loot.addReplacement(*it);
  3876. }
  3877. if (!isCustom)
  3878. iw.text.addReplacement (loot.buildList());
  3879. }
  3880. break;
  3881. case MISSION_RESOURCES:
  3882. {
  3883. MetaString loot;
  3884. for (int i = 0; i < 7; ++i)
  3885. {
  3886. if (m7resources[i])
  3887. {
  3888. iw.components.push_back (Component (Component::RESOURCE, i, m7resources[i], 0));
  3889. loot << "%d %s";
  3890. loot.addReplacement(m7resources[i]);
  3891. loot.addReplacement(MetaString::RES_NAMES, i);
  3892. }
  3893. }
  3894. if (!isCustom)
  3895. iw.text.addReplacement (loot.buildList());
  3896. }
  3897. break;
  3898. case MISSION_PLAYER:
  3899. iw.components.push_back (Component (Component::FLAG, m13489val, 0, 0));
  3900. if (!isCustom)
  3901. iw.text.addReplacement (VLC->generaltexth->colors[m13489val]);
  3902. break;
  3903. }
  3904. cb->setObjProperty (id,10,1);
  3905. cb->showInfoDialog(&iw);
  3906. }
  3907. else if (!checkQuest(h))
  3908. {
  3909. iw.text << nextVisitText;
  3910. cb->showInfoDialog(&iw);
  3911. }
  3912. if (checkQuest(h)) // propose completion, also on first visit
  3913. {
  3914. BlockingDialog bd (true, false);
  3915. bd.player = h->getOwner();
  3916. bd.soundID = soundBase::QUEST;
  3917. bd.text << completedText;
  3918. switch (missionType)
  3919. {
  3920. case CQuest::MISSION_LEVEL:
  3921. if (!isCustom)
  3922. bd.text.addReplacement(m13489val);
  3923. break;
  3924. case CQuest::MISSION_PRIMARY_STAT:
  3925. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  3926. {
  3927. MetaString loot;
  3928. for (int i = 0; i < 4; ++i)
  3929. {
  3930. if (m2stats[i])
  3931. {
  3932. loot << "%d %s";
  3933. loot.addReplacement(m2stats[i]);
  3934. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3935. }
  3936. }
  3937. if (!isCustom)
  3938. bd.text.addReplacement(loot.buildList());
  3939. }
  3940. break;
  3941. case CQuest::MISSION_ART:
  3942. {
  3943. MetaString loot;
  3944. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3945. {
  3946. loot << "%s";
  3947. loot.addReplacement(MetaString::ART_NAMES, *it);
  3948. }
  3949. if (!isCustom)
  3950. bd.text.addReplacement(loot.buildList());
  3951. }
  3952. break;
  3953. case CQuest::MISSION_ARMY:
  3954. {
  3955. MetaString loot;
  3956. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3957. {
  3958. loot << "%s";
  3959. loot.addReplacement(*it);
  3960. }
  3961. if (!isCustom)
  3962. bd.text.addReplacement(loot.buildList());
  3963. }
  3964. break;
  3965. case CQuest::MISSION_RESOURCES:
  3966. {
  3967. MetaString loot;
  3968. for (int i = 0; i < 7; ++i)
  3969. {
  3970. if (m7resources[i])
  3971. {
  3972. loot << "%d %s";
  3973. loot.addReplacement(m7resources[i]);
  3974. loot.addReplacement(MetaString::RES_NAMES, i);
  3975. }
  3976. }
  3977. if (!isCustom)
  3978. bd.text.addReplacement(loot.buildList());
  3979. }
  3980. break;
  3981. case MISSION_KILL_HERO:
  3982. if (!isCustom)
  3983. bd.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3984. break;
  3985. case MISSION_HERO:
  3986. if (!isCustom)
  3987. bd.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3988. break;
  3989. case MISSION_KILL_CREATURE:
  3990. {
  3991. {
  3992. bd.text.addReplacement(cb->gameState()->map->monsters[m13489val]->getArmy()[0]);
  3993. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3994. {
  3995. bd.text.addReplacement(VLC->generaltexth->arraytxt[147+checkDirection()]);
  3996. }
  3997. }
  3998. }
  3999. case MISSION_PLAYER:
  4000. if (!isCustom)
  4001. bd.text.addReplacement(VLC->generaltexth->colors[m13489val]);
  4002. break;
  4003. }
  4004. switch (rewardType)
  4005. {
  4006. case 1: bd.components.push_back (Component (Component::EXPERIENCE, 0, rVal*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f, 0));
  4007. break;
  4008. case 2: bd.components.push_back (Component (Component::PRIM_SKILL, 5, rVal, 0));
  4009. break;
  4010. case 3: bd.components.push_back (Component (Component::MORALE, 0, rVal, 0));
  4011. break;
  4012. case 4: bd.components.push_back (Component (Component::LUCK, 0, rVal, 0));
  4013. break;
  4014. case 5: bd.components.push_back (Component (Component::RESOURCE, rID, rVal, 0));
  4015. break;
  4016. case 6: bd.components.push_back (Component (Component::PRIM_SKILL, rID, rVal, 0));
  4017. break;
  4018. case 7: bd.components.push_back (Component (Component::SEC_SKILL, rID, rVal, 0));
  4019. break;
  4020. case 8: bd.components.push_back (Component (Component::ARTIFACT, rID, 0, 0));
  4021. break;
  4022. case 9: bd.components.push_back (Component (Component::SPELL, rID, 0, 0));
  4023. break;
  4024. case 10: bd.components.push_back (Component (Component::CREATURE, rID, rVal, 0));
  4025. break;
  4026. }
  4027. cb->showBlockingDialog (&bd, boost::bind (&CGSeerHut::finishQuest, this, h, _1));
  4028. return;
  4029. }
  4030. }
  4031. else
  4032. {
  4033. iw.text << VLC->generaltexth->seerEmpty[textOption];
  4034. if (ID == 83)
  4035. iw.text.addReplacement(seerName);
  4036. cb->showInfoDialog(&iw);
  4037. }
  4038. }
  4039. int CGSeerHut::checkDirection() const
  4040. {
  4041. int3 cord = cb->gameState()->map->monsters[m13489val]->pos;
  4042. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  4043. {
  4044. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  4045. return 8;
  4046. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  4047. return 1;
  4048. else //northeast
  4049. return 2;
  4050. }
  4051. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  4052. {
  4053. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  4054. return 7;
  4055. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  4056. return 9;
  4057. else //east
  4058. return 3;
  4059. }
  4060. else //south
  4061. {
  4062. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  4063. return 6;
  4064. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  4065. return 5;
  4066. else //southeast
  4067. return 4;
  4068. }
  4069. }
  4070. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  4071. {
  4072. if (accept)
  4073. {
  4074. switch (missionType)
  4075. {
  4076. case CQuest::MISSION_ART:
  4077. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  4078. {
  4079. cb->removeArtifact(VLC->arth->artifacts[*it], h->id);
  4080. }
  4081. break;
  4082. case CQuest::MISSION_ARMY:
  4083. cb->takeCreatures(h->id, m6creatures);
  4084. break;
  4085. case CQuest::MISSION_RESOURCES:
  4086. for (int i = 0; i < 7; ++i)
  4087. {
  4088. cb->giveResource(h->getOwner(), i, -m7resources[i]);
  4089. }
  4090. break;
  4091. default:
  4092. break;
  4093. }
  4094. cb->setObjProperty(id,11,0); //no more mission avaliable
  4095. completeQuest(h); //make sure to remove QuestQuard at the very end
  4096. }
  4097. }
  4098. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  4099. {
  4100. switch (rewardType)
  4101. {
  4102. case 1: //experience
  4103. {
  4104. int expVal = rVal*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f;
  4105. cb->changePrimSkill(h->id, 4, expVal, false);
  4106. break;
  4107. }
  4108. case 2: //mana points
  4109. {
  4110. cb->setManaPoints(h->id, h->mana+rVal);
  4111. break;
  4112. }
  4113. case 3: case 4: //morale /luck
  4114. {
  4115. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  4116. Bonus::OBJECT, rVal, h->id, "", -1);
  4117. GiveBonus gb;
  4118. gb.id = h->id;
  4119. gb.bonus = hb;
  4120. cb->giveHeroBonus(&gb);
  4121. }
  4122. break;
  4123. case 5: //resources
  4124. cb->giveResource(h->getOwner(), rID, rVal);
  4125. break;
  4126. case 6: //main ability bonus (attak, defence etd.)
  4127. cb->changePrimSkill(h->id, rID, rVal, false);
  4128. break;
  4129. case 7: // secondary ability gain
  4130. cb->changeSecSkill(h->id, rID, rVal, false);
  4131. break;
  4132. case 8: // artifact
  4133. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],-2);
  4134. break;
  4135. case 9:// spell
  4136. {
  4137. std::set<ui32> spell;
  4138. spell.insert (rID);
  4139. cb->changeSpells(h->id, true, spell);
  4140. }
  4141. break;
  4142. case 10:// creature
  4143. {
  4144. CCreatureSet creatures;
  4145. creatures.setCreature(0, rID, rVal);
  4146. cb->giveCreatures(this, h, creatures, false);
  4147. }
  4148. break;
  4149. default:
  4150. break;
  4151. }
  4152. }
  4153. void CGQuestGuard::initObj()
  4154. {
  4155. blockVisit = true;
  4156. progress = 0;
  4157. textOption = ran()%3 + 3; //3-5
  4158. if (missionType && !isCustom)
  4159. {
  4160. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  4161. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  4162. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  4163. }
  4164. else
  4165. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  4166. }
  4167. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  4168. {
  4169. cb->removeObject(id);
  4170. }
  4171. void CGWitchHut::initObj()
  4172. {
  4173. ability = allowedAbilities[ran()%allowedAbilities.size()];
  4174. }
  4175. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  4176. {
  4177. InfoWindow iw;
  4178. iw.soundID = soundBase::gazebo;
  4179. iw.player = h->getOwner();
  4180. if(!hasVisited(h->tempOwner))
  4181. cb->setObjProperty(id,10,h->tempOwner);
  4182. if(h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //you alredy know this skill
  4183. {
  4184. iw.text << std::pair<ui8,ui32>(11,172);
  4185. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4186. }
  4187. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  4188. {
  4189. iw.text << std::pair<ui8,ui32>(11,173);
  4190. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4191. }
  4192. else //give sec skill
  4193. {
  4194. iw.components.push_back(Component(1, ability, 1, 0));
  4195. iw.text << std::pair<ui8,ui32>(11,171);
  4196. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4197. cb->changeSecSkill(h->id,ability,1,true);
  4198. }
  4199. cb->showInfoDialog(&iw);
  4200. }
  4201. const std::string & CGWitchHut::getHoverText() const
  4202. {
  4203. hoverName = VLC->generaltexth->names[ID];
  4204. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4205. {
  4206. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  4207. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  4208. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4209. if(h && h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //hero knows that ability
  4210. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  4211. }
  4212. return hoverName;
  4213. }
  4214. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  4215. {
  4216. bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  4217. int messageID;
  4218. int bonusMove = 0, sound = -1;
  4219. InfoWindow iw;
  4220. iw.player = h->tempOwner;
  4221. GiveBonus gbonus;
  4222. gbonus.id = h->id;
  4223. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4224. gbonus.bonus.source = Bonus::OBJECT;
  4225. gbonus.bonus.id = ID;
  4226. bool second = false;
  4227. Bonus secondBonus;
  4228. switch(ID)
  4229. {
  4230. case 11: //buoy
  4231. messageID = 21;
  4232. sound = soundBase::MORALE;
  4233. gbonus.bonus.type = Bonus::MORALE;
  4234. gbonus.bonus.val = +1;
  4235. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  4236. break;
  4237. case 14: //swan pond
  4238. messageID = 29;
  4239. sound = soundBase::LUCK;
  4240. gbonus.bonus.type = Bonus::LUCK;
  4241. gbonus.bonus.val = 2;
  4242. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  4243. bonusMove = -h->movement;
  4244. break;
  4245. case 28: //Faerie Ring
  4246. messageID = 49;
  4247. sound = soundBase::LUCK;
  4248. gbonus.bonus.type = Bonus::LUCK;
  4249. gbonus.bonus.val = 1;
  4250. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  4251. break;
  4252. case 30: //fountain of fortune
  4253. messageID = 55;
  4254. sound = soundBase::LUCK;
  4255. gbonus.bonus.type = Bonus::LUCK;
  4256. gbonus.bonus.val = rand()%5 - 1;
  4257. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  4258. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  4259. break;
  4260. case 38: //idol of fortune
  4261. messageID = 62;
  4262. sound = soundBase::experience;
  4263. gbonus.bonus.val = 1;
  4264. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  4265. if(cb->getDate(1) == 7) //7th day of week
  4266. {
  4267. gbonus.bonus.type = Bonus::MORALE;
  4268. second = true;
  4269. secondBonus = gbonus.bonus;
  4270. secondBonus.type = Bonus::LUCK;
  4271. }
  4272. else
  4273. {
  4274. gbonus.bonus.type = (cb->getDate(1)%2) ? Bonus::LUCK : Bonus::MORALE;
  4275. }
  4276. break;
  4277. case 52: //Mermaid
  4278. messageID = 83;
  4279. sound = soundBase::LUCK;
  4280. gbonus.bonus.type = Bonus::LUCK;
  4281. gbonus.bonus.val = 1;
  4282. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  4283. break;
  4284. case 64: //Rally Flag
  4285. sound = soundBase::MORALE;
  4286. messageID = 111;
  4287. gbonus.bonus.type = Bonus::MORALE;
  4288. gbonus.bonus.val = 1;
  4289. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  4290. second = true;
  4291. secondBonus = gbonus.bonus;
  4292. secondBonus.type = Bonus::LUCK;
  4293. bonusMove = 400;
  4294. break;
  4295. case 56: //oasis
  4296. messageID = 95;
  4297. gbonus.bonus.type = Bonus::MORALE;
  4298. gbonus.bonus.val = 1;
  4299. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  4300. bonusMove = 800;
  4301. break;
  4302. case 96: //temple
  4303. messageID = 140;
  4304. iw.soundID = soundBase::temple;
  4305. gbonus.bonus.type = Bonus::MORALE;
  4306. if(cb->getDate(1)==7) //sunday
  4307. {
  4308. gbonus.bonus.val = 2;
  4309. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  4310. }
  4311. else
  4312. {
  4313. gbonus.bonus.val = 1;
  4314. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  4315. }
  4316. break;
  4317. case 110://Watering Hole
  4318. sound = soundBase::MORALE;
  4319. messageID = 166;
  4320. gbonus.bonus.type = Bonus::MORALE;
  4321. gbonus.bonus.val = 1;
  4322. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  4323. bonusMove = 400;
  4324. break;
  4325. case 31: //Fountain of Youth
  4326. sound = soundBase::MORALE;
  4327. messageID = 57;
  4328. gbonus.bonus.type = Bonus::MORALE;
  4329. gbonus.bonus.val = 1;
  4330. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  4331. bonusMove = 400;
  4332. break;
  4333. case 94: //Stables
  4334. sound = soundBase::horse20;
  4335. CCreatureSet creatures;
  4336. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); ++i)
  4337. {
  4338. if(i->second->type->idNumber == 10)
  4339. creatures.slots.insert(*i);
  4340. }
  4341. if (creatures.slots.size())
  4342. {
  4343. messageID = 138;
  4344. iw.components.push_back(Component(Component::CREATURE,11,0,1));
  4345. for (TSlots::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); ++i)
  4346. {
  4347. cb->changeStackType(StackLocation(h, i->first), VLC->creh->creatures[11]);
  4348. }
  4349. }
  4350. else
  4351. messageID = 137;
  4352. gbonus.bonus.type = Bonus::LAND_MOVEMENT;
  4353. gbonus.bonus.val = 600;
  4354. bonusMove = 600;
  4355. gbonus.bonus.duration = Bonus::ONE_WEEK;
  4356. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  4357. break;
  4358. }
  4359. if(visited)
  4360. {
  4361. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  4362. messageID--;
  4363. else
  4364. messageID++;
  4365. }
  4366. else
  4367. {
  4368. //TODO: fix if second bonus val != main bonus val
  4369. if(gbonus.bonus.type == Bonus::MORALE || secondBonus.type == Bonus::MORALE)
  4370. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  4371. if(gbonus.bonus.type == Bonus::LUCK || secondBonus.type == Bonus::LUCK)
  4372. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  4373. cb->giveHeroBonus(&gbonus);
  4374. if(second)
  4375. {
  4376. gbonus.bonus = secondBonus;
  4377. cb->giveHeroBonus(&gbonus);
  4378. }
  4379. if(bonusMove) //swan pond - take all move points, stables - give move point this day
  4380. {
  4381. SetMovePoints smp;
  4382. smp.hid = h->id;
  4383. smp.val = h->movement + bonusMove;
  4384. cb->setMovePoints(&smp);
  4385. }
  4386. }
  4387. iw.soundID = sound;
  4388. iw.text << std::pair<ui8,ui32>(11,messageID);
  4389. cb->showInfoDialog(&iw);
  4390. }
  4391. const std::string & CGBonusingObject::getHoverText() const
  4392. {
  4393. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4394. hoverName = VLC->generaltexth->names[ID];
  4395. if(h)
  4396. {
  4397. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  4398. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4399. else
  4400. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4401. }
  4402. return hoverName;
  4403. }
  4404. void CGBonusingObject::initObj()
  4405. {
  4406. if(ID == 11 || ID == 52) //Buoy / Mermaid
  4407. {
  4408. blockVisit = true;
  4409. }
  4410. }
  4411. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  4412. {
  4413. int messageID;
  4414. InfoWindow iw;
  4415. iw.player = h->tempOwner;
  4416. iw.soundID = soundBase::GENIE;
  4417. if (!visited)
  4418. {
  4419. if (h->mana > h->manaLimit())
  4420. messageID = 76;
  4421. else
  4422. {
  4423. messageID = 74;
  4424. cb->setManaPoints (h->id, 2 * h->manaLimit());
  4425. cb->setObjProperty (id, ObjProperty::VISITED, true);
  4426. }
  4427. }
  4428. else
  4429. messageID = 75;
  4430. iw.text << std::pair<ui8,ui32>(11,messageID);
  4431. cb->showInfoDialog(&iw);
  4432. }
  4433. const std::string & CGMagicSpring::getHoverText() const
  4434. {
  4435. hoverName = VLC->generaltexth->names[ID];
  4436. if(!visited)
  4437. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4438. else
  4439. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4440. return hoverName;
  4441. }
  4442. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  4443. {
  4444. int message;
  4445. InfoWindow iw;
  4446. iw.soundID = soundBase::faerie;
  4447. iw.player = h->tempOwner;
  4448. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  4449. {
  4450. message = 78;
  4451. }
  4452. else if(h->mana < h->manaLimit())
  4453. {
  4454. giveDummyBonus(h->id);
  4455. cb->setManaPoints(h->id,h->manaLimit());
  4456. message = 77;
  4457. }
  4458. else
  4459. {
  4460. message = 79;
  4461. }
  4462. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  4463. cb->showInfoDialog(&iw);
  4464. }
  4465. const std::string & CGMagicWell::getHoverText() const
  4466. {
  4467. getNameVis(hoverName);
  4468. return hoverName;
  4469. }
  4470. void CGPandoraBox::initObj()
  4471. {
  4472. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  4473. }
  4474. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  4475. {
  4476. BlockingDialog bd (true, false);
  4477. bd.player = h->getOwner();
  4478. bd.soundID = soundBase::QUEST;
  4479. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  4480. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  4481. }
  4482. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  4483. {
  4484. if (accept)
  4485. {
  4486. if (stacksCount() > 0) //if pandora's box is protected by army
  4487. {
  4488. InfoWindow iw;
  4489. iw.player = h->tempOwner;
  4490. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4491. cb->showInfoDialog(&iw);
  4492. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  4493. }
  4494. else if (message.size() == 0 && resources.size() == 0
  4495. && primskills.size() == 0 && abilities.size() == 0
  4496. && abilityLevels.size() == 0 && artifacts.size() == 0
  4497. && spells.size() == 0 && creatures.Slots().size() > 0
  4498. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  4499. {
  4500. InfoWindow iw;
  4501. iw.player = h->tempOwner;
  4502. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  4503. cb->showInfoDialog(&iw);
  4504. }
  4505. else //if it gives something without battle
  4506. {
  4507. giveContents (h, false);
  4508. }
  4509. }
  4510. }
  4511. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  4512. {
  4513. if(result->winner)
  4514. return;
  4515. giveContents(h,true);
  4516. }
  4517. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  4518. {
  4519. InfoWindow iw;
  4520. iw.player = h->getOwner();
  4521. bool changesPrimSkill = false;
  4522. for (int i = 0; i < primskills.size(); i++)
  4523. {
  4524. if(primskills[i])
  4525. {
  4526. changesPrimSkill = true;
  4527. break;
  4528. }
  4529. }
  4530. if(gainedExp || changesPrimSkill || abilities.size())
  4531. {
  4532. expType expVal = gainedExp*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f;
  4533. getText(iw,afterBattle,175,h);
  4534. if(expVal)
  4535. iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
  4536. for(int i=0; i<primskills.size(); i++)
  4537. if(primskills[i])
  4538. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  4539. for(int i=0; i<abilities.size(); i++)
  4540. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  4541. cb->showInfoDialog(&iw);
  4542. //give exp
  4543. if(expVal)
  4544. cb->changePrimSkill(h->id,4,expVal,false);
  4545. //give prim skills
  4546. for(int i=0; i<primskills.size(); i++)
  4547. if(primskills[i])
  4548. cb->changePrimSkill(h->id,i,primskills[i],false);
  4549. //give sec skills
  4550. for(int i=0; i<abilities.size(); i++)
  4551. {
  4552. int curLev = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(abilities[i]));
  4553. if( (curLev && curLev < abilityLevels[i])
  4554. || (h->secSkills.size() < SKILL_PER_HERO) )
  4555. {
  4556. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  4557. }
  4558. }
  4559. }
  4560. if(spells.size())
  4561. {
  4562. std::set<ui32> spellsToGive;
  4563. iw.components.clear();
  4564. std::vector<ConstTransitivePtr<CSpell> > * sp = &VLC->spellh->spells;
  4565. for(std::vector<si32>::const_iterator i=spells.begin(); i != spells.end(); i++)
  4566. {
  4567. if ((*sp)[*i]->level <= h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2) //enough wisdom
  4568. {
  4569. iw.components.push_back(Component(Component::SPELL,*i,0,0));
  4570. spellsToGive.insert(*i);
  4571. }
  4572. }
  4573. if(spellsToGive.size())
  4574. {
  4575. cb->changeSpells(h->id,true,spellsToGive);
  4576. cb->showInfoDialog(&iw);
  4577. }
  4578. }
  4579. if(manaDiff)
  4580. {
  4581. getText(iw,afterBattle,manaDiff,176,177,h);
  4582. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  4583. cb->showInfoDialog(&iw);
  4584. cb->setManaPoints(h->id, h->mana + manaDiff);
  4585. }
  4586. if(moraleDiff)
  4587. {
  4588. getText(iw,afterBattle,moraleDiff,178,179,h);
  4589. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  4590. cb->showInfoDialog(&iw);
  4591. GiveBonus gb;
  4592. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id,"");
  4593. gb.id = h->id;
  4594. cb->giveHeroBonus(&gb);
  4595. }
  4596. if(luckDiff)
  4597. {
  4598. getText(iw,afterBattle,luckDiff,180,181,h);
  4599. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  4600. cb->showInfoDialog(&iw);
  4601. GiveBonus gb;
  4602. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id,"");
  4603. gb.id = h->id;
  4604. cb->giveHeroBonus(&gb);
  4605. }
  4606. iw.components.clear();
  4607. iw.text.clear();
  4608. for(int i=0; i<resources.size(); i++)
  4609. {
  4610. if(resources[i] < 0)
  4611. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4612. }
  4613. if(iw.components.size())
  4614. {
  4615. getText(iw,afterBattle,182,h);
  4616. cb->showInfoDialog(&iw);
  4617. }
  4618. iw.components.clear();
  4619. iw.text.clear();
  4620. for(int i=0; i<resources.size(); i++)
  4621. {
  4622. if(resources[i] > 0)
  4623. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4624. }
  4625. if(iw.components.size())
  4626. {
  4627. getText(iw,afterBattle,183,h);
  4628. cb->showInfoDialog(&iw);
  4629. }
  4630. iw.components.clear();
  4631. // getText(iw,afterBattle,183,h);
  4632. for(int i=0; i<artifacts.size(); i++)
  4633. {
  4634. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  4635. if(iw.components.size() >= 14)
  4636. {
  4637. cb->showInfoDialog(&iw);
  4638. iw.components.clear();
  4639. }
  4640. }
  4641. if(iw.components.size())
  4642. {
  4643. cb->showInfoDialog(&iw);
  4644. }
  4645. for(int i=0; i<resources.size(); i++)
  4646. if(resources[i])
  4647. cb->giveResource(h->getOwner(),i,resources[i]);
  4648. for(int i=0; i<artifacts.size(); i++)
  4649. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[artifacts[i]],-2);
  4650. iw.components.clear();
  4651. iw.text.clear();
  4652. if (creatures.Slots().size())
  4653. { //this part is taken straight from creature bank
  4654. MetaString loot;
  4655. for(TSlots::const_iterator i = creatures.Slots().begin(); i != creatures.Slots().end(); i++)
  4656. { //build list of joined creatures
  4657. iw.components.push_back(Component(*i->second));
  4658. loot << "%s";
  4659. loot.addReplacement(*i->second);
  4660. }
  4661. if (creatures.Slots().size() == 1 && creatures.Slots().begin()->second->count == 1)
  4662. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  4663. else
  4664. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  4665. iw.text.addReplacement(loot.buildList());
  4666. iw.text.addReplacement(h->name);
  4667. cb->showInfoDialog(&iw);
  4668. cb->giveCreatures(this, h, creatures, true);
  4669. //boost::bind(&CGPandoraBox::endBattle, this, h, _1)
  4670. }
  4671. if(!afterBattle && message.size())
  4672. {
  4673. iw.text << message;
  4674. cb->showInfoDialog(&iw);
  4675. }
  4676. if (!creatures.Slots().size())
  4677. cb->removeObject(id); //only when we don't need to display garrison window
  4678. }
  4679. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  4680. {
  4681. if(afterBattle || !message.size())
  4682. {
  4683. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  4684. iw.text.addReplacement(h->name);
  4685. }
  4686. else
  4687. {
  4688. iw.text << message;
  4689. afterBattle = true;
  4690. }
  4691. }
  4692. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  4693. {
  4694. iw.components.clear();
  4695. iw.text.clear();
  4696. if(afterBattle || !message.size())
  4697. {
  4698. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  4699. iw.text.addReplacement(h->name);
  4700. }
  4701. else
  4702. {
  4703. iw.text << message;
  4704. afterBattle = true;
  4705. }
  4706. }
  4707. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  4708. {
  4709. if(!(availableFor & (1 << h->tempOwner)))
  4710. return;
  4711. if(cb->getPlayerSettings(h->tempOwner)->human)
  4712. {
  4713. if(humanActivate)
  4714. activated(h);
  4715. }
  4716. else if(computerActivate)
  4717. activated(h);
  4718. }
  4719. void CGEvent::activated( const CGHeroInstance * h ) const
  4720. {
  4721. if(stacksCount() > 0)
  4722. {
  4723. InfoWindow iw;
  4724. iw.player = h->tempOwner;
  4725. if(message.size())
  4726. iw.text << message;
  4727. else
  4728. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4729. cb->showInfoDialog(&iw);
  4730. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  4731. }
  4732. else
  4733. {
  4734. giveContents(h,false);
  4735. }
  4736. }
  4737. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  4738. {
  4739. InfoWindow iw;
  4740. iw.player = h->tempOwner;
  4741. switch (ID)
  4742. {
  4743. case 58://redwood observatory
  4744. case 60://pillar of fire
  4745. {
  4746. iw.soundID = soundBase::LIGHTHOUSE;
  4747. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  4748. FoWChange fw;
  4749. fw.player = h->tempOwner;
  4750. fw.mode = 1;
  4751. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  4752. cb->sendAndApply (&fw);
  4753. break;
  4754. }
  4755. case 15://cover of darkness
  4756. {
  4757. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  4758. hideTiles(h->tempOwner, 20);
  4759. break;
  4760. }
  4761. }
  4762. cb->showInfoDialog(&iw);
  4763. }
  4764. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  4765. {
  4766. if(spell == 255)
  4767. {
  4768. tlog1 << "Not initialized shrine visited!\n";
  4769. return;
  4770. }
  4771. if(!hasVisited(h->tempOwner))
  4772. cb->setObjProperty(id,10,h->tempOwner);
  4773. InfoWindow iw;
  4774. iw.soundID = soundBase::temple;
  4775. iw.player = h->getOwner();
  4776. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  4777. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  4778. iw.text << ".";
  4779. if(!h->getArt(17)) //no spellbook
  4780. {
  4781. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  4782. }
  4783. else if(ID == 90 && !h->getSecSkillLevel(CGHeroInstance::WISDOM)) //it's third level spell and hero doesn't have wisdom
  4784. {
  4785. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  4786. }
  4787. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  4788. {
  4789. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  4790. }
  4791. else //give spell
  4792. {
  4793. std::set<ui32> spells;
  4794. spells.insert(spell);
  4795. cb->changeSpells(h->id, true, spells);
  4796. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  4797. }
  4798. cb->showInfoDialog(&iw);
  4799. }
  4800. void CGShrine::initObj()
  4801. {
  4802. if(spell == 255) //spell not set
  4803. {
  4804. int level = ID-87;
  4805. std::vector<ui16> possibilities;
  4806. cb->getAllowedSpells (possibilities, level);
  4807. if(!possibilities.size())
  4808. {
  4809. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  4810. return;
  4811. }
  4812. spell = possibilities[ran() % possibilities.size()];
  4813. }
  4814. }
  4815. const std::string & CGShrine::getHoverText() const
  4816. {
  4817. hoverName = VLC->generaltexth->names[ID];
  4818. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4819. {
  4820. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  4821. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell]->name);
  4822. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4823. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  4824. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  4825. }
  4826. return hoverName;
  4827. }
  4828. void CGSignBottle::initObj()
  4829. {
  4830. //if no text is set than we pick random from the predefined ones
  4831. if(!message.size())
  4832. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  4833. if(ID == 59)
  4834. {
  4835. blockVisit = true;
  4836. }
  4837. }
  4838. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  4839. {
  4840. InfoWindow iw;
  4841. iw.soundID = soundBase::STORE;
  4842. iw.player = h->getOwner();
  4843. iw.text << message;
  4844. cb->showInfoDialog(&iw);
  4845. if(ID == 59)
  4846. cb->removeObject(id);
  4847. }
  4848. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  4849. {
  4850. }
  4851. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  4852. {
  4853. int type = bonusType;
  4854. int bid = bonusID;
  4855. //check if the bonus if applicable, if not - give primary skill (always possible)
  4856. int ssl = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(bid)); //current sec skill level, used if bonusType == 1
  4857. if((type == 1
  4858. && ((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)
  4859. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)
  4860. || (VLC->spellh->spells[bid]->level > h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2)
  4861. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  4862. {
  4863. type = 0;
  4864. bid = ran() % PRIMARY_SKILLS;
  4865. }
  4866. InfoWindow iw;
  4867. iw.soundID = soundBase::gazebo;
  4868. iw.player = h->getOwner();
  4869. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  4870. switch (type)
  4871. {
  4872. case 0:
  4873. cb->changePrimSkill(h->id,bid,+1);
  4874. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  4875. break;
  4876. case 1:
  4877. cb->changeSecSkill(h->id,bid,+1);
  4878. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  4879. break;
  4880. case 2:
  4881. {
  4882. std::set<ui32> hlp;
  4883. hlp.insert(bid);
  4884. cb->changeSpells(h->id,true,hlp);
  4885. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  4886. }
  4887. break;
  4888. default:
  4889. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  4890. return;
  4891. }
  4892. cb->showInfoDialog(&iw);
  4893. cb->removeObject(id);
  4894. }
  4895. void CGScholar::initObj()
  4896. {
  4897. blockVisit = true;
  4898. if(bonusType == 255)
  4899. {
  4900. bonusType = ran()%3;
  4901. switch(bonusType)
  4902. {
  4903. case 0:
  4904. bonusID = ran() % PRIMARY_SKILLS;
  4905. break;
  4906. case 1:
  4907. bonusID = ran() % SKILL_QUANTITY;
  4908. break;
  4909. case 2:
  4910. std::vector<ui16> possibilities;
  4911. for (int i = 1; i < 6; ++i)
  4912. cb->getAllowedSpells (possibilities, i);
  4913. bonusID = possibilities[ran() % possibilities.size()];
  4914. break;
  4915. }
  4916. }
  4917. }
  4918. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  4919. {
  4920. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  4921. if (!ally && stacksCount() > 0) {
  4922. //TODO: Find a way to apply magic garrison effects in battle.
  4923. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  4924. return;
  4925. }
  4926. //New owner.
  4927. if (!ally)
  4928. cb->setOwner(id, h->tempOwner);
  4929. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  4930. }
  4931. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  4932. {
  4933. if (result->winner == 0)
  4934. onHeroVisit(h);
  4935. }
  4936. ui8 CGGarrison::getPassableness() const
  4937. {
  4938. if ( !stacksCount() )//empty - anyone can visit
  4939. return ALL_PLAYERS;
  4940. if ( tempOwner == 255 )//neutral guarded - noone can visit
  4941. return 0;
  4942. ui8 mask = 0;
  4943. TeamState * ts = cb->gameState()->getPlayerTeam(tempOwner);
  4944. BOOST_FOREACH(ui8 it, ts->players)
  4945. mask |= 1<<it;//allies - add to possible visitors
  4946. return mask;
  4947. }
  4948. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  4949. {
  4950. int sound = 0;
  4951. int txtid = -1;
  4952. switch(ID)
  4953. {
  4954. case 22: //Corpse
  4955. txtid = 37;
  4956. sound = soundBase::MYSTERY;
  4957. break;
  4958. case 39: //Lean To
  4959. sound = soundBase::GENIE;
  4960. txtid = 64;
  4961. break;
  4962. case 105://Wagon
  4963. sound = soundBase::GENIE;
  4964. txtid = 154;
  4965. break;
  4966. case 108:
  4967. break;
  4968. default:
  4969. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  4970. return;
  4971. }
  4972. if(ID == 108)//Warrior's Tomb
  4973. {
  4974. //ask if player wants to search the Tomb
  4975. BlockingDialog bd(true, false);
  4976. sound = soundBase::GRAVEYARD;
  4977. bd.player = h->getOwner();
  4978. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  4979. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  4980. return;
  4981. }
  4982. InfoWindow iw;
  4983. iw.soundID = sound;
  4984. iw.player = h->getOwner();
  4985. if(players.size()) //we have been already visited...
  4986. {
  4987. txtid++;
  4988. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  4989. txtid++;
  4990. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4991. }
  4992. else //first visit - give bonus!
  4993. {
  4994. switch(artOrRes)
  4995. {
  4996. case 0: // first visit but empty
  4997. if (ID == 22) //Corpse
  4998. ++txtid;
  4999. else
  5000. txtid+=2;
  5001. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5002. break;
  5003. case 1: //art
  5004. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  5005. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5006. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5007. if (ID == 22) //Corpse
  5008. {
  5009. iw.text << "%s";
  5010. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5011. }
  5012. break;
  5013. case 2: //res
  5014. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5015. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  5016. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  5017. break;
  5018. }
  5019. if(ID == 105 && artOrRes == 1)
  5020. {
  5021. iw.text.localStrings.back().second++;
  5022. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5023. }
  5024. }
  5025. cb->showInfoDialog(&iw);
  5026. cb->setObjProperty(id,10,h->getOwner());
  5027. }
  5028. const std::string & CGOnceVisitable::getHoverText() const
  5029. {
  5030. hoverName = VLC->generaltexth->names[ID] + " ";
  5031. hoverName += (hasVisited(cb->getCurrentPlayer())
  5032. ? (VLC->generaltexth->allTexts[352]) //visited
  5033. : ( VLC->generaltexth->allTexts[353])); //not visited
  5034. return hoverName;
  5035. }
  5036. void CGOnceVisitable::initObj()
  5037. {
  5038. switch(ID)
  5039. {
  5040. case 22: //Corpse
  5041. {
  5042. blockVisit = true;
  5043. int hlp = ran()%100;
  5044. if(hlp < 20)
  5045. {
  5046. artOrRes = 1;
  5047. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  5048. }
  5049. else
  5050. {
  5051. artOrRes = 0;
  5052. }
  5053. }
  5054. break;
  5055. case 39: //Lean To
  5056. {
  5057. artOrRes = 2;
  5058. bonusType = ran()%6; //any basic resource without gold
  5059. bonusVal = ran()%4 + 1;
  5060. }
  5061. break;
  5062. case 108://Warrior's Tomb
  5063. {
  5064. artOrRes = 1;
  5065. int hlp = ran()%100;
  5066. if(hlp < 30)
  5067. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  5068. else if(hlp < 80)
  5069. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  5070. else if(hlp < 95)
  5071. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  5072. else
  5073. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  5074. }
  5075. break;
  5076. case 105://Wagon
  5077. {
  5078. int hlp = ran()%100;
  5079. if(hlp < 10)
  5080. {
  5081. artOrRes = 0; // nothing... :(
  5082. }
  5083. else if(hlp < 50) //minor or treasure art
  5084. {
  5085. artOrRes = 1;
  5086. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  5087. }
  5088. else //2 - 5 of non-gold resource
  5089. {
  5090. artOrRes = 2;
  5091. bonusType = ran()%6;
  5092. bonusVal = ran()%4 + 2;
  5093. }
  5094. }
  5095. break;
  5096. }
  5097. }
  5098. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  5099. {
  5100. if(accept)
  5101. {
  5102. InfoWindow iw;
  5103. iw.player = h->getOwner();
  5104. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  5105. if(players.size()) //we've been already visited, player found nothing
  5106. {
  5107. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  5108. }
  5109. else //first visit - give artifact
  5110. {
  5111. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  5112. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  5113. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5114. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5115. }
  5116. if(!h->hasBonusFrom(Bonus::OBJECT,ID)) //we don't have modifier from this object yet
  5117. {
  5118. //ruin morale
  5119. GiveBonus gb;
  5120. gb.id = h->id;
  5121. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,-3,id,"");
  5122. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  5123. cb->giveHeroBonus(&gb);
  5124. }
  5125. cb->showInfoDialog(&iw);
  5126. cb->setObjProperty(id,10,h->getOwner());
  5127. }
  5128. }
  5129. void CBank::initObj()
  5130. {
  5131. switch (ID) //find apriopriate key
  5132. {
  5133. case 16: //bank
  5134. index = subID; break;
  5135. case 24: //derelict ship
  5136. index = 8; break;
  5137. case 25: //utopia
  5138. index = 10; break;
  5139. case 84: //crypt
  5140. index = 9; break;
  5141. case 85: //shipwreck
  5142. index = 7; break;
  5143. }
  5144. bc = NULL;
  5145. daycounter = 0;
  5146. multiplier = 1;
  5147. }
  5148. const std::string & CBank::getHoverText() const
  5149. {
  5150. hoverName = VLC->objh->creBanksNames[index];
  5151. if (bc == NULL)
  5152. hoverName += " " + VLC->generaltexth->allTexts[352];
  5153. else
  5154. hoverName += " " + VLC->generaltexth->allTexts[353];
  5155. return hoverName;
  5156. }
  5157. void CBank::reset(ui16 var1) //prevents desync
  5158. {
  5159. ui8 chance = 0;
  5160. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  5161. {
  5162. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  5163. {
  5164. bc = VLC->objh->banksInfo[index][i];
  5165. break;
  5166. }
  5167. }
  5168. artifacts.clear();
  5169. }
  5170. void CBank::initialize() const
  5171. {
  5172. cb->setObjProperty (id, 14, ran()); //synchronous reset
  5173. for (ui8 i = 0; i <= 3; i++)
  5174. {
  5175. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  5176. {
  5177. cb->setObjProperty (id, 18 + i, ran()); //synchronic artifacts
  5178. }
  5179. }
  5180. cb->setObjProperty (id, 17, ran()); //get army
  5181. }
  5182. void CBank::setPropertyDer (ui8 what, ui32 val)
  5183. /// random values are passed as arguments and processed identically on all clients
  5184. {
  5185. switch (what)
  5186. {
  5187. case 11: //daycounter
  5188. if (val == 0)
  5189. daycounter = 1; //yes, 1
  5190. else
  5191. daycounter++;
  5192. break;
  5193. case 12: //multiplier, in percent
  5194. multiplier = ((float)val)/100;
  5195. break;
  5196. case 13: //bank preset
  5197. bc = VLC->objh->banksInfo[index][val];
  5198. break;
  5199. case 14:
  5200. reset (val%100);
  5201. break;
  5202. case 15:
  5203. bc = NULL;
  5204. break;
  5205. case 16: //remove rewards from Derelict Ship
  5206. artifacts.clear();
  5207. break;
  5208. case 17: //set ArmedInstance army
  5209. {
  5210. int upgraded = 0;
  5211. if (val%100 < bc->upgradeChance) //once again anti-desync
  5212. upgraded = 1;
  5213. switch (bc->guards.size())
  5214. {
  5215. case 1:
  5216. for (int i = 0; i < 4; ++i)
  5217. setCreature (i, bc->guards[0].first, bc->guards[0].second / 5 );
  5218. setCreature (4, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  5219. break;
  5220. case 4:
  5221. {
  5222. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  5223. if (bc->guards.back().second) //all stacks are present
  5224. {
  5225. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  5226. {
  5227. setCreature (stacksCount(), it->first, it->second);
  5228. }
  5229. }
  5230. else if (bc->guards[2].second)//Wraiths are present, split two stacks in Crypt
  5231. {
  5232. setCreature (0, bc->guards[0].first, bc->guards[0].second / 2 );
  5233. setCreature (1, bc->guards[1].first, bc->guards[1].second / 2);
  5234. setCreature (2, bc->guards[2].first + upgraded, bc->guards[2].second);
  5235. setCreature (3, bc->guards[1].first, bc->guards[1].second / 2 );
  5236. setCreature (4, bc->guards[0].first, bc->guards[0].second - (bc->guards[0].second / 2) );
  5237. }
  5238. else //split both stacks
  5239. {
  5240. for (int i = 0; i < 3; ++i) //skellies
  5241. setCreature (2*i, bc->guards[0].first, bc->guards[0].second / 3);
  5242. for (int i = 0; i < 2; ++i) //zombies
  5243. setCreature (2*i+1, bc->guards[1].first, bc->guards[1].second / 2);
  5244. }
  5245. }
  5246. break;
  5247. default:
  5248. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  5249. return;
  5250. }
  5251. }
  5252. break;
  5253. case 18: //add Artifact
  5254. {
  5255. int id = cb->getArtSync(val, CArtifact::ART_TREASURE);
  5256. artifacts.push_back (id);
  5257. cb->erasePickedArt(id);
  5258. break;
  5259. }
  5260. case 19: //add Artifact
  5261. {
  5262. int id = cb->getArtSync(val, CArtifact::ART_MINOR);
  5263. artifacts.push_back (id);
  5264. cb->erasePickedArt(id);
  5265. break;
  5266. }
  5267. case 20: //add Artifact
  5268. {
  5269. int id = cb->getArtSync(val, CArtifact::ART_MAJOR);
  5270. artifacts.push_back (id);
  5271. cb->erasePickedArt(id);
  5272. break;
  5273. }
  5274. case 21: //add Artifact
  5275. {
  5276. int id = cb->getArtSync(val, CArtifact::ART_RELIC);
  5277. artifacts.push_back (id);
  5278. cb->erasePickedArt(id);
  5279. break;
  5280. }
  5281. }
  5282. }
  5283. void CBank::newTurn() const
  5284. {
  5285. if (bc == NULL)
  5286. {
  5287. if (cb->getDate(0) == 1)
  5288. initialize(); //initialize on first day
  5289. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  5290. {
  5291. initialize();
  5292. cb->setObjProperty (id, 11, 0); //daycounter 0
  5293. if (ID == 24 && cb->getDate(0) > 1)
  5294. {
  5295. cb->setObjProperty (id, 12, 0);//ugly hack to make derelict ships usable only once
  5296. cb->setObjProperty (id, 16, 0);
  5297. }
  5298. }
  5299. else
  5300. cb->setObjProperty (id, 11, 1); //daycounter++
  5301. }
  5302. }
  5303. void CBank::onHeroVisit (const CGHeroInstance * h) const
  5304. {
  5305. if (bc)
  5306. {
  5307. int banktext = 0;
  5308. switch (ID)
  5309. {
  5310. case 16: //generic bank
  5311. banktext = 32;
  5312. break;
  5313. case 24:
  5314. banktext = 41;
  5315. break;
  5316. case 25: //utopia
  5317. banktext = 47;
  5318. break;
  5319. case 84: //crypt
  5320. banktext = 119;
  5321. break;
  5322. case 85: //shipwreck
  5323. banktext = 122;
  5324. break;
  5325. }
  5326. BlockingDialog bd (true, false);
  5327. bd.player = h->getOwner();
  5328. bd.soundID = soundBase::DANGER;
  5329. bd.text << VLC->generaltexth->advobtxt[banktext];
  5330. if (ID == 16)
  5331. bd.text.addReplacement(VLC->objh->creBanksNames[index]);
  5332. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5333. }
  5334. else
  5335. {
  5336. InfoWindow iw;
  5337. iw.soundID = soundBase::GRAVEYARD;
  5338. iw.player = h->getOwner();
  5339. if (ID == 84) //morale penalty for empty Crypt
  5340. {
  5341. GiveBonus gbonus;
  5342. gbonus.id = h->id;
  5343. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5344. gbonus.bonus.source = Bonus::OBJECT;
  5345. gbonus.bonus.id = ID;
  5346. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[98];
  5347. gbonus.bonus.type = Bonus::MORALE;
  5348. gbonus.bonus.val = -1;
  5349. cb->giveHeroBonus(&gbonus);
  5350. iw.text << VLC->generaltexth->advobtxt[120];
  5351. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5352. }
  5353. else
  5354. {
  5355. iw.text << VLC->generaltexth->advobtxt[33];
  5356. iw.text.addReplacement(VLC->objh->creBanksNames[index]);
  5357. }
  5358. cb->showInfoDialog(&iw);
  5359. }
  5360. }
  5361. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  5362. {
  5363. if (accept)
  5364. {
  5365. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  5366. }
  5367. }
  5368. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5369. {
  5370. if (result->winner == 0)
  5371. {
  5372. int textID = -1;
  5373. InfoWindow iw;
  5374. iw.player = h->getOwner();
  5375. MetaString loot;
  5376. switch (ID)
  5377. {
  5378. case 16: case 25:
  5379. textID = 34;
  5380. break;
  5381. case 24: //derelict ship
  5382. if (multiplier)
  5383. textID = 43;
  5384. else
  5385. {
  5386. GiveBonus gbonus;
  5387. gbonus.id = h->id;
  5388. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5389. gbonus.bonus.source = Bonus::OBJECT;
  5390. gbonus.bonus.id = ID;
  5391. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  5392. gbonus.bonus.type = Bonus::MORALE;
  5393. gbonus.bonus.val = -1;
  5394. cb->giveHeroBonus(&gbonus);
  5395. textID = 42;
  5396. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5397. }
  5398. break;
  5399. case 84: //Crypt
  5400. if (bc->resources.size() != 0)
  5401. textID = 121;
  5402. else
  5403. {
  5404. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5405. GiveBonus gbonus;
  5406. gbonus.id = h->id;
  5407. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5408. gbonus.bonus.source = Bonus::OBJECT;
  5409. gbonus.bonus.id = ID;
  5410. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  5411. gbonus.bonus.type = Bonus::MORALE;
  5412. gbonus.bonus.val = -1;
  5413. cb->giveHeroBonus(&gbonus);
  5414. textID = 120;
  5415. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5416. }
  5417. break;
  5418. case 85: //shipwreck
  5419. if (bc->resources.size())
  5420. textID = 124;
  5421. else
  5422. textID = 123;
  5423. break;
  5424. }
  5425. //grant resources
  5426. if (textID != 42) //empty derelict ship gives no cash
  5427. {
  5428. for (int it = 0; it < bc->resources.size(); it++)
  5429. {
  5430. if (bc->resources[it] != 0)
  5431. {
  5432. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  5433. loot << "%d %s";
  5434. loot.addReplacement(iw.components.back().val);
  5435. loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
  5436. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  5437. }
  5438. }
  5439. }
  5440. //grant artifacts
  5441. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  5442. {
  5443. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  5444. loot << "%s";
  5445. loot.addReplacement(MetaString::ART_NAMES, *it);
  5446. cb->giveHeroNewArtifact (h, VLC->arth->artifacts[*it], -2);
  5447. }
  5448. //display loot
  5449. if (!iw.components.empty())
  5450. {
  5451. if (textID == 34)
  5452. {
  5453. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  5454. iw.text.addReplacement(MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  5455. iw.text.addReplacement(loot.buildList());
  5456. }
  5457. else
  5458. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  5459. cb->showInfoDialog(&iw);
  5460. }
  5461. loot.clear();
  5462. iw.components.clear();
  5463. iw.text.clear();
  5464. //grant creatures
  5465. CCreatureSet ourArmy;
  5466. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  5467. {
  5468. int slot = ourArmy.getSlotFor(it->first);
  5469. ourArmy.addToSlot(slot, it->first, it->second);
  5470. }
  5471. for (TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  5472. {
  5473. iw.components.push_back(Component(*i->second));
  5474. loot << "%s";
  5475. loot.addReplacement(*i->second);
  5476. }
  5477. if (ourArmy.Slots().size())
  5478. {
  5479. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second->count == 1)
  5480. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  5481. else
  5482. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  5483. iw.text.addReplacement(loot.buildList());
  5484. iw.text.addReplacement(h->name);
  5485. cb->showInfoDialog(&iw);
  5486. cb->giveCreatures(this, h, ourArmy, false);
  5487. }
  5488. cb->setObjProperty (id, 15, 0); //bc = NULL
  5489. }
  5490. }
  5491. void CGPyramid::initObj()
  5492. {
  5493. std::vector<ui16> available;
  5494. cb->getAllowedSpells (available, 5);
  5495. if (available.size())
  5496. {
  5497. bc = VLC->objh->banksInfo[21].front(); //TODO: remove hardcoded value?
  5498. spell = (available[rand()%available.size()]);
  5499. }
  5500. else
  5501. {
  5502. tlog1 <<"No spells available for Pyramid! Object set to empty.\n";
  5503. }
  5504. setPropertyDer (17,ran()); //set guards at game start
  5505. }
  5506. const std::string & CGPyramid::getHoverText() const
  5507. {
  5508. hoverName = VLC->objh->creBanksNames[21];
  5509. if (bc == NULL)
  5510. hoverName += " " + VLC->generaltexth->allTexts[352];
  5511. else
  5512. hoverName += " " + VLC->generaltexth->allTexts[353];
  5513. return hoverName;
  5514. }
  5515. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  5516. {
  5517. if (bc)
  5518. {
  5519. BlockingDialog bd (true, false);
  5520. bd.player = h->getOwner();
  5521. bd.soundID = soundBase::MYSTERY;
  5522. bd.text << VLC->generaltexth->advobtxt[105];
  5523. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5524. }
  5525. else
  5526. {
  5527. InfoWindow iw;
  5528. iw.player = h->getOwner();
  5529. iw.text << VLC->generaltexth->advobtxt[107];
  5530. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  5531. GiveBonus gb;
  5532. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  5533. gb.id = h->id;
  5534. cb->giveHeroBonus(&gb);
  5535. cb->showInfoDialog(&iw);
  5536. }
  5537. }
  5538. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5539. {
  5540. if (result->winner == 0)
  5541. {
  5542. InfoWindow iw;
  5543. iw.player = h->getOwner();
  5544. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  5545. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  5546. if (!h->getArt(17))
  5547. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  5548. else if (h->getSecSkillLevel(CGHeroInstance::WISDOM) < 3)
  5549. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  5550. else
  5551. {
  5552. std::set<ui32> spells;
  5553. spells.insert (spell);
  5554. cb->changeSpells (h->id, true, spells);
  5555. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  5556. }
  5557. cb->showInfoDialog(&iw);
  5558. cb->setObjProperty (id, 15, 0);
  5559. }
  5560. }
  5561. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  5562. {
  5563. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  5564. playerKeyMap.find(what-101)->second.insert((ui8)val);
  5565. }
  5566. bool CGKeys::wasMyColorVisited (int player) const
  5567. {
  5568. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  5569. return true;
  5570. else
  5571. return false;
  5572. }
  5573. const std::string & CGKeymasterTent::getHoverText() const
  5574. {
  5575. hoverName = VLC->generaltexth->names[ID];
  5576. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5577. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5578. else
  5579. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5580. return hoverName;
  5581. }
  5582. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  5583. {
  5584. InfoWindow iw;
  5585. iw.soundID = soundBase::CAVEHEAD;
  5586. iw.player = h->getOwner();
  5587. if (!wasMyColorVisited (h->getOwner()) )
  5588. {
  5589. cb->setObjProperty(id, h->tempOwner+101, subID);
  5590. iw.text << std::pair<ui8,ui32>(11,19);
  5591. }
  5592. else
  5593. iw.text << std::pair<ui8,ui32>(11,20);
  5594. cb->showInfoDialog(&iw);
  5595. }
  5596. void CGBorderGuard::initObj()
  5597. {
  5598. blockVisit = true;
  5599. }
  5600. const std::string & CGBorderGuard::getHoverText() const
  5601. {
  5602. hoverName = VLC->generaltexth->names[ID];
  5603. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5604. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5605. else
  5606. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5607. return hoverName;
  5608. }
  5609. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  5610. {
  5611. if (wasMyColorVisited (h->getOwner()) )
  5612. {
  5613. BlockingDialog bd (true, false);
  5614. bd.player = h->getOwner();
  5615. bd.soundID = soundBase::QUEST;
  5616. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  5617. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  5618. }
  5619. else
  5620. {
  5621. InfoWindow iw;
  5622. iw.player = h->getOwner();
  5623. iw.soundID = soundBase::CAVEHEAD;
  5624. iw.text << std::pair<ui8,ui32>(11,18);
  5625. cb->showInfoDialog (&iw);
  5626. }
  5627. }
  5628. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  5629. {
  5630. if (accept)
  5631. cb->removeObject(id);
  5632. }
  5633. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  5634. {
  5635. if (!wasMyColorVisited (h->getOwner()) )
  5636. {
  5637. InfoWindow iw;
  5638. iw.player = h->getOwner();
  5639. iw.text << std::pair<ui8,ui32>(11,18);
  5640. cb->showInfoDialog(&iw);
  5641. }
  5642. }
  5643. ui8 CGBorderGate::getPassableness() const
  5644. {
  5645. ui8 ret = 0;
  5646. for (int i = 0; i < PLAYER_LIMIT; i++)
  5647. ret |= wasMyColorVisited(i)<<i;
  5648. return ret;
  5649. }
  5650. void CGMagi::initObj()
  5651. {
  5652. if (ID == 27)
  5653. {
  5654. blockVisit = true;
  5655. eyelist[subID].push_back(id);
  5656. }
  5657. }
  5658. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  5659. {
  5660. if (ID == 37)
  5661. {
  5662. InfoWindow iw;
  5663. CenterView cv;
  5664. FoWChange fw;
  5665. cv.player = iw.player = fw.player = h->tempOwner;
  5666. iw.soundID = soundBase::LIGHTHOUSE;
  5667. iw.player = h->tempOwner;
  5668. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  5669. cb->showInfoDialog(&iw);
  5670. fw.mode = 1;
  5671. std::vector<si32>::iterator it;
  5672. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  5673. {
  5674. const CGObjectInstance *eye = cb->getObj(*it);
  5675. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  5676. cb->sendAndApply(&fw);
  5677. cv.pos = eye->pos;
  5678. cv.focusTime = 2000;
  5679. cb->sendAndApply(&cv);
  5680. }
  5681. cv.pos = h->getPosition(false);
  5682. cb->sendAndApply(&cv);
  5683. }
  5684. else if (ID == 27)
  5685. {
  5686. InfoWindow iw;
  5687. iw.player = h->tempOwner;
  5688. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  5689. cb->showInfoDialog(&iw);
  5690. }
  5691. }
  5692. void CGBoat::initObj()
  5693. {
  5694. hero = NULL;
  5695. }
  5696. void CGSirens::initObj()
  5697. {
  5698. blockVisit = true;
  5699. }
  5700. const std::string & CGSirens::getHoverText() const
  5701. {
  5702. getNameVis(hoverName);
  5703. return hoverName;
  5704. }
  5705. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  5706. {
  5707. InfoWindow iw;
  5708. iw.soundID = soundBase::DANGER;
  5709. iw.player = h->tempOwner;
  5710. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  5711. {
  5712. iw.text.addTxt(11,133);
  5713. }
  5714. else
  5715. {
  5716. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  5717. int xp = 0;
  5718. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  5719. {
  5720. TQuantity drown = i->second->count * 0.3;
  5721. if(drown)
  5722. {
  5723. cb->changeStackCount(StackLocation(h, i->first), -drown);
  5724. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  5725. }
  5726. }
  5727. if(xp)
  5728. {
  5729. iw.text.addTxt(11,132);
  5730. iw.text.addReplacement(xp);
  5731. }
  5732. else
  5733. {
  5734. iw.text.addTxt(11,134);
  5735. }
  5736. }
  5737. cb->showInfoDialog(&iw);
  5738. //////////////////////////////////////////////////////////////////////////
  5739. ///TODO: WHAT WITH EXP?
  5740. }
  5741. //bool IShipyard::validLocation() const
  5742. //{
  5743. // std::vector<int3> offsets;
  5744. // getOutOffsets(offsets);
  5745. //
  5746. // TerrainTile *tile;
  5747. // for(int i = 0; i < offsets.size(); i++)
  5748. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5749. // return true;
  5750. // return false;
  5751. //}
  5752. int3 IBoatGenerator::bestLocation() const
  5753. {
  5754. std::vector<int3> offsets;
  5755. getOutOffsets(offsets);
  5756. for (int i = 0; i < offsets.size(); ++i)
  5757. {
  5758. TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offsets[i]);
  5759. if (tile) //tile is in the map
  5760. {
  5761. if (tile->tertype == TerrainTile::water && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  5762. return o->pos + offsets[i];
  5763. }
  5764. }
  5765. return int3 (-1,-1,-1);
  5766. }
  5767. int IBoatGenerator::state() const
  5768. {
  5769. int3 tile = bestLocation();
  5770. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  5771. if(!t)
  5772. return 2; //no available water
  5773. else if(!t->blockingObjects.size())
  5774. return 0; //OK
  5775. else if(t->blockingObjects.front()->ID == 8)
  5776. return 1; //blocked with boat
  5777. else
  5778. return 2; //blocked
  5779. }
  5780. int IBoatGenerator::getBoatType() const
  5781. {
  5782. //We make good ships by default
  5783. return 1;
  5784. }
  5785. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  5786. : o(O)
  5787. {
  5788. }
  5789. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  5790. {
  5791. switch(state())
  5792. {
  5793. case 1:
  5794. out.addTxt(MetaString::GENERAL_TXT, 51);
  5795. break;
  5796. case 2:
  5797. if(visitor)
  5798. {
  5799. out.addTxt(MetaString::GENERAL_TXT, 134);
  5800. out.addReplacement(visitor->name);
  5801. }
  5802. else
  5803. out.addTxt(MetaString::ADVOB_TXT, 189);
  5804. break;
  5805. case 3:
  5806. tlog1 << "Shipyard without water!!! " << o->pos << "\t" << o->id << std::endl;
  5807. return;
  5808. }
  5809. }
  5810. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  5811. {
  5812. cost.resize(RESOURCE_QUANTITY);
  5813. cost[0] = 10;
  5814. cost[6] = 1000;
  5815. }
  5816. IShipyard::IShipyard(const CGObjectInstance *O)
  5817. : IBoatGenerator(O)
  5818. {
  5819. }
  5820. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  5821. {
  5822. if(!obj)
  5823. return NULL;
  5824. if(obj->ID == TOWNI_TYPE)
  5825. {
  5826. return static_cast<CGTownInstance*>(obj);
  5827. }
  5828. else if(obj->ID == 87)
  5829. {
  5830. return static_cast<CGShipyard*>(obj);
  5831. }
  5832. else
  5833. {
  5834. //tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  5835. return NULL;
  5836. }
  5837. }
  5838. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  5839. {
  5840. return castFrom(const_cast<CGObjectInstance*>(obj));
  5841. }
  5842. CGShipyard::CGShipyard()
  5843. :IShipyard(this)
  5844. {
  5845. }
  5846. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  5847. {
  5848. // H J L K I
  5849. // A x S x B
  5850. // C E G F D
  5851. offsets += int3(-3,0,0), int3(1,0,0), //AB
  5852. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  5853. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0); //HIJKL
  5854. }
  5855. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  5856. {
  5857. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  5858. cb->setOwner(id, h->tempOwner);
  5859. int s = state();
  5860. if(s)
  5861. {
  5862. InfoWindow iw;
  5863. iw.player = tempOwner;
  5864. getProblemText(iw.text, h);
  5865. cb->showInfoDialog(&iw);
  5866. }
  5867. else
  5868. {
  5869. OpenWindow ow;
  5870. ow.id1 = id;
  5871. ow.id2 = h->id;
  5872. ow.window = OpenWindow::SHIPYARD_WINDOW;
  5873. cb->sendAndApply(&ow);
  5874. }
  5875. }
  5876. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  5877. {
  5878. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  5879. {
  5880. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  5881. {
  5882. //ask if he wants to buy one
  5883. int text;
  5884. switch (subID)
  5885. {
  5886. case 0:
  5887. text = 25;
  5888. break;
  5889. case 1:
  5890. text = 26;
  5891. break;
  5892. case 2:
  5893. text = 27;
  5894. break;
  5895. default:
  5896. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  5897. }
  5898. BlockingDialog bd (true, false);
  5899. bd.player = h->getOwner();
  5900. bd.soundID = soundBase::LIGHTHOUSE;
  5901. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  5902. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  5903. }
  5904. else //if he cannot afford
  5905. {
  5906. InfoWindow iw;
  5907. iw.player = h->getOwner();
  5908. iw.soundID = soundBase::CAVEHEAD;
  5909. iw.text << std::pair<ui8,ui32>(11,28);
  5910. cb->showInfoDialog (&iw);
  5911. }
  5912. }
  5913. else //if he already visited carographer
  5914. {
  5915. InfoWindow iw;
  5916. iw.player = h->getOwner();
  5917. iw.soundID = soundBase::CAVEHEAD;
  5918. iw.text << std::pair<ui8,ui32>(11,24);
  5919. cb->showInfoDialog (&iw);
  5920. }
  5921. }
  5922. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  5923. {
  5924. if (accept) //if hero wants to buy map
  5925. {
  5926. cb->giveResource (h->tempOwner, 6, -1000);
  5927. FoWChange fw;
  5928. fw.mode = 1;
  5929. fw.player = h->tempOwner;
  5930. //subIDs of different types of cartographers:
  5931. //water = 0; land = 1; underground = 2;
  5932. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  5933. cb->sendAndApply (&fw);
  5934. cb->setObjProperty (id, 10, h->tempOwner);
  5935. }
  5936. }
  5937. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  5938. {
  5939. cb->showThievesGuildWindow(id);
  5940. }
  5941. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  5942. {
  5943. InfoWindow iw;
  5944. iw.player = h->tempOwner;
  5945. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  5946. assert(ts);
  5947. int team = ts->id;
  5948. if(!hasVisited(team))
  5949. {
  5950. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  5951. cb->sendAndApply(&iw);
  5952. cb->setObjProperty(id,20,team); //increment general visited obelisks counter
  5953. OpenWindow ow;
  5954. ow.id1 = h->tempOwner;
  5955. ow.window = OpenWindow::PUZZLE_MAP;
  5956. cb->sendAndApply(&ow);
  5957. cb->setObjProperty(id,10,team); //mark that particular obelisk as visited
  5958. }
  5959. else
  5960. {
  5961. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  5962. cb->sendAndApply(&iw);
  5963. }
  5964. }
  5965. void CGObelisk::initObj()
  5966. {
  5967. obeliskCount++;
  5968. }
  5969. const std::string & CGObelisk::getHoverText() const
  5970. {
  5971. hoverName = VLC->generaltexth->names[ID];
  5972. if(hasVisited(cb->getCurrentPlayer()))
  5973. hoverName += " " + VLC->generaltexth->allTexts[352]; //not visited
  5974. else
  5975. hoverName += " " + VLC->generaltexth->allTexts[353]; //visited
  5976. return hoverName;
  5977. }
  5978. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  5979. {
  5980. CPlayersVisited::setPropertyDer(what, val);
  5981. switch(what)
  5982. {
  5983. case 20:
  5984. assert(val < PLAYER_LIMIT);
  5985. visited[val]++;
  5986. if(visited[val] > obeliskCount)
  5987. {
  5988. tlog0 << "Error: Visited " << visited[val] << "\t\t" << obeliskCount << std::endl;
  5989. assert(0);
  5990. }
  5991. break;
  5992. }
  5993. }
  5994. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  5995. {
  5996. if(h->tempOwner != tempOwner)
  5997. {
  5998. ui8 oldOwner = tempOwner;
  5999. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  6000. InfoWindow iw;
  6001. iw.player = h->tempOwner;
  6002. iw.text.addTxt(MetaString::ADVOB_TXT, 69);
  6003. iw.soundID = soundBase::LIGHTHOUSE;
  6004. cb->sendAndApply(&iw);
  6005. giveBonusTo(h->tempOwner);
  6006. if(oldOwner < PLAYER_LIMIT) //remove bonus from old owner
  6007. {
  6008. RemoveBonus rb(RemoveBonus::PLAYER);
  6009. rb.whoID = oldOwner;
  6010. rb.source = Bonus::OBJECT;
  6011. rb.id = id;
  6012. cb->sendAndApply(&rb);
  6013. }
  6014. }
  6015. }
  6016. void CGLighthouse::initObj()
  6017. {
  6018. if(tempOwner < PLAYER_LIMIT)
  6019. {
  6020. giveBonusTo(tempOwner);
  6021. }
  6022. }
  6023. const std::string & CGLighthouse::getHoverText() const
  6024. {
  6025. hoverName = VLC->generaltexth->names[ID];
  6026. //TODO: owned by %s player
  6027. return hoverName;
  6028. }
  6029. void CGLighthouse::giveBonusTo( ui8 player ) const
  6030. {
  6031. GiveBonus gb(GiveBonus::PLAYER);
  6032. gb.bonus.type = Bonus::SEA_MOVEMENT;
  6033. gb.bonus.val = 500;
  6034. gb.id = player;
  6035. gb.bonus.duration = Bonus::PERMANENT;
  6036. gb.bonus.source = Bonus::OBJECT;
  6037. gb.bonus.id = id;
  6038. cb->sendAndApply(&gb);
  6039. }
  6040. CCreatureSet& CArmedInstance::getArmy() const
  6041. { //do not return itself by value, or it will xplode
  6042. // CCreatureSet set = *this; return set;
  6043. //WARNING! A DIRTY CONST_CAST! TO BE INVESTIGATED AND PROBABLY REMOVED!
  6044. return *(const_cast<CArmedInstance*>(this));
  6045. }
  6046. void CArmedInstance::randomizeArmy(int type)
  6047. {
  6048. int max = VLC->creh->creatures.size();
  6049. for (TSlots::iterator j = slots.begin(); j != slots.end();j++)
  6050. {
  6051. int randID = j->second->idRand;
  6052. if(randID > max)
  6053. {
  6054. if(randID % 2)
  6055. j->second->setType(VLC->townh->towns[type].basicCreatures[(randID-197) / 2 -1]);
  6056. else
  6057. j->second->setType(VLC->townh->towns[type].upgradedCreatures[(randID-197) / 2 -1]);
  6058. randID = -1;
  6059. }
  6060. assert(j->second->armyObj == this);
  6061. }
  6062. return;
  6063. }
  6064. // void CArmedInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  6065. // {
  6066. // /* //already given via PlayerState->getBonuses();
  6067. // const PlayerState *p = cb->getPlayerState(tempOwner);
  6068. // if (p && p != root)
  6069. // out.insert(p);
  6070. // */
  6071. // out.insert(&cb->gameState()->globalEffects); //global effects are always active I believe
  6072. //
  6073. // if(battle)
  6074. // out.insert(battle);
  6075. // }
  6076. CArmedInstance::CArmedInstance()
  6077. {
  6078. battle = NULL;
  6079. }
  6080. // void CArmedInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  6081. // {
  6082. // CBonusSystemNode::getBonuses(out, selector, root);
  6083. //
  6084. // if(!battle)
  6085. // {
  6086. // //TODO do it clean, unify with BattleInfo version
  6087. // if(Selector::matchesType(selector, Bonus::MORALE) || Selector::matchesType(selector, Bonus::LUCK))
  6088. // {
  6089. // for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  6090. // i->second.getBonuses(out, selector, Selector::effectRange(Bonus::ONLY_ALLIED_ARMY), this);
  6091. // }
  6092. // }
  6093. //
  6094. // if(Selector::matchesType(selector, Bonus::MORALE))
  6095. // {
  6096. // //number of alignments and presence of undead
  6097. // if(contains(dynamic_cast<const CStackInstance*>(root)))
  6098. // {
  6099. // bool archangelInArmy = false;
  6100. // bool canMix = hasBonusOfType(Bonus::NONEVIL_ALIGNMENT_MIX);
  6101. // std::set<si8> factions;
  6102. // for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  6103. // {
  6104. // // Take Angelic Alliance troop-mixing freedom of non-evil, non-Conflux units into account.
  6105. // const si8 faction = i->second.type->faction;
  6106. // if (canMix
  6107. // && ((faction >= 0 && faction <= 2) || faction == 6 || faction == 7))
  6108. // {
  6109. // factions.insert(0); // Insert a single faction of the affected group, Castle will do.
  6110. // }
  6111. // else
  6112. // {
  6113. // factions.insert(faction);
  6114. // }
  6115. //
  6116. // if(i->second.type->idNumber == 13)
  6117. // archangelInArmy = true;
  6118. // }
  6119. //
  6120. // if(factions.size() == 1)
  6121. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, +1, id, VLC->generaltexth->arraytxt[115]));//All troops of one alignment +1
  6122. // else
  6123. // {
  6124. // int fcountModifier = 2-factions.size();
  6125. // 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
  6126. // }
  6127. //
  6128. // if(vstd::contains(factions,4))
  6129. // out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, id, VLC->generaltexth->arraytxt[116]));//Undead in group -1
  6130. // }
  6131. // }
  6132. // }
  6133. int CArmedInstance::valOfGlobalBonuses(CSelector selector) const
  6134. {
  6135. //if (tempOwner != NEUTRAL_PLAYER)
  6136. return cb->gameState()->players[tempOwner].valOfBonuses(selector);
  6137. }
  6138. bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const
  6139. {
  6140. switch(mode)
  6141. {
  6142. case RESOURCE_RESOURCE:
  6143. {
  6144. float effectiveness = std::min(((float)getMarketEfficiency()+1.0f) / 20.0f, 0.5f);
  6145. float r = VLC->objh->resVals[id1], //value of given resource
  6146. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  6147. if(r>g) //if given resource is more expensive than wanted
  6148. {
  6149. val2 = ceil(r / g);
  6150. val1 = 1;
  6151. }
  6152. else //if wanted resource is more expensive
  6153. {
  6154. val1 = (g / r) + 0.5f;
  6155. val2 = 1;
  6156. }
  6157. }
  6158. break;
  6159. case CREATURE_RESOURCE:
  6160. {
  6161. const float effectivenessArray[] = {0, 0.3, 0.45, 0.50, 0.65, 0.7, 0.85, 0.9, 1};
  6162. float effectiveness = effectivenessArray[std::min(getMarketEfficiency(), 8)];
  6163. float r = VLC->creh->creatures[id1]->cost[6], //value of given creature in gold
  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 RESOURCE_PLAYER:
  6178. val1 = 1;
  6179. val2 = 1;
  6180. break;
  6181. case RESOURCE_ARTIFACT:
  6182. {
  6183. float effectiveness = std::min(((float)getMarketEfficiency()+3.0f) / 20.0f, 0.6f);
  6184. float r = VLC->objh->resVals[id1], //value of offered resource
  6185. g = VLC->arth->artifacts[id2]->price / effectiveness; //value of bought artifact in gold
  6186. if(id1 != 6) //non-gold prices are doubled
  6187. r /= 2;
  6188. assert(g >= r); //should we allow artifacts cheaper than unit of resource?
  6189. val1 = (g / r) + 0.5f;
  6190. val2 = 1;
  6191. }
  6192. break;
  6193. case CREATURE_EXP:
  6194. {
  6195. val1 = 1;
  6196. val2 = (VLC->creh->creatures[id1]->AIValue / 40) * 5;
  6197. }
  6198. break;
  6199. case ARTIFACT_EXP:
  6200. {
  6201. val1 = 1;
  6202. int givenClass = VLC->arth->artifacts[id1]->getArtClassSerial();
  6203. if(givenClass < 0 || givenClass > 3)
  6204. {
  6205. val2 = 0;
  6206. return false;
  6207. }
  6208. static const int expPerClass[] = {1000, 1500, 3000, 6000};
  6209. val2 = expPerClass[givenClass];
  6210. }
  6211. break;
  6212. default:
  6213. assert(0);
  6214. return false;
  6215. }
  6216. return true;
  6217. }
  6218. bool IMarket::allowsTrade(EMarketMode mode) const
  6219. {
  6220. return false;
  6221. }
  6222. int IMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  6223. {
  6224. switch(mode)
  6225. {
  6226. case RESOURCE_RESOURCE:
  6227. case ARTIFACT_RESOURCE:
  6228. case CREATURE_RESOURCE:
  6229. return -1;
  6230. default:
  6231. return 1;
  6232. }
  6233. }
  6234. std::vector<int> IMarket::availableItemsIds(EMarketMode mode) const
  6235. {
  6236. std::vector<int> ret;
  6237. switch(mode)
  6238. {
  6239. case RESOURCE_RESOURCE:
  6240. case ARTIFACT_RESOURCE:
  6241. case CREATURE_RESOURCE:
  6242. for (int i = 0; i < 7; i++)
  6243. ret.push_back(i);
  6244. default:
  6245. break;
  6246. }
  6247. return ret;
  6248. }
  6249. const IMarket * IMarket::castFrom(const CGObjectInstance *obj)
  6250. {
  6251. switch(obj->ID)
  6252. {
  6253. case TOWNI_TYPE:
  6254. return static_cast<const CGTownInstance*>(obj);
  6255. case 2: //Altar of Sacrifice
  6256. case 7: //Black Market
  6257. case 99: //Trading Post
  6258. case 221: //Trading Post (snow)
  6259. case 213: //Freelancer's Guild
  6260. return static_cast<const CGMarket*>(obj);
  6261. case 104: //University
  6262. return static_cast<const CGUniversity*>(obj);
  6263. default:
  6264. tlog1 << "Cannot cast to IMarket object with ID " << obj->ID << std::endl;
  6265. return NULL;
  6266. }
  6267. }
  6268. IMarket::IMarket(const CGObjectInstance *O)
  6269. :o(O)
  6270. {
  6271. }
  6272. std::vector<EMarketMode> IMarket::availableModes() const
  6273. {
  6274. std::vector<EMarketMode> ret;
  6275. for (int i = 0; i < MARTKET_AFTER_LAST_PLACEHOLDER; i++)
  6276. if(allowsTrade((EMarketMode)i))
  6277. ret.push_back((EMarketMode)i);
  6278. return ret;
  6279. }
  6280. void CGMarket::onHeroVisit(const CGHeroInstance * h) const
  6281. {
  6282. OpenWindow ow;
  6283. ow.id1 = id;
  6284. ow.id2 = h->id;
  6285. ow.window = OpenWindow::MARKET_WINDOW;
  6286. cb->sendAndApply(&ow);
  6287. }
  6288. int CGMarket::getMarketEfficiency() const
  6289. {
  6290. return 5;
  6291. }
  6292. bool CGMarket::allowsTrade(EMarketMode mode) const
  6293. {
  6294. switch(mode)
  6295. {
  6296. case RESOURCE_RESOURCE:
  6297. case RESOURCE_PLAYER:
  6298. switch(ID)
  6299. {
  6300. case 99: //Trading Post
  6301. case 221: //Trading Post (snow)
  6302. return true;
  6303. default:
  6304. return false;
  6305. }
  6306. case CREATURE_RESOURCE:
  6307. return ID == 213; //Freelancer's Guild
  6308. //case ARTIFACT_RESOURCE:
  6309. case RESOURCE_ARTIFACT:
  6310. return ID == 7; //Black Market
  6311. case ARTIFACT_EXP:
  6312. case CREATURE_EXP:
  6313. return ID == 2; //TODO? check here for alignment of visiting hero? - would not be coherent with other checks here
  6314. case RESOURCE_SKILL:
  6315. return ID == 104;//University
  6316. default:
  6317. return false;
  6318. }
  6319. }
  6320. int CGMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  6321. {
  6322. return -1;
  6323. }
  6324. std::vector<int> CGMarket::availableItemsIds(EMarketMode mode) const
  6325. {
  6326. switch(mode)
  6327. {
  6328. case RESOURCE_RESOURCE:
  6329. case RESOURCE_PLAYER:
  6330. return IMarket::availableItemsIds(mode);
  6331. default:
  6332. return std::vector<int>();
  6333. }
  6334. }
  6335. CGMarket::CGMarket()
  6336. :IMarket(this)
  6337. {
  6338. }
  6339. std::vector<int> CGBlackMarket::availableItemsIds(EMarketMode mode) const
  6340. {
  6341. switch(mode)
  6342. {
  6343. case ARTIFACT_RESOURCE:
  6344. return IMarket::availableItemsIds(mode);
  6345. case RESOURCE_ARTIFACT:
  6346. {
  6347. std::vector<int> ret;
  6348. BOOST_FOREACH(const CArtifact *a, artifacts)
  6349. if(a)
  6350. ret.push_back(a->id);
  6351. else
  6352. ret.push_back(-1);
  6353. return ret;
  6354. }
  6355. default:
  6356. return std::vector<int>();
  6357. }
  6358. }
  6359. void CGBlackMarket::newTurn() const
  6360. {
  6361. if(cb->getDate(2) != 1)
  6362. return;
  6363. SetAvailableArtifacts saa;
  6364. saa.id = id;
  6365. cb->pickAllowedArtsSet(saa.arts);
  6366. cb->sendAndApply(&saa);
  6367. }
  6368. void CGUniversity::initObj()
  6369. {
  6370. std::vector <int> toChoose;
  6371. for (int i=0; i<SKILL_QUANTITY; i++)
  6372. if (cb->isAllowed(2,i))
  6373. toChoose.push_back(i);
  6374. if (toChoose.size() < 4)
  6375. {
  6376. tlog0<<"Warning: less then 4 available skills was found by University initializer!\n";
  6377. return;
  6378. }
  6379. for (int i=0; i<4; i++)//get 4 skills
  6380. {
  6381. int skillPos = ran()%toChoose.size();
  6382. skills.push_back(toChoose[skillPos]);//move it to selected
  6383. toChoose.erase(toChoose.begin()+skillPos);//remove from list
  6384. }
  6385. }
  6386. std::vector<int> CGUniversity::availableItemsIds(EMarketMode mode) const
  6387. {
  6388. switch (mode)
  6389. {
  6390. case RESOURCE_SKILL:
  6391. return skills;
  6392. default:
  6393. return std::vector <int> ();
  6394. }
  6395. }
  6396. void CGUniversity::onHeroVisit(const CGHeroInstance * h) const
  6397. {
  6398. OpenWindow ow;
  6399. ow.id1 = id;
  6400. ow.id2 = h->id;
  6401. ow.window = OpenWindow::UNIVERSITY_WINDOW;
  6402. cb->sendAndApply(&ow);
  6403. }
  6404. const CArtifactInstance* CArtifactSet::getArt(ui16 pos) const
  6405. {
  6406. if(const ArtSlotInfo *si = getSlot(pos))
  6407. {
  6408. if(si->artifact && !si->locked)
  6409. return si->artifact;
  6410. }
  6411. return NULL;
  6412. }
  6413. si32 CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  6414. {
  6415. for(std::map<ui16, ArtSlotInfo>::const_iterator i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  6416. if(i->second.artifact->artType->id == aid)
  6417. return i->first;
  6418. if(onlyWorn)
  6419. return -1;
  6420. for(int i = 0; i < artifactsInBackpack.size(); i++)
  6421. if(artifactsInBackpack[i].artifact->artType->id == aid)
  6422. return Arts::BACKPACK_START + i;
  6423. return -1;
  6424. }
  6425. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= true*/) const
  6426. {
  6427. return getArtPos(aid, onlyWorn) != -1;
  6428. }
  6429. const ArtSlotInfo * CArtifactSet::getSlot(ui16 pos) const
  6430. {
  6431. if(vstd::contains(artifactsWorn, pos))
  6432. return &artifactsWorn[pos];
  6433. if(pos >= Arts::AFTER_LAST )
  6434. {
  6435. int backpackPos = (int)pos - Arts::BACKPACK_START;
  6436. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  6437. return NULL;
  6438. else
  6439. return &artifactsInBackpack[backpackPos];
  6440. }
  6441. return NULL;
  6442. }
  6443. bool CArtifactSet::isPositionFree(ui16 pos) const
  6444. {
  6445. if(const ArtSlotInfo *s = getSlot(pos))
  6446. return !s->artifact && !s->locked;
  6447. return true; //no slot means not used
  6448. }