From 5cfbdd2967b4bd5314d0a6bd681e78ad8d2d02b7 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 6 Jun 2023 18:32:53 +0300 Subject: [PATCH] Partially split CommonConstructors.cpp into few separate files --- AI/Nullkiller/Engine/FuzzyHelper.cpp | 2 +- AI/Nullkiller/Engine/PriorityEvaluator.cpp | 2 +- AI/VCAI/FuzzyHelper.cpp | 2 +- cmake_modules/VCMI_lib.cmake | 3 + .../CBankInstanceConstructor.cpp | 255 ++++++++++++++++++ .../CBankInstanceConstructor.h | 105 ++++++++ .../CDefaultObjectTypeHandler.h | 52 ++++ .../CObjectClassesHandler.cpp | 1 + .../CommonConstructors.cpp | 239 +--------------- .../CommonConstructors.h | 123 +-------- lib/mapObjects/CBank.cpp | 2 +- lib/registerTypes/RegisterTypes.h | 1 + 12 files changed, 423 insertions(+), 364 deletions(-) create mode 100644 lib/mapObjectConstructors/CBankInstanceConstructor.cpp create mode 100644 lib/mapObjectConstructors/CBankInstanceConstructor.h create mode 100644 lib/mapObjectConstructors/CDefaultObjectTypeHandler.h diff --git a/AI/Nullkiller/Engine/FuzzyHelper.cpp b/AI/Nullkiller/Engine/FuzzyHelper.cpp index 0815f4767..f9ac898dc 100644 --- a/AI/Nullkiller/Engine/FuzzyHelper.cpp +++ b/AI/Nullkiller/Engine/FuzzyHelper.cpp @@ -15,7 +15,7 @@ #include "../../../lib/mapObjectConstructors/AObjectTypeHandler.h" #include "../../../lib/mapObjectConstructors/CObjectClassesHandler.h" -#include "../../../lib/mapObjectConstructors/CommonConstructors.h" +#include "../../../lib/mapObjectConstructors/CBankInstanceConstructor.h" namespace NKAI { diff --git a/AI/Nullkiller/Engine/PriorityEvaluator.cpp b/AI/Nullkiller/Engine/PriorityEvaluator.cpp index 902470d97..1e8790f64 100644 --- a/AI/Nullkiller/Engine/PriorityEvaluator.cpp +++ b/AI/Nullkiller/Engine/PriorityEvaluator.cpp @@ -13,7 +13,7 @@ #include "Nullkiller.h" #include "../../../lib/mapObjectConstructors/AObjectTypeHandler.h" #include "../../../lib/mapObjectConstructors/CObjectClassesHandler.h" -#include "../../../lib/mapObjectConstructors/CommonConstructors.h" +#include "../../../lib/mapObjectConstructors/CBankInstanceConstructor.h" #include "../../../lib/mapObjects/MapObjects.h" #include "../../../lib/CCreatureHandler.h" #include "../../../lib/CPathfinder.h" diff --git a/AI/VCAI/FuzzyHelper.cpp b/AI/VCAI/FuzzyHelper.cpp index 2f2a75d67..1a7d1dc45 100644 --- a/AI/VCAI/FuzzyHelper.cpp +++ b/AI/VCAI/FuzzyHelper.cpp @@ -15,7 +15,7 @@ #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h" #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h" -#include "../../lib/mapObjectConstructors/CommonConstructors.h" +#include "../../lib/mapObjectConstructors/CBankInstanceConstructor.h" #include "../../lib/mapObjects/CBank.h" #include "../../lib/mapObjects/CGDwelling.h" diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index b7b7069be..888e01b17 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -66,6 +66,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/logging/CLogger.cpp ${MAIN_LIB_DIR}/mapObjectConstructors/AObjectTypeHandler.cpp + ${MAIN_LIB_DIR}/mapObjectConstructors/CBankInstanceConstructor.cpp ${MAIN_LIB_DIR}/mapObjectConstructors/CObjectClassesHandler.cpp ${MAIN_LIB_DIR}/mapObjectConstructors/CommonConstructors.cpp ${MAIN_LIB_DIR}/mapObjectConstructors/CRewardableConstructor.cpp @@ -369,6 +370,8 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/logging/CLogger.h ${MAIN_LIB_DIR}/mapObjectConstructors/AObjectTypeHandler.h + ${MAIN_LIB_DIR}/mapObjectConstructors/CBankInstanceConstructor.h + ${MAIN_LIB_DIR}/mapObjectConstructors/CDefaultObjectTypeHandler.h ${MAIN_LIB_DIR}/mapObjectConstructors/CObjectClassesHandler.h ${MAIN_LIB_DIR}/mapObjectConstructors/CommonConstructors.h ${MAIN_LIB_DIR}/mapObjectConstructors/CRewardableConstructor.h diff --git a/lib/mapObjectConstructors/CBankInstanceConstructor.cpp b/lib/mapObjectConstructors/CBankInstanceConstructor.cpp new file mode 100644 index 000000000..fb55d43f1 --- /dev/null +++ b/lib/mapObjectConstructors/CBankInstanceConstructor.cpp @@ -0,0 +1,255 @@ +/* +* CBankInstanceConstructor.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 "CBankInstanceConstructor.h" + +#include "../JsonRandom.h" +#include "../CGeneralTextHandler.h" +#include "../IGameCallback.h" + +VCMI_LIB_NAMESPACE_BEGIN + +bool CBankInstanceConstructor::hasNameTextID() const +{ + return true; +} + +void CBankInstanceConstructor::initTypeData(const JsonNode & input) +{ + if (input.Struct().count("name") == 0) + logMod->warn("Bank %s missing name!", getJsonKey()); + + VLC->generaltexth->registerString(input.meta, getNameTextID(), input["name"].String()); + + levels = input["levels"].Vector(); + bankResetDuration = static_cast(input["resetDuration"].Float()); +} + +CGObjectInstance *CBankInstanceConstructor::create(std::shared_ptr tmpl) const +{ + return createTyped(tmpl); +} + +BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRandomGenerator & rng) const +{ + BankConfig bc; + + bc.chance = static_cast(level["chance"].Float()); + + bc.guards = JsonRandom::loadCreatures(level["guards"], rng); + bc.upgradeChance = static_cast(level["upgrade_chance"].Float()); + bc.combatValue = static_cast(level["combat_value"].Float()); + + std::vector spells; + IObjectInterface::cb->getAllowedSpells(spells); + + bc.resources = ResourceSet(level["reward"]["resources"]); + bc.creatures = JsonRandom::loadCreatures(level["reward"]["creatures"], rng); + bc.artifacts = JsonRandom::loadArtifacts(level["reward"]["artifacts"], rng); + bc.spells = JsonRandom::loadSpells(level["reward"]["spells"], rng, spells); + + bc.value = static_cast(level["value"].Float()); + + return bc; +} + +void CBankInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const +{ + auto * bank = dynamic_cast(object); + + bank->resetDuration = bankResetDuration; + + si32 totalChance = 0; + for(const auto & node : levels) + totalChance += static_cast(node["chance"].Float()); + + assert(totalChance != 0); + + si32 selectedChance = rng.nextInt(totalChance - 1); + + int cumulativeChance = 0; + for(const auto & node : levels) + { + cumulativeChance += static_cast(node["chance"].Float()); + if(selectedChance < cumulativeChance) + { + bank->setConfig(generateConfig(node, rng)); + break; + } + } +} + +CBankInfo::CBankInfo(const JsonVector & Config) : + config(Config) +{ + assert(!Config.empty()); +} + +static void addStackToArmy(IObjectInfo::CArmyStructure & army, const CCreature * crea, si32 amount) +{ + army.totalStrength += crea->getFightValue() * amount; + + bool walker = true; + if(crea->hasBonusOfType(BonusType::SHOOTER)) + { + army.shootersStrength += crea->getFightValue() * amount; + walker = false; + } + if(crea->hasBonusOfType(BonusType::FLYING)) + { + army.flyersStrength += crea->getFightValue() * amount; + walker = false; + } + if(walker) + army.walkersStrength += crea->getFightValue() * amount; +} + +IObjectInfo::CArmyStructure CBankInfo::minGuards() const +{ + std::vector armies; + for(auto configEntry : config) + { + auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]); + IObjectInfo::CArmyStructure army; + for(auto & stack : stacks) + { + assert(!stack.allowedCreatures.empty()); + auto weakest = boost::range::min_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b) + { + return a->getFightValue() < b->getFightValue(); + }); + addStackToArmy(army, *weakest, stack.minAmount); + } + armies.push_back(army); + } + return *boost::range::min_element(armies); +} + +IObjectInfo::CArmyStructure CBankInfo::maxGuards() const +{ + std::vector armies; + for(auto configEntry : config) + { + auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]); + IObjectInfo::CArmyStructure army; + for(auto & stack : stacks) + { + assert(!stack.allowedCreatures.empty()); + auto strongest = boost::range::max_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b) + { + return a->getFightValue() < b->getFightValue(); + }); + addStackToArmy(army, *strongest, stack.maxAmount); + } + armies.push_back(army); + } + return *boost::range::max_element(armies); +} + +TPossibleGuards CBankInfo::getPossibleGuards() const +{ + TPossibleGuards out; + + for(const JsonNode & configEntry : config) + { + const JsonNode & guardsInfo = configEntry["guards"]; + auto stacks = JsonRandom::evaluateCreatures(guardsInfo); + IObjectInfo::CArmyStructure army; + + + for(auto stack : stacks) + { + army.totalStrength += stack.allowedCreatures.front()->getAIValue() * (stack.minAmount + stack.maxAmount) / 2; + //TODO: add fields for flyers, walkers etc... + } + + ui8 chance = static_cast(configEntry["chance"].Float()); + out.push_back(std::make_pair(chance, army)); + } + return out; +} + +std::vector> CBankInfo::getPossibleResourcesReward() const +{ + std::vector> result; + + for(const JsonNode & configEntry : config) + { + const JsonNode & resourcesInfo = configEntry["reward"]["resources"]; + + if(!resourcesInfo.isNull()) + { + result.emplace_back(configEntry["chance"].Integer(), TResources(resourcesInfo)); + } + } + + return result; +} + +std::vector> CBankInfo::getPossibleCreaturesReward() const +{ + std::vector> aproximateReward; + + for(const JsonNode & configEntry : config) + { + const JsonNode & guardsInfo = configEntry["reward"]["creatures"]; + auto stacks = JsonRandom::evaluateCreatures(guardsInfo); + + for(auto stack : stacks) + { + const auto * creature = stack.allowedCreatures.front(); + + aproximateReward.emplace_back(configEntry["chance"].Integer(), CStackBasicDescriptor(creature, (stack.minAmount + stack.maxAmount) / 2)); + } + } + + return aproximateReward; +} + +bool CBankInfo::givesResources() const +{ + for(const JsonNode & node : config) + if(!node["reward"]["resources"].isNull()) + return true; + return false; +} + +bool CBankInfo::givesArtifacts() const +{ + for(const JsonNode & node : config) + if(!node["reward"]["artifacts"].isNull()) + return true; + return false; +} + +bool CBankInfo::givesCreatures() const +{ + for(const JsonNode & node : config) + if(!node["reward"]["creatures"].isNull()) + return true; + return false; +} + +bool CBankInfo::givesSpells() const +{ + for(const JsonNode & node : config) + if(!node["reward"]["spells"].isNull()) + return true; + return false; +} + + +std::unique_ptr CBankInstanceConstructor::getObjectInfo(std::shared_ptr tmpl) const +{ + return std::unique_ptr(new CBankInfo(levels)); +} + + +VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjectConstructors/CBankInstanceConstructor.h b/lib/mapObjectConstructors/CBankInstanceConstructor.h new file mode 100644 index 000000000..232e61676 --- /dev/null +++ b/lib/mapObjectConstructors/CBankInstanceConstructor.h @@ -0,0 +1,105 @@ +/* +* CBankInstanceConstructor.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 "CDefaultObjectTypeHandler.h" +#include "IObjectInfo.h" + +#include "../CCreatureSet.h" +#include "../ResourceSet.h" +#include "../mapObjects/CBank.h" + +VCMI_LIB_NAMESPACE_BEGIN + +struct BankConfig +{ + ui32 value = 0; //overall value of given things + ui32 chance = 0; //chance for this level being chosen + ui32 upgradeChance = 0; //chance for creatures to be in upgraded versions + ui32 combatValue = 0; //how hard are guards of this level + std::vector guards; //creature ID, amount + ResourceSet resources; //resources given in case of victory + std::vector creatures; //creatures granted in case of victory (creature ID, amount) + std::vector artifacts; //artifacts given in case of victory + std::vector spells; // granted spell(s), for Pyramid + + template void serialize(Handler &h, const int version) + { + h & chance; + h & upgradeChance; + h & guards; + h & combatValue; + h & resources; + h & creatures; + h & artifacts; + h & value; + h & spells; + } +}; + +using TPossibleGuards = std::vector>; + +template +struct DLL_LINKAGE PossibleReward +{ + int chance; + T data; + + PossibleReward(int chance, const T & data) : chance(chance), data(data) {} +}; + +class DLL_LINKAGE CBankInfo : public IObjectInfo +{ + const JsonVector & config; +public: + CBankInfo(const JsonVector & Config); + + TPossibleGuards getPossibleGuards() const; + std::vector> getPossibleResourcesReward() const; + std::vector> getPossibleCreaturesReward() const; + + // These functions should try to evaluate minimal possible/max possible guards to give provide information on possible thread to AI + CArmyStructure minGuards() const override; + CArmyStructure maxGuards() const override; + + bool givesResources() const override; + bool givesArtifacts() const override; + bool givesCreatures() const override; + bool givesSpells() const override; +}; + +class CBankInstanceConstructor : public CDefaultObjectTypeHandler +{ + BankConfig generateConfig(const JsonNode & conf, CRandomGenerator & rng) const; + + JsonVector levels; +protected: + void initTypeData(const JsonNode & input) override; + +public: + // all banks of this type will be reset N days after clearing, + si32 bankResetDuration = 0; + + CGObjectInstance * create(std::shared_ptr tmpl = nullptr) const override; + void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override; + + bool hasNameTextID() const override; + + std::unique_ptr getObjectInfo(std::shared_ptr tmpl) const override; + + template void serialize(Handler &h, const int version) + { + h & levels; + h & bankResetDuration; + h & static_cast&>(*this); + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjectConstructors/CDefaultObjectTypeHandler.h b/lib/mapObjectConstructors/CDefaultObjectTypeHandler.h new file mode 100644 index 000000000..6b055e288 --- /dev/null +++ b/lib/mapObjectConstructors/CDefaultObjectTypeHandler.h @@ -0,0 +1,52 @@ +/* +* CDefaultObjectTypeHandler.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 "AObjectTypeHandler.h" + +VCMI_LIB_NAMESPACE_BEGIN + +/// Class that is used for objects that do not have dedicated handler +template +class CDefaultObjectTypeHandler : public AObjectTypeHandler +{ +protected: + ObjectType * createTyped(std::shared_ptr tmpl /* = nullptr */) const + { + auto obj = new ObjectType(); + preInitObject(obj); + + //Set custom template or leave null + if (tmpl) + { + obj->appearance = tmpl; + } + + return obj; + } +public: + CDefaultObjectTypeHandler() {} + + CGObjectInstance * create(std::shared_ptr tmpl = nullptr) const override + { + return createTyped(tmpl); + } + + virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override + { + } + + virtual std::unique_ptr getObjectInfo(std::shared_ptr tmpl) const override + { + return nullptr; + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjectConstructors/CObjectClassesHandler.cpp b/lib/mapObjectConstructors/CObjectClassesHandler.cpp index 063f6b173..57bd7392f 100644 --- a/lib/mapObjectConstructors/CObjectClassesHandler.cpp +++ b/lib/mapObjectConstructors/CObjectClassesHandler.cpp @@ -23,6 +23,7 @@ #include "../mapObjectConstructors/CRewardableConstructor.h" #include "../mapObjectConstructors/CommonConstructors.h" +#include "../mapObjectConstructors/CBankInstanceConstructor.h" #include "../mapObjects/CQuest.h" #include "../mapObjects/CGPandoraBox.h" #include "../mapObjects/ObjectTemplate.h" diff --git a/lib/mapObjectConstructors/CommonConstructors.cpp b/lib/mapObjectConstructors/CommonConstructors.cpp index 434cf2083..7f7a5464e 100644 --- a/lib/mapObjectConstructors/CommonConstructors.cpp +++ b/lib/mapObjectConstructors/CommonConstructors.cpp @@ -22,7 +22,7 @@ #include "../mapObjects/CGTownInstance.h" #include "../mapObjects/ObjectTemplate.h" #include "../mapping/CMapDefines.h" -#include "JsonRandom.h" +#include "../JsonRandom.h" VCMI_LIB_NAMESPACE_BEGIN @@ -356,241 +356,4 @@ void MarketInstanceConstructor::configureObject(CGObjectInstance * object, CRand } } - -bool CBankInstanceConstructor::hasNameTextID() const -{ - return true; -} - -void CBankInstanceConstructor::initTypeData(const JsonNode & input) -{ - if (input.Struct().count("name") == 0) - logMod->warn("Bank %s missing name!", getJsonKey()); - - VLC->generaltexth->registerString(input.meta, getNameTextID(), input["name"].String()); - - levels = input["levels"].Vector(); - bankResetDuration = static_cast(input["resetDuration"].Float()); -} - -CGObjectInstance *CBankInstanceConstructor::create(std::shared_ptr tmpl) const -{ - return createTyped(tmpl); -} - -BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRandomGenerator & rng) const -{ - BankConfig bc; - - bc.chance = static_cast(level["chance"].Float()); - - bc.guards = JsonRandom::loadCreatures(level["guards"], rng); - bc.upgradeChance = static_cast(level["upgrade_chance"].Float()); - bc.combatValue = static_cast(level["combat_value"].Float()); - - std::vector spells; - for (size_t i=0; i<6; i++) - IObjectInterface::cb->getAllowedSpells(spells, static_cast(i)); - - bc.resources = ResourceSet(level["reward"]["resources"]); - bc.creatures = JsonRandom::loadCreatures(level["reward"]["creatures"], rng); - bc.artifacts = JsonRandom::loadArtifacts(level["reward"]["artifacts"], rng); - bc.spells = JsonRandom::loadSpells(level["reward"]["spells"], rng, spells); - - bc.value = static_cast(level["value"].Float()); - - return bc; -} - -void CBankInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const -{ - auto * bank = dynamic_cast(object); - - bank->resetDuration = bankResetDuration; - - si32 totalChance = 0; - for(const auto & node : levels) - totalChance += static_cast(node["chance"].Float()); - - assert(totalChance != 0); - - si32 selectedChance = rng.nextInt(totalChance - 1); - - int cumulativeChance = 0; - for(const auto & node : levels) - { - cumulativeChance += static_cast(node["chance"].Float()); - if(selectedChance < cumulativeChance) - { - bank->setConfig(generateConfig(node, rng)); - break; - } - } -} - -CBankInfo::CBankInfo(const JsonVector & Config) : - config(Config) -{ - assert(!Config.empty()); -} - -static void addStackToArmy(IObjectInfo::CArmyStructure & army, const CCreature * crea, si32 amount) -{ - army.totalStrength += crea->getFightValue() * amount; - - bool walker = true; - if(crea->hasBonusOfType(BonusType::SHOOTER)) - { - army.shootersStrength += crea->getFightValue() * amount; - walker = false; - } - if(crea->hasBonusOfType(BonusType::FLYING)) - { - army.flyersStrength += crea->getFightValue() * amount; - walker = false; - } - if(walker) - army.walkersStrength += crea->getFightValue() * amount; -} - -IObjectInfo::CArmyStructure CBankInfo::minGuards() const -{ - std::vector armies; - for(auto configEntry : config) - { - auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]); - IObjectInfo::CArmyStructure army; - for(auto & stack : stacks) - { - assert(!stack.allowedCreatures.empty()); - auto weakest = boost::range::min_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b) - { - return a->getFightValue() < b->getFightValue(); - }); - addStackToArmy(army, *weakest, stack.minAmount); - } - armies.push_back(army); - } - return *boost::range::min_element(armies); -} - -IObjectInfo::CArmyStructure CBankInfo::maxGuards() const -{ - std::vector armies; - for(auto configEntry : config) - { - auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]); - IObjectInfo::CArmyStructure army; - for(auto & stack : stacks) - { - assert(!stack.allowedCreatures.empty()); - auto strongest = boost::range::max_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b) - { - return a->getFightValue() < b->getFightValue(); - }); - addStackToArmy(army, *strongest, stack.maxAmount); - } - armies.push_back(army); - } - return *boost::range::max_element(armies); -} - -TPossibleGuards CBankInfo::getPossibleGuards() const -{ - TPossibleGuards out; - - for(const JsonNode & configEntry : config) - { - const JsonNode & guardsInfo = configEntry["guards"]; - auto stacks = JsonRandom::evaluateCreatures(guardsInfo); - IObjectInfo::CArmyStructure army; - - - for(auto stack : stacks) - { - army.totalStrength += stack.allowedCreatures.front()->getAIValue() * (stack.minAmount + stack.maxAmount) / 2; - //TODO: add fields for flyers, walkers etc... - } - - ui8 chance = static_cast(configEntry["chance"].Float()); - out.push_back(std::make_pair(chance, army)); - } - return out; -} - -std::vector> CBankInfo::getPossibleResourcesReward() const -{ - std::vector> result; - - for(const JsonNode & configEntry : config) - { - const JsonNode & resourcesInfo = configEntry["reward"]["resources"]; - - if(!resourcesInfo.isNull()) - { - result.emplace_back(configEntry["chance"].Integer(), TResources(resourcesInfo)); - } - } - - return result; -} - -std::vector> CBankInfo::getPossibleCreaturesReward() const -{ - std::vector> aproximateReward; - - for(const JsonNode & configEntry : config) - { - const JsonNode & guardsInfo = configEntry["reward"]["creatures"]; - auto stacks = JsonRandom::evaluateCreatures(guardsInfo); - - for(auto stack : stacks) - { - const auto * creature = stack.allowedCreatures.front(); - - aproximateReward.emplace_back(configEntry["chance"].Integer(), CStackBasicDescriptor(creature, (stack.minAmount + stack.maxAmount) / 2)); - } - } - - return aproximateReward; -} - -bool CBankInfo::givesResources() const -{ - for(const JsonNode & node : config) - if(!node["reward"]["resources"].isNull()) - return true; - return false; -} - -bool CBankInfo::givesArtifacts() const -{ - for(const JsonNode & node : config) - if(!node["reward"]["artifacts"].isNull()) - return true; - return false; -} - -bool CBankInfo::givesCreatures() const -{ - for(const JsonNode & node : config) - if(!node["reward"]["creatures"].isNull()) - return true; - return false; -} - -bool CBankInfo::givesSpells() const -{ - for(const JsonNode & node : config) - if(!node["reward"]["spells"].isNull()) - return true; - return false; -} - - -std::unique_ptr CBankInstanceConstructor::getObjectInfo(std::shared_ptr tmpl) const -{ - return std::unique_ptr(new CBankInfo(levels)); -} - VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjectConstructors/CommonConstructors.h b/lib/mapObjectConstructors/CommonConstructors.h index 12745afe0..d50630714 100644 --- a/lib/mapObjectConstructors/CommonConstructors.h +++ b/lib/mapObjectConstructors/CommonConstructors.h @@ -10,14 +10,13 @@ #pragma once #include "AObjectTypeHandler.h" +#include "CDefaultObjectTypeHandler.h" #include "../mapObjects/CGMarket.h" #include "../mapObjects/MiscObjects.h" #include "../mapObjects/CGHeroInstance.h" #include "../mapObjects/CGTownInstance.h" -#include "../mapObjects/CBank.h" #include "../LogicalExpression.h" -#include "IObjectInfo.h" VCMI_LIB_NAMESPACE_BEGIN @@ -30,42 +29,6 @@ class CBank; class CFaction; class CStackBasicDescriptor; -/// Class that is used for objects that do not have dedicated handler -template -class CDefaultObjectTypeHandler : public AObjectTypeHandler -{ -protected: - ObjectType * createTyped(std::shared_ptr tmpl /* = nullptr */) const - { - auto obj = new ObjectType(); - preInitObject(obj); - - //Set custom template or leave null - if (tmpl) - { - obj->appearance = tmpl; - } - - return obj; - } -public: - CDefaultObjectTypeHandler() {} - - CGObjectInstance * create(std::shared_ptr tmpl = nullptr) const override - { - return createTyped(tmpl); - } - - virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override - { - } - - virtual std::unique_ptr getObjectInfo(std::shared_ptr tmpl) const override - { - return nullptr; - } -}; - class CObstacleConstructor : public CDefaultObjectTypeHandler { public: @@ -178,90 +141,6 @@ public: } }; -struct BankConfig -{ - ui32 value = 0; //overall value of given things - ui32 chance = 0; //chance for this level being chosen - ui32 upgradeChance = 0; //chance for creatures to be in upgraded versions - ui32 combatValue = 0; //how hard are guards of this level - std::vector guards; //creature ID, amount - ResourceSet resources; //resources given in case of victory - std::vector creatures; //creatures granted in case of victory (creature ID, amount) - std::vector artifacts; //artifacts given in case of victory - std::vector spells; // granted spell(s), for Pyramid - - template void serialize(Handler &h, const int version) - { - h & chance; - h & upgradeChance; - h & guards; - h & combatValue; - h & resources; - h & creatures; - h & artifacts; - h & value; - h & spells; - } -}; - -using TPossibleGuards = std::vector>; - -template -struct DLL_LINKAGE PossibleReward -{ - int chance; - T data; - - PossibleReward(int chance, const T & data) : chance(chance), data(data) {} -}; - -class DLL_LINKAGE CBankInfo : public IObjectInfo -{ - const JsonVector & config; -public: - CBankInfo(const JsonVector & Config); - - TPossibleGuards getPossibleGuards() const; - std::vector> getPossibleResourcesReward() const; - std::vector> getPossibleCreaturesReward() const; - - // These functions should try to evaluate minimal possible/max possible guards to give provide information on possible thread to AI - CArmyStructure minGuards() const override; - CArmyStructure maxGuards() const override; - - bool givesResources() const override; - bool givesArtifacts() const override; - bool givesCreatures() const override; - bool givesSpells() const override; -}; - -class CBankInstanceConstructor : public CDefaultObjectTypeHandler -{ - BankConfig generateConfig(const JsonNode & conf, CRandomGenerator & rng) const; - - JsonVector levels; -protected: - void initTypeData(const JsonNode & input) override; - -public: - // all banks of this type will be reset N days after clearing, - si32 bankResetDuration = 0; - - CGObjectInstance * create(std::shared_ptr tmpl = nullptr) const override; - void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override; - - bool hasNameTextID() const override; - - std::unique_ptr getObjectInfo(std::shared_ptr tmpl) const override; - - template void serialize(Handler &h, const int version) - { - h & levels; - h & bankResetDuration; - h & static_cast&>(*this); - } -}; - class MarketInstanceConstructor : public CDefaultObjectTypeHandler { protected: diff --git a/lib/mapObjects/CBank.cpp b/lib/mapObjects/CBank.cpp index a5b090bf4..efccca9c7 100644 --- a/lib/mapObjects/CBank.cpp +++ b/lib/mapObjects/CBank.cpp @@ -18,7 +18,7 @@ #include "../CGeneralTextHandler.h" #include "../CSoundBase.h" #include "../mapObjectConstructors/CObjectClassesHandler.h" -#include "../mapObjectConstructors/CommonConstructors.h" +#include "../mapObjectConstructors/CBankInstanceConstructor.h" #include "../IGameCallback.h" #include "../CGameState.h" diff --git a/lib/registerTypes/RegisterTypes.h b/lib/registerTypes/RegisterTypes.h index 741cb0b6f..d38af4b1b 100644 --- a/lib/registerTypes/RegisterTypes.h +++ b/lib/registerTypes/RegisterTypes.h @@ -20,6 +20,7 @@ #include "../CModHandler.h" //needed? #include "../mapObjectConstructors/CRewardableConstructor.h" #include "../mapObjectConstructors/CommonConstructors.h" +#include "../mapObjectConstructors/CBankInstanceConstructor.h" #include "../mapObjects/MapObjects.h" #include "../mapObjects/CGTownBuilding.h" #include "../mapObjects/ObjectTemplate.h"