CHeroHandler.cpp 12 KB

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