📄 mirgame.cpp
字号:
BOOL CMirGame::CloseSelCharServer()
{
//LogSystem("断开角色服务器!");
m_MirSocket.CloseConnection(m_sSelChar);
return TRUE;
}
int CMirGame::GetMsg( MMSG * pMsg )
{
int ilen=0;
//char*tmpBuffer=new char[BUFFERLENGTH];
//char*tmpBuffer2=new char[BUFFERLENGTH];
if(m_MsgQueue.GetMsg(tmpBuffer))
{
ilen=strlen(tmpBuffer);
if(tmpBuffer[ilen-1]=='!')
{
tmpBuffer[ilen-1]='\0';
}
if(tmpBuffer[1]=='+')
{
tmpBuffer[6]='\0';
if(stricmp(tmpBuffer,"#+GOOD")==0)
{
ActionOk();
}
else if(stricmp(tmpBuffer,"#+FAIL")==0)
{
ActionFail();
}
return GetMsg(pMsg);
}
ilen=UnGameCode(&tmpBuffer[1],(BYTE*)pMsg);
if(pMsg->wCmd==0x0a || pMsg->wCmd==0x26d )
{
UnNameCode( &pMsg->data[8], tmpBuffer2 );
}
return ilen;
}
return 0;
}
#define CLEARMSG(a) memset( &##a, 0, sizeof( MMSG ))
BOOL CMirGame::StartGame()
{
if(!m_MirSocket.SetNoBlock(m_sGame))
{
LogSystem("Error:SetNoBlock");
goto __err;
}
m_bInGame = TRUE;
AfxBeginThread((AFX_THREADPROC)::MessageProc,this);
memset(&m_LastAction,0,sizeof(m_LastAction));
return TRUE;
__err:
return FALSE;
}
void CMirGame::ActionOk()
{
CString s;
//LogSystem("Action Ok!");
switch(m_LastAction.action)
{
case ACTION_RUN:
//LogFile( "End Run!");
case ACTION_WALK:
SetCharacterPos(m_LastAction.x,m_LastAction.y);
break;
case ACTION_TRUN:
//LogSystem("转身OK!");
break;
}
//s.Format("Pos( %d, %d)",m_CharSelf.pos_x,m_CharSelf.pos_y);
//LogSystem(s);
m_LastAction.action=0;
memset(&m_LastAction,0,sizeof(m_LastAction));
}
void CMirGame::ActionFail()
{
switch(m_LastAction.action)
{
case ACTION_RUN:
LogSystem("跑步失败!");
SetCharacterPos(m_CharSelf.pos_x,m_CharSelf.pos_y);
break;
case ACTION_WALK:
LogSystem("走路失败!");
SetCharacterPos(m_CharSelf.pos_x,m_CharSelf.pos_y);
break;
case ACTION_TRUN:
//LogSystem("转身失败!");
break;
}
m_LastAction.action=0;
memset(&m_LastAction,0,sizeof(m_LastAction));
}
BOOL CMirGame::Logout()
{
m_bInGame = FALSE;
return TRUE;
}
void CMirGame::SetCharacterPos(int x, int y)
{
m_CharSelf.pos_x = x;
m_CharSelf.pos_y = y;
//m_MiniMap.SetCurPos( x, y);
CPoint pnt(x,y);
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SET_PERSON_INFO,NULL,(LPARAM)&pnt);
}
DWORD CMirGame::GameLoop()
{
//LogSystem("GameLoop");
FILE*g_fpLog;
RECT rect;
int i2=10;
int ilen = 0;
char pTxt[256];
MMSGSETCHARINFO*pmsginfo;
MMSG msg;
CString s;
CPoint pnt;
memset(&msg,0,sizeof(MMSG));
g_fpLog=fopen(".\\Log.txt","a");
if(m_bInGame)
{
//LogSystem("GameLoop");
if((ilen=GetMsg(&msg))!=0)
{
switch(msg.wCmd)
{
case 0x9:
if(msg.dwFlag==m_CharSelf.dwServerId)
{
SetCharacterPos(msg.w1,msg.w2);
s.Format("重新设置角色坐标在%d,%d...",msg.w1,msg.w2);
LogSystem(s);
if(m_LastAction.action!=0)
{
m_LastAction.x=msg.w1;
m_LastAction.y=msg.w2;
}
}
break;
case 0xa:
//case 0xb:
//case 0xd:
if(msg.data[8]!=(char)0)
{
UnNameCode(&msg.data[8],pTxt);
//s.Format("%s出现在(%d,%d)处!%d",pTxt,msg.w1,msg.w2,msg.dwFlag);
//LogSystem(s);
rect.bottom=(DWORD)pTxt;
rect.left=msg.w1;
rect.right=msg.w2;
rect.top=msg.dwFlag;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SB_APPEAR,
0,(LPARAM)&rect);
//fprintf(g_fpLog,"%s\n",pTxt);
//goto label_default;
}
else
{
//s.Format("%d出现在(%d,%d)处!",msg.dwFlag,msg.w1,msg.w2);
//LogSystem(s);
//rect.bottom=(DWORD)pTxt;
rect.left=msg.w1;
rect.right=msg.w2;
rect.top=msg.dwFlag;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SB_APPEAR,
1,(LPARAM)&rect);
}
break;
/*攻击
flag = attacker id
wcmd = 0x0e
w1 = x
w2 = y
w3 = dir
*/
case 0x1e:
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SB_DISAPPEAR,0,
msg.dwFlag);
break;
case 0x1f:
/*被攻击
flag = char id
wcmd = 0x1f
w1 = cur hp
w2 = max hp
w3 = dir
data = feather [4bytes] +
0 [4bytes] +
attacker id[4 bytes]+
0 [4bytes]
*/
if(msg.dwFlag==m_CharSelf.dwServerId)
{
s.Format(
"你被别人攻击 减少%d的生命,剩下%d的生命,总共%d的生命.",
msg.w3,msg.w1,msg.w2);
LogSystem(s);
rect.top=msg.w2;
rect.left=msg.w1;
rect.right=msg.w3;
rect.bottom=msg.dwFlag;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_HP_CHANGE,
0,(LPARAM)&rect);
}
else
{
s.Format(
"Serverid=%d被别人攻击 减少%d的生命,剩下%d的生命,总共%d的生命.",
msg.dwFlag,msg.w3,msg.w1,msg.w2);
LogSystem(s);
rect.top=msg.w2;
rect.left=msg.w1;
rect.right=msg.w3;
rect.bottom=msg.dwFlag;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_HP_CHANGE,
1,(LPARAM)&rect);
}
break;
case 0x22:
/*死亡
wMsgFlag = char id
wcmd = 0x22
w1 = x
w2 = y
w3 = dir
data = feather and 0 */
if(msg.dwFlag==m_CharSelf.dwServerId)
{
LogSystem("你被杀死了!");
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SB_DEAD,0,msg.dwFlag);
}
else
{
//s.Format("ServerId = %d 被人杀死了!",msg.dwFlag);
//LogSystem(s);
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SB_DEAD,1,msg.dwFlag);
}
break;
case 0x2a:// 玩家角色名字
if(msg.w1=0xff && m_CharSelf.dwServerId==msg.dwFlag)
{
s.Format("玩家角色名字 %s",msg.data);
LogSystem(s);
}//else goto label_default;
break;
case 0x33:
m_CharSelf.pos_x=msg.w1;
m_CharSelf.pos_y=msg.w2;
s.Format("出现在地图 %d, %d 处",msg.w1,msg.w2);
LogSystem(s);
m_CharSelf.dwServerId=msg.dwFlag;
fprintf(g_fpLog,"我的ID:(0x%x)\n\n",msg.dwFlag);
s=msg.data;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SET_GAME_MAP,NULL,(LPARAM)&s);
pnt.x=msg.w1;
pnt.y=msg.w2;
SendMessage(AfxGetMainWnd()->m_hWnd,WM_SET_PERSON_INFO,NULL,(LPARAM)&pnt);
//SetCharacterPos(msg.w1,msg.w2);
break;
case 0x34:
pmsginfo=(MMSGSETCHARINFO*)&msg;
s.Format("等级:%u",pmsginfo->bLevel);
LogSystem(s);
s.Format("金币:%u",pmsginfo->dwGold);
LogSystem(s);
s.Format("防御:%u-%u",pmsginfo->ACHi, pmsginfo->ACLow);
LogSystem(s);
s.Format("魔御:%u-%u",pmsginfo->MACHi, pmsginfo->MACLow);
LogSystem(s);
s.Format("攻击:%u-%u",pmsginfo->DCHi,pmsginfo->DCLow);
LogSystem(s);
s.Format("魔法:%u-%u",pmsginfo->MCHi, pmsginfo->MCLow);
LogSystem(s);
s.Format("道术:%u-%u",pmsginfo->SCHi, pmsginfo->SCLow);
LogSystem(s);
break;
/*魔法,hp改变
dwMsgFlag = char id
wcmd = 0x35
w1 = curhp
w2 = curmp
w3 = maxhp*/
case 0x35:
s.Format("HP:%d/%d MP:%d/%d",msg.w1,msg.w3,msg.w2,(DWORD)msg.data);
LogSystem(s);
break;
case 0x36:
s.Format("当前地图%s.",msg.data);
LogSystem(s);
i2=0;
CLEARMSG(msg);
msg.wCmd=0x409;
if(m_MirMsg.SendMsg(m_sGame,&msg)==-1)
{
goto __err;
}
m_bInWorld=TRUE;
break;
case 0x64:
s.Format("系统信息> %s",msg.data);
LogSystem(s);
break;
case 0x67:
/*HandleCommand(msg.data);
LogSystem(msg.data);*/
break;
case 0x28:
s.Format("行会聊天> %s",msg.data);
LogSystem(s);
break;
case 0x68:
s.Format("聊天频道> %s",msg.data);
LogSystem(s);
break;
case 0x262:// 地上出现物品
s.Format("物品 %s 出现在:(%d,%d)",msg.data,msg.w1,msg.w2);
LogSystem(s);
break;
/*[吃紧创药]
负重减轻
hiword(dwmsgflag) = 背包负重
wcmd = 0x26e
w1 = 身上负重?
w2 = 手上负重?*/
case 0x279:
LogSystem("切换地图!");
break;
case 0x27a:
if(msg.dwFlag==m_CharSelf.dwServerId)
{
SetCharacterPos(msg.w1,msg.w2);
s.Format("切换地图后,重新设置角色坐标在%d,%d ...",msg.w1,msg.w2);
LogSystem(s);
if(m_LastAction.action!=0)
{
m_LastAction.x=msg.w1;
m_LastAction.y=msg.w2;
}
}
break;
/*[吃紧创药]
吃药成功
dwMsgFlag = 0
wCmd = 0x27b*/
case 0x291:
if(msg.w2==80)
LogSystem("身体状态:隐身");
else
LogSystem("身体状态:正常");
case 0x292:// 进入游戏世界
CLEARMSG(msg);
msg.wCmd=0x3fa;
if(m_MirMsg.SendMsg(m_sGame,&msg)==-1)
{
goto __err;
}
LogSystem("进入游戏世界...");
break;
case 0x2c4:
if(msg.w1==0)
LogSystem("当前地图不是战斗地图");
else
LogSystem("当前地图是战斗地图");
break;
case 0x2c6:
//m_MiniMap.SetMMap(msg.w1);
break;
case 0x2ee:// 玩家行会信息
s.Format("玩家行会信息 %s",msg.data);
LogSystem(s);
break;
/*[吃紧创药]
wMsgFlag = drug id
wcmd = 0x3ee
data = name
*/
default:
label_default:
fprintf(g_fpLog, "MSG: FLAG = 0x%08x wCmd = 0x%04x w1 = 0x%04x w2 = 0x%04x w3 = 0x%04x data = [%s] hex = [", msg.dwFlag, msg.wCmd, msg.w1, msg.w2, msg.w3, msg.data );
for( i2 = 0;i2 < ilen - MSGHEADERSIZE;i2 ++ )
{
fprintf(g_fpLog," %02x ", msg.data[i2] & 0xff );
}
fprintf(g_fpLog,"]\n" );
//LogSystem("未知的命令!!");
break;
}
}
}
fclose(g_fpLog);
return TRUE;
__err:
memset(&msg,0,sizeof(MMSG));
msg.wCmd=0x3f1;
m_MirMsg.SendMsg(m_sGame,&msg);
LogSystem("断开游戏服务器!");
m_MirSocket.CloseConnection(m_sGame);
return FALSE;
}
int xofs_walk[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int yofs_walk[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
void CMirGame::Run(int dir)
{
//LogSystem("CMirGame::Run");
MMSG msg;
int erri;
int tx,ty;
if(!CanDoAction())
return;
if( dir <0 || dir > 7 )
return;
tx = m_CharSelf.pos_x + (xofs_walk[dir]*2);
ty = m_CharSelf.pos_y + (yofs_walk[dir]*2);
//LogFile( "Start Run!" );
//LogChat( "Start Run at ( %d, %d )", m_CharSelf.pos_x,m_CharSelf.pos_y );
//CString s;
//s.Format("Start at( %d , %d )",m_CharSelf.pos_x,m_CharSelf.pos_y);
//LogSystem(s);
//s.Format(" Run to ( %d , %d )",tx,ty);
//LogSystem(s);
/*if(!m_GameMap.TestMap(tx,ty)||!m_GameMap.TestMap(m_CharSelf.pos_x+xofs_walk[dir],m_CharSelf.pos_y+yofs_walk[dir]))
{
LogChat( "前方无法通过!" );
return;
}*/
memset(&msg,0,sizeof(MMSG));
msg.wa=tx;
msg.wb=ty;
msg.wCmd=0xbc5;
msg.w2=dir;
erri=m_MirMsg.SendMsg(m_sGame,&msg);
if(erri==-1)
return;
m_LastAction.action=ACTION_RUN;
m_LastAction.x=tx;
m_LastAction.y=ty;
m_LastAction.dir=dir;
return ;
}
BOOL CMirGame::CanDoAction()
{
if( m_LastAction.action == 0 )
return 1;
return 0;
}
BOOL CMirGame::Say(CString s)
{
MMSG msg;
memset(&msg,0,sizeof(MMSG));
strcpy(msg.data,s);
msg.wCmd = 0xbd6;
int erri;
erri = m_MirMsg.SendMsg(m_sGame,&msg);
if(erri==-1)
LogSystem("说话失败!!!!!!");
return (erri!= -1);
}
void CMirGame::Walk(int dir)
{
MMSG msg;
int erri;
int tx,ty;
if( !CanDoAction())
return;
if( dir <0 || dir > 7 )
return;
tx = m_CharSelf.pos_x + (xofs_walk[dir]);
ty = m_CharSelf.pos_y + (yofs_walk[dir]);
/*if( !m_GameMap.TestMap( tx, ty ))
{
LogChat( "前方无法通过!" );
return;
}*/
memset( &msg, 0, sizeof( MMSG ));
msg.wa = tx;//g_pos_x;
msg.wb = ty;//g_pos_y;
msg.wCmd = 0xbc3;
msg.w2 = dir;
erri = m_MirMsg.SendMsg( m_sGame, &msg );
if( erri == -1 )
return;
m_LastAction.action = ACTION_WALK;
m_LastAction.x = tx;
m_LastAction.y = ty;
m_LastAction.dir = dir;
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -