Algorithm thinking MMORPG server scene aoi

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/lwtbn1/article/details/37961695

     MMORPG recently doing a project on the mobile platform, the server is responsible for the work, for the first time to do the development of this type of game servers, the work must be ready, comprehensive online information and their own ideas, summed up what the realization of AOI way, to talk about the following three aspects. Limited, the proposed implementation may be very awkward, but meet the general scene is still possible.
       1, an active attack on the player's monster detect
if there are N monster within the scene, M players, then a test is required to calculate the distance between the players and the N * M times monster, this method stupid. A new approach is: First, look at the scene of the game were divided in the following figure, the scene is divided into several squares, each grid has a unique number gridId, white star represents the current position of the player, the player is red grid on behalf of the yellow grid area is eight adjacent areas, mesh objects for each server maintains two lists: monsterList and playerList, two lists recorded monsters and players in the current grid.

 

     When the player enters the scene, the player currently resides mesh id (curGridId) arranged gridId red mesh, 8 mesh yellow gridId placed around the player adjacentGridList. Specific monster attack detection process:

 

 

     The benefits of this process are:

     (1) only if the player moves the player will be active attacks monster detection. Avoiding unnecessary detection;

     (2)    减少了计算怪物和玩家距离的次数,如一开始所述,如果场景内有N个怪物,M个玩家,则进行一次检测需要计算N*M次距离。该方法首先在场景更新线程中判断玩家是否从网格g1进入网格g2,判断时会与周围8个格子的中心点计算,最差的情况是计算8次,但是此处也可以优化,如果知道玩家的移动向量,则只进行一次计算就可以确定玩家是否跨越了格子。怪物攻击玩家检测线程中,只计算玩家当前所在的格子中的怪物与玩家的距离,计算次数为(假设每个格子中怪物和玩家平均分布):(N/格子数量)*(M/格子数量)。

     2、怪物移动控制
     游戏中会单独启动一个线程(T_MonsterMove)去执行怪物移动的操作,该线程属于定时线程,以1秒或更短的频率根据怪物的类型更新怪物的位置。并将移动后怪物的坐标信息发送给客户端,T_MonsterMove线程并不需要和主逻辑线程进行同步操作,因为T_MonsterMove线程中移动的怪物都是处于服务器托管的,怪物主动攻击线程检测到玩家进入怪物攻击范围后,会将该怪物从服务器托管列表中移除,交由客户端托管。
     3、怪物和玩家移动等信息的广播范围
在游戏中,我们把格子的大小定义为玩家360度视野的范围。adjacentGridList保存的是玩家相邻的8个格子id和curGridId保存的是当前玩家所在的格子的id。以玩家移动信息为例,只需要将消息发送给这9个格子中的玩家即可。将信息发送给相邻的8个格子的玩家可以避免玩家突然闪进视野的情况发生。


Guess you like

Origin www.cnblogs.com/xuxinstyle/p/11210071.html