MMORPG大型游戏设计与开发(服务器 游戏场景 搜索机)
双十一注定是忙碌的日子,所以到了现在我才将今天自己学习的内容拿出来跟大家分享。搜索机是我自己暂时取的名字,其实简单的说就是场景里提供搜索的一个工具,负责场景对象的范围搜索和获取。空洞的理论总是让人一头雾水,如果玩过游戏的朋友不妨想一想查看附近的玩家、选择附近的玩家、点击任务怪物名称就可以自动寻路打怪这些功能就大致有个印象了。
一张截图
搜索机
1、数据
1. 状态
typedef enum operator_status_enum {
kOperatorStatusContinue, //扫描继续
kOperatorStatusBreak, //中断本次扫描,并进入下一次扫描
kOperatorStatusStop, //停止扫描
} operator_status_t; //扫描状态
2. 基础
typedef operator_base_struct {
scene::Base *scene; //场景指针
int32_t zoneid; //区域ID
int32_t zoneradius; //搜索半径
bool scanhuman; //是否搜索玩家的列表
} operator_base_t; //基础操作扫描数据结构
2、实现
该实现为通用的父类接口,具体的类型搜索在子类中实现。
1. 初始化(init)
初始化扫描控制器,主要是初始化基础数据。
2. 操作前回调(on before)
回调需要在扫描之前的数据处理。
3. 判断区域是否需要扫描(is need scan)
根据区域ID判断是否需要扫描,如果不需要则不必再扫描。
4. 找到对象后的回调(on find object)
找到了一个对象的返回值,判断是否需要继续扫描。
5. 操作后的回调(on after)
扫描完成后的逻辑数据处理。
3、种类
1. 附近活跃的队友
typedef struct operator_active_teammates_strcut : public operator_base_struct {
world_position_t position; //位置信息
int16_t teamid; //队伍ID
uint32_t member_guid; //成员ID
float radius; //搜索半径
} operator_active_teammates_t; //活跃队友的数据结构
2. 面积有效状态
进入有效范围则对象会获得该状态。
typedef struct operator_AEimpact_struct : public operator_base_struct {
object::list_t *targets; //对象列表指针
float radius; //搜索半径
int32_t count; //数量
owner_impact impact; //拥有的特殊状态
impact_logic_t const *logic; //状态逻辑对象指针
object::Character *self; //自己的对象指针
world_position_t center_point; //中心点位置
} operator_AEimpact_t; //面积有效状态的数据结构
3. 面积有效技能
进入该技能左右范围后对象会获得该技能的效果。
typedef struct operator_AEskill_struct : public operator_base_struct {
object::list_t *targets; //对象列表指针
float radius; //搜索半径
skillinfo_t const *skillinfo; //技能信息对象指针
object::Character *self; //自己的对象指针
world_position_t center_point; //中心点位置
} operator_AEimpact_t; //面积有效技能的数据结构
4. 合符聊天要求的对象
typedef struct operator_chat_struct : public operator_base_struct {
packet::Base *packet; //网络包指针
int8_t chattype; //聊天的类型
int16_t guildid; //帮会ID
//其他数据...
} operator_chat_t; //聊天对象的数据结构
5. 附近的敌人
比如任务中自动打怪的搜索。
typedef struct operator_enemy_struct : public operator_base_struct {
object::Monster *monster; //怪物指针
float radius; //搜索半径
} operator_enemy_t; //敌人对象的数据结构
6. 扇形扫描技能有效范围
进入扇形区域有效的范围将被该技能作用。
typedef struct operator_sector_skill_struct : public operator_base_struct {
object::list_t *targets; //对象列表指针
float radius; //搜索半径
int32_t count; //最大可以搜索的对象数量
skillinfo_t const *skillinfo; //搜索主体的技能信息对象指针
object::Character *self; //自己的对象指针
world_position_t center_point; //中心点位置
} operator_sector_skill_t; //以扇形区域搜索技能范围的结构
7. 附近的队友
查询附近的队友信息。
typedef struct operator_teammates_struct : public operator_base_struct {
object::Monster *monster; //怪物对象指针
float radius; //搜索半径
int32_t count; //最大可以搜索的对象数量
int8_t type; //类型
bool only_noenemy; //是否只搜索没有敌人的队员
bool scan_allmonster; //是否扫描所有敌人
} operator_teammates_t; //队伍搜索结构
8. 附近的玩家
查看附近的玩家列表(名称、状态、等级等)。
typedef struct operator_character_struct : public operator_base_struct {
object::list_t *targets; //对象列表指针
float radius; //搜索半径
int32_t count; //最大可以搜索的对象数量
object::Special *self; //搜索主体
world_position_t center_point; //中心点位置
} operator_character_t; //特殊对象玩家搜索结构
9. 附近的陷阱
如果附近有陷阱,则对象在陷阱有效范围里将被陷阱作用。
typedef struct operator_trap_struct : public operator_base_struct {
object::list_t *targets; //对象列表指针
float radius; //搜索半径
int32_t count; //最大可以搜索的对象数量
object::Special *self; //搜索主体
world_position_t center_point; //中心点位置
} operator_trap_t; //特殊对象搜索陷阱的结构
算法(树查找和哈希查找)
1、基于二叉排序树的查找
叉排序树定义性质:
1 如果二叉树的左子树不为空,则左子树上的每一个节点的元素值都小于其对应的根节点元素的值。
2 如果二叉树的右子树不为空,则右子树上的每一个节点的元素值都大于其对应的根节点元素的值。
3 时二叉树的左子树和右子树同时满足1、2两项特性,即左子树和右子树都是一棵二叉树。
基于二叉排序树的查找算法分为插入操作和查找操作的两个部分。
插入操作不需要移动节点,仅需要移动节点指针。
code.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <malloc.h> /**
* 二叉排序树定义性质:
* 1 如果二叉树的左子树不为空,则左子树上的每一个节点的元素值都小于其对应的根节点元素的值。
* 2 如果二叉树的右子树不为空,则右子树上的每一个节点的元素值都大于其对应的根节点元素的值。
* 3 同时二叉树的左子树和右子树同时满足1、2两项特性,即左子树和右子树都是一棵二叉树。
*/ /**
* 基于二叉排序树的查找算法分为插入操作和查找操作的两个部分。
* 插入操作不需要移动节点,仅需要移动节点指针。
*/
typedef struct node_struct {
int32_t data;
struct node_struct *left, *right;
} node_t, *nodepointer_t; //二叉树的查找的结构 //二叉树查找
nodepointer_t binarytree_search(nodepointer_t tree, int32_t x);
//二叉树插入。如果树中不存在元素x,则将x插入到正确的位置并返回1,否则返回0
int32_t binarytree_insert(nodepointer_t *trees, int32_t x);
//中序遍历二叉排序树
void in_ordertraverse(nodepointer_t tree); int32_t main(int32_t argc, char *argv[]) {
nodepointer_t tree = NULL, pointer;
int32_t table[] = {, , , , , , , , , };
int32_t length = sizeof(table) / sizeof(table[]);
int32_t x, i;
//插入并生成二叉排序树
for (i = ; i < length; ++i)
binarytree_insert(&tree, table[i]);
printf("in order traverse list is: \n");
in_ordertraverse(tree);
printf("\nplease input a number you want search: ");
scanf("%d", &x);
pointer = binarytree_search(tree, x);
if (pointer != NULL) {
printf("%d is a member of array\n", x);
} else {
printf("%d is not a member of array\n", x);
}
return ;
} nodepointer_t binarytree_search(nodepointer_t tree, int32_t x) {
node_t *pointer = NULL;
if (tree != NULL) {
pointer = tree;
while (pointer != NULL) {
if (pointer->data == x) { //如果找到,则返回指向该节点的指针
return pointer;
} else if (x < pointer->data) { //如果关键字小于pointer指向的节点的值,则在左子树中查找
pointer = pointer->left;
} else if (x > pointer->data) { //如果关键字大于pointer指向的节点的值,则在右子树中查找
pointer = pointer->right;
}
}
}
return NULL;
} int32_t binarytree_insert(nodepointer_t *trees, int32_t x) {
node_t *pointer = NULL, *current = NULL, *parent = NULL;
current = *trees;
while (current != NULL) {
if (current->data == x) //如果二叉树中存在元素为x的节点,则返回0
return ;
parent = current; //parent指向current的前驱节点
if (x < current->data) { //如果关键字小于pointer指向节点的值,则在左子树中查找
current = current->left;
} else { //如果关键字大于pointer指向节点的值,则在右子树中查找
current = current->right;
}
}
pointer = (node_t *)malloc(sizeof(node_t)); //生成节点
if (NULL == pointer) return ; //内存不足
pointer->data = x;
pointer->left = NULL;
pointer->right = NULL;
if (!parent) { //如果二叉树为空,则第一节点成为根节点
*trees = pointer;
} else if (x < parent->data) { //如果x小于parent指向的节点元素,则x成为parent的左节点数据
parent->left = pointer;
} else { //如果x大于parent所指向的节点元素,则x成为parent的右节点数据
parent->right = pointer;
}
return ;
} void in_ordertraverse(nodepointer_t tree) {
if (tree) {
in_ordertraverse(tree->left); //中序遍历左子树
printf("%4d", tree->data); //访问根节点
in_ordertraverse(tree->right); //中序遍历右子树
}
}
result.
2、哈希查找
哈希表的查找方法与前面的基于线性和树形的查找算法不同,哈希表直接定位了元素所在位置,基本不需要逐个比较元素(除了冲突)。
该算法需要解决的两个问题:构造哈希表和处理冲突。
最常用的构造哈希表的方法是除留余数法,最为常用的处理冲突的方法是开放定址法和链地址法。
code.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <malloc.h>
#include <stdlib.h> /**
* 哈希表的查找方法与前面的基于线性和树形的查找算法不同,哈希表直接定位了元素所在
* 的位置,基本不需要逐个比较元素(除了冲突)。
* 该算法需要解决的两个问题:构造哈希表和处理冲突。
* 最常用的构造哈希表的方法是除留余数法,最为常用的处理冲突的方法是开放定址法和链地址法。
*/ typedef struct datatype_struct {
int32_t value; //元素值
int32_t repeatcount; //重复次数
} datatype_t; //元素类型结构 typedef struct hashtable_struct {
datatype_t *data;
int32_t length; //长度
int32_t number; //个数
} hashtable_t; //哈希表的结构 //构造哈希表并处理冲突
void create_hashtable(hashtable_t *hashtable,
int32_t m,
int32_t p,
int32_t hash[],
int32_t length);
//在哈希表中查找值为x的元素
int32_t hash_search(hashtable_t hashtable, int32_t x);
//求哈希表的平均查找长度
void hashASL(hashtable_t hashtable, int32_t m);
//哈希表打印
void displayhash(hashtable_t hashtable, int32_t m);
//数组打印
void displayarray(int32_t array[], int32_t length); int32_t main(int32_t argc, char *argv[]) {
int32_t hash[] = {, , , , , , , , , , };
hashtable_t hashtable;
int32_t m = , p = ;
int32_t length = sizeof(hash) / sizeof(hash[]);
int32_t position, x;
create_hashtable(&hashtable, m, p, hash, length);
displayhash(hashtable, m);
printf("please a number you want search: ");
scanf("%d", &x);
position = hash_search(hashtable, x);
printf("%d in array position: %d\n", x, position);
hashASL(hashtable, m);
return ;
} void create_hashtable(hashtable_t *hashtable,
int32_t m,
int32_t p,
int32_t hash[],
int32_t length) {
int32_t i, k = ;
int32_t sum, addr, di;
(*hashtable).data = (datatype_t *)malloc(m * sizeof(datatype_t));
if (NULL == (*hashtable).data) return; //not enough memory
(*hashtable).number = length; //元素个数
(*hashtable).length = m; //哈希表长度
for (i = ; i < m; ++i) { //哈希表初始化
(*hashtable).data[i].value = -;
(*hashtable).data[i].repeatcount = ;
}
for (i = ; i < length; ++i) { //构造哈希表并初始化
sum = ; //sum 记录冲突次数
addr = hash[i] % p; //利用除留余数法求哈希函数地址
di = addr;
//如果不冲突则将元素存储在表中
if (- == (*hashtable).data[addr].value) {
(*hashtable).data[addr].value = hash[i];
(*hashtable).data[addr].repeatcount = ;
} else { //用线性探测再散列法处理冲突
do {
di = (di + k) % m;
sum += ;
} while ((*hashtable).data[di].value != -);
(*hashtable).data[di].value = hash[i];
(*hashtable).data[di].repeatcount = sum + ;
}
}
} int32_t hash_search(hashtable_t hashtable, int32_t x) {
int32_t d, d1, m;
m = hashtable.length;
d = d1 = x % m;
while (hashtable.data[d].value != -) {
if (hashtable.data[d].value == x) { //如果找到x,则返回其所在位置
return d;
} else { //如果没有找到,则继续向后查找
d = (d + ) % m;
}
//如果已经遍历完所有位置还是没有找到,则返回0
if (d == d1) return ;
}
return ;
} void hashASL(hashtable_t hashtable, int32_t m) {
float avg = ;
int32_t i;
for (i = ; i < m; ++i)
avg = avg + hashtable.data[i].repeatcount;
avg = avg / hashtable.number;
printf("avg search length ASL: %2.f", avg);
printf("\n");
} void displayhash(hashtable_t hashtable, int32_t m) {
int32_t i;
printf("hash table address: ");
for (i = ; i < m; ++i) //输出哈希表的地址
printf("%-5d", i);
printf("\n");
printf("member value: ");
for (i = ; i < m; ++i) //输出哈希表的元素值
printf("%-5d", hashtable.data[i].value);
printf("\n");
printf("repeat times: ");
for (i = ; i < m; ++i) //冲突次数
printf("%-5d", hashtable.data[i].repeatcount);
printf("\n");
} void displayarray(int32_t array[], int32_t length) {
int32_t i;
for (i = ; i < length; ++i)
printf("%4d", array[i]);
printf("\n");
}
result.
MMORPG大型游戏设计与开发(服务器 游戏场景 搜索机)的更多相关文章
- MMORPG大型游戏设计与开发(游戏服务器 游戏场景 概述 updated)
我们在玩游戏的时候,我们进入游戏后第一眼往往都是看到游戏世界中的场景,当然除了个别例外,因为那些游戏将游戏场景隐藏了起来,如文字游戏中的地点一样.既然我们接触了游戏世界的核心,那么作为核心的场景又包括 ...
- MMORPG大型游戏设计与开发(服务器 AI 概述)
游戏世界中我们拥有许多对象,常见的就是角色自身以及怪物和NPC,我们可以见到怪物和NPC拥有许多的行为,比如说怪物常常见到敌对的玩家就会攻击一样,又如一些NPC来游戏世界中走来走去,又有些怪物和NPC ...
- MMORPG大型游戏设计与开发(服务器 游戏场景 动态场景与副本)
场景的内容讲解到今天算是暂时划上一个句号了,接下来为大家讲解的是AI部分(大型AI),如果有兴趣的朋友不妨持续关注这些文章,大家一起学习和进步.动态场景和副本是场景中特殊的类型,副本在这里想必已经是无 ...
- MMORPG大型游戏设计与开发(概述)updated
1.定义 MMORPG,是英文Massive(或Massively)Multiplayer Online Role-PlayingGame的缩写,即大型多人在线角色扮演游戏. 2.技术与知识 在这系列 ...
- MMORPG大型游戏设计与开发(客户端架构 part8 of vegine)
脚本模块是游戏设计中争论比较多的话题,那是因为作为脚本本身所带来的利弊.其实这都无关紧要,取舍是人必须学会的一项技能,如果你不会取舍那么就让趋势给你一个满意的答复.自从魔兽世界以及传奇(世界)问世以来 ...
- MMORPG大型游戏设计与开发(服务器 游戏场景 地图和区域)
地图的数据以及区域的信息是场景的重要组成部分,这些数据同时存在客户端和服务器,而且都是由编辑器生成的.那么保存的文件数据结构是怎样的?一张3D的场景地图又是怎样处理这些数据的?同时告诉大家这里同样只是 ...
- MMORPG大型游戏设计与开发(服务器 游戏场景 多线程)
多线程在随着cpu发展应用的是越来越多,游戏场景因为其在服务器所占的数据量与逻辑复杂度的原因必须依赖于它.为什么场景要采用多线程?场景的线程是怎样的?场景的线程又是如何创建的?场景的线程又是怎样管理的 ...
- MMORPG大型游戏设计与开发(服务器 游戏场景 核心详述)
核心这个词来的是多么的高深,可能我们也因为这个字眼望而却步,也就很难去掌握这部分的知识.之所以将核心放在最前面讲解,也可以看出它真的很重要,希望朋友们不会错过这个一直以来让大家不熟悉的知识,同我一起进 ...
- MMORPG大型游戏设计与开发(服务器 AI 事件)
AI中的事件与场景中的事件大致相同,都是由特定的条件触发的.只不过AI的事件与其他事件不同的是,对于AI的事件往往是根据不同的AI类型,和动态的触发条件下才产生的.其实不管AI多么智能,它对应的触发条 ...
- MMORPG大型游戏设计与开发(服务器 AI 基础接口)
一个模块都往往需要统一的接口支持,特别是对于非常大型的模块,基础结构的统一性非常重要,它往往决定了其扩展对象的通用性.昨天说了AI的基本概述以及组成,作为与场景模块中核心一样重要的地位,基础部分的设计 ...
随机推荐
- html5数字和颜色输入框
html5功能强大,数字和颜色输入框例子 效果:http://hovertree.com/code/html5/rxujb6g8.htm <!DOCTYPE html> <html& ...
- jQuery图片轮播特效
效果预览:http://hovertree.com/texiao/jquery/51/ 这款特效有缩略图,包含文字说明和链接,可以自动播放,也可以手动切换. 使用的jQuery库版本为1.12.3 , ...
- nodejs事件模块
nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...
- Webstorm常用的快捷键
WS的常用操作: 常用快捷键(Keymap/Eclipse): 复制当前行: Ctrl+Alt+↓ 向上/下移动当前行: Alt+↑/↓ 删除当前行: Ctrl+D 注释/取消当前行: Ctrl+/ ...
- HTML 定时页面跳转
有 2 种方法可以实现 html 的定时页面跳转,1.meta refresh 实现.2.JavaScript 实现. 1.通过 meta refresh 实现 3 秒后自动跳转到 http://ww ...
- How To Search and Restore files from Site Collection Recycle Bin
$sitecoll = Get-SPSite "http://wheresmydoc.findit.com" $sitecoll.RecycleBin | ?{$_.Title - ...
- ObjectAnimator.start()工作原理
分析下面一段代码的逻辑 objectAnimator.start(); 他会调用父类的start(),即ValueAnimator,我们分析valueAnimator.start()即可 ValueA ...
- 记录一次Quartz2D学习(一)
经常看点 drawRect的重写 但是不知道这究竟是神马 今天开始学习这一块的东西,更确切地说是深入 早在view的时候 就经常会调用layer的maskToBounds属性,其实 重写 drawR ...
- Servlet、Filter、Listener、Interceptor
首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角 色,Interceptor是某些MVC框架中的角色,比如Str ...