Summary
The problem of location synchronization involves three aspects: client, server, and other clients.
Game player feel is the most priority to meet the conditions, so the position of the client game player is a benchmark, but depends entirely on the game player client data is in danger of being cheated, so the server per second will be based on the general state of the projection of a game player client position.
The position that other clients see depends entirely on the state of the base client, and if there is a delay or pause, the state will be reset according to the latest status.
moving environment of gird
How do clients perceive each other's presence? The server map is divided into a grid of the same specifications, each client and NPC are assigned to different lattices. As shown in the following figure
3D or 2D games can be divided in this way, the difference is that the height of the 3D game will be much larger than the grid X or Y, so that the same XY area under the 3D game to have more space.
[attachment=34862:1.jpg]
Server synchronization grid data.
In the MMO game, players want to see all the players in the visible range, which requires other players to synchronize data over the network. Synchronous usually use 9 house grids determine what data need to be synchronized over the game player, and then their behavior to be synchronized to what game player.
The size of the server large grid to 3*3 lattice to the client than to show a larger range of principles. Larger than the client, is to reserve the time to load the resources.
As shown below, green represents the display area of the mobile client, when the character A is in grid 6, he could see the contents of the 9 lattices of 1,2,3,5,6,7,9,10,11, so when his state changes, you need to synchronize all the players in the 9 grids. also, when there is a change in the player or monster in the 9 grids, it also needs to be synchronized to the character A.
[attachment=34863:2.jpg]
When the character A moves to the grid where the character B is (7), Then he will no longer see the contents of the three 1,5,9 (players and NPC), at the same time, he will be able to see the contents of the map in the three 4,8,12. So in this process, the server to send a message, delete the role of the client where the 1,5,9 of the three A map of the contents of the grid, while the next message to add the contents of these three 4,8,12.
Player movement process
Player status refers to the current state of the player's operation as shown below, see the example program HelloWorldScene.cpp getstatus () function.
[attachment=34864:3.jpg]
Player state is divided into 8 directions, corresponding to the keyboard up, dawn, right, lefe. When you press the "up" button will trigger the state of the broadcast, the player's current location, state sent to the server, and through the server to all visible players broadcast. see HelloWorldScene.cpp onKeyPressed () function. The client coordinates the current player's coordinates x, y, and status (setstatus) to the server. The server handles this request via status.lua. Status.lua first get the length and width of the grid into g_x_len, g_y_len. calculate the current player client request. get the server to calculate the current position of the player to calculate the coordinates of the grid. and set the player's new status to path. the path is then put into the output buffer "script.out" and the "helpregist" script is used to calculate the grid and the grid that the player is going to enter. and call "client/clientregist", "client/clientunregist" script.
The server has two lists for each grid. "Client/clientregist" is the script executed by the server when the client enters the grid. first, the data in the "listclient" table is sent to the client, Here are all the client data stored in the grid. the player's data is then sent to other clients. the NPC data in the listnpc table is then sent to the client. then determine whether the input parameter r is 1 if it is 1 to record the current grid. "s.replyid" will format the content of "script.out" to XML and return it to the client.
The "parabuf" function in the client HelloWorldScene.cpp is used to handle all the returned data from the server. the "regist" command contains the NPC data within the visible range of the player. among them are the initial coordinates of NPC "P0" and the "P1" start time stamp "stamp". the client determines whether the current NPC exists and creates a, and according to the timestamp exists, if it does not exist as a "bomb" object.and according to the NPC coordinates and player coordinates to calculate the coordinates of the screen in the NPC, the NCP will be placed into the screen.
Sample program download
Compiled client relies on cocos2dx, download address