2
0

CSkillHandler.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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->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. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  86. {
  87. CSkill * skill = new CSkill(SecondarySkill(id));
  88. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  89. for (auto bonus : defaultBonus(SecondarySkill(id), level))
  90. skill->addNewBonus(bonus, level);
  91. objects.push_back(skill);
  92. }
  93. }
  94. std::vector<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
  95. {
  96. // not supported - no legacy data to load
  97. std::vector<JsonNode> legacyData;
  98. return legacyData;
  99. }
  100. const std::string CSkillHandler::getTypeName() const
  101. {
  102. return "skill";
  103. }
  104. CSkill * CSkillHandler::loadFromJson(const JsonNode & json, const std::string & identifier)
  105. {
  106. CSkill * skill = NULL;
  107. for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
  108. {
  109. if(NSecondarySkill::names[id].compare(identifier) == 0)
  110. {
  111. skill = new CSkill(SecondarySkill(id));
  112. break;
  113. }
  114. }
  115. if(!skill)
  116. {
  117. logGlobal->error("unknown secondary skill %s", identifier);
  118. throw std::runtime_error("invalid skill");
  119. }
  120. for(int level = 1; level < NSecondarySkill::levels.size(); level++)
  121. {
  122. const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
  123. const JsonNode & levelNode = json[levelName];
  124. // parse bonus effects
  125. for(auto b : levelNode["effects"].Vector())
  126. {
  127. auto bonus = JsonUtils::parseBonus(b);
  128. bonus->sid = skill->id;
  129. skill->addNewBonus(bonus, level);
  130. }
  131. // parse skill description - tracked separately
  132. if(vstd::contains(levelNode.Struct(), "description") && !levelNode["description"].isNull())
  133. //CGI->generaltexth->skillInfoTexts[skill->id][level-1] = levelNode["description"].String();
  134. skill->setDescription(levelNode["description"].String(), level);
  135. }
  136. logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id);
  137. logMod->trace("%s", skill->toString());
  138. return skill;
  139. }
  140. void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  141. {
  142. auto type_name = getTypeName();
  143. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  144. if(object->id == SecondarySkill::DEFAULT) // new skill - no index identified
  145. {
  146. object->id = SecondarySkill(objects.size());
  147. objects.push_back(object);
  148. }
  149. else
  150. objects[object->id] = object;
  151. registerObject(scope, type_name, name, object->id);
  152. }
  153. void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  154. {
  155. auto type_name = getTypeName();
  156. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  157. assert(object->id == index);
  158. objects[index] = object;
  159. registerObject(scope,type_name, name, object->id);
  160. }
  161. void CSkillHandler::afterLoadFinalization()
  162. {
  163. }
  164. void CSkillHandler::beforeValidate(JsonNode & object)
  165. {
  166. }
  167. CSkillHandler::~CSkillHandler()
  168. {
  169. }
  170. std::vector<bool> CSkillHandler::getDefaultAllowed() const
  171. {
  172. std::vector<bool> allowedSkills(objects.size(), true);
  173. return allowedSkills;
  174. }
  175. // HMM3 default bonus provided by secondary skill
  176. std::vector<std::shared_ptr<Bonus>> CSkillHandler::defaultBonus(SecondarySkill skill, int level) const
  177. {
  178. std::vector<std::shared_ptr<Bonus>> result;
  179. // add bonus based on current values - useful for adding multiple bonuses easily
  180. auto addBonus = [=,&result](int bonusVal, Bonus::BonusType bonusType = Bonus::SECONDARY_SKILL_PREMY, int subtype = 0)
  181. {
  182. if(bonusType == Bonus::SECONDARY_SKILL_PREMY || bonusType == Bonus::SECONDARY_SKILL_VAL2)
  183. subtype = skill;
  184. result.push_back(std::make_shared<Bonus>(Bonus::PERMANENT, bonusType, Bonus::SECONDARY_SKILL, bonusVal, skill, subtype, Bonus::BASE_NUMBER));
  185. };
  186. switch(skill)
  187. {
  188. case SecondarySkill::PATHFINDING:
  189. addBonus(25 * level); break;
  190. case SecondarySkill::ARCHERY:
  191. addBonus(5 + 5 * level * level); break;
  192. case SecondarySkill::LOGISTICS:
  193. addBonus(10 * level); break;
  194. case SecondarySkill::SCOUTING:
  195. addBonus(level, Bonus::SIGHT_RADIOUS); break;
  196. case SecondarySkill::DIPLOMACY:
  197. addBonus(20 * level, Bonus::SURRENDER_DISCOUNT); break;
  198. case SecondarySkill::NAVIGATION:
  199. addBonus(50 * level); break;
  200. case SecondarySkill::LEADERSHIP:
  201. addBonus(level, Bonus::MORALE); break;
  202. case SecondarySkill::LUCK:
  203. addBonus(level, Bonus::LUCK); break;
  204. case SecondarySkill::BALLISTICS:
  205. addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::CATAPULT);
  206. addBonus(level);
  207. break;
  208. case SecondarySkill::EAGLE_EYE:
  209. addBonus(30 + 10 * level);
  210. addBonus(1 + level, Bonus::SECONDARY_SKILL_VAL2);
  211. break;
  212. case SecondarySkill::NECROMANCY:
  213. addBonus(10 * level); break;
  214. case SecondarySkill::ESTATES:
  215. addBonus(125 << (level-1)); break;
  216. case SecondarySkill::FIRE_MAGIC:
  217. case SecondarySkill::AIR_MAGIC:
  218. case SecondarySkill::WATER_MAGIC:
  219. case SecondarySkill::EARTH_MAGIC:
  220. addBonus(level); break;
  221. case SecondarySkill::SCHOLAR:
  222. addBonus(1 + level); break;
  223. case SecondarySkill::TACTICS:
  224. addBonus(1 + 2 * level); break;
  225. case SecondarySkill::ARTILLERY:
  226. addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::BALLISTA);
  227. addBonus(25 + 25 * level);
  228. if(level > 1) // extra attack
  229. addBonus(1, Bonus::SECONDARY_SKILL_VAL2);
  230. break;
  231. case SecondarySkill::LEARNING:
  232. addBonus(5 * level); break;
  233. case SecondarySkill::OFFENCE:
  234. addBonus(10 * level); break;
  235. case SecondarySkill::ARMORER:
  236. addBonus(5 * level); break;
  237. case SecondarySkill::INTELLIGENCE:
  238. addBonus(25 << (level-1)); break;
  239. case SecondarySkill::SORCERY:
  240. addBonus(5 * level); break;
  241. case SecondarySkill::RESISTANCE:
  242. addBonus(5 << (level-1)); break;
  243. case SecondarySkill::FIRST_AID:
  244. addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::FIRST_AID_TENT);
  245. addBonus(25 + 25 * level); break;
  246. default:
  247. addBonus(level); break;
  248. }
  249. return result;
  250. }