1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +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;
}
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);

View File

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

View File

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

View File

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

View File

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

View File

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