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