Glory of Kings: Deciphering the glory of kings from the programmer's point of view, and the professional terminology of the Dean.

Source: Internet
Author: User
Tags local time random seed

Pesticides since the line, relying on a strong product force and Tencent's operational capabilities, in the game market performance is a wind, according to the third party survey data show, "Glory of the King" penetration rate of 22.3%, The user size reaches 201 million people, Daily active user (DAU) average value is 54.128 million people. Such a considerable amount of data was admirable.

Of course, as a technical person, is more willing to learn from the technology to some of the glory of the realization of the principle and structure of the way to find new areas of knowledge, expand their knowledge boundaries, enrich their professional skills. With this game, we'll talk about the glory of the king's Technology and synchronization methods, more from the Moba (Multiplayer Online Tactical competition game) direction to resolve the realization of the King's plan, if there are endless analysis of the direction, welcome to discuss the improvement. Here are a few key points to explain:

1. Server architecture

2. Mode of communication

3. Synchronization scheme

4. Skill Synchronization

5. Disconnected connection

First, server architecture

It is not difficult to find that the king of the glory of the server using room mode, each player after landing, and then into the hall, to match the game. After the match is complete, put the player in the game into a room for a battle.

Room class play and MMORPG is very different, because of its online broadcast unit uncertainty and the number of broadcasts is very small, and need to match a room server to allow a few people into a server.

The most important thing about this kind of game is the load capacity of its "game hall", each "game room" is limited by logic, the number of players that need to be maintained and broadcast is finite, but the "game lobby" needs to maintain a fairly high amount of online users, so in general the game needs to be "split". The most challenging task in the "Game Hall" is to "match" players into a "game room," which requires searching and filtering for all online players, and for a better experience, the player will be matched in a separate locale to facilitate faster synchronization.

The general way is the player first log in "lobby Server", and then select the function of the team game, the server will notify all the participating game clients, new open a connection to the room server, so that all participating users can play in the room server interactive.

Ii. Means of communication

When it comes to communication, there will generally be HTTP and socket two ways, but the HTTP bottom is also the use of sockets, only after each communication is completed will be disconnected, this way for the need for frequent interaction between the two sides, appears to be too inefficient, Therefore, the general real-time requirements of the game is to use the socket method to communicate.

But Sokect communication, but also divided into two kinds: TCP vs UDP, the specific use of that type of socket, the need to specifically look at the game type. Here are two types of pros and cons:

From the above comparison, we can find that the socket, we want to do, TCP has helped us to do, we only need to establish links, and then read and write as the file read and write. And UDP requires us to design everything ourselves. When you see all this, you may be the first to feel the adoption of TCP rather than UDP, so this is true. Based on the game business and the scene is different, I can tell you clearly, the King of Glory is the use of UDP, including Tencent most of the long chain took over the tour is the use of UDP, this is why.

1, TCP to ensure the reliability of data has a price

TCP can guarantee the reliability and order of the packets, all of which will help you to encapsulate them. TCP sends a packet for a period of time until it detects that the packet is missing, and if it does not receive its ACK, then resend the missing packet to the target computer. Duplicate packets will be discarded at the receiving end, and the ordered packets will be reordered. In order to ensure the reliability and order of the packet.

However, in order to ensure reliable and orderly, it is necessary to ensure that TCP no matter what the case, as long as the packet error, you must wait for the packet to be sent back. What does that mean, even if the latest data arrives, but you can't access the packets, the new data is placed in a queue and you need to wait for the missing packets to be sent back and all the data is not lost before it can be accessed.

So, if the network environment is too bad or unstable, such as the domestic mobile network, or encountered network congestion, a packet loss, everything needs to stop to wait for the packet to be sent back. The client will appear waiting to receive data, players will appear in the operation of cotton and the response is not timely phenomenon.

2, the reliability of UDP-diy manual assembly

From the above we can know that UDP mainly in the reliability of the main is not guarantee the order of packets, such as the 100th packet received is not necessarily the 100th of the packet, but also can not guarantee that the packet lost, during a packet loss, UDP is not going to school inspection. If these two problems are solved, most of the reliability problems of UDP will be solved.

The specific solution is that we are not going to elaborate on this one, and this is largely the solution:

1, for each packet to increase the serial number, each packet, add local serial number.

2, each packet added a field, used to accommodate multiple identifiers. Confirm the number of characters, follow the application of the contract rate to feel, the higher the rate, the number of confirmed characters are correspondingly more.

3, each receive the package, the received package on the serial number into a confirmation character, send the packet when the confirmation characters with these.

4. If you find a packet missing from the confirmation character, leave it to the application to write a new packet that contains the missing data and, if necessary, send it with a new serial number.

5, for many times to receive the same package can give up it

Third, synchronization program

In the game common synchronization scheme, has the state synchronization and the frame synchronization, the general large-scale MMOARPG all is uses the state synchronization, for example World of Warcraft, the state uses the C/s architecture synchronously, all state by the server controls, the security is relatively high, but the traffic is quite big. Frame synchronization using the prisoner mode, all C-end force a logical frame rate, so as to ensure the output consistent, the characteristics of the traffic is small, security is relatively poor.

The glory of the king is to use frame synchronization, then what is the specific frame synchronization, how to achieve, we from two places to decompose:

1, Frame rate

What is the frame rate, may not have done the client classmate is not very clear about this term, we from a small Li Zilai explained. I remember when I was a kid, there was a comic book, and it was quick to see the characters in the comics moving.

Because of the special physiological structure of human eyes, if the frame rate of the picture is higher than about 10-12 frames per second, it will be considered as coherent, this phenomenon is called visual temporary retention. That's why the film is shot in one grid, and then quickly, just like the picture on the top of a comic book.

All the animation in the game is also used in this way to render, but the frame rate is a GPU to control, you see the screen are all have a GPU frame frame rendering, such as 30 frames/s, you see the screen is more fluent. The higher the frame rate, the smoother you see.

2, lockstep-frame synchronization

Frame synchronization can be said to extend through the frame rate, you can think of a game as a huge state machine, all the participants are using the same logical frame rate to continue to move forward.

We look at the following 2 graphs:

This is the timeline of three players in A, B, and C, which is not a local time on the computer, but a timeline defined when a, B, and C are online. The dotted line separates the time slices called turn and can be understood as a frame. The arrow indicates that the player broadcasts his or her operation instructions to another player.

We think of a game as a large state machine, because everyone is playing the same game, so F is the same, the initial state S0 is the same. At the end of the first turn, all players received the exact same input I, noting that I was not a value here, but that it contained a set of instructions for all players in the current game. The computer calculates the results for all players at T1 time. Because F, S0 and I are fixed, the next state S1 calculated on each player's computer must be the same.

So through the above we can know:

1, we put the game forward into a frame, where the frame and the game's rendering frame rate is not a, just draw on the concept of frames, custom frames, we call turn. The process of the game is every turn keep pushing forward, each player's turn propulsion speed is consistent.

2, each frame only when the server set all the players operation instructions, that is, the input is determined, before you can calculate, into the next turn, otherwise it will wait for the slowest player. And then broadcast it to all the players. This ensures that frames are consistent.

3, lockstep game is strictly in accordance with the turn forward, if someone's delay is higher, other players must wait for the player to follow up and then continue to calculate, there is no player leading or behind the other players a number of turn. In a game that uses the lockstep synchronization mechanism, each player's delay equals the person who has the highest latency.

4, because everybody's turn consistent, as well as the input fixed, therefore each step all client's computation result is consistent.

Let's look at the specific execution process:

We can see clearly in the image above that the frame synchronization of this prisoner pattern, in the second frame, because the Player 1 has the delay, causes the second frame synchronization time to be delayed, thus causes all players to wait, appears the phenomenon of Carrington.

Iv. optimistic Lock & disconnection connection

A fatal flaw in the prisoner mode frame synchronization is that if a networked player has a slow speed, it will affect the experience of other players because the server waits until all inputs are reached and then synchronizes to all C terminals. In addition, if someone dropped the line, the game will be unable to continue or lose line players can not be linked, because in the strict frame synchronization, midway into the game is technically very difficult. Since you're back in, your initial state is inconsistent with everyone else, and your state information is lost, such as your rank, random seed, attribute information for the role, and so on. For example, played the early frozen throne know, once off the line basic this bureau will be abolished, need to re-opened, as to why there is no cotton phenomenon, because at that time is the solution is to use the LAN way, so basically there is no delay problem.

Later in order to solve this problem, now including the glory of the king, the server will save the player on the spot game instructions and status information, when the player disconnected, can revert to the state before the disconnection. But this is still unable to solve the frame synchronization problem, because strict frame synchronization, is to wait until all players have input, then to notify the broadcast of the client update, if a server has not been input synchronization over, we are to wait, then how to solve this problem.

The optimistic way of "timed not Waiting" is to broadcast the operation to all users every time the interval clock occurs, and it does not depend on whether the individual player has an operation update. So the frame rate of the clock in the server control, when the client has operations in time to send the server, and then the service side 20-50 times per second to all clients send update messages. The following figure:

In the image above, we see that the server will not wait until all user input has been collected before the next frame. But in accordance with the fixed frequency to sync the input information to each of the C-side, if there is a player network delay, the server frame step will not wait, such as the image above, in the second frame, the player a slow speed, then he this time , will be the fast speed of the player to give seconds (other games are similar). But the slow speed of the player will not card to the fast player, will only feel the operation of their own delay.

V. Skill synchronization

A lot of the game is related to probability, such as the damage of the skill has a certain probability of crit damage or refraction was hit. According to the frame synchronization, based on the same input, each player's client is independent calculation of the damage, then how to ensure that all computer crit damage consistent. This time we need to use pseudo random.

Most of the random numbers in the built-in libraries of the programming languages are generated by linear congruence generators, and if random seed (Random seed) is not specified, the current system timestamp is used as the random seed by default. Once a random seed is specified, the resulting sequence of random numbers is determined. This means that the two computers use the same random seed, and the nth random results are consistent.

So before the game starts, the server assigns a random seed to each player, then synchronizes it to the client, so that each client calculates the skill of each character to ensure that the damage is consistent. This is also the majority of frame synchronization games adopted by the scheme, including the glory of Kings.

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.