CHeroHandler.cpp 12 KB

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