2
0

CHeroHandler.cpp 12 KB

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