📄 reliableratiorandomizer.h
字号:
#pragma once
//=============================================================================================================================
/// 脚汾且 荐 乐绰 犬伏 眠免扁 Class
/**
@author Kim Min Wook < taiyo@webzen.co.kr >
@since 2005. 7. 13
@remarks
- 沥犬茄(!) 犬伏 沥痹拳(normalization)啊 啊瓷茄 努贰胶
@note
- 酒捞袍,捣 靛而 犬伏 拌魂俊 荤侩且 荐 乐促.
@todo
- 烹拌甫 困茄 眠啊 皋辑靛 累己且 鞘夸 乐澜
- 弥利拳 累诀 : RandomizedKey()矫俊 犬伏捞 臭篮 鉴辑肺 郴覆瞒鉴栏肺 固府 沥纺且 鞘夸啊 乐澜
@history
- n/a
@usage
-
enum DROP_TYPE
{
DROP_NOTHING = 0,
DROP_NORMALITEM,
DROP_MONEY,
DROP_RAREITEM,
DROP_WASTEITEM,
}
ReliableRatioRandomizer<10> m_ratioSelect; //< 弥措 涝仿且 厚啦 俺荐 = 10
or
ReliableRatioRandomizer<10, DWORD, DWORD> m_ratioSelect;
// 厚啦 涝仿
// param1 : 虐
// param2 : 盒磊
// param3 : 盒葛
assert( FALSE == m_ratioSelect.AddRatio( 0, 30, 100 ) ); //< 虐肺 0阑 荤侩且 荐 绝促.
assert( TRUE == m_ratioSelect.AddRatio( DROP_NORMALITEM, 13, 100 ) );
assert( TRUE == m_ratioSelect.AddRatio( DROP_MONEY, 15, 1000 ) );
assert( TRUE == m_ratioSelect.AddRatio( DROP_RAREITEM, 2, 1000000 ) );
assert( TRUE == m_ratioSelect.AddRatio( DROP_WASTEITEM, 20, 1000 ) );
// 角力 荤侩
for(int i = 0 ;i < 1000 ; ++i )
{
DWORD key = m_ratioSelect.RandomizedKey();
switch( key )
{
case 0: //< N/A
break;
default:
{
// 公攫啊啊 冻绢柳促.
}
}
}
*/
//=============================================================================================================================
#include <Methods.h>
#include <Misc.h>
// 脚汾且 荐 乐绰 厚啦 眠免扁
template< DWORD nSeperator, class Type = DWORD, typename KeyType = DWORD >
class ReliableRatioRandomizer
{
public:
ReliableRatioRandomizer(void):
m_nReload(0)
{
Clear();
}
~ReliableRatioRandomizer(void){}
VOID Clear()
{
m_CurTotalRatio = 0;
m_MaxTotalRatio = 0;
m_freeIndex = 0;
m_nReload = 0;
memset( m_Ratio, 0, sizeof(Type)*nSeperator );
memset( m_MaxRatio, 0, sizeof(Type)*nSeperator );
memset( m_Key, 0, sizeof(KeyType)*nSeperator );
}
BOOL AddRatio( KeyType Key, Type numerator, Type denominator );
// 罚待茄 荐狼 虐甫 府畔
KeyType RandomizedKey(BOOL bDebug= FALSE);
// 醚 惯鞭茄 犬伏
inline DWORD GetTotalRatio()
{
return (m_nReload*m_MaxTotalRatio+(m_MaxTotalRatio-m_CurTotalRatio));
}
// The Key俊 措茄 醚 惯鞭 厚啦
inline DWORD GetRatioForTheKey( KeyType Key )
{
DWORD index = m_freeIndex;
for( DWORD i = 0 ; i < m_freeIndex ; ++i )
{
if( m_Key[i] == Key )
{
index = i;
break;
}
}
if( index == m_freeIndex ) return 0;
return (m_nReload*m_MaxRatio[index]+(m_MaxRatio[index]-m_Ratio[index]));
}
private:
VOID Reload();
inline DWORD GetIndexForTheKey( KeyType Key )
{
DWORD index = m_freeIndex;
for( DWORD i = 0 ; i < m_freeIndex ; ++i )
{
if( m_Key[i] == Key )
{
index = i;
break;
}
}
}
BOOL UpdateRatio( Type prelcm, Type lcm, Type numerator, Type denominator );
private:
// == 0捞 登搁 Reload()
Type m_CurTotalRatio;
Type m_MaxTotalRatio;
Type m_Ratio[nSeperator]; //< 漂沥Index俊 措茄 傈眉 犬伏俊 措茄 何盒 厚啦
Type m_MaxRatio[nSeperator]; //< 漂沥Index俊 措茄 傈眉 犬伏俊 措茄 何盒 厚啦
KeyType m_Key[nSeperator]; //< 漂沥Index俊 措秦 瘤沥等 虐
DWORD m_freeIndex;
// 烹拌甫 困茄 干滚
DWORD m_nReload;
};
template< DWORD nSeperator, class Type, typename KeyType >
BOOL ReliableRatioRandomizer<nSeperator, Type, KeyType>::AddRatio(KeyType Key, Type numerator, Type denominator)
{
if( m_freeIndex >= nSeperator )
return FALSE;
if( Key == 0 ) return FALSE;
ASSERT( m_Ratio[m_freeIndex] == 0 );
ASSERT( m_CurTotalRatio == m_MaxTotalRatio ); //< 荤侩窍扁傈俊 AddRatio啊 捞风绢廉具 茄促.
for( DWORD i = 0 ; i < m_freeIndex ; ++i )
{
ASSERT( m_Key[i] != Key );
}
Type lcm = 0;
if( 0 == m_MaxTotalRatio )
lcm = denominator;
else
lcm = LCM( denominator, m_MaxTotalRatio );
if( !UpdateRatio( m_MaxTotalRatio, lcm, numerator, denominator ) )
return FALSE;
m_CurTotalRatio = m_MaxTotalRatio = lcm;
ASSERT( m_CurTotalRatio != 0 );
m_Key[m_freeIndex] = Key;
++m_freeIndex;
return TRUE;
}
template< DWORD nSeperator, class Type, typename KeyType >
BOOL ReliableRatioRandomizer<nSeperator, Type, KeyType>::UpdateRatio( Type prelcm, Type lcm, Type numerator, Type denominator )
{
ASSERT( m_CurTotalRatio == m_MaxTotalRatio );
ASSERT( m_Ratio[m_freeIndex] == 0 && m_MaxRatio[m_freeIndex] == 0 );
Type CalcRatio = 0;
DWORD i = 0;
for( i = 0 ; i < m_freeIndex ; ++i )
{
//ASSERT( m_MaxRatio[i] == m_Ratio[i] );
m_MaxRatio[i] = m_Ratio[i] *= (lcm/prelcm);
CalcRatio += m_MaxRatio[i];
}
m_MaxRatio[i] = m_Ratio[i] = numerator*(lcm/denominator);
CalcRatio += m_MaxRatio[i];
if( CalcRatio > lcm ) return FALSE;
return TRUE;
}
template< DWORD nSeperator, class Type, typename KeyType >
KeyType ReliableRatioRandomizer<nSeperator, Type, KeyType>::RandomizedKey(BOOL bDebug)
{
// 檬扁拳 秦初绊 蔼捞 绝阑锭
if( 0 == m_MaxTotalRatio ) return 0;
if( 0 == m_CurTotalRatio ) Reload();
ASSERT( m_CurTotalRatio != 0 );
KeyType luckyNumber = 0;
Type FromPercent = 0;
Type ToPercent = 0;
//Type seed = __random( 0, m_CurTotalRatio - 1 );
//Type seed = rand() % ( m_CurTotalRatio );
Type seed = (Type)dRandom( 0, m_CurTotalRatio-1 );
--m_CurTotalRatio;
for( DWORD i = 0 ; i < m_freeIndex ; ++i )
{
FromPercent = ToPercent;
ToPercent += m_Ratio[i];
if(FromPercent <= seed && seed < ToPercent)
{
ASSERT(m_Ratio[i]!=0);
--m_Ratio[i];
luckyNumber = m_Key[i];
break;
}
}
return luckyNumber;
}
template< DWORD nSeperator, class Type, typename KeyType >
VOID ReliableRatioRandomizer<nSeperator, Type, KeyType>::Reload()
{
ASSERT( m_CurTotalRatio == 0 );
ASSERT( m_MaxTotalRatio != 0 );
++m_nReload;
m_CurTotalRatio = m_MaxTotalRatio;
memcpy( m_Ratio, m_MaxRatio, sizeof(Type)*nSeperator );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -