CHeroHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. //loadObstacles(config["moats"], true, moats);
  107. }
  108. void CHeroHandler::loadPuzzleInfo()
  109. {
  110. const JsonNode config(GameConstants::DATA_DIR + "/config/puzzle_map.json");
  111. int faction = 0;
  112. BOOST_FOREACH(const JsonNode &puzzle, config["puzzles"].Vector())
  113. {
  114. int idx = 0;
  115. BOOST_FOREACH(const JsonNode &piece, puzzle.Vector())
  116. {
  117. SPuzzleInfo spi;
  118. spi.x = piece["x"].Float();
  119. spi.y = piece["y"].Float();
  120. spi.whenUncovered = piece["order"].Float();
  121. spi.number = idx;
  122. // filename calculation
  123. std::ostringstream suffix;
  124. suffix << std::setfill('0') << std::setw(2);
  125. suffix << idx << ".BMP";
  126. static const std::string factionToInfix[GameConstants::F_NUMBER] = {"CAS", "RAM", "TOW", "INF", "NEC", "DUN", "STR", "FOR", "ELE"};
  127. spi.filename = "PUZ" + factionToInfix[faction] + suffix.str();
  128. puzzleInfo[faction].push_back(spi);
  129. idx ++;
  130. }
  131. assert(idx == PUZZLES_PER_FACTION);
  132. faction ++;
  133. }
  134. assert(faction == GameConstants::F_NUMBER);
  135. }
  136. void CHeroHandler::loadHeroes()
  137. {
  138. VLC->heroh = this;
  139. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  140. int it=0;
  141. std::string dump;
  142. for(int i=0; i<2; ++i)
  143. {
  144. loadToIt(dump,buf,it,3);
  145. }
  146. int numberOfCurrentClassHeroes = 0;
  147. int currentClass = 0;
  148. int additHero = 0;
  149. CHero::EHeroClasses addTab[12];
  150. addTab[0] = CHero::KNIGHT;
  151. addTab[1] = CHero::WITCH;
  152. addTab[2] = CHero::KNIGHT;
  153. addTab[3] = CHero::WIZARD;
  154. addTab[4] = CHero::RANGER;
  155. addTab[5] = CHero::BARBARIAN;
  156. addTab[6] = CHero::DEATHKNIGHT;
  157. addTab[7] = CHero::WARLOCK;
  158. addTab[8] = CHero::KNIGHT;
  159. addTab[9] = CHero::WARLOCK;
  160. addTab[10] = CHero::BARBARIAN;
  161. addTab[11] = CHero::DEMONIAC;
  162. for (int i=0; i<GameConstants::HEROES_QUANTITY; i++)
  163. {
  164. CHero * nher = new CHero;
  165. if(currentClass<18)
  166. {
  167. nher->heroType = static_cast<CHero::EHeroClasses>(currentClass);
  168. ++numberOfCurrentClassHeroes;
  169. if(numberOfCurrentClassHeroes==8)
  170. {
  171. numberOfCurrentClassHeroes = 0;
  172. ++currentClass;
  173. }
  174. }
  175. else
  176. {
  177. nher->heroType = addTab[additHero++];
  178. }
  179. std::string pom ;
  180. loadToIt(nher->name,buf,it,4);
  181. for(int x=0;x<3;x++)
  182. {
  183. loadToIt(pom,buf,it,4);
  184. nher->lowStack[x] = atoi(pom.c_str());
  185. loadToIt(pom,buf,it,4);
  186. nher->highStack[x] = atoi(pom.c_str());
  187. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  188. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  189. if(hlp>=0)
  190. nher->refTypeStack[x].replace(hlp,1,"");
  191. }
  192. nher->ID = heroes.size();
  193. heroes.push_back(nher);
  194. }
  195. // Load heroes information
  196. const JsonNode config(GameConstants::DATA_DIR + "/config/heroes.json");
  197. BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
  198. int hid = hero["id"].Float();
  199. const JsonNode *value;
  200. // sex: 0=male, 1=female
  201. heroes[hid]->sex = !!hero["female"].Bool();
  202. BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) {
  203. heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float()));
  204. }
  205. value = &hero["spell"];
  206. if (!value->isNull()) {
  207. heroes[hid]->startingSpell = value->Float();
  208. }
  209. value = &hero["specialties"];
  210. if (!value->isNull()) {
  211. BOOST_FOREACH(const JsonNode &specialty, value->Vector()) {
  212. SSpecialtyInfo dummy;
  213. dummy.type = specialty["type"].Float();
  214. dummy.val = specialty["val"].Float();
  215. dummy.subtype = specialty["subtype"].Float();
  216. dummy.additionalinfo = specialty["info"].Float();
  217. heroes[hid]->spec.push_back(dummy); //put a copy of dummy
  218. }
  219. }
  220. }
  221. loadHeroClasses();
  222. initHeroClasses();
  223. expPerLevel.push_back(0);
  224. expPerLevel.push_back(1000);
  225. expPerLevel.push_back(2000);
  226. expPerLevel.push_back(3200);
  227. expPerLevel.push_back(4600);
  228. expPerLevel.push_back(6200);
  229. expPerLevel.push_back(8000);
  230. expPerLevel.push_back(10000);
  231. expPerLevel.push_back(12200);
  232. expPerLevel.push_back(14700);
  233. expPerLevel.push_back(17500);
  234. expPerLevel.push_back(20600);
  235. expPerLevel.push_back(24320);
  236. expPerLevel.push_back(28784);
  237. expPerLevel.push_back(34140);
  238. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  239. {
  240. int i = expPerLevel.size() - 1;
  241. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  242. }
  243. expPerLevel.pop_back();//last value is broken
  244. //ballistics info
  245. buf = bitmaph->getTextFile("BALLIST.TXT");
  246. it = 0;
  247. for(int i=0; i<22; ++i)
  248. {
  249. loadToIt(dump,buf,it,4);
  250. }
  251. for(int lvl=0; lvl<4; ++lvl)
  252. {
  253. CHeroHandler::SBallisticsLevelInfo bli;
  254. si32 tempNum;
  255. loadToIt(tempNum,buf,it,4);
  256. bli.keep = tempNum;
  257. loadToIt(tempNum,buf,it,4);
  258. bli.tower = tempNum;
  259. loadToIt(tempNum,buf,it,4);
  260. bli.gate = tempNum;
  261. loadToIt(tempNum,buf,it,4);
  262. bli.wall = tempNum;
  263. loadToIt(tempNum,buf,it,4);
  264. bli.shots = tempNum;
  265. loadToIt(tempNum,buf,it,4);
  266. bli.noDmg = tempNum;
  267. loadToIt(tempNum,buf,it,4);
  268. bli.oneDmg = tempNum;
  269. loadToIt(tempNum,buf,it,4);
  270. bli.twoDmg = tempNum;
  271. loadToIt(tempNum,buf,it,4);
  272. bli.sum = tempNum;
  273. if(lvl!=3)
  274. {
  275. loadToIt(dump,buf,it,4);
  276. }
  277. ballistics.push_back(bli);
  278. }
  279. }
  280. void CHeroHandler::loadHeroClasses()
  281. {
  282. std::istringstream str(bitmaph->getTextFile("HCTRAITS.TXT")); //we'll be reading from it
  283. const int BUFFER_SIZE = 5000;
  284. char buffer[BUFFER_SIZE+1];
  285. for(int i=0; i<3; ++i) str.getline(buffer, BUFFER_SIZE); //omitting rubbish
  286. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  287. {
  288. CHeroClass * hc = new CHeroClass;
  289. hc->alignment = ss / 6;
  290. char name[BUFFER_SIZE+1];
  291. str.get(name, BUFFER_SIZE, '\t');
  292. hc->name = name;
  293. //workaround for locale issue (different localisations use different decimal separator)
  294. int intPart,fracPart;
  295. str >> intPart;
  296. str.ignore();//ignore decimal separator
  297. str >> fracPart;
  298. hc->aggression = intPart + fracPart/100.0;
  299. str >> hc->initialAttack;
  300. str >> hc->initialDefence;
  301. str >> hc->initialPower;
  302. str >> hc->initialKnowledge;
  303. hc->primChance.resize(GameConstants::PRIMARY_SKILLS);
  304. for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
  305. {
  306. str >> hc->primChance[x].first;
  307. }
  308. for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
  309. {
  310. str >> hc->primChance[x].second;
  311. }
  312. hc->proSec.resize(GameConstants::SKILL_QUANTITY);
  313. for(int dd=0; dd<GameConstants::SKILL_QUANTITY; ++dd)
  314. {
  315. str >> hc->proSec[dd];
  316. }
  317. for(int dd=0; dd<ARRAY_COUNT(hc->selectionProbability); ++dd)
  318. {
  319. str >> hc->selectionProbability[dd];
  320. }
  321. heroClasses.push_back(hc);
  322. str.getline(buffer, BUFFER_SIZE); //removing end of line characters
  323. }
  324. }
  325. void CHeroHandler::initHeroClasses()
  326. {
  327. for(int gg=0; gg<heroes.size(); ++gg)
  328. {
  329. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  330. }
  331. loadTerrains();
  332. }
  333. ui32 CHeroHandler::level (ui64 experience) const
  334. {
  335. int i;
  336. if (experience <= expPerLevel.back())
  337. {
  338. for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
  339. return i + 1;
  340. }
  341. else
  342. {
  343. i = expPerLevel.size() - 1;
  344. while (experience > reqExp (i))
  345. i++;
  346. return i;
  347. }
  348. }
  349. ui64 CHeroHandler::reqExp (ui32 level) const
  350. {
  351. if(!level)
  352. return 0;
  353. if (level <= expPerLevel.size())
  354. {
  355. return expPerLevel[level-1];
  356. }
  357. else
  358. {
  359. tlog3 << "A hero has reached unsupported amount of experience\n";
  360. return expPerLevel[expPerLevel.size()-1];
  361. }
  362. }
  363. void CHeroHandler::loadTerrains()
  364. {
  365. int faction = 0;
  366. const JsonNode config(GameConstants::DATA_DIR + "/config/terrains.json");
  367. nativeTerrains.resize(GameConstants::F_NUMBER);
  368. BOOST_FOREACH(const JsonNode &terrain, config["terrains"].Vector()) {
  369. BOOST_FOREACH(const JsonNode &cost, terrain["costs"].Vector()) {
  370. int curCost = cost.Float();
  371. heroClasses[2*faction]->terrCosts.push_back(curCost);
  372. heroClasses[2*faction+1]->terrCosts.push_back(curCost);
  373. }
  374. nativeTerrains[faction] = terrain["native"].Float();
  375. faction ++;
  376. }
  377. assert(faction == GameConstants::F_NUMBER);
  378. }
  379. CHero::CHero()
  380. {
  381. startingSpell = -1;
  382. sex = 0xff;
  383. }
  384. CHero::~CHero()
  385. {
  386. }