CObjectHandler.cpp 147 KB

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