In-depth Broadcast Algorithm

Source: Internet
Author: User

For MMORPG games, the number of people in each scenario may count in thousands. For the sake of server performance, each player's vision must be limited. Broadcast algorithms are mainly used to solve how to make every player aware of objects. I will focus on various broadcast algorithms and optimization.

1. Common Algorithms

Concepts:

Entity: various objects that require server management, such as players/NPC/pets

 

The original method of the broadcast algorithm is to store a list of surrounding entities for each object, and then regularly detect the distance between entities in the entire scenario. based on this distribution, the algorithm complexity is 0 (N ^ 2 ). for a slightly larger scenario, the number of entities may be tens of thousands. This part of the consumption will be very impressive, and few people are using it.

The broadcast of existing games is generally calculated based on the grid, and commonly used are 3*3, 5*5 broadcasts. The general process is:

When a player enters the scene, it is added to the corresponding grid according to the coordinates. When each movement is performed, it checks whether the grid is crossed. If the grid is crossed, the grid that disappears/appears is calculated. dispatch events to objects. the gamer exits from the scene, and then sends a disappearance event to the object in the surrounding lattice.

The biggest benefit of this algorithm is to evenly share the consumption of distance detection. the overall consumption is also very small. the disadvantage is the asymmetry of the field of view. Players cannot always be at the center of the Grid. Most of the situations are that the players can see 50 m in a certain direction and m in another direction. these redundant visions will significantly increase server traffic. an ideal state is that the player's field of view is consistent in that direction. that is, the circular field of view.

Assuming that the minimum field of view required by planning is R, we can calculate the field of view for various algorithms.

3*3 broadcast: the player's field of view is (3 * r) ^ 2 = 9 * r ^ 2

5*5 broadcast, with the field of view (5 * r/2) ^ 2 = 6.25 * r ^ 2

Circular broadcast, pI * r ^ 2

The field of view area is proportional to the broadcast traffic. From the calculation here, we can see that the circular algorithm can significantly reduce the broadcast traffic.

Ii. Circular Broadcast Algorithm

This section summarizes the benefits of the circular algorithm.

First, we will analyze the problem in more detail.

1. Since the broadcast is optimized, in reality, we only need to deal with Entity types that may generate large amounts of traffic from players to players/NPC/pets. All other appearance/disappearance logics use the Grid Algorithm

2. In the copy, the broadcast volume is small and circular broadcast is not required.

We integrate the advantages and disadvantages of the original algorithm and the lattice algorithm. to ensure that the grid size of the scenario is the smallest field of view R, determine the following circular broadcast algorithm flow (assuming that the player/NPC/PET and other types to be detected are Entity ):

1. When a player enters the scene, take the entity within the jiugongge range. A. determine the distance and dispatch events within the field of view.

2. Players exit from the scene and distribute the disappearance events for the previously stored broadcast entities.

3. the appearance or disappearance of a player's movements in the scene is triggered by regular distance judgment.

Here, step 3 is the focus and is divided into the following three steps:

1. A tag is saved in the player's list of surrounding entities. Each scheduled cycle starts and is set to 0.

2. each player in the scenario is centered on his or her lattice C. it is always used to retrieve the entity a in a lattice index that is not smaller than C. set the field of view to 1. if the event appears for the first time in the surrounding list, the event is distributed.

3. Check the mark position of the entity around each player. The event of dismounting is still 0.

For efficiency considerations, pay attention to the following details:

1. For more convenient and convenient retrieval of specified objects in the grid, objects in the grid are stored by type.

2. Step 1 of the regular check can be merged into step 3.

3. the surrounding objects are also stored by type. reduce the consumption of scheduled check step 3.

4. It is critical to select the data structure of the surrounding Object List. tested, the hash_map with memory optimization has better applicability here.

In general, the detection consumption of 1600 million objects will still reach more than 6%. This is a huge one. More optimizations need to be considered.

3. More Optimizations

Regular detection of circular algorithms is a computing intensive place. More optimizations require further exploration of hardware potential. the options include: vectorized (SSE), parallel and general computing capability acceleration using GPU (opencl ). based on the characteristics of the algorithm and the hardware conditions of the server (insufficient multi-core utilization, no GPU), select the optimization focus for the next step in parallel.

There are many underlying implementation solutions for parallelism, such as OpenMP, TBB, and vc2010.

OpenMP can be selected for better applicability and convenience.

Currently, the logic structure of the server is generally single-threaded/multi-process. after the introduction of parallelism, in order to prevent the spread of multithreading and put as much computing as possible into the parallel loop. we need to strip the logic of dispatching/disappearing events during the detection period. wait until the end of the parallel computing before triggering.

After the primary loop for scheduled detection is parallel, it is roughly as follows:

# Pragma OMP parallel

For (Iy = 0; Iy <m_cntrow; ++ Iy)

{

Curcellid = Iy * m_cntcol;

For (IX = 0; ix <m_cntcol; ++ IX, ++ curcellid)

{

Detects the distance from players in the curcellid to other Grid entities.

Detects the distance from players in this grid to other entities, and examines the tag space

}

}

Cache-triggered events

The parallel processing is very effective. According to the number of cores, it can generally increase N * 0.9 times. 8000 entity mutual detection only accounts for about 5%.

Iv. Broadcasting algorithm multiplexing and other

In view of the importance of broadcast on the server, to achieve the maximum reuse/optimization effect. we can start another process on the server to do this, and agree on the protocol with the main logic server, so that all games can be used

Define the master logical server as m, and the broadcast server as n. The protocol is defined as follows:

M --> N creation scenario

M --> N create an object at a certain position

M --> N object movement

M --> N object Deletion

N --> m between entities

N --> M object disappears

If the processing of events that occur or disappear consumes a large amount of time, you can improve the circular field of view broadcast algorithm to minimize the field of view. This reduces the number of events that players frequently move back and forth on the border.

5. Extended applications

Based on the above algorithm IDEA, it can also be applied to the loading of client resources in the infinite world.

Client resource loading also has the disadvantages of server broadcasting: Resource loading is not balanced, and the boundary is frequently created and destroyed.

We can further refine each lattice into smaller grids based on the nine grids, and use the small grid distance to determine the approximate Effect of circular loading.

Then, we introduce a large and small field of view to reduce the creation/destruction of border areas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.