CHeroHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. const JsonNode config(DATA_DIR "/config/obstacles.json");
  136. BOOST_FOREACH(const JsonNode &obs, config["obstacles"].Vector()) {
  137. CObstacleInfo obi;
  138. obi.ID = obs["id"].Float();
  139. obi.defName = obs["defname"].String();
  140. obi.blockmap = obs["blockmap"].String();
  141. obi.allowedTerrains = obs["terrains"].String();
  142. obi.posShift.first = obs["shift_x"].Float();
  143. obi.posShift.second = obs["shift_y"].Float();
  144. obstacles[obi.ID] = obi;
  145. }
  146. }
  147. void CHeroHandler::loadPuzzleInfo()
  148. {
  149. const JsonNode config(DATA_DIR "/config/puzzle_map.json");
  150. int faction = 0;
  151. BOOST_FOREACH(const JsonNode &puzzle, config["puzzles"].Vector()) {
  152. int idx = 0;
  153. BOOST_FOREACH(const JsonNode &piece, puzzle.Vector()) {
  154. SPuzzleInfo spi;
  155. spi.x = piece["x"].Float();
  156. spi.y = piece["y"].Float();
  157. spi.whenUncovered = piece["order"].Float();
  158. spi.number = idx;
  159. // filename calculation
  160. std::ostringstream suffix;
  161. suffix << std::setfill('0') << std::setw(2);
  162. suffix << idx << ".BMP";
  163. static const std::string factionToInfix[F_NUMBER] = {"CAS", "RAM", "TOW", "INF", "NEC", "DUN", "STR", "FOR", "ELE"};
  164. spi.filename = "PUZ" + factionToInfix[faction] + suffix.str();
  165. puzzleInfo[faction].push_back(spi);
  166. idx ++;
  167. }
  168. assert(idx == PUZZLES_PER_FACTION);
  169. faction ++;
  170. }
  171. assert(faction == F_NUMBER);
  172. }
  173. void CHeroHandler::loadHeroes()
  174. {
  175. VLC->heroh = this;
  176. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  177. int it=0;
  178. std::string dump;
  179. for(int i=0; i<2; ++i)
  180. {
  181. loadToIt(dump,buf,it,3);
  182. }
  183. int numberOfCurrentClassHeroes = 0;
  184. int currentClass = 0;
  185. int additHero = 0;
  186. CHero::EHeroClasses addTab[12];
  187. addTab[0] = CHero::KNIGHT;
  188. addTab[1] = CHero::WITCH;
  189. addTab[2] = CHero::KNIGHT;
  190. addTab[3] = CHero::WIZARD;
  191. addTab[4] = CHero::RANGER;
  192. addTab[5] = CHero::BARBARIAN;
  193. addTab[6] = CHero::DEATHKNIGHT;
  194. addTab[7] = CHero::WARLOCK;
  195. addTab[8] = CHero::KNIGHT;
  196. addTab[9] = CHero::WARLOCK;
  197. addTab[10] = CHero::BARBARIAN;
  198. addTab[11] = CHero::DEMONIAC;
  199. for (int i=0; i<HEROES_QUANTITY; i++)
  200. {
  201. CHero * nher = new CHero;
  202. if(currentClass<18)
  203. {
  204. nher->heroType = static_cast<CHero::EHeroClasses>(currentClass);
  205. ++numberOfCurrentClassHeroes;
  206. if(numberOfCurrentClassHeroes==8)
  207. {
  208. numberOfCurrentClassHeroes = 0;
  209. ++currentClass;
  210. }
  211. }
  212. else
  213. {
  214. nher->heroType = addTab[additHero++];
  215. }
  216. std::string pom ;
  217. loadToIt(nher->name,buf,it,4);
  218. for(int x=0;x<3;x++)
  219. {
  220. loadToIt(pom,buf,it,4);
  221. nher->lowStack[x] = atoi(pom.c_str());
  222. loadToIt(pom,buf,it,4);
  223. nher->highStack[x] = atoi(pom.c_str());
  224. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  225. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  226. if(hlp>=0)
  227. nher->refTypeStack[x].replace(hlp,1,"");
  228. }
  229. nher->ID = heroes.size();
  230. heroes.push_back(nher);
  231. }
  232. // Load heroes information
  233. const JsonNode config(DATA_DIR "/config/heroes.json");
  234. BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
  235. int hid = hero["id"].Float();
  236. const JsonNode *value;
  237. // sex: 0=male, 1=female
  238. heroes[hid]->sex = !!hero["female"].Bool();
  239. BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) {
  240. heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float()));
  241. }
  242. value = &hero["spell"];
  243. if (!value->isNull()) {
  244. heroes[hid]->startingSpell = value->Float();
  245. }
  246. value = &hero["specialties"];
  247. if (!value->isNull()) {
  248. BOOST_FOREACH(const JsonNode &specialty, value->Vector()) {
  249. SSpecialtyInfo dummy;
  250. dummy.type = specialty["type"].Float();
  251. dummy.val = specialty["val"].Float();
  252. dummy.subtype = specialty["subtype"].Float();
  253. dummy.additionalinfo = specialty["info"].Float();
  254. heroes[hid]->spec.push_back(dummy); //put a copy of dummy
  255. }
  256. }
  257. }
  258. loadHeroClasses();
  259. initHeroClasses();
  260. expPerLevel.push_back(0);
  261. expPerLevel.push_back(1000);
  262. expPerLevel.push_back(2000);
  263. expPerLevel.push_back(3200);
  264. expPerLevel.push_back(4600);
  265. expPerLevel.push_back(6200);
  266. expPerLevel.push_back(8000);
  267. expPerLevel.push_back(10000);
  268. expPerLevel.push_back(12200);
  269. expPerLevel.push_back(14700);
  270. expPerLevel.push_back(17500);
  271. expPerLevel.push_back(20600);
  272. expPerLevel.push_back(24320);
  273. expPerLevel.push_back(28784);
  274. expPerLevel.push_back(34140);
  275. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  276. {
  277. int i = expPerLevel.size() - 1;
  278. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  279. }
  280. expPerLevel.pop_back();//last value is broken
  281. //ballistics info
  282. buf = bitmaph->getTextFile("BALLIST.TXT");
  283. it = 0;
  284. for(int i=0; i<22; ++i)
  285. {
  286. loadToIt(dump,buf,it,4);
  287. }
  288. for(int lvl=0; lvl<4; ++lvl)
  289. {
  290. CHeroHandler::SBallisticsLevelInfo bli;
  291. si32 tempNum;
  292. loadToIt(tempNum,buf,it,4);
  293. bli.keep = tempNum;
  294. loadToIt(tempNum,buf,it,4);
  295. bli.tower = tempNum;
  296. loadToIt(tempNum,buf,it,4);
  297. bli.gate = tempNum;
  298. loadToIt(tempNum,buf,it,4);
  299. bli.wall = tempNum;
  300. loadToIt(tempNum,buf,it,4);
  301. bli.shots = tempNum;
  302. loadToIt(tempNum,buf,it,4);
  303. bli.noDmg = tempNum;
  304. loadToIt(tempNum,buf,it,4);
  305. bli.oneDmg = tempNum;
  306. loadToIt(tempNum,buf,it,4);
  307. bli.twoDmg = tempNum;
  308. loadToIt(tempNum,buf,it,4);
  309. bli.sum = tempNum;
  310. if(lvl!=3)
  311. {
  312. loadToIt(dump,buf,it,4);
  313. }
  314. ballistics.push_back(bli);
  315. }
  316. }
  317. void CHeroHandler::loadHeroClasses()
  318. {
  319. std::istringstream str(bitmaph->getTextFile("HCTRAITS.TXT")); //we'll be reading from it
  320. const int BUFFER_SIZE = 5000;
  321. char buffer[BUFFER_SIZE+1];
  322. for(int i=0; i<3; ++i) str.getline(buffer, BUFFER_SIZE); //omiting rubbish
  323. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  324. {
  325. CHeroClass * hc = new CHeroClass;
  326. hc->alignment = ss / 6;
  327. char name[BUFFER_SIZE+1];
  328. str.get(name, BUFFER_SIZE, '\t');
  329. hc->name = name;
  330. //workaround for locale issue (different localisations use different decimal separator)
  331. int intPart,fracPart;
  332. str >> intPart;
  333. str.ignore();//ignore decimal separator
  334. str >> fracPart;
  335. hc->aggression = intPart + fracPart/100.0f;
  336. str >> hc->initialAttack;
  337. str >> hc->initialDefence;
  338. str >> hc->initialPower;
  339. str >> hc->initialKnowledge;
  340. hc->primChance.resize(PRIMARY_SKILLS);
  341. for(int x=0; x<PRIMARY_SKILLS; ++x)
  342. {
  343. str >> hc->primChance[x].first;
  344. }
  345. for(int x=0; x<PRIMARY_SKILLS; ++x)
  346. {
  347. str >> hc->primChance[x].second;
  348. }
  349. hc->proSec.resize(SKILL_QUANTITY);
  350. for(int dd=0; dd<SKILL_QUANTITY; ++dd)
  351. {
  352. str >> hc->proSec[dd];
  353. }
  354. for(int dd=0; dd<ARRAY_COUNT(hc->selectionProbability); ++dd)
  355. {
  356. str >> hc->selectionProbability[dd];
  357. }
  358. heroClasses.push_back(hc);
  359. str.getline(buffer, BUFFER_SIZE); //removing end of line characters
  360. }
  361. }
  362. void CHeroHandler::initHeroClasses()
  363. {
  364. for(int gg=0; gg<heroes.size(); ++gg)
  365. {
  366. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  367. }
  368. initTerrainCosts();
  369. loadNativeTerrains();
  370. }
  371. unsigned int CHeroHandler::level (ui64 experience) const
  372. {
  373. int i;
  374. if (experience <= expPerLevel.back())
  375. {
  376. for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
  377. return i + 1;
  378. }
  379. else
  380. {
  381. i = expPerLevel.size() - 1;
  382. while (experience > reqExp (i))
  383. i++;
  384. return i;
  385. }
  386. }
  387. ui64 CHeroHandler::reqExp (unsigned int level) const
  388. {
  389. if(!level)
  390. return 0;
  391. if (level <= expPerLevel.size())
  392. {
  393. return expPerLevel[level-1];
  394. }
  395. else
  396. {
  397. tlog3 << "A hero has reached unsupported amount of experience\n";
  398. return expPerLevel[expPerLevel.size()-1];
  399. }
  400. }
  401. void CHeroHandler::initTerrainCosts()
  402. {
  403. std::ifstream inp;
  404. inp.open(DATA_DIR "/config/TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  405. if(!inp.is_open())
  406. {
  407. tlog1 << "Error while opening config/TERCOSTS.TXT file!" << std::endl;
  408. }
  409. int tynum;
  410. inp>>tynum;
  411. for(int i=0; i<2*tynum; i+=2)
  412. {
  413. int catNum;
  414. inp>>catNum;
  415. for(int k=0; k<catNum; ++k)
  416. {
  417. int curCost;
  418. inp>>curCost;
  419. heroClasses[i]->terrCosts.push_back(curCost);
  420. heroClasses[i+1]->terrCosts.push_back(curCost);
  421. }
  422. }
  423. inp.close();
  424. }
  425. void CHeroHandler::loadNativeTerrains()
  426. {
  427. std::ifstream inp;
  428. inp.open(DATA_DIR "/config/native_terrains.txt", std::ios_base::in|std::ios_base::binary);
  429. if(!inp.is_open())
  430. {
  431. tlog1 << "Error while opening config/native_terrains.txt file!" << std::endl;
  432. }
  433. const int MAX_ELEM = 1000;
  434. char buf[MAX_ELEM+1];
  435. inp.getline(buf, MAX_ELEM);
  436. inp.getline(buf, MAX_ELEM);
  437. nativeTerrains.resize(F_NUMBER);
  438. for(int i=0; i<F_NUMBER; ++i)
  439. {
  440. int faction, terrain;
  441. inp >> faction;
  442. inp >> terrain;
  443. nativeTerrains[faction] = terrain;
  444. }
  445. inp.close();
  446. }
  447. CHero::CHero()
  448. {
  449. startingSpell = -1;
  450. sex = 0xff;
  451. }
  452. CHero::~CHero()
  453. {
  454. }