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

Cleaned up object constructors to reduce duplicated code

This commit is contained in:
Ivan Savenko 2023-06-08 00:42:47 +03:00
parent 4d947be287
commit 77b58bc66d
19 changed files with 111 additions and 183 deletions

View File

@ -146,21 +146,19 @@
}
},
"types" : {
"boatNecropolis" : // Necropolis
{
"boatNecropolis" : {
"index" : 0,
"actualAnimation" : "AB01_.def",
"overlayAnimation" : "ABM01_.def",
"flagAnimations" : ["ABF01L", "ABF01G", "ABF01R", "ABF01D", "ABF01B", "ABF01P", "ABF01W", "ABF01K"]
},
"boatCastle" : // Castle
{
"boatCastle" : {
"index" : 1,
"actualAnimation" : "AB02_.def",
"overlayAnimation" : "ABM02_.def",
"flagAnimations" : ["ABF02L", "ABF02G", "ABF02R", "ABF02D", "ABF02B", "ABF02P", "ABF02W", "ABF02K"]
},
"boatFortress" : { // Fortress
"boatFortress" : {
"index" : 2,
"actualAnimation" : "AB03_.def",
"overlayAnimation" : "ABM03_.def",

View File

@ -124,6 +124,10 @@
"type" : "string",
"description" : "Identifier of war machine produced by blacksmith in town"
},
"boat" : {
"type" : "string",
"description" : "Identifier of boat type that is produced by shipyard in town, if any"
},
"horde" : {
"type" : "array",
"maxItems" : 2,

View File

@ -11,6 +11,7 @@
#include "StdInc.h"
#include "AObjectTypeHandler.h"
#include "IObjectInfo.h"
#include "../CGeneralTextHandler.h"
#include "../VCMI_Lib.h"
#include "../mapObjects/CGObjectInstance.h"
@ -221,5 +222,9 @@ void AObjectTypeHandler::afterLoadFinalization()
{
}
std::unique_ptr<IObjectInfo> AObjectTypeHandler::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
{
return nullptr;
}
VCMI_LIB_NAMESPACE_END

View File

@ -107,7 +107,7 @@ public:
virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
/// Returns object configuration, if available. Otherwise returns NULL
virtual std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const = 0;
virtual std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const;
template <typename Handler> void serialize(Handler &h, const int version)
{

View File

@ -32,11 +32,6 @@ void CBankInstanceConstructor::initTypeData(const JsonNode & input)
bankResetDuration = static_cast<si32>(input["resetDuration"].Float());
}
CGObjectInstance *CBankInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
{
return createTyped(tmpl);
}
BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRandomGenerator & rng) const
{
BankConfig bc;
@ -60,10 +55,8 @@ BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRan
return bc;
}
void CBankInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
void CBankInstanceConstructor::randomizeObject(CBank * bank, CRandomGenerator & rng) const
{
auto * bank = dynamic_cast<CBank *>(object);
bank->resetDuration = bankResetDuration;
si32 totalChance = 0;

View File

@ -87,8 +87,7 @@ public:
// all banks of this type will be reset N days after clearing,
si32 bankResetDuration = 0;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
void randomizeObject(CBank * object, CRandomGenerator & rng) const override;
bool hasNameTextID() const override;

View File

@ -13,39 +13,40 @@
VCMI_LIB_NAMESPACE_BEGIN
/// Class that is used for objects that do not have dedicated handler
/// Class that is used as base for multiple object constructors
template<class ObjectType>
class CDefaultObjectTypeHandler : public AObjectTypeHandler
{
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const final
{
ObjectType * castedObject = dynamic_cast<ObjectType*>(object);
if(castedObject == nullptr)
throw std::runtime_error("Unexpected object type!");
randomizeObject(castedObject, rng);
}
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const final
{
ObjectType * result = createObject();
preInitObject(result);
if(tmpl)
result->appearance = tmpl;
initializeObject(result);
return result;
}
protected:
ObjectType * createTyped(std::shared_ptr<const ObjectTemplate> tmpl /* = nullptr */) const
virtual void initializeObject(ObjectType * object) const {}
virtual void randomizeObject(ObjectType * object, CRandomGenerator & rng) const {}
virtual ObjectType * createObject() 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<const ObjectTemplate> tmpl = nullptr) const override
{
return createTyped(tmpl);
}
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override
{
}
std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override
{
return nullptr;
return new ObjectType();
}
};

View File

@ -30,6 +30,11 @@
#include "../mapObjects/CGPandoraBox.h"
#include "../mapObjects/CQuest.h"
#include "../mapObjects/ObjectTemplate.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../mapObjects/CGTownInstance.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -15,14 +15,18 @@
#include "../CHeroHandler.h"
#include "../CModHandler.h"
#include "../IGameCallback.h"
#include "../JsonRandom.h"
#include "../StringConstants.h"
#include "../TerrainHandler.h"
#include "../mapObjects/CBank.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/CGTownInstance.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/ObjectTemplate.h"
#include "../mapping/CMapDefines.h"
#include "../JsonRandom.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -69,15 +73,13 @@ bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std
return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
}
CGObjectInstance * CTownInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
{
CGTownInstance * obj = createTyped(tmpl);
obj->town = faction->town;
obj->tempOwner = PlayerColor::NEUTRAL;
return obj;
}
void CTownInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const
{
auto templ = getOverride(CGObjectInstance::cb->getTile(object->pos)->terType->getId(), object);
if(templ)
@ -121,14 +123,12 @@ bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, std
return false;
}
CGObjectInstance * CHeroInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void CHeroInstanceConstructor::initializeObject(CGHeroInstance * obj) const
{
CGHeroInstance * obj = createTyped(tmpl);
obj->type = nullptr; //FIXME: set to valid value. somehow.
return obj;
}
void CHeroInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomGenerator & rng) const
{
}
@ -172,20 +172,17 @@ bool CDwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, st
return false;
}
CGObjectInstance * CDwellingInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void CDwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
{
CGDwelling * obj = createTyped(tmpl);
obj->creatures.resize(availableCreatures.size());
for(const auto & entry : availableCreatures)
{
for(const CCreature * cre : entry)
obj->creatures.back().second.push_back(cre->getId());
}
return obj;
}
void CDwellingInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator &rng) const
void CDwellingInstanceConstructor::randomizeObject(CGDwelling * object, CRandomGenerator &rng) const
{
auto * dwelling = dynamic_cast<CGDwelling *>(object);
@ -274,9 +271,8 @@ void BoatInstanceConstructor::initTypeData(const JsonNode & input)
bonuses = JsonRandom::loadBonuses(input["bonuses"]);
}
CGObjectInstance * BoatInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
{
CGBoat * boat = createTyped(tmpl);
boat->layer = layer;
boat->actualAnimation = actualAnimation;
boat->overlayAnimation = overlayAnimation;
@ -285,8 +281,6 @@ CGObjectInstance * BoatInstanceConstructor::create(std::shared_ptr<const ObjectT
boat->onboardVisitAllowed = onboardVisitAllowed;
for(auto & b : bonuses)
boat->addNewBonus(std::make_shared<Bonus>(b));
return boat;
}
std::string BoatInstanceConstructor::getBoatAnimationName() const
@ -294,11 +288,6 @@ std::string BoatInstanceConstructor::getBoatAnimationName() const
return actualAnimation;
}
void BoatInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
{
}
void BoatInstanceConstructor::afterLoadFinalization()
{
if (layer == EPathfindingLayer::SAIL)
@ -323,31 +312,25 @@ void MarketInstanceConstructor::initTypeData(const JsonNode & input)
speech = input["speech"].String();
}
CGObjectInstance * MarketInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
CGMarket * MarketInstanceConstructor::createObject() const
{
CGMarket * market = nullptr;
if(marketModes.size() == 1)
{
switch(*marketModes.begin())
{
case EMarketMode::ARTIFACT_RESOURCE:
case EMarketMode::RESOURCE_ARTIFACT:
market = new CGBlackMarket;
break;
return new CGBlackMarket;
case EMarketMode::RESOURCE_SKILL:
market = new CGUniversity;
break;
return new CGUniversity;
}
}
if(!market)
market = new CGMarket;
preInitObject(market);
return new CGMarket;
}
if(tmpl)
market->appearance = tmpl;
void MarketInstanceConstructor::initializeObject(CGMarket * market) const
{
market->marketModes = marketModes;
market->marketEfficiency = marketEfficiency;
@ -357,11 +340,9 @@ CGObjectInstance * MarketInstanceConstructor::create(std::shared_ptr<const Objec
if (!speech.empty())
market->speech = VLC->generaltexth->translate(speech);
return market;
}
void MarketInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
void MarketInstanceConstructor::randomizeObject(CGMarket * object, CRandomGenerator & rng) const
{
if(auto * university = dynamic_cast<CGUniversity *>(object))
{

View File

@ -12,20 +12,19 @@
#include "AObjectTypeHandler.h"
#include "CDefaultObjectTypeHandler.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../mapObjects/CGTownInstance.h"
#include "../LogicalExpression.h"
VCMI_LIB_NAMESPACE_BEGIN
class CGArtifact;
class CGObjectInstance;
class CGTownInstance;
class CGHeroInstance;
class CGDwelling;
class CGMarket;
class CHeroClass;
class CBank;
class CGBoat;
class CFaction;
class CStackBasicDescriptor;
@ -46,8 +45,8 @@ public:
CFaction * faction = nullptr;
std::map<std::string, LogicalExpression<BuildingID>> filters;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
void initializeObject(CGTownInstance * object) const override;
void randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const override;
void afterLoadFinalization() override;
template <typename Handler> void serialize(Handler &h, const int version)
@ -70,8 +69,8 @@ public:
CHeroClass * heroClass = nullptr;
std::map<std::string, LogicalExpression<HeroTypeID>> filters;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
void initializeObject(CGHeroInstance * object) const override;
void randomizeObject(CGHeroInstance * object, CRandomGenerator & rng) const override;
void afterLoadFinalization() override;
template <typename Handler> void serialize(Handler &h, const int version)
@ -96,8 +95,8 @@ protected:
public:
bool hasNameTextID() const override;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
void initializeObject(CGDwelling * object) const override;
void randomizeObject(CGDwelling * object, CRandomGenerator & rng) const override;
bool producesCreature(const CCreature * crea) const;
std::vector<const CCreature *> getProducedCreatures() const;
@ -125,8 +124,7 @@ protected:
std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
public:
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
void initializeObject(CGBoat * object) const override;
void afterLoadFinalization() override;
/// Returns boat preview animation, for use in Shipyards
@ -157,8 +155,9 @@ protected:
std::string title, speech;
public:
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
CGMarket * createObject() const override;
void initializeObject(CGMarket * object) const override;
void randomizeObject(CGMarket * object, CRandomGenerator & rng) const override;
template <typename Handler> void serialize(Handler &h, const int version)
{

View File

@ -11,7 +11,6 @@
#include "HillFortInstanceConstructor.h"
#include "../mapObjects/MiscObjects.h"
#include "IObjectInfo.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -20,26 +19,9 @@ void HillFortInstanceConstructor::initTypeData(const JsonNode & config)
parameters = config;
}
CGObjectInstance * HillFortInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void HillFortInstanceConstructor::initializeObject(HillFort * fort) const
{
HillFort * fort = new HillFort;
preInitObject(fort);
if(tmpl)
fort->appearance = tmpl;
fort->upgradeCostPercentage = parameters["upgradeCostFactor"].convertTo<std::vector<int>>();
return fort;
}
void HillFortInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
{
}
std::unique_ptr<IObjectInfo> HillFortInstanceConstructor::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
{
return nullptr;
}
VCMI_LIB_NAMESPACE_END

View File

@ -9,19 +9,19 @@
*/
#pragma once
#include "AObjectTypeHandler.h"
#include "CDefaultObjectTypeHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
class HillFortInstanceConstructor final : public AObjectTypeHandler
class HillFort;
class HillFortInstanceConstructor final : public CDefaultObjectTypeHandler<HillFort>
{
JsonNode parameters;
protected:
void initTypeData(const JsonNode & config) override;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override;
void initializeObject(HillFort * object) const override;
public:
template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -11,7 +11,6 @@
#include "ShipyardInstanceConstructor.h"
#include "../mapObjects/MiscObjects.h"
#include "IObjectInfo.h"
#include "../CModHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -21,27 +20,9 @@ void ShipyardInstanceConstructor::initTypeData(const JsonNode & config)
parameters = config;
}
CGObjectInstance * ShipyardInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void ShipyardInstanceConstructor::initializeObject(CGShipyard * shipyard) const
{
CGShipyard * shipyard = new CGShipyard;
preInitObject(shipyard);
if(tmpl)
shipyard->appearance = tmpl;
shipyard->createdBoat = BoatId(*VLC->modh->identifiers.getIdentifier("core:boat", parameters["boat"]));
return shipyard;
}
void ShipyardInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
{
}
std::unique_ptr<IObjectInfo> ShipyardInstanceConstructor::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
{
return nullptr;
}
VCMI_LIB_NAMESPACE_END

View File

@ -9,19 +9,19 @@
*/
#pragma once
#include "AObjectTypeHandler.h"
#include "CDefaultObjectTypeHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
class ShipyardInstanceConstructor final : public AObjectTypeHandler
class CGShipyard;
class ShipyardInstanceConstructor final : public CDefaultObjectTypeHandler<CGShipyard>
{
JsonNode parameters;
protected:
void initTypeData(const JsonNode & config) override;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override;
void initializeObject(CGShipyard * object) const override;
public:
template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -10,7 +10,6 @@
#include "StdInc.h"
#include "ShrineInstanceConstructor.h"
#include "IObjectInfo.h"
#include "../mapObjects/MiscObjects.h"
#include "../JsonRandom.h"
#include "../IGameCallback.h"
@ -22,25 +21,8 @@ void ShrineInstanceConstructor::initTypeData(const JsonNode & config)
parameters = config;
}
CGObjectInstance * ShrineInstanceConstructor::create(std::shared_ptr<const ObjectTemplate> tmpl) const
void ShrineInstanceConstructor::randomizeObject(CGShrine * shrine, CRandomGenerator & rng) const
{
CGShrine * shrine = new CGShrine;
preInitObject(shrine);
if(tmpl)
shrine->appearance = tmpl;
return shrine;
}
void ShrineInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
{
CGShrine * shrine = dynamic_cast<CGShrine*>(object);
if (!shrine)
throw std::runtime_error("Unexpected object instance in ShrineInstanceConstructor!");
auto visitTextParameter = parameters["visitText"];
if (visitTextParameter.isNumber())
@ -57,9 +39,4 @@ void ShrineInstanceConstructor::configureObject(CGObjectInstance * object, CRand
}
}
std::unique_ptr<IObjectInfo> ShrineInstanceConstructor::getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const
{
return nullptr;
}
VCMI_LIB_NAMESPACE_END

View File

@ -9,19 +9,19 @@
*/
#pragma once
#include "AObjectTypeHandler.h"
#include "CDefaultObjectTypeHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
class ShrineInstanceConstructor final : public AObjectTypeHandler
class CGShrine;
class ShrineInstanceConstructor final : public CDefaultObjectTypeHandler<CGShrine>
{
JsonNode parameters;
protected:
void initTypeData(const JsonNode & config) override;
CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override;
void randomizeObject(CGShrine * object, CRandomGenerator & rng) const override;
public:
template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -1839,8 +1839,15 @@ void CGMagi::onHeroVisit(const CGHeroInstance * h) const
{
h->showInfoDialog(48);
}
}
CGBoat::CGBoat()
{
hero = nullptr;
direction = 4;
layer = EPathfindingLayer::EEPathfindingLayer::SAIL;
}
void CGBoat::initObj(CRandomGenerator & rand)
{
hero = nullptr;

View File

@ -432,15 +432,10 @@ public:
std::string overlayAnimation; //waves animations
std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
CGBoat();
void initObj(CRandomGenerator & rand) override;
static int3 translatePos(const int3 &pos, bool reverse = false);
CGBoat()
{
hero = nullptr;
direction = 4;
layer = EPathfindingLayer::EEPathfindingLayer::SAIL;
}
template <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<CGObjectInstance&>(*this);

View File

@ -23,6 +23,7 @@
#include "../../mapObjectConstructors/AObjectTypeHandler.h"
#include "../../mapObjectConstructors/CObjectClassesHandler.h"
#include "../../mapObjectConstructors/CommonConstructors.h"
#include "../../mapObjects/CGHeroInstance.h"
#include "../../mapObjects/CGPandoraBox.h"
#include "../../CCreatureHandler.h"
#include "../../spells/CSpellHandler.h" //for choosing random spells