CObjectHandler.cpp 147 KB

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