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;
 }
 
+CRandomGenerator & CCallbackBase::getRandomGenerator() const
+{
+	return rand;
+}
+
 boost::optional<PlayerColor> CCallbackBase::getPlayerID() const
 {
 	return player;
@@ -1106,7 +1111,7 @@ std::pair<ui32, ui32> 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);

+ 3 - 0
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<PlayerColor> player; // not set gives access to all information, otherwise callback provides only information "visible" for player
 
 	CCallbackBase(CGameState *GS, boost::optional<PlayerColor> Player)
@@ -63,6 +65,7 @@ protected:
 public:
 	boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything
 	boost::optional<PlayerColor> getPlayerID() const;
+	CRandomGenerator & getRandomGenerator() const;
 
 	friend class CBattleInfoEssentials;
 };

+ 1 - 1
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;

+ 0 - 5
lib/IGameCallback.cpp

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

+ 0 - 2
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;

+ 1 - 1
server/CGameHandler.h

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