CObjectHandler.cpp 179 KB

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