CObjectHandler.cpp 73 KB

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