Synchronization of object states in the game's big map

    The map of the game battlefield is very large, with hundreds of players active on the map. How does the server synchronize the status of player heroes, monsters, buildings and other map objects on the map? It is certainly not possible to synchronize the state of all objects on the map to the player, because the amount of data is too large. The server only needs to synchronize the state of the map objects around the player’s field of view to the player. This can meet the needs. The following describes how to synchronize the player’s field of view. The state of the map object.

    You can think of the map as consisting of many small squares, and the player heroes, monsters, and buildings are all on the squares, and the server only needs to synchronize the state changes of the map objects on the squares around the grid where the field of view is located to the player. This is exactly the area seen by the player's screen. As shown:

    We can number each grid from left to right, and then from top to bottom, starting from zero, so that we can use an array to manage map objects. Calculate index=y*width+x according to the coordinates x, y, and use index as a subscript, we can quickly find the map object on the coordinate grid where we are, and the calculation time complexity is O(1).

    How to define the map objects around the player’s field of vision is a problem. Assuming that a mobile phone can display 10×10 squares, then we only need to synchronize the state of the map objects on the 10×10 squares with the player’s field of view as the center point. Give it to the player. In this way, the client can obtain the change in the state of the map object and present it to the player. As the player's hero moves, the player's point of view is constantly moved, so that the player can watch the status and displacement of other heroes, monsters, and buildings on the map.

    However, every time the player's field of view moves a grid, the surrounding ten grid maps need to be recalculated, and then the map objects that have been out of the field of view are removed from the field of view, and the newly entered map objects are added to the field of view, and synchronization is completed. When the state of a monster or a hero changes, it is also necessary to calculate the grids where the object can be seen in the field of view, and synchronize the state to the players with the field of view on these grids, which is more complicated to calculate.

    There is another way to deal with it, which is to divide the map again with a screen as a grid. A large grid contains 10×10 small squares. In this way, the entire map is divided by a large grid with the screen as the unit. As shown:

 

When the player enters the map for the first time, it is calculated based on the coordinates of the spawn point. The screen grid where the player is located, if the screen grid is numbered 5. Then you need to set the view point on the nine grids 1-9, and push the map objects on all the small grids on the nine screen grids to the front end for display.

Because these 9 grids have set the field of view, if there is a change in the state of the map object on these nine grids or when a player hero enters these nine grids, the front end will receive the message push of the state change of the map object.

When the player's view point moves within the large grid 5, there is no need to modify the view point. In this way, no matter where the player's view point is on the screen grid, the status information of all objects displayed on the screen can be obtained. When the player's view point moves to other grids, recalculate to determine the new nine grids, set the view point on the new grid, and remove the view point on the old grid. In addition, the map objects on the old grid are removed from the field of view, and the map objects on the new grid are added to the field of view.

When the state of the map object changes, the large grid is calculated, and the players corresponding to all the points of view on the large grid are found for state synchronization.

In this way, the synchronization of the status and position of map objects such as heroes, monsters, and buildings on the big map is realized.

Guess you like

Origin blog.csdn.net/qq_19825249/article/details/108736686