CSkillHandler.cpp 8.3 KB

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