mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-15 01:24:45 +02:00
Added RandomGeneratorUtil::nextItemWeighted convenience method
This commit is contained in:
@ -22,6 +22,8 @@
|
|||||||
#include "../../lib/battle/BattleStateInfoForRetreat.h"
|
#include "../../lib/battle/BattleStateInfoForRetreat.h"
|
||||||
#include "../../lib/battle/CObstacleInstance.h"
|
#include "../../lib/battle/CObstacleInstance.h"
|
||||||
#include "../../lib/battle/BattleAction.h"
|
#include "../../lib/battle/BattleAction.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
|
|
||||||
|
|
||||||
// TODO: remove
|
// TODO: remove
|
||||||
// Eventually only IBattleInfoCallback and battle::Unit should be used,
|
// Eventually only IBattleInfoCallback and battle::Unit should be used,
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "DangerHitMapAnalyzer.h"
|
#include "DangerHitMapAnalyzer.h"
|
||||||
|
|
||||||
#include "../Engine/Nullkiller.h"
|
#include "../Engine/Nullkiller.h"
|
||||||
|
#include "../../../lib/CRandomGenerator.h"
|
||||||
|
|
||||||
namespace NKAI
|
namespace NKAI
|
||||||
{
|
{
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "../../lib/CCreatureHandler.h"
|
#include "../../lib/CCreatureHandler.h"
|
||||||
#include "../../lib/battle/BattleAction.h"
|
#include "../../lib/battle/BattleAction.h"
|
||||||
#include "../../lib/battle/BattleInfo.h"
|
#include "../../lib/battle/BattleInfo.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
|
|
||||||
CStupidAI::CStupidAI()
|
CStupidAI::CStupidAI()
|
||||||
: side(-1)
|
: side(-1)
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include "../../CCallback.h"
|
#include "../../CCallback.h"
|
||||||
#include "../../lib/CConfigHandler.h"
|
#include "../../lib/CConfigHandler.h"
|
||||||
#include "../../lib/CGeneralTextHandler.h"
|
#include "../../lib/CGeneralTextHandler.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
#include "../../lib/CStack.h"
|
#include "../../lib/CStack.h"
|
||||||
#include "../../lib/battle/BattleAction.h"
|
#include "../../lib/battle/BattleAction.h"
|
||||||
#include "../../lib/spells/CSpellHandler.h"
|
#include "../../lib/spells/CSpellHandler.h"
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
#include "../../lib/spells/ISpellMechanics.h"
|
#include "../../lib/spells/ISpellMechanics.h"
|
||||||
#include "../../lib/battle/BattleAction.h"
|
#include "../../lib/battle/BattleAction.h"
|
||||||
#include "../../lib/battle/BattleHex.h"
|
#include "../../lib/battle/BattleHex.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
#include "../../lib/CStack.h"
|
#include "../../lib/CStack.h"
|
||||||
#include "../../lib/CondSh.h"
|
#include "../../lib/CondSh.h"
|
||||||
#include "../../lib/TextOperations.h"
|
#include "../../lib/TextOperations.h"
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
#include "../CPlayerInterface.h"
|
#include "../CPlayerInterface.h"
|
||||||
#include "../CMusicHandler.h"
|
#include "../CMusicHandler.h"
|
||||||
#include "../CVideoHandler.h"
|
#include "../CVideoHandler.h"
|
||||||
#include "../CPlayerInterface.h"
|
|
||||||
#include "../CServerHandler.h"
|
#include "../CServerHandler.h"
|
||||||
#include "../gui/CGuiHandler.h"
|
#include "../gui/CGuiHandler.h"
|
||||||
#include "../gui/Shortcut.h"
|
#include "../gui/Shortcut.h"
|
||||||
@ -43,6 +42,7 @@
|
|||||||
|
|
||||||
#include "../../lib/CGeneralTextHandler.h"
|
#include "../../lib/CGeneralTextHandler.h"
|
||||||
#include "../../lib/CHeroHandler.h"
|
#include "../../lib/CHeroHandler.h"
|
||||||
|
#include "../../lib/CRandomGenerator.h"
|
||||||
#include "../../lib/CThreadHelper.h"
|
#include "../../lib/CThreadHelper.h"
|
||||||
#include "../../lib/filesystem/Filesystem.h"
|
#include "../../lib/filesystem/Filesystem.h"
|
||||||
#include "../../lib/mapping/CMapInfo.h"
|
#include "../../lib/mapping/CMapInfo.h"
|
||||||
|
@ -47,6 +47,25 @@ namespace RandomGeneratorUtil
|
|||||||
return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)());
|
return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Container>
|
||||||
|
size_t nextItemWeighted(Container & container, vstd::RNG & rand)
|
||||||
|
{
|
||||||
|
assert(!container.empty());
|
||||||
|
|
||||||
|
int64_t totalWeight = std::accumulate(container.begin(), container.end(), 0);
|
||||||
|
assert(totalWeight > 0);
|
||||||
|
|
||||||
|
int64_t roll = rand.getInt64Range(0, totalWeight - 1)();
|
||||||
|
|
||||||
|
for (size_t i = 0; i < container.size(); ++i)
|
||||||
|
{
|
||||||
|
roll -= container[i];
|
||||||
|
if(roll < 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return container.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void randomShuffle(std::vector<T> & container, vstd::RNG & rand)
|
void randomShuffle(std::vector<T> & container, vstd::RNG & rand)
|
||||||
{
|
{
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
#include "ResourceSet.h"
|
#include "ResourceSet.h"
|
||||||
#include "filesystem/Filesystem.h"
|
#include "filesystem/Filesystem.h"
|
||||||
#include "VCMI_Lib.h"
|
#include "VCMI_Lib.h"
|
||||||
|
#include "CRandomGenerator.h"
|
||||||
#include "CTownHandler.h"
|
#include "CTownHandler.h"
|
||||||
#include "GameSettings.h"
|
#include "GameSettings.h"
|
||||||
#include "constants/StringConstants.h"
|
#include "constants/StringConstants.h"
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
#include "GameConstants.h"
|
#include "GameConstants.h"
|
||||||
#include "JsonNode.h"
|
#include "JsonNode.h"
|
||||||
#include "IHandlerBase.h"
|
#include "IHandlerBase.h"
|
||||||
#include "CRandomGenerator.h"
|
|
||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include "filesystem/ResourcePath.h"
|
#include "filesystem/ResourcePath.h"
|
||||||
|
|
||||||
@ -29,6 +28,7 @@ class CLegacyConfigParser;
|
|||||||
class CCreatureHandler;
|
class CCreatureHandler;
|
||||||
class CCreature;
|
class CCreature;
|
||||||
class JsonSerializeFormat;
|
class JsonSerializeFormat;
|
||||||
|
class CRandomGenerator;
|
||||||
|
|
||||||
class DLL_LINKAGE CCreature : public Creature, public CBonusSystemNode
|
class DLL_LINKAGE CCreature : public Creature, public CBonusSystemNode
|
||||||
{
|
{
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "battle/BattleHex.h"
|
#include "battle/BattleHex.h"
|
||||||
#include "CCreatureHandler.h"
|
#include "CCreatureHandler.h"
|
||||||
#include "GameSettings.h"
|
#include "GameSettings.h"
|
||||||
|
#include "CRandomGenerator.h"
|
||||||
#include "CTownHandler.h"
|
#include "CTownHandler.h"
|
||||||
#include "CSkillHandler.h"
|
#include "CSkillHandler.h"
|
||||||
#include "BattleFieldHandler.h"
|
#include "BattleFieldHandler.h"
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
#include <vcmi/Metatype.h>
|
#include <vcmi/Metatype.h>
|
||||||
|
|
||||||
#include "CGameInfoCallback.h" // for CGameInfoCallback
|
#include "CGameInfoCallback.h" // for CGameInfoCallback
|
||||||
#include "CRandomGenerator.h"
|
|
||||||
#include "networkPacks/ObjProperty.h"
|
#include "networkPacks/ObjProperty.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
@ -23,6 +22,7 @@ struct BlockingDialog;
|
|||||||
struct TeleportDialog;
|
struct TeleportDialog;
|
||||||
struct StackLocation;
|
struct StackLocation;
|
||||||
struct ArtifactLocation;
|
struct ArtifactLocation;
|
||||||
|
class CRandomGenerator;
|
||||||
class CCreatureSet;
|
class CCreatureSet;
|
||||||
class CStackBasicDescriptor;
|
class CStackBasicDescriptor;
|
||||||
class CGCreature;
|
class CGCreature;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include "CObstacleInstance.h"
|
#include "CObstacleInstance.h"
|
||||||
#include "bonuses/Limiters.h"
|
#include "bonuses/Limiters.h"
|
||||||
#include "bonuses/Updaters.h"
|
#include "bonuses/Updaters.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
#include "../CStack.h"
|
#include "../CStack.h"
|
||||||
#include "../CHeroHandler.h"
|
#include "../CHeroHandler.h"
|
||||||
#include "../filesystem/Filesystem.h"
|
#include "../filesystem/Filesystem.h"
|
||||||
@ -20,6 +21,7 @@
|
|||||||
#include "../BattleFieldHandler.h"
|
#include "../BattleFieldHandler.h"
|
||||||
#include "../ObstacleHandler.h"
|
#include "../ObstacleHandler.h"
|
||||||
|
|
||||||
|
|
||||||
//TODO: remove
|
//TODO: remove
|
||||||
#include "../IGameCallback.h"
|
#include "../IGameCallback.h"
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "../networkPacks/PacksForClientBattle.h"
|
#include "../networkPacks/PacksForClientBattle.h"
|
||||||
#include "../BattleFieldHandler.h"
|
#include "../BattleFieldHandler.h"
|
||||||
#include "../Rect.h"
|
#include "../Rect.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "IGameCallback.h"
|
#include "IGameCallback.h"
|
||||||
#include "LoadProgress.h"
|
#include "LoadProgress.h"
|
||||||
#include "ConstTransitivePtr.h"
|
#include "ConstTransitivePtr.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "../JsonRandom.h"
|
#include "../JsonRandom.h"
|
||||||
#include "../CGeneralTextHandler.h"
|
#include "../CGeneralTextHandler.h"
|
||||||
#include "../IGameCallback.h"
|
#include "../IGameCallback.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "../networkPacks/PacksForClientBattle.h"
|
#include "../networkPacks/PacksForClientBattle.h"
|
||||||
#include "../networkPacks/StackLocation.h"
|
#include "../networkPacks/StackLocation.h"
|
||||||
#include "../serializer/JsonSerializeFormat.h"
|
#include "../serializer/JsonSerializeFormat.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
#include "../modding/ModUtility.h"
|
#include "../modding/ModUtility.h"
|
||||||
#include "../networkPacks/PacksForClient.h"
|
#include "../networkPacks/PacksForClient.h"
|
||||||
#include "../spells/CSpellHandler.h"
|
#include "../spells/CSpellHandler.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "../JsonRandom.h"
|
#include "../JsonRandom.h"
|
||||||
#include "../mapObjects/IObjectInterface.h"
|
#include "../mapObjects/IObjectInterface.h"
|
||||||
#include "../modding/IdentifierStorage.h"
|
#include "../modding/IdentifierStorage.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include "../networkPacks/PacksForClientBattle.h"
|
#include "../networkPacks/PacksForClientBattle.h"
|
||||||
#include "../networkPacks/SetStackEffect.h"
|
#include "../networkPacks/SetStackEffect.h"
|
||||||
#include "../CStack.h"
|
#include "../CStack.h"
|
||||||
|
#include "../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "../../mapObjects/CGTownInstance.h"
|
#include "../../mapObjects/CGTownInstance.h"
|
||||||
#include "../../networkPacks/PacksForClientBattle.h"
|
#include "../../networkPacks/PacksForClientBattle.h"
|
||||||
#include "../../serializer/JsonSerializeFormat.h"
|
#include "../../serializer/JsonSerializeFormat.h"
|
||||||
|
#include "../../CRandomGenerator.h"
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
#include "../lib/serializer/Connection.h"
|
#include "../lib/serializer/Connection.h"
|
||||||
#include "../lib/StartInfo.h"
|
#include "../lib/StartInfo.h"
|
||||||
|
#include "../lib/CRandomGenerator.h"
|
||||||
|
|
||||||
|
|
||||||
// Campaigns
|
// Campaigns
|
||||||
#include "../lib/campaign/CampaignState.h"
|
#include "../lib/campaign/CampaignState.h"
|
||||||
|
Reference in New Issue
Block a user