CSkillHandler.cpp 7.6 KB

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