2
0

CObjectHandler.cpp 190 KB

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