CHeroHandler.cpp 12 KB

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