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

Moved banks randomization to server-side with client netpack

This commit is contained in:
Ivan Savenko 2024-07-13 15:46:55 +00:00
parent b07408e984
commit 1c63fefe02
14 changed files with 68 additions and 14 deletions

View File

@ -25,6 +25,7 @@ class BinaryDeserializer;
class BinarySerializer; class BinarySerializer;
class BattleAction; class BattleAction;
class BattleInfo; class BattleInfo;
struct BankConfig;
template<typename T> class CApplier; template<typename T> class CApplier;
@ -214,6 +215,7 @@ public:
void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override {}; void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override {};
void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override {}; void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override {};
void setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration) override {};
void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override {}; void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override {};
void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override{}; void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override{};

View File

@ -27,6 +27,7 @@ struct BlockingDialog;
struct TeleportDialog; struct TeleportDialog;
struct StackLocation; struct StackLocation;
struct ArtifactLocation; struct ArtifactLocation;
struct BankConfig;
class CCreatureSet; class CCreatureSet;
class CStackBasicDescriptor; class CStackBasicDescriptor;
class CGCreature; class CGCreature;
@ -84,6 +85,7 @@ class DLL_LINKAGE IGameEventCallback
{ {
public: public:
virtual void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value = 0) = 0; virtual void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value = 0) = 0;
virtual void setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration) = 0;
virtual void setRewardableObjectConfiguration(ObjectInstanceID mapObjectID, const Rewardable::Configuration & configuration) = 0; virtual void setRewardableObjectConfiguration(ObjectInstanceID mapObjectID, const Rewardable::Configuration & configuration) = 0;
virtual void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) = 0; virtual void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) = 0;
virtual void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) = 0; virtual void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) = 0;

View File

@ -37,7 +37,7 @@ void CBankInstanceConstructor::initTypeData(const JsonNode & input)
regularUnitPlacement = input["regularUnitPlacement"].Bool(); regularUnitPlacement = input["regularUnitPlacement"].Bool();
} }
BankConfig CBankInstanceConstructor::generateConfig(IGameCallback * cb, const JsonNode & level, vstd::RNG & rng) const BankConfig CBankInstanceConstructor::generateLevelConfiguration(IGameCallback * cb, const JsonNode & level, vstd::RNG & rng) const
{ {
BankConfig bc; BankConfig bc;
JsonRandom randomizer(cb); JsonRandom randomizer(cb);
@ -60,7 +60,11 @@ void CBankInstanceConstructor::randomizeObject(CBank * bank, vstd::RNG & rng) co
bank->blockVisit = blockVisit; bank->blockVisit = blockVisit;
bank->coastVisitable = coastVisitable; bank->coastVisitable = coastVisitable;
bank->regularUnitPlacement = regularUnitPlacement; bank->regularUnitPlacement = regularUnitPlacement;
bank->setConfig(generateConfiguration(bank->cb, rng, bank->ID));
}
BankConfig CBankInstanceConstructor::generateConfiguration(IGameCallback * cb, vstd::RNG & rng, MapObjectID objectID) const
{
si32 totalChance = 0; si32 totalChance = 0;
for(const auto & node : levels) for(const auto & node : levels)
totalChance += static_cast<si32>(node["chance"].Float()); totalChance += static_cast<si32>(node["chance"].Float());
@ -74,11 +78,10 @@ void CBankInstanceConstructor::randomizeObject(CBank * bank, vstd::RNG & rng) co
{ {
cumulativeChance += static_cast<int>(node["chance"].Float()); cumulativeChance += static_cast<int>(node["chance"].Float());
if(selectedChance < cumulativeChance) if(selectedChance < cumulativeChance)
{ return generateLevelConfiguration(cb, node, rng);
bank->setConfig(generateConfig(bank->cb, node, rng));
break;
}
} }
throw std::runtime_error("Failed to select bank configuration");
} }
CBankInfo::CBankInfo(const JsonVector & Config) : CBankInfo::CBankInfo(const JsonVector & Config) :

View File

@ -69,7 +69,7 @@ public:
class CBankInstanceConstructor : public CDefaultObjectTypeHandler<CBank> class CBankInstanceConstructor : public CDefaultObjectTypeHandler<CBank>
{ {
BankConfig generateConfig(IGameCallback * cb, const JsonNode & conf, vstd::RNG & rng) const; BankConfig generateLevelConfiguration(IGameCallback * cb, const JsonNode & conf, vstd::RNG & rng) const;
JsonVector levels; JsonVector levels;
@ -92,6 +92,8 @@ public:
bool hasNameTextID() const override; bool hasNameTextID() const override;
std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override; std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override;
BankConfig generateConfiguration(IGameCallback * cb, vstd::RNG & rand, MapObjectID objectID) const;
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -97,6 +97,8 @@ void CBank::setConfig(const BankConfig & config)
for(const auto & stack : config.guards) for(const auto & stack : config.guards)
setCreature (SlotID(stacksCount()), stack.type->getId(), stack.count); setCreature (SlotID(stacksCount()), stack.type->getId(), stack.count);
daycounter = 1; //yes, 1 since "today" daycounter won't be incremented
} }
void CBank::setPropertyDer (ObjProperty what, ObjPropertyID identifier) void CBank::setPropertyDer (ObjProperty what, ObjPropertyID identifier)
@ -106,11 +108,6 @@ void CBank::setPropertyDer (ObjProperty what, ObjPropertyID identifier)
case ObjProperty::BANK_DAYCOUNTER: //daycounter case ObjProperty::BANK_DAYCOUNTER: //daycounter
daycounter+= identifier.getNum(); daycounter+= identifier.getNum();
break; break;
case ObjProperty::BANK_RESET:
// FIXME: Object reset must be done by separate netpack from server
initObj(cb->gameState()->getRandomGenerator());
daycounter = 1; //yes, 1 since "today" daycounter won't be incremented
break;
case ObjProperty::BANK_CLEAR: case ObjProperty::BANK_CLEAR:
bankConfig.reset(); bankConfig.reset();
break; break;
@ -124,7 +121,11 @@ void CBank::newTurn(vstd::RNG & rand) const
if (resetDuration != 0) if (resetDuration != 0)
{ {
if (daycounter >= resetDuration) if (daycounter >= resetDuration)
cb->setObjPropertyValue(id, ObjProperty::BANK_RESET); //daycounter 0 {
auto handler = std::dynamic_pointer_cast<CBankInstanceConstructor>(getObjectHandler());
auto config = handler->generateConfiguration(cb, rand, ID);
cb->setBankObjectConfiguration(id, config);
}
else else
cb->setObjPropertyValue(id, ObjProperty::BANK_DAYCOUNTER, 1); //daycounter++ cb->setObjPropertyValue(id, ObjProperty::BANK_DAYCOUNTER, 1); //daycounter++
} }

View File

@ -115,9 +115,10 @@ void CMapUndoManager::setUndoCallback(std::function<void(bool, bool)> functor)
CMapEditManager::CMapEditManager(CMap * map) CMapEditManager::CMapEditManager(CMap * map)
: map(map), terrainSel(map), objectSel(map) : map(map), terrainSel(map), objectSel(map)
{ {
} }
CMapEditManager::~CMapEditManager() = default;
CMap * CMapEditManager::getMap() CMap * CMapEditManager::getMap()
{ {
return map; return map;

View File

@ -68,6 +68,7 @@ class DLL_LINKAGE CMapEditManager : boost::noncopyable
{ {
public: public:
CMapEditManager(CMap * map); CMapEditManager(CMap * map);
~CMapEditManager();
CMap * getMap(); CMap * getMap();
/// Clears the terrain. The free level is filled with water and the underground level with rock. /// Clears the terrain. The free level is filled with water and the underground level with rock.

View File

@ -36,6 +36,7 @@ public:
virtual void visitGamePause(GamePause & pack) {} virtual void visitGamePause(GamePause & pack) {}
virtual void visitEntitiesChanged(EntitiesChanged & pack) {} virtual void visitEntitiesChanged(EntitiesChanged & pack) {}
virtual void visitSetRewardableConfiguration(SetRewardableConfiguration & pack) {} virtual void visitSetRewardableConfiguration(SetRewardableConfiguration & pack) {}
virtual void visitSetBankConfiguration(SetBankConfiguration & pack) {}
virtual void visitSetResources(SetResources & pack) {} virtual void visitSetResources(SetResources & pack) {}
virtual void visitSetPrimSkill(SetPrimSkill & pack) {} virtual void visitSetPrimSkill(SetPrimSkill & pack) {}
virtual void visitSetSecSkill(SetSecSkill & pack) {} virtual void visitSetSecSkill(SetSecSkill & pack) {}

View File

@ -33,6 +33,7 @@
#include "StartInfo.h" #include "StartInfo.h"
#include "CPlayerState.h" #include "CPlayerState.h"
#include "TerrainHandler.h" #include "TerrainHandler.h"
#include "mapObjects/CBank.h"
#include "mapObjects/CGCreature.h" #include "mapObjects/CGCreature.h"
#include "mapObjects/CGMarket.h" #include "mapObjects/CGMarket.h"
#include "mapObjects/CGTownInstance.h" #include "mapObjects/CGTownInstance.h"
@ -129,6 +130,11 @@ void SetRewardableConfiguration::visitTyped(ICPackVisitor & visitor)
visitor.visitSetRewardableConfiguration(*this); visitor.visitSetRewardableConfiguration(*this);
} }
void SetBankConfiguration::visitTyped(ICPackVisitor & visitor)
{
visitor.visitSetBankConfiguration(*this);
}
void SetResources::visitTyped(ICPackVisitor & visitor) void SetResources::visitTyped(ICPackVisitor & visitor)
{ {
visitor.visitSetResources(*this); visitor.visitSetResources(*this);
@ -2448,6 +2454,16 @@ void SetRewardableConfiguration::applyGs(CGameState * gs)
rewardablePtr->configuration = configuration; rewardablePtr->configuration = configuration;
} }
void SetBankConfiguration::applyGs(CGameState * gs)
{
auto * objectPtr = gs->getObjInstance(objectID);
auto * bankPtr = dynamic_cast<CBank *>(objectPtr);
assert(bankPtr);
bankPtr->setConfig(configuration);
}
const CArtifactInstance * ArtSlotInfo::getArt() const const CArtifactInstance * ArtSlotInfo::getArt() const
{ {
if(locked) if(locked)

View File

@ -43,7 +43,6 @@ enum class ObjProperty : int8_t
//creature-bank specific //creature-bank specific
BANK_DAYCOUNTER, BANK_DAYCOUNTER,
BANK_RESET,
BANK_CLEAR, BANK_CLEAR,
//object with reward //object with reward

View File

@ -12,6 +12,7 @@
#include "NetPacksBase.h" #include "NetPacksBase.h"
#include "../rewardable/Configuration.h" #include "../rewardable/Configuration.h"
#include "../mapObjectConstructors/CBankInstanceConstructor.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -32,4 +33,19 @@ struct DLL_LINKAGE SetRewardableConfiguration : public CPackForClient
} }
}; };
struct DLL_LINKAGE SetBankConfiguration : public CPackForClient
{
void applyGs(CGameState * gs);
void visitTyped(ICPackVisitor & visitor) override;
ObjectInstanceID objectID;
BankConfig configuration;
template <typename Handler> void serialize(Handler & h)
{
h & objectID;
h & configuration;
}
};
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -122,6 +122,7 @@ void registerTypesClientPacks(Serializer &s)
s.template registerType<CGarrisonOperationPack, BulkSmartRebalanceStacks>(); s.template registerType<CGarrisonOperationPack, BulkSmartRebalanceStacks>();
s.template registerType<SetRewardableConfiguration, CPackForClient>(); s.template registerType<SetRewardableConfiguration, CPackForClient>();
s.template registerType<SetBankConfiguration, CPackForClient>();
} }
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -4377,6 +4377,14 @@ void CGameHandler::setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, Ob
sendAndApply(&sob); sendAndApply(&sob);
} }
void CGameHandler::setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration)
{
SetBankConfiguration srb;
srb.objectID = objid;
srb.configuration = configuration;
sendAndApply(&srb);
}
void CGameHandler::setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) void CGameHandler::setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration)
{ {
SetRewardableConfiguration srb; SetRewardableConfiguration srb;

View File

@ -169,6 +169,7 @@ public:
bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero) override; bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero) override;
void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override; void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override;
void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override; void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override;
void setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration) override;
void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override; void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override;
void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override; void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override;
void showInfoDialog(InfoWindow * iw) override; void showInfoDialog(InfoWindow * iw) override;