### 3.6 AOI模块单元测试
```go
package core
import (
"fmt"
"testing"
)
func TestNewAOIManager(t *testing.T) {
aoiMgr := NewAOIManager(100,300, 4, 200,450, 5)
fmt.Println(aoiMgr)
}
func TestAOIManagerSuroundGridsByGid(t *testing.T) {
aoiMgr := NewAOIManager(0,250, 5, 0,250, 5)
for k, _ := range aoiMgr.grids {
//得到当前格子周边的九宫格
grids := aoiMgr.GetSurroundGridsByGid(k)
//得到九宫格所有的IDs
fmt.Println("gid : ", k, " grids len = ", len(grids))
gIDs := make([]int, 0, len(grids))
for _, grid := range grids {
gIDs = append(gIDs, grid.GID)
}
fmt.Printf("grid ID: %d, surrounding grid IDs are %v\n", k, gIDs)
}
}
```
结果
```bash
AOIManagr:
minX:100, maxX:300, cntsX:4, minY:200, maxY:450, cntsY:5
Grids in AOI Manager:
Grid id: 1, minX:150, maxX:200, minY:200, maxY:250, playerIDs:map[]
Grid id: 5, minX:150, maxX:200, minY:250, maxY:300, playerIDs:map[]
Grid id: 6, minX:200, maxX:250, minY:250, maxY:300, playerIDs:map[]
Grid id: 12, minX:100, maxX:150, minY:350, maxY:400, playerIDs:map[]
Grid id: 19, minX:250, maxX:300, minY:400, maxY:450, playerIDs:map[]
Grid id: 7, minX:250, maxX:300, minY:250, maxY:300, playerIDs:map[]
Grid id: 8, minX:100, maxX:150, minY:300, maxY:350, playerIDs:map[]
Grid id: 10, minX:200, maxX:250, minY:300, maxY:350, playerIDs:map[]
Grid id: 11, minX:250, maxX:300, minY:300, maxY:350, playerIDs:map[]
Grid id: 15, minX:250, maxX:300, minY:350, maxY:400, playerIDs:map[]
Grid id: 18, minX:200, maxX:250, minY:400, maxY:450, playerIDs:map[]
Grid id: 0, minX:100, maxX:150, minY:200, maxY:250, playerIDs:map[]
Grid id: 3, minX:250, maxX:300, minY:200, maxY:250, playerIDs:map[]
Grid id: 4, minX:100, maxX:150, minY:250, maxY:300, playerIDs:map[]
Grid id: 14, minX:200, maxX:250, minY:350, maxY:400, playerIDs:map[]
Grid id: 16, minX:100, maxX:150, minY:400, maxY:450, playerIDs:map[]
Grid id: 2, minX:200, maxX:250, minY:200, maxY:250, playerIDs:map[]
Grid id: 9, minX:150, maxX:200, minY:300, maxY:350, playerIDs:map[]
Grid id: 13, minX:150, maxX:200, minY:350, maxY:400, playerIDs:map[]
Grid id: 17, minX:150, maxX:200, minY:400, maxY:450, playerIDs:map[]
gid : 3 grids len = 6
grid ID: 3, surrounding grid IDs are [3 2 4 8 7 9]
gid : 5 grids len = 6
grid ID: 5, surrounding grid IDs are [5 6 0 10 1 11]
gid : 6 grids len = 9
grid ID: 6, surrounding grid IDs are [6 5 7 1 11 0 10 2 12]
gid : 11 grids len = 9
grid ID: 11, surrounding grid IDs are [11 10 12 6 16 5 15 7 17]
gid : 18 grids len = 9
grid ID: 18, surrounding grid IDs are [18 17 19 13 23 12 22 14 24]
gid : 2 grids len = 6
grid ID: 2, surrounding grid IDs are [2 1 3 7 6 8]
gid : 4 grids len = 4
grid ID: 4, surrounding grid IDs are [4 3 9 8]
gid : 7 grids len = 9
grid ID: 7, surrounding grid IDs are [7 6 8 2 12 1 11 3 13]
gid : 8 grids len = 9
grid ID: 8, surrounding grid IDs are [8 7 9 3 13 2 12 4 14]
gid : 19 grids len = 6
grid ID: 19, surrounding grid IDs are [19 18 14 24 13 23]
gid : 22 grids len = 6
grid ID: 22, surrounding grid IDs are [22 21 23 17 16 18]
gid : 0 grids len = 4
grid ID: 0, surrounding grid IDs are [0 1 5 6]
gid : 1 grids len = 6
grid ID: 1, surrounding grid IDs are [1 0 2 6 5 7]
gid : 13 grids len = 9
grid ID: 13, surrounding grid IDs are [13 12 14 8 18 7 17 9 19]
gid : 14 grids len = 6
grid ID: 14, surrounding grid IDs are [14 13 9 19 8 18]
gid : 16 grids len = 9
grid ID: 16, surrounding grid IDs are [16 15 17 11 21 10 20 12 22]
gid : 17 grids len = 9
grid ID: 17, surrounding grid IDs are [17 16 18 12 22 11 21 13 23]
gid : 23 grids len = 6
grid ID: 23, surrounding grid IDs are [23 22 24 18 17 19]
gid : 24 grids len = 4
grid ID: 24, surrounding grid IDs are [24 23 19 18]
gid : 9 grids len = 6
grid ID: 9, surrounding grid IDs are [9 8 4 14 3 13]
gid : 10 grids len = 6
grid ID: 10, surrounding grid IDs are [10 11 5 15 6 16]
gid : 12 grids len = 9
grid ID: 12, surrounding grid IDs are [12 11 13 7 17 6 16 8 18]
gid : 15 grids len = 6
grid ID: 15, surrounding grid IDs are [15 16 10 20 11 21]
gid : 20 grids len = 4
grid ID: 20, surrounding grid IDs are [20 21 15 16]
gid : 21 grids len = 6
grid ID: 21, surrounding grid IDs are [21 20 22 16 15 17]
PASS
ok zinx/zinx_app_demo/mmo_game/core 0.002s
```
我们可以用我们的AOI地图验证一下,是一致的。
- 一、引言
- 1、写在前面
- 2、初探Zinx架构
- 二、初识Zinx框架
- 1. Zinx-V0.1-基础Server
- 2.Zinx-V0.2-简单的连接封装与业务绑定
- 三、Zinx框架基础路由模块
- 3.1 IRequest 消息请求抽象类
- 3.2 IRouter 路由配置抽象类
- 3.3 Zinx-V0.3-集成简单路由功能
- 3.4 Zinx-V0.3代码实现
- 3.5 使用Zinx-V0.3完成应用程序
- 四、Zinx的全局配置
- 4.1 Zinx-V0.4增添全局配置代码实现
- 4.2 使用Zinx-V0.4完成应用程序
- 五、Zinx的消息封装
- 5.1 创建消息封装类型
- 5.2 消息的封包与拆包
- 5.3 Zinx-V0.5代码实现
- 5.4 使用Zinx-V0.5完成应用程序
- 六、Zinx的多路由模式
- 6.1 创建消息管理模块
- 6.2 Zinx-V0.6代码实现
- 6.3 使用Zinx-V0.6完成应用程序
- 七、Zinx的读写分离模型
- 7.1 Zinx-V0.7代码实现
- 7.2 使用Zinx-V0.7完成应用程序
- 八、Zinx的消息队列及多任务机制
- 8.1 创建消息队列
- 8.2 创建及启动Worker工作池
- 8.3 发送消息给消息队列
- 8.4 Zinx-V0.8代码实现
- 8.5 使用Zinx-V0.8完成应用程序
- 九、Zinx的链接管理
- 9.1 创建链接管理模块
- 9.2 链接管理模块集成到Zinx中
- 9.3 链接的带缓冲的发包方法
- 9.4 注册链接启动/停止自定义Hook方法功能
- 9.5 使用Zinx-V0.9完成应用程序
- 十、Zinx的连接属性设置
- 10.1 给链接添加链接配置接口
- 10.2 链接属性方法实现
- 10.3 链接属性Zinx-V0.10单元测试
- 基于Zinx的应用案例
- 一、应用案例介绍
- 二、服务器应用基础协议
- 三、MMO多人在线游戏AOI算法
- 3.1 网络法实现AOI算法
- 3.2 实现AOI格子结构
- 3.3 实现AOI管理模块
- 3.4 求出九宫格
- 3.5 AOI格子添加删除操作
- 3.6 AOI模块单元测试
- 四、数据传输协议protocol buffer
- 4.1 简介
- 4.2 数据交换格式
- 4.3 protobuf环境安装
- 4.4 protobuf语法
- 4.5 编译protobuf
- 4.6 利用protobuf生成的类来编码
- 五、MMO游戏的Proto3协议
- 六、构建项目与用户上线
- 6.1 构建项目
- 6.2用户上线流程
- 七、世界聊天系统实现
- 7.1 世界管理模块
- 7.2 世界聊天系统实现
- 八、上线位置信息同步
- 九、移动位置与AOI广播(未跨越格子)
- 十、玩家下线
- 十一、移动与AOI广播(跨越格子)