CObjectHandler.cpp 155 KB

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