CHeroHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. int CHeroClass::chooseSecSkill(const std::set<int> & possibles) const //picks secondary skill out from given possibilities
  21. {
  22. if(possibles.size()==1)
  23. return *possibles.begin();
  24. int totalProb = 0;
  25. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  26. {
  27. totalProb += secSkillProbability[*i];
  28. }
  29. int ran = rand()%totalProb;
  30. for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  31. {
  32. ran -= secSkillProbability[*i];
  33. if(ran<0)
  34. return *i;
  35. }
  36. throw std::runtime_error("Cannot pick secondary skill!");
  37. }
  38. EAlignment::EAlignment CHeroClass::getAlignment() const
  39. {
  40. return EAlignment::EAlignment(VLC->townh->factions[faction].alignment);
  41. }
  42. std::vector<BattleHex> CObstacleInfo::getBlocked(BattleHex hex) const
  43. {
  44. std::vector<BattleHex> ret;
  45. if(isAbsoluteObstacle)
  46. {
  47. assert(!hex.isValid());
  48. range::copy(blockedTiles, std::back_inserter(ret));
  49. return ret;
  50. }
  51. BOOST_FOREACH(int offset, blockedTiles)
  52. {
  53. BattleHex toBlock = hex + offset;
  54. if((hex.getY() & 1) && !(toBlock.getY() & 1))
  55. toBlock += BattleHex::LEFT;
  56. if(!toBlock.isValid())
  57. tlog1 << "Misplaced obstacle!\n";
  58. else
  59. ret.push_back(toBlock);
  60. }
  61. return ret;
  62. }
  63. bool CObstacleInfo::isAppropriate(int terrainType, int specialBattlefield /*= -1*/) const
  64. {
  65. if(specialBattlefield != -1)
  66. return vstd::contains(allowedSpecialBfields, specialBattlefield);
  67. return vstd::contains(allowedTerrains, terrainType);
  68. }
  69. void CHeroClassHandler::load()
  70. {
  71. CLegacyConfigParser parser("DATA/HCTRAITS.TXT");
  72. parser.endLine(); // header
  73. parser.endLine();
  74. do
  75. {
  76. CHeroClass * hc = new CHeroClass;
  77. hc->name = parser.readString();
  78. hc->aggression = parser.readNumber();
  79. hc->id = heroClasses.size();
  80. hc->primarySkillInitial = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  81. hc->primarySkillLowLevel = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  82. hc->primarySkillHighLevel = parser.readNumArray<int>(GameConstants::PRIMARY_SKILLS);
  83. hc->secSkillProbability = parser.readNumArray<int>(GameConstants::SKILL_QUANTITY);
  84. for(int dd=0; dd<GameConstants::F_NUMBER; ++dd)
  85. {
  86. hc->selectionProbability[dd] = parser.readNumber();
  87. }
  88. VLC->modh->identifiers.requestIdentifier("faction." + ETownType::names[heroClasses.size()/2],
  89. [=](si32 faction)
  90. {
  91. hc->faction = faction;
  92. });
  93. heroClasses.push_back(hc);
  94. VLC->modh->identifiers.registerObject("heroClass." + GameConstants::HERO_CLASSES_NAMES[hc->id], hc->id);
  95. }
  96. while (parser.endLine() && !parser.isNextEntryEmpty());
  97. const JsonNode & heroGraphics = JsonNode(ResourceID("config/heroClasses.json"));
  98. for (size_t i=0; i<heroClasses.size(); i++)
  99. {
  100. const JsonNode & battle = heroGraphics["heroBattleAnim"].Vector()[i/2];
  101. heroClasses[i]->imageBattleFemale = battle["female"].String();
  102. heroClasses[i]->imageBattleMale = battle["male"].String();
  103. const JsonNode & map = heroGraphics["heroMapAnim"].Vector()[i];
  104. heroClasses[i]->imageMapMale = map.String();
  105. heroClasses[i]->imageMapFemale = map.String();
  106. }
  107. }
  108. void CHeroClassHandler::load(const JsonNode & classes)
  109. {
  110. BOOST_FOREACH(auto & entry, classes.Struct())
  111. {
  112. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  113. {
  114. CHeroClass * heroClass = loadClass(entry.second);
  115. heroClass->identifier = entry.first;
  116. heroClass->id = heroClasses.size();
  117. heroClasses.push_back(heroClass);
  118. tlog3 << "Added hero class: " << entry.first << "\n";
  119. VLC->modh->identifiers.registerObject("heroClass." + heroClass->identifier, heroClass->id);
  120. }
  121. }
  122. }
  123. CHeroClass *CHeroClassHandler::loadClass(const JsonNode & node)
  124. {
  125. CHeroClass * heroClass = new CHeroClass;
  126. heroClass->imageBattleFemale = node["animation"]["battle"]["female"].String();
  127. heroClass->imageBattleMale = node["animation"]["battle"]["male"].String();
  128. heroClass->imageMapFemale = node["animation"]["map"]["female"].String();
  129. heroClass->imageMapMale = node["animation"]["map"]["male"].String();
  130. heroClass->name = node["name"].String();
  131. BOOST_FOREACH(const std::string & pSkill, PrimarySkill::names)
  132. {
  133. heroClass->primarySkillInitial.push_back(node["primarySkills"][pSkill].Float());
  134. heroClass->primarySkillLowLevel.push_back(node["lowLevelChance"][pSkill].Float());
  135. heroClass->primarySkillHighLevel.push_back(node["highLevelChance"][pSkill].Float());
  136. }
  137. BOOST_FOREACH(const std::string & secSkill, SecondarySkill::names)
  138. {
  139. heroClass->secSkillProbability.push_back(node["secondarySkills"][secSkill].Float());
  140. }
  141. BOOST_FOREACH(auto & tavern, node["tavern"].Struct())
  142. {
  143. int value = tavern.second.Float();
  144. VLC->modh->identifiers.requestIdentifier("faction." + tavern.first,
  145. [=](si32 factionID)
  146. {
  147. heroClass->selectionProbability[factionID] = value;
  148. });
  149. }
  150. VLC->modh->identifiers.requestIdentifier("faction." + node["faction"].String(),
  151. [=](si32 factionID)
  152. {
  153. heroClass->faction = factionID;
  154. });
  155. return heroClass;
  156. }
  157. CHeroClassHandler::~CHeroClassHandler()
  158. {
  159. BOOST_FOREACH(auto heroClass, heroClasses)
  160. {
  161. delete heroClass.get();
  162. }
  163. }
  164. CHeroHandler::~CHeroHandler()
  165. {
  166. BOOST_FOREACH(auto hero, heroes)
  167. delete hero.get();
  168. }
  169. CHeroHandler::CHeroHandler()
  170. {}
  171. void CHeroHandler::load(const JsonNode & input)
  172. {
  173. BOOST_FOREACH(auto & entry, input.Struct())
  174. {
  175. if (!entry.second.isNull()) // may happens if mod removed creature by setting json entry to null
  176. {
  177. CHero * hero = loadHero(entry.second);
  178. hero->ID = heroes.size();
  179. heroes.push_back(hero);
  180. tlog3 << "Added hero : " << entry.first << "\n";
  181. VLC->modh->identifiers.registerObject("hero." + entry.first, hero->ID);
  182. }
  183. }
  184. }
  185. CHero * CHeroHandler::loadHero(const JsonNode & node)
  186. {
  187. CHero * hero = new CHero;
  188. hero->name = node["texts"]["name"].String();
  189. hero->biography = node["texts"]["biography"].String();
  190. hero->specName = node["texts"]["specialty"]["name"].String();
  191. hero->specTooltip = node["texts"]["specialty"]["tooltip"].String();
  192. hero->specDescr = node["texts"]["specialty"]["description"].String();
  193. hero->imageIndex = node["images"]["index"].Float();
  194. hero->iconSpecSmall = node["images"]["specialtySmall"].String();
  195. hero->iconSpecLarge = node["images"]["specialtyLarge"].String();
  196. hero->portraitSmall = node["images"]["small"].String();
  197. hero->portraitLarge = node["images"]["large"].String();
  198. assert(node["army"].Vector().size() <= 3); // anything bigger is useless - army initialization uses up to 3 slots
  199. hero->initialArmy.resize(node["army"].Vector().size());
  200. for (size_t i=0; i< hero->initialArmy.size(); i++)
  201. {
  202. const JsonNode & source = node["army"].Vector()[i];
  203. hero->initialArmy[i].minAmount = source["min"].Float();
  204. hero->initialArmy[i].maxAmount = source["max"].Float();
  205. assert(hero->initialArmy[i].minAmount <= hero->initialArmy[i].maxAmount);
  206. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + source["creature"].String(), [=](si32 creature)
  207. {
  208. hero->initialArmy[i].creature = creature;
  209. });
  210. }
  211. loadHeroJson(hero, node);
  212. return hero;
  213. }
  214. void CHeroHandler::loadHeroJson(CHero * hero, const JsonNode & node)
  215. {
  216. // sex: 0=male, 1=female
  217. hero->sex = !!node["female"].Bool();
  218. BOOST_FOREACH(const JsonNode &set, node["skills"].Vector())
  219. {
  220. int skillID = boost::range::find(SecondarySkill::names, set["skill"].String()) - boost::begin(SecondarySkill::names);
  221. int skillLevel = boost::range::find(SecondarySkill::levels, set["level"].String()) - boost::begin(SecondarySkill::levels);
  222. hero->secSkillsInit.push_back(std::make_pair(skillID, skillLevel));
  223. }
  224. BOOST_FOREACH(const JsonNode & spell, node["spellbook"].Vector())
  225. {
  226. hero->spells.insert(spell.Float());
  227. }
  228. BOOST_FOREACH(const JsonNode &specialty, node["specialties"].Vector())
  229. {
  230. SSpecialtyInfo spec;
  231. spec.type = specialty["type"].Float();
  232. spec.val = specialty["val"].Float();
  233. spec.subtype = specialty["subtype"].Float();
  234. spec.additionalinfo = specialty["info"].Float();
  235. hero->spec.push_back(spec); //put a copy of dummy
  236. }
  237. VLC->modh->identifiers.requestIdentifier("heroClass." + node["class"].String(),
  238. [=](si32 classID)
  239. {
  240. hero->heroClass = classes.heroClasses[classID];
  241. });
  242. }
  243. void CHeroHandler::load()
  244. {
  245. classes.load();
  246. loadHeroes();
  247. loadHeroTexts();
  248. loadObstacles();
  249. loadTerrains();
  250. loadBallistics();
  251. loadExperience();
  252. }
  253. void CHeroHandler::loadExperience()
  254. {
  255. expPerLevel.push_back(0);
  256. expPerLevel.push_back(1000);
  257. expPerLevel.push_back(2000);
  258. expPerLevel.push_back(3200);
  259. expPerLevel.push_back(4600);
  260. expPerLevel.push_back(6200);
  261. expPerLevel.push_back(8000);
  262. expPerLevel.push_back(10000);
  263. expPerLevel.push_back(12200);
  264. expPerLevel.push_back(14700);
  265. expPerLevel.push_back(17500);
  266. expPerLevel.push_back(20600);
  267. expPerLevel.push_back(24320);
  268. expPerLevel.push_back(28784);
  269. expPerLevel.push_back(34140);
  270. while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
  271. {
  272. int i = expPerLevel.size() - 1;
  273. expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
  274. }
  275. expPerLevel.pop_back();//last value is broken
  276. }
  277. void CHeroHandler::loadObstacles()
  278. {
  279. auto loadObstacles = [](const JsonNode &node, bool absolute, std::map<int, CObstacleInfo> &out)
  280. {
  281. BOOST_FOREACH(const JsonNode &obs, node.Vector())
  282. {
  283. int ID = obs["id"].Float();
  284. CObstacleInfo & obi = out[ID];
  285. obi.ID = ID;
  286. obi.defName = obs["defname"].String();
  287. obi.width = obs["width"].Float();
  288. obi.height = obs["height"].Float();
  289. obi.allowedTerrains = obs["allowedTerrain"].convertTo<std::vector<ui8> >();
  290. obi.allowedSpecialBfields = obs["specialBattlefields"].convertTo<std::vector<ui8> >();
  291. obi.blockedTiles = obs["blockedTiles"].convertTo<std::vector<si16> >();
  292. obi.isAbsoluteObstacle = absolute;
  293. }
  294. };
  295. const JsonNode config(ResourceID("config/obstacles.json"));
  296. loadObstacles(config["obstacles"], false, obstacles);
  297. loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
  298. //loadObstacles(config["moats"], true, moats);
  299. }
  300. void CHeroHandler::loadHeroes()
  301. {
  302. VLC->heroh = this;
  303. CLegacyConfigParser parser("DATA/HOTRAITS.TXT");
  304. parser.endLine(); //ignore header
  305. parser.endLine();
  306. for (int i=0; i<GameConstants::HEROES_QUANTITY; i++)
  307. {
  308. CHero * hero = new CHero;
  309. hero->name = parser.readString();
  310. hero->initialArmy.resize(3);
  311. for(int x=0;x<3;x++)
  312. {
  313. hero->initialArmy[x].minAmount = parser.readNumber();
  314. hero->initialArmy[x].maxAmount = parser.readNumber();
  315. std::string refName = parser.readString();
  316. boost::algorithm::replace_all(refName, " ", ""); //remove spaces
  317. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + refName, [=](si32 creature)
  318. {
  319. hero->initialArmy[x].creature = creature;
  320. });
  321. }
  322. parser.endLine();
  323. hero->ID = heroes.size();
  324. hero->imageIndex = hero->ID;
  325. heroes.push_back(hero);
  326. }
  327. // Load heroes information
  328. const JsonNode config(ResourceID("config/heroes.json"));
  329. BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector())
  330. {
  331. loadHeroJson(heroes[hero["id"].Float()], hero);
  332. }
  333. }
  334. void CHeroHandler::loadHeroTexts()
  335. {
  336. CLegacyConfigParser parser("DATA/HEROSPEC.TXT");
  337. CLegacyConfigParser bioParser("DATA/HEROBIOS.TXT");
  338. //skip header
  339. parser.endLine();
  340. parser.endLine();
  341. int i=0;
  342. do
  343. {
  344. CHero * hero = heroes[i++];
  345. hero->specName = parser.readString();
  346. hero->specTooltip = parser.readString();
  347. hero->specDescr = parser.readString();
  348. hero->biography = bioParser.readString();
  349. }
  350. while (parser.endLine() && bioParser.endLine() && heroes.size() < i);
  351. }
  352. void CHeroHandler::loadBallistics()
  353. {
  354. CLegacyConfigParser ballParser("DATA/BALLIST.TXT");
  355. ballParser.endLine(); //header
  356. ballParser.endLine();
  357. do
  358. {
  359. ballParser.readString();
  360. ballParser.readString();
  361. CHeroHandler::SBallisticsLevelInfo bli;
  362. bli.keep = ballParser.readNumber();
  363. bli.tower = ballParser.readNumber();
  364. bli.gate = ballParser.readNumber();
  365. bli.wall = ballParser.readNumber();
  366. bli.shots = ballParser.readNumber();
  367. bli.noDmg = ballParser.readNumber();
  368. bli.oneDmg = ballParser.readNumber();
  369. bli.twoDmg = ballParser.readNumber();
  370. bli.sum = ballParser.readNumber();
  371. ballistics.push_back(bli);
  372. }
  373. while (ballParser.endLine());
  374. }
  375. ui32 CHeroHandler::level (ui64 experience) const
  376. {
  377. return boost::range::upper_bound(expPerLevel, experience) - boost::begin(expPerLevel);
  378. }
  379. ui64 CHeroHandler::reqExp (ui32 level) const
  380. {
  381. if(!level)
  382. return 0;
  383. if (level <= expPerLevel.size())
  384. {
  385. return expPerLevel[level-1];
  386. }
  387. else
  388. {
  389. tlog3 << "A hero has reached unsupported amount of experience\n";
  390. return expPerLevel[expPerLevel.size()-1];
  391. }
  392. }
  393. void CHeroHandler::loadTerrains()
  394. {
  395. const JsonNode config(ResourceID("config/terrains.json"));
  396. terrCosts.reserve(GameConstants::TERRAIN_TYPES);
  397. BOOST_FOREACH(const std::string & name, GameConstants::TERRAIN_NAMES)
  398. terrCosts.push_back(config[name]["moveCost"].Float());
  399. }
  400. std::vector<ui8> CHeroHandler::getDefaultAllowedHeroes() const
  401. {
  402. // Look Data/HOTRAITS.txt for reference
  403. std::vector<ui8> allowedHeroes;
  404. allowedHeroes.resize(156, 1);
  405. for(int i = 145; i < 156; ++i)
  406. {
  407. allowedHeroes[i] = 0;
  408. }
  409. allowedHeroes[4] = 0;
  410. allowedHeroes[25] = 0;
  411. return allowedHeroes;
  412. }