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

Replaced remaining placeholder code with callbacks

This commit is contained in:
Ivan Savenko 2024-01-09 19:08:22 +02:00
parent e67e4430ba
commit 60ffb81b33
14 changed files with 55 additions and 52 deletions

View File

@ -141,7 +141,7 @@ uint64_t getCreatureBankArmyReward(const CGObjectInstance * target, const CGHero
{
auto objectInfo = target->getObjectHandler()->getObjectInfo(target->appearance);
CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
auto creatures = bankInfo->getPossibleCreaturesReward();
auto creatures = bankInfo->getPossibleCreaturesReward(target->cb);
uint64_t result = 0;
const auto& slots = hero->Slots();

View File

@ -34,10 +34,10 @@ void CBankInstanceConstructor::initTypeData(const JsonNode & input)
coastVisitable = input["coastVisitable"].Bool();
}
BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRandomGenerator & rng) const
BankConfig CBankInstanceConstructor::generateConfig(IGameCallback * cb, const JsonNode & level, CRandomGenerator & rng) const
{
BankConfig bc;
JsonRandom randomizer(nullptr);
JsonRandom randomizer(cb);
JsonRandom::Variables emptyVariables;
bc.chance = static_cast<ui32>(level["chance"].Float());
@ -71,7 +71,7 @@ void CBankInstanceConstructor::randomizeObject(CBank * bank, CRandomGenerator &
cumulativeChance += static_cast<int>(node["chance"].Float());
if(selectedChance < cumulativeChance)
{
bank->setConfig(generateConfig(node, rng));
bank->setConfig(generateConfig(bank->cb, node, rng));
break;
}
}
@ -125,10 +125,10 @@ std::vector<PossibleReward<TResources>> CBankInfo::getPossibleResourcesReward()
return result;
}
std::vector<PossibleReward<CStackBasicDescriptor>> CBankInfo::getPossibleCreaturesReward() const
std::vector<PossibleReward<CStackBasicDescriptor>> CBankInfo::getPossibleCreaturesReward(IGameCallback * cb) const
{
JsonRandom::Variables emptyVariables;
JsonRandom randomizer(nullptr);
JsonRandom randomizer(cb);
std::vector<PossibleReward<CStackBasicDescriptor>> aproximateReward;
for(const JsonNode & configEntry : config)

View File

@ -57,7 +57,7 @@ public:
TPossibleGuards getPossibleGuards(IGameCallback * cb) const;
std::vector<PossibleReward<TResources>> getPossibleResourcesReward() const;
std::vector<PossibleReward<CStackBasicDescriptor>> getPossibleCreaturesReward() const;
std::vector<PossibleReward<CStackBasicDescriptor>> getPossibleCreaturesReward(IGameCallback * cb) const;
bool givesResources() const override;
bool givesArtifacts() const override;
@ -67,7 +67,7 @@ public:
class CBankInstanceConstructor : public CDefaultObjectTypeHandler<CBank>
{
BankConfig generateConfig(const JsonNode & conf, CRandomGenerator & rng) const;
BankConfig generateConfig(IGameCallback * cb, const JsonNode & conf, CRandomGenerator & rng) const;
JsonVector levels;

View File

@ -44,7 +44,7 @@ void CRewardableConstructor::configureObject(CGObjectInstance * object, CRandomG
{
if(auto * rewardableObject = dynamic_cast<CRewardableObject*>(object))
{
objectInfo.configureObject(rewardableObject->configuration, rng);
objectInfo.configureObject(rewardableObject->configuration, rng, object->cb);
for(auto & rewardInfo : rewardableObject->configuration.info)
{
for (auto & bonus : rewardInfo.reward.bonuses)

View File

@ -322,7 +322,7 @@ void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
auto building = town->town->buildings.at(bID);
building->rewardableObjectInfo.configureObject(configuration, rand);
building->rewardableObjectInfo.configureObject(configuration, rand, cb);
for(auto & rewardInfo : configuration.info)
{
for (auto & bonus : rewardInfo.reward.bonuses)

View File

@ -176,10 +176,7 @@ static JsonNode loadPatches(const std::string & path)
std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName)
{
static JsonNode node;
if (node.isNull())
node = loadPatches("config/mapOverrides.json");
static const JsonNode node = loadPatches("config/mapOverrides.json");
boost::to_lower(scenarioName);
logGlobal->debug("Request to patch map %s", scenarioName);

View File

@ -66,7 +66,7 @@ bool ObstacleProxy::isProhibited(const rmg::Area& objArea) const
return false;
};
int ObstacleProxy::getWeightedObjects(const int3 & tile, CRandomGenerator & rand, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects)
int ObstacleProxy::getWeightedObjects(const int3 & tile, CRandomGenerator & rand, IGameCallback * cb, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects)
{
int maxWeight = std::numeric_limits<int>::min();
for(auto & possibleObstacle : possibleObstacles)
@ -147,7 +147,7 @@ int ObstacleProxy::getWeightedObjects(const int3 & tile, CRandomGenerator & rand
return maxWeight;
}
std::set<CGObjectInstance*> ObstacleProxy::createObstacles(CRandomGenerator & rand)
std::set<CGObjectInstance*> ObstacleProxy::createObstacles(CRandomGenerator & rand, IGameCallback * cb)
{
//reverse order, since obstacles begin in bottom-right corner, while the map coordinates begin in top-left
auto blockedTiles = blockedArea.getTilesVector();
@ -160,7 +160,7 @@ std::set<CGObjectInstance*> ObstacleProxy::createObstacles(CRandomGenerator & ra
std::list<rmg::Object> allObjects;
std::vector<std::pair<rmg::Object*, int3>> weightedObjects;
int maxWeight = getWeightedObjects(tile, rand, allObjects, weightedObjects);
int maxWeight = getWeightedObjects(tile, rand, cb, allObjects, weightedObjects);
if(weightedObjects.empty())
{
@ -222,7 +222,7 @@ bool EditorObstaclePlacer::isInTheMap(const int3& tile)
std::set<CGObjectInstance*> EditorObstaclePlacer::placeObstacles(CRandomGenerator & rand)
{
auto obstacles = createObstacles(rand);
auto obstacles = createObstacles(rand, map->cb);
finalInsertion(map->getEditManager(), obstacles);
return obstacles;
}

View File

@ -19,6 +19,7 @@ class CMapEditManager;
class CGObjectInstance;
class ObjectTemplate;
class CRandomGenerator;
class IGameCallback;
class DLL_LINKAGE ObstacleProxy
{
@ -41,7 +42,7 @@ public:
virtual void placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances);
virtual std::set<CGObjectInstance*> createObstacles(CRandomGenerator & rand);
virtual std::set<CGObjectInstance*> createObstacles(CRandomGenerator & rand, IGameCallback * cb);
virtual bool isInTheMap(const int3& tile) = 0;
@ -50,7 +51,7 @@ public:
virtual void postProcess(const rmg::Object& object) {};
protected:
int getWeightedObjects(const int3& tile, CRandomGenerator& rand, std::list<rmg::Object>& allObjects, std::vector<std::pair<rmg::Object*, int3>>& weightedObjects);
int getWeightedObjects(const int3& tile, CRandomGenerator& rand, IGameCallback * cb, std::list<rmg::Object>& allObjects, std::vector<std::pair<rmg::Object*, int3>>& weightedObjects);
rmg::Area blockedArea;

View File

@ -105,14 +105,14 @@ void Rewardable::Info::init(const JsonNode & objectConfig, const std::string & o
loadString(parameters["onEmptyMessage"], TextIdentifier(objectName, "onEmpty"));
}
Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, const JsonNode & source) const
{
Rewardable::LimitersList result;
for (const auto & input : source.Vector())
{
auto newLimiter = std::make_shared<Rewardable::Limiter>();
configureLimiter(object, rng, *newLimiter, input);
configureLimiter(object, rng, cb, *newLimiter, input);
result.push_back(newLimiter);
}
@ -120,10 +120,10 @@ Rewardable::LimitersList Rewardable::Info::configureSublimiters(Rewardable::Conf
return result;
}
void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const
void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, Rewardable::Limiter & limiter, const JsonNode & source) const
{
auto const & variables = object.variables.values;
JsonRandom randomizer(nullptr);
JsonRandom randomizer(cb);
limiter.dayOfWeek = randomizer.loadValue(source["dayOfWeek"], rng, variables);
limiter.daysPassed = randomizer.loadValue(source["daysPassed"], rng, variables);
@ -147,15 +147,15 @@ void Rewardable::Info::configureLimiter(Rewardable::Configuration & object, CRan
limiter.heroes = randomizer.loadHeroes(source["heroes"], rng);
limiter.heroClasses = randomizer.loadHeroClasses(source["heroClasses"], rng);
limiter.allOf = configureSublimiters(object, rng, source["allOf"] );
limiter.anyOf = configureSublimiters(object, rng, source["anyOf"] );
limiter.noneOf = configureSublimiters(object, rng, source["noneOf"] );
limiter.allOf = configureSublimiters(object, rng, cb, source["allOf"] );
limiter.anyOf = configureSublimiters(object, rng, cb, source["anyOf"] );
limiter.noneOf = configureSublimiters(object, rng, cb, source["noneOf"] );
}
void Rewardable::Info::configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Reward & reward, const JsonNode & source) const
void Rewardable::Info::configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, Rewardable::Reward & reward, const JsonNode & source) const
{
auto const & variables = object.variables.values;
JsonRandom randomizer(nullptr);
JsonRandom randomizer(cb);
reward.resources = randomizer.loadResources(source["resources"], rng, variables);
@ -216,9 +216,9 @@ void Rewardable::Info::configureResetInfo(Rewardable::Configuration & object, CR
resetParameters.rewards = source["rewards"].Bool();
}
void Rewardable::Info::configureVariables(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const
void Rewardable::Info::configureVariables(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, const JsonNode & source) const
{
JsonRandom randomizer(nullptr);
JsonRandom randomizer(cb);
for(const auto & category : source.Struct())
{
@ -277,6 +277,7 @@ void Rewardable::Info::replaceTextPlaceholders(MetaString & target, const Variab
void Rewardable::Info::configureRewards(
Rewardable::Configuration & object,
CRandomGenerator & rng,
IGameCallback * cb,
const JsonNode & source,
Rewardable::EEventType event,
const std::string & modeName) const
@ -319,8 +320,8 @@ void Rewardable::Info::configureRewards(
}
Rewardable::VisitInfo info;
configureLimiter(object, rng, info.limiter, reward["limiter"]);
configureReward(object, rng, info.reward, reward);
configureLimiter(object, rng, cb, info.limiter, reward["limiter"]);
configureReward(object, rng, cb, info.reward, reward);
info.visitType = event;
info.message = loadMessage(reward["message"], TextIdentifier(objectTextID, modeName, i));
@ -333,15 +334,15 @@ void Rewardable::Info::configureRewards(
}
}
void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const
void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb) const
{
object.info.clear();
configureVariables(object, rng, parameters["variables"]);
configureVariables(object, rng, cb, parameters["variables"]);
configureRewards(object, rng, parameters["rewards"], Rewardable::EEventType::EVENT_FIRST_VISIT, "rewards");
configureRewards(object, rng, parameters["onVisited"], Rewardable::EEventType::EVENT_ALREADY_VISITED, "onVisited");
configureRewards(object, rng, parameters["onEmpty"], Rewardable::EEventType::EVENT_NOT_AVAILABLE, "onEmpty");
configureRewards(object, rng, cb, parameters["rewards"], Rewardable::EEventType::EVENT_FIRST_VISIT, "rewards");
configureRewards(object, rng, cb, parameters["onVisited"], Rewardable::EEventType::EVENT_ALREADY_VISITED, "onVisited");
configureRewards(object, rng, cb, parameters["onEmpty"], Rewardable::EEventType::EVENT_NOT_AVAILABLE, "onEmpty");
object.onSelect = loadMessage(parameters["onSelectMessage"], TextIdentifier(objectTextID, "onSelect"));
object.description = loadMessage(parameters["description"], TextIdentifier(objectTextID, "description"));
@ -405,7 +406,7 @@ void Rewardable::Info::configureObject(Rewardable::Configuration & object, CRand
}
if (object.visitMode == Rewardable::VISIT_LIMITER)
configureLimiter(object, rng, object.visitLimiter, parameters["visitLimiter"]);
configureLimiter(object, rng, cb, object.visitLimiter, parameters["visitLimiter"]);
}

View File

@ -17,6 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN
class CRandomGenerator;
class MetaString;
class IGameCallback;
namespace Rewardable
{
@ -38,13 +39,13 @@ class DLL_LINKAGE Info : public IObjectInfo
void replaceTextPlaceholders(MetaString & target, const Variables & variables) const;
void replaceTextPlaceholders(MetaString & target, const Variables & variables, const VisitInfo & info) const;
void configureVariables(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const;
void configureRewards(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source, Rewardable::EEventType mode, const std::string & textPrefix) const;
void configureVariables(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, const JsonNode & source) const;
void configureRewards(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, const JsonNode & source, Rewardable::EEventType mode, const std::string & textPrefix) const;
void configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Limiter & limiter, const JsonNode & source) const;
Rewardable::LimitersList configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, const JsonNode & source) const;
void configureLimiter(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, Rewardable::Limiter & limiter, const JsonNode & source) const;
Rewardable::LimitersList configureSublimiters(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, const JsonNode & source) const;
void configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::Reward & info, const JsonNode & source) const;
void configureReward(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb, Rewardable::Reward & info, const JsonNode & source) const;
void configureResetInfo(Rewardable::Configuration & object, CRandomGenerator & rng, Rewardable::ResetInfo & info, const JsonNode & source) const;
public:
const JsonNode & getParameters() const;
@ -64,7 +65,7 @@ public:
bool givesBonuses() const override;
void configureObject(Rewardable::Configuration & object, CRandomGenerator & rng) const;
void configureObject(Rewardable::Configuration & object, CRandomGenerator & rng, IGameCallback * cb) const;
void init(const JsonNode & objectConfig, const std::string & objectTextID);

View File

@ -79,9 +79,9 @@ void ObjectDistributor::distributeLimitedObjects()
RandomGeneratorUtil::randomShuffle(matchingZones, zone.getRand());
for (auto& zone : matchingZones)
{
oi.generateObject = [this, primaryID, secondaryID]() -> CGObjectInstance *
oi.generateObject = [cb=map.mapInstance->cb, primaryID, secondaryID]() -> CGObjectInstance *
{
return VLC->objtypeh->getHandlerFor(primaryID, secondaryID)->create(map.mapInstance->cb, nullptr);
return VLC->objtypeh->getHandlerFor(primaryID, secondaryID)->create(cb, nullptr);
};
oi.value = rmgInfo.value;

View File

@ -87,7 +87,7 @@ void ObstaclePlacer::process()
prohibitedArea.unite(zone.areaPossible());
}
auto objs = createObstacles(zone.getRand());
auto objs = createObstacles(zone.getRand(), map.mapInstance->cb);
mapProxy->insertObjects(objs);
}

View File

@ -114,7 +114,7 @@ class DLL_LINKAGE BinaryDeserializer : public CLoaderBase
class IPointerLoader
{
public:
virtual void * loadPtr(CLoaderBase &ar, ui32 pid) const =0; //data is pointer to the ACTUAL POINTER
virtual void * loadPtr(CLoaderBase &ar, IGameCallback * cb, ui32 pid) const =0; //data is pointer to the ACTUAL POINTER
virtual ~IPointerLoader() = default;
template<typename Type> static IPointerLoader *getApplier(const Type * t = nullptr)
@ -127,12 +127,12 @@ class DLL_LINKAGE BinaryDeserializer : public CLoaderBase
class CPointerLoader : public IPointerLoader
{
public:
void * loadPtr(CLoaderBase &ar, ui32 pid) const override //data is pointer to the ACTUAL POINTER
void * loadPtr(CLoaderBase &ar, IGameCallback * cb, ui32 pid) const override //data is pointer to the ACTUAL POINTER
{
auto & s = static_cast<BinaryDeserializer &>(ar);
//create new object under pointer
Type * ptr = ClassObjectCreator<Type>::invoke(nullptr); //does new npT or throws for abstract classes
Type * ptr = ClassObjectCreator<Type>::invoke(cb); //does new npT or throws for abstract classes
s.ptrAllocated(ptr, pid);
assert(s.fileVersion != 0);
@ -152,6 +152,7 @@ public:
std::map<ui32, void*> loadedPointers;
std::map<const void*, std::shared_ptr<void>> loadedSharedPointers;
IGameCallback * cb;
bool smartPointerSerialization;
bool saving;
@ -305,7 +306,7 @@ public:
data = nullptr;
return;
}
data = static_cast<T>(app->loadPtr(*this, pid));
data = static_cast<T>(app->loadPtr(*this, cb, pid));
}
}

View File

@ -11,6 +11,7 @@
#include "Connection.h"
#include "../networkPacks/NetPacksBase.h"
#include "../gameState/CGameState.h"
#include <boost/asio.hpp>
@ -342,6 +343,7 @@ void CConnection::enterGameplayConnectionMode(CGameState * gs)
enableStackSendingByID();
disableSmartPointerSerialization();
addStdVecItems(gs);
iser.cb = gs->callback;
}
void CConnection::disableSmartVectorMemberSerialization()