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

📄 gamemir.cpp

📁 是一个基于热血战国协议的网络游戏。现在脱机客户端先放出来给大家研究
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/////////////////////////////////////////////////////////////////////////////
// name : GameMir.cpp
// desc : 实现文件

#include "stdafx.h"
#include "..\ZhanGuoClient.h"
//#include "..\TalkToNpcDlg.h"
#include "..\ZhanGuoClientDlg.h"
#include "gamemir.h"
//#include "..\MainFrm.h"

//#include ".\gamemir.h"

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : ToUnicode
// 功能描述 : 汉字的Unicode编码
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
LPCWSTR ToUnicode(LPCSTR lpMultiByteStr, LPWSTR lpWideCharStr, int cchWideChar)
{
	int retval=MultiByteToWideChar(CP_ACP, 0, lpMultiByteStr, -1, lpWideCharStr, cchWideChar );
	lpWideCharStr[retval]=static_cast<WCHAR>(0);
	return lpWideCharStr;
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : ToAnsi
// 功能描述 : Ansi编码
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
LPCSTR ToAnsi(LPCWSTR lpWideCharStr, LPSTR lpMultiByteStr, int cchMultiByteChar)
{
	int retval=WideCharToMultiByte(CP_ACP, 0, lpWideCharStr, -1, lpMultiByteStr, cchMultiByteChar, NULL, NULL );
	lpMultiByteStr[retval]=static_cast<CHAR>(0);
	return lpMultiByteStr;
}

#define CLASS_GAME CGameMir

IMPLEMENT_DYNAMIC(CGameMir,CNet);


/////////////////////////////////////////////////////////////////////////////
// 函数名字 : ParseName
// 功能描述 : 分析角色的名字
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
std::string ParseName(std::string txt)
{
	size_t iFind=txt.find('/');
	if(iFind!=std::string::npos)
	{
		txt=txt.substr(0,iFind);
	}
	iFind=txt.find('\\');
	if(iFind!=std::string::npos)
	{
		txt=txt.substr(0,iFind);
	}
	return txt;
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : DecodeName
// 功能描述 : 译解角色的名字
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
int DecodeName(const char * in, char * out)
{
	int i = 0;
	while( in[i] != '\0' )
	{
		out[i] = (in[i] & 0x3f ) << 2;
		out[i] |= (in[i + 1] & 0xc0 ) >> 6;
		i ++;
	}
	out[i] = '\0';
	return 1;
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : InitData
// 功能描述 : 初始化游戏数据
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
void CGameMir::InitData(void)
{
	m_AreaStateValue   = 0;
	m_NeededAttackMode = 0;
	m_EatingItem       = 0;
	m_HpProtectTime    = 0;
	m_CharName.clear();

	ZeroMemory(&m_UseItems,sizeof(m_UseItems));
	
	//m_FrameWnd.UpdateSelfInfo();
	//m_FrameWnd.UpdateSelfPos();
	//m_FrameWnd.UpdateUseItem();
	//m_FrameWnd.ClearBag();
	//m_FrameWnd.ClearActors();

	m_BagItems.clear();     //
	m_DropedItems.clear();  //
	m_Magics.clear();       //
	m_ActorList.clear();    //
}


/////////////////////////////////////////////////////////////////////////////
// 函数名字 : CGameMir
// 功能描述 : 构造函数
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
CGameMir::CGameMir(CZhanGuoClientDlg& wnd)
: m_FrameWnd(wnd)
//, m_Action(*this)
{
	//
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : ~CGameMir
// 功能描述 : 拆构函数
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
CGameMir::~CGameMir()
{
	//
}

// CGameMir 成员函数

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : Login
// 功能描述 : 登陆游戏
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
void CGameMir::Login(const std::string& ServerAddress, const std::string& ServerName, UINT ServerPort, const std::string& Account, const std::string& Password)
{
	BOOL nRet = false; 
	InitData();

	Close();
	m_GameStage = GS_LOGIN;
	nRet = Create();
	m_ServerAddress = ServerAddress;
	m_ServerName    = ServerName; 
	g_ServerName    = ServerName;
	m_ServerPort    = ServerPort;
	m_Account       = Account;
	m_Password      = Password;

	m_FrameWnd.AddLog(crMessage,"\r\n");
	m_FrameWnd.AddLog(crGreen,"开始登录!==>%s[%s]\r\n", ServerName.c_str(), ServerAddress.c_str());

	//连接账号服务器
	nRet = Connect(ServerAddress.c_str(),ServerPort);
}

const unsigned char Decode6BitMask[5] = { 0xfc, 0xf8, 0xf0, 0xe0, 0xc0 };

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : fnEncode6BitBufA
// 功能描述 : 游戏数据包解密函数
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
int CGameMir::fnEncode6BitBufA(unsigned char *pszSrc, char *pszDest, int nSrcLen, int nDestLen)
{
	int				nDestPos	= 0;
	int				nRestCount	= 0;
	unsigned char	chMade      = 0; 
	unsigned char	chRest      = 0;

	for (int i = 0; i < nSrcLen; i++)
	{
		if (nDestPos >= nDestLen) break;

		chMade = ((chRest | (pszSrc[i] >> (2 + nRestCount))) & 0x3f);
		chRest = (((pszSrc[i] << (8 - (2 + nRestCount))) >> 2) & 0x3f);

		nRestCount += 2;

		if (nRestCount < 6)
			pszDest[nDestPos++] = chMade + 0x3c;
		else
		{
			if (nDestPos < nDestLen - 1)
			{
				pszDest[nDestPos++]	= chMade + 0x3c;
				pszDest[nDestPos++]	= chRest + 0x3c;
			}
			else
				pszDest[nDestPos++] = chMade + 0x3c;

			nRestCount	= 0;
			chRest		= 0;
		}
	}

	if (nRestCount > 0)
		pszDest[nDestPos++] = chRest + 0x3c;

	//pszDest[nDestPos] = '\0';

	return nDestPos;
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : fnDecode6BitBufA
// 功能描述 : 游戏数据包加密函数
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
int CGameMir::fnDecode6BitBufA(const char *pszSrc, char *pszDest, int nDestLen)
{
	int				nLen = strlen((const char *)pszSrc);
	int				nDestPos = 0, nBitPos = 2;
	int				nMadeBit = 0;
	unsigned char	ch, chCode, tmp;

	for (int i = 0; i < nLen; i++)
	{
		if ( pszSrc[i]==0 ) break;

		if ((pszSrc[i] - 0x3c) >= 0)
			ch = pszSrc[i] - 0x3c;
		else
		{
			continue;
		}

		if (nDestPos >= nDestLen) break;

		if ((nMadeBit + 6) >= 8)
		{
			chCode = (tmp | ((ch & 0x3f) >> (6 - nBitPos)));
			pszDest[nDestPos++] = chCode;

			nMadeBit = 0;

			if (nBitPos < 6) 
				nBitPos += 2;
			else
			{
				nBitPos = 2;
				continue;
			}
		}

		tmp = ((ch << nBitPos) & Decode6BitMask[nBitPos - 2]);

		nMadeBit += (8 - nBitPos);
	}

	//pszDest[nDestPos] = '\0';

	return nDestPos;
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : OnConnect
// 功能描述 : 连接游戏
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
void CGameMir::OnConnect(int nErrorCode)
{
	MMSG msg;
	ZeroMemory(&msg,sizeof(msg));
	switch(m_GameStage)
	{
	case GS_LOGIN: SendLogin(m_Account, m_Password); break; //发送--登陆游戏
	case GS_SEL:   SendQueryChr();                   break; //发送--分析角色
/*
	case GS_GAME://进入游戏
		{
			m_FrameWnd.AddLog(crMessage,"游戏服务器连接成功!\n");
			char buf[0x100];
			sprintf(buf,"**%s/%s/%s/%d/0",m_Account.c_str(),m_CharName.c_str(),m_SID.c_str(),MIR2_VERSION);
			m_FrameWnd.AddLog(crDebug,"%s\n",buf);
			SendMsg(*((MMSG*)buf),&buf[sizeof(MMSG)]);
		}
		//m_SettingMgr.Load(m_ServerName,m_CharName);
		m_Sets.Load();*/
		break;
	}
	CNet::OnConnect(nErrorCode);
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : OnResult
// 功能描述 : 返回结果
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
void CGameMir::OnResult(const char * result)
{
//	if tagstr = 'PWR'  then BoNextTimePowerHit := TRUE;  //促澜锅俊 powerhit阑 锭副 荐 乐澜...
//	if tagstr = 'LNG'  then BoCanLongHit := TRUE;
//	if tagstr = 'ULNG' then BoCanLongHit := FALSE;
//	if tagstr = 'WID'  then BoCanWideHit := TRUE;
//	if tagstr = 'UWID' then BoCanWideHit := FALSE;
//	if tagstr = 'CRS'  then BoCanCrossHit := TRUE;
//	if tagstr = 'UCRS' then BoCanCrossHit := FALSE;
//	// 2003/07/15 脚痹公傍 眠啊
//	if tagstr = 'TWN'  then BoCanTwinHit := TRUE;
//	if tagstr = 'UTWN' then BoCanTwinHit := FALSE;
//	if tagstr = 'FIR'  then begin
//BoNextTimeFireHit := TRUE;  //堪拳搬捞 技泼等 惑怕
//LatestFireHitTime := GetTickCount;
//	//Myself.SendMsg (SM_READYFIREHIT, Myself.XX, Myself.YY, Myself.Dir, 0, 0, '', 0);
//	end;
//	if tagstr = 'STN'  then BoCanStoneHit := TRUE;
//	if tagstr = 'USTN' then BoCanStoneHit := FALSE;
//
//	if tagstr = 'UFIR' then BoNextTimeFireHit := FALSE;
//	if tagstr = 'GOOD' then begin
//ActionLock := FALSE;
//	Inc (ReceiveCount);
//i:=StrToInt(data);
//ServerTime:=i div 1000;
//	end;
//	if tagstr = 'FAIL' then begin
//		ActionFailed;
//ActionLock := FALSE;
//	Inc (ReceiveCount);
//i:=StrToInt(data);
//ServerTime:=i div 1000;
//	end;
//	if data <> '' then begin
//		CheckSpeedHack (Str_ToInt(data, 0));
//	end;
//	exit;
//	end;
//	if Length(datablock) < DEFBLOCKSIZE then begin
//		if datablock[1] = '=' then begin
//data := Copy (datablock, 2, Length(datablock)-1);
//	if data = 'DIG' then begin
//		Myself.BoDigFragment := TRUE;
//	end;
//	end;
//	exit;
/*
	std::vector<std::string> sl;
	char buf[32];
	strncpy(buf,result,32);
	SplitString( buf, "/", sl );
	if ( sl[0]=="LNG" )
		m_FrameWnd.AddLog(crDebug,"刺杀开启\n");
	else if ( sl[0]=="ULNG" )
		m_FrameWnd.AddLog(crDebug,"刺杀关闭\n");
	else// if ( !m_Action.OnResult(result) )
		m_FrameWnd.AddLog(crDebug,"%s\n",result);
*/
//	m_Action.OnResult(result);
}

/////////////////////////////////////////////////////////////////////////////
// 函数名字 : OnMessage
// 功能描述 : 处理游戏数据包
// 参数     : void
// 返回值   : NULL
/////////////////////////////////////////////////////////////////////////////
void CGameMir::OnMessage(MMSG& msg, char* body)
{
	switch(msg.wCmd)
	{
	case SM_PASSOK_SELECTSERVER:	RecvPassOKSelectServer(msg, body);	break; //账号_选择角色服务器
	case SM_PASSWD_FAIL:            RecvPassWDFail(msg, body);          break; //账号_密码_失败
	case SM_SELECTSERVER_OK:        RecvSelectSreverOK(msg, body);      break;
	case SM_QUERYCHR:				RecvQueryChr(msg, body);            break; //角色-请求角色列表-成功
	case SM_QUERYCHR_FAIL:			RecvQueryChrFail(msg, body);        break; //角色-请求角色列表-失败
	case SM_STARTPLAY: break;
	case SM_SENDNOTICE: break;
	case SM_SYSMESSAGE: break;
	case SM_WHISPER: break;
	case SM_HEAR: break;
	case SM_GUILDMESSAGE: break;
	case SM_TURN: break;
	case SM_WALK: break;
	case SM_RUN: break;
	case SM_DISAPPEAR: break;
	case SM_ABILITY: break;
	case SM_NEWMAP: break;
	case SM_LOGON: break;
	case SM_HIT: break;
	case SM_BACKSTEP: break;
	case SM_MERCHANTSAY: break;
	case SM_HEAVYHIT: break;
	case SM_POWERHIT: break;
	case SM_LONGHIT: break;
	case SM_WIDEHIT: break;
	case SM_FIREHIT: break;
	case SM_BIGHIT: break;
	case SM_USERNAME: break;
	case SM_WINEXP: break;
	case SM_MAPDESCRIPTION: break;
	case SM_BAGITEMS: break;
	case SM_SENDUSEITEMS: break;
	case SM_CLEAROBJECTS: break;
	case SM_CHANGEMAP: break;
	case SM_HEALTHSPELLCHANGED: break;
	case SM_NOWDEATH: break;
	case SM_DEATH: break;
	case SM_SENDMYMAGIC: break;
	case SM_STRUCK: break;
	case SM_DURACHANGE: break;
	case SM_ADDITEM: break;
	case SM_FEATURECHANGED: break;
	case SM_CHANGELIGHT: break;
	case SM_AREASTATE: break;
	case SM_GOLDCHANGED: break;
	case SM_READMINIMAP_OK: break;
	case SM_READMINIMAP_FAIL: break;
	case SM_ALIVE: break;
	case SM_EAT_OK: break;
	case SM_EAT_FAIL: break;
	case SM_ITEMSHOW: break;
	case SM_ITEMHIDE: break;
	case SM_SENDGOODSLIST: break;
	case SM_MERCHANTDLGCLOSE: break;
	case SM_BUYITEM_SUCCESS: break;
	case SM_WEIGHTCHANGED: break;
	case SM_SENDDETAILGOODSLIST: break;

		/*
		ROUTINE_MM_ENTRY(SM_PASSOK_SELECTSERVER);
		ROUTINE_MM_ENTRY(SM_PASSWD_FAIL);
		ROUTINE_MM_ENTRY(SM_SELECTSERVER_OK);
		ROUTINE_MM_ENTRY(SM_QUERYCHR);
		ROUTINE_MM_ENTRY(SM_STARTPLAY);
		ROUTINE_MM_ENTRY(SM_SENDNOTICE);
		ROUTINE_MM_ENTRY(SM_SYSMESSAGE);

⌨️ 快捷键说明

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