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