CObjectHandler.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  22. IGameCallback * IObjectInterface::cb = NULL;
  23. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  24. extern CLodHandler * bitmaph;
  25. extern boost::rand48 ran;
  26. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  27. {};
  28. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  29. {};
  30. void IObjectInterface::newTurn () const
  31. {};
  32. IObjectInterface::~IObjectInterface()
  33. {}
  34. IObjectInterface::IObjectInterface()
  35. {}
  36. void IObjectInterface::initObj()
  37. {}
  38. void CObjectHandler::loadObjects()
  39. {
  40. tlog5 << "\t\tReading cregens \n";
  41. cregens.resize(110); //TODO: hardcoded value - change
  42. for(size_t i=0; i < cregens.size(); ++i)
  43. {
  44. cregens[i]=-1;
  45. }
  46. std::ifstream ifs("config/cregens.txt");
  47. while(!ifs.eof())
  48. {
  49. int dw, cr;
  50. ifs >> dw >> cr;
  51. cregens[dw]=cr;
  52. }
  53. ifs.close();
  54. ifs.clear();
  55. tlog5 << "\t\tDone loading objects!\n";
  56. }
  57. int CGObjectInstance::getOwner() const
  58. {
  59. //if (state)
  60. // return state->owner;
  61. //else
  62. return tempOwner; //won't have owner
  63. }
  64. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  65. {
  66. pos = int3(-1,-1,-1);
  67. //std::cout << "Tworze obiekt "<<this<<std::endl;
  68. //state = new CLuaObjectScript();
  69. ID = subID = id = -1;
  70. defInfo = NULL;
  71. info = NULL;
  72. tempOwner = 254;
  73. blockVisit = false;
  74. }
  75. CGObjectInstance::~CGObjectInstance()
  76. {
  77. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  78. //if (state)
  79. // delete state;
  80. //state=NULL;
  81. }
  82. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  83. //{
  84. // pos = right.pos;
  85. // ID = right.ID;
  86. // subID = right.subID;
  87. // id = right.id;
  88. // defInfo = right.defInfo;
  89. // info = right.info;
  90. // blockVisit = right.blockVisit;
  91. // //state = new CLuaObjectScript(right.state->);
  92. // //*state = *right.state;
  93. // //state = right.state;
  94. // tempOwner = right.tempOwner;
  95. //}
  96. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  97. //{
  98. // pos = right.pos;
  99. // ID = right.ID;
  100. // subID = right.subID;
  101. // id = right.id;
  102. // defInfo = right.defInfo;
  103. // info = right.info;
  104. // blockVisit = right.blockVisit;
  105. // //state = new CLuaObjectScript();
  106. // //*state = *right.state;
  107. // tempOwner = right.tempOwner;
  108. // return *this;
  109. //}
  110. const std::string & CGObjectInstance::getHoverText() const
  111. {
  112. return hoverName;
  113. }
  114. void CGObjectInstance::setOwner(int ow)
  115. {
  116. //if (state)
  117. // state->owner = ow;
  118. //else
  119. tempOwner = ow;
  120. }
  121. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  122. {
  123. return defInfo->width;
  124. }
  125. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  126. {
  127. return defInfo->width;
  128. }
  129. 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)
  130. {
  131. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  132. return false;
  133. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  134. return true;
  135. return false;
  136. }
  137. bool CGObjectInstance::blockingAt(int x, int y) const
  138. {
  139. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  140. return false;
  141. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  142. return true;
  143. return false;
  144. }
  145. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  146. {
  147. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  148. return true;
  149. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  150. return false;
  151. if(this->pos.y<cmp.pos.y)
  152. return true;
  153. if(this->pos.y>cmp.pos.y)
  154. return false;
  155. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  156. return true;
  157. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  158. return false;
  159. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  160. return true;
  161. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  162. return false;
  163. if(this->pos.x<cmp.pos.x)
  164. return true;
  165. return false;
  166. }
  167. void CGObjectInstance::initObj()
  168. {
  169. }
  170. int lowestSpeed(const CGHeroInstance * chi)
  171. {
  172. if(!chi->army.slots.size())
  173. {
  174. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  175. return 20;
  176. }
  177. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  178. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  179. for (;i!=chi->army.slots.end();i++)
  180. {
  181. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  182. }
  183. return ret;
  184. }
  185. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype, const int & remaingMP) const
  186. {
  187. if(remaingMP <= 100) return 100; //workaround for strange behaviour manifested by Heroes III
  188. unsigned int ret = type->heroClass->terrCosts[ttype];
  189. //applying pathfinding skill
  190. switch(getSecSkillLevel(0))
  191. {
  192. case 1: //basic
  193. switch(ttype)
  194. {
  195. case rough:
  196. ret = 100;
  197. break;
  198. case sand: case snow:
  199. if(ret>125)
  200. ret = 125;
  201. break;
  202. case swamp:
  203. if(ret>150)
  204. ret = 150;
  205. break;
  206. default:
  207. //TODO do something nasty here throw maybe? or some def value asing
  208. break;
  209. }
  210. break;
  211. case 2: //advanced
  212. switch(ttype)
  213. {
  214. case rough:
  215. case sand:
  216. case snow:
  217. ret = 100;
  218. break;
  219. case swamp:
  220. if(ret>125)
  221. ret = 125;
  222. break;
  223. default:
  224. //TODO look up
  225. break;
  226. }
  227. break;
  228. case 3: //expert
  229. ret = 100;
  230. break;
  231. default:
  232. //TODO look up
  233. break;
  234. }
  235. //calculating road influence
  236. switch(rdtype)
  237. {
  238. case dirtRoad:
  239. ret*=0.75;
  240. break;
  241. case grazvelRoad:
  242. ret*=0.667;
  243. break;
  244. case cobblestoneRoad:
  245. ret*=0.5;
  246. break;
  247. default:
  248. //TODO killllll me
  249. break;
  250. }
  251. return ret;
  252. }
  253. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  254. {
  255. unsigned int sl = 100;
  256. for(size_t h=0; h < army.slots.size(); ++h)
  257. {
  258. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  259. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  260. }
  261. return sl;
  262. }
  263. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  264. {
  265. if (toh3m)
  266. {
  267. src.x+=1;
  268. return src;
  269. }
  270. else
  271. {
  272. src.x-=1;
  273. return src;
  274. }
  275. }
  276. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  277. {
  278. if (h3m)
  279. {
  280. return pos;
  281. }
  282. else
  283. {
  284. return convertPosition(pos,false);
  285. }
  286. }
  287. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  288. {
  289. return 6 + getSecSkillLevel(3); //default + scouting
  290. }
  291. si32 CGHeroInstance::manaLimit() const
  292. {
  293. double modifier = 1.0;
  294. switch(getSecSkillLevel(24)) //intelligence level
  295. {
  296. case 1: modifier+=0.25; break;
  297. case 2: modifier+=0.5; break;
  298. case 3: modifier+=1.0; break;
  299. }
  300. return 10*getPrimSkillLevel(3)*modifier;
  301. }
  302. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  303. //{
  304. // if (h3m)
  305. // pos = Pos;
  306. // else
  307. // pos = convertPosition(Pos,true);
  308. //}
  309. bool CGHeroInstance::canWalkOnSea() const
  310. {
  311. //TODO: write it - it should check if hero is flying, or something similiar
  312. return false;
  313. }
  314. int CGHeroInstance::getPrimSkillLevel(int id) const
  315. {
  316. return primSkills[id];
  317. }
  318. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  319. {
  320. for(size_t i=0; i < secSkills.size(); ++i)
  321. if(secSkills[i].first==ID)
  322. return secSkills[i].second;
  323. return 0;
  324. }
  325. int CGHeroInstance::maxMovePoints(bool onLand) const
  326. {
  327. int ret = 1270+70*lowestSpeed(this);
  328. if (ret>2000)
  329. ret=2000;
  330. if(onLand)
  331. {
  332. //logistics:
  333. switch(getSecSkillLevel(2))
  334. {
  335. case 1:
  336. ret *= 1.1f;
  337. break;
  338. case 2:
  339. ret *= 1.2f;
  340. break;
  341. case 3:
  342. ret *= 1.3f;
  343. break;
  344. }
  345. }
  346. else
  347. {
  348. //navigation:
  349. switch(getSecSkillLevel(2))
  350. {
  351. case 1:
  352. ret *= 1.5f;
  353. break;
  354. case 2:
  355. ret *= 2.0f;
  356. break;
  357. case 3:
  358. ret *= 2.5f;
  359. break;
  360. }
  361. }
  362. return ret;
  363. }
  364. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  365. {
  366. if(pos<19)
  367. if(vstd::contains(artifWorn,pos))
  368. return artifWorn.find(pos)->second;
  369. else
  370. return -1;
  371. else
  372. if(pos-19 < artifacts.size())
  373. return artifacts[pos-19];
  374. else
  375. return -1;
  376. }
  377. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  378. {
  379. if(art<0)
  380. {
  381. if(pos<19)
  382. artifWorn.erase(pos);
  383. else
  384. artifacts -= artifacts[pos-19];
  385. }
  386. else
  387. {
  388. if(pos<19)
  389. artifWorn[pos] = art;
  390. else
  391. if(pos-19 < artifacts.size())
  392. artifacts[pos-19] = art;
  393. else
  394. artifacts.push_back(art);
  395. }
  396. }
  397. const CArtifact * CGHeroInstance::getArt(int pos) const
  398. {
  399. int id = getArtAtPos(pos);
  400. if(id>=0)
  401. return &VLC->arth->artifacts[id];
  402. else
  403. return NULL;
  404. }
  405. int CGHeroInstance::getSpellSecLevel(int spell) const
  406. {
  407. int bestslvl = 0;
  408. if(VLC->spellh->spells[spell].air)
  409. if(getSecSkillLevel(15) >= bestslvl)
  410. {
  411. bestslvl = getSecSkillLevel(15);
  412. }
  413. if(VLC->spellh->spells[spell].fire)
  414. if(getSecSkillLevel(14) >= bestslvl)
  415. {
  416. bestslvl = getSecSkillLevel(14);
  417. }
  418. if(VLC->spellh->spells[spell].water)
  419. if(getSecSkillLevel(16) >= bestslvl)
  420. {
  421. bestslvl = getSecSkillLevel(16);
  422. }
  423. if(VLC->spellh->spells[spell].earth)
  424. if(getSecSkillLevel(17) >= bestslvl)
  425. {
  426. bestslvl = getSecSkillLevel(17);
  427. }
  428. return bestslvl;
  429. }
  430. CGHeroInstance::CGHeroInstance()
  431. {
  432. ID = 34;
  433. tacticFormationEnabled = inTownGarrison = false;
  434. mana = movement = portrait = level = -1;
  435. isStanding = true;
  436. moveDir = 4;
  437. exp = 0xffffffff;
  438. visitedTown = NULL;
  439. type = NULL;
  440. secSkills.push_back(std::make_pair(-1, -1));
  441. }
  442. void CGHeroInstance::initHero(int SUBID)
  443. {
  444. subID = SUBID;
  445. initHero();
  446. }
  447. void CGHeroInstance::initHero()
  448. {
  449. initHeroDefInfo();
  450. if(!type)
  451. type = VLC->heroh->heroes[subID];
  452. artifWorn[16] = 3;
  453. if(type->heroType % 2 == 1) //it's a magical hero
  454. {
  455. artifWorn[17] = 0; //give him spellbook
  456. }
  457. if(portrait < 0 || portrait == 255)
  458. portrait = subID;
  459. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  460. {
  461. primSkills.resize(4);
  462. primSkills[0] = type->heroClass->initialAttack;
  463. primSkills[1] = type->heroClass->initialDefence;
  464. primSkills[2] = type->heroClass->initialPower;
  465. primSkills[3] = type->heroClass->initialKnowledge;
  466. }
  467. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  468. secSkills = type->secSkillsInit;
  469. if(mana < 0)
  470. mana = manaLimit();
  471. if (!name.length())
  472. name = type->name;
  473. if (exp == 0xffffffff)
  474. {
  475. exp=40+ (ran()) % 50;
  476. level = 1;
  477. }
  478. else
  479. {
  480. level = VLC->heroh->level(exp);
  481. }
  482. if (!army.slots.size()) //standard army//initial army
  483. {
  484. int pom, pom2=0;
  485. for(int x=0;x<3;x++)
  486. {
  487. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  488. if(pom>=145 && pom<=149) //war machine
  489. {
  490. pom2++;
  491. switch (pom)
  492. {
  493. case 145: //catapult
  494. artifWorn[16] = 3;
  495. break;
  496. default:
  497. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  498. break;
  499. }
  500. continue;
  501. }
  502. army.slots[x-pom2].first = pom;
  503. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  504. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  505. else
  506. army.slots[x-pom2].second = +type->lowStack[x];
  507. army.formation = false;
  508. }
  509. }
  510. hoverName = VLC->generaltexth->allTexts[15];
  511. boost::algorithm::replace_first(hoverName,"%s",name);
  512. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  513. }
  514. void CGHeroInstance::initHeroDefInfo()
  515. {
  516. if(!defInfo)
  517. {
  518. defInfo = new CGDefInfo();
  519. defInfo->id = 34;
  520. defInfo->subid = subID;
  521. defInfo->printPriority = 0;
  522. defInfo->visitDir = 0xff;
  523. }
  524. for(int i=0;i<6;i++)
  525. {
  526. defInfo->blockMap[i]=255;
  527. defInfo->visitMap[i]=0;
  528. }
  529. defInfo->handler=NULL;
  530. defInfo->blockMap[5] = 253;
  531. defInfo->visitMap[5] = 2;
  532. }
  533. CGHeroInstance::~CGHeroInstance()
  534. {
  535. }
  536. bool CGHeroInstance::needsLastStack() const
  537. {
  538. return true;
  539. }
  540. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  541. {
  542. //TODO: check for allies
  543. if(tempOwner == h->tempOwner) //our hero
  544. {
  545. //exchange
  546. }
  547. else
  548. {
  549. cb->startBattleI(
  550. &h->army,
  551. &army,
  552. h->pos,
  553. h,
  554. this,
  555. 0);
  556. }
  557. }
  558. const std::string & CGHeroInstance::getBiography() const
  559. {
  560. if (biography.length())
  561. return biography;
  562. else
  563. return VLC->generaltexth->hTxts[subID].biography;
  564. }
  565. void CGHeroInstance::initObj()
  566. {
  567. blockVisit = true;
  568. }
  569. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  570. {
  571. int ret = 0;
  572. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  573. for(int i=0; i < mods.size(); i++)
  574. ret += mods[i].first;
  575. if(ret > 3)
  576. return 3;
  577. if(ret < -3)
  578. return -3;
  579. return ret;
  580. }
  581. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  582. {
  583. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  584. std::vector<std::pair<int,std::string> > ret;
  585. //various morale bonuses (from buildings, artifacts, etc)
  586. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  587. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  588. ret.push_back(std::make_pair(i->val, i->description));
  589. //leadership
  590. if(getSecSkillLevel(6))
  591. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  592. //town structures
  593. if(town && visitedTown)
  594. {
  595. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  596. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  597. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  598. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  599. }
  600. //number of alignments and presence of undead
  601. if(stack>=0)
  602. {
  603. bool archangelInArmy = false;
  604. std::set<si8> factions;
  605. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  606. {
  607. factions.insert(VLC->creh->creatures[i->second.first].faction);
  608. if(i->second.first == 13)
  609. archangelInArmy = true;
  610. }
  611. if(factions.size() == 1)
  612. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  613. else
  614. {
  615. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  616. {
  617. char buf[150];
  618. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  619. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  620. }
  621. else
  622. {
  623. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  624. }
  625. }
  626. if(vstd::contains(factions,4))
  627. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  628. if(archangelInArmy)
  629. {
  630. char buf[100];
  631. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl);
  632. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //%s in group +1
  633. }
  634. }
  635. return ret;
  636. }
  637. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  638. {
  639. int ret = 0;
  640. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  641. for(int i=0; i < mods.size(); i++)
  642. ret += mods[i].first;
  643. if(ret > 3)
  644. return 3;
  645. if(ret < -3)
  646. return -3;
  647. return ret;
  648. }
  649. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  650. {
  651. std::vector<std::pair<int,std::string> > ret;
  652. //various morale bonuses (from buildings, artifacts, etc)
  653. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  654. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  655. ret.push_back(std::make_pair(i->val, i->description));
  656. //luck skill
  657. if(getSecSkillLevel(9))
  658. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  659. return ret;
  660. }
  661. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  662. {
  663. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  664. if(i->source == from && i->id == id)
  665. return &*i;
  666. return NULL;
  667. }
  668. int CGTownInstance::getSightDistance() const //returns sight distance
  669. {
  670. return 10;
  671. }
  672. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  673. {
  674. if((builtBuildings.find(9))!=builtBuildings.end())
  675. return 3;
  676. if((builtBuildings.find(8))!=builtBuildings.end())
  677. return 2;
  678. if((builtBuildings.find(7))!=builtBuildings.end())
  679. return 1;
  680. return 0;
  681. }
  682. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  683. {
  684. if ((builtBuildings.find(13))!=builtBuildings.end())
  685. return 3;
  686. if ((builtBuildings.find(12))!=builtBuildings.end())
  687. return 2;
  688. if ((builtBuildings.find(11))!=builtBuildings.end())
  689. return 1;
  690. if ((builtBuildings.find(10))!=builtBuildings.end())
  691. return 0;
  692. return -1;
  693. }
  694. int CGTownInstance::mageGuildLevel() const
  695. {
  696. if ((builtBuildings.find(4))!=builtBuildings.end())
  697. return 5;
  698. if ((builtBuildings.find(3))!=builtBuildings.end())
  699. return 4;
  700. if ((builtBuildings.find(2))!=builtBuildings.end())
  701. return 3;
  702. if ((builtBuildings.find(1))!=builtBuildings.end())
  703. return 2;
  704. if ((builtBuildings.find(0))!=builtBuildings.end())
  705. return 1;
  706. return 0;
  707. }
  708. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  709. {
  710. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  711. }
  712. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  713. {
  714. return town->hordeLvl[HID];
  715. }
  716. int CGTownInstance::creatureGrowth(const int & level) const
  717. {
  718. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  719. switch(fortLevel())
  720. {
  721. case 3:
  722. ret*=2;break;
  723. case 2:
  724. ret*=(1.5); break;
  725. }
  726. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  727. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  728. if(getHordeLevel(0)==level)
  729. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  730. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  731. if(getHordeLevel(1)==level)
  732. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  733. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  734. return ret;
  735. }
  736. int CGTownInstance::dailyIncome() const
  737. {
  738. int ret = 0;
  739. if ((builtBuildings.find(26))!=builtBuildings.end())
  740. ret+=5000;
  741. if ((builtBuildings.find(13))!=builtBuildings.end())
  742. ret+=4000;
  743. else if ((builtBuildings.find(12))!=builtBuildings.end())
  744. ret+=2000;
  745. else if ((builtBuildings.find(11))!=builtBuildings.end())
  746. ret+=1000;
  747. else if ((builtBuildings.find(10))!=builtBuildings.end())
  748. ret+=500;
  749. return ret;
  750. }
  751. bool CGTownInstance::hasFort() const
  752. {
  753. return (builtBuildings.find(7))!=builtBuildings.end();
  754. }
  755. bool CGTownInstance::hasCapitol() const
  756. {
  757. return (builtBuildings.find(13))!=builtBuildings.end();
  758. }
  759. CGTownInstance::CGTownInstance()
  760. {
  761. builded=-1;
  762. destroyed=-1;
  763. garrisonHero=NULL;
  764. town=NULL;
  765. visitingHero = NULL;
  766. }
  767. CGTownInstance::~CGTownInstance()
  768. {}
  769. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  770. {
  771. if(checkGuild && mageGuildLevel() < level)
  772. return 0;
  773. int ret = 6 - level; //how many spells are available at this level
  774. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  775. ret++;
  776. return ret;
  777. }
  778. bool CGTownInstance::needsLastStack() const
  779. {
  780. if(garrisonHero)
  781. return true;
  782. else return false;
  783. }
  784. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  785. {
  786. if(getOwner() != h->getOwner())
  787. {
  788. return;
  789. }
  790. cb->heroVisitCastle(id,h->id);
  791. }
  792. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  793. {
  794. cb->stopHeroVisitCastle(id,h->id);
  795. }
  796. void CGTownInstance::initObj()
  797. {
  798. MetaString ms;
  799. ms << name << ", " << town->Name();
  800. hoverName = toString(ms);
  801. }
  802. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  803. {
  804. if(visitors.find(h->id)==visitors.end())
  805. {
  806. onNAHeroVisit(h->id, false);
  807. if(ID != 102 && ID!=4 && ID!=41) //not tree nor arena nor library of enlightenment
  808. cb->setObjProperty(id,4,h->id); //add to the visitors
  809. }
  810. else
  811. {
  812. onNAHeroVisit(h->id, true);
  813. }
  814. }
  815. void CGVisitableOPH::initObj()
  816. {
  817. if(ID==102)
  818. ttype = ran()%3;
  819. else
  820. ttype = -1;
  821. }
  822. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  823. {
  824. if(result==0) //player agreed to give res for exp
  825. {
  826. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  827. cb->changePrimSkill(heroID,4,expVal); //give exp
  828. cb->setObjProperty(id,4,heroID); //add to the visitors
  829. }
  830. }
  831. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  832. {
  833. int id=0, subid=0, ot=0, val=1;
  834. switch(ID)
  835. {
  836. case 4:
  837. ot = 0;
  838. break;
  839. case 51:
  840. subid=0;
  841. ot=80;
  842. break;
  843. case 23:
  844. subid=1;
  845. ot=39;
  846. break;
  847. case 61:
  848. subid=2;
  849. ot=100;
  850. break;
  851. case 32:
  852. subid=3;
  853. ot=59;
  854. break;
  855. case 100:
  856. id=5;
  857. ot=143;
  858. val=1000;
  859. break;
  860. case 102:
  861. id = 5;
  862. subid = 1;
  863. ot = 146;
  864. val = 1;
  865. break;
  866. case 41:
  867. ot = 66;
  868. break;
  869. }
  870. if (!alreadyVisited)
  871. {
  872. switch (ID)
  873. {
  874. case 4: //arena
  875. {
  876. SelectionDialog sd;
  877. sd.text << std::pair<ui8,ui32>(11,ot);
  878. sd.components.push_back(Component(0,0,2,0));
  879. sd.components.push_back(Component(0,1,2,0));
  880. sd.player = cb->getOwner(heroID);
  881. cb->showSelectionDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  882. return;
  883. }
  884. case 51:
  885. case 23:
  886. case 61:
  887. case 32:
  888. {
  889. cb->changePrimSkill(heroID,subid,val);
  890. InfoWindow iw;
  891. iw.components.push_back(Component(0,subid,val,0));
  892. iw.text << std::pair<ui8,ui32>(11,ot);
  893. iw.player = cb->getOwner(heroID);
  894. cb->showInfoDialog(&iw);
  895. break;
  896. }
  897. case 100: //give exp
  898. {
  899. InfoWindow iw;
  900. iw.components.push_back(Component(id,subid,val,0));
  901. iw.player = cb->getOwner(heroID);
  902. iw.text << std::pair<ui8,ui32>(11,ot);
  903. cb->showInfoDialog(&iw);
  904. cb->changePrimSkill(heroID,4,val);
  905. break;
  906. }
  907. case 102:
  908. {
  909. const CGHeroInstance *h = cb->getHero(heroID);
  910. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  911. if(!ttype)
  912. {
  913. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  914. InfoWindow iw;
  915. iw.components.push_back(Component(id,subid,1,0));
  916. iw.player = cb->getOwner(heroID);
  917. iw.text << std::pair<ui8,ui32>(11,148);
  918. cb->showInfoDialog(&iw);
  919. cb->changePrimSkill(heroID,4,val);
  920. break;
  921. }
  922. else
  923. {
  924. int res, resval;
  925. if(ttype==1)
  926. {
  927. res = 6;
  928. resval = 2000;
  929. ot = 149;
  930. }
  931. else
  932. {
  933. res = 5;
  934. resval = 10;
  935. ot = 151;
  936. }
  937. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  938. {
  939. ot++;
  940. InfoWindow iw;
  941. iw.player = h->tempOwner;
  942. iw.text << std::pair<ui8,ui32>(11,ot);
  943. cb->showInfoDialog(&iw);
  944. return;
  945. }
  946. YesNoDialog sd;
  947. sd.player = cb->getOwner(heroID);
  948. sd.text << std::pair<ui8,ui32>(11,ot);
  949. sd.components.push_back(Component(id,subid,val,0));
  950. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  951. }
  952. break;
  953. }
  954. case 41:
  955. {
  956. const CGHeroInstance *h = cb->getHero(heroID);
  957. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  958. {
  959. InfoWindow iw;
  960. iw.player = cb->getOwner(heroID);
  961. iw.text << std::pair<ui8,ui32>(11,68);
  962. cb->showInfoDialog(&iw);
  963. }
  964. else
  965. {
  966. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  967. cb->changePrimSkill(heroID,0,2);
  968. cb->changePrimSkill(heroID,1,2);
  969. cb->changePrimSkill(heroID,2,2);
  970. cb->changePrimSkill(heroID,3,2);
  971. InfoWindow iw;
  972. iw.player = cb->getOwner(heroID);
  973. iw.text << std::pair<ui8,ui32>(11,66);
  974. cb->showInfoDialog(&iw);
  975. }
  976. break;
  977. }
  978. }
  979. }
  980. else
  981. {
  982. ot++;
  983. InfoWindow iw;
  984. iw.player = cb->getOwner(heroID);
  985. iw.text << std::pair<ui8,ui32>(11,ot);
  986. cb->showInfoDialog(&iw);
  987. }
  988. }
  989. const std::string & CGVisitableOPH::getHoverText() const
  990. {
  991. int pom = -1;
  992. switch(ID)
  993. {
  994. case 4:
  995. pom = -1;
  996. break;
  997. case 51:
  998. pom = 8;
  999. break;
  1000. case 23:
  1001. pom = 7;
  1002. break;
  1003. case 61:
  1004. pom = 11;
  1005. break;
  1006. case 32:
  1007. pom = 4;
  1008. break;
  1009. case 100:
  1010. pom = 5;
  1011. break;
  1012. case 102:
  1013. pom = 18;
  1014. break;
  1015. case 41:
  1016. break;
  1017. default:
  1018. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1019. }
  1020. hoverName = VLC->generaltexth->names[ID];
  1021. if(pom >= 0)
  1022. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1023. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1024. if(h)
  1025. {
  1026. hoverName += ' ';
  1027. hoverName += (vstd::contains(visitors,h->id))
  1028. ? (VLC->generaltexth->allTexts[352]) //visited
  1029. : ( VLC->generaltexth->allTexts[353]); //not visited
  1030. }
  1031. return hoverName;
  1032. }
  1033. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1034. {
  1035. cb->setObjProperty(id,4,heroID); //add to the visitors
  1036. cb->changePrimSkill(heroID,primSkill,2);
  1037. }
  1038. bool CArmedInstance::needsLastStack() const
  1039. {
  1040. return false;
  1041. }
  1042. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1043. {
  1044. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1045. }
  1046. void CGCreature::endBattle( BattleResult *result ) const
  1047. {
  1048. if(result->winner==0)
  1049. {
  1050. cb->removeObject(id);
  1051. }
  1052. else
  1053. {
  1054. int killedAmount=0;
  1055. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1056. if(i->first == subID)
  1057. killedAmount += i->second;
  1058. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1059. MetaString ms;
  1060. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1061. pom = 174 + 3*pom + 1;
  1062. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1063. cb->setHoverName(id,&ms);
  1064. }
  1065. }
  1066. void CGCreature::initObj()
  1067. {
  1068. army.slots[0].first = subID;
  1069. si32 &amount = army.slots[0].second;
  1070. CCreature &c = VLC->creh->creatures[subID];
  1071. if(!amount)
  1072. if(c.ammMax == c.ammMin)
  1073. amount = c.ammMax;
  1074. else
  1075. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1076. MetaString ms;
  1077. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1078. pom = 174 + 3*pom + 1;
  1079. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1080. hoverName = toString(ms);
  1081. }
  1082. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1083. {
  1084. if(subID == 7) //TODO: support for abandoned mine
  1085. return;
  1086. if(h->tempOwner == tempOwner) //we're visiting our mine
  1087. return; //TODO: leaving garrison
  1088. //TODO: check if mine is guarded
  1089. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1090. MetaString ms;
  1091. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1092. cb->setHoverName(id,&ms);
  1093. int vv=1; //amount of resource per turn
  1094. if (subID==0 || subID==2)
  1095. vv++;
  1096. else if (subID==6)
  1097. vv = 1000;
  1098. InfoWindow iw;
  1099. iw.text << std::pair<ui8,ui32>(10,subID);
  1100. iw.player = h->tempOwner;
  1101. iw.components.push_back(Component(2,subID,vv,-1));
  1102. cb->showInfoDialog(&iw);
  1103. }
  1104. void CGMine::newTurn() const
  1105. {
  1106. if (tempOwner == NEUTRAL_PLAYER)
  1107. return;
  1108. int vv = 1;
  1109. if (subID==0 || subID==2)
  1110. vv++;
  1111. else if (subID==6)
  1112. vv = 1000;
  1113. cb->giveResource(tempOwner,subID,vv);
  1114. }
  1115. void CGMine::initObj()
  1116. {
  1117. MetaString ms;
  1118. ms << std::pair<ui8,ui32>(9,subID);
  1119. if(tempOwner >= PLAYER_LIMIT)
  1120. tempOwner = NEUTRAL_PLAYER;
  1121. else
  1122. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1123. hoverName = toString(ms);
  1124. }
  1125. void CGResource::initObj()
  1126. {
  1127. blockVisit = true;
  1128. hoverName = VLC->generaltexth->restypes[subID];
  1129. if(!amount)
  1130. {
  1131. switch(subID)
  1132. {
  1133. case 6:
  1134. amount = 500 + (rand()%6)*100;
  1135. break;
  1136. case 0: case 2:
  1137. amount = 6 + (rand()%5);
  1138. break;
  1139. default:
  1140. amount = 3 + (rand()%3);
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1146. {
  1147. if(army.slots.size())
  1148. {
  1149. if(message.size())
  1150. {
  1151. YesNoDialog ynd;
  1152. ynd.player = h->getOwner();
  1153. ynd.text << message;
  1154. cb->showYesNoDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1155. }
  1156. else
  1157. {
  1158. fightForRes(0,h);
  1159. }
  1160. }
  1161. else
  1162. {
  1163. if(message.length())
  1164. {
  1165. InfoWindow iw;
  1166. iw.player = h->tempOwner;
  1167. iw.text << message;
  1168. cb->showInfoDialog(&iw);
  1169. }
  1170. collectRes(h->getOwner());
  1171. }
  1172. }
  1173. void CGResource::collectRes( int player ) const
  1174. {
  1175. cb->giveResource(player,subID,amount);
  1176. ShowInInfobox sii;
  1177. sii.player = player;
  1178. sii.c = Component(2,subID,amount,0);
  1179. sii.text << std::pair<ui8,ui32>(11,113);
  1180. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1181. cb->showCompInfo(&sii);
  1182. cb->removeObject(id);
  1183. }
  1184. void CGResource::fightForRes(ui32 refusedFight, const CGHeroInstance *h) const
  1185. {
  1186. if(!refusedFight)
  1187. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1188. }
  1189. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1190. {
  1191. if(result->winner == 0) //attacker won
  1192. collectRes(h->getOwner());
  1193. }
  1194. void CGVisitableOPW::newTurn() const
  1195. {
  1196. if (cb->getDate(1)==1) //first day of week
  1197. {
  1198. cb->setObjProperty(id,5,false);
  1199. MetaString ms; //set text to "not visited"
  1200. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1201. cb->setHoverName(id,&ms);
  1202. }
  1203. }
  1204. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1205. {
  1206. int mid;
  1207. switch (ID)
  1208. {
  1209. case 55:
  1210. mid = 92;
  1211. break;
  1212. case 112:
  1213. mid = 170;
  1214. break;
  1215. case 109:
  1216. mid = 164;
  1217. break;
  1218. }
  1219. if (visited)
  1220. {
  1221. if (ID!=112)
  1222. mid++;
  1223. else
  1224. mid--;
  1225. InfoWindow iw;
  1226. iw.player = h->tempOwner;
  1227. iw.text << std::pair<ui8,ui32>(11,mid);
  1228. cb->showInfoDialog(&iw);
  1229. }
  1230. else
  1231. {
  1232. int type, sub, val;
  1233. type = 2;
  1234. switch (ID)
  1235. {
  1236. case 55:
  1237. if (rand()%2)
  1238. {
  1239. sub = 5;
  1240. val = 5;
  1241. }
  1242. else
  1243. {
  1244. sub = 6;
  1245. val = 500;
  1246. }
  1247. break;
  1248. case 112:
  1249. mid = 170;
  1250. sub = (rand() % 5) + 1;
  1251. val = (rand() % 4) + 3;
  1252. break;
  1253. case 109:
  1254. mid = 164;
  1255. sub = 6;
  1256. if(cb->getDate(2)<2)
  1257. val = 500;
  1258. else
  1259. val = 1000;
  1260. }
  1261. cb->giveResource(h->tempOwner,sub,val);
  1262. InfoWindow iw;
  1263. iw.player = h->tempOwner;
  1264. iw.components.push_back(Component(type,sub,val,0));
  1265. iw.text << std::pair<ui8,ui32>(11,mid);
  1266. cb->showInfoDialog(&iw);
  1267. cb->setObjProperty(id,5,true);
  1268. MetaString ms; //set text to "visited"
  1269. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1270. cb->setHoverName(id,&ms);
  1271. }
  1272. }
  1273. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1274. {
  1275. int destinationid=-1;
  1276. switch(ID)
  1277. {
  1278. case 43: //one way - find correspong exit monolith
  1279. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1280. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1281. else
  1282. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1283. break;
  1284. case 45: //two way monolith - pick any other one
  1285. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1286. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1287. else
  1288. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1289. break;
  1290. case 103: //find nearest subterranean gate on the other level
  1291. {
  1292. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1293. for(int i=0; i<objs[103][0].size(); i++)
  1294. {
  1295. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1296. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1297. if(hlp<best.second)
  1298. {
  1299. best.first = objs[103][0][i];
  1300. best.second = hlp;
  1301. }
  1302. }
  1303. if(best.first<0)
  1304. return;
  1305. else
  1306. destinationid = best.first;
  1307. break;
  1308. }
  1309. }
  1310. if(destinationid < 0)
  1311. {
  1312. tlog2 << "Cannot find exit... :( \n";
  1313. return;
  1314. }
  1315. cb->moveHero(h->id,
  1316. (ID!=103)
  1317. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1318. : (cb->getObj(destinationid)->pos),
  1319. true);
  1320. }
  1321. void CGTeleport::initObj()
  1322. {
  1323. objs[ID][subID].push_back(id);
  1324. }
  1325. void CGArtifact::initObj()
  1326. {
  1327. blockVisit = true;
  1328. if(ID == 5)
  1329. hoverName = VLC->arth->artifacts[subID].Name();
  1330. }
  1331. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1332. {
  1333. if(!army.slots.size())
  1334. {
  1335. InfoWindow iw;
  1336. iw.player = h->tempOwner;
  1337. iw.components.push_back(Component(4,subID,0,0));
  1338. if(message.length())
  1339. iw.text << message;
  1340. else
  1341. iw.text << std::pair<ui8,ui32>(12,subID);
  1342. cb->showInfoDialog(&iw);
  1343. pick(h);
  1344. }
  1345. else
  1346. {
  1347. if(message.size())
  1348. {
  1349. YesNoDialog ynd;
  1350. ynd.player = h->getOwner();
  1351. ynd.text << message;
  1352. cb->showYesNoDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1353. }
  1354. else
  1355. {
  1356. fightForArt(0,h);
  1357. }
  1358. }
  1359. }
  1360. void CGArtifact::pick(const CGHeroInstance * h) const
  1361. {
  1362. cb->giveHeroArtifact(subID,h->id,-2);
  1363. cb->removeObject(id);
  1364. }
  1365. void CGArtifact::fightForArt( ui32 refusedFight, const CGHeroInstance *h ) const
  1366. {
  1367. if(!refusedFight)
  1368. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1369. }
  1370. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1371. {
  1372. if(result->winner == 0) //attacker won
  1373. pick(h);
  1374. }
  1375. void CGPickable::initObj()
  1376. {
  1377. blockVisit = true;
  1378. switch(ID)
  1379. {
  1380. case 12: //campfire
  1381. val2 = (ran()%3) + 4; //4 - 6
  1382. val1 = val2 * 100;
  1383. type = ran()%6; //given resource
  1384. break;
  1385. case 101: //treasure chest
  1386. {
  1387. int hlp = ran()%100;
  1388. if(hlp >= 95)
  1389. {
  1390. type = 1;
  1391. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1392. return;
  1393. }
  1394. else if (hlp >= 65)
  1395. {
  1396. val1 = 2000;
  1397. }
  1398. else if(hlp >= 33)
  1399. {
  1400. val1 = 1500;
  1401. }
  1402. else
  1403. {
  1404. val1 = 1000;
  1405. }
  1406. val2 = val1 - 500;
  1407. type = 0;
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1413. {
  1414. switch(ID)
  1415. {
  1416. case 12: //campfire
  1417. {
  1418. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1419. cb->giveResource(h->tempOwner,6,val1);//gold
  1420. InfoWindow iw;
  1421. iw.player = h->tempOwner;
  1422. iw.components.push_back(Component(2,6,val1,0));
  1423. iw.components.push_back(Component(2,type,val2,0));
  1424. iw.text << std::pair<ui8,ui32>(11,23);
  1425. cb->showInfoDialog(&iw);
  1426. break;
  1427. }
  1428. case 101: //treasure chest
  1429. {
  1430. if (subID) //not OH3 treasure chest
  1431. {
  1432. tlog2 << "Not supported WoG treasure chest!\n";
  1433. return;
  1434. }
  1435. if(type) //there is an artifact
  1436. {
  1437. cb->giveHeroArtifact(val1,h->id,-2);
  1438. InfoWindow iw;
  1439. iw.player = h->tempOwner;
  1440. iw.components.push_back(Component(4,val1,1,0));
  1441. iw.text << std::pair<ui8,ui32>(11,145);
  1442. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1443. cb->showInfoDialog(&iw);
  1444. break;
  1445. }
  1446. else
  1447. {
  1448. SelectionDialog sd;
  1449. sd.player = h->tempOwner;
  1450. sd.text << std::pair<ui8,ui32>(11,146);
  1451. sd.components.push_back(Component(2,6,val1,0));
  1452. sd.components.push_back(Component(5,0,val2,0));
  1453. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1454. cb->showSelectionDialog(&sd,fun);
  1455. return;
  1456. }
  1457. }
  1458. }
  1459. cb->removeObject(id);
  1460. }
  1461. void CGPickable::chosen( int which, int heroID ) const
  1462. {
  1463. switch(which)
  1464. {
  1465. case 0: //player pick gold
  1466. cb->giveResource(cb->getOwner(heroID),6,val1);
  1467. break;
  1468. case 1: //player pick exp
  1469. cb->changePrimSkill(heroID, 4, val2);
  1470. break;
  1471. default:
  1472. throw std::string("Unhandled treasure choice");
  1473. }
  1474. cb->removeObject(id);
  1475. }
  1476. void CGWitchHut::initObj()
  1477. {
  1478. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1479. }
  1480. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1481. {
  1482. InfoWindow iw;
  1483. iw.player = h->getOwner();
  1484. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1485. {
  1486. iw.text << std::pair<ui8,ui32>(11,172);
  1487. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1488. }
  1489. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1490. {
  1491. iw.text << std::pair<ui8,ui32>(11,173);
  1492. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1493. }
  1494. else //give sec skill
  1495. {
  1496. iw.components.push_back(Component(1, ability, 1, 0));
  1497. iw.text << std::pair<ui8,ui32>(11,171);
  1498. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1499. cb->changeSecSkill(h->id,ability,1,true);
  1500. }
  1501. cb->showInfoDialog(&iw);
  1502. }
  1503. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1504. {
  1505. }
  1506. void CGDwelling::initObj()
  1507. {
  1508. }
  1509. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1510. {
  1511. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1512. int messageID, bonusType, bonusVal;
  1513. int bonusMove = 0;
  1514. InfoWindow iw;
  1515. iw.player = h->tempOwner;
  1516. GiveBonus gbonus;
  1517. gbonus.hid = h->id;
  1518. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1519. gbonus.bonus.source = HeroBonus::OBJECT;
  1520. gbonus.bonus.id = ID;
  1521. switch(ID)
  1522. {
  1523. case 14: //swan pond
  1524. messageID = 29;
  1525. gbonus.bonus.type = HeroBonus::LUCK;
  1526. gbonus.bonus.val = 2;
  1527. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1528. bonusMove = -h->movement;
  1529. break;
  1530. case 28: //Faerie Ring
  1531. messageID = 49;
  1532. gbonus.bonus.type = HeroBonus::LUCK;
  1533. gbonus.bonus.val = 1;
  1534. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1535. break;
  1536. case 30: //fountain of fortune
  1537. messageID = 55;
  1538. gbonus.bonus.type = HeroBonus::LUCK;
  1539. gbonus.bonus.val = rand()%5 - 1;
  1540. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  1541. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  1542. break;
  1543. case 38: //idol of fortune
  1544. messageID = 62;
  1545. if(cb->getDate(1) == 7) //7th day of week
  1546. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1547. else
  1548. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  1549. gbonus.bonus.val = 1;
  1550. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  1551. break;
  1552. case 64: //Rally Flag
  1553. messageID = 111;
  1554. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1555. gbonus.bonus.val = 1;
  1556. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  1557. bonusMove = 400;
  1558. break;
  1559. case 56: //oasis
  1560. messageID = 95;
  1561. gbonus.bonus.type = HeroBonus::MORALE;
  1562. gbonus.bonus.val = 1;
  1563. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  1564. bonusMove = 800;
  1565. break;
  1566. case 96: //temple
  1567. messageID = 140;
  1568. gbonus.bonus.type = HeroBonus::MORALE;
  1569. if(cb->getDate(1)==7) //sunday
  1570. {
  1571. gbonus.bonus.val = 2;
  1572. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  1573. }
  1574. else
  1575. {
  1576. gbonus.bonus.val = 1;
  1577. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  1578. }
  1579. break;
  1580. case 110://Watering Hole
  1581. messageID = 166;
  1582. gbonus.bonus.type = HeroBonus::MORALE;
  1583. gbonus.bonus.val = 1;
  1584. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  1585. bonusMove = 400;
  1586. break;
  1587. case 31: //Fountain of Youth
  1588. messageID = 57;
  1589. gbonus.bonus.type = HeroBonus::MORALE;
  1590. gbonus.bonus.val = 1;
  1591. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  1592. bonusMove = 400;
  1593. break;
  1594. }
  1595. if(visited)
  1596. {
  1597. if(ID==64 || ID==96 || ID==56)
  1598. messageID--;
  1599. else
  1600. messageID++;
  1601. }
  1602. else
  1603. {
  1604. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1605. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  1606. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1607. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  1608. cb->giveHeroBonus(&gbonus);
  1609. if(bonusMove) //swan pond - take all move points
  1610. {
  1611. SetMovePoints smp;
  1612. smp.hid = h->id;
  1613. smp.val = h->movement + bonusMove;
  1614. cb->setMovePoints(&smp);
  1615. }
  1616. }
  1617. iw.text << std::pair<ui8,ui32>(11,messageID);
  1618. cb->showInfoDialog(&iw);
  1619. }
  1620. const std::string & CGBonusingObject::getHoverText() const
  1621. {
  1622. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1623. hoverName = VLC->generaltexth->names[ID];
  1624. if(h)
  1625. {
  1626. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1627. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1628. else
  1629. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1630. }
  1631. return hoverName;
  1632. }
  1633. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  1634. {
  1635. int message;
  1636. InfoWindow iw;
  1637. iw.player = h->tempOwner;
  1638. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  1639. {
  1640. message = 78;
  1641. }
  1642. else if(h->mana < h->manaLimit())
  1643. {
  1644. GiveBonus gbonus;
  1645. gbonus.bonus.type = HeroBonus::NONE;
  1646. gbonus.hid = h->id;
  1647. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  1648. gbonus.bonus.source = HeroBonus::OBJECT;
  1649. gbonus.bonus.id = ID;
  1650. cb->giveHeroBonus(&gbonus);
  1651. cb->setManaPoints(h->id,h->manaLimit());
  1652. message = 77;
  1653. }
  1654. else
  1655. {
  1656. message = 79;
  1657. }
  1658. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  1659. cb->showInfoDialog(&iw);
  1660. }
  1661. const std::string & CGMagicWell::getHoverText() const
  1662. {
  1663. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1664. hoverName = VLC->generaltexth->names[ID];
  1665. if(h)
  1666. {
  1667. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1668. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1669. else
  1670. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1671. }
  1672. return hoverName;
  1673. }
  1674. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  1675. {
  1676. /*if(!(availableFor & (1 << h->tempOwner)))
  1677. return;
  1678. if(cb->getPlayerSettings(h->tempOwner)->human)
  1679. {
  1680. if(humanActivate)
  1681. activated(h);
  1682. }
  1683. else if(computerActivate)
  1684. activated(h);*/
  1685. }
  1686. void CGEvent::endBattle( BattleResult *result ) const
  1687. {
  1688. if(result->winner)
  1689. return;
  1690. //give
  1691. }
  1692. void CGEvent::activated( const CGHeroInstance * h ) const
  1693. {
  1694. InfoWindow iw;
  1695. iw.player = h->tempOwner;
  1696. iw.text << message;
  1697. cb->showInfoDialog(&iw);
  1698. if(guarders)
  1699. cb->startBattleI(h->id,guarders,pos,boost::bind(&CGEvent::endBattle,this,_1));
  1700. cb->removeObject(id);
  1701. }