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

GameState now uses random generator from server. Blocked access to rmg

on client
This commit is contained in:
Ivan Savenko 2024-06-01 16:09:14 +00:00
parent 63bcf7d83c
commit 0d66ddbeec
10 changed files with 36 additions and 13 deletions

View File

@ -556,6 +556,13 @@ void CClient::invalidatePaths()
pathCache.clear(); pathCache.clear();
} }
vstd::RNG & CClient::getRandomGenerator()
{
// Client should use CRandomGenerator::getDefault() for UI logic
// Gamestate should never call this method on client!
throw std::runtime_error("Illegal access to random number generator from client code!");
}
std::shared_ptr<const CPathsInfo> CClient::getPathsInfo(const CGHeroInstance * h) std::shared_ptr<const CPathsInfo> CClient::getPathsInfo(const CGHeroInstance * h)
{ {
assert(h); assert(h);

View File

@ -218,6 +218,8 @@ public:
void showInfoDialog(InfoWindow * iw) override {}; void showInfoDialog(InfoWindow * iw) override {};
void removeGUI() const; void removeGUI() const;
vstd::RNG & getRandomGenerator() override;
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
scripting::Pool * getGlobalContextPool() const override; scripting::Pool * getGlobalContextPool() const override;
#endif #endif

View File

@ -135,6 +135,9 @@ public:
virtual void changeFogOfWar(std::unordered_set<int3> &tiles, PlayerColor player, ETileVisibility mode) = 0; virtual void changeFogOfWar(std::unordered_set<int3> &tiles, PlayerColor player, ETileVisibility mode) = 0;
virtual void castSpell(const spells::Caster * caster, SpellID spellID, const int3 &pos) = 0; virtual void castSpell(const spells::Caster * caster, SpellID spellID, const int3 &pos) = 0;
virtual vstd::RNG & getRandomGenerator() = 0;
}; };
class DLL_LINKAGE CNonConstInfoCallback : public CPrivilegedInfoCallback class DLL_LINKAGE CNonConstInfoCallback : public CPrivilegedInfoCallback

View File

@ -185,8 +185,6 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog
{ {
assert(services); assert(services);
assert(callback); assert(callback);
logGlobal->info("\tUsing random seed: %d", si->seedToBeUsed);
rand = std::make_unique<CRandomGenerator>(si->seedToBeUsed);
scenarioOps = CMemorySerializer::deepCopy(*si).release(); scenarioOps = CMemorySerializer::deepCopy(*si).release();
initialOpts = CMemorySerializer::deepCopy(*si).release(); initialOpts = CMemorySerializer::deepCopy(*si).release();
si = nullptr; si = nullptr;
@ -1966,7 +1964,7 @@ TeamState::TeamState()
vstd::RNG & CGameState::getRandomGenerator() vstd::RNG & CGameState::getRandomGenerator()
{ {
return *rand; return callback->getRandomGenerator();
} }
ArtifactID CGameState::pickRandomArtifact(vstd::RNG & rand, int flags, std::function<bool(ArtifactID)> accepts) ArtifactID CGameState::pickRandomArtifact(vstd::RNG & rand, int flags, std::function<bool(ArtifactID)> accepts)

View File

@ -9,10 +9,10 @@
*/ */
#pragma once #pragma once
#include "bonuses/CBonusSystemNode.h" #include "../bonuses/CBonusSystemNode.h"
#include "IGameCallback.h" #include "../IGameCallback.h"
#include "LoadProgress.h" #include "../LoadProgress.h"
#include "ConstTransitivePtr.h" #include "../ConstTransitivePtr.h"
namespace boost namespace boost
{ {
@ -186,7 +186,11 @@ public:
h & teams; h & teams;
h & heroesPool; h & heroesPool;
h & globalEffects; h & globalEffects;
h & rand; if (h.version < Handler::Version::REMOVE_LIB_RNG)
{
std::string oldStateOfRNG;
h & oldStateOfRNG;
}
h & rumor; h & rumor;
h & campaign; h & campaign;
h & allocatedArtifacts; h & allocatedArtifacts;
@ -234,7 +238,6 @@ private:
// ---- data ----- // ---- data -----
std::shared_ptr<CApplier<CBaseForGSApply>> applier; std::shared_ptr<CApplier<CBaseForGSApply>> applier;
std::unique_ptr<CRandomGenerator> rand;
Services * services; Services * services;
/// Pointer to campaign state manager. Nullptr for single scenarios /// Pointer to campaign state manager. Nullptr for single scenarios

View File

@ -56,6 +56,7 @@ enum class ESerializationVersion : int32_t
REMOVE_FOG_OF_WAR_POINTER, // 846 - fog of war is serialized as reference instead of pointer REMOVE_FOG_OF_WAR_POINTER, // 846 - fog of war is serialized as reference instead of pointer
SIMPLE_TEXT_CONTAINER_SERIALIZATION, // 847 - text container is serialized using common routine instead of custom approach SIMPLE_TEXT_CONTAINER_SERIALIZATION, // 847 - text container is serialized using common routine instead of custom approach
MAP_FORMAT_ADDITIONAL_INFOS, // 848 - serialize new infos in map format MAP_FORMAT_ADDITIONAL_INFOS, // 848 - serialize new infos in map format
REMOVE_LIB_RNG, // 849 - removed random number generators from library classes
CURRENT = MAP_FORMAT_ADDITIONAL_INFOS CURRENT = REMOVE_LIB_RNG
}; };

View File

@ -549,9 +549,11 @@ void CGameHandler::reinitScripting()
void CGameHandler::init(StartInfo *si, Load::ProgressAccumulator & progressTracking) void CGameHandler::init(StartInfo *si, Load::ProgressAccumulator & progressTracking)
{ {
if (si->seedToBeUsed == 0) if (si->seedToBeUsed == 0)
{
si->seedToBeUsed = CRandomGenerator::getDefault().nextInt(); si->seedToBeUsed = CRandomGenerator::getDefault().nextInt();
}
logGlobal->info("Using random seed: %d", si->seedToBeUsed);
randomNumberGenerator = std::make_unique<CRandomGenerator>(si->seedToBeUsed);
CMapService mapService; CMapService mapService;
gs = new CGameState(); gs = new CGameState();
gs->preInit(VLC, this); gs->preInit(VLC, this);

View File

@ -277,7 +277,7 @@ public:
void checkVictoryLossConditions(const std::set<PlayerColor> & playerColors); void checkVictoryLossConditions(const std::set<PlayerColor> & playerColors);
void checkVictoryLossConditionsForAll(); void checkVictoryLossConditionsForAll();
vstd::RNG & getRandomGenerator(); vstd::RNG & getRandomGenerator() override;
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
scripting::Pool * getGlobalContextPool() const override; scripting::Pool * getGlobalContextPool() const override;

View File

@ -31,3 +31,8 @@ void GameCallbackMock::sendAndApply(CPackForClient * pack)
{ {
upperCallback->apply(pack); upperCallback->apply(pack);
} }
vstd::RNG & GameCallbackMock::getRandomGenerator()
{
throw std::runtime_error("Not implemented!");
}

View File

@ -93,6 +93,8 @@ public:
///useful callback methods ///useful callback methods
void sendAndApply(CPackForClient * pack) override; void sendAndApply(CPackForClient * pack) override;
vstd::RNG & getRandomGenerator() override;
#if SCRIPTING_ENABLED #if SCRIPTING_ENABLED
MOCK_CONST_METHOD0(getGlobalContextPool, scripting::Pool *()); MOCK_CONST_METHOD0(getGlobalContextPool, scripting::Pool *());
#endif #endif