CObjectHandler.cpp 179 KB

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