CHeroHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CHeroHandler.h"
  4. #include "CLodHandler.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include <iomanip>
  7. #include <sstream>
  8. #include <fstream>
  9. #include <boost/version.hpp>
  10. #if BOOST_VERSION >= 103800
  11. #include <boost/spirit/include/classic.hpp>
  12. #else
  13. #include <boost/spirit.hpp>
  14. #endif
  15. using namespace boost::spirit;
  16. extern CLodHandler * bitmaph;
  17. void loadToIt(std::string &dest, const std::string &src, int &iter, int mode);
  18. /*
  19. * CHeroHandler.cpp, part of VCMI engine
  20. *
  21. * Authors: listed in file AUTHORS in main folder
  22. *
  23. * License: GNU General Public License v2.0 or later
  24. * Full text of license available in license.txt file, in main folder
  25. *
  26. */
  27. CHeroClass::CHeroClass()
  28. {
  29. skillLimit = 8;
  30. }
  31. CHeroClass::~CHeroClass()
  32. {
  33. }
  34. int CHeroClass::chooseSecSkill(const std::set<int> & possibles) const //picks secondary skill out from given possibilities
  35. {
  36. if(possibles.size()==1)
  37. return *possibles.begin();
  38. int totalProb = 0;
  39. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  40. {
  41. totalProb += proSec[*i];
  42. }
  43. int ran = rand()%totalProb;
  44. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  45. {
  46. ran -= proSec[*i];
  47. if(ran<0)
  48. return *i;
  49. }
  50. throw std::string("Cannot pick secondary skill!");
  51. }
  52. EAlignment CHeroClass::getAlignment()
  53. {
  54. return (EAlignment)alignment;
  55. }
  56. int CObstacleInfo::getWidth() const
  57. {
  58. int ret = 1;
  59. int line = 1;
  60. for(int h=0; h<blockmap.size(); ++h)
  61. {
  62. int cur = - line/2;
  63. switch(blockmap[h])
  64. {
  65. case 'X' : case 'N':
  66. ++cur;
  67. break;
  68. case 'L':
  69. if(cur > ret)
  70. ret = cur;
  71. ++line;
  72. break;
  73. }
  74. }
  75. return ret;
  76. }
  77. int CObstacleInfo::getHeight() const
  78. {
  79. int ret = 1;
  80. for(int h=0; h<blockmap.size(); ++h)
  81. {
  82. if(blockmap[h] == 'L')
  83. {
  84. ++ret;
  85. }
  86. }
  87. return ret;
  88. }
  89. std::vector<THex> CObstacleInfo::getBlocked(THex hex) const
  90. {
  91. std::vector<THex> ret;
  92. int cur = hex; //currently browsed hex
  93. int curBeg = hex; //beginning of current line
  94. for(int h=0; h<blockmap.size(); ++h)
  95. {
  96. switch(blockmap[h])
  97. {
  98. case 'X':
  99. ret.push_back(cur);
  100. ++cur;
  101. break;
  102. case 'L':
  103. cur = curBeg + BFIELD_WIDTH;
  104. if((cur/BFIELD_WIDTH)%2 == 1)
  105. {
  106. cur--;
  107. }
  108. curBeg = cur;
  109. break;
  110. case 'N':
  111. ++cur;
  112. break;
  113. }
  114. }
  115. return ret;
  116. }
  117. THex CObstacleInfo::getMaxBlocked(THex hex) const
  118. {
  119. std::vector<THex> blocked = getBlocked(hex);
  120. return *std::max_element(blocked.begin(), blocked.end());
  121. }
  122. CHeroHandler::~CHeroHandler()
  123. {
  124. for (int i = 0; i < heroes.size(); i++)
  125. heroes[i].dellNull();
  126. for (int i = 0; i < heroClasses.size(); i++)
  127. delete heroClasses[i];
  128. }
  129. CHeroHandler::CHeroHandler()
  130. {}
  131. void CHeroHandler::loadObstacles()
  132. {
  133. std::ifstream inp;
  134. inp.open(DATA_DIR "/config/obstacles.txt", std::ios_base::in|std::ios_base::binary);
  135. if(!inp.is_open())
  136. {
  137. tlog1<<"missing file: config/obstacles.txt"<<std::endl;
  138. }
  139. else
  140. {
  141. const int MAX_DUMP = 10000;
  142. char dump[MAX_DUMP+1];
  143. for(int i=0; i<8; ++i)
  144. {
  145. inp.getline(dump, MAX_DUMP);
  146. }
  147. while(true)
  148. {
  149. CObstacleInfo obi;
  150. inp>>obi.ID;
  151. if(obi.ID == -1) break;
  152. inp>>obi.defName;
  153. inp>>obi.blockmap;
  154. inp>>obi.allowedTerrains;
  155. inp>>obi.posShift.first;
  156. inp>>obi.posShift.second;
  157. obstacles[obi.ID] = obi;
  158. }
  159. inp.close();
  160. }
  161. }
  162. void CHeroHandler::loadPuzzleInfo()
  163. {
  164. std::ifstream inp;
  165. inp.open(DATA_DIR "/config/puzzle_map.txt", std::ios_base::in|std::ios_base::binary);
  166. if(!inp.is_open())
  167. {
  168. tlog1<<"missing file: config/puzzle_map.txt"<<std::endl;
  169. }
  170. else
  171. {
  172. const int MAX_DUMP = 10000;
  173. char dump[MAX_DUMP+1];
  174. inp.getline(dump, MAX_DUMP);
  175. for(int fct = 0; fct < F_NUMBER; ++fct)
  176. {
  177. std::string dmp;
  178. inp >> dmp;
  179. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  180. {
  181. SPuzzleInfo spi;
  182. inp >> spi.x;
  183. inp >> spi.y;
  184. inp >> spi.whenUncovered;
  185. spi.number = g;
  186. //filename calculation
  187. std::ostringstream suffix;
  188. suffix << std::setfill('0') << std::setw(2);
  189. suffix << g << ".BMP";
  190. static const std::string factionToInfix[F_NUMBER] = {"CAS", "RAM", "TOW", "INF", "NEC", "DUN", "STR", "FOR", "ELE"};
  191. spi.filename = "PUZ" + factionToInfix[fct] + suffix.str();
  192. puzzleInfo[fct].push_back(spi);
  193. }
  194. }
  195. inp.close();
  196. }
  197. }
  198. void CHeroHandler::loadHeroes()
  199. {
  200. VLC->heroh = this;
  201. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  202. int it=0;
  203. std::string dump;
  204. for(int i=0; i<2; ++i)
  205. {
  206. loadToIt(dump,buf,it,3);
  207. }
  208. int numberOfCurrentClassHeroes = 0;
  209. int currentClass = 0;
  210. int additHero = 0;
  211. CHero::EHeroClasses addTab[12];
  212. addTab[0] = CHero::KNIGHT;
  213. addTab[1] = CHero::WITCH;
  214. addTab[2] = CHero::KNIGHT;
  215. addTab[3] = CHero::WIZARD;
  216. addTab[4] = CHero::RANGER;
  217. addTab[5] = CHero::BARBARIAN;
  218. addTab[6] = CHero::DEATHKNIGHT;
  219. addTab[7] = CHero::WARLOCK;
  220. addTab[8] = CHero::KNIGHT;
  221. addTab[9] = CHero::WARLOCK;
  222. addTab[10] = CHero::BARBARIAN;
  223. addTab[11] = CHero::DEMONIAC;
  224. for (int i=0; i<HEROES_QUANTITY; i++)
  225. {
  226. CHero * nher = new CHero;
  227. if(currentClass<18)
  228. {
  229. nher->heroType = static_cast<CHero::EHeroClasses>(currentClass);
  230. ++numberOfCurrentClassHeroes;
  231. if(numberOfCurrentClassHeroes==8)
  232. {
  233. numberOfCurrentClassHeroes = 0;
  234. ++currentClass;
  235. }
  236. }
  237. else
  238. {
  239. nher->heroType = addTab[additHero++];
  240. }
  241. std::string pom ;
  242. loadToIt(nher->name,buf,it,4);
  243. for(int x=0;x<3;x++)
  244. {
  245. loadToIt(pom,buf,it,4);
  246. nher->lowStack[x] = atoi(pom.c_str());
  247. loadToIt(pom,buf,it,4);
  248. nher->highStack[x] = atoi(pom.c_str());
  249. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  250. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  251. if(hlp>=0)
  252. nher->refTypeStack[x].replace(hlp,1,"");
  253. }
  254. nher->ID = heroes.size();
  255. heroes.push_back(nher);
  256. }
  257. //loading initial secondary skills
  258. {
  259. std::ifstream inp;
  260. inp.open(DATA_DIR "/config/heroes_sec_skills.txt", std::ios_base::in|std::ios_base::binary);
  261. if(!inp.is_open())
  262. {
  263. tlog1<<"missing file: config/heroes_sec_skills.txt"<<std::endl;
  264. }
  265. else
  266. {
  267. inp>>dump;
  268. int hid; //ID of currently read hero
  269. int secQ; //number of secondary abilities
  270. while(true)
  271. {
  272. inp>>hid;
  273. if(hid == -1)
  274. break;
  275. inp>>secQ;
  276. heroes[hid]->sex = secQ;
  277. inp>>secQ;
  278. for(int g=0; g<secQ; ++g)
  279. {
  280. int a, b;
  281. inp>>a; inp>>b;
  282. heroes[hid]->secSkillsInit.push_back(std::make_pair(a, b));
  283. }
  284. }
  285. inp.close();
  286. }
  287. }
  288. //initial skills loaded
  289. {
  290. std::ifstream inp;
  291. std::istringstream iss;
  292. dump.clear();
  293. inp.open(DATA_DIR "/config/hero_spells.txt");
  294. while(inp)
  295. {
  296. getline(inp, dump);
  297. if(!dump.size() || dump[0] == '-')
  298. continue;
  299. iss.clear();
  300. iss.str(dump);
  301. int hid, sid;
  302. iss >> hid >> sid;
  303. heroes[hid]->startingSpell = sid;
  304. }
  305. inp.close();
  306. }
  307. loadHeroClasses();
  308. initHeroClasses();
  309. expPerLevel.push_back(0);
  310. expPerLevel.push_back(1000);
  311. expPerLevel.push_back(2000);
  312. expPerLevel.push_back(3200);
  313. expPerLevel.push_back(4600);
  314. expPerLevel.push_back(6200);
  315. expPerLevel.push_back(8000);
  316. expPerLevel.push_back(10000);
  317. expPerLevel.push_back(12200);
  318. expPerLevel.push_back(14700);
  319. expPerLevel.push_back(17500);
  320. expPerLevel.push_back(20600);
  321. expPerLevel.push_back(24320);
  322. expPerLevel.push_back(28784);
  323. expPerLevel.push_back(34140);
  324. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  325. {
  326. int i = expPerLevel.size() - 1;
  327. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  328. }
  329. expPerLevel.pop_back();//last value is broken
  330. //ballistics info
  331. buf = bitmaph->getTextFile("BALLIST.TXT");
  332. it = 0;
  333. for(int i=0; i<22; ++i)
  334. {
  335. loadToIt(dump,buf,it,4);
  336. }
  337. for(int lvl=0; lvl<4; ++lvl)
  338. {
  339. CHeroHandler::SBallisticsLevelInfo bli;
  340. si32 tempNum;
  341. loadToIt(tempNum,buf,it,4);
  342. bli.keep = tempNum;
  343. loadToIt(tempNum,buf,it,4);
  344. bli.tower = tempNum;
  345. loadToIt(tempNum,buf,it,4);
  346. bli.gate = tempNum;
  347. loadToIt(tempNum,buf,it,4);
  348. bli.wall = tempNum;
  349. loadToIt(tempNum,buf,it,4);
  350. bli.shots = tempNum;
  351. loadToIt(tempNum,buf,it,4);
  352. bli.noDmg = tempNum;
  353. loadToIt(tempNum,buf,it,4);
  354. bli.oneDmg = tempNum;
  355. loadToIt(tempNum,buf,it,4);
  356. bli.twoDmg = tempNum;
  357. loadToIt(tempNum,buf,it,4);
  358. bli.sum = tempNum;
  359. if(lvl!=3)
  360. {
  361. loadToIt(dump,buf,it,4);
  362. }
  363. ballistics.push_back(bli);
  364. }
  365. {
  366. it = 0;
  367. std::ifstream inp;
  368. dump.clear();
  369. inp.open(DATA_DIR "/config/specials.txt"); //loading hero specials
  370. assert(inp);
  371. SSpecialtyInfo dummy;
  372. si32 hid;
  373. inp.ignore(100, '\n');
  374. for (int i = 0; i < 175; ++i)
  375. {
  376. inp >> hid;
  377. inp >> dummy.type;
  378. inp >> dummy.val;
  379. inp >> dummy.subtype;
  380. inp >> dummy.additionalinfo;
  381. heroes[hid]->spec.push_back(dummy); //put a copy of dummy
  382. }
  383. inp.close();
  384. }
  385. }
  386. void CHeroHandler::loadHeroClasses()
  387. {
  388. std::istringstream str(bitmaph->getTextFile("HCTRAITS.TXT")); //we'll be reading from it
  389. const int BUFFER_SIZE = 5000;
  390. char buffer[BUFFER_SIZE+1];
  391. for(int i=0; i<3; ++i) str.getline(buffer, BUFFER_SIZE); //omiting rubbish
  392. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  393. {
  394. CHeroClass * hc = new CHeroClass;
  395. hc->alignment = ss / 6;
  396. char name[BUFFER_SIZE+1];
  397. str.get(name, BUFFER_SIZE, '\t');
  398. hc->name = name;
  399. //workaround for locale issue (different localisations use different decimal separator)
  400. int intPart,fracPart;
  401. str >> intPart;
  402. str.ignore();//ignore decimal separator
  403. str >> fracPart;
  404. hc->aggression = intPart + fracPart/100.0f;
  405. str >> hc->initialAttack;
  406. str >> hc->initialDefence;
  407. str >> hc->initialPower;
  408. str >> hc->initialKnowledge;
  409. hc->primChance.resize(PRIMARY_SKILLS);
  410. for(int x=0; x<PRIMARY_SKILLS; ++x)
  411. {
  412. str >> hc->primChance[x].first;
  413. }
  414. for(int x=0; x<PRIMARY_SKILLS; ++x)
  415. {
  416. str >> hc->primChance[x].second;
  417. }
  418. hc->proSec.resize(SKILL_QUANTITY);
  419. for(int dd=0; dd<SKILL_QUANTITY; ++dd)
  420. {
  421. str >> hc->proSec[dd];
  422. }
  423. for(int dd=0; dd<ARRAY_COUNT(hc->selectionProbability); ++dd)
  424. {
  425. str >> hc->selectionProbability[dd];
  426. }
  427. heroClasses.push_back(hc);
  428. str.getline(buffer, BUFFER_SIZE); //removing end of line characters
  429. }
  430. }
  431. void CHeroHandler::initHeroClasses()
  432. {
  433. for(int gg=0; gg<heroes.size(); ++gg)
  434. {
  435. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  436. }
  437. initTerrainCosts();
  438. loadNativeTerrains();
  439. }
  440. unsigned int CHeroHandler::level (ui64 experience) const
  441. {
  442. int i;
  443. if (experience <= expPerLevel.back())
  444. {
  445. for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
  446. return i + 1;
  447. }
  448. else
  449. {
  450. i = expPerLevel.size() - 1;
  451. while (experience > reqExp (i))
  452. i++;
  453. return i;
  454. }
  455. }
  456. ui64 CHeroHandler::reqExp (unsigned int level) const
  457. {
  458. if(!level)
  459. return 0;
  460. if (level <= expPerLevel.size())
  461. {
  462. return expPerLevel[level-1];
  463. }
  464. else
  465. {
  466. tlog3 << "A hero has reached unsupported amount of experience\n";
  467. return expPerLevel[expPerLevel.size()-1];
  468. }
  469. }
  470. void CHeroHandler::initTerrainCosts()
  471. {
  472. std::ifstream inp;
  473. inp.open(DATA_DIR "/config/TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  474. if(!inp.is_open())
  475. {
  476. tlog1 << "Error while opening config/TERCOSTS.TXT file!" << std::endl;
  477. }
  478. int tynum;
  479. inp>>tynum;
  480. for(int i=0; i<2*tynum; i+=2)
  481. {
  482. int catNum;
  483. inp>>catNum;
  484. for(int k=0; k<catNum; ++k)
  485. {
  486. int curCost;
  487. inp>>curCost;
  488. heroClasses[i]->terrCosts.push_back(curCost);
  489. heroClasses[i+1]->terrCosts.push_back(curCost);
  490. }
  491. }
  492. inp.close();
  493. }
  494. void CHeroHandler::loadNativeTerrains()
  495. {
  496. std::ifstream inp;
  497. inp.open(DATA_DIR "/config/native_terrains.txt", std::ios_base::in|std::ios_base::binary);
  498. if(!inp.is_open())
  499. {
  500. tlog1 << "Error while opening config/native_terrains.txt file!" << std::endl;
  501. }
  502. const int MAX_ELEM = 1000;
  503. char buf[MAX_ELEM+1];
  504. inp.getline(buf, MAX_ELEM);
  505. inp.getline(buf, MAX_ELEM);
  506. nativeTerrains.resize(F_NUMBER);
  507. for(int i=0; i<F_NUMBER; ++i)
  508. {
  509. int faction, terrain;
  510. inp >> faction;
  511. inp >> terrain;
  512. nativeTerrains[faction] = terrain;
  513. }
  514. inp.close();
  515. }
  516. CHero::CHero()
  517. {
  518. startingSpell = -1;
  519. }
  520. CHero::~CHero()
  521. {
  522. }