CObjectHandler.cpp 184 KB

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