CObjectHandler.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308
  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. using namespace boost::assign;
  26. /*
  27. * CObjectHandler.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  36. IGameCallback * IObjectInterface::cb = NULL;
  37. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  38. extern CLodHandler * bitmaph;
  39. extern boost::rand48 ran;
  40. std::map <ui8, std::set <ui8> > CGKeys::playerKeyMap;
  41. std::map <si32, std::vector<si32> > CGMagi::eyelist;
  42. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  43. {};
  44. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  45. {};
  46. void IObjectInterface::newTurn () const
  47. {};
  48. IObjectInterface::~IObjectInterface()
  49. {}
  50. IObjectInterface::IObjectInterface()
  51. {}
  52. void IObjectInterface::initObj()
  53. {}
  54. void IObjectInterface::setProperty( ui8 what, ui32 val )
  55. {}
  56. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  57. {
  58. if(what == 10)
  59. players.insert(val);
  60. }
  61. bool CPlayersVisited::hasVisited( ui8 player ) const
  62. {
  63. return vstd::contains(players,player);
  64. }
  65. static void readCreatures(std::istream & is, BankConfig & bc, bool guards) //helper function for void CObjectHandler::loadObjects()
  66. {
  67. const int MAX_BUF = 5000;
  68. char buffer[MAX_BUF + 1];
  69. std::pair<si16, si32> guardInfo = std::make_pair(0, 0);
  70. std::string creName;
  71. is >> guardInfo.first;
  72. //one getline just does not work... probably a kind of left whitespace
  73. is.getline(buffer, MAX_BUF, '\t');
  74. is.getline(buffer, MAX_BUF, '\t');
  75. creName = buffer;
  76. if( std::string(buffer) == "None" ) //no creature to be added
  77. return;
  78. //look for the best creature that is described by given name
  79. if( vstd::contains(VLC->creh->nameToID, creName) )
  80. {
  81. guardInfo.second = VLC->creh->nameToID[creName];
  82. }
  83. else
  84. {
  85. for(int g=0; g<VLC->creh->creatures.size(); ++g)
  86. {
  87. if(VLC->creh->creatures[g].namePl == creName
  88. || VLC->creh->creatures[g].nameRef == creName
  89. || VLC->creh->creatures[g].nameSing == creName)
  90. {
  91. guardInfo.second = VLC->creh->creatures[g].idNumber;
  92. }
  93. }
  94. }
  95. if(guards)
  96. bc.guards.push_back(guardInfo);
  97. else //given creatures
  98. bc.creatures.push_back(guardInfo);
  99. }
  100. void CObjectHandler::loadObjects()
  101. {
  102. {
  103. tlog5 << "\t\tReading cregens \n";
  104. cregens.resize(110); //TODO: hardcoded value - change
  105. for(size_t i=0; i < cregens.size(); ++i)
  106. {
  107. cregens[i]=-1;
  108. }
  109. std::ifstream ifs("config/cregens.txt");
  110. while(!ifs.eof())
  111. {
  112. int dw, cr;
  113. ifs >> dw >> cr;
  114. cregens[dw]=cr;
  115. }
  116. tlog5 << "\t\tDone loading objects!\n";
  117. }
  118. std::string banksConfig = bitmaph->getTextFile("ZCRBANK.TXT");
  119. std::istringstream istr(banksConfig);
  120. const int MAX_BUF = 5000;
  121. char buffer[MAX_BUF + 1];
  122. //omitting unnecessary lines
  123. istr.getline(buffer, MAX_BUF);
  124. istr.getline(buffer, MAX_BUF);
  125. for(int g=0; g<21; ++g) //TODO: remove hardcoded value
  126. {
  127. //reading name - TODO: use it if necessary
  128. istr.getline(buffer, MAX_BUF, '\t');
  129. for(int i=0; i<4; ++i) //reading levels
  130. {
  131. BankConfig bc;
  132. std::string buf;
  133. char dump;
  134. //bc.level is of type char and thus we cannot read directly to it; same for some othre variables
  135. istr >> buf;
  136. bc.level = atoi(buf.c_str());
  137. istr >> buf;
  138. bc.chance = atoi(buf.c_str());
  139. readCreatures(istr, bc, true);
  140. istr >> buf;
  141. bc.upgradeChance = atoi(buf.c_str());
  142. for(int b=0; b<3; ++b)
  143. readCreatures(istr, bc, true);
  144. istr >> bc.combatValue;
  145. bc.resources.resize(RESOURCE_QUANTITY);
  146. for(int h=0; h<7; ++h)
  147. {
  148. istr >> bc.resources[h];
  149. }
  150. readCreatures(istr, bc, false);
  151. bc.artifacts.resize(4);
  152. for(int b=0; b<4; ++b)
  153. {
  154. istr >> bc.artifacts[b];
  155. }
  156. istr >> bc.value;
  157. istr >> bc.rewardDifficulty;
  158. istr >> buf;
  159. bc.easiest = atoi(buf.c_str());
  160. banksInfo[g].push_back(bc);
  161. }
  162. }
  163. }
  164. int CGObjectInstance::getOwner() const
  165. {
  166. //if (state)
  167. // return state->owner;
  168. //else
  169. return tempOwner; //won't have owner
  170. }
  171. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  172. {
  173. pos = int3(-1,-1,-1);
  174. //std::cout << "Tworze obiekt "<<this<<std::endl;
  175. //state = new CLuaObjectScript();
  176. ID = subID = id = -1;
  177. defInfo = NULL;
  178. info = NULL;
  179. tempOwner = 254;
  180. blockVisit = false;
  181. }
  182. CGObjectInstance::~CGObjectInstance()
  183. {
  184. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  185. //if (state)
  186. // delete state;
  187. //state=NULL;
  188. }
  189. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  190. //{
  191. // pos = right.pos;
  192. // ID = right.ID;
  193. // subID = right.subID;
  194. // id = right.id;
  195. // defInfo = right.defInfo;
  196. // info = right.info;
  197. // blockVisit = right.blockVisit;
  198. // //state = new CLuaObjectScript(right.state->);
  199. // //*state = *right.state;
  200. // //state = right.state;
  201. // tempOwner = right.tempOwner;
  202. //}
  203. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  204. //{
  205. // pos = right.pos;
  206. // ID = right.ID;
  207. // subID = right.subID;
  208. // id = right.id;
  209. // defInfo = right.defInfo;
  210. // info = right.info;
  211. // blockVisit = right.blockVisit;
  212. // //state = new CLuaObjectScript();
  213. // //*state = *right.state;
  214. // tempOwner = right.tempOwner;
  215. // return *this;
  216. //}
  217. const std::string & CGObjectInstance::getHoverText() const
  218. {
  219. return hoverName;
  220. }
  221. void CGObjectInstance::setOwner(int ow)
  222. {
  223. //if (state)
  224. // state->owner = ow;
  225. //else
  226. tempOwner = ow;
  227. }
  228. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  229. {
  230. return defInfo->width;
  231. }
  232. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  233. {
  234. return defInfo->height;
  235. }
  236. 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)
  237. {
  238. if(defInfo==NULL)
  239. {
  240. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  241. return false;
  242. }
  243. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  244. {
  245. return true;
  246. }
  247. return false;
  248. }
  249. bool CGObjectInstance::blockingAt(int x, int y) const
  250. {
  251. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  252. return false;
  253. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  254. return false;
  255. return true;
  256. }
  257. bool CGObjectInstance::coveringAt(int x, int y) const
  258. {
  259. if((defInfo->coverageMap[y] >> (7-(x) )) & 1)
  260. return true;
  261. return false;
  262. }
  263. std::set<int3> CGObjectInstance::getBlockedPos() const
  264. {
  265. std::set<int3> ret;
  266. for(int w=0; w<getWidth(); ++w)
  267. {
  268. for(int h=0; h<getHeight(); ++h)
  269. {
  270. if(blockingAt(w, h))
  271. ret.insert(int3(pos.x - getWidth() + w + 1, pos.y - getHeight() + h + 1, pos.z));
  272. }
  273. }
  274. return ret;
  275. }
  276. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  277. {
  278. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  279. return true;
  280. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  281. return false;
  282. if(this->pos.y<cmp.pos.y)
  283. return true;
  284. if(this->pos.y>cmp.pos.y)
  285. return false;
  286. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  287. return true;
  288. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  289. return false;
  290. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  291. return true;
  292. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  293. return false;
  294. if(this->pos.x<cmp.pos.x)
  295. return true;
  296. return false;
  297. }
  298. void CGObjectInstance::initObj()
  299. {
  300. }
  301. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  302. {
  303. switch(what)
  304. {
  305. case 1:
  306. tempOwner = val;
  307. break;
  308. case 2:
  309. blockVisit = val;
  310. break;
  311. case 6:
  312. ID = val;
  313. break;
  314. }
  315. setPropertyDer(what, val);
  316. }
  317. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  318. {}
  319. int3 CGObjectInstance::getSightCenter() const
  320. {
  321. //return vistiable tile if possible
  322. for(int i=0; i < 8; i++)
  323. for(int j=0; j < 6; j++)
  324. if(visitableAt(i,j))
  325. return(pos + int3(i-7, j-5, 0));
  326. return pos;
  327. }
  328. int CGObjectInstance::getSightRadious() const
  329. {
  330. return 3;
  331. }
  332. int3 CGObjectInstance::getVisitableOffset() const
  333. {
  334. for(int y = 0; y < 6; y++)
  335. for (int x = 0; x < 8; x++)
  336. if((defInfo->visitMap[5-y] >> x) & 1)
  337. return int3(x,y,0);
  338. tlog2 << "Warning: getVisitableOffset called on non-visitable obj!\n";
  339. return int3(-1,-1,-1);
  340. }
  341. void CGObjectInstance::getNameVis( std::string &hname ) const
  342. {
  343. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  344. hname = VLC->generaltexth->names[ID];
  345. if(h)
  346. {
  347. if(!h->getBonus(HeroBonus::OBJECT,ID))
  348. hname += " " + VLC->generaltexth->allTexts[353]; //not visited
  349. else
  350. hname += " " + VLC->generaltexth->allTexts[352]; //visited
  351. }
  352. }
  353. void CGObjectInstance::giveDummyBonus(int heroID, ui8 duration) const
  354. {
  355. GiveBonus gbonus;
  356. gbonus.bonus.type = HeroBonus::NONE;
  357. gbonus.hid = heroID;
  358. gbonus.bonus.duration = duration;
  359. gbonus.bonus.source = HeroBonus::OBJECT;
  360. gbonus.bonus.id = ID;
  361. cb->giveHeroBonus(&gbonus);
  362. }
  363. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  364. {
  365. }
  366. static int lowestSpeed(const CGHeroInstance * chi)
  367. {
  368. if(!chi->army.slots.size())
  369. {
  370. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  371. return 20;
  372. }
  373. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  374. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  375. for (;i!=chi->army.slots.end();i++)
  376. {
  377. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  378. }
  379. return ret;
  380. }
  381. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  382. {
  383. //TODO: check if all creatures are on its native terrain and change cost appropriately
  384. //base move cost
  385. unsigned ret = 100;
  386. //if there is road both on dest and src tiles - use road movement cost
  387. if(dest.malle && from.malle)
  388. {
  389. int road = std::min(dest.malle,from.malle); //used road ID
  390. switch(road)
  391. {
  392. case TerrainTile::dirtRoad:
  393. ret = 75;
  394. break;
  395. case TerrainTile::grazvelRoad:
  396. ret = 65;
  397. break;
  398. case TerrainTile::cobblestoneRoad:
  399. ret = 50;
  400. break;
  401. default:
  402. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  403. break;
  404. }
  405. }
  406. else
  407. {
  408. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  409. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  410. }
  411. return ret;
  412. }
  413. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  414. {
  415. unsigned int sl = 100;
  416. for(size_t h=0; h < army.slots.size(); ++h)
  417. {
  418. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  419. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  420. }
  421. return sl;
  422. }
  423. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  424. {
  425. if (toh3m)
  426. {
  427. src.x+=1;
  428. return src;
  429. }
  430. else
  431. {
  432. src.x-=1;
  433. return src;
  434. }
  435. }
  436. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  437. {
  438. if (h3m)
  439. {
  440. return pos;
  441. }
  442. else
  443. {
  444. return convertPosition(pos,false);
  445. }
  446. }
  447. si32 CGHeroInstance::manaLimit() const
  448. {
  449. double modifier = 1.0;
  450. switch(getSecSkillLevel(24)) //intelligence level
  451. {
  452. case 1: modifier+=0.25; break;
  453. case 2: modifier+=0.5; break;
  454. case 3: modifier+=1.0; break;
  455. }
  456. return si32(10*getPrimSkillLevel(3)*modifier);
  457. }
  458. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  459. //{
  460. // if (h3m)
  461. // pos = Pos;
  462. // else
  463. // pos = convertPosition(Pos,true);
  464. //}
  465. bool CGHeroInstance::canWalkOnSea() const
  466. {
  467. //TODO: write it - it should check if hero is flying, or something similar
  468. return false;
  469. }
  470. int CGHeroInstance::getPrimSkillLevel(int id) const
  471. {
  472. int ret = primSkills[id];
  473. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  474. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  475. ret += i->val;
  476. amax(ret, id/2);//minimum value for attack and defense is 0 and for spell power and knowledge - 1
  477. return ret;
  478. }
  479. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  480. {
  481. for(size_t i=0; i < secSkills.size(); ++i)
  482. if(secSkills[i].first==ID)
  483. return secSkills[i].second;
  484. return 0;
  485. }
  486. int CGHeroInstance::maxMovePoints(bool onLand) const
  487. {
  488. int ret = std::min(2000, 1270+70*lowestSpeed(this)),
  489. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  490. double modifier = 0;
  491. if(onLand)
  492. {
  493. //logistics:
  494. switch(getSecSkillLevel(2))
  495. {
  496. case 1:
  497. modifier = 0.1;
  498. break;
  499. case 2:
  500. modifier = 0.2;
  501. break;
  502. case 3:
  503. modifier = 0.3;
  504. break;
  505. }
  506. }
  507. else
  508. {
  509. //navigation:
  510. switch(getSecSkillLevel(5))
  511. {
  512. case 1:
  513. modifier = 0.5;
  514. break;
  515. case 2:
  516. modifier = 1.0;
  517. break;
  518. case 3:
  519. modifier = 1.5;
  520. break;
  521. }
  522. }
  523. return int(ret + ret*modifier) + bonus;
  524. }
  525. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  526. {
  527. if(pos<19)
  528. if(vstd::contains(artifWorn,pos))
  529. return artifWorn.find(pos)->second;
  530. else
  531. return -1;
  532. else
  533. if(pos-19 < artifacts.size())
  534. return artifacts[pos-19];
  535. else
  536. return -1;
  537. }
  538. const CArtifact * CGHeroInstance::getArt(int pos) const
  539. {
  540. int id = getArtAtPos(pos);
  541. if(id>=0)
  542. return &VLC->arth->artifacts[id];
  543. else
  544. return NULL;
  545. }
  546. int CGHeroInstance::getSpellSecLevel(int spell) const
  547. {
  548. int bestslvl = 0;
  549. if(VLC->spellh->spells[spell].air)
  550. if(getSecSkillLevel(15) >= bestslvl)
  551. {
  552. bestslvl = getSecSkillLevel(15);
  553. }
  554. if(VLC->spellh->spells[spell].fire)
  555. if(getSecSkillLevel(14) >= bestslvl)
  556. {
  557. bestslvl = getSecSkillLevel(14);
  558. }
  559. if(VLC->spellh->spells[spell].water)
  560. if(getSecSkillLevel(16) >= bestslvl)
  561. {
  562. bestslvl = getSecSkillLevel(16);
  563. }
  564. if(VLC->spellh->spells[spell].earth)
  565. if(getSecSkillLevel(17) >= bestslvl)
  566. {
  567. bestslvl = getSecSkillLevel(17);
  568. }
  569. return bestslvl;
  570. }
  571. CGHeroInstance::CGHeroInstance()
  572. {
  573. ID = HEROI_TYPE;
  574. tacticFormationEnabled = inTownGarrison = false;
  575. mana = movement = portrait = level = -1;
  576. isStanding = true;
  577. moveDir = 4;
  578. exp = 0xffffffff;
  579. visitedTown = NULL;
  580. type = NULL;
  581. boat = NULL;
  582. secSkills.push_back(std::make_pair(-1, -1));
  583. }
  584. void CGHeroInstance::initHero(int SUBID)
  585. {
  586. subID = SUBID;
  587. initHero();
  588. }
  589. void CGHeroInstance::initHero()
  590. {
  591. if(ID == HEROI_TYPE)
  592. initHeroDefInfo();
  593. if(!type)
  594. type = VLC->heroh->heroes[subID];
  595. artifWorn[16] = 3;
  596. if(type->startingSpell >= 0) //hero starts with a spell
  597. {
  598. artifWorn[17] = 0; //give him spellbook
  599. spells.insert(type->startingSpell);
  600. }
  601. if(portrait < 0 || portrait == 255)
  602. portrait = subID;
  603. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  604. {
  605. primSkills.resize(4);
  606. primSkills[0] = type->heroClass->initialAttack;
  607. primSkills[1] = type->heroClass->initialDefence;
  608. primSkills[2] = type->heroClass->initialPower;
  609. primSkills[3] = type->heroClass->initialKnowledge;
  610. }
  611. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  612. secSkills = type->secSkillsInit;
  613. if(mana < 0)
  614. mana = manaLimit();
  615. if (!name.length())
  616. name = type->name;
  617. if (exp == 0xffffffff)
  618. {
  619. exp=40+ (ran()) % 50;
  620. level = 1;
  621. }
  622. else
  623. {
  624. level = VLC->heroh->level(exp);
  625. }
  626. if (!army.slots.size()) //standard army//initial army
  627. {
  628. int pom, pom2=0;
  629. int x = 0; //how many stacks will hero receives <1 - 3>
  630. pom = ran()%100;
  631. if(pom < 9)
  632. x = 1;
  633. else if(pom < 79)
  634. x = 2;
  635. else
  636. x = 3;
  637. for(int x=0;x<3;x++)
  638. {
  639. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  640. if(pom>=145 && pom<=149) //war machine
  641. {
  642. pom2++;
  643. switch (pom)
  644. {
  645. case 145: //catapult
  646. artifWorn[16] = 3;
  647. break;
  648. default:
  649. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  650. break;
  651. }
  652. continue;
  653. }
  654. army.slots[x-pom2].first = pom;
  655. pom = type->highStack[x] - type->lowStack[x];
  656. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  657. army.formation = false;
  658. }
  659. }
  660. hoverName = VLC->generaltexth->allTexts[15];
  661. boost::algorithm::replace_first(hoverName,"%s",name);
  662. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  663. recreateArtBonuses();
  664. }
  665. void CGHeroInstance::initHeroDefInfo()
  666. {
  667. if(!defInfo || defInfo->id != HEROI_TYPE)
  668. {
  669. defInfo = new CGDefInfo();
  670. defInfo->id = HEROI_TYPE;
  671. defInfo->subid = subID;
  672. defInfo->printPriority = 0;
  673. defInfo->visitDir = 0xff;
  674. }
  675. for(int i=0;i<6;i++)
  676. {
  677. defInfo->blockMap[i] = 255;
  678. defInfo->visitMap[i] = 0;
  679. defInfo->coverageMap[i] = 0;
  680. }
  681. defInfo->handler=NULL;
  682. defInfo->blockMap[5] = 253;
  683. defInfo->visitMap[5] = 2;
  684. defInfo->coverageMap[4] = defInfo->coverageMap[5] = 224;
  685. }
  686. CGHeroInstance::~CGHeroInstance()
  687. {
  688. }
  689. bool CGHeroInstance::needsLastStack() const
  690. {
  691. return true;
  692. }
  693. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  694. {
  695. if(h == this) return; //exclude potential self-visiting
  696. if (ID == HEROI_TYPE) //hero
  697. {
  698. //TODO: check for allies
  699. if(tempOwner == h->tempOwner) //our hero
  700. {
  701. //exchange
  702. cb->heroExchange(id, h->id);
  703. }
  704. else
  705. {
  706. //battle
  707. cb->startBattleI(h, this, false);
  708. }
  709. }
  710. else if(ID == 62) //prison
  711. {
  712. InfoWindow iw;
  713. iw.player = h->tempOwner;
  714. iw.soundID = soundBase::ROGUE;
  715. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  716. {
  717. cb->changeObjPos(id,pos+int3(1,0,0),0);
  718. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  719. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  720. iw.text << std::pair<ui8,ui32>(11,102);
  721. }
  722. else //already 8 wandering heroes
  723. {
  724. iw.text << std::pair<ui8,ui32>(11,103);
  725. }
  726. cb->showInfoDialog(&iw);
  727. }
  728. }
  729. const std::string & CGHeroInstance::getBiography() const
  730. {
  731. if (biography.length())
  732. return biography;
  733. else
  734. return VLC->generaltexth->hTxts[subID].biography;
  735. }
  736. void CGHeroInstance::initObj()
  737. {
  738. blockVisit = true;
  739. }
  740. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  741. {
  742. int ret = 0;
  743. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  744. for(int i=0; i < mods.size(); i++)
  745. ret += mods[i].first;
  746. if(ret > 3)
  747. return 3;
  748. if(ret < -3)
  749. return -3;
  750. return ret;
  751. }
  752. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  753. {
  754. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  755. std::vector<std::pair<int,std::string> > ret;
  756. //various morale bonuses (from buildings, artifacts, etc)
  757. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  758. {
  759. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  760. {
  761. ret.push_back(std::make_pair(i->val, i->description));
  762. }
  763. }
  764. //leadership
  765. if(getSecSkillLevel(6))
  766. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  767. //town structures
  768. if(town && visitedTown)
  769. {
  770. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  771. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  772. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  773. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  774. }
  775. //number of alignments and presence of undead
  776. if(stack>=0)
  777. {
  778. bool archangelInArmy = false;
  779. std::set<si8> factions;
  780. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  781. {
  782. factions.insert(VLC->creh->creatures[i->second.first].faction);
  783. if(i->second.first == 13)
  784. archangelInArmy = true;
  785. }
  786. if(factions.size() == 1)
  787. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  788. else
  789. {
  790. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  791. {
  792. char buf[150];
  793. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  794. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  795. }
  796. else
  797. {
  798. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  799. }
  800. }
  801. if(vstd::contains(factions,4))
  802. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  803. if(archangelInArmy)
  804. {
  805. char buf[100];
  806. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  807. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  808. }
  809. }
  810. return ret;
  811. }
  812. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  813. {
  814. int ret = 0;
  815. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  816. for(int i=0; i < mods.size(); i++)
  817. ret += mods[i].first;
  818. if(ret > 3)
  819. return 3;
  820. if(ret < -3)
  821. return -3;
  822. return ret;
  823. }
  824. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  825. {
  826. std::vector<std::pair<int,std::string> > ret;
  827. //various morale bonuses (from buildings, artifacts, etc)
  828. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  829. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  830. ret.push_back(std::make_pair(i->val, i->description));
  831. //luck skill
  832. if(getSecSkillLevel(9))
  833. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  834. return ret;
  835. }
  836. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  837. {
  838. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  839. if(i->source == from && i->id == id)
  840. return &*i;
  841. return NULL;
  842. }
  843. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  844. {
  845. if(what == 3)
  846. army.slots[0].second = val;
  847. }
  848. double CGHeroInstance::getHeroStrength() const
  849. {
  850. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  851. }
  852. int CGHeroInstance::getTotalStrength() const
  853. {
  854. double ret = getHeroStrength() * getArmyStrength();
  855. return (int) ret;
  856. }
  857. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  858. {
  859. ui8 skill = 0; //skill level
  860. if(spell->fire)
  861. skill = std::max(skill,getSecSkillLevel(14));
  862. if(spell->air)
  863. skill = std::max(skill,getSecSkillLevel(15));
  864. if(spell->water)
  865. skill = std::max(skill,getSecSkillLevel(16));
  866. if(spell->earth)
  867. skill = std::max(skill,getSecSkillLevel(17));
  868. //bonuses (eg. from special terrains)
  869. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  870. if(spell->fire)
  871. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 1));
  872. if(spell->air)
  873. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 2));
  874. if(spell->water)
  875. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 4));
  876. if(spell->earth)
  877. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 8));
  878. return skill;
  879. }
  880. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  881. {
  882. if(!getArt(17)) //if hero has no spellbook
  883. return false;
  884. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  885. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  886. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  887. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  888. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  889. )
  890. return true;
  891. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  892. {
  893. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  894. {
  895. return true;
  896. }
  897. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  898. {
  899. return true;
  900. }
  901. }
  902. return false;
  903. }
  904. int3 CGHeroInstance::getSightCenter() const
  905. {
  906. return getPosition(false);
  907. }
  908. int CGHeroInstance::getSightRadious() const
  909. {
  910. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  911. }
  912. si32 CGHeroInstance::manaRegain() const
  913. {
  914. if (hasBonusOfType(HeroBonus::FULL_MANA_REGENERATION))
  915. return manaLimit();
  916. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  917. }
  918. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  919. {
  920. int ret = 0;
  921. if(subtype == -1)
  922. {
  923. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  924. if(i->type == type)
  925. ret += i->val;
  926. }
  927. else
  928. {
  929. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  930. if(i->type == type && i->subtype == subtype)
  931. ret += i->val;
  932. }
  933. return ret;
  934. }
  935. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  936. {
  937. if(subtype == -1) //any subtype
  938. {
  939. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  940. if(i->type == type)
  941. return true;
  942. }
  943. else //given subtype
  944. {
  945. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  946. if(i->type == type && i->subtype == subtype)
  947. return true;
  948. }
  949. return false;
  950. }
  951. si32 CGHeroInstance::getArtPos(int aid) const
  952. {
  953. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  954. if(i->second == aid)
  955. return i->first;
  956. return -1;
  957. }
  958. void CGHeroInstance::recreateArtBonuses()
  959. {
  960. //clear all bonuses from artifacts (if present) and give them again
  961. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  962. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  963. {
  964. CArtifact &art = VLC->arth->artifacts[ari->second];
  965. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  966. bonuses.push_back(*i);
  967. }
  968. }
  969. void CGDwelling::initObj()
  970. {
  971. switch(ID)
  972. {
  973. case 17:
  974. {
  975. int crid = VLC->objh->cregens[subID];
  976. CCreature *crs = &VLC->creh->creatures[crid];
  977. creatures.resize(1);
  978. creatures[0].second.push_back(crid);
  979. hoverName = VLC->generaltexth->creGens[subID];
  980. if(crs->level > 4)
  981. {
  982. army.slots[0].first = crs->idNumber;
  983. army.slots[0].second = (8 - crs->level) * 3;
  984. }
  985. }
  986. break;
  987. case 20:
  988. creatures.resize(4);
  989. if(subID == 1) //Golem Factory
  990. {
  991. creatures[0].second.push_back(32); //Stone Golem
  992. creatures[1].second.push_back(33); //Iron Golem
  993. creatures[2].second.push_back(116); //Gold Golem
  994. creatures[3].second.push_back(117); //Diamond Golem
  995. //guards
  996. army.slots[0].first = 116;
  997. army.slots[0].second = 9;
  998. army.slots[1].first = 117;
  999. army.slots[1].second = 6;
  1000. }
  1001. else if(subID == 0) // Elemental Conflux
  1002. {
  1003. creatures[0].second.push_back(112); //Air Elemental
  1004. creatures[1].second.push_back(114); //Fire Elemental
  1005. creatures[2].second.push_back(113); //Earth Elemental
  1006. creatures[3].second.push_back(115); //Water Elemental
  1007. //guards
  1008. army.slots[0].first = 113;
  1009. army.slots[0].second = 12;
  1010. }
  1011. else
  1012. {
  1013. assert(0);
  1014. }
  1015. hoverName = VLC->generaltexth->creGens4[subID];
  1016. break;
  1017. default:
  1018. assert(0);
  1019. break;
  1020. }
  1021. }
  1022. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1023. {
  1024. if(h->tempOwner != tempOwner && army) //object is guarded
  1025. {
  1026. BlockingDialog bd;
  1027. bd.player = h->tempOwner;
  1028. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1029. 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?
  1030. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1031. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  1032. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  1033. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1034. return;
  1035. }
  1036. if(h->tempOwner != tempOwner)
  1037. cb->setOwner(id, h->tempOwner);
  1038. BlockingDialog bd;
  1039. bd.player = h->tempOwner;
  1040. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1041. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  1042. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1043. for(size_t i = 0; i < creatures.size(); i++)
  1044. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1045. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1046. }
  1047. void CGDwelling::newTurn() const
  1048. {
  1049. if(cb->getDate(1) != 1) //not first day of week
  1050. return;
  1051. //town growths are handled separately
  1052. if(ID == TOWNI_TYPE)
  1053. return;
  1054. bool change = false;
  1055. SetAvailableCreatures sac;
  1056. sac.creatures = creatures;
  1057. sac.tid = id;
  1058. for (size_t i = 0; i < creatures.size(); i++)
  1059. {
  1060. if(creatures[i].second.size())
  1061. {
  1062. if(false /*accumulate creatures*/)
  1063. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  1064. else
  1065. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  1066. change = true;
  1067. }
  1068. }
  1069. if(change)
  1070. cb->sendAndApply(&sac);
  1071. }
  1072. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1073. {
  1074. if(!answer)
  1075. return;
  1076. int crid = creatures[0].second[0];
  1077. CCreature *crs = &VLC->creh->creatures[crid];
  1078. if(crs->level == 1) //first level - creatures are for free
  1079. {
  1080. if(creatures[0].first) //there are available creatures
  1081. {
  1082. int slot = h->army.getSlotFor(crid);
  1083. if(slot < 0) //no available slot
  1084. {
  1085. InfoWindow iw;
  1086. iw.player = h->tempOwner;
  1087. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1088. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1089. cb->showInfoDialog(&iw);
  1090. }
  1091. else //give creatures
  1092. {
  1093. SetAvailableCreatures sac;
  1094. sac.tid = id;
  1095. sac.creatures = creatures;
  1096. sac.creatures[0].first = 0;
  1097. SetGarrisons sg;
  1098. sg.garrs[h->id] = h->army;
  1099. sg.garrs[h->id].slots[slot].first = crid;
  1100. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1101. InfoWindow iw;
  1102. iw.player = h->tempOwner;
  1103. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1104. iw.text.addReplacement(creatures[0].first);
  1105. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1106. cb->showInfoDialog(&iw);
  1107. cb->sendAndApply(&sac);
  1108. cb->sendAndApply(&sg);
  1109. }
  1110. }
  1111. else //there no creatures
  1112. {
  1113. InfoWindow iw;
  1114. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1115. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1116. iw.player = h->tempOwner;
  1117. cb->sendAndApply(&iw);
  1118. }
  1119. }
  1120. else
  1121. {
  1122. OpenWindow ow;
  1123. ow.id1 = id;
  1124. ow.id2 = h->id;
  1125. ow.window = ID == 17
  1126. ? OpenWindow::RECRUITMENT_FIRST
  1127. : OpenWindow::RECRUITMENT_ALL;
  1128. cb->sendAndApply(&ow);
  1129. }
  1130. }
  1131. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1132. {
  1133. if(answer)
  1134. cb->startBattleI(h, this, false, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1135. }
  1136. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1137. {
  1138. if (result->winner == 0)
  1139. {
  1140. onHeroVisit(h);
  1141. }
  1142. }
  1143. int CGTownInstance::getSightRadious() const //returns sight distance
  1144. {
  1145. if (subID == 2) //tower
  1146. if ((builtBuildings.find(17)) != builtBuildings.end()) //skyship
  1147. return cb->getMapSize().first + cb->getMapSize().second;
  1148. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1149. return 20;
  1150. return 5;
  1151. }
  1152. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1153. {
  1154. if((builtBuildings.find(9))!=builtBuildings.end())
  1155. return 3;
  1156. if((builtBuildings.find(8))!=builtBuildings.end())
  1157. return 2;
  1158. if((builtBuildings.find(7))!=builtBuildings.end())
  1159. return 1;
  1160. return 0;
  1161. }
  1162. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1163. {
  1164. if ((builtBuildings.find(13))!=builtBuildings.end())
  1165. return 3;
  1166. if ((builtBuildings.find(12))!=builtBuildings.end())
  1167. return 2;
  1168. if ((builtBuildings.find(11))!=builtBuildings.end())
  1169. return 1;
  1170. if ((builtBuildings.find(10))!=builtBuildings.end())
  1171. return 0;
  1172. return -1;
  1173. }
  1174. int CGTownInstance::mageGuildLevel() const
  1175. {
  1176. if ((builtBuildings.find(4))!=builtBuildings.end())
  1177. return 5;
  1178. if ((builtBuildings.find(3))!=builtBuildings.end())
  1179. return 4;
  1180. if ((builtBuildings.find(2))!=builtBuildings.end())
  1181. return 3;
  1182. if ((builtBuildings.find(1))!=builtBuildings.end())
  1183. return 2;
  1184. if ((builtBuildings.find(0))!=builtBuildings.end())
  1185. return 1;
  1186. return 0;
  1187. }
  1188. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1189. {
  1190. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1191. }
  1192. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1193. {
  1194. return town->hordeLvl[HID];
  1195. }
  1196. int CGTownInstance::creatureGrowth(const int & level) const
  1197. {
  1198. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1199. switch(fortLevel())
  1200. {
  1201. case 3:
  1202. ret*=2;break;
  1203. case 2:
  1204. ret*=(1.5); break;
  1205. }
  1206. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  1207. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  1208. if(getHordeLevel(0)==level)
  1209. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1210. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1211. if(getHordeLevel(1)==level)
  1212. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1213. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1214. //support for legs of legion etc.
  1215. if(garrisonHero)
  1216. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1217. if(visitingHero)
  1218. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1219. return ret;
  1220. }
  1221. int CGTownInstance::dailyIncome() const
  1222. {
  1223. int ret = 0;
  1224. if ((builtBuildings.find(26))!=builtBuildings.end())
  1225. ret+=5000;
  1226. if ((builtBuildings.find(13))!=builtBuildings.end())
  1227. ret+=4000;
  1228. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1229. ret+=2000;
  1230. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1231. ret+=1000;
  1232. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1233. ret+=500;
  1234. return ret;
  1235. }
  1236. bool CGTownInstance::hasFort() const
  1237. {
  1238. return (builtBuildings.find(7))!=builtBuildings.end();
  1239. }
  1240. bool CGTownInstance::hasCapitol() const
  1241. {
  1242. return (builtBuildings.find(13))!=builtBuildings.end();
  1243. }
  1244. CGTownInstance::CGTownInstance()
  1245. :IShipyard(this)
  1246. {
  1247. builded=-1;
  1248. destroyed=-1;
  1249. garrisonHero=NULL;
  1250. town=NULL;
  1251. visitingHero = NULL;
  1252. }
  1253. CGTownInstance::~CGTownInstance()
  1254. {}
  1255. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1256. {
  1257. if(checkGuild && mageGuildLevel() < level)
  1258. return 0;
  1259. int ret = 6 - level; //how many spells are available at this level
  1260. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1261. ret++;
  1262. return ret;
  1263. }
  1264. bool CGTownInstance::needsLastStack() const
  1265. {
  1266. if(garrisonHero)
  1267. return true;
  1268. else return false;
  1269. }
  1270. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1271. {
  1272. if(getOwner() != h->getOwner())
  1273. {
  1274. //TODO ally check
  1275. if(army)
  1276. {
  1277. const CGHeroInstance *defendingHero = NULL;
  1278. if(visitingHero)
  1279. defendingHero = visitingHero;
  1280. else if(garrisonHero)
  1281. defendingHero = garrisonHero;
  1282. const CArmedInstance *defendingArmy = this;
  1283. if(defendingHero)
  1284. defendingArmy = defendingHero;
  1285. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1286. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1287. }
  1288. else
  1289. {
  1290. cb->setOwner(id, h->tempOwner);
  1291. }
  1292. }
  1293. for (std::vector<CGTownBuilding>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1294. i->onHeroVisit (h);
  1295. cb->heroVisitCastle(id, h->id);
  1296. }
  1297. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1298. {
  1299. cb->stopHeroVisitCastle(id,h->id);
  1300. }
  1301. void CGTownInstance::initObj()
  1302. {
  1303. hoverName = name + ", " + town->Name();
  1304. creatures.resize(CREATURES_PER_TOWN);
  1305. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1306. {
  1307. if(creatureDwelling(i,false))
  1308. creatures[i].second.push_back(town->basicCreatures[i]);
  1309. if(creatureDwelling(i,true))
  1310. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1311. }
  1312. switch (alignment)
  1313. { //add new visitable objects
  1314. case 2: case 3: case 5: case 6:
  1315. bonusingBuildings.push_back(*new CTownBonus(23, this));
  1316. break;
  1317. case 7:
  1318. bonusingBuildings.push_back(*new CTownBonus(17, this));
  1319. break;
  1320. }
  1321. }
  1322. int3 CGTownInstance::getSightCenter() const
  1323. {
  1324. return pos - int3(2,0,0);
  1325. }
  1326. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1327. {
  1328. offsets += int3(-1,3,0), int3(-3,3,0);
  1329. }
  1330. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1331. {
  1332. if(result->winner == 0)
  1333. {
  1334. cb->setOwner(id, h->tempOwner);
  1335. }
  1336. }
  1337. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1338. {
  1339. if(visitors.find(h->id)==visitors.end())
  1340. {
  1341. onNAHeroVisit(h->id, false);
  1342. switch(ID)
  1343. {
  1344. case 102: //tree
  1345. case 4: //arena
  1346. case 41://library
  1347. case 47: //School of Magic
  1348. case 107://School of War
  1349. break;
  1350. default:
  1351. cb->setObjProperty(id,4,h->id); //add to the visitors
  1352. break;
  1353. }
  1354. }
  1355. else
  1356. {
  1357. onNAHeroVisit(h->id, true);
  1358. }
  1359. }
  1360. void CGVisitableOPH::initObj()
  1361. {
  1362. if(ID==102)
  1363. ttype = ran()%3;
  1364. else
  1365. ttype = -1;
  1366. }
  1367. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1368. {
  1369. if(result) //player agreed to give res for exp
  1370. {
  1371. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1372. cb->changePrimSkill(heroID,4,expVal); //give exp
  1373. cb->setObjProperty(id,4,heroID); //add to the visitors
  1374. }
  1375. }
  1376. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1377. {
  1378. int id=0, subid=0, ot=0, val=1, sound = 0;
  1379. switch(ID)
  1380. {
  1381. case 4: //arena
  1382. sound = soundBase::NOMAD;
  1383. ot = 0;
  1384. break;
  1385. case 51: //mercenary camp
  1386. sound = soundBase::NOMAD;
  1387. subid=0;
  1388. ot=80;
  1389. break;
  1390. case 23: //marletto tower
  1391. sound = soundBase::NOMAD;
  1392. subid=1;
  1393. ot=39;
  1394. break;
  1395. case 61:
  1396. sound = soundBase::gazebo;
  1397. subid=2;
  1398. ot=100;
  1399. break;
  1400. case 32:
  1401. sound = soundBase::GETPROTECTION;
  1402. subid=3;
  1403. ot=59;
  1404. break;
  1405. case 100:
  1406. sound = soundBase::gazebo;
  1407. id=5;
  1408. ot=143;
  1409. val=1000;
  1410. break;
  1411. case 102:
  1412. sound = soundBase::gazebo;
  1413. id = 5;
  1414. subid = 1;
  1415. ot = 146;
  1416. val = 1;
  1417. break;
  1418. case 41:
  1419. sound = soundBase::gazebo;
  1420. ot = 66;
  1421. break;
  1422. case 47: //School of Magic
  1423. sound = soundBase::faerie;
  1424. ot = 71;
  1425. break;
  1426. case 107://School of War
  1427. sound = soundBase::MILITARY;
  1428. ot = 158;
  1429. break;
  1430. }
  1431. if (!alreadyVisited)
  1432. {
  1433. switch (ID)
  1434. {
  1435. case 4: //arena
  1436. {
  1437. BlockingDialog sd(false,true);
  1438. sd.soundID = sound;
  1439. sd.text << std::pair<ui8,ui32>(11,ot);
  1440. sd.components.push_back(Component(0,0,2,0));
  1441. sd.components.push_back(Component(0,1,2,0));
  1442. sd.player = cb->getOwner(heroID);
  1443. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1444. return;
  1445. }
  1446. case 51:
  1447. case 23:
  1448. case 61:
  1449. case 32:
  1450. {
  1451. cb->changePrimSkill(heroID,subid,val);
  1452. InfoWindow iw;
  1453. iw.soundID = sound;
  1454. iw.components.push_back(Component(0,subid,val,0));
  1455. iw.text << std::pair<ui8,ui32>(11,ot);
  1456. iw.player = cb->getOwner(heroID);
  1457. cb->showInfoDialog(&iw);
  1458. break;
  1459. }
  1460. case 100: //give exp
  1461. {
  1462. InfoWindow iw;
  1463. iw.soundID = sound;
  1464. iw.components.push_back(Component(id,subid,val,0));
  1465. iw.player = cb->getOwner(heroID);
  1466. iw.text << std::pair<ui8,ui32>(11,ot);
  1467. iw.soundID = soundBase::gazebo;
  1468. cb->showInfoDialog(&iw);
  1469. cb->changePrimSkill(heroID,4,val);
  1470. break;
  1471. }
  1472. case 102://tree
  1473. {
  1474. const CGHeroInstance *h = cb->getHero(heroID);
  1475. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1476. if(!ttype)
  1477. {
  1478. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1479. InfoWindow iw;
  1480. iw.soundID = sound;
  1481. iw.components.push_back(Component(id,subid,1,0));
  1482. iw.player = cb->getOwner(heroID);
  1483. iw.text << std::pair<ui8,ui32>(11,148);
  1484. cb->showInfoDialog(&iw);
  1485. cb->changePrimSkill(heroID,4,val);
  1486. break;
  1487. }
  1488. else
  1489. {
  1490. int res, resval;
  1491. if(ttype==1)
  1492. {
  1493. res = 6;
  1494. resval = 2000;
  1495. ot = 149;
  1496. }
  1497. else
  1498. {
  1499. res = 5;
  1500. resval = 10;
  1501. ot = 151;
  1502. }
  1503. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1504. {
  1505. ot++;
  1506. InfoWindow iw;
  1507. iw.soundID = sound;
  1508. iw.player = h->tempOwner;
  1509. iw.text << std::pair<ui8,ui32>(11,ot);
  1510. cb->showInfoDialog(&iw);
  1511. return;
  1512. }
  1513. BlockingDialog sd(true,false);
  1514. sd.soundID = sound;
  1515. sd.player = cb->getOwner(heroID);
  1516. sd.text << std::pair<ui8,ui32>(11,ot);
  1517. sd.components.push_back(Component(id,subid,val,0));
  1518. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1519. }
  1520. break;
  1521. }
  1522. case 41://library of enlightenment
  1523. {
  1524. const CGHeroInstance *h = cb->getHero(heroID);
  1525. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1526. {
  1527. InfoWindow iw;
  1528. iw.soundID = sound;
  1529. iw.player = cb->getOwner(heroID);
  1530. iw.text << std::pair<ui8,ui32>(11,68);
  1531. cb->showInfoDialog(&iw);
  1532. }
  1533. else
  1534. {
  1535. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1536. cb->changePrimSkill(heroID,0,2);
  1537. cb->changePrimSkill(heroID,1,2);
  1538. cb->changePrimSkill(heroID,2,2);
  1539. cb->changePrimSkill(heroID,3,2);
  1540. InfoWindow iw;
  1541. iw.soundID = sound;
  1542. iw.player = cb->getOwner(heroID);
  1543. iw.text << std::pair<ui8,ui32>(11,66);
  1544. cb->showInfoDialog(&iw);
  1545. }
  1546. break;
  1547. }
  1548. case 47: //School of Magic
  1549. case 107://School of War
  1550. {
  1551. int skill = (ID==47 ? 2 : 0);
  1552. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1553. {
  1554. InfoWindow iw;
  1555. iw.soundID = sound;
  1556. iw.player = cb->getOwner(heroID);
  1557. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1558. cb->showInfoDialog(&iw);
  1559. }
  1560. else
  1561. {
  1562. BlockingDialog sd(true,true);
  1563. sd.soundID = sound;
  1564. sd.player = cb->getOwner(heroID);
  1565. sd.text << std::pair<ui8,ui32>(11,ot);
  1566. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1567. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1568. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1569. }
  1570. }
  1571. break;
  1572. }
  1573. }
  1574. else
  1575. {
  1576. ot++;
  1577. InfoWindow iw;
  1578. iw.soundID = sound;
  1579. iw.player = cb->getOwner(heroID);
  1580. iw.text << std::pair<ui8,ui32>(11,ot);
  1581. cb->showInfoDialog(&iw);
  1582. }
  1583. }
  1584. const std::string & CGVisitableOPH::getHoverText() const
  1585. {
  1586. int pom = -1;
  1587. switch(ID)
  1588. {
  1589. case 4:
  1590. pom = -1;
  1591. break;
  1592. case 51:
  1593. pom = 8;
  1594. break;
  1595. case 23:
  1596. pom = 7;
  1597. break;
  1598. case 61:
  1599. pom = 11;
  1600. break;
  1601. case 32:
  1602. pom = 4;
  1603. break;
  1604. case 100:
  1605. pom = 5;
  1606. break;
  1607. case 102:
  1608. pom = 18;
  1609. break;
  1610. case 41:
  1611. break;
  1612. case 47: //School of Magic
  1613. pom = 9;
  1614. break;
  1615. case 107://School of War
  1616. pom = 10;
  1617. break;
  1618. default:
  1619. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1620. }
  1621. hoverName = VLC->generaltexth->names[ID];
  1622. if(pom >= 0)
  1623. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1624. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1625. if(h)
  1626. {
  1627. hoverName += ' ';
  1628. hoverName += (vstd::contains(visitors,h->id))
  1629. ? (VLC->generaltexth->allTexts[352]) //visited
  1630. : ( VLC->generaltexth->allTexts[353]); //not visited
  1631. }
  1632. return hoverName;
  1633. }
  1634. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1635. {
  1636. cb->setObjProperty(id,4,heroID); //add to the visitors
  1637. cb->changePrimSkill(heroID,primSkill-1,2);
  1638. }
  1639. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1640. {
  1641. if(what == 4)
  1642. visitors.insert(val);
  1643. }
  1644. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1645. {
  1646. if(!which) //player refused to pay
  1647. return;
  1648. int base = (ID == 47 ? 2 : 0);
  1649. cb->setObjProperty(id,4,heroID); //add to the visitors
  1650. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1651. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1652. }
  1653. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  1654. {
  1655. ID = index;
  1656. town = TOWN;
  1657. }
  1658. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  1659. {
  1660. int heroID = h->id;
  1661. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  1662. {
  1663. InfoWindow iw;
  1664. switch (ID)
  1665. {
  1666. case 23:
  1667. switch(town->alignment)
  1668. {
  1669. case 2: //wall
  1670. cb->changePrimSkill (heroID, 3, 1);
  1671. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1672. break;
  1673. case 3: //order of fire
  1674. cb->changePrimSkill (heroID, 2, 1);
  1675. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1676. break;
  1677. case 6://hall of valhalla
  1678. cb->changePrimSkill (heroID, 0, 1);
  1679. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1680. break;
  1681. case 5://academy of battle scholars
  1682. cb->changePrimSkill (heroID, 4, 1000);
  1683. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  1684. break;
  1685. }
  1686. break;
  1687. case 17:
  1688. switch(town->alignment)
  1689. {
  1690. case 7: //cage of warlords
  1691. cb->changePrimSkill (heroID, 1, 1);
  1692. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1693. break;
  1694. }
  1695. break;
  1696. }
  1697. iw.player = cb->getOwner(heroID);
  1698. iw.text << std::pair<ui8,ui32>(11,66);
  1699. //iw.soundID = sound;
  1700. cb->showInfoDialog(&iw);
  1701. cb->setObjProperty (id, 4, h->id); //add to visitors
  1702. }
  1703. }
  1704. bool CArmedInstance::needsLastStack() const
  1705. {
  1706. return false;
  1707. }
  1708. int CArmedInstance::getArmyStrength() const
  1709. {
  1710. int ret = 0;
  1711. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1712. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1713. return ret;
  1714. }
  1715. /*const std::string & CGCreature::getHoverText() const
  1716. {
  1717. hoverName = VLC->generaltexth->names[ID];
  1718. hoverName += "\n Power rating: ";
  1719. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  1720. if (ratio < 0.1) hoverName += "Effortless";
  1721. else if (ratio < 0.3) hoverName += "Very Weak";
  1722. else if (ratio < 0.6) hoverName += "Weak";
  1723. else if (ratio < 0.9) hoverName += "A bit weaker";
  1724. else if (ratio < 1.1) hoverName += "Equal";
  1725. else if (ratio < 1.3) hoverName += "A bit stronger";
  1726. else if (ratio < 1.8) hoverName += "Strong";
  1727. else if (ratio < 2.5) hoverName += "Very Strong";
  1728. else if (ratio < 4) hoverName += "Challenging";
  1729. else if (ratio < 8) hoverName += "Overpowering";
  1730. else if (ratio < 20) hoverName += "Deadly";
  1731. else hoverName += "Impossible";
  1732. return hoverName;
  1733. }*/
  1734. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1735. {
  1736. int action = takenAction(h);
  1737. switch( action ) //decide what we do...
  1738. {
  1739. case -2: //fight
  1740. fight(h);
  1741. break;
  1742. case -1: //flee
  1743. {
  1744. flee(h);
  1745. break;
  1746. }
  1747. case 0: //join for free
  1748. {
  1749. BlockingDialog ynd(true,false);
  1750. ynd.player = h->tempOwner;
  1751. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1752. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1753. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1754. break;
  1755. }
  1756. default: //join for gold
  1757. {
  1758. assert(action > 0);
  1759. //ask if player agrees to pay gold
  1760. BlockingDialog ynd(true,false);
  1761. ynd.player = h->tempOwner;
  1762. std::string tmp = VLC->generaltexth->advobtxt[90];
  1763. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1764. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1765. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1766. ynd.text << tmp;
  1767. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. void CGCreature::endBattle( BattleResult *result ) const
  1773. {
  1774. if(result->winner==0)
  1775. {
  1776. cb->removeObject(id);
  1777. }
  1778. else
  1779. {
  1780. //int killedAmount=0;
  1781. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1782. // if(i->first == subID)
  1783. // killedAmount += i->second;
  1784. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1785. MetaString ms;
  1786. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1787. pom = 174 + 3*pom + 1;
  1788. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1789. cb->setHoverName(id,&ms);
  1790. }
  1791. }
  1792. void CGCreature::initObj()
  1793. {
  1794. blockVisit = true;
  1795. switch(character)
  1796. {
  1797. case 0:
  1798. character = 0;
  1799. break;
  1800. case 1:
  1801. character = 1 + ran()%7;
  1802. break;
  1803. case 2:
  1804. character = 1 + ran()%10;
  1805. break;
  1806. case 3:
  1807. character = 4 + ran()%7;
  1808. break;
  1809. case 4:
  1810. character = 10;
  1811. break;
  1812. }
  1813. army.slots[0].first = subID;
  1814. si32 &amount = army.slots[0].second;
  1815. CCreature &c = VLC->creh->creatures[subID];
  1816. if(!amount)
  1817. if(c.ammMax == c.ammMin)
  1818. amount = c.ammMax;
  1819. else
  1820. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1821. MetaString ms;
  1822. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1823. pom = 174 + 3*pom + 1;
  1824. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1825. ms.toString(hoverName);
  1826. }
  1827. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1828. {
  1829. double hlp = h->getTotalStrength() / getArmyStrength();
  1830. if(!character) //compliant creatures will always join
  1831. return 0;
  1832. else if(allowJoin)//test for joining
  1833. {
  1834. int factor;
  1835. if(hlp >= 7)
  1836. factor = 11;
  1837. else if(hlp >= 1)
  1838. factor = (int)(2*(hlp-1));
  1839. else if(hlp >= 0.5)
  1840. factor = -1;
  1841. else if(hlp >= 0.333)
  1842. factor = -2;
  1843. else
  1844. factor = -3;
  1845. int sympathy = 0;
  1846. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1847. myKindCres.insert(subID); //we
  1848. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1849. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1850. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1851. myKindCres.insert(i->idNumber);
  1852. int count = 0, //how many creatures of our kind has hero
  1853. totalCount = 0;
  1854. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1855. {
  1856. if(vstd::contains(myKindCres,i->second.first))
  1857. count += i->second.second;
  1858. totalCount += i->second.second;
  1859. }
  1860. if(count*2 > totalCount)
  1861. sympathy++;
  1862. if(count)
  1863. sympathy++;
  1864. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1865. if(charisma >= character) //creatures might join...
  1866. {
  1867. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1868. return 0; //join for free
  1869. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1870. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1871. }
  1872. }
  1873. //we are still here - creatures not joined heroes, test for fleeing
  1874. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1875. //TODO: should be deterministic (will be needed for Vision spell)
  1876. int hlp2 = (int) (hlp - 2)*1000;
  1877. if(!neverFlees
  1878. && hlp2 >= 0
  1879. && rand()%2000 < hlp2
  1880. )
  1881. return -1; //flee
  1882. else
  1883. return -2; //fight
  1884. }
  1885. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1886. {
  1887. if(pursue)
  1888. {
  1889. fight(h);
  1890. }
  1891. else
  1892. {
  1893. cb->removeObject(id);
  1894. }
  1895. }
  1896. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1897. {
  1898. if(!accept)
  1899. {
  1900. if(takenAction(h,false) == -1) //they flee
  1901. {
  1902. flee(h);
  1903. }
  1904. else //they fight
  1905. {
  1906. InfoWindow iw;
  1907. iw.player = h->tempOwner;
  1908. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1909. cb->showInfoDialog(&iw);
  1910. fight(h);
  1911. }
  1912. }
  1913. else //accepted
  1914. {
  1915. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1916. {
  1917. InfoWindow iw;
  1918. iw.player = h->tempOwner;
  1919. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1920. cb->showInfoDialog(&iw);
  1921. //act as if player refused
  1922. joinDecision(h,cost,false);
  1923. return;
  1924. }
  1925. //take gold
  1926. if(cost)
  1927. cb->giveResource(h->tempOwner,6,-cost);
  1928. int slot = h->army.getSlotFor(subID);
  1929. if(slot >= 0) //there is place
  1930. {
  1931. //add creatures
  1932. SetGarrisons sg;
  1933. sg.garrs[h->id] = h->army;
  1934. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1935. {
  1936. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1937. }
  1938. else //add as a new stack
  1939. {
  1940. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1941. }
  1942. cb->sendAndApply(&sg);
  1943. cb->removeObject(id);
  1944. }
  1945. else
  1946. {
  1947. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1948. }
  1949. }
  1950. }
  1951. void CGCreature::fight( const CGHeroInstance *h ) const
  1952. {
  1953. cb->startBattleI(h, this, false, boost::bind(&CGCreature::endBattle,this,_1));
  1954. }
  1955. void CGCreature::flee( const CGHeroInstance * h ) const
  1956. {
  1957. BlockingDialog ynd(true,false);
  1958. ynd.player = h->tempOwner;
  1959. ynd.text << std::pair<ui8,ui32>(11,91);
  1960. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1961. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1962. }
  1963. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1964. {
  1965. if(subID == 7) //TODO: support for abandoned mine
  1966. return;
  1967. if(h->tempOwner == tempOwner) //we're visiting our mine
  1968. {
  1969. cb->showGarrisonDialog(id,h->id,0);
  1970. return;
  1971. }
  1972. //TODO: check if mine is guarded
  1973. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1974. MetaString ms;
  1975. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1976. cb->setHoverName(id,&ms);
  1977. int vv=1; //amount of resource per turn
  1978. if (subID==0 || subID==2)
  1979. vv++;
  1980. else if (subID==6)
  1981. vv = 1000;
  1982. InfoWindow iw;
  1983. iw.soundID = soundBase::FLAGMINE;
  1984. iw.text << std::pair<ui8,ui32>(10,subID);
  1985. iw.player = h->tempOwner;
  1986. iw.components.push_back(Component(2,subID,vv,-1));
  1987. cb->showInfoDialog(&iw);
  1988. }
  1989. void CGMine::newTurn() const
  1990. {
  1991. if(cb->getDate() == 1)
  1992. return;
  1993. if (tempOwner == NEUTRAL_PLAYER)
  1994. return;
  1995. int vv = 1;
  1996. if (subID==0 || subID==2)
  1997. vv++;
  1998. else if (subID==6)
  1999. vv = 1000;
  2000. cb->giveResource(tempOwner,subID,vv);
  2001. }
  2002. void CGMine::initObj()
  2003. {
  2004. MetaString ms;
  2005. ms << std::pair<ui8,ui32>(9,subID);
  2006. if(tempOwner >= PLAYER_LIMIT)
  2007. tempOwner = NEUTRAL_PLAYER;
  2008. else
  2009. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2010. ms.toString(hoverName);
  2011. }
  2012. void CGResource::initObj()
  2013. {
  2014. blockVisit = true;
  2015. hoverName = VLC->generaltexth->restypes[subID];
  2016. if(!amount)
  2017. {
  2018. switch(subID)
  2019. {
  2020. case 6:
  2021. amount = 500 + (rand()%6)*100;
  2022. break;
  2023. case 0: case 2:
  2024. amount = 6 + (rand()%5);
  2025. break;
  2026. default:
  2027. amount = 3 + (rand()%3);
  2028. break;
  2029. }
  2030. }
  2031. }
  2032. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2033. {
  2034. if(army.slots.size())
  2035. {
  2036. if(message.size())
  2037. {
  2038. BlockingDialog ynd(true,false);
  2039. ynd.player = h->getOwner();
  2040. ynd.text << message;
  2041. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2042. }
  2043. else
  2044. {
  2045. fightForRes(1,h);
  2046. }
  2047. }
  2048. else
  2049. {
  2050. if(message.length())
  2051. {
  2052. InfoWindow iw;
  2053. iw.player = h->tempOwner;
  2054. iw.text << message;
  2055. cb->showInfoDialog(&iw);
  2056. }
  2057. collectRes(h->getOwner());
  2058. }
  2059. }
  2060. void CGResource::collectRes( int player ) const
  2061. {
  2062. cb->giveResource(player,subID,amount);
  2063. ShowInInfobox sii;
  2064. sii.player = player;
  2065. sii.c = Component(2,subID,amount,0);
  2066. sii.text << std::pair<ui8,ui32>(11,113);
  2067. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2068. cb->showCompInfo(&sii);
  2069. cb->removeObject(id);
  2070. }
  2071. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2072. {
  2073. if(agreed)
  2074. cb->startBattleI(h, this, false, boost::bind(&CGResource::endBattle,this,_1,h));
  2075. }
  2076. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2077. {
  2078. if(result->winner == 0) //attacker won
  2079. collectRes(h->getOwner());
  2080. }
  2081. void CGVisitableOPW::newTurn() const
  2082. {
  2083. if (cb->getDate(1)==1) //first day of week
  2084. {
  2085. cb->setObjProperty(id,5,false);
  2086. MetaString ms; //set text to "not visited"
  2087. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2088. cb->setHoverName(id,&ms);
  2089. }
  2090. }
  2091. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2092. {
  2093. int mid, sound = 0;
  2094. switch (ID)
  2095. {
  2096. case 55: //mystical garden
  2097. sound = soundBase::experience;
  2098. mid = 92;
  2099. break;
  2100. case 112://windmill
  2101. sound = soundBase::GENIE;
  2102. mid = 170;
  2103. break;
  2104. case 109://waterwheel
  2105. sound = soundBase::GENIE;
  2106. mid = 164;
  2107. break;
  2108. }
  2109. if (visited)
  2110. {
  2111. if (ID!=112)
  2112. mid++;
  2113. else
  2114. mid--;
  2115. InfoWindow iw;
  2116. iw.soundID = sound;
  2117. iw.player = h->tempOwner;
  2118. iw.text << std::pair<ui8,ui32>(11,mid);
  2119. cb->showInfoDialog(&iw);
  2120. }
  2121. else
  2122. {
  2123. int type, sub, val;
  2124. type = 2;
  2125. switch (ID)
  2126. {
  2127. case 55:
  2128. if (rand()%2)
  2129. {
  2130. sub = 5;
  2131. val = 5;
  2132. }
  2133. else
  2134. {
  2135. sub = 6;
  2136. val = 500;
  2137. }
  2138. break;
  2139. case 112:
  2140. mid = 170;
  2141. sub = (rand() % 5) + 1;
  2142. val = (rand() % 4) + 3;
  2143. break;
  2144. case 109:
  2145. mid = 164;
  2146. sub = 6;
  2147. if(cb->getDate(0)<8)
  2148. val = 500;
  2149. else
  2150. val = 1000;
  2151. }
  2152. cb->giveResource(h->tempOwner,sub,val);
  2153. InfoWindow iw;
  2154. iw.soundID = sound;
  2155. iw.player = h->tempOwner;
  2156. iw.components.push_back(Component(type,sub,val,0));
  2157. iw.text << std::pair<ui8,ui32>(11,mid);
  2158. cb->showInfoDialog(&iw);
  2159. cb->setObjProperty(id,5,true);
  2160. MetaString ms; //set text to "visited"
  2161. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2162. cb->setHoverName(id,&ms);
  2163. }
  2164. }
  2165. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2166. {
  2167. if(what == 5)
  2168. visited = val;
  2169. }
  2170. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2171. {
  2172. int destinationid=-1;
  2173. switch(ID)
  2174. {
  2175. case 43: //one way - find corresponding exit monolith
  2176. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2177. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2178. else
  2179. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2180. break;
  2181. case 45: //two way monolith - pick any other one
  2182. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2183. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2184. else
  2185. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2186. break;
  2187. case 103: //find nearest subterranean gate on the other level
  2188. {
  2189. std::pair<int,double> best(-1,150000); //pair<id,dist>
  2190. for(int i=0; i<objs[103][0].size(); i++)
  2191. {
  2192. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  2193. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  2194. if(hlp<best.second)
  2195. {
  2196. best.first = objs[103][0][i];
  2197. best.second = hlp;
  2198. }
  2199. }
  2200. if(best.first<0)
  2201. return;
  2202. else
  2203. destinationid = best.first;
  2204. break;
  2205. }
  2206. }
  2207. if(destinationid < 0)
  2208. {
  2209. tlog2 << "Cannot find exit... :( \n";
  2210. return;
  2211. }
  2212. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2213. true);
  2214. }
  2215. void CGTeleport::initObj()
  2216. {
  2217. objs[ID][subID].push_back(id);
  2218. }
  2219. void CGArtifact::initObj()
  2220. {
  2221. blockVisit = true;
  2222. if(ID == 5)
  2223. hoverName = VLC->arth->artifacts[subID].Name();
  2224. }
  2225. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2226. {
  2227. if(!army.slots.size())
  2228. {
  2229. if(ID == 5)
  2230. {
  2231. InfoWindow iw;
  2232. iw.soundID = soundBase::treasure;
  2233. iw.player = h->tempOwner;
  2234. iw.components.push_back(Component(4,subID,0,0));
  2235. if(message.length())
  2236. iw.text << message;
  2237. else
  2238. iw.text << std::pair<ui8,ui32>(12,subID);
  2239. cb->showInfoDialog(&iw);
  2240. }
  2241. pick(h);
  2242. }
  2243. else
  2244. {
  2245. if(message.size())
  2246. {
  2247. BlockingDialog ynd(true,false);
  2248. ynd.player = h->getOwner();
  2249. ynd.text << message;
  2250. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2251. }
  2252. else
  2253. {
  2254. fightForArt(0,h);
  2255. }
  2256. }
  2257. }
  2258. void CGArtifact::pick(const CGHeroInstance * h) const
  2259. {
  2260. if(ID == 5) //Artifact
  2261. {
  2262. cb->giveHeroArtifact(subID,h->id,-2);
  2263. }
  2264. else if(ID == 93) // Spell scroll
  2265. {
  2266. //TODO: support for the spell scroll
  2267. }
  2268. cb->removeObject(id);
  2269. }
  2270. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2271. {
  2272. if(agreed)
  2273. cb->startBattleI(h, this, false, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2274. }
  2275. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2276. {
  2277. if(result->winner == 0) //attacker won
  2278. pick(h);
  2279. }
  2280. void CGPickable::initObj()
  2281. {
  2282. blockVisit = true;
  2283. switch(ID)
  2284. {
  2285. case 12: //campfire
  2286. val2 = (ran()%3) + 4; //4 - 6
  2287. val1 = val2 * 100;
  2288. type = ran()%6; //given resource
  2289. break;
  2290. case 29: //floatsam
  2291. switch(type = ran()%4)
  2292. {
  2293. case 0:
  2294. val1 = val2 = 0;
  2295. break;
  2296. case 1:
  2297. val1 = 5;
  2298. val2 = 0;
  2299. break;
  2300. case 2:
  2301. val1 = 5;
  2302. val2 = 200;
  2303. break;
  2304. case 3:
  2305. val1 = 10;
  2306. val2 = 500;
  2307. break;
  2308. }
  2309. break;
  2310. case 82: //sea chest
  2311. {
  2312. int hlp = ran()%100;
  2313. if(hlp < 20)
  2314. {
  2315. val1 = 0;
  2316. type = 0;
  2317. }
  2318. else if(hlp < 90)
  2319. {
  2320. val1 = 1500;
  2321. type = 2;
  2322. }
  2323. else
  2324. {
  2325. val1 = 1000;
  2326. val2 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2327. type = 1;
  2328. }
  2329. }
  2330. break;
  2331. case 86: //Shipwreck Survivor
  2332. {
  2333. int hlp = ran()%100;
  2334. if(hlp < 55)
  2335. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2336. else if(hlp < 75)
  2337. val1 = VLC->arth->minors[ran()%VLC->arth->minors.size()]->id;
  2338. else if(hlp < 95)
  2339. val1 = VLC->arth->majors[ran()%VLC->arth->majors.size()]->id;
  2340. else
  2341. val1 = VLC->arth->relics[ran()%VLC->arth->relics.size()]->id;
  2342. }
  2343. break;
  2344. case 101: //treasure chest
  2345. {
  2346. int hlp = ran()%100;
  2347. if(hlp >= 95)
  2348. {
  2349. type = 1;
  2350. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2351. return;
  2352. }
  2353. else if (hlp >= 65)
  2354. {
  2355. val1 = 2000;
  2356. }
  2357. else if(hlp >= 33)
  2358. {
  2359. val1 = 1500;
  2360. }
  2361. else
  2362. {
  2363. val1 = 1000;
  2364. }
  2365. val2 = val1 - 500;
  2366. type = 0;
  2367. break;
  2368. }
  2369. }
  2370. }
  2371. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2372. {
  2373. switch(ID)
  2374. {
  2375. case 12: //campfire
  2376. {
  2377. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2378. cb->giveResource(h->tempOwner,6,val1);//gold
  2379. InfoWindow iw;
  2380. iw.soundID = soundBase::experience;
  2381. iw.player = h->tempOwner;
  2382. iw.components.push_back(Component(2,6,val1,0));
  2383. iw.components.push_back(Component(2,type,val2,0));
  2384. iw.text << std::pair<ui8,ui32>(11,23);
  2385. cb->showInfoDialog(&iw);
  2386. break;
  2387. }
  2388. case 29: //flotsam
  2389. {
  2390. cb->giveResource(h->tempOwner,0,val1); //wood
  2391. cb->giveResource(h->tempOwner,6,val2);//gold
  2392. InfoWindow iw;
  2393. iw.soundID = soundBase::GENIE;
  2394. iw.player = h->tempOwner;
  2395. if(val1)
  2396. iw.components.push_back(Component(2,0,val1,0));
  2397. if(val2)
  2398. iw.components.push_back(Component(2,6,val2,0));
  2399. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2400. cb->showInfoDialog(&iw);
  2401. break;
  2402. }
  2403. case 82: //Sea Chest
  2404. {
  2405. InfoWindow iw;
  2406. iw.soundID = soundBase::chest;
  2407. iw.player = h->tempOwner;
  2408. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2409. if(val1) //there is gold
  2410. {
  2411. iw.components.push_back(Component(2,6,val1,0));
  2412. cb->giveResource(h->tempOwner,6,val1);
  2413. }
  2414. if(type == 1) //art
  2415. {
  2416. //TODO: what if no space in backpack?
  2417. iw.components.push_back(Component(4, val2, 1, 0));
  2418. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2419. cb->giveHeroArtifact(val2,h->id,-2);
  2420. }
  2421. cb->showInfoDialog(&iw);
  2422. break;
  2423. }
  2424. case 86: //Shipwreck Survivor
  2425. {
  2426. //TODO: what if no space in backpack?
  2427. InfoWindow iw;
  2428. iw.soundID = soundBase::experience;
  2429. iw.player = h->tempOwner;
  2430. iw.components.push_back(Component(4,val1,1,0));
  2431. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2432. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2433. cb->giveHeroArtifact(val1,h->id,-2);
  2434. cb->showInfoDialog(&iw);
  2435. break;
  2436. }
  2437. case 101: //treasure chest
  2438. {
  2439. if (subID) //not OH3 treasure chest
  2440. {
  2441. tlog2 << "Not supported WoG treasure chest!\n";
  2442. return;
  2443. }
  2444. if(type) //there is an artifact
  2445. {
  2446. cb->giveHeroArtifact(val1,h->id,-2);
  2447. InfoWindow iw;
  2448. iw.soundID = soundBase::treasure;
  2449. iw.player = h->tempOwner;
  2450. iw.components.push_back(Component(4,val1,1,0));
  2451. iw.text << std::pair<ui8,ui32>(11,145);
  2452. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2453. cb->showInfoDialog(&iw);
  2454. break;
  2455. }
  2456. else
  2457. {
  2458. BlockingDialog sd(false,true);
  2459. sd.player = h->tempOwner;
  2460. sd.text << std::pair<ui8,ui32>(11,146);
  2461. sd.components.push_back(Component(2,6,val1,0));
  2462. sd.components.push_back(Component(5,0,val2,0));
  2463. sd.soundID = soundBase::chest;
  2464. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2465. cb->showBlockingDialog(&sd,fun);
  2466. return;
  2467. }
  2468. }
  2469. }
  2470. cb->removeObject(id);
  2471. }
  2472. void CGPickable::chosen( int which, int heroID ) const
  2473. {
  2474. switch(which)
  2475. {
  2476. case 1: //player pick gold
  2477. cb->giveResource(cb->getOwner(heroID),6,val1);
  2478. break;
  2479. case 2: //player pick exp
  2480. cb->changePrimSkill(heroID, 4, val2);
  2481. break;
  2482. default:
  2483. throw std::string("Unhandled treasure choice");
  2484. }
  2485. cb->removeObject(id);
  2486. }
  2487. void CGWitchHut::initObj()
  2488. {
  2489. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2490. }
  2491. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2492. {
  2493. InfoWindow iw;
  2494. iw.soundID = soundBase::gazebo;
  2495. iw.player = h->getOwner();
  2496. if(!hasVisited(h->tempOwner))
  2497. cb->setObjProperty(id,10,h->tempOwner);
  2498. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2499. {
  2500. iw.text << std::pair<ui8,ui32>(11,172);
  2501. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2502. }
  2503. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2504. {
  2505. iw.text << std::pair<ui8,ui32>(11,173);
  2506. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2507. }
  2508. else //give sec skill
  2509. {
  2510. iw.components.push_back(Component(1, ability, 1, 0));
  2511. iw.text << std::pair<ui8,ui32>(11,171);
  2512. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2513. cb->changeSecSkill(h->id,ability,1,true);
  2514. }
  2515. cb->showInfoDialog(&iw);
  2516. }
  2517. const std::string & CGWitchHut::getHoverText() const
  2518. {
  2519. hoverName = VLC->generaltexth->names[ID];
  2520. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2521. {
  2522. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2523. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2524. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2525. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2526. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2527. }
  2528. return hoverName;
  2529. }
  2530. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2531. {
  2532. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2533. int messageID, bonusType, bonusVal;
  2534. int bonusMove = 0, sound = -1;
  2535. InfoWindow iw;
  2536. iw.player = h->tempOwner;
  2537. GiveBonus gbonus;
  2538. gbonus.hid = h->id;
  2539. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2540. gbonus.bonus.source = HeroBonus::OBJECT;
  2541. gbonus.bonus.id = ID;
  2542. switch(ID)
  2543. {
  2544. case 11: //buoy
  2545. messageID = 21;
  2546. sound = soundBase::MORALE;
  2547. gbonus.bonus.type = HeroBonus::MORALE;
  2548. gbonus.bonus.val = +1;
  2549. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2550. break;
  2551. case 14: //swan pond
  2552. messageID = 29;
  2553. sound = soundBase::LUCK;
  2554. gbonus.bonus.type = HeroBonus::LUCK;
  2555. gbonus.bonus.val = 2;
  2556. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2557. bonusMove = -h->movement;
  2558. break;
  2559. case 28: //Faerie Ring
  2560. messageID = 49;
  2561. sound = soundBase::LUCK;
  2562. gbonus.bonus.type = HeroBonus::LUCK;
  2563. gbonus.bonus.val = 1;
  2564. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2565. break;
  2566. case 30: //fountain of fortune
  2567. messageID = 55;
  2568. sound = soundBase::LUCK;
  2569. gbonus.bonus.type = HeroBonus::LUCK;
  2570. gbonus.bonus.val = rand()%5 - 1;
  2571. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2572. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2573. break;
  2574. case 38: //idol of fortune
  2575. messageID = 62;
  2576. iw.soundID = soundBase::experience;
  2577. if(cb->getDate(1) == 7) //7th day of week
  2578. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2579. else
  2580. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2581. gbonus.bonus.val = 1;
  2582. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2583. break;
  2584. case 52: //Mermaid
  2585. messageID = 83;
  2586. sound = soundBase::LUCK;
  2587. gbonus.bonus.type = HeroBonus::LUCK;
  2588. gbonus.bonus.val = 1;
  2589. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2590. break;
  2591. case 64: //Rally Flag
  2592. sound = soundBase::MORALE;
  2593. messageID = 111;
  2594. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2595. gbonus.bonus.val = 1;
  2596. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2597. bonusMove = 400;
  2598. break;
  2599. case 56: //oasis
  2600. messageID = 95;
  2601. gbonus.bonus.type = HeroBonus::MORALE;
  2602. gbonus.bonus.val = 1;
  2603. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2604. bonusMove = 800;
  2605. break;
  2606. case 96: //temple
  2607. messageID = 140;
  2608. iw.soundID = soundBase::temple;
  2609. gbonus.bonus.type = HeroBonus::MORALE;
  2610. if(cb->getDate(1)==7) //sunday
  2611. {
  2612. gbonus.bonus.val = 2;
  2613. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2614. }
  2615. else
  2616. {
  2617. gbonus.bonus.val = 1;
  2618. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2619. }
  2620. break;
  2621. case 110://Watering Hole
  2622. sound = soundBase::MORALE;
  2623. messageID = 166;
  2624. gbonus.bonus.type = HeroBonus::MORALE;
  2625. gbonus.bonus.val = 1;
  2626. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2627. bonusMove = 400;
  2628. break;
  2629. case 31: //Fountain of Youth
  2630. sound = soundBase::MORALE;
  2631. messageID = 57;
  2632. gbonus.bonus.type = HeroBonus::MORALE;
  2633. gbonus.bonus.val = 1;
  2634. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2635. bonusMove = 400;
  2636. break;
  2637. case 94: //Stables TODO: upgrade Cavaliers
  2638. sound = soundBase::horse20;
  2639. std::set<ui32> slots;
  2640. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2641. {
  2642. if(i->second.first == 10)
  2643. slots.insert(i->first);
  2644. }
  2645. if (!slots.empty())
  2646. {
  2647. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2648. {
  2649. UpgradeCreature uc(*i, id, 11);
  2650. //uc.applyGh (&gh);
  2651. }
  2652. }
  2653. messageID = 137;
  2654. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  2655. gbonus.bonus.val = 600;
  2656. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  2657. break;
  2658. }
  2659. if(visited)
  2660. {
  2661. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  2662. messageID--;
  2663. else
  2664. messageID++;
  2665. }
  2666. else
  2667. {
  2668. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2669. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2670. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2671. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2672. cb->giveHeroBonus(&gbonus);
  2673. if(bonusMove) //swan pond - take all move points
  2674. {
  2675. SetMovePoints smp;
  2676. smp.hid = h->id;
  2677. smp.val = h->movement + bonusMove;
  2678. cb->setMovePoints(&smp);
  2679. }
  2680. }
  2681. iw.soundID = sound;
  2682. iw.text << std::pair<ui8,ui32>(11,messageID);
  2683. cb->showInfoDialog(&iw);
  2684. }
  2685. const std::string & CGBonusingObject::getHoverText() const
  2686. {
  2687. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2688. hoverName = VLC->generaltexth->names[ID];
  2689. if(h)
  2690. {
  2691. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2692. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2693. else
  2694. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2695. }
  2696. return hoverName;
  2697. }
  2698. void CGBonusingObject::initObj()
  2699. {
  2700. if(ID == 11 || ID == 52) //Buoy / Mermaid
  2701. {
  2702. blockVisit = true;
  2703. }
  2704. }
  2705. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2706. {
  2707. int message;
  2708. InfoWindow iw;
  2709. iw.soundID = soundBase::faerie;
  2710. iw.player = h->tempOwner;
  2711. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2712. {
  2713. message = 78;
  2714. }
  2715. else if(h->mana < h->manaLimit())
  2716. {
  2717. giveDummyBonus(h->id);
  2718. cb->setManaPoints(h->id,h->manaLimit());
  2719. message = 77;
  2720. }
  2721. else
  2722. {
  2723. message = 79;
  2724. }
  2725. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2726. cb->showInfoDialog(&iw);
  2727. }
  2728. const std::string & CGMagicWell::getHoverText() const
  2729. {
  2730. getNameVis(hoverName);
  2731. return hoverName;
  2732. }
  2733. void CGPandoraBox::initObj()
  2734. {
  2735. blockVisit = true;
  2736. }
  2737. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  2738. {
  2739. BlockingDialog bd (true, false);
  2740. bd.player = h->getOwner();
  2741. bd.soundID = soundBase::QUEST;
  2742. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  2743. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  2744. }
  2745. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  2746. {
  2747. if (accept)
  2748. {
  2749. if (army) //if pandora's box is protected by army
  2750. {
  2751. InfoWindow iw;
  2752. iw.player = h->tempOwner;
  2753. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2754. cb->showInfoDialog(&iw);
  2755. cb->startBattleI(h, this, false, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  2756. }
  2757. else if (message.size() == 0 && resources.size() == 0
  2758. && primskills.size() == 0 && abilities.size() == 0
  2759. && abilityLevels.size() == 0 && artifacts.size() == 0
  2760. && spells.size() == 0 && creatures == 0
  2761. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  2762. {
  2763. InfoWindow iw;
  2764. iw.player = h->tempOwner;
  2765. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  2766. cb->showInfoDialog(&iw);
  2767. }
  2768. else //if it gives something without battle
  2769. {
  2770. giveContents (h, false);
  2771. }
  2772. }
  2773. }
  2774. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2775. {
  2776. if(result->winner)
  2777. return;
  2778. giveContents(h,true);
  2779. }
  2780. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2781. {
  2782. InfoWindow iw;
  2783. iw.player = h->getOwner();
  2784. bool changesPrimSkill = false;
  2785. for (int i = 0; i < primskills.size(); i++)
  2786. {
  2787. if(primskills[i])
  2788. {
  2789. changesPrimSkill = true;
  2790. break;
  2791. }
  2792. }
  2793. if(gainedExp || changesPrimSkill || abilities.size())
  2794. {
  2795. getText(iw,afterBattle,175,h);
  2796. if(gainedExp)
  2797. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2798. for(int i=0; i<primskills.size(); i++)
  2799. if(primskills[i])
  2800. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2801. for(int i=0; i<abilities.size(); i++)
  2802. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2803. cb->showInfoDialog(&iw);
  2804. //give exp
  2805. if(gainedExp)
  2806. cb->changePrimSkill(h->id,4,gainedExp,false);
  2807. //give prim skills
  2808. for(int i=0; i<primskills.size(); i++)
  2809. if(primskills[i])
  2810. cb->changePrimSkill(h->id,i,primskills[i],false);
  2811. //give sec skills
  2812. for(int i=0; i<abilities.size(); i++)
  2813. {
  2814. int curLev = h->getSecSkillLevel(abilities[i]);
  2815. if(curLev && curLev < abilityLevels[i]
  2816. || h->secSkills.size() < SKILL_PER_HERO )
  2817. {
  2818. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2819. }
  2820. }
  2821. }
  2822. if(spells.size())
  2823. {
  2824. std::set<ui32> spellsToGive;
  2825. iw.components.clear();
  2826. for(int i=0; i<spells.size(); i++)
  2827. {
  2828. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2829. spellsToGive.insert(spells[i]);
  2830. }
  2831. if(spellsToGive.size())
  2832. {
  2833. cb->changeSpells(h->id,true,spellsToGive);
  2834. cb->showInfoDialog(&iw);
  2835. }
  2836. }
  2837. if(manaDiff)
  2838. {
  2839. getText(iw,afterBattle,manaDiff,176,177,h);
  2840. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2841. cb->showInfoDialog(&iw);
  2842. cb->setManaPoints(h->id, h->mana + manaDiff);
  2843. }
  2844. if(moraleDiff)
  2845. {
  2846. getText(iw,afterBattle,moraleDiff,178,179,h);
  2847. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2848. cb->showInfoDialog(&iw);
  2849. GiveBonus gb;
  2850. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2851. gb.hid = h->id;
  2852. cb->giveHeroBonus(&gb);
  2853. }
  2854. if(luckDiff)
  2855. {
  2856. getText(iw,afterBattle,luckDiff,180,181,h);
  2857. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2858. cb->showInfoDialog(&iw);
  2859. GiveBonus gb;
  2860. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2861. gb.hid = h->id;
  2862. cb->giveHeroBonus(&gb);
  2863. }
  2864. iw.components.clear();
  2865. iw.text.clear();
  2866. for(int i=0; i<resources.size(); i++)
  2867. {
  2868. if(resources[i] < 0)
  2869. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2870. }
  2871. if(iw.components.size())
  2872. {
  2873. getText(iw,afterBattle,182,h);
  2874. cb->showInfoDialog(&iw);
  2875. }
  2876. iw.components.clear();
  2877. iw.text.clear();
  2878. for(int i=0; i<resources.size(); i++)
  2879. {
  2880. if(resources[i] > 0)
  2881. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2882. }
  2883. if(iw.components.size())
  2884. {
  2885. getText(iw,afterBattle,183,h);
  2886. cb->showInfoDialog(&iw);
  2887. }
  2888. iw.components.clear();
  2889. getText(iw,afterBattle,183,h);
  2890. for(int i=0; i<artifacts.size(); i++)
  2891. {
  2892. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2893. if(iw.components.size() >= 14)
  2894. {
  2895. cb->showInfoDialog(&iw);
  2896. iw.components.clear();
  2897. }
  2898. }
  2899. if(iw.components.size())
  2900. {
  2901. cb->showInfoDialog(&iw);
  2902. }
  2903. for(int i=0; i<resources.size(); i++)
  2904. if(resources[i])
  2905. cb->giveResource(h->getOwner(),i,resources[i]);
  2906. for(int i=0; i<artifacts.size(); i++)
  2907. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2908. //show dialog with given creatures
  2909. iw.components.clear();
  2910. iw.text.clear();
  2911. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2912. {
  2913. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2914. }
  2915. if(iw.components.size())
  2916. {
  2917. if(afterBattle)
  2918. {
  2919. if(iw.components.front().val == 1)
  2920. {
  2921. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2922. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2923. }
  2924. else
  2925. {
  2926. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2927. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2928. }
  2929. iw.text.addReplacement(h->name);
  2930. }
  2931. else
  2932. {
  2933. iw.text << message;
  2934. afterBattle = true;
  2935. }
  2936. cb->showInfoDialog(&iw);
  2937. }
  2938. //check if creatures can be moved to hero army
  2939. CCreatureSet heroArmy = h->army;
  2940. CCreatureSet ourArmy = creatures;
  2941. while(ourArmy)
  2942. {
  2943. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2944. if(slot < 0)
  2945. break;
  2946. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2947. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2948. ourArmy.slots.erase(ourArmy.slots.begin());
  2949. }
  2950. if(!ourArmy) //all creatures can be moved to hero army - do that
  2951. {
  2952. SetGarrisons sg;
  2953. sg.garrs[h->id] = heroArmy;
  2954. cb->sendAndApply(&sg);
  2955. }
  2956. else //show garrison window and let player pick creatures
  2957. {
  2958. SetGarrisons sg;
  2959. sg.garrs[id] = creatures;
  2960. cb->sendAndApply(&sg);
  2961. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2962. }
  2963. if(!afterBattle && message.size())
  2964. {
  2965. iw.text << message;
  2966. cb->showInfoDialog(&iw);
  2967. }
  2968. cb->removeObject(id);
  2969. }
  2970. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2971. {
  2972. if(afterBattle || !message.size())
  2973. {
  2974. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2975. iw.text.addReplacement(h->name);
  2976. }
  2977. else
  2978. {
  2979. iw.text << message;
  2980. afterBattle = true;
  2981. }
  2982. }
  2983. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  2984. {
  2985. iw.components.clear();
  2986. iw.text.clear();
  2987. if(afterBattle || !message.size())
  2988. {
  2989. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2990. iw.text.addReplacement(h->name);
  2991. }
  2992. else
  2993. {
  2994. iw.text << message;
  2995. afterBattle = true;
  2996. }
  2997. }
  2998. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2999. {
  3000. if(!(availableFor & (1 << h->tempOwner)))
  3001. return;
  3002. if(cb->getPlayerSettings(h->tempOwner)->human)
  3003. {
  3004. if(humanActivate)
  3005. activated(h);
  3006. }
  3007. else if(computerActivate)
  3008. activated(h);
  3009. }
  3010. void CGEvent::activated( const CGHeroInstance * h ) const
  3011. {
  3012. if(army)
  3013. {
  3014. InfoWindow iw;
  3015. iw.player = h->tempOwner;
  3016. if(message.size())
  3017. iw.text << message;
  3018. else
  3019. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3020. cb->showInfoDialog(&iw);
  3021. cb->startBattleI(h, this, false, boost::bind(&CGEvent::endBattle,this,h,_1));
  3022. }
  3023. else
  3024. {
  3025. giveContents(h,false);
  3026. }
  3027. }
  3028. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  3029. {
  3030. InfoWindow iw;
  3031. iw.soundID = soundBase::LIGHTHOUSE;
  3032. iw.player = h->tempOwner;
  3033. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  3034. cb->showInfoDialog(&iw);
  3035. FoWChange fw;
  3036. fw.player = h->tempOwner;
  3037. fw.mode = 1;
  3038. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  3039. cb->sendAndApply(&fw);
  3040. }
  3041. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  3042. {
  3043. if(spell == 255)
  3044. {
  3045. tlog1 << "Not initialized shrine visited!\n";
  3046. return;
  3047. }
  3048. if(!hasVisited(h->tempOwner))
  3049. cb->setObjProperty(id,10,h->tempOwner);
  3050. InfoWindow iw;
  3051. iw.soundID = soundBase::temple;
  3052. iw.player = h->getOwner();
  3053. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  3054. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  3055. iw.text << ".";
  3056. if(!h->getArt(17)) //no spellbook
  3057. {
  3058. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  3059. }
  3060. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  3061. {
  3062. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  3063. }
  3064. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  3065. {
  3066. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  3067. }
  3068. else //give spell
  3069. {
  3070. std::set<ui32> spells;
  3071. spells.insert(spell);
  3072. cb->changeSpells(h->id,true,spells);
  3073. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  3074. }
  3075. cb->showInfoDialog(&iw);
  3076. }
  3077. void CGShrine::initObj()
  3078. {
  3079. if(spell == 255) //spell not set
  3080. {
  3081. int level = ID-87;
  3082. std::vector<ui32> possibilities;
  3083. //add all allowed spells of wanted level
  3084. for(int i=0; i<SPELLS_QUANTITY; i++)
  3085. {
  3086. if(VLC->spellh->spells[i].level == level
  3087. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  3088. {
  3089. possibilities.push_back(VLC->spellh->spells[i].id);
  3090. }
  3091. }
  3092. if(!possibilities.size())
  3093. {
  3094. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  3095. return;
  3096. }
  3097. spell = possibilities[ran() % possibilities.size()];
  3098. }
  3099. }
  3100. const std::string & CGShrine::getHoverText() const
  3101. {
  3102. hoverName = VLC->generaltexth->names[ID];
  3103. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3104. {
  3105. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  3106. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  3107. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3108. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  3109. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  3110. }
  3111. return hoverName;
  3112. }
  3113. void CGSignBottle::initObj()
  3114. {
  3115. //if no text is set than we pick random from the predefined ones
  3116. if(!message.size())
  3117. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  3118. if(ID == 59)
  3119. {
  3120. blockVisit = true;
  3121. }
  3122. }
  3123. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  3124. {
  3125. InfoWindow iw;
  3126. iw.soundID = soundBase::STORE;
  3127. iw.player = h->getOwner();
  3128. iw.text << message;
  3129. cb->showInfoDialog(&iw);
  3130. if(ID == 59)
  3131. cb->removeObject(id);
  3132. }
  3133. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  3134. {
  3135. }
  3136. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  3137. {
  3138. int type = bonusType;
  3139. int bid = bonusID;
  3140. //check if the bonus if applicable, if not - give primary skill (always possible)
  3141. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  3142. if((type == 1
  3143. && ((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)
  3144. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  3145. {
  3146. type = 0;
  3147. bid = ran() % PRIMARY_SKILLS;
  3148. }
  3149. InfoWindow iw;
  3150. iw.soundID = soundBase::gazebo;
  3151. iw.player = h->getOwner();
  3152. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  3153. switch (type)
  3154. {
  3155. case 0:
  3156. cb->changePrimSkill(h->id,bid,+1);
  3157. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  3158. break;
  3159. case 1:
  3160. cb->changeSecSkill(h->id,bid,+1);
  3161. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  3162. break;
  3163. case 2:
  3164. {
  3165. std::set<ui32> hlp;
  3166. hlp.insert(bid);
  3167. cb->changeSpells(h->id,true,hlp);
  3168. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  3169. }
  3170. break;
  3171. default:
  3172. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3173. return;
  3174. }
  3175. cb->showInfoDialog(&iw);
  3176. cb->removeObject(id);
  3177. }
  3178. void CGScholar::initObj()
  3179. {
  3180. blockVisit = true;
  3181. if(bonusType == 255)
  3182. {
  3183. bonusType = ran()%3;
  3184. switch(bonusType)
  3185. {
  3186. case 0:
  3187. bonusID = ran() % PRIMARY_SKILLS;
  3188. break;
  3189. case 1:
  3190. bonusID = ran() % SKILL_QUANTITY;
  3191. break;
  3192. case 2:
  3193. bonusID = ran() % SPELLS_QUANTITY;
  3194. break;
  3195. }
  3196. }
  3197. }
  3198. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3199. {
  3200. int sound = 0;
  3201. int txtid = -1;
  3202. switch(ID)
  3203. {
  3204. case 22: //Corpse
  3205. txtid = 37;
  3206. sound = soundBase::MYSTERY;
  3207. break;
  3208. case 39: //Lean To
  3209. sound = soundBase::GENIE;
  3210. txtid = 64;
  3211. break;
  3212. case 105://Wagon
  3213. sound = soundBase::GENIE;
  3214. txtid = 154;
  3215. break;
  3216. case 108:
  3217. break;
  3218. default:
  3219. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3220. return;
  3221. }
  3222. if(ID == 108)//Warrior's Tomb
  3223. {
  3224. //ask if player wants to search the Tomb
  3225. BlockingDialog bd(true, false);
  3226. sound = soundBase::GRAVEYARD;
  3227. bd.player = h->getOwner();
  3228. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3229. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3230. return;
  3231. }
  3232. InfoWindow iw;
  3233. iw.soundID = sound;
  3234. iw.player = h->getOwner();
  3235. if(players.size()) //we have been already visited...
  3236. {
  3237. txtid++;
  3238. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  3239. txtid++;
  3240. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3241. }
  3242. else //first visit - give bonus!
  3243. {
  3244. switch(artOrRes)
  3245. {
  3246. case 0:
  3247. txtid++;
  3248. break;
  3249. case 1: //art
  3250. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3251. cb->giveHeroArtifact(bonusType,h->id,-2);
  3252. break;
  3253. case 2: //res
  3254. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  3255. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  3256. break;
  3257. }
  3258. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3259. if(ID == 105 && artOrRes == 1)
  3260. {
  3261. iw.text.localStrings.back().second++;
  3262. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3263. }
  3264. }
  3265. cb->showInfoDialog(&iw);
  3266. cb->setObjProperty(id,10,h->getOwner());
  3267. }
  3268. const std::string & CGOnceVisitable::getHoverText() const
  3269. {
  3270. hoverName = VLC->generaltexth->names[ID] + " ";
  3271. hoverName += (hasVisited(cb->getCurrentPlayer())
  3272. ? (VLC->generaltexth->allTexts[352]) //visited
  3273. : ( VLC->generaltexth->allTexts[353])); //not visited
  3274. return hoverName;
  3275. }
  3276. void CGOnceVisitable::initObj()
  3277. {
  3278. switch(ID)
  3279. {
  3280. case 22: //Corpse
  3281. {
  3282. blockVisit = true;
  3283. int hlp = ran()%100;
  3284. if(hlp < 20)
  3285. {
  3286. artOrRes = 1;
  3287. std::vector<CArtifact*> arts;
  3288. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3289. bonusType = arts[ran() % arts.size()]->id;
  3290. }
  3291. else
  3292. {
  3293. artOrRes = 0;
  3294. }
  3295. }
  3296. break;
  3297. case 39: //Lean To
  3298. {
  3299. artOrRes = 2;
  3300. bonusType = ran()%6; //any basic resource without gold
  3301. bonusVal = ran()%4 + 1;
  3302. break;
  3303. }
  3304. case 108://Warrior's Tomb
  3305. {
  3306. artOrRes = 1;
  3307. std::vector<CArtifact*> arts;
  3308. int hlp = ran()%100;
  3309. if(hlp < 30)
  3310. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  3311. else if(hlp < 80)
  3312. cb->getAllowed(arts,CArtifact::ART_MINOR);
  3313. else if(hlp < 95)
  3314. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  3315. else
  3316. cb->getAllowed(arts,CArtifact::ART_RELIC);
  3317. bonusType = arts[ran() % arts.size()]->id;
  3318. }
  3319. break;
  3320. case 105://Wagon
  3321. {
  3322. int hlp = ran()%100;
  3323. if(hlp < 10)
  3324. {
  3325. artOrRes = 0; // nothing... :(
  3326. }
  3327. else if(hlp < 50) //minor or treasure art
  3328. {
  3329. artOrRes = 1;
  3330. std::vector<CArtifact*> arts;
  3331. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3332. bonusType = arts[ran() % arts.size()]->id;
  3333. }
  3334. else //2 - 5 of non-gold resource
  3335. {
  3336. artOrRes = 2;
  3337. bonusType = ran()%6;
  3338. bonusVal = ran()%4 + 2;
  3339. }
  3340. break;
  3341. }
  3342. }
  3343. }
  3344. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3345. {
  3346. if(accept)
  3347. {
  3348. InfoWindow iw;
  3349. iw.player = h->getOwner();
  3350. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3351. if(players.size()) //we've been already visited, player found nothing
  3352. {
  3353. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3354. }
  3355. else //first visit - give artifact
  3356. {
  3357. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3358. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3359. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3360. cb->giveHeroArtifact(bonusType,h->id,-2);
  3361. }
  3362. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3363. {
  3364. //ruin morale
  3365. GiveBonus gb;
  3366. gb.hid = h->id;
  3367. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3368. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3369. cb->giveHeroBonus(&gb);
  3370. }
  3371. }
  3372. }
  3373. void CBank::initObj()
  3374. {
  3375. index = 0;
  3376. switch (ID) //find apriopriate key
  3377. {
  3378. case 16: //bank
  3379. index = subID; break;
  3380. case 24: //derelict ship
  3381. index = 8;
  3382. case 25: //utopia
  3383. index = 10; break;
  3384. case 84: //crypt
  3385. index = 9; break;
  3386. case 85: //shipwreck
  3387. index = 7; break;
  3388. }
  3389. bc = NULL;
  3390. daycounter = 0;
  3391. multiplier = 1;
  3392. //reset();
  3393. }
  3394. void CBank::reset()
  3395. {
  3396. int val1 = ran()%100;
  3397. int chance = 0;
  3398. for (ui8 i = 1; i <= VLC->objh->banksInfo[index].size(); i++)
  3399. {
  3400. if (val1 < (chance += VLC->objh->banksInfo[index][i].chance))
  3401. cb->setObjProperty (id, 13, i);
  3402. }
  3403. artifacts.clear();
  3404. for (ui8 i = 1; i <= 4; i++)
  3405. {
  3406. for (ui8 j = 1; j <= bc->artifacts[i]; j++)
  3407. cb->setObjProperty (id, 18, i);
  3408. }
  3409. }
  3410. void CBank::setPropertyDer (ui8 what, ui32 val)
  3411. {
  3412. switch (what)
  3413. {
  3414. case 11: //daycounter
  3415. if (val == 0)
  3416. daycounter = 0;
  3417. else
  3418. daycounter++;
  3419. break;
  3420. case 12: //multiplier
  3421. multiplier = ((float)val)/100;
  3422. break;
  3423. case 13: //bank preset
  3424. bc = &VLC->objh->banksInfo[index][val];
  3425. break;
  3426. case 14:
  3427. reset();
  3428. break;
  3429. case 15:
  3430. bc = NULL;
  3431. break;
  3432. case 16:
  3433. artifacts.clear();
  3434. break;
  3435. case 18: //Artifacts
  3436. {
  3437. std::vector<CArtifact*> arts;
  3438. switch (val)
  3439. {
  3440. case 1:
  3441. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  3442. break;
  3443. case 2:
  3444. cb->getAllowed (arts, CArtifact::ART_MINOR);
  3445. break;
  3446. case 3:
  3447. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  3448. break;
  3449. case 4:
  3450. cb->getAllowed (arts, CArtifact::ART_RELIC);
  3451. break;
  3452. }
  3453. artifacts.push_back (arts[ran() % arts.size()]->id);
  3454. }
  3455. break;
  3456. }
  3457. }
  3458. void CBank::newTurn() const
  3459. {
  3460. if (bc == NULL)
  3461. {
  3462. if (daycounter >= 28 || cb->getDate(0) == 1)
  3463. {
  3464. cb->setObjProperty (id,11,0); //daycounter 0
  3465. cb->setObjProperty (id,14,0); //reset
  3466. if (ID == 24 && cb->getDate(0) > 1)
  3467. {
  3468. cb->setObjProperty (id,16,0);; //derelict ships are usable only once
  3469. }
  3470. }
  3471. else
  3472. cb->setObjProperty (id,11,1); //daycounter++
  3473. }
  3474. }
  3475. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3476. {
  3477. if (bc != NULL)
  3478. {
  3479. int banktext = 0;
  3480. switch (ID)
  3481. {
  3482. case 16: //generic bank
  3483. banktext = 32;
  3484. break;
  3485. case 24:
  3486. banktext = 41;
  3487. break;
  3488. case 25: //utopia
  3489. banktext = 47;
  3490. break;
  3491. case 84: //crypt
  3492. banktext = 119;
  3493. break;
  3494. case 85: //shipwreck
  3495. banktext = 122;
  3496. break;
  3497. }
  3498. BlockingDialog bd (true, false);
  3499. bd.player = h->getOwner();
  3500. bd.soundID = soundBase::DANGER;
  3501. std::string desc = VLC->generaltexth->allTexts[banktext];
  3502. boost::algorithm::replace_first (desc, "%s", VLC->generaltexth->names[ID]);
  3503. bd.text << desc;
  3504. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3505. }
  3506. else
  3507. {
  3508. InfoWindow iw;
  3509. if (ID == 85)
  3510. {
  3511. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  3512. GiveBonus gbonus;
  3513. gbonus.hid = h->id;
  3514. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3515. gbonus.bonus.source = HeroBonus::OBJECT;
  3516. gbonus.bonus.id = ID;
  3517. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  3518. gbonus.bonus.type = HeroBonus::MORALE;
  3519. gbonus.bonus.val = -1;
  3520. cb->giveHeroBonus(&gbonus);
  3521. }
  3522. iw.soundID = soundBase::GRAVEYARD;
  3523. iw.player = h->getOwner();
  3524. std::string desc = VLC->generaltexth->advobtxt[33];
  3525. boost::algorithm::replace_first (desc, "%s", VLC->generaltexth->names[ID]);
  3526. iw.text << desc;
  3527. cb->showInfoDialog(&iw);
  3528. }
  3529. }
  3530. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3531. {
  3532. if (accept)
  3533. {
  3534. int upgraded = 0;
  3535. if (ran()%100 < bc->upgradeChance) upgraded = 1;
  3536. CCreatureSet ourArmy;
  3537. switch (bc->guards.size())
  3538. {
  3539. case 1:
  3540. for (int i = 1; i <= 5; i++)
  3541. ourArmy.setCreature (i, bc->guards[1].first + upgraded, bc->guards[1].second / 5 );
  3542. break;
  3543. case 4:
  3544. {
  3545. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3546. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3547. ourArmy.setCreature (ourArmy.slots.size() + 1, it->first, it->second );
  3548. }
  3549. break;
  3550. default:
  3551. tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3552. return;
  3553. }
  3554. cb->startBattleI (h, this, true, boost::bind (&CBank::endBattle, this, h, _1));
  3555. }
  3556. }
  3557. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  3558. {
  3559. if (result->winner == 0)
  3560. {
  3561. int textID = -1;
  3562. InfoWindow iw;
  3563. switch (ID)
  3564. {
  3565. case 16: //generic bank
  3566. textID = 34;
  3567. break;
  3568. case 24: //derelict ship
  3569. if (bc->resources.size() != 0)
  3570. textID = 43;
  3571. else
  3572. {
  3573. textID = 42;
  3574. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3575. }
  3576. break;
  3577. case 25: //utopia
  3578. textID = 47;
  3579. break;
  3580. case 84: //crypt
  3581. textID = 121;
  3582. break;
  3583. case 85: //shipwreck
  3584. if (bc->resources.size() != 0)
  3585. textID = 124;
  3586. else
  3587. {
  3588. textID = 123;
  3589. }
  3590. break;
  3591. }
  3592. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  3593. iw.player = cb->getCurrentPlayer();
  3594. //grant resources
  3595. for (int it = 0; it < bc->resources.size(); ++it)
  3596. {
  3597. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  3598. cb->giveResource (cb->getCurrentPlayer(), it, bc->resources[it]);
  3599. }
  3600. //grant artifacts
  3601. for (std::vector<si32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  3602. {
  3603. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3604. iw.text.addReplacement (MetaString::ART_NAMES, *it);
  3605. cb->giveHeroArtifact (*it, cb->getSelectedHero() ,-2);
  3606. }
  3607. //grant creatures
  3608. CCreatureSet ourArmy;
  3609. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  3610. {
  3611. int slot = ourArmy.getSlotFor (it->second);
  3612. ourArmy.slots[slot] = *it; //assuming we're not going to add multiple stacks of same creature
  3613. }
  3614. cb->giveCreatures (id, cb->getHero (cb->getSelectedHero()), &ourArmy);
  3615. cb->setObjProperty (id, 15, 0); //bc = NULL
  3616. }
  3617. else
  3618. cb->setObjProperty (id, 14, 0); //reset
  3619. }
  3620. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  3621. {
  3622. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  3623. playerKeyMap.find(what-101)->second.insert(val);
  3624. }
  3625. bool CGKeys::wasMyColorVisited (int player) const
  3626. {
  3627. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  3628. return true;
  3629. else
  3630. return false;
  3631. }
  3632. const std::string & CGKeymasterTent::getHoverText() const
  3633. {
  3634. hoverName = VLC->generaltexth->names[ID];
  3635. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3636. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3637. else
  3638. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3639. return hoverName;
  3640. }
  3641. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  3642. {
  3643. InfoWindow iw;
  3644. iw.soundID = soundBase::CAVEHEAD;
  3645. iw.player = h->getOwner();
  3646. if (!wasMyColorVisited (h->getOwner()) )
  3647. {
  3648. cb->setObjProperty(id, h->tempOwner+101, subID);
  3649. iw.text << std::pair<ui8,ui32>(11,19);
  3650. }
  3651. else
  3652. iw.text << std::pair<ui8,ui32>(11,20);
  3653. cb->showInfoDialog(&iw);
  3654. }
  3655. void CGBorderGuard::initObj()
  3656. {
  3657. blockVisit = true;
  3658. }
  3659. const std::string & CGBorderGuard::getHoverText() const
  3660. {
  3661. hoverName = VLC->generaltexth->names[ID];
  3662. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3663. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3664. else
  3665. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3666. return hoverName;
  3667. }
  3668. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  3669. {
  3670. if (wasMyColorVisited (h->getOwner()) )
  3671. {
  3672. BlockingDialog bd (true, false);
  3673. bd.player = h->getOwner();
  3674. bd.soundID = soundBase::QUEST;
  3675. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  3676. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  3677. }
  3678. else
  3679. {
  3680. InfoWindow iw;
  3681. iw.player = h->getOwner();
  3682. iw.soundID = soundBase::CAVEHEAD;
  3683. iw.text << std::pair<ui8,ui32>(11,18);
  3684. cb->showInfoDialog (&iw);
  3685. }
  3686. }
  3687. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  3688. {
  3689. if (accept)
  3690. cb->removeObject(id);
  3691. }
  3692. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  3693. {
  3694. InfoWindow iw;
  3695. iw.player = h->getOwner();
  3696. if (!wasMyColorVisited (h->getOwner()) )
  3697. {
  3698. iw.text << std::pair<ui8,ui32>(11,18);
  3699. cb->showInfoDialog(&iw);
  3700. }
  3701. }
  3702. void CGMagi::initObj()
  3703. {
  3704. if (ID == 27)
  3705. {
  3706. blockVisit = true;
  3707. eyelist[subID].push_back(id);
  3708. }
  3709. }
  3710. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  3711. {
  3712. if (ID == 37)
  3713. {
  3714. InfoWindow iw;
  3715. CenterView cv;
  3716. FoWChange fw;
  3717. cv.player = iw.player = fw.player = h->tempOwner;
  3718. iw.soundID = soundBase::LIGHTHOUSE;
  3719. iw.player = h->tempOwner;
  3720. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  3721. cb->showInfoDialog(&iw);
  3722. fw.mode = 1;
  3723. std::vector<si32>::iterator it;
  3724. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  3725. {
  3726. const CGObjectInstance *eye = cb->getObj(*it);
  3727. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  3728. cb->sendAndApply(&fw);
  3729. cv.pos = eye->pos;
  3730. cv.focusTime = 2000;
  3731. cb->sendAndApply(&cv);
  3732. }
  3733. cv.pos = h->getPosition(false);
  3734. cb->sendAndApply(&cv);
  3735. }
  3736. else if (ID == 27)
  3737. {
  3738. InfoWindow iw;
  3739. iw.player = h->tempOwner;
  3740. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  3741. cb->showInfoDialog(&iw);
  3742. }
  3743. }
  3744. void CGBoat::initObj()
  3745. {
  3746. hero = NULL;
  3747. }
  3748. void CGSirens::initObj()
  3749. {
  3750. blockVisit = true;
  3751. }
  3752. const std::string & CGSirens::getHoverText() const
  3753. {
  3754. getNameVis(hoverName);
  3755. return hoverName;
  3756. }
  3757. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  3758. {
  3759. int message;
  3760. InfoWindow iw;
  3761. iw.soundID = soundBase::DANGER;
  3762. iw.player = h->tempOwner;
  3763. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  3764. {
  3765. iw.text.addTxt(11,133);
  3766. }
  3767. else
  3768. {
  3769. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  3770. int xp = 0;
  3771. SetGarrisons sg;
  3772. sg.garrs[h->id] = h->army;
  3773. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  3774. {
  3775. int drown = (int)(i->second.second * 0.3);
  3776. if(drown)
  3777. {
  3778. sg.garrs[h->id].slots[i->first].second -= drown;
  3779. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  3780. }
  3781. }
  3782. if(xp)
  3783. {
  3784. iw.text.addTxt(11,132);
  3785. iw.text.addReplacement(xp);
  3786. cb->sendAndApply(&sg);
  3787. }
  3788. else
  3789. {
  3790. iw.text.addTxt(11,134);
  3791. }
  3792. }
  3793. cb->showInfoDialog(&iw);
  3794. }
  3795. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  3796. {
  3797. cost.resize(RESOURCE_QUANTITY);
  3798. cost[0] = 10;
  3799. cost[6] = 1000;
  3800. }
  3801. //bool IShipyard::validLocation() const
  3802. //{
  3803. // std::vector<int3> offsets;
  3804. // getOutOffsets(offsets);
  3805. //
  3806. // TerrainTile *tile;
  3807. // for(int i = 0; i < offsets.size(); i++)
  3808. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3809. // return true;
  3810. // return false;
  3811. //}
  3812. int3 IShipyard::bestLocation() const
  3813. {
  3814. std::vector<int3> offsets;
  3815. getOutOffsets(offsets);
  3816. TerrainTile *tile;
  3817. for(int i = 0; i < offsets.size(); i++)
  3818. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3819. return o->pos + offsets[i];
  3820. return int3(-1,-1,-1);
  3821. }
  3822. IShipyard::IShipyard(const CGObjectInstance *O)
  3823. : o(O)
  3824. {
  3825. }
  3826. int IShipyard::state() const
  3827. {
  3828. int3 tile = bestLocation();
  3829. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  3830. if(!t)
  3831. return 3; //no water
  3832. else if(!t->blockingObjects.size())
  3833. return 0; //OK
  3834. else if(t->blockingObjects.front()->ID == 8)
  3835. return 1; //blocked with boat
  3836. else
  3837. return 2; //blocked
  3838. }
  3839. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  3840. {
  3841. if(obj->ID == TOWNI_TYPE)
  3842. {
  3843. return static_cast<CGTownInstance*>(obj);
  3844. }
  3845. else if(obj->ID == 87)
  3846. {
  3847. return static_cast<CGShipyard*>(obj);
  3848. }
  3849. else
  3850. {
  3851. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  3852. return NULL;
  3853. }
  3854. }
  3855. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  3856. {
  3857. return castFrom(const_cast<CGObjectInstance*>(obj));
  3858. }
  3859. CGShipyard::CGShipyard()
  3860. :IShipyard(this)
  3861. {
  3862. }
  3863. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  3864. {
  3865. 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),
  3866. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  3867. }
  3868. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  3869. {
  3870. if(tempOwner != h->tempOwner)
  3871. cb->setOwner(id, h->tempOwner);
  3872. int s = state();
  3873. if(s)
  3874. {
  3875. InfoWindow iw;
  3876. iw.player = tempOwner;
  3877. switch(s)
  3878. {
  3879. case 1:
  3880. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  3881. break;
  3882. case 2:
  3883. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  3884. break;
  3885. case 3:
  3886. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  3887. return;
  3888. }
  3889. cb->showInfoDialog(&iw);
  3890. }
  3891. else
  3892. {
  3893. OpenWindow ow;
  3894. ow.id1 = id;
  3895. ow.id2 = h->id;
  3896. ow.window = OpenWindow::SHIPYARD_WINDOW;
  3897. cb->sendAndApply(&ow);
  3898. }
  3899. }
  3900. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  3901. {
  3902. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  3903. {
  3904. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  3905. {
  3906. //ask if he wants to buy one
  3907. int text;
  3908. if (cb->getTile(pos)->tertype == 8) //water
  3909. text = 25;
  3910. else
  3911. {
  3912. if (pos.z == 0)
  3913. text = 26;
  3914. else
  3915. text = 27;
  3916. }
  3917. BlockingDialog bd (true, false);
  3918. bd.player = h->getOwner();
  3919. bd.soundID = soundBase::LIGHTHOUSE;
  3920. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  3921. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  3922. }
  3923. else //if he cannot afford
  3924. {
  3925. InfoWindow iw;
  3926. iw.player = h->getOwner();
  3927. iw.soundID = soundBase::CAVEHEAD;
  3928. iw.text << std::pair<ui8,ui32>(11,28);
  3929. cb->showInfoDialog (&iw);
  3930. }
  3931. }
  3932. else //if he already visited carographer
  3933. {
  3934. InfoWindow iw;
  3935. iw.player = h->getOwner();
  3936. iw.soundID = soundBase::CAVEHEAD;
  3937. iw.text << std::pair<ui8,ui32>(11,24);
  3938. cb->showInfoDialog (&iw);
  3939. }
  3940. }
  3941. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  3942. {
  3943. if (accept) //if hero wants to buy map
  3944. {
  3945. cb->giveResource (h->tempOwner, 6, -1000);
  3946. FoWChange fw;
  3947. fw.player = h->tempOwner;
  3948. int surface = 0;
  3949. if (cb->getTile(pos)->tertype == 8) //water
  3950. surface = 2;
  3951. else
  3952. surface = 1;
  3953. //reveal apropriate tiles
  3954. cb->getAllTiles (fw.tiles, h->tempOwner, pos.z, surface);
  3955. cb->sendAndApply (&fw);
  3956. cb->setObjProperty (id, 10, h->tempOwner);
  3957. }
  3958. }