CHeroHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "StdInc.h"
  2. #include "CHeroHandler.h"
  3. #include "CLodHandler.h"
  4. #include "../lib/VCMI_Lib.h"
  5. #include "../lib/JsonNode.h"
  6. #include "GameConstants.h"
  7. #include <boost/version.hpp>
  8. #include "BattleHex.h"
  9. extern CLodHandler * bitmaph;
  10. void loadToIt(std::string &dest, const std::string &src, int &iter, int mode);
  11. /*
  12. * CHeroHandler.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. CHeroClass::CHeroClass()
  21. {
  22. skillLimit = 8;
  23. }
  24. CHeroClass::~CHeroClass()
  25. {
  26. }
  27. int CHeroClass::chooseSecSkill(const std::set<int> & possibles) const //picks secondary skill out from given possibilities
  28. {
  29. if(possibles.size()==1)
  30. return *possibles.begin();
  31. int totalProb = 0;
  32. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  33. {
  34. totalProb += proSec[*i];
  35. }
  36. int ran = rand()%totalProb;
  37. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  38. {
  39. ran -= proSec[*i];
  40. if(ran<0)
  41. return *i;
  42. }
  43. throw std::runtime_error("Cannot pick secondary skill!");
  44. }
  45. EAlignment::EAlignment CHeroClass::getAlignment()
  46. {
  47. return (EAlignment::EAlignment)alignment;
  48. }
  49. std::vector<BattleHex> CObstacleInfo::getBlocked(BattleHex hex) const
  50. {
  51. std::vector<BattleHex> ret;
  52. if(isAbsoluteObstacle)
  53. {
  54. assert(!hex.isValid());
  55. range::copy(blockedTiles, std::back_inserter(ret));
  56. return ret;
  57. }
  58. BOOST_FOREACH(int offset, blockedTiles)
  59. {
  60. BattleHex toBlock = hex + offset;
  61. if((hex.getY() & 1) && !(toBlock.getY() & 1))
  62. toBlock += BattleHex::LEFT;
  63. if(!toBlock.isValid())
  64. tlog1 << "Misplaced obstacle!\n";
  65. else
  66. ret.push_back(toBlock);
  67. }
  68. return ret;
  69. }
  70. bool CObstacleInfo::isAppropriate(int terrainType, int specialBattlefield /*= -1*/) const
  71. {
  72. if(specialBattlefield != -1)
  73. return vstd::contains(allowedSpecialBfields, specialBattlefield);
  74. return vstd::contains(allowedTerrains, terrainType);
  75. }
  76. CHeroHandler::~CHeroHandler()
  77. {
  78. for (int i = 0; i < heroes.size(); i++)
  79. heroes[i].dellNull();
  80. for (int i = 0; i < heroClasses.size(); i++)
  81. delete heroClasses[i];
  82. }
  83. CHeroHandler::CHeroHandler()
  84. {}
  85. void CHeroHandler::loadObstacles()
  86. {
  87. auto loadObstacles = [](const JsonNode &node, bool absolute, std::map<int, CObstacleInfo> &out)
  88. {
  89. BOOST_FOREACH(const JsonNode &obs, node.Vector())
  90. {
  91. int ID = obs["id"].Float();
  92. CObstacleInfo & obi = out[ID];
  93. obi.ID = ID;
  94. obi.defName = obs["defname"].String();
  95. obi.width = obs["width"].Float();
  96. obi.height = obs["height"].Float();
  97. obi.allowedTerrains = obs["allowedTerrain"].StdVector<ui8>();
  98. obi.allowedSpecialBfields = obs["specialBattlefields"].StdVector<ui8>();
  99. obi.blockedTiles = obs["blockedTiles"].StdVector<si16>();
  100. obi.isAbsoluteObstacle = absolute;
  101. }
  102. };
  103. const JsonNode config(GameConstants::DATA_DIR + "/config/obstacles.json");
  104. loadObstacles(config["obstacles"], false, obstacles);
  105. loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
  106. }
  107. void CHeroHandler::loadPuzzleInfo()
  108. {
  109. const JsonNode config(GameConstants::DATA_DIR + "/config/puzzle_map.json");
  110. int faction = 0;
  111. BOOST_FOREACH(const JsonNode &puzzle, config["puzzles"].Vector())
  112. {
  113. int idx = 0;
  114. BOOST_FOREACH(const JsonNode &piece, puzzle.Vector())
  115. {
  116. SPuzzleInfo spi;
  117. spi.x = piece["x"].Float();
  118. spi.y = piece["y"].Float();
  119. spi.whenUncovered = piece["order"].Float();
  120. spi.number = idx;
  121. // filename calculation
  122. std::ostringstream suffix;
  123. suffix << std::setfill('0') << std::setw(2);
  124. suffix << idx << ".BMP";
  125. static const std::string factionToInfix[GameConstants::F_NUMBER] = {"CAS", "RAM", "TOW", "INF", "NEC", "DUN", "STR", "FOR", "ELE"};
  126. spi.filename = "PUZ" + factionToInfix[faction] + suffix.str();
  127. puzzleInfo[faction].push_back(spi);
  128. idx ++;
  129. }
  130. assert(idx == PUZZLES_PER_FACTION);
  131. faction ++;
  132. }
  133. assert(faction == GameConstants::F_NUMBER);
  134. }
  135. void CHeroHandler::loadHeroes()
  136. {
  137. VLC->heroh = this;
  138. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  139. int it=0;
  140. std::string dump;
  141. for(int i=0; i<2; ++i)
  142. {
  143. loadToIt(dump,buf,it,3);
  144. }
  145. int numberOfCurrentClassHeroes = 0;
  146. int currentClass = 0;
  147. int additHero = 0;
  148. CHero::EHeroClasses addTab[12];
  149. addTab[0] = CHero::KNIGHT;
  150. addTab[1] = CHero::WITCH;
  151. addTab[2] = CHero::KNIGHT;
  152. addTab[3] = CHero::WIZARD;
  153. addTab[4] = CHero::RANGER;
  154. addTab[5] = CHero::BARBARIAN;
  155. addTab[6] = CHero::DEATHKNIGHT;
  156. addTab[7] = CHero::WARLOCK;
  157. addTab[8] = CHero::KNIGHT;
  158. addTab[9] = CHero::WARLOCK;
  159. addTab[10] = CHero::BARBARIAN;
  160. addTab[11] = CHero::DEMONIAC;
  161. for (int i=0; i<GameConstants::HEROES_QUANTITY; i++)
  162. {
  163. CHero * nher = new CHero;
  164. if(currentClass<18)
  165. {
  166. nher->heroType = static_cast<CHero::EHeroClasses>(currentClass);
  167. ++numberOfCurrentClassHeroes;
  168. if(numberOfCurrentClassHeroes==8)
  169. {
  170. numberOfCurrentClassHeroes = 0;
  171. ++currentClass;
  172. }
  173. }
  174. else
  175. {
  176. nher->heroType = addTab[additHero++];
  177. }
  178. std::string pom ;
  179. loadToIt(nher->name,buf,it,4);
  180. for(int x=0;x<3;x++)
  181. {
  182. loadToIt(pom,buf,it,4);
  183. nher->lowStack[x] = atoi(pom.c_str());
  184. loadToIt(pom,buf,it,4);
  185. nher->highStack[x] = atoi(pom.c_str());
  186. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  187. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  188. if(hlp>=0)
  189. nher->refTypeStack[x].replace(hlp,1,"");
  190. }
  191. nher->ID = heroes.size();
  192. heroes.push_back(nher);
  193. }
  194. // Load heroes information
  195. const JsonNode config(GameConstants::DATA_DIR + "/config/heroes.json");
  196. BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
  197. int hid = hero["id"].Float();
  198. const JsonNode *value;
  199. // sex: 0=male, 1=female
  200. heroes[hid]->sex = !!hero["female"].Bool();
  201. BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) {
  202. heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float()));
  203. }
  204. value = &hero["spell"];
  205. if (!value->isNull()) {
  206. heroes[hid]->startingSpell = value->Float();
  207. }
  208. value = &hero["specialties"];
  209. if (!value->isNull()) {
  210. BOOST_FOREACH(const JsonNode &specialty, value->Vector()) {
  211. SSpecialtyInfo dummy;
  212. dummy.type = specialty["type"].Float();
  213. dummy.val = specialty["val"].Float();
  214. dummy.subtype = specialty["subtype"].Float();
  215. dummy.additionalinfo = specialty["info"].Float();
  216. heroes[hid]->spec.push_back(dummy); //put a copy of dummy
  217. }
  218. }
  219. }
  220. loadHeroClasses();
  221. initHeroClasses();
  222. expPerLevel.push_back(0);
  223. expPerLevel.push_back(1000);
  224. expPerLevel.push_back(2000);
  225. expPerLevel.push_back(3200);
  226. expPerLevel.push_back(4600);
  227. expPerLevel.push_back(6200);
  228. expPerLevel.push_back(8000);
  229. expPerLevel.push_back(10000);
  230. expPerLevel.push_back(12200);
  231. expPerLevel.push_back(14700);
  232. expPerLevel.push_back(17500);
  233. expPerLevel.push_back(20600);
  234. expPerLevel.push_back(24320);
  235. expPerLevel.push_back(28784);
  236. expPerLevel.push_back(34140);
  237. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  238. {
  239. int i = expPerLevel.size() - 1;
  240. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  241. }
  242. expPerLevel.pop_back();//last value is broken
  243. //ballistics info
  244. buf = bitmaph->getTextFile("BALLIST.TXT");
  245. it = 0;
  246. for(int i=0; i<22; ++i)
  247. {
  248. loadToIt(dump,buf,it,4);
  249. }
  250. for(int lvl=0; lvl<4; ++lvl)
  251. {
  252. CHeroHandler::SBallisticsLevelInfo bli;
  253. si32 tempNum;
  254. loadToIt(tempNum,buf,it,4);
  255. bli.keep = tempNum;
  256. loadToIt(tempNum,buf,it,4);
  257. bli.tower = tempNum;
  258. loadToIt(tempNum,buf,it,4);
  259. bli.gate = tempNum;
  260. loadToIt(tempNum,buf,it,4);
  261. bli.wall = tempNum;
  262. loadToIt(tempNum,buf,it,4);
  263. bli.shots = tempNum;
  264. loadToIt(tempNum,buf,it,4);
  265. bli.noDmg = tempNum;
  266. loadToIt(tempNum,buf,it,4);
  267. bli.oneDmg = tempNum;
  268. loadToIt(tempNum,buf,it,4);
  269. bli.twoDmg = tempNum;
  270. loadToIt(tempNum,buf,it,4);
  271. bli.sum = tempNum;
  272. if(lvl!=3)
  273. {
  274. loadToIt(dump,buf,it,4);
  275. }
  276. ballistics.push_back(bli);
  277. }
  278. }
  279. void CHeroHandler::loadHeroClasses()
  280. {
  281. std::istringstream str(bitmaph->getTextFile("HCTRAITS.TXT")); //we'll be reading from it
  282. const int BUFFER_SIZE = 5000;
  283. char buffer[BUFFER_SIZE+1];
  284. for(int i=0; i<3; ++i) str.getline(buffer, BUFFER_SIZE); //omitting rubbish
  285. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  286. {
  287. CHeroClass * hc = new CHeroClass;
  288. hc->alignment = ss / 6;
  289. char name[BUFFER_SIZE+1];
  290. str.get(name, BUFFER_SIZE, '\t');
  291. hc->name = name;
  292. //workaround for locale issue (different localisations use different decimal separator)
  293. int intPart,fracPart;
  294. str >> intPart;
  295. str.ignore();//ignore decimal separator
  296. str >> fracPart;
  297. hc->aggression = intPart + fracPart/100.0;
  298. str >> hc->initialAttack;
  299. str >> hc->initialDefence;
  300. str >> hc->initialPower;
  301. str >> hc->initialKnowledge;
  302. hc->primChance.resize(GameConstants::PRIMARY_SKILLS);
  303. for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
  304. {
  305. str >> hc->primChance[x].first;
  306. }
  307. for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
  308. {
  309. str >> hc->primChance[x].second;
  310. }
  311. hc->proSec.resize(GameConstants::SKILL_QUANTITY);
  312. for(int dd=0; dd<GameConstants::SKILL_QUANTITY; ++dd)
  313. {
  314. str >> hc->proSec[dd];
  315. }
  316. for(int dd=0; dd<ARRAY_COUNT(hc->selectionProbability); ++dd)
  317. {
  318. str >> hc->selectionProbability[dd];
  319. }
  320. heroClasses.push_back(hc);
  321. str.getline(buffer, BUFFER_SIZE); //removing end of line characters
  322. }
  323. }
  324. void CHeroHandler::initHeroClasses()
  325. {
  326. for(int gg=0; gg<heroes.size(); ++gg)
  327. {
  328. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  329. }
  330. loadTerrains();
  331. }
  332. ui32 CHeroHandler::level (ui64 experience) const
  333. {
  334. int i;
  335. if (experience <= expPerLevel.back())
  336. {
  337. for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
  338. return i + 1;
  339. }
  340. else
  341. {
  342. i = expPerLevel.size() - 1;
  343. while (experience > reqExp (i))
  344. i++;
  345. return i;
  346. }
  347. }
  348. ui64 CHeroHandler::reqExp (ui32 level) const
  349. {
  350. if(!level)
  351. return 0;
  352. if (level <= expPerLevel.size())
  353. {
  354. return expPerLevel[level-1];
  355. }
  356. else
  357. {
  358. tlog3 << "A hero has reached unsupported amount of experience\n";
  359. return expPerLevel[expPerLevel.size()-1];
  360. }
  361. }
  362. void CHeroHandler::loadTerrains()
  363. {
  364. int faction = 0;
  365. const JsonNode config(GameConstants::DATA_DIR + "/config/terrains.json");
  366. nativeTerrains.resize(GameConstants::F_NUMBER);
  367. BOOST_FOREACH(const JsonNode &terrain, config["terrains"].Vector()) {
  368. BOOST_FOREACH(const JsonNode &cost, terrain["costs"].Vector()) {
  369. int curCost = cost.Float();
  370. heroClasses[2*faction]->terrCosts.push_back(curCost);
  371. heroClasses[2*faction+1]->terrCosts.push_back(curCost);
  372. }
  373. nativeTerrains[faction] = terrain["native"].Float();
  374. faction ++;
  375. }
  376. assert(faction == GameConstants::F_NUMBER);
  377. }
  378. CHero::CHero()
  379. {
  380. startingSpell = -1;
  381. sex = 0xff;
  382. }
  383. CHero::~CHero()
  384. {
  385. }