CObjectHandler.cpp 192 KB

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