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[] = {32, 13, 23, 56, 53, 67, 65, 88, 25, 36};
  int32_t length = sizeof(table) / sizeof(table[0]);
  int32_t x, i;
  //插入并生成二叉排序树
  for (i = 0; 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 0;
}

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 0;
    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 0; //内存不足
  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 1;
}

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[] = {45, 35, 23, 17, 83, 27, 91, 33, 56, 78, 99};
  hashtable_t hashtable;
  int32_t m = 11, p = 11;
  int32_t length = sizeof(hash) / sizeof(hash[0]);
  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 0;
}

void create_hashtable(hashtable_t *hashtable, 
                      int32_t m, 
                      int32_t p, 
                      int32_t hash[], 
                      int32_t length) {
  int32_t i, k = 1;
  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 = 0; i < m; ++i) { //哈希表初始化
    (*hashtable).data[i].value = -1;
    (*hashtable).data[i].repeatcount = 0;
  }
  for (i = 0; i < length; ++i) { //构造哈希表并初始化
    sum = 0; //sum 记录冲突次数
    addr = hash[i] % p; //利用除留余数法求哈希函数地址
    di = addr;
    //如果不冲突则将元素存储在表中
    if (-1 == (*hashtable).data[addr].value) {
      (*hashtable).data[addr].value = hash[i];
      (*hashtable).data[addr].repeatcount = 1;
    } else { //用线性探测再散列法处理冲突
      do {
        di = (di + k) % m;
        sum += 1;
      } while ((*hashtable).data[di].value != -1);
      (*hashtable).data[di].value = hash[i];
      (*hashtable).data[di].repeatcount = sum + 1;
    }
  }
}

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 != -1) {
    if (hashtable.data[d].value == x) { //如果找到x,则返回其所在位置
      return d;
    } else { //如果没有找到,则继续向后查找
      d = (d + 1) % m;
    }
    //如果已经遍历完所有位置还是没有找到,则返回0
    if (d == d1) return 0;
  }
  return 0;
}

void hashASL(hashtable_t hashtable, int32_t m) {
  float avg = 0;
  int32_t i;
  for (i = 0; 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 = 0; i < m; ++i) //输出哈希表的地址
    printf("%-5d", i);
  printf("\n");
  printf("member value: ");
  for (i = 0; i < m; ++i) //输出哈希表的元素值
    printf("%-5d", hashtable.data[i].value);
  printf("\n");
  printf("repeat times: ");
  for (i = 0; i < m; ++i) //冲突次数
    printf("%-5d", hashtable.data[i].repeatcount);
  printf("\n");
}

void displayarray(int32_t array[], int32_t length) {
  int32_t i;
  for (i = 0; i < length; ++i)
    printf("%4d", array[i]);
  printf("\n");
}

    result.

posted @ 2014-11-11 21:05  恋月  阅读(2219)  评论(5编辑  收藏  举报