CSkillHandler.cpp 7.2 KB

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