CObjectHandler.cpp 107 KB

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