1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Added RandomGeneratorUtil::nextItemWeighted convenience method

This commit is contained in:
Ivan Savenko 2024-01-31 00:17:40 +02:00
parent 7992144763
commit a9866bb5c6
21 changed files with 42 additions and 3 deletions

View File

@ -22,6 +22,8 @@
#include "../../lib/battle/BattleStateInfoForRetreat.h"
#include "../../lib/battle/CObstacleInstance.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/CRandomGenerator.h"
// TODO: remove
// Eventually only IBattleInfoCallback and battle::Unit should be used,

View File

@ -11,6 +11,7 @@
#include "DangerHitMapAnalyzer.h"
#include "../Engine/Nullkiller.h"
#include "../../../lib/CRandomGenerator.h"
namespace NKAI
{

View File

@ -15,6 +15,7 @@
#include "../../lib/CCreatureHandler.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/battle/BattleInfo.h"
#include "../../lib/CRandomGenerator.h"
CStupidAI::CStupidAI()
: side(-1)

View File

@ -29,6 +29,7 @@
#include "../../CCallback.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/CStack.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/spells/CSpellHandler.h"

View File

@ -37,6 +37,7 @@
#include "../../lib/spells/ISpellMechanics.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/battle/BattleHex.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/CStack.h"
#include "../../lib/CondSh.h"
#include "../../lib/TextOperations.h"

View File

@ -22,7 +22,6 @@
#include "../CPlayerInterface.h"
#include "../CMusicHandler.h"
#include "../CVideoHandler.h"
#include "../CPlayerInterface.h"
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
@ -43,6 +42,7 @@
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/CThreadHelper.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/mapping/CMapInfo.h"

View File

@ -47,6 +47,25 @@ namespace RandomGeneratorUtil
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>
void randomShuffle(std::vector<T> & container, vstd::RNG & rand)
{

View File

@ -14,6 +14,7 @@
#include "ResourceSet.h"
#include "filesystem/Filesystem.h"
#include "VCMI_Lib.h"
#include "CRandomGenerator.h"
#include "CTownHandler.h"
#include "GameSettings.h"
#include "constants/StringConstants.h"

View File

@ -16,7 +16,6 @@
#include "GameConstants.h"
#include "JsonNode.h"
#include "IHandlerBase.h"
#include "CRandomGenerator.h"
#include "Color.h"
#include "filesystem/ResourcePath.h"
@ -29,6 +28,7 @@ class CLegacyConfigParser;
class CCreatureHandler;
class CCreature;
class JsonSerializeFormat;
class CRandomGenerator;
class DLL_LINKAGE CCreature : public Creature, public CBonusSystemNode
{

View File

@ -18,6 +18,7 @@
#include "battle/BattleHex.h"
#include "CCreatureHandler.h"
#include "GameSettings.h"
#include "CRandomGenerator.h"
#include "CTownHandler.h"
#include "CSkillHandler.h"
#include "BattleFieldHandler.h"

View File

@ -12,7 +12,6 @@
#include <vcmi/Metatype.h>
#include "CGameInfoCallback.h" // for CGameInfoCallback
#include "CRandomGenerator.h"
#include "networkPacks/ObjProperty.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -23,6 +22,7 @@ struct BlockingDialog;
struct TeleportDialog;
struct StackLocation;
struct ArtifactLocation;
class CRandomGenerator;
class CCreatureSet;
class CStackBasicDescriptor;
class CGCreature;

View File

@ -12,6 +12,7 @@
#include "CObstacleInstance.h"
#include "bonuses/Limiters.h"
#include "bonuses/Updaters.h"
#include "../CRandomGenerator.h"
#include "../CStack.h"
#include "../CHeroHandler.h"
#include "../filesystem/Filesystem.h"
@ -20,6 +21,7 @@
#include "../BattleFieldHandler.h"
#include "../ObstacleHandler.h"
//TODO: remove
#include "../IGameCallback.h"

View File

@ -25,6 +25,7 @@
#include "../networkPacks/PacksForClientBattle.h"
#include "../BattleFieldHandler.h"
#include "../Rect.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -13,6 +13,7 @@
#include "IGameCallback.h"
#include "LoadProgress.h"
#include "ConstTransitivePtr.h"
#include "../CRandomGenerator.h"
namespace boost
{

View File

@ -13,6 +13,7 @@
#include "../JsonRandom.h"
#include "../CGeneralTextHandler.h"
#include "../IGameCallback.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -20,6 +20,7 @@
#include "../networkPacks/PacksForClientBattle.h"
#include "../networkPacks/StackLocation.h"
#include "../serializer/JsonSerializeFormat.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -31,6 +31,7 @@
#include "../modding/ModUtility.h"
#include "../networkPacks/PacksForClient.h"
#include "../spells/CSpellHandler.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -20,6 +20,7 @@
#include "../JsonRandom.h"
#include "../mapObjects/IObjectInterface.h"
#include "../modding/IdentifierStorage.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -19,6 +19,7 @@
#include "../networkPacks/PacksForClientBattle.h"
#include "../networkPacks/SetStackEffect.h"
#include "../CStack.h"
#include "../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -20,6 +20,7 @@
#include "../../mapObjects/CGTownInstance.h"
#include "../../networkPacks/PacksForClientBattle.h"
#include "../../serializer/JsonSerializeFormat.h"
#include "../../CRandomGenerator.h"
VCMI_LIB_NAMESPACE_BEGIN

View File

@ -15,6 +15,8 @@
#include "../lib/serializer/Connection.h"
#include "../lib/StartInfo.h"
#include "../lib/CRandomGenerator.h"
// Campaigns
#include "../lib/campaign/CampaignState.h"