CObjectHandler.cpp 58 KB

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