CSkillHandler.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. b->description.appendRawString(" %+d");
  83. levels[level-1].effects.push_back(b);
  84. }
  85. const CSkill::LevelInfo & CSkill::at(int level) const
  86. {
  87. assert(1 <= level && level < NSecondarySkill::levels.size());
  88. return levels[level - 1];
  89. }
  90. CSkill::LevelInfo & CSkill::at(int level)
  91. {
  92. assert(1 <= level && level < NSecondarySkill::levels.size());
  93. return levels[level - 1];
  94. }
  95. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info)
  96. {
  97. for(int i=0; i < info.effects.size(); i++)
  98. out << (i ? "," : "") << info.effects[i]->Description();
  99. return out << "])";
  100. }
  101. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill)
  102. {
  103. out << "Skill(" << skill.id.getNum() << "," << skill.identifier << "): [";
  104. for(int i=0; i < skill.levels.size(); i++)
  105. out << (i ? "," : "") << skill.levels[i];
  106. return out << "]";
  107. }
  108. std::string CSkill::toString() const
  109. {
  110. std::ostringstream ss;
  111. ss << *this;
  112. return ss.str();
  113. }
  114. void CSkill::updateFrom(const JsonNode & data)
  115. {
  116. }
  117. void CSkill::serializeJson(JsonSerializeFormat & handler)
  118. {
  119. }
  120. ///CSkillHandler
  121. std::vector<JsonNode> CSkillHandler::loadLegacyData()
  122. {
  123. CLegacyConfigParser parser(TextPath::builtin("DATA/SSTRAITS.TXT"));
  124. //skip header
  125. parser.endLine();
  126. parser.endLine();
  127. std::vector<std::string> skillNames;
  128. std::vector<std::vector<std::string>> skillInfoTexts;
  129. do
  130. {
  131. skillNames.push_back(parser.readString());
  132. skillInfoTexts.emplace_back();
  133. for(int i = 0; i < 3; i++)
  134. skillInfoTexts.back().push_back(parser.readString());
  135. }
  136. while (parser.endLine());
  137. assert(skillNames.size() == GameConstants::SKILL_QUANTITY);
  138. //store & construct JSON
  139. std::vector<JsonNode> legacyData;
  140. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  141. {
  142. JsonNode skillNode;
  143. skillNode["name"].String() = skillNames[id];
  144. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  145. {
  146. std::string & desc = skillInfoTexts[id][level-1];
  147. auto & levelNode = skillNode[NSecondarySkill::levels[level]].Struct();
  148. levelNode["description"].String() = desc;
  149. levelNode["effects"].Struct(); // create empty effects objects
  150. }
  151. legacyData.push_back(skillNode);
  152. }
  153. objects.resize(legacyData.size());
  154. return legacyData;
  155. }
  156. const std::vector<std::string> & CSkillHandler::getTypeNames() const
  157. {
  158. static const std::vector<std::string> typeNames = { "skill", "secondarySkill" };
  159. return typeNames;
  160. }
  161. CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
  162. {
  163. assert(identifier.find(':') == std::string::npos);
  164. assert(!scope.empty());
  165. bool major;
  166. bool minor;
  167. major = json["obligatoryMajor"].Bool();
  168. minor = json["obligatoryMinor"].Bool();
  169. auto * skill = new CSkill(SecondarySkill((si32)index), identifier, major, minor);
  170. skill->modScope = scope;
  171. skill->onlyOnWaterMap = json["onlyOnWaterMap"].Bool();
  172. VLC->generaltexth->registerString(scope, skill->getNameTextID(), json["name"].String());
  173. switch(json["gainChance"].getType())
  174. {
  175. case JsonNode::JsonType::DATA_INTEGER:
  176. skill->gainChance[0] = static_cast<si32>(json["gainChance"].Integer());
  177. skill->gainChance[1] = static_cast<si32>(json["gainChance"].Integer());
  178. break;
  179. case JsonNode::JsonType::DATA_STRUCT:
  180. skill->gainChance[0] = static_cast<si32>(json["gainChance"]["might"].Integer());
  181. skill->gainChance[1] = static_cast<si32>(json["gainChance"]["magic"].Integer());
  182. break;
  183. default:
  184. break;
  185. }
  186. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  187. {
  188. const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
  189. const JsonNode & levelNode = json[levelName];
  190. // parse bonus effects
  191. for(const auto & b : levelNode["effects"].Struct())
  192. {
  193. auto bonus = JsonUtils::parseBonus(b.second);
  194. skill->addNewBonus(bonus, level);
  195. }
  196. CSkill::LevelInfo & skillAtLevel = skill->at(level);
  197. VLC->generaltexth->registerString(scope, skill->getDescriptionTextID(level), levelNode["description"].String());
  198. skillAtLevel.iconSmall = levelNode["images"]["small"].String();
  199. skillAtLevel.iconMedium = levelNode["images"]["medium"].String();
  200. skillAtLevel.iconLarge = levelNode["images"]["large"].String();
  201. }
  202. logMod->debug("loaded secondary skill %s(%d)", identifier, skill->id.getNum());
  203. return skill;
  204. }
  205. void CSkillHandler::afterLoadFinalization()
  206. {
  207. }
  208. void CSkillHandler::beforeValidate(JsonNode & object)
  209. {
  210. //handle "base" level info
  211. JsonNode & base = object["base"];
  212. auto inheritNode = [&](const std::string & name){
  213. JsonUtils::inherit(object[name], base);
  214. };
  215. inheritNode("basic");
  216. inheritNode("advanced");
  217. inheritNode("expert");
  218. }
  219. std::set<SecondarySkill> CSkillHandler::getDefaultAllowed() const
  220. {
  221. std::set<SecondarySkill> result;
  222. for (auto const & skill : objects)
  223. result.insert(skill->getId());
  224. return result;
  225. }
  226. VCMI_LIB_NAMESPACE_END