CSkillHandler.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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() : description("")
  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->duration = Bonus::PERMANENT;
  46. b->description = identifier;
  47. levels[level-1].effects.push_back(b);
  48. }
  49. void CSkill::setDescription(const std::string & desc, int level)
  50. {
  51. levels[level-1].description = desc;
  52. }
  53. const std::vector<std::shared_ptr<Bonus>> & CSkill::getBonus(int level) const
  54. {
  55. return levels[level-1].effects;
  56. }
  57. const std::string & CSkill::getDescription(int level) const
  58. {
  59. return levels[level-1].description;
  60. }
  61. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const CSkill::LevelInfo &info)
  62. {
  63. out << "(\"" << info.description << "\", [";
  64. for(int i=0; i < info.effects.size(); i++)
  65. out << (i ? "," : "") << info.effects[i]->Description();
  66. return out << "])";
  67. }
  68. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const CSkill &skill)
  69. {
  70. out << "Skill(" << (int)skill.id << "," << skill.identifier << "): [";
  71. for(int i=0; i < skill.levels.size(); i++)
  72. out << (i ? "," : "") << skill.levels[i];
  73. return out << "]";
  74. }
  75. std::string CSkill::toString() const
  76. {
  77. std::ostringstream ss;
  78. ss << *this;
  79. return ss.str();
  80. }
  81. ///CSkillHandler
  82. CSkillHandler::CSkillHandler()
  83. {
  84. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  85. {
  86. CSkill * skill = new CSkill(SecondarySkill(id));
  87. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  88. skill->addNewBonus(defaultBonus(SecondarySkill(id), level), level);
  89. objects.push_back(skill);
  90. }
  91. }
  92. std::vector<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
  93. {
  94. // not supported - no legacy data to load
  95. std::vector<JsonNode> legacyData;
  96. return legacyData;
  97. }
  98. const std::string CSkillHandler::getTypeName() const
  99. {
  100. return "skill";
  101. }
  102. CSkill * CSkillHandler::loadFromJson(const JsonNode & json, const std::string & identifier)
  103. {
  104. CSkill * skill = NULL;
  105. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  106. {
  107. if(NSecondarySkill::names[id].compare(identifier) == 0)
  108. {
  109. skill = new CSkill(SecondarySkill(id));
  110. break;
  111. }
  112. }
  113. if(!skill)
  114. {
  115. logGlobal->error("unknown secondary skill %s", identifier);
  116. throw std::runtime_error("invalid skill");
  117. }
  118. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  119. {
  120. const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
  121. const JsonNode & levelNode = json[levelName];
  122. // parse bonus effects
  123. for(auto b : levelNode["effects"].Vector())
  124. {
  125. auto bonus = JsonUtils::parseBonus(b);
  126. bonus->sid = skill->id;
  127. skill->addNewBonus(bonus, level);
  128. }
  129. // parse skill description - tracked separately
  130. if(vstd::contains(levelNode.Struct(), "description") && !levelNode["description"].isNull())
  131. //CGI->generaltexth->skillInfoTexts[skill->id][level-1] = levelNode["description"].String();
  132. skill->setDescription(levelNode["description"].String(), level);
  133. }
  134. logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id);
  135. logMod->trace("%s", skill->toString());
  136. return skill;
  137. }
  138. void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  139. {
  140. auto type_name = getTypeName();
  141. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  142. if(object->id == SecondarySkill::DEFAULT) // new skill - no index identified
  143. {
  144. object->id = SecondarySkill(objects.size());
  145. objects.push_back(object);
  146. }
  147. else
  148. objects[object->id] = object;
  149. registerObject(scope, type_name, name, object->id);
  150. }
  151. void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  152. {
  153. auto type_name = getTypeName();
  154. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  155. assert(object->id == index);
  156. objects[index] = object;
  157. registerObject(scope,type_name, name, object->id);
  158. }
  159. void CSkillHandler::afterLoadFinalization()
  160. {
  161. }
  162. void CSkillHandler::beforeValidate(JsonNode & object)
  163. {
  164. }
  165. CSkillHandler::~CSkillHandler()
  166. {
  167. }
  168. std::vector<bool> CSkillHandler::getDefaultAllowed() const
  169. {
  170. std::vector<bool> allowedSkills(objects.size(), true);
  171. return allowedSkills;
  172. }
  173. // HMM3 default bonus provided by secondary skill
  174. const std::shared_ptr<Bonus> CSkillHandler::defaultBonus(SecondarySkill skill, int level) const
  175. {
  176. Bonus::BonusType bonusType = Bonus::SECONDARY_SKILL_PREMY;
  177. Bonus::ValueType valueType = Bonus::BASE_NUMBER;
  178. int bonusVal = level;
  179. switch (skill)
  180. {
  181. case SecondarySkill::ARCHERY:
  182. bonusVal = 5 + 5 * level * level; break;
  183. case SecondarySkill::LOGISTICS:
  184. bonusVal = 10 * level; break;
  185. case SecondarySkill::DIPLOMACY:
  186. bonusType = Bonus::SURRENDER_DISCOUNT;
  187. bonusVal = 20 * level; break;
  188. case SecondarySkill::NAVIGATION:
  189. bonusVal = 50 * level; break;
  190. case SecondarySkill::LEADERSHIP:
  191. bonusType = Bonus::MORALE; break;
  192. case SecondarySkill::LUCK:
  193. bonusType = Bonus::LUCK; break;
  194. case SecondarySkill::EAGLE_EYE:
  195. bonusVal = 30 + 10 * level; break;
  196. case SecondarySkill::NECROMANCY:
  197. bonusVal = 10 * level; break;
  198. case SecondarySkill::ESTATES:
  199. bonusVal = 125 << (level-1); break;
  200. case SecondarySkill::TACTICS:
  201. bonusVal = 1 + 2 * level; break;
  202. case SecondarySkill::LEARNING:
  203. bonusVal = 5 * level; break;
  204. case SecondarySkill::OFFENCE:
  205. bonusVal = 10 * level; break;
  206. case SecondarySkill::ARMORER:
  207. bonusVal = 5 * level; break;
  208. case SecondarySkill::INTELLIGENCE:
  209. bonusVal = 25 << (level-1); break;
  210. case SecondarySkill::SORCERY:
  211. bonusVal = 5 * level; break;
  212. case SecondarySkill::RESISTANCE:
  213. bonusVal = 5 << (level-1); break;
  214. case SecondarySkill::FIRST_AID:
  215. bonusVal = 25 + 25 * level; break;
  216. default:
  217. valueType = Bonus::INDEPENDENT_MIN; break;
  218. }
  219. return std::make_shared<Bonus>(Bonus::PERMANENT, bonusType, Bonus::SECONDARY_SKILL, bonusVal, skill, skill, valueType);
  220. }