CHeroHandler.cpp 10 KB

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