Browse Source

CCallbackBase: add CRandomGenerator to all callbacks

This is easiest way to make server RNG available to CBattleInfoCallback.

Now server, client and gamestate have own RNG instance.
Only server and gamestate RNGs are serialized.
Arseniy Shestakov 9 years ago
parent
commit
c8bcb14d34
6 changed files with 13 additions and 12 deletions
  1. 8 3
      lib/CBattleCallback.cpp
  2. 3 0
      lib/CBattleCallback.h
  3. 1 1
      lib/Connection.h
  4. 0 5
      lib/IGameCallback.cpp
  5. 0 2
      lib/IGameCallback.h
  6. 1 1
      server/CGameHandler.h

+ 8 - 3
lib/CBattleCallback.cpp

@@ -113,6 +113,11 @@ void CCallbackBase::setBattle(const BattleInfo *B)
 	battle = B;
 	battle = B;
 }
 }
 
 
+CRandomGenerator & CCallbackBase::getRandomGenerator() const
+{
+	return rand;
+}
+
 boost::optional<PlayerColor> CCallbackBase::getPlayerID() const
 boost::optional<PlayerColor> CCallbackBase::getPlayerID() const
 {
 {
 	return player;
 	return player;
@@ -1106,7 +1111,7 @@ std::pair<ui32, ui32> CBattleInfoCallback::battleEstimateDamage(const BattleAtta
 			{
 			{
 				BattleStackAttacked bsa;
 				BattleStackAttacked bsa;
 				bsa.damageAmount = ret.*pairElems[i];
 				bsa.damageAmount = ret.*pairElems[i];
-				bai.defender->prepareAttacked(bsa, gs->getRandomGenerator(), bai.defenderCount);
+				bai.defender->prepareAttacked(bsa, getRandomGenerator(), bai.defenderCount);
 
 
 				auto retaliationAttack = bai.reverse();
 				auto retaliationAttack = bai.reverse();
 				retaliationAttack.attackerCount = bsa.newAmount;
 				retaliationAttack.attackerCount = bsa.newAmount;
@@ -2045,7 +2050,7 @@ SpellID CBattleInfoCallback::getRandomBeneficialSpell(const CStack * subject) co
 
 
 	if(!beneficialSpells.empty())
 	if(!beneficialSpells.empty())
 	{
 	{
-		return *RandomGeneratorUtil::nextItem(beneficialSpells, gs->getRandomGenerator());
+		return *RandomGeneratorUtil::nextItem(beneficialSpells, getRandomGenerator());
 	}
 	}
 	else
 	else
 	{
 	{
@@ -2065,7 +2070,7 @@ SpellID CBattleInfoCallback::getRandomCastedSpell(const CStack * caster) const
 	{
 	{
 		totalWeight += std::max(b->additionalInfo, 1); //minimal chance to cast is 1
 		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)
 	for(Bonus * b : *bl)
 	{
 	{
 		randomPos -= std::max(b->additionalInfo, 1);
 		randomPos -= std::max(b->additionalInfo, 1);

+ 3 - 0
lib/CBattleCallback.h

@@ -1,5 +1,6 @@
 #pragma once
 #pragma once
 #include "BattleHex.h"
 #include "BattleHex.h"
+#include "CRandomGenerator.h"
 
 
 /*
 /*
  * CBattleCallback.h, part of VCMI engine
  * CBattleCallback.h, part of VCMI engine
@@ -48,6 +49,7 @@ class DLL_LINKAGE CCallbackBase
 
 
 protected:
 protected:
 	CGameState *gs;
 	CGameState *gs;
+	mutable CRandomGenerator rand;
 	boost::optional<PlayerColor> player; // not set gives access to all information, otherwise callback provides only information "visible" for player
 	boost::optional<PlayerColor> player; // not set gives access to all information, otherwise callback provides only information "visible" for player
 
 
 	CCallbackBase(CGameState *GS, boost::optional<PlayerColor> Player)
 	CCallbackBase(CGameState *GS, boost::optional<PlayerColor> Player)
@@ -63,6 +65,7 @@ protected:
 public:
 public:
 	boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything
 	boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything
 	boost::optional<PlayerColor> getPlayerID() const;
 	boost::optional<PlayerColor> getPlayerID() const;
+	CRandomGenerator & getRandomGenerator() const;
 
 
 	friend class CBattleInfoEssentials;
 	friend class CBattleInfoEssentials;
 };
 };

+ 1 - 1
lib/Connection.h

@@ -27,7 +27,7 @@
 #include "mapping/CCampaignHandler.h" //for CCampaignState
 #include "mapping/CCampaignHandler.h" //for CCampaignState
 #include "rmg/CMapGenerator.h" // for CMapGenOptions
 #include "rmg/CMapGenerator.h" // for CMapGenOptions
 
 
-const ui32 version = 760;
+const ui32 version = 761;
 const ui32 minSupportedVersion = 753;
 const ui32 minSupportedVersion = 753;
 
 
 class CISer;
 class CISer;

+ 0 - 5
lib/IGameCallback.cpp

@@ -249,11 +249,6 @@ const CGCreature * IGameCallback::putNewMonster(CreatureID creID, int count, int
 	return dynamic_cast<const CGCreature*>(m);
 	return dynamic_cast<const CGCreature*>(m);
 }
 }
 
 
-CRandomGenerator & IGameCallback::getRandomGenerator()
-{
-	return rand;
-}
-
 bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
 bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
 {
 {
 	//only server knows
 	//only server knows

+ 0 - 2
lib/IGameCallback.h

@@ -124,8 +124,6 @@ public:
 
 
 	//get info
 	//get info
 	virtual bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero);
 	virtual bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero);
-	CRandomGenerator rand;
-	CRandomGenerator & getRandomGenerator();
 
 
 	friend struct CPack;
 	friend struct CPack;
 	friend struct CPackForClient;
 	friend struct CPackForClient;

+ 1 - 1
server/CGameHandler.h

@@ -247,7 +247,7 @@ public:
 	template <typename Handler> void serialize(Handler &h, const int version)
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
 	{
 		h & QID & states & finishingBattle;
 		h & QID & states & finishingBattle;
-		if(version >= 760)
+		if(version >= 761)
 		{
 		{
 			h & rand;
 			h & rand;
 		}
 		}