网络游戏结构:游戏结构



就像修房子别管你用多贵砖头它只是块砖头而已并不是你想要房子砖头可以在开发时候随时换但是结构旦定下来就不好修改了

那么个基本却又efficient结构究竟是什么样呢?很简单所有你所需要就是引擎无限状态机和内存池这 3个东西点都不复杂你可以从头开始写:
首先是你引擎类需要些什么呢?
CEngine
{
public:
CEngine;
~CEngine;

Initialize;
Start;
Restart;
Pause;
Shutdown;

AddModule(CModuleBase *Module, moduleID, updateFrequency);

RemoveModule( moduleID);

};
这就是个引擎需要具备基本功能不要管图形也不要管网络什么都不要管只是负责加入个模块加入模块在指定时间内刷新这样网络也好图形也好人工智能也好都是以模块形式在初试化时候加入到引擎刷新列表里面

那么无限状态机又是什么呢无限状态机就是个给你储存状态不过可以储存所有你想要状态比如你在刷新网络时候接收到新玩家坐标那么就需要把坐标值修改那么坐标值放在哪呢?无限状态机里面在网络模块更新时候只需要写Set(\"坐标\", &myCurPos);然后在图形模块更新时候只需要写myCurPos = Get(\"坐标\")那么myCurPos值存放在什么地方呢?就是存放在内存池里面内存池在游戏时候被创建在游戏结束时候销毁提供游戏中所有数据存放而无限状态机则是提供串到数据指针映射

下面就来看看无限状态机实现:
首先你需要个内存池:
// MemoryMag.h
#ndef __MEMORY_MAG_H__
# __MEMORY_MAG_H__

# <stdio.h>
# <assert.h>

template < T>
CMemoryMag
{
public:
CMemoryMag( size);
~CMemoryMag;

T *GetNew;
Free(T *var);

private:

T *m_data;
*m_freed;
m_freedSize;
m_maxSize;
m_curSize;
};

template< T>
CMemoryMag<T>::CMemoryMag( size) :
m_maxSize(size),
m_curSize(0),
m_freedSize(0)
{
m_data = T[size];
assert(m_data != NULL);

m_freed = [size];
assert(m_freed != NULL);
}

template< T>
CMemoryMag<T>::~CMemoryMag
{
delete m_data;
delete m_freed;
}

template< T>
T *CMemoryMag<T>::GetNew
{
(!m_freedSize)
{
(m_curSize < m_maxSize)
&m_data[m_curSize];

NULL;
}

&m_data[m_freed[--m_freedSize]];
}

template< T>
CMemoryMag<T>::Free(T *var)
{
i,j;
found = 0;

for(j=0; j<m_freedSize; j)
(&m_data[m_freed[j]] var) found = 1;

(found) 0;

for(i=0; i<m_curSize; i)
{
(&m_data[i] var)
{
found = i;
;
}
}

(!found) 0;

(found != (m_curSize - 1))
{
m_freed[m_freedSize] = found;
1;
}

m_curSize --;
1;
}

#end

//然后你需要个hash表
//hasher.h
#ndef __HASHER_H__
# __HASHER_H__

# \"MemoryMag.h\"

CHasher
{
public:
CHasher( size);
~CHasher;

Enter(const char *pStr, void *node);
Remove(const char *pStr);
void *Find(const char *pStr);

private:

GetHash(const char *pStr);

typedef struct hashNode
{
char *name;
void *ptr;
struct hashNode *next;
} HASHNODE;

CMemoryMag<HASHNODE> *m_hashArr;
HASHNODE **m_hashTable;
m_h;
m_objTable;
m_size;
};

#end

//hasher.cpp
# \"Hasher.h\"
# <>

T =
{
1, 87, 49, 12, 176, 178, 102, 166, 121, 193, 6, 84, 249, 230, 44, 163,
14, 197, 213, 181, 161, 85, 218, 80, 64, 239, 24, 226, 236, 142, 38, 200,
110, 177, 104, 103, 141, 253, 255, 50, 77, 101, 81, 18, 45, 96, 31, 222,
25, 107, 190, 70, 86, 237, 240, 34, 72, 242, 20, 214, 244, 227, 149, 235,
97, 234, 57, 22, 60, 250, 82, 175, 208, 5, 127, 199, 111, 62, 135, 248,
174, 169, 211, 58, 66, 154, 106, 195, 245, 171, 17, 187, 182, 179, 0, 243,
132, 56, 148, 75, 128, 133, 158, 100, 130, 126, 91, 13, 153, 246, 216, 219,
119, 68, 223, 78, 83, 88, 201, 99, 122, 11, 92, 32, 136, 114, 52, 10,
138, 30, 48, 183, 156, 35, 61, 26, 143, 74, 251, 94, 129, 162, 63, 152,
170, 7, 115, 167, 241, 206, 3, 150, 55, 59, 151, 220, 90, 53, 23, 131,
125, 173, 15, 238, 79, 95, 89, 16, 105, 137, 225, 224, 217, 160, 37, 123,
118, 73, 2, 157, 46, 116, 9, 145, 134, 228, 207, 212, 202, 215, 69, 229,
27, 188, 67, 124, 168, 252, 42, 4, 29, 108, 21, 247, 19, 205, 39, 203,
233, 40, 186, 147, 198, 192, 155, 33, 164, 191, 98, 204, 165, 180, 117, 76,
140, 36, 210, 172, 41, 54, 159, 8, 185, 232, 113, 196, 231, 47, 146, 120,
51, 65, 28, 144, 254, 221, 93, 189, 194, 139, 112, 43, 71, 109, 184, 209,
};


hashstr(const char * s, maxn)
{
register unsigned char oh, h;
register unsigned char *p;
register i;



(!*s) 0;
p = (unsigned char *) s;
oh = T[*p];
h = (*(p) + 1) & 0xff;

for (i = maxn - 1; *p && --i >= 0; ) {
oh = T[oh ^ *p];
h = T[h ^ *(p)];
}

(oh << 8) + h;
}

inline CHasher::GetHash(const char *pStr)
{
hashstr(pStr, 16) & m_objTable;
}

CHasher::CHasher( size) :
m_hashArr(NULL),
m_size(size)
{
realSize;

for (realSize = 1; realSize < size; realSize *= 2);

m_objTable = realSize - 1;

m_hashArr = CMemoryMag<HASHNODE>(realSize);
m_hashTable = HASHNODE*[size];
}

CHasher::~CHasher
{
delete m_hashArr;
while(m_size --)
{
HASHNODE *next = m_hashTable[m_size];
while(next)
{
(next->name != NULL)
{
delete next->name;
next->name = NULL;
}
next = next->next;
}
}

delete m_hashTable;
}

char *GetNewStr(const char *pStr)
{
len(strlen(pStr));
(len >= 0)
{
char *p = char[len];
assert(p != NULL);
memcpy(p, pStr, len);
p;
}
NULL;
}

CHasher::Enter(const char *pStr, void *node)
{
HASHNODE *Node = m_hashArr->GetNew;

n = GetHash(pStr);

Node->next = m_hashTable[n];

Node->ptr = node;
Node->name = GetNewStr(pStr);
Node->next = m_hashTable[n];
m_hashTable[n] = Node;
1;
}

void *CHasher::Find(const char *pStr)
{
n = GetHash(pStr);
HASHNODE *node = m_hashTable[n];
while(node)
{
(!strcmp(node->name, pStr)) node->ptr;
node = node->next;
}
NULL;
}

CHasher::Remove(const char *pStr)
{
HASHNODE *prev, *curr;

m_h = GetHash(pStr);
curr = m_hashTable[m_h];
prev = 0;

while (curr) {
(!strcmp(curr->name, pStr))
{
(prev) prev->next = curr->next;
delete curr->name;
curr->name = NULL;
m_hashArr->Free(curr);
1;
}
prev = curr;
curr = curr->next;
}
0;
}
//然后看看FSM(Finite Status Machine)
//fsm.h
// Finite State Machine
#ndef __MYFSM_H__
# __MYFSM_H__

# FSM_D (CFiniteStateMachine::GetInstance)
# MAX_VARIABLE_NUM 512

# \"Hasher.h\"

CFiniteStateMachine
{
public:
CFiniteStateMachine;
~CFiniteStateMachine;

CFiniteStateMachine *GetInstance;

Set(const char *varName, void *varvalue);

void* Get(const char *varName);

private:

CHasher m_hasher;
CFiniteStateMachine *m_myInstance;
};

#end

//以及FSM实现
# \"FSM.h\"

CFiniteStateMachine *CFiniteStateMachine::m_myInstance = NULL;

CFiniteStateMachine *CFiniteStateMachine::GetInstance
{
(m_myInstance NULL)
{
m_myInstance = CFiniteStateMachine;
m_myInstance;
}
m_myInstance;
}

CFiniteStateMachine::CFiniteStateMachine :
m_hasher(MAX_VARIABLE_NUM)
{
}

CFiniteStateMachine::~CFiniteStateMachine
{
}

CFiniteStateMachine::Set(const char *varName, void *varvalue)
{
(m_hasher.Find(varName) != NULL)
{
m_hasher.Remove(varName);
m_hasher.Enter(varName, varvalue);
}
m_hasher.Enter(varName, varvalue);
}

void* CFiniteStateMachine::Get(const char *varName)
{
void *var;
((var = m_hasher.Find(varName)) != NULL) var;
NULL;
}

至此个简单游戏引擎部分就已经基本完成了剩下事就是看看你这个是客户端还是服务器需要图形模块或者网络模块或者NPC的类都可以慢慢考虑了



Tags:  结构游戏观察记录 网络游戏结构

延伸阅读

最新评论

发表评论