CHeroHandler.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "StdInc.h"
  2. #include "CHeroHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "Filesystem/CResourceLoader.h"
  5. #include "VCMI_Lib.h"
  6. #include "JsonNode.h"
  7. #include "StringConstants.h"
  8. #include "BattleHex.h"
  9. #include "CModHandler.h"
  10. #include "CTownHandler.h"
  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. }
  23. CHeroClass::~CHeroClass()
  24. {
  25. }
  26. int CHeroClass::chooseSecSkill(const std::set<int> & possibles) const //picks secondary skill out from given possibilities
  27. {
  28. if(possibles.size()==1)
  29. return *possibles.begin();
  30. int totalProb = 0;
  31. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  32. {
  33. totalProb += secSkillProbability[*i];
  34. }
  35. int ran = rand()%totalProb;
  36. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  37. {
  38. ran -= secSkillProbability[*i];
  39. if(ran<0)
  40. return *i;
  41. }
  42. throw std::runtime_error("Cannot pick secondary skill!");
  43. }
  44. EAlignment::EAlignment CHeroClass::getAlignment() const
  45. {
  46. return EAlignment::EAlignment(VLC->townh->factions[faction].alignment);
  47. }
  48. std::vector<BattleHex> CObstacleInfo::getBlocked(BattleHex hex) const
  49. {
  50. std::vector<BattleHex> ret;
  51. if(isAbsoluteObstacle)
  52. {
  53. assert(!hex.isValid());
  54. range::copy(blockedTiles, std::back_inserter(ret));
  55. return ret;
  56. }
  57. BOOST_FOREACH(int offset, blockedTiles)
  58. {
  59. BattleHex toBlock = hex + offset;
  60. if((hex.getY() & 1) && !(toBlock.getY() & 1))
  61. toBlock += BattleHex::LEFT;
  62. if(!toBlock.isValid())
  63. tlog1 << "Misplaced obstacle!\n";
  64. else
  65. ret.push_back(toBlock);
  66. }
  67. return ret;
  68. }
  69. bool CObstacleInfo::isAppropriate(int terrainType, int specialBattlefield /*= -1*/) const
  70. {
  71. if(specialBattlefield != -1)
  72. return vstd::contains(allowedSpecialBfields, specialBattlefield);
  73. return vstd::contains(allowedTerrains, terrainType);
  74. }
  75. void CHeroClassHandler::load()
  76. {
  77. CLegacyConfigParser parser("DATA/HCTRAITS.TXT");
  78. parser.endLine(); // header
  79. parser.endLine();
  80. do
  81. {
  82. CHeroClass * hc = new CHeroClass;
  83. hc->name = parser.readString();
  84. hc->aggression = parser.readNumber();
  85. hc->id = heroClasses.size();
  86. hc->primarySkillInitial = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  87. hc->primarySkillLowLevel = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  88. hc->primarySkillHighLevel = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  89. hc->secSkillProbability = parser.readNumArray<int>(GameConstants::SKILL_QUANTITY);
  90. for(int dd=0; dd<GameConstants::F_NUMBER; ++dd)
  91. {
  92. hc->selectionProbability[dd] = parser.readNumber();
  93. }
  94. VLC->modh->identifiers.requestIdentifier("faction." + ETownType::names[heroClasses.size()/2],
  95. [=](si32 faction)
  96. {
  97. hc->faction = faction;
  98. });
  99. heroClasses.push_back(hc);
  100. VLC->modh->identifiers.registerObject("heroClass." + GameConstants::HERO_CLASSES_NAMES[hc->id], hc->id);
  101. }
  102. while (parser.endLine() && !parser.isNextEntryEmpty());
  103. }
  104. void CHeroClassHandler::load(const JsonNode & classes)
  105. {
  106. //TODO
  107. }
  108. CHeroClass *CHeroClassHandler::loadClass(const JsonNode & heroClass)
  109. {
  110. //TODO
  111. return new CHeroClass;
  112. }
  113. CHeroClassHandler::~CHeroClassHandler()
  114. {
  115. BOOST_FOREACH(auto heroClass, heroClasses)
  116. {
  117. delete heroClass.get();
  118. }
  119. }
  120. CHeroHandler::~CHeroHandler()
  121. {
  122. BOOST_FOREACH(auto hero, heroes)
  123. delete hero.get();
  124. }
  125. CHeroHandler::CHeroHandler()
  126. {}
  127. void CHeroHandler::load(const JsonNode & heroes)
  128. {
  129. //TODO
  130. }
  131. CHero * CHeroHandler::loadHero(const JsonNode & hero)
  132. {
  133. //TODO
  134. return new CHero;
  135. }
  136. void CHeroHandler::load()
  137. {
  138. classes.load();
  139. loadHeroes();
  140. loadObstacles();
  141. loadTerrains();
  142. loadBallistics();
  143. loadExperience();
  144. }
  145. void CHeroHandler::loadExperience()
  146. {
  147. expPerLevel.push_back(0);
  148. expPerLevel.push_back(1000);
  149. expPerLevel.push_back(2000);
  150. expPerLevel.push_back(3200);
  151. expPerLevel.push_back(4600);
  152. expPerLevel.push_back(6200);
  153. expPerLevel.push_back(8000);
  154. expPerLevel.push_back(10000);
  155. expPerLevel.push_back(12200);
  156. expPerLevel.push_back(14700);
  157. expPerLevel.push_back(17500);
  158. expPerLevel.push_back(20600);
  159. expPerLevel.push_back(24320);
  160. expPerLevel.push_back(28784);
  161. expPerLevel.push_back(34140);
  162. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  163. {
  164. int i = expPerLevel.size() - 1;
  165. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  166. }
  167. expPerLevel.pop_back();//last value is broken
  168. }
  169. void CHeroHandler::loadObstacles()
  170. {
  171. auto loadObstacles = [](const JsonNode &node, bool absolute, std::map<int, CObstacleInfo> &out)
  172. {
  173. BOOST_FOREACH(const JsonNode &obs, node.Vector())
  174. {
  175. int ID = obs["id"].Float();
  176. CObstacleInfo & obi = out[ID];
  177. obi.ID = ID;
  178. obi.defName = obs["defname"].String();
  179. obi.width = obs["width"].Float();
  180. obi.height = obs["height"].Float();
  181. obi.allowedTerrains = obs["allowedTerrain"].convertTo<std::vector<ui8> >();
  182. obi.allowedSpecialBfields = obs["specialBattlefields"].convertTo<std::vector<ui8> >();
  183. obi.blockedTiles = obs["blockedTiles"].convertTo<std::vector<si16> >();
  184. obi.isAbsoluteObstacle = absolute;
  185. }
  186. };
  187. const JsonNode config(ResourceID("config/obstacles.json"));
  188. loadObstacles(config["obstacles"], false, obstacles);
  189. loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
  190. //loadObstacles(config["moats"], true, moats);
  191. }
  192. void CHeroHandler::loadHeroes()
  193. {
  194. VLC->heroh = this;
  195. CLegacyConfigParser parser("DATA/HOTRAITS.TXT");
  196. parser.endLine(); //ignore header
  197. parser.endLine();
  198. for (int i=0; i<GameConstants::HEROES_QUANTITY; i++)
  199. {
  200. CHero * hero = new CHero;
  201. hero->name = parser.readString();
  202. for(int x=0;x<3;x++)
  203. {
  204. hero->initialArmy[x].minAmount = parser.readNumber();
  205. hero->initialArmy[x].maxAmount = parser.readNumber();
  206. std::string refName = parser.readString();
  207. boost::algorithm::replace_all(refName, " ", ""); //remove spaces
  208. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + refName, [=](si32 creature)
  209. {
  210. hero->initialArmy[x].creature = creature;
  211. });
  212. }
  213. parser.endLine();
  214. hero->ID = heroes.size();
  215. heroes.push_back(hero);
  216. }
  217. // Load heroes information
  218. const JsonNode config(ResourceID("config/heroes.json"));
  219. BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
  220. CHero * currentHero = heroes[hero["id"].Float()];
  221. const JsonNode *value;
  222. // sex: 0=male, 1=female
  223. currentHero->sex = !!hero["female"].Bool();
  224. BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector())
  225. {
  226. int skillID = boost::range::find(SecondarySkill::names, set["skill"].String()) - boost::begin(SecondarySkill::names);
  227. int skillLevel = boost::range::find(SecondarySkill::levels, set["level"].String()) - boost::begin(SecondarySkill::levels);
  228. currentHero->secSkillsInit.push_back(std::make_pair(skillID, skillLevel));
  229. }
  230. value = &hero["spell"];
  231. if (!value->isNull()) {
  232. currentHero->startingSpell = value->Float();
  233. }
  234. BOOST_FOREACH(const JsonNode &specialty, hero["specialties"].Vector())
  235. {
  236. SSpecialtyInfo dummy;
  237. dummy.type = specialty["type"].Float();
  238. dummy.val = specialty["val"].Float();
  239. dummy.subtype = specialty["subtype"].Float();
  240. dummy.additionalinfo = specialty["info"].Float();
  241. currentHero->spec.push_back(dummy); //put a copy of dummy
  242. }
  243. VLC->modh->identifiers.requestIdentifier("heroClass." + hero["class"].String(),
  244. [=](si32 classID)
  245. {
  246. currentHero->heroClass = classes.heroClasses[classID];
  247. });
  248. }
  249. }
  250. void CHeroHandler::loadBallistics()
  251. {
  252. CLegacyConfigParser ballParser("DATA/BALLIST.TXT");
  253. ballParser.endLine(); //header
  254. ballParser.endLine();
  255. do
  256. {
  257. ballParser.readString();
  258. ballParser.readString();
  259. CHeroHandler::SBallisticsLevelInfo bli;
  260. bli.keep = ballParser.readNumber();
  261. bli.tower = ballParser.readNumber();
  262. bli.gate = ballParser.readNumber();
  263. bli.wall = ballParser.readNumber();
  264. bli.shots = ballParser.readNumber();
  265. bli.noDmg = ballParser.readNumber();
  266. bli.oneDmg = ballParser.readNumber();
  267. bli.twoDmg = ballParser.readNumber();
  268. bli.sum = ballParser.readNumber();
  269. ballistics.push_back(bli);
  270. }
  271. while (ballParser.endLine());
  272. }
  273. ui32 CHeroHandler::level (ui64 experience) const
  274. {
  275. int i;
  276. if (experience <= expPerLevel.back())
  277. {
  278. for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
  279. return i + 1;
  280. }
  281. else
  282. {
  283. i = expPerLevel.size() - 1;
  284. while (experience > reqExp (i))
  285. i++;
  286. return i;
  287. }
  288. }
  289. ui64 CHeroHandler::reqExp (ui32 level) const
  290. {
  291. if(!level)
  292. return 0;
  293. if (level <= expPerLevel.size())
  294. {
  295. return expPerLevel[level-1];
  296. }
  297. else
  298. {
  299. tlog3 << "A hero has reached unsupported amount of experience\n";
  300. return expPerLevel[expPerLevel.size()-1];
  301. }
  302. }
  303. void CHeroHandler::loadTerrains()
  304. {
  305. const JsonNode config(ResourceID("config/terrains.json"));
  306. terrCosts.reserve(GameConstants::TERRAIN_TYPES);
  307. BOOST_FOREACH(const std::string & name, GameConstants::TERRAIN_NAMES)
  308. terrCosts.push_back(config[name]["moveCost"].Float());
  309. }
  310. std::vector<ui8> CHeroHandler::getDefaultAllowedHeroes() const
  311. {
  312. // Look Data/HOTRAITS.txt for reference
  313. std::vector<ui8> allowedHeroes;
  314. allowedHeroes.resize(156, 1);
  315. for(int i = 145; i < 156; ++i)
  316. {
  317. allowedHeroes[i] = 0;
  318. }
  319. allowedHeroes[4] = 0;
  320. allowedHeroes[25] = 0;
  321. return allowedHeroes;
  322. }
  323. CHero::CHero()
  324. {
  325. startingSpell = -1;
  326. sex = 0xff;
  327. }
  328. CHero::~CHero()
  329. {
  330. }