Series Article Directory
Article directory
foreword
Since the launch of pesticides, relying on the strong product power and Tencent's operational capabilities, it can be described as a booming performance in the game market. According to third-party research data, the penetration rate of "Honor of Kings" has reached 22.3%, and the number of users has reached 22.3%. 201 million people, with an average daily active user (DAU) of 54.128 million. Such impressive data is very admirable.
Of course, as a technical person, I am more willing to technically understand some of the realization principles and architectural methods of King's Glory, find new knowledge areas, expand my knowledge boundaries, and enrich my professional skills.
With the help of this game, in this article, let's talk about the technical implementation and synchronization method of the King's Glory, and more from the MOBA (multiplayer online tactical competition game) direction to analyze and reason the king's implementation plan, if there are endless analysis local, welcome to discuss improvements together.
1. Server Architecture
It is not difficult to find that the server of King of Glory adopts the room mode. After each player logs in, they enter the lobby and play the matching game. After the match is complete, put the players who are playing together in a room to play against each other.
Room-type gameplay is very different from MMORPG in that its online broadcast unit has a small number of uncertainties and broadcasts, and it needs to match a room server to allow a few people to enter a server.
The most important thing for this type of game is the capacity of its "game hall", each "game room" is limited by logic, and the player data that needs to be maintained and broadcast is limited, but the "game hall" needs to maintain a fairly high online The number of users, so generally speaking, this kind of game still needs to be "split". The most challenging task in the "game lobby" is to "automatically match" players into a "game room", which requires searching and filtering all online players, and for a better experience, the players will be divided into regions Match for faster synchronization.
The general method is that players log in to the "lobby server" first , and then select the function of teaming games. The server will notify all participating game clients to open a new connection to the room server, so that all participating users can play in the room server. Interact with the game.
2. Communication method
When it comes to communication methods, there are generally two methods: http and socket, but the bottom layer of http also uses socket, but it will be disconnected after each communication is completed. This method is too inefficient for both parties who need frequent interaction. Therefore, games with high real-time requirements generally use socket mode to communicate.
However, sokect communication is divided into two types: TCP vs UDP, which socket type is used, and the game type needs to be looked at in detail. The following are the pros and cons of the two types:
From the above comparison, we can find that tcp does everything we want to do about sockets. We only need to establish a link, and then read and write like a file. That's it. And udp requires us to design everything ourselves.
Seeing all this, your first impression may be to use tcp instead of udp, so is this the real situation? Game-based businesses and scenarios are different. I can tell you clearly that the King of Glory uses udp, including most of Tencent's long-link mobile games using udp. Why?
1. TCP guarantees data reliability at a cost
tcp can ensure the reliability and order of data packets, all of which are encapsulated for you. TCP sends a packet, waits for a period of time until it detects that the packet is lost, and if it does not receive its ACK, then resends the lost packet to the target computer. Duplicate packets will be dropped at the receiver and out-of-order packets will be reordered. This ensures the reliability and orderliness of the data packets.
However, in order to ensure reliability and order, it is necessary to ensure that TCP no matter what the situation, as long as the data packet is wrong, it must wait for the retransmission of the data packet. What does this mean, that is, even if the latest data has arrived, but these data packets cannot be accessed, the newly arrived data will be placed in a queue, and it is necessary to wait for the lost packets to be resent, all data is not lost to be able to access.
In this way, if the network environment is too poor or unstable, such as the domestic mobile network, or network congestion occurs, and a data packet is lost, everything needs to stop and wait for the data packet to be resent. The client will wait to receive data, and the player's operation will be stuck and the response will not be timely.
2. The reliability of udp - DIY manual assembly
From the above, we can know that udp mainly cannot guarantee the order of data packets in terms of reliability. For example, the 100th received data packet is not necessarily the 100th sent data packet, and there is no guarantee that packets will not be lost during the period. There is a packet loss, and udp will not check it. If these two problems are solved, most of the reliability problems of udp are solved.
We will not elaborate on the specific plan in this article, but generally solve it in this way:
-
1. Add a sequence number to each data packet, and increase the local sequence number each time a packet is sent.
-
2. A bit field is added to each data packet to accommodate multiple acknowledgments. According to the number of confirmation characters, follow the application's packet sending rate. The higher the rate, the more confirmation characters.
-
3. Every time a packet is received, the serial number on the received packet is changed into a confirmation character, and these confirmation characters are brought when sending the packet.
-
4. If a packet is found to be lost from the acknowledgment character, leave it to the application to write a new packet containing the missing data, and if necessary, send the packet with a new sequence number.
-
5. You can abandon it when you receive the same package multiple times.
3. Synchronization scheme
Common synchronization schemes in games include state synchronization and frame synchronization. Generally, large-scale MMOARPGs use state synchronization, such as World of Warcraft. State synchronization adopts C/S architecture, and all states are controlled by the server, which is relatively safe. , but the flow is relatively large. The frame synchronization adopts the prisoner mode, and all c-ends are forced to adopt a logical frame rate to ensure consistent output. It is characterized by small traffic and poor security.
The King of Glory uses frame synchronization, so what is the specific frame synchronization and how to achieve it, we will break it down from two places:
1. Frame rate
What is frame rate, maybe students who haven't been a client are not very clear about this term, let's explain it from a small plum. I remember when I was a kid there was a kind of mini-book, and if you flipped through it quickly, you could see that the characters in the comic would move.
Due to the special physiological structure of the human eye, if the frame rate of the viewed picture is higher than about 10-12 frames per second, it will be considered coherent, and this phenomenon is called persistence of vision. This is why the film footage is shot frame by frame and then played back quickly, just like flipping through the little book in the picture above.
All animations in the game are also rendered in this way, but the frame rate is controlled by the GPU, and the pictures you see are all rendered by the GPU one frame at a time, such as 30 frames/s. The picture you see is smoother. And the higher the frame rate, the smoother what you see.
2.Lockstep—frame synchronization
Frame synchronization can be said to be extended by frame rate. You can think of a game as a huge state machine, and all participants use the same logical frame rate to continuously move forward.
Let's look at the following two pictures:
The picture shows the timeline of three players A, B, and C. This timeline is not the local time on the computer, but a timeline defined when A, B, and C are online. The time slice separated by the dotted line is called turn, which can be understood as a frame. The arrow indicates that the player broadcasts his operation instructions to other players.
We regard a game as a large state machine, because everyone is playing the same game, so F is the same, and the initial state S0 is also the same. At the end of the first turn, all players receive the exact same input I, note that I here is not a value, but contains the set of operation instructions of all players in the current game. At t1, the computers of all players calculate the results by themselves. Since F, S0 and I are fixed, the next state S1 calculated on each player's computer must be the same.
So from the above we can know:
-
1. We divide the progress of the game into frames. The frame here is not the same as the rendering frame rate of the game. It just borrows the concept of frames. The custom frame is called turn. The process of the game is that each turn advances continuously, and each player's turn advances at the same speed.
-
2、每一帧只有当服务器集齐了所有玩家的操作指令,也就是输入确定了之后,才可以进行计算,进入下一个turn,否则就要等待最慢的玩家。之后再广播给所有的玩家。如此才能保证帧一致。
-
3、Lockstep的游戏是严格按照turn向前推进的,如果有人延迟比较高,其他玩家必须等待该玩家跟上之后再继续计算,不存在某个玩家领先或落后其他玩家若干个turn的情况。使用Lockstep同步机制的游戏中,每个玩家的延迟都等于延迟最高的那个人。
-
4、由于大家的turn一致,以及输入固定,所以每一步所有客户端的计算结果都一致的。
我们来看看具体的执行流程:
上图中我们可以明显看到,这种囚徒模式的帧同步,在第二帧的时候,因为玩家1有延迟,而导致第二帧的同步时间发生延迟,从而导致所有玩家都在等待,出现卡顿现象。
四、乐观锁&断线重连
囚徒模式的帧同步,有一个致命的缺陷就是,若联网的玩家有一个网速慢了,势必会影响其他玩家的体验,因为服务器要等待所有输入达到之后再同步到所有的c端。
另外如果中途有人掉线了,游戏就会无法继续或者掉线玩家无法重连,因为在严格的帧同步的情况下,中途加入游戏是从技术上来讲是非常困难的。因为你重新进来之后,你的初始状态和大家不一致,而且你的状态信息都是丢失状态的,比如,你的等级,随机种子,角色的属性信息等。
比如玩过早期的冰封王座都知道,一旦掉线基本这局就废了,需要重开,至于为何没有卡顿的现象,因为那时都是解决方案都是采用局域网的方式,所以基本是没有延迟问题的。
后期为了解决这个问题,如今包括王者荣耀,服务器会保存玩家当场游戏的游戏指令以及状态信息,在玩家断线重连的时候,能够恢复到断线前的状态。
不过这个还是无法解决帧同步的问题,因为严格的帧同步,是要等到所有玩家都输入之后,再去通知广播client更新,如果A服务器一直没有输入同步过来,大家是要等着的,那么如何解决这个问题?
The optimistic method of "no waiting for timing" is used to broadcast the operation to all users every time the Interval clock occurs, regardless of whether each player has an operation update. The clock of such a frame rate is controlled by the server. When the client has an operation, the server is sent in time, and then the server sends update messages to all clients 20-50 times per second. As shown in the figure below:
In the above figure, we see that the server will not wait until all user input is collected before proceeding to the next frame, but will synchronize the player's input information to each c-side at a fixed frequency. If there is a player network delay, the server will For example, in the above picture, in the second frame, player A's network speed is slow, so at this time, he will be given seconds by players with fast network speed (other games are similar) . However, players with slow Internet speeds will not be stuck with fast players, and will only feel that their operations are delayed.
5. Skill synchronization
Many games are related to probability, for example, the damage of skills has a certain probability of crit damage or refraction. According to the frame synchronization, based on the same input, each player's client calculates the damage independently, so how to ensure that the critical damage of all computers is the same. This is where pseudo-random is needed.
The random numbers in the built-in libraries of most programming languages are generated by linear congruence generators. If Random Seed is not specified, the current system timestamp is used as the random seed by default. Once the random seed is specified, the resulting random number sequence is deterministic. That is to say, the two computers use the same random seed, and the Nth random result is the same.
So before the game starts, the server assigns a random seed to each player, and then synchronizes it to the client, so that each client can ensure that the damage is consistent when calculating the skills of each character. This is also the solution adopted by most frame-synchronized games, including the Honor of Kings.
Summarize
The above is the frame synchronization of the current game. Since there are too many contents involved, I will have the opportunity to talk about this part in detail later. It is very convenient, we must master it. Hope you all support! In addition, if there are any problems with the above, please understand my brother's advice, but it doesn't matter, the main thing is that you can persevere, and I hope that there are classmates who study together to help me correct me, but if you can please tell me gently, love and peace are eternal themes , love you all. Come on!