CSkillHandler.cpp 7.2 KB

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