CObjectHandler.cpp 172 KB

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