CHeroHandler.cpp 12 KB

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