CObjectHandler.cpp 174 KB

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