CObjectHandler.cpp 192 KB

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