2
0

CSkillHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * CSkillHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include <cctype>
  12. #include "CSkillHandler.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "filesystem/Filesystem.h"
  15. #include "JsonNode.h"
  16. #include "CModHandler.h"
  17. #include "StringConstants.h"
  18. ///CSkill
  19. CSkill::LevelInfo::LevelInfo()
  20. {
  21. }
  22. CSkill::LevelInfo::~LevelInfo()
  23. {
  24. }
  25. CSkill::CSkill(SecondarySkill id, std::string identifier)
  26. : id(id), identifier(identifier)
  27. {
  28. gainChance[0] = gainChance[1] = 0; //affects CHeroClassHandler::afterLoadFinalization()
  29. levels.resize(NSecondarySkill::levels.size() - 1);
  30. }
  31. CSkill::~CSkill()
  32. {
  33. }
  34. int32_t CSkill::getIndex() const
  35. {
  36. return id.num;
  37. }
  38. int32_t CSkill::getIconIndex() const
  39. {
  40. return getIndex(); //TODO: actual value with skill level
  41. }
  42. const std::string & CSkill::getName() const
  43. {
  44. return name;
  45. }
  46. const std::string & CSkill::getJsonKey() const
  47. {
  48. return identifier;
  49. }
  50. void CSkill::registerIcons(const IconRegistar & cb) const
  51. {
  52. for(int level = 1; level <= 3; level++)
  53. {
  54. int frame = 2 + level + 3 * id;
  55. const LevelInfo & skillAtLevel = at(level);
  56. cb(frame, "SECSK32", skillAtLevel.iconSmall);
  57. cb(frame, "SECSKILL", skillAtLevel.iconMedium);
  58. cb(frame, "SECSK82", skillAtLevel.iconLarge);
  59. }
  60. }
  61. SecondarySkill CSkill::getId() const
  62. {
  63. return id;
  64. }
  65. void CSkill::addNewBonus(const std::shared_ptr<Bonus> & b, int level)
  66. {
  67. b->source = Bonus::SECONDARY_SKILL;
  68. b->sid = id;
  69. b->duration = Bonus::PERMANENT;
  70. b->description = name;
  71. levels[level-1].effects.push_back(b);
  72. }
  73. const CSkill::LevelInfo & CSkill::at(int level) const
  74. {
  75. assert(1 <= level && level < NSecondarySkill::levels.size());
  76. return levels[level - 1];
  77. }
  78. CSkill::LevelInfo & CSkill::at(int level)
  79. {
  80. assert(1 <= level && level < NSecondarySkill::levels.size());
  81. return levels[level - 1];
  82. }
  83. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info)
  84. {
  85. out << "(\"" << info.description << "\", [";
  86. for(int i=0; i < info.effects.size(); i++)
  87. out << (i ? "," : "") << info.effects[i]->Description();
  88. return out << "])";
  89. }
  90. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill)
  91. {
  92. out << "Skill(" << (int)skill.id << "," << skill.identifier << "): [";
  93. for(int i=0; i < skill.levels.size(); i++)
  94. out << (i ? "," : "") << skill.levels[i];
  95. return out << "]";
  96. }
  97. std::string CSkill::toString() const
  98. {
  99. std::ostringstream ss;
  100. ss << *this;
  101. return ss.str();
  102. }
  103. void CSkill::updateFrom(const JsonNode & data)
  104. {
  105. }
  106. void CSkill::serializeJson(JsonSerializeFormat & handler)
  107. {
  108. }
  109. ///CSkillHandler
  110. CSkillHandler::CSkillHandler()
  111. {
  112. }
  113. std::vector<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
  114. {
  115. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  116. //skip header
  117. parser.endLine();
  118. parser.endLine();
  119. std::vector<std::string> skillNames;
  120. std::vector<std::vector<std::string>> skillInfoTexts;
  121. do
  122. {
  123. skillNames.push_back(parser.readString());
  124. skillInfoTexts.push_back(std::vector<std::string>());
  125. for(int i = 0; i < 3; i++)
  126. skillInfoTexts.back().push_back(parser.readString());
  127. }
  128. while (parser.endLine());
  129. assert(skillNames.size() == GameConstants::SKILL_QUANTITY);
  130. //store & construct JSON
  131. std::vector<JsonNode> legacyData;
  132. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  133. {
  134. JsonNode skillNode(JsonNode::JsonType::DATA_STRUCT);
  135. skillNode["name"].String() = skillNames[id];
  136. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  137. {
  138. std::string & desc = skillInfoTexts[id][level-1];
  139. auto & levelNode = skillNode[NSecondarySkill::levels[level]].Struct();
  140. levelNode["description"].String() = desc;
  141. levelNode["effects"].Struct(); // create empty effects objects
  142. }
  143. legacyData.push_back(skillNode);
  144. }
  145. objects.resize(legacyData.size());
  146. return legacyData;
  147. }
  148. const std::vector<std::string> & CSkillHandler::getTypeNames() const
  149. {
  150. static const std::vector<std::string> typeNames = { "skill", "secondarySkill" };
  151. return typeNames;
  152. }
  153. const std::string & CSkillHandler::skillInfo(int skill, int level) const
  154. {
  155. return objects[skill]->at(level).description;
  156. }
  157. const std::string & CSkillHandler::skillName(int skill) const
  158. {
  159. return objects[skill]->name;
  160. }
  161. CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
  162. {
  163. CSkill * skill = new CSkill(SecondarySkill((si32)index), identifier);
  164. skill->name = json["name"].String();
  165. switch(json["gainChance"].getType())
  166. {
  167. case JsonNode::JsonType::DATA_INTEGER:
  168. skill->gainChance[0] = static_cast<si32>(json["gainChance"].Integer());
  169. skill->gainChance[1] = static_cast<si32>(json["gainChance"].Integer());
  170. break;
  171. case JsonNode::JsonType::DATA_STRUCT:
  172. skill->gainChance[0] = static_cast<si32>(json["gainChance"]["might"].Integer());
  173. skill->gainChance[1] = static_cast<si32>(json["gainChance"]["magic"].Integer());
  174. break;
  175. default:
  176. break;
  177. }
  178. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  179. {
  180. const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
  181. const JsonNode & levelNode = json[levelName];
  182. // parse bonus effects
  183. for(auto b : levelNode["effects"].Struct())
  184. {
  185. auto bonus = JsonUtils::parseBonus(b.second);
  186. skill->addNewBonus(bonus, level);
  187. }
  188. CSkill::LevelInfo & skillAtLevel = skill->at(level);
  189. skillAtLevel.description = levelNode["description"].String();
  190. skillAtLevel.iconSmall = levelNode["images"]["small"].String();
  191. skillAtLevel.iconMedium = levelNode["images"]["medium"].String();
  192. skillAtLevel.iconLarge = levelNode["images"]["large"].String();
  193. }
  194. logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id);
  195. logMod->trace("%s", skill->toString());
  196. return skill;
  197. }
  198. void CSkillHandler::afterLoadFinalization()
  199. {
  200. }
  201. void CSkillHandler::beforeValidate(JsonNode & object)
  202. {
  203. //handle "base" level info
  204. JsonNode & base = object["base"];
  205. auto inheritNode = [&](const std::string & name){
  206. JsonUtils::inherit(object[name], base);
  207. };
  208. inheritNode("basic");
  209. inheritNode("advanced");
  210. inheritNode("expert");
  211. }
  212. CSkillHandler::~CSkillHandler()
  213. {
  214. }
  215. std::vector<bool> CSkillHandler::getDefaultAllowed() const
  216. {
  217. std::vector<bool> allowedSkills(objects.size(), true);
  218. return allowedSkills;
  219. }
  220. si32 CSkillHandler::decodeSkill(const std::string & identifier)
  221. {
  222. auto rawId = VLC->modh->identifiers.getIdentifier("core", "skill", identifier);
  223. if(rawId)
  224. return rawId.get();
  225. else
  226. return -1;
  227. }
  228. std::string CSkillHandler::encodeSkill(const si32 index)
  229. {
  230. return (*VLC->skillh)[SecondarySkill(index)]->identifier;
  231. }
  232. std::string CSkillHandler::encodeSkillWithType(const si32 index)
  233. {
  234. return CModHandler::makeFullIdentifier("", "skill", encodeSkill(index));
  235. }