CHeroHandler.cpp 12 KB

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