From 408f34654159c491361d5c852f7e0fc17f3580f4 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sun, 24 Jan 2016 02:27:14 +0300 Subject: [PATCH] Drafts for hero serialization --- lib/CArtHandler.cpp | 11 ++++++ lib/CArtHandler.h | 6 +++- lib/StringConstants.h | 16 ++++++++- lib/mapObjects/CGHeroInstance.cpp | 59 +++++++++++++++++++------------ lib/mapObjects/CGHeroInstance.h | 16 +++++---- lib/mapping/MapFormatJson.cpp | 1 - 6 files changed, 76 insertions(+), 33 deletions(-) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 0946658f6..d613e488d 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -19,6 +19,7 @@ #include "mapObjects/MapObjects.h" #include "NetPacksBase.h" #include "GameConstants.h" +#include "StringConstants.h" #include "CRandomGenerator.h" #include "mapObjects/CObjectClassesHandler.h" @@ -1258,3 +1259,13 @@ void CArtifactSet::artDeserializationFix(CBonusSystemNode *node) if(elem.second.artifact && !elem.second.locked) node->attachTo(elem.second.artifact); } + +void CArtifactSet::writeJson(JsonNode& json) const +{ + +} + +void CArtifactSet::readJson(const JsonNode& json) +{ + +} diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index deea66062..808193f88 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -48,7 +48,7 @@ protected: std::string eventText; //short story displayed upon picking public: enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes - + std::string identifier; std::string image; std::string large; // big image for cutom artifacts, used in drag & drop @@ -309,4 +309,8 @@ public: } void artDeserializationFix(CBonusSystemNode *node); + +protected: + void writeJson(JsonNode & json) const; + void readJson(const JsonNode & json); }; diff --git a/lib/StringConstants.h b/lib/StringConstants.h index 3004d3afa..78fa1a8d8 100644 --- a/lib/StringConstants.h +++ b/lib/StringConstants.h @@ -21,7 +21,7 @@ namespace GameConstants const std::string TERRAIN_NAMES [TERRAIN_TYPES] = { "dirt", "sand", "grass", "snow", "swamp", "rough", "subterra", "lava", "water", "rock" }; - + const std::string RESOURCE_NAMES [RESOURCE_QUANTITY] = { "wood", "mercury", "ore", "sulfur", "crystal", "gems", "gold", "mithril" }; @@ -84,3 +84,17 @@ namespace ETownType "stronghold", "fortress", "conflux" }; } + +namespace NArtifactPosition +{ + const std::string names [19] = + { + "head", "shoulders", "neck", "rightHand", "leftHand", "torso", //5 + "rightRing", "leftRing", "feet", //8 + "misc1", "misc2", "misc3", "misc4", //12 + "mach1", "mach2", "mach3", "mach4", //16 + "spellbook", "misc5" //18 + }; + + const std::string backpack = "backpack"; +} diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 2299c42a0..9633c1bda 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -868,18 +868,18 @@ TExpType CGHeroInstance::calculateXp(TExpType exp) const ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool) const { si16 skill = -1; //skill level - + spell->forEachSchool([&, this](const SpellSchoolInfo & cnf, bool & stop) { int thisSchool = std::max(getSecSkillLevel(cnf.skill), valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1 << ((ui8)cnf.id))); //FIXME: Bonus shouldn't be additive (Witchking Artifacts : Crown of Skies) - if(thisSchool > skill) - { - skill = thisSchool; - if(outSelectedSchool) - *outSelectedSchool = (ui8)cnf.id; - } + if(thisSchool > skill) + { + skill = thisSchool; + if(outSelectedSchool) + *outSelectedSchool = (ui8)cnf.id; + } }); - + vstd::amax(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus vstd::amax(skill, valOfBonuses(Bonus::SPELL, spell->id.toEnum())); //given by artifact or other effect @@ -904,7 +904,7 @@ ui32 CGHeroInstance::getSpellBonus(const CSpell * spell, ui32 base, const CStack if (affectedStack && affectedStack->getCreature()->level) //Hero specials like Solmyr, Deemer base *= (100. + ((valOfBonuses(Bonus::SPECIAL_SPELL_LEV, spell->id.toEnum()) * level) / affectedStack->getCreature()->level)) / 100.0; - return base; + return base; } int CGHeroInstance::getEffectLevel(const CSpell * spell) const @@ -912,7 +912,7 @@ int CGHeroInstance::getEffectLevel(const CSpell * spell) const if(hasBonusOfType(Bonus::MAXED_SPELL, spell->id)) return 3;//todo: recheck specialty from where this bonus is. possible bug else - return getSpellSchoolLevel(spell); + return getSpellSchoolLevel(spell); } int CGHeroInstance::getEffectPower(const CSpell * spell) const @@ -922,7 +922,7 @@ int CGHeroInstance::getEffectPower(const CSpell * spell) const int CGHeroInstance::getEnchantPower(const CSpell * spell) const { - return getPrimSkillLevel(PrimarySkill::SPELL_POWER) + valOfBonuses(Bonus::SPELL_DURATION); + return getPrimSkillLevel(PrimarySkill::SPELL_POWER) + valOfBonuses(Bonus::SPELL_DURATION); } int CGHeroInstance::getEffectValue(const CSpell * spell) const @@ -1107,8 +1107,8 @@ void CGHeroInstance::getOutOffsets(std::vector &offsets) const { // FIXME: Offsets need to be fixed once we get rid of convertPosition // Check issue 515 for details - offsets = - { + offsets = + { int3(-1,1,0), int3(-1,-1,0), int3(-2,0,0), int3(0,0,0), int3(0,1,0), int3(-2,1,0), int3(0,-1,0), int3(-2,-1,0) }; } @@ -1436,20 +1436,33 @@ void CGHeroInstance::levelUpAutomatically() bool CGHeroInstance::hasVisions(const CGObjectInstance * target, const int subtype) const { //VISIONS spell support - - const std::string cached = boost::to_string((boost::format("type_%d__subtype_%d") % Bonus::VISIONS % subtype)); - + + const std::string cached = boost::to_string((boost::format("type_%d__subtype_%d") % Bonus::VISIONS % subtype)); + const int visionsMultiplier = valOfBonuses(Selector::typeSubtype(Bonus::VISIONS,subtype), cached); - + int visionsRange = visionsMultiplier * getPrimSkillLevel(PrimarySkill::SPELL_POWER); - - if (visionsMultiplier > 0) + + if (visionsMultiplier > 0) vstd::amax(visionsRange, 3); //minimum range is 3 tiles, but only if VISIONS bonus present - + const int distance = target->pos.dist2d(getPosition(false)); - + //logGlobal->debug(boost::to_string(boost::format("Visions: dist %d, mult %d, range %d") % distance % visionsMultiplier % visionsRange)); - - return (distance < visionsRange) && (target->pos.z == pos.z); + + return (distance < visionsRange) && (target->pos.z == pos.z); } +void CGHeroInstance::writeJsonOptions(JsonNode& json) const +{ + CArmedInstance::writeJsonOptions(json); + CGObjectInstance::writeOwner(json); + CArtifactSet::writeJson(json["artifacts"]); +} + +void CGHeroInstance::readJsonOptions(const JsonNode& json) +{ + CArmedInstance::readJsonOptions(json); + CGObjectInstance::readOwner(json); + CArtifactSet::readJson(json["artifacts"]); +} diff --git a/lib/mapObjects/CGHeroInstance.h b/lib/mapObjects/CGHeroInstance.h index a6cc59cb1..0177f9336 100644 --- a/lib/mapObjects/CGHeroInstance.h +++ b/lib/mapObjects/CGHeroInstance.h @@ -177,7 +177,7 @@ public: double getHeroStrength() const; // includes fighting and magic strength ui64 getTotalStrength() const; // includes fighting strength and army strength TExpType calculateXp(TExpType exp) const; //apply learning skill - + bool canCastThisSpell(const CSpell * spell) const; //determines if this hero can cast given spell; takes into account existing spell in spellbook, existing spellbook and artifact bonuses CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const; void showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const; @@ -201,23 +201,23 @@ public: void Updatespecialty(); void recreateSecondarySkillsBonuses(); void updateSkill(SecondarySkill which, int val); - + bool hasVisions(const CGObjectInstance * target, const int subtype) const; CGHeroInstance(); virtual ~CGHeroInstance(); - + ///ArtBearer ArtBearer::ArtBearer bearerType() const override; ///IBonusBearer CBonusSystemNode *whereShouldBeAttached(CGameState *gs) override; std::string nodeName() const override; - + ///ISpellCaster ui8 getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool = nullptr) const override; ui32 getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const override; - + ///default spell school level for effect calculation int getEffectLevel(const CSpell * spell) const override; @@ -229,9 +229,9 @@ public: ///damage/heal override(ignores spell configuration, effect level and effect power) int getEffectValue(const CSpell * spell) const override; - + const PlayerColor getOwner() const override; - + void deserializationFix(); void initObj() override; @@ -239,6 +239,8 @@ public: std::string getObjectName() const override; protected: void setPropertyDer(ui8 what, ui32 val) override;//synchr + void writeJsonOptions(JsonNode & json) const override; + void readJsonOptions(const JsonNode & json) override; private: void levelUpAutomatically(); diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index 8a65c3f04..eb8d5c2e2 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -683,7 +683,6 @@ void CMapSaverJson::addToArchive(const JsonNode& data, const std::string& filena void CMapSaverJson::saveMap(const std::unique_ptr& map) { - //TODO: saveMap this->map = map.get(); writeHeader(); writeTerrain();