CHeroHandler.cpp 11 KB

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