2
0

CSkillHandler.cpp 6.8 KB

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