CObjectHandler.cpp 182 KB

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