2
0

CSkillHandler.cpp 6.9 KB

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