CObjectHandler.cpp 188 KB

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