1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

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.
This commit is contained in:
Arseniy Shestakov 2016-08-23 11:35:03 +03:00
parent 85f49bc968
commit c8bcb14d34
6 changed files with 13 additions and 12 deletions

View File

@ -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);

View File

@ -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;
}; };

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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;
} }