CObjectHandler.cpp 179 KB

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