diff --git a/lib/CBattleCallback.cpp b/lib/CBattleCallback.cpp index f6b885f4d..71061f22e 100644 --- a/lib/CBattleCallback.cpp +++ b/lib/CBattleCallback.cpp @@ -113,6 +113,11 @@ void CCallbackBase::setBattle(const BattleInfo *B) battle = B; } +CRandomGenerator & CCallbackBase::getRandomGenerator() const +{ + return rand; +} + boost::optional CCallbackBase::getPlayerID() const { return player; @@ -1106,7 +1111,7 @@ std::pair CBattleInfoCallback::battleEstimateDamage(const BattleAtta { BattleStackAttacked bsa; bsa.damageAmount = ret.*pairElems[i]; - bai.defender->prepareAttacked(bsa, gs->getRandomGenerator(), bai.defenderCount); + bai.defender->prepareAttacked(bsa, getRandomGenerator(), bai.defenderCount); auto retaliationAttack = bai.reverse(); retaliationAttack.attackerCount = bsa.newAmount; @@ -2045,7 +2050,7 @@ SpellID CBattleInfoCallback::getRandomBeneficialSpell(const CStack * subject) co if(!beneficialSpells.empty()) { - return *RandomGeneratorUtil::nextItem(beneficialSpells, gs->getRandomGenerator()); + return *RandomGeneratorUtil::nextItem(beneficialSpells, getRandomGenerator()); } else { @@ -2065,7 +2070,7 @@ SpellID CBattleInfoCallback::getRandomCastedSpell(const CStack * caster) const { totalWeight += std::max(b->additionalInfo, 1); //minimal chance to cast is 1 } - int randomPos = gs->getRandomGenerator().nextInt(totalWeight - 1); + int randomPos = getRandomGenerator().nextInt(totalWeight - 1); for(Bonus * b : *bl) { randomPos -= std::max(b->additionalInfo, 1); diff --git a/lib/CBattleCallback.h b/lib/CBattleCallback.h index fdf7be1df..17be53dd9 100644 --- a/lib/CBattleCallback.h +++ b/lib/CBattleCallback.h @@ -1,5 +1,6 @@ #pragma once #include "BattleHex.h" +#include "CRandomGenerator.h" /* * CBattleCallback.h, part of VCMI engine @@ -48,6 +49,7 @@ class DLL_LINKAGE CCallbackBase protected: CGameState *gs; + mutable CRandomGenerator rand; boost::optional player; // not set gives access to all information, otherwise callback provides only information "visible" for player CCallbackBase(CGameState *GS, boost::optional Player) @@ -63,6 +65,7 @@ protected: public: boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything boost::optional getPlayerID() const; + CRandomGenerator & getRandomGenerator() const; friend class CBattleInfoEssentials; }; diff --git a/lib/Connection.h b/lib/Connection.h index 307de34dd..95040a7a4 100644 --- a/lib/Connection.h +++ b/lib/Connection.h @@ -27,7 +27,7 @@ #include "mapping/CCampaignHandler.h" //for CCampaignState #include "rmg/CMapGenerator.h" // for CMapGenOptions -const ui32 version = 760; +const ui32 version = 761; const ui32 minSupportedVersion = 753; class CISer; diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp index 636d48a4c..bc0b57581 100644 --- a/lib/IGameCallback.cpp +++ b/lib/IGameCallback.cpp @@ -249,11 +249,6 @@ const CGCreature * IGameCallback::putNewMonster(CreatureID creID, int count, int return dynamic_cast(m); } -CRandomGenerator & IGameCallback::getRandomGenerator() -{ - return rand; -} - bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero) { //only server knows diff --git a/lib/IGameCallback.h b/lib/IGameCallback.h index 4c4fb152a..ed1115d1d 100644 --- a/lib/IGameCallback.h +++ b/lib/IGameCallback.h @@ -124,8 +124,6 @@ public: //get info virtual bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero); - CRandomGenerator rand; - CRandomGenerator & getRandomGenerator(); friend struct CPack; friend struct CPackForClient; diff --git a/server/CGameHandler.h b/server/CGameHandler.h index eaceff895..1eae7b97f 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -247,7 +247,7 @@ public: template void serialize(Handler &h, const int version) { h & QID & states & finishingBattle; - if(version >= 760) + if(version >= 761) { h & rand; }