MMORGP大型游戏设计与开发(客户端架构 part13 of vegine)

一些数据是需要不断改动的,程序不可能因为这些改动而不厌其烦的去改动代码,早期的这种做法就成了程序员们最悲哀的痛苦。自从有了数据管理后,程序的世界逐渐清晰,这些烦恼也不再出现,不过若是要很好的管理数据可是一门需要不断摸索的学问。天龙八部/武侠世界中管理配置数据小到ini,大到mysql数据库,各种各样的数据都需要采用专有的模块与方法去实现。而在配置中,策划会比较喜欢由excel来配置一些游戏中的参数,所以才有了我们今天所说的文件数据模块,即一张txt。其实这种用法现在有不少游戏也正在使用,举个例子那便是金山西山居制作的大部分游戏。

CODE

  模块db下模块structs文件character.h -- 有关角色的配置表结构,由于表比较多,在这里只举这个例子

/**
 * PAP Engine ( -- )
 * $Id character.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-22 19:26:00
 * @uses vengine database character struct module
 *       cn: 角色相关数据定义
 */
#ifndef VENGINE_DB_STRUCT_CHARACTER_H_
#define VENGINE_DB_STRUCT_CHARACTER_H_

namespace character {

//主角种族
const uint16_t kRaceId = 101; //数据表中的id
typedef struct {
  uint8_t id;
  int32_t modelid; //模型ID
  int32_t hair; //头发
  int32_t face; //
  int32_t shoulder; //
  int32_t body; //身体
  int32_t hand; //
  int32_t foot; //
  uint32_t idleinterval; //休闲时间间隔
} race_t;

//主角头发模型
const uint16_t kHairId = 102;
typedef struct {
  uint16_t id;
  uint8_t race;
  const char* meshfile;
  const char* showname;
} hair_t;

//主角脸型
const uint16_t kFaceId = 103;
typedef struct {
  uint16_t id;
  uint8_t race;
  const char* meshfile;
  const char* showname;
} face_t;

//主角动作组
const uint16_t kActionSetId = 104;
const uint8_t kWeaponTypeMax = 14;
typedef struct {
  uint16_t id;
  const char* weaponset[kWeaponTypeMax]; //对应武器类型
  bool hideweapon; //是否隐藏武器
  uint32_t appoint_weaponid; //指定的武器ID
  const char* description; //描述
} actionset_t;

//主角特效
const uint16_t kEffectId = 105;
typedef struct {
  uint16_t id;
  uint16_t effect1;
  uint16_t effect2;
  const char* locator;
  int32_t soundid;
  const char* weaponlocator;
} effect_t;

//主角升级经验
const uint16_t kExpId = 106;
typedef struct {
  uint8_t id;
  uint32_t effectneed;
} exp_t;

//外形ID对应模型名称
const uint16_t kModelId = 107;
enum {
  kMountNumberMax = 20,
  kSoundNumberMax = 6, //数值根据地面的类型得出
};
typedef struct {
  uint16_t id;
  float fusetime; //动作融合时间
  const char* modelname; //模型文件
  int16_t soundid[kSoundNumberMax]; //不同地面跑步的声音
  const char* actionset_none; //不使用坐骑的动作文件
  const char* actionset_mount[kMountNumberMax]; //使用坐骑的动作文件
} model_t;

//头像
const uint16_t kHeadPortrait = 108;
typedef struct {
  uint16_t id;
  uint8_t race;
  const char* imagesetname;
} headportrait_t;

//坐骑表
const uint16_t kMountId = 109;
const uint8_t kMountEffectMax = 8;
typedef struct {
  const char* effectlocator; //特效绑定点
  const char* effectname; //特效名称
} mounteffect_t;

typedef struct {
  uint16_t id;
  float leaveground_height; //距离地面的距离,特殊坐骑用
  uint16_t modelid;
  int16_t character_actionindex; //人物骑乘时的动作组
  mounteffect_t effectinfo[kMountEffectMax]; //特效信息
} mount_t;

//称号表
const uint16_t kTitleInfoId = 110;
typedef struct {
  uint16_t id;
  uint8_t type;
  const char* color; //显示颜色
  bool defaultshow; //默认是否显示
  const char* man; //男性称号
  const char* woman; //女性称号
  const char* description; //描述
  int32_t timelimit; //时限
  int16_t effectid; //效果ID
  const char* reclaimcause; //回收原因
  int16_t groupid; //组合称号ID
} titleinfo_t;

//阵营
const uint16_t kCampDataId = 111;

//人物等级携带金钱上限
const uint16_t kLevelMoneyMaxId = 112;
typedef struct {
  uint8_t id;
  uint32_t moneymax;
  uint8_t level; //等级
} level_moneymax_t;

//改变发型消耗表
const uint16_t kHairStyleCostId = 113;
typedef struct {
  uint16_t id;
  int32_t itemid; //物品ID
  int32_t itemcount; //消耗物品数量
  uint8_t sex; //性别
} hairstyle_cost_t;

}; //namespace character

#endif //VENGINE_DB_STRUCT_CHARACTER_H_

  db模块 文件file.h

/**
 * PAP Engine ( -- )
 * $Id file.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-22 18:08:41
 * @uses vengine file database module
 *       cn: 文件数据模块
 */
#ifndef VENGINE_DB_FILE_H_
#define VENGINE_DB_FILE_H_

#include "vengine/config.h"

/** 注意此处引用了公用文件,文件数据库 **/
namespace pap_common_file {

class Database;

}; //namespace pap_common_file

namespace vengine_db {

class VENGINE_API File {

 public:
   typedef struct {
     uint32_t identify; //标示
     int32_t field_number; //列数
     int32_t record_number; //记录数
     int32_t string_block_size; //字符串区大小
   } file_head_t;
   
   typedef enum { //field type
     kTypeInt = 0,
     kTypeFloat = 1,
     kTypeString = 2,
   } field_type_enum;

   union field_data {
     float float_value;
     int32_t int_value;
     const char* string_value; //can't change excel value in memory
     field_data() {/** do nothing **/}
     field_data(float value) {float_value = value;}
     field_data(int32_t value) {int_value = value;}
     field_data(const char* value) {string_value = value;}
   };
   
 public:
   virtual const field_data* search_index_equal(int32_t index) const = 0;
   virtual const field_data* search_line_equal(int32_t line) const = 0;
   virtual const field_data* search_position(int32_t line, 
                                             int32_t column) const = 0;
   virtual const field_data* search_first_column_equal(
       int32_t column, 
       const field_data &value) const = 0;

 public:
   virtual const pap_common_file::Database* get_databasefile() const = 0;
   virtual int32_t get_id() const = 0;
   virtual int32_t get_field_number() const = 0;
   virtual int32_t get_record_number() const = 0;

};

}; //namespace vengine_db

#endif //VENGINE_DB_FILE_H_

  模块db 文件system.h

/**
 * PAP Engine ( -- )
 * $Id system.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-22 18:27:52
 * @uses vengine file database module
 */
#ifndef VENGINE_DB_SYSTEM_H_
#define VENGINE_DB_SYSTEM_H_

#include "vengine/config.h"
#include "vengine/kernel/node.h"
#include "vengine/db/file.h"

namespace vengine_db {

class VENGINE_API System : public vengine_kernel::Node {

VENGINE_KERNEL_DECLARE_DYNAMIC(vengine_db_System);

 public:
   virtual void open() = 0; //打开所有数据
   virtual void close() = 0; //关闭所有数据
   virtual const File* get(int32_t id) const = 0;

}; 

}; //namespace vengine_db

#endif //VENGINE_DB_SYSTEM_H_

SIMPLE

  天龙八部的表配置

  总结

 我在这里简述一下读取txt的基本原理,那就是以行为单位作为一个结构读取。每个格子为一个数据的基本单位,它的顺序为自左至右从上而下读取。这个会在以后在详细讲到,大家可以关注以后的讲解,新的框架中也集成了解析txt格式的文件数据处理。

原文地址:https://www.cnblogs.com/lianyue/p/3921742.html