1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

added basic handler for loading secondary skill bonuses

This commit is contained in:
Henning Koehler 2017-08-20 19:18:07 +12:00
parent 8180e4d5c5
commit fbab52eb18
8 changed files with 202 additions and 0 deletions

View File

@ -115,6 +115,7 @@ set(lib_SRCS
CModHandler.cpp
CPathfinder.cpp
CRandomGenerator.cpp
CSkillHandler.cpp
CStack.cpp
CThreadHelper.cpp
CTownHandler.cpp

117
lib/CSkillHandler.cpp Normal file
View File

@ -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 <cctype>
#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<Bonus>& 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<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
{
// not supported - no legacy data to load
std::vector<JsonNode> 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<bool> CSkillHandler::getDefaultAllowed() const
{
std::vector<bool> allowedSkills(objects.size(), true);
return allowedSkills;
}

66
lib/CSkillHandler.h Normal file
View File

@ -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<CBonusSystemNode *> bonusByLevel; // bonuses provided by none, basic, advanced and expert level
public:
CSkill();
~CSkill();
void addNewBonus(const std::shared_ptr<Bonus>& b, int level);
CBonusSystemNode * getBonus(int level);
SecondarySkill id;
std::string identifier;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & id & identifier;
h & bonusByLevel;
}
friend class CSkillHandler;
};
class DLL_LINKAGE CSkillHandler: public CHandlerBase<SecondarySkill, CSkill>
{
public:
CSkillHandler();
virtual ~CSkillHandler();
///IHandler base
std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
void afterLoadFinalization() override;
void beforeValidate(JsonNode & object) override;
std::vector<bool> getDefaultAllowed() const override;
const std::string getTypeName() const override;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & objects ;
}
protected:
CSkill * loadFromJson(const JsonNode & json, const std::string & identifier) override;
};

View File

@ -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 << <ArtifactInstanceID>(std::ostream & os, BaseForID<ArtifactInstanceID> id);
//template std::ostream & operator << <ObjectInstanceID>(std::ostream & os, BaseForID<ObjectInstanceID> id);

View File

@ -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;

View File

@ -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"

View File

@ -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;

View File

@ -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;