2
0

CSkillHandler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "CStack.h"
  19. #include "battle/BattleInfo.h"
  20. #include "battle/CBattleInfoCallback.h"
  21. ///CSkill
  22. CSkill::LevelInfo::LevelInfo()
  23. {
  24. }
  25. CSkill::LevelInfo::~LevelInfo()
  26. {
  27. }
  28. CSkill::CSkill(SecondarySkill id) : id(id)
  29. {
  30. if(id == SecondarySkill::DEFAULT)
  31. identifier = "default";
  32. else
  33. identifier = NSecondarySkill::names[id];
  34. // init levels
  35. LevelInfo emptyLevel;
  36. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  37. levels.push_back(emptyLevel);
  38. }
  39. CSkill::~CSkill()
  40. {
  41. }
  42. void CSkill::addNewBonus(const std::shared_ptr<Bonus> & b, int level)
  43. {
  44. b->source = Bonus::SECONDARY_SKILL;
  45. b->sid = id;
  46. b->duration = Bonus::PERMANENT;
  47. b->description = identifier;
  48. levels[level-1].effects.push_back(b);
  49. }
  50. void CSkill::setDescription(const std::string & desc, int level)
  51. {
  52. levels[level-1].description = desc;
  53. }
  54. const std::vector<std::shared_ptr<Bonus>> & CSkill::getBonus(int level) const
  55. {
  56. return levels[level-1].effects;
  57. }
  58. const std::string & CSkill::getDescription(int level) const
  59. {
  60. return levels[level-1].description;
  61. }
  62. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info)
  63. {
  64. out << "(\"" << info.description << "\", [";
  65. for(int i=0; i < info.effects.size(); i++)
  66. out << (i ? "," : "") << info.effects[i]->Description();
  67. return out << "])";
  68. }
  69. DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill)
  70. {
  71. out << "Skill(" << (int)skill.id << "," << skill.identifier << "): [";
  72. for(int i=0; i < skill.levels.size(); i++)
  73. out << (i ? "," : "") << skill.levels[i];
  74. return out << "]";
  75. }
  76. std::string CSkill::toString() const
  77. {
  78. std::ostringstream ss;
  79. ss << *this;
  80. return ss.str();
  81. }
  82. ///CSkillHandler
  83. CSkillHandler::CSkillHandler()
  84. {
  85. }
  86. std::vector<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
  87. {
  88. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  89. //skip header
  90. parser.endLine();
  91. parser.endLine();
  92. std::vector<std::string> skillNames;
  93. std::vector<std::vector<std::string>> skillInfoTexts;
  94. do
  95. {
  96. skillNames.push_back(parser.readString());
  97. skillInfoTexts.push_back(std::vector<std::string>());
  98. for(int i = 0; i < 3; i++)
  99. skillInfoTexts.back().push_back(parser.readString());
  100. }
  101. while (parser.endLine());
  102. assert(skillNames.size() == GameConstants::SKILL_QUANTITY);
  103. //store & construct JSON
  104. std::vector<JsonNode> legacyData;
  105. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  106. {
  107. JsonNode skillNode(JsonNode::DATA_STRUCT);
  108. skillNode["name"].String() = skillNames[id];
  109. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  110. {
  111. std::string & desc = skillInfoTexts[id][level-1];
  112. auto & levelNode = skillNode[NSecondarySkill::levels[level]].Struct();
  113. levelNode["description"].String() = desc;
  114. levelNode["effects"].Struct(); // create empty effects objects
  115. }
  116. legacyData.push_back(skillNode);
  117. }
  118. objects.resize(legacyData.size());
  119. return legacyData;
  120. }
  121. const std::string CSkillHandler::getTypeName() const
  122. {
  123. return "skill";
  124. }
  125. const std::string & CSkillHandler::skillInfo(int skill, int level) const
  126. {
  127. return objects[skill]->getDescription(level);
  128. }
  129. const std::string & CSkillHandler::skillName(int skill) const
  130. {
  131. return objects[skill]->name;
  132. }
  133. CSkill * CSkillHandler::loadFromJson(const JsonNode & json, const std::string & identifier)
  134. {
  135. CSkill * skill = nullptr;
  136. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  137. {
  138. if(NSecondarySkill::names[id].compare(identifier) == 0)
  139. {
  140. skill = new CSkill(SecondarySkill(id));
  141. break;
  142. }
  143. }
  144. if(!skill)
  145. {
  146. logMod->error("unknown secondary skill %s", identifier);
  147. throw std::runtime_error("invalid skill");
  148. }
  149. skill->name = json["name"].String();
  150. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  151. {
  152. const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
  153. const JsonNode & levelNode = json[levelName];
  154. // parse bonus effects
  155. for(auto b : levelNode["effects"].Struct())
  156. {
  157. auto bonus = JsonUtils::parseBonus(b.second);
  158. bonus->sid = skill->id;
  159. skill->addNewBonus(bonus, level);
  160. }
  161. skill->setDescription(levelNode["description"].String(), level);
  162. }
  163. logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id);
  164. logMod->trace("%s", skill->toString());
  165. return skill;
  166. }
  167. void CSkillHandler::afterLoadFinalization()
  168. {
  169. }
  170. void CSkillHandler::beforeValidate(JsonNode & object)
  171. {
  172. //handle "base" level info
  173. JsonNode & base = object["base"];
  174. auto inheritNode = [&](const std::string & name){
  175. JsonUtils::inherit(object[name], base);
  176. };
  177. inheritNode("basic");
  178. inheritNode("advanced");
  179. inheritNode("expert");
  180. }
  181. CSkillHandler::~CSkillHandler()
  182. {
  183. }
  184. std::vector<bool> CSkillHandler::getDefaultAllowed() const
  185. {
  186. std::vector<bool> allowedSkills(objects.size(), true);
  187. return allowedSkills;
  188. }