From 89b1ba753821ba0e197c6009c53ffde8a7c4d64c Mon Sep 17 00:00:00 2001 From: Henning Koehler Date: Mon, 11 Sep 2017 18:21:24 +1200 Subject: [PATCH] added basics for specialty format updating --- lib/CHeroHandler.cpp | 28 +++++++++++++++++++--------- lib/HeroBonus.cpp | 34 ++++++++++++++++++++++++++++++++++ lib/HeroBonus.h | 4 ++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index 985d1bd6b..a92214803 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -542,18 +542,28 @@ void CHeroHandler::loadHeroSpecialty(CHero * hero, const JsonNode & node) return bonus; }; - //deprecated, used only for original spciealties - for(const JsonNode &specialty : node["specialties"].Vector()) + //deprecated, used only for original specialties + const JsonNode & specialties = node["specialties"]; + if (!specialties.isNull()) { - SSpecialtyInfo spec; + logMod->warn("Hero %s has deprecated specialties format. New format:", hero->identifier); + JsonNode specVec(JsonNode::DATA_VECTOR); + for(const JsonNode &specialty : node["specialties"].Vector()) + { + SSpecialtyInfo spec; - spec.type = specialty["type"].Float(); - spec.val = specialty["val"].Float(); - spec.subtype = specialty["subtype"].Float(); - spec.additionalinfo = specialty["info"].Float(); + spec.type = specialty["type"].Float(); + spec.val = specialty["val"].Float(); + spec.subtype = specialty["subtype"].Float(); + spec.additionalinfo = specialty["info"].Float(); - for(std::shared_ptr bonus : SpecialtyInfoToBonuses(spec, sid)) - hero->specialty.push_back(bonus); + for(std::shared_ptr bonus : SpecialtyInfoToBonuses(spec, sid)) + { + hero->specialty.push_back(bonus); + specVec.Vector().push_back(bonus->toJsonNode()); + } + } + logMod->info("\"specialty\" = %s", specVec.toJson()); } //new format, using bonus system for(const JsonNode & specialty : node["specialty"].Vector()) diff --git a/lib/HeroBonus.cpp b/lib/HeroBonus.cpp index f59d27510..cccd9fec8 100644 --- a/lib/HeroBonus.cpp +++ b/lib/HeroBonus.cpp @@ -1163,6 +1163,22 @@ std::string Bonus::Description() const return str.str(); } +JsonNode Bonus::toJsonNode() const +{ + JsonNode root(JsonNode::DATA_STRUCT); + + root["type"].Float() = type; + if(subtype != -1) + root["subtype"].Float() = subtype; + if(val != 0) + root["val"].Float() = val; + if(valType != ADDITIVE_VALUE) + root["valType"].Float() = valType; + if(updater) + root["updater"] = updater->toJsonNode(); + return root; +} + Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype) : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc) { @@ -1611,6 +1627,24 @@ std::string ScalingUpdater::toString() const return std::string(buf); } +JsonNode ScalingUpdater::toJsonNode() const +{ + JsonNode root(JsonNode::DATA_STRUCT); + auto addParam = [&](int param) + { + JsonNode paramNode(JsonNode::DATA_INTEGER); + paramNode.Integer() = param; + root["parameters"].Vector().push_back(paramNode); + }; + + root["type"].String() = "GROWS_WITH_LEVEL"; + addParam(valPer20); + if(stepSize > 1) + addParam(stepSize); + + return root; +} + std::shared_ptr Bonus::addUpdater(TUpdaterPtr Updater) { updater = Updater; diff --git a/lib/HeroBonus.h b/lib/HeroBonus.h index d60b04fca..24344a3ad 100644 --- a/lib/HeroBonus.h +++ b/lib/HeroBonus.h @@ -10,6 +10,7 @@ #pragma once #include "GameConstants.h" +#include "JsonNode.h" class CCreature; struct Bonus; @@ -424,6 +425,7 @@ struct DLL_LINKAGE Bonus : public std::enable_shared_from_this } std::string Description() const; + JsonNode toJsonNode() const; std::shared_ptr addLimiter(TLimiterPtr Limiter); //returns this for convenient chain-calls std::shared_ptr addPropagator(TPropagatorPtr Propagator); //returns this for convenient chain-calls @@ -1025,6 +1027,7 @@ public: virtual bool update(Bonus & b, const CBonusSystemNode & context) const = 0; virtual std::string toString() const; + virtual JsonNode toJsonNode() const = 0; template void serialize(Handler &h, const int version) { @@ -1048,4 +1051,5 @@ struct DLL_LINKAGE ScalingUpdater : public IUpdater bool update(Bonus & b, const CBonusSystemNode & context) const override; virtual std::string toString() const override; + virtual JsonNode toJsonNode() const override; };