CSkillHandler.cpp 8.9 KB

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