2
0

CSkillHandler.cpp 7.3 KB

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