Use BitMap to achieve game tasks and synchronization of achievements

Foreword:

Nowadays, mobile games generally have systems similar to tasks and achievements. We assume that there are many, many achievements, and the traffic of synchronizing the state between the server and the client will be relatively large. What is a good way to reduce traffic consumption, we can use BitMap.


text

BitMap
  • Definition: Use a bit to mark the Value corresponding to an element, and the Key is the element.
  • advantage:
    • High operational efficiency (no need to compare and shift)
    • Occupy less memory (0 and 1 are oil meaning)
  • shortcoming:
    • All data cannot be repeated.
  • Purpose: can quickly search, judge and delete data
  • Scope of application: Generally speaking, the data range is less than 10 times that of int
  • For the achievement system, the achievement has its own id, so it will definitely not be repeated.

  • Implementation ideas:

    • The total number is N, the required memory space: int [1 + N/32]
    • When depositing:
      • The subscript in the array a corresponding to the decimal number 0-N: index_loc = N / 32
      • i >> 5 is equivalent to i / 32
      • Find the bit corresponding to the decimal number 0-N: bit_loc = N % 32
      • n & 31 is equivalent to n % 32
  • Code:

class BitMap
{
    const int BITSPERWORD = 32;
    const int SHIFT = 5;
    const int MASK = 0x1F; // 31
    const int N = 10000000;

    private int[] a;

    //申请内存的大小  
    public BitMap() {
        a = new int[1 + N / BITSPERWORD]; 
    }

    public BitMap(int totalCount) {
        a = new int[1 + totalCount / BITSPERWORD];
    }

    public void SetBit(int i)
    {
        a[i >> SHIFT] |= (1 << (i & MASK));
    }

    //Clear 初始化所有的bit位为0  
    public void Clear(int i)
    {
        a[i >> SHIFT] &= ~(1 << (i & MASK));
    }

    //test 测试所在的bit为是否为1  
    public bool GetIsTrue(int i)
    {
        int index = i & MASK;
        return (a[i >> SHIFT] & (1 << index)) != 0 ;
    }
}
  • Example of use:
int count = 10000;
int randomCount = 10;

BitMap bitMap = new BitMap(count);
Random random = new Random();


for (int i = 0; i < randomCount; i++)
{
    int num = random.Next(count);
    Console.WriteLine(num);
    bitMap.SetBit(num);
}

Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");

for (int i = 0; i < count; i++)
{
    if (bitMap.GetIsTrue(i))
    {
        Console.WriteLine(i);
    }
}

Console.ReadKey();
  • Effect:

  • The design idea of ​​achievement system:
    • All achievement information is table configuration data, including rewards and achievement conditions.
    • When detecting whether an achievement is achieved, both the client and the server calculate in real time.
    • When the achievement ID is configured, there can be an offset value, such as 10000, and when the SC actually communicates, the id can subtract this offset value, starting from 0.
    • Use BitMap to record whether the achievement reward is received, synchronize this status, and cooperate with the client's local calculation to get the status of a specific achievement: to be achieved, achieved or received.

Extension:

The open source implementation of https://github.com/lemire/javaewah
bitmap includes EWAH, which adopts RLE (Run Length Encoding) compression, which solves the waste of storage space well.


If there are any mistakes, please point them out.

email:dxmdxm1992#gmail.com

blog: http://blog.csdn.net/david_dai_1108

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517057&siteId=291194637