diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 14ad6253d..c1f2dc53a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -115,6 +115,7 @@ set(lib_SRCS CModHandler.cpp CPathfinder.cpp CRandomGenerator.cpp + CSkillHandler.cpp CStack.cpp CThreadHelper.cpp CTownHandler.cpp diff --git a/lib/CSkillHandler.cpp b/lib/CSkillHandler.cpp new file mode 100644 index 000000000..8a1a3796a --- /dev/null +++ b/lib/CSkillHandler.cpp @@ -0,0 +1,117 @@ +/* + * CSkillHandler.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "StdInc.h" + +#include + +#include "CSkillHandler.h" + +#include "CGeneralTextHandler.h" +#include "filesystem/Filesystem.h" + +#include "JsonNode.h" + +#include "CModHandler.h" +#include "StringConstants.h" + +#include "CStack.h" +#include "battle/BattleInfo.h" +#include "battle/CBattleInfoCallback.h" + +///CSkill +CSkill::CSkill() +{ + for(auto level : NSecondarySkill::levels) + bonusByLevel.push_back(new CBonusSystemNode()); +} + +CSkill::~CSkill() +{ + for(auto bonus : bonusByLevel) + delete bonus; +} + +void CSkill::addNewBonus(const std::shared_ptr& b, int level) +{ + b->source = Bonus::SECONDARY_SKILL; + b->duration = Bonus::PERMANENT; + b->description = identifier; + bonusByLevel[level]->addNewBonus(b); +} + +CBonusSystemNode * CSkill::getBonus(int level) +{ + return bonusByLevel[level]; +} + +///CSkillHandler +CSkillHandler::CSkillHandler() +{ +} + +std::vector CSkillHandler::loadLegacyData(size_t dataSize) +{ + // not supported - no legacy data to load + std::vector legacyData; + return legacyData; +} + +const std::string CSkillHandler::getTypeName() const +{ + return "secondarySkill"; +} + +CSkill * CSkillHandler::loadFromJson(const JsonNode & json, const std::string & identifier) +{ + CSkill * skill = new CSkill(); + skill->identifier = identifier; + + skill->id = SecondarySkill::DEFAULT; + for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++) + { + if(NSecondarySkill::names[id].compare(identifier) == 0) + { + skill->id = SecondarySkill(id); + break; + } + } + + for(int level = 1; level < NSecondarySkill::levels.size(); level++) + { + const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert + for(auto b : json[levelName].Vector()) + { + auto bonus = JsonUtils::parseBonus(b); + bonus->sid = skill->id; + skill->addNewBonus(bonus, level); + } + } + + return skill; +} + +void CSkillHandler::afterLoadFinalization() +{ +} + +void CSkillHandler::beforeValidate(JsonNode & object) +{ +} + +CSkillHandler::~CSkillHandler() +{ +} + +std::vector CSkillHandler::getDefaultAllowed() const +{ + std::vector allowedSkills(objects.size(), true); + return allowedSkills; +} diff --git a/lib/CSkillHandler.h b/lib/CSkillHandler.h new file mode 100644 index 000000000..38aca6829 --- /dev/null +++ b/lib/CSkillHandler.h @@ -0,0 +1,66 @@ +/* + * CSkillHandler.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#pragma once + +#include "../lib/HeroBonus.h" +#include "GameConstants.h" +#include "IHandlerBase.h" + +class CSkillHandler; +class CGHeroInstance; +class CMap; +class JsonSerializeFormat; + +class DLL_LINKAGE CSkill // secondary skill +{ +protected: + std::vector bonusByLevel; // bonuses provided by none, basic, advanced and expert level + +public: + CSkill(); + ~CSkill(); + + void addNewBonus(const std::shared_ptr& b, int level); + CBonusSystemNode * getBonus(int level); + + SecondarySkill id; + std::string identifier; + + template void serialize(Handler &h, const int version) + { + h & id & identifier; + h & bonusByLevel; + } + + friend class CSkillHandler; +}; + +class DLL_LINKAGE CSkillHandler: public CHandlerBase +{ +public: + CSkillHandler(); + virtual ~CSkillHandler(); + + ///IHandler base + std::vector loadLegacyData(size_t dataSize) override; + void afterLoadFinalization() override; + void beforeValidate(JsonNode & object) override; + + std::vector getDefaultAllowed() const override; + const std::string getTypeName() const override; + + template void serialize(Handler &h, const int version) + { + h & objects ; + } + +protected: + CSkill * loadFromJson(const JsonNode & json, const std::string & identifier) override; +}; diff --git a/lib/GameConstants.cpp b/lib/GameConstants.cpp index b039ef29c..7da3b4906 100644 --- a/lib/GameConstants.cpp +++ b/lib/GameConstants.cpp @@ -21,6 +21,7 @@ #include "CArtHandler.h" #include "CCreatureHandler.h" #include "spells/CSpellHandler.h" +#include "CSkillHandler.h" #include "StringConstants.h" #include "CGeneralTextHandler.h" @@ -65,6 +66,11 @@ const CSpell * SpellID::toSpell() const return VLC->spellh->objects[*this]; } +const CSkill * SecondarySkill::toSkill() const +{ + return VLC->skillh->objects.at(*this); +} + //template std::ostream & operator << (std::ostream & os, BaseForID id); //template std::ostream & operator << (std::ostream & os, BaseForID id); diff --git a/lib/GameConstants.h b/lib/GameConstants.h index 9218f7f67..4cce159ea 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -62,6 +62,7 @@ class CArtifactInstance; class CCreature; class CHero; class CSpell; +class CSkill; class CGameInfoCallback; class CNonConstInfoCallback; @@ -320,6 +321,8 @@ public: SecondarySkill(ESecondarySkill _num = WRONG) : num(_num) {} + DLL_LINKAGE const CSkill * toSkill() const; + ID_LIKE_CLASS_COMMON(SecondarySkill, ESecondarySkill) ESecondarySkill num; diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp index 27c968c28..584ade005 100644 --- a/lib/IGameCallback.cpp +++ b/lib/IGameCallback.cpp @@ -12,6 +12,7 @@ #include "CHeroHandler.h" // for CHeroHandler #include "spells/CSpellHandler.h"// for CSpell +#include "CSkillHandler.h"// for CSkill #include "NetPacks.h" #include "CBonusTypeHandler.h" #include "CModHandler.h" diff --git a/lib/VCMI_Lib.cpp b/lib/VCMI_Lib.cpp index 4b158afef..67fd7141c 100644 --- a/lib/VCMI_Lib.cpp +++ b/lib/VCMI_Lib.cpp @@ -20,6 +20,7 @@ #include "CTownHandler.h" #include "CBuildingHandler.h" #include "spells/CSpellHandler.h" +#include "CSkillHandler.h" #include "CGeneralTextHandler.h" #include "CModHandler.h" #include "IGameEventsReceiver.h" @@ -113,6 +114,8 @@ void LibClasses::init() createHandler(spellh, "Spell", pomtime); + createHandler(skillh, "Skill", pomtime); + createHandler(terviewh, "Terrain view pattern", pomtime); createHandler(tplh, "Template", pomtime); //templates need already resolved identifiers (refactor?) @@ -137,6 +140,7 @@ void LibClasses::clear() delete objh; delete objtypeh; delete spellh; + delete skillh; delete modh; delete bth; delete tplh; @@ -154,6 +158,7 @@ void LibClasses::makeNull() objh = nullptr; objtypeh = nullptr; spellh = nullptr; + skillh = nullptr; modh = nullptr; bth = nullptr; tplh = nullptr; diff --git a/lib/VCMI_Lib.h b/lib/VCMI_Lib.h index 26bfa2a24..a31e73727 100644 --- a/lib/VCMI_Lib.h +++ b/lib/VCMI_Lib.h @@ -14,6 +14,7 @@ class CArtHandler; class CHeroHandler; class CCreatureHandler; class CSpellHandler; +class CSkillHandler; class CBuildingHandler; class CObjectHandler; class CObjectClassesHandler; @@ -41,6 +42,7 @@ public: CHeroHandler * heroh; CCreatureHandler * creh; CSpellHandler * spellh; + CSkillHandler * skillh; CObjectHandler * objh; CObjectClassesHandler * objtypeh; CTownHandler * townh; @@ -67,6 +69,7 @@ public: h & objh; h & objtypeh; h & spellh; + h & skillh; h & modh; h & IS_AI_ENABLED; h & bth;