CSkillHandler.cpp 7.0 KB

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