CSkillHandler.cpp 7.1 KB

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