CObjectHandler.cpp 87 KB

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