CObjectHandler.cpp 59 KB

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