CObjectHandler.cpp 182 KB

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