CObjectHandler.cpp 181 KB

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