CObjectHandler.cpp 184 KB

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