⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamedata.cpp

📁 是一个基于热血战国协议的网络游戏。现在脱机客户端先放出来给大家研究
💻 CPP
字号:

/*
 * name: GameData.cpp
 *
 * desc: 游戏数据(门口,地图,小地图,捆道具)等文本文件的读取
 *
*/

#include "StdAfx.h"
#include "GameMir.h"
#include ".\gamemir.h"

#define DIR_PRE

/*
=======================================================================
函数名       : CGameData
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
CGameData::CGameData(void)
{
	LoadDoorLink(); //导入门口
	LoadMapTitle(); //导入地图
	LoadMiniMap();  //导入小地图
	LoadBindItems();//导入捆道具
}

/*
=======================================================================
函数名       : ~CGameData
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
CGameData::~CGameData(void)
{

}

/*
=======================================================================
函数名       : GetMap
功能描述     : 得到地图
参数         : const std::string& name
参数         : _TMAP **map
返回值       : bool
=======================================================================
*/
bool CGameData::GetMap(const std::string& name, _TMAP **map)
{
	for(tMapList::iterator pos=m_Maps.begin();pos!=m_Maps.end();pos++)
	{
		if ( pos->mapname == name )
		{
			*map=&(*pos);
			return true;
		}
	}
	*map=NULL;
	return false;
}

/*
=======================================================================
函数名       : InsertMap 
功能描述     : 插入地图
参数         : std::string name
返回值       : _TMAP *
=======================================================================
*/
_TMAP *CGameData::InsertMap(std::string name)
{
	_TMAP *pmap;
	if(GetMap(name,&pmap))
		return pmap;

	_TMAP map;
	map.mapname=name;
	m_Maps.push_back(map);//在地图向量容器插入一个新的

	if(GetMap(name,&pmap))
		return pmap;

	return NULL;
}

/*
=======================================================================
函数名       : GetMainDir
功能描述     : 得到程序主目录
参数         : void
返回值       : static std::string 
=======================================================================
*/
static std::string GetMainDir(void)
{
	char buf[MAX_PATH];
	ZeroMemory(buf,sizeof(buf));
	GetModuleFileName(0,buf,sizeof(buf));
	long len=strlen(buf);

	while ( len>0 ) 
	{ 
		if ( buf[len] == '\\' ) 
		{ 
			buf[len] = static_cast<char>(0); 
			break; 
		} 
		--len; 
	}
	strcat(buf,"\\");

	return std::string(buf);
}

/*
=======================================================================
函数名       : LoadDoorLink 
功能描述     : 导入门口数据文件
参数         : void
返回值       : void
=======================================================================
*/
void CGameData::LoadDoorLink(void)
{
	// doorlink.ini
	CFile doorlink_ini((GetMainDir()+DIR_PRE"doorlink.ini").c_str(),CFile::typeBinary|CFile::modeRead|CFile::shareDenyWrite);//2007-07-20		
	long flen = doorlink_ini.GetLength();
	char* pBuf = new char[flen+1];
	ZeroMemory(pBuf,flen+1);

	doorlink_ini.Read(pBuf,flen);
	pBuf[flen] = static_cast<char>(0);

	std::wstring text;
	LPWSTR UBuf = new WCHAR[flen+1];;
	text = ToUnicode(pBuf,UBuf,flen);
	delete[]pBuf;
	delete[]UBuf;

	std::vector<std::wstring> Lines;
	const boost::wregex reSplit(L"\\s*\\n\\s*",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reDoorLink(L"^(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)(?:\\s+\\[>>(.+?)\\])?$",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reDoorDist(L"^(.+?)\\s+(\\d+)\\s+(\\d+)\\s+\\1\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)$",boost::regbase::normal|boost::regbase::icase);
	boost::regex_split(std::back_inserter(Lines),text,reSplit);
	boost::wsmatch match;

	int cnt1 = 0,cnt2 = 0;
	char cBuf[STRING_BUFFER_LENGTH+1];
	for(std::vector<std::wstring>::iterator pos=Lines.begin();pos!=Lines.end();pos++)
	{
		if (boost::regex_match( *pos, match, reDoorLink))
		{
			_TDOORLINK dlink;
			dlink.source.mapname= ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH);
			dlink.source.pos.x  = boost::lexical_cast<int>(match.str(2));
			dlink.source.pos.y  = boost::lexical_cast<int>(match.str(3));
			dlink.dest.mapname  = ToAnsi(match.str(4).c_str(),cBuf,STRING_BUFFER_LENGTH);
			dlink.dest.pos.x    = boost::lexical_cast<int>(match.str(5));
			dlink.dest.pos.y    = boost::lexical_cast<int>(match.str(6));
			dlink.dlg_name      = ToAnsi(match.str(7).c_str(),cBuf,STRING_BUFFER_LENGTH);

			_TMAP *map=NULL;

			map=InsertMap(dlink.dest.mapname);
			if (map==NULL)
			{
				throw "添加地图失败1";
			}
			map=InsertMap(dlink.source.mapname);
			if (map==NULL)
			{
				throw "添加地图失败2";
			}
			map->DoorLinks.push_back(dlink);
			cnt1++;
		}
		else if (boost::regex_match( *pos, match, reDoorDist))
		{
			_TDOORDIST ddist;
			ddist.mapname = ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH);
			ddist.pos1.x  = boost::lexical_cast<int>(match.str(2));
			ddist.pos1.y  = boost::lexical_cast<int>(match.str(3));
			ddist.pos2.x  = boost::lexical_cast<int>(match.str(4));
			ddist.pos2.y  = boost::lexical_cast<int>(match.str(5));
			ddist.dist    = boost::lexical_cast<int>(match.str(6));

			_TMAP *map=NULL;

			map=InsertMap(ddist.mapname);
			if (map==NULL)
			{
				throw "添加地图失败";
			}
			map->DoorDists.push_back(ddist);
			cnt2++;
		}
	}
}

/*
=======================================================================
函数名       : LoadMapTitle 
功能描述     : 导入地图数据文件
参数         : void
返回值       : void
=======================================================================
*/
void CGameData::LoadMapTitle(void)
{
	// mapinfo.txt
	CFile mapinfo_txt((GetMainDir()+DIR_PRE"mapinfo.txt").c_str(),CFile::typeBinary|CFile::modeRead|CFile::shareDenyWrite);//2007-07-20	
	long flen=mapinfo_txt.GetLength();
	char*pBuf=new char[flen+1];
	ZeroMemory(pBuf,flen+1);
	mapinfo_txt.Read(pBuf,flen);
	pBuf[flen]=static_cast<char>(0);

	std::wstring text;
	LPWSTR UBuf=new WCHAR[flen+1];;
	text=ToUnicode(pBuf,UBuf,flen);
	delete[]pBuf;
	delete[]UBuf;

	std::vector<std::wstring> Lines;
	const boost::wregex reSplit(L"\\s*\\n\\s*",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reMapTitle(L"^\\[(.+?)\\s+(.+?)\\s+.+$",boost::regbase::normal|boost::regbase::icase);
	boost::regex_split(std::back_inserter(Lines),text,reSplit);
	boost::wsmatch match;

	char cBuf[STRING_BUFFER_LENGTH+1];
	for(std::vector<std::wstring>::iterator pos=Lines.begin();pos!=Lines.end();++pos)
	{
		if (boost::regex_match( *pos, match, reMapTitle))
		{
			_TMAP *map=NULL;
			map=InsertMap(ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH));
			if (map==NULL)
			{
				throw "添加地图失败";
			}
			map->maptitle=ToAnsi(match.str(2).c_str(),cBuf,STRING_BUFFER_LENGTH);
		}
	}
}

/*
=======================================================================
函数名       : LoadMiniMap 
功能描述     : 导入小地图数据文件
参数         : void
返回值       : void
=======================================================================
*/
void CGameData::LoadMiniMap(void)
{
	// MiniMap.txt
	CFile minimap_txt((GetMainDir()+DIR_PRE"MiniMap.txt").c_str(),CFile::typeBinary|CFile::modeRead|CFile::shareDenyWrite);//2007-07-20	
	long flen=minimap_txt.GetLength();
	char*pBuf=new char[flen+1];
	ZeroMemory(pBuf,flen+1);
	minimap_txt.Read(pBuf,flen);
	pBuf[flen]=static_cast<char>(0);

	std::wstring text;
	LPWSTR UBuf=new WCHAR[flen+1];;
	text=ToUnicode(pBuf,UBuf,flen);
	delete[]pBuf;
	delete[]UBuf;

	std::vector<std::wstring> Lines;
	const boost::wregex reSplit(L"\\s*\\n\\s*",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reMapTitle(L"^(\\S+)\\s+(\\d+)$",boost::regbase::normal|boost::regbase::icase);
	boost::regex_split(std::back_inserter(Lines),text,reSplit);
	boost::wsmatch match;

	m_MiniMaps.clear();
	char cBuf[STRING_BUFFER_LENGTH+1];
	for(std::vector<std::wstring>::iterator pos=Lines.begin();pos!=Lines.end();pos++)
	{
		if (boost::regex_match( *pos, match, reMapTitle))
		{
			std::pair<std::string,WORD> item;
			item.first=ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH);
			item.second=boost::lexical_cast<WORD>(match.str(2));
			m_MiniMaps.insert(item);
		}
	}
}

/*
=======================================================================
函数名       : LoadBindItems 
功能描述     : 导入捆道具数据文件
参数         : void
返回值       : void
=======================================================================
*/
void CGameData::LoadBindItems(void)
{
	// BindItems.txt
	CFile BindItems_txt((GetMainDir()+DIR_PRE"BindItems.txt").c_str(),CFile::typeBinary|CFile::modeRead|CFile::shareDenyWrite);//2007-07-20	
	long flen=BindItems_txt.GetLength();
	char*pBuf=new char[flen+1];
	ZeroMemory(pBuf,flen+1);
	BindItems_txt.Read(pBuf,flen);
	pBuf[flen]=static_cast<char>(0);

	std::wstring text;
	LPWSTR UBuf=new WCHAR[flen+1];;
	text=ToUnicode(pBuf,UBuf,flen);
	delete[]pBuf;
	delete[]UBuf;

	std::vector<std::wstring> Lines;
	const boost::wregex reSplit(L"\\s*\\n\\s*",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reBindItem(L"^(.+?)\\s+(.+)$",boost::regbase::normal|boost::regbase::icase);
	boost::regex_split(std::back_inserter(Lines),text,reSplit);
	boost::wsmatch match;
	m_BandItems.clear();

	char cBuf[STRING_BUFFER_LENGTH+1];
	for(std::vector<std::wstring>::iterator pos=Lines.begin();pos!=Lines.end();pos++)
	{
		if (boost::regex_match( *pos, match, reBindItem))
		{
			std::pair<std::string,std::string> item;
			item.first  = ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH);
			item.second = ToAnsi(match.str(2).c_str(),cBuf,STRING_BUFFER_LENGTH);
			m_BandItems.insert(item);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -