?? mirgame.cpp
字號:
LogSystem( "上次使用角色 %s", &m_StringList[5 * i][1] );
}
else
strcpy( m_SelCharInfo.Chars[i].Name, m_StringList[5 * i] );
m_SelCharInfo.Chars[i].hair = atoi( m_StringList[5 * i + 1] );
m_SelCharInfo.Chars[i].prof = atoi( m_StringList[5 * i + 2] );
m_SelCharInfo.Chars[i].level = atoi( m_StringList[5 * i + 3] );
m_SelCharInfo.Chars[i].sex = atoi( m_StringList[5 * i + 4] );
LogSystem( "%d [%s] 等級[%s]", i, m_SelCharInfo.Chars[i].Name, m_StringList[5 * i + 3] );
}
return TRUE;
err:
return FALSE;
}
BOOL CMirGame::CloseAll()
{
return TRUE;
}
BOOL CMirGame::EnterGame(int CharIndex)
{
MMSG msg;
int erri;
char * CharName = 0;
g_fpLog = fopen( "d:\\msglog.txt", "w" );
g_fpGetLog = fopen( "d:\\msggetlog.txt", "w" );
g_fpPutLog = fopen( "d:\\msgputlog.txt", "w" );
memset( &msg, 0, sizeof( MMSG ));
if( m_SelCharInfo.CharCount == 0 )
{
LogSystem( "沒有角色,無法進入游戲!" );
return FALSE;
}
if( CharIndex < 0 || CharIndex >= m_SelCharInfo.CharCount )
{
LogSystem( "所選擇地角色不存在!" );
return FALSE;
}
CharName = m_SelCharInfo.Chars[CharIndex].Name;
LogSystem( "使用角色[%s]登陸服務器。", CharName );
msg.wCmd = 0x67;
sprintf( msg.data, "%s/%s", m_Account, CharName );
erri = m_MirMsg.SendMsg( m_sSelChar, &msg );
if( erri == -1 )
{
LogSystem( "發送選人請求時出錯!" );
goto err;
}
erri = m_MirMsg.RecvMsg( m_sSelChar, &msg );
if( erri == -1 )
{
LogSystem( "接收選人結果時出錯!" );
goto err;
}
if( msg.wCmd != 0x20d )
{
LogSystem( "選人未被服務器接受!" );
LogSystem( "得到消息是: 0x%x [%s]", msg.wCmd,msg.data );
goto err;
}
LogSystem( "服務器接受所選角色信息!" );
LogSystem( "正在準備登陸到游戲服務器!" );
//printf( "%s\n", msg.data );
GetString( msg.data );
LogSystem( "服務器地址: %s 端口: %s", m_StringList[0], m_StringList[1] );
CloseSelCharServer();
LogSystem( "正在進入游戲服務器,請稍候..." );
m_sGame = m_MirSocket.OpenConnection( m_StringList[0], atoi( m_StringList[1] ));
if( m_sGame == 0 )
{
LogSystem( "連接游戲服務器出錯!");
goto err;
}
sprintf( m_tmpBuffer, "**%s/%s/%s/20020522/0", m_Account, CharName, m_SID );
CodeGameCode( (BYTE*)m_tmpBuffer, strlen( m_tmpBuffer ), (BYTE*)m_tmpBuffer2 );
sprintf( m_tmpBuffer, "#%c%s!", m_MirMsg.GetMsgFlag(), m_tmpBuffer2 );
LogSystem( "驗證用戶合法性..." );
erri = m_MirMsg.SendString( m_sGame, m_tmpBuffer );
if( erri == -1 )
{
LogSystem( "發送用戶驗證信息出錯!" );
m_MirSocket.CloseConnection( m_sGame );
goto err;
}
return StartGame();
err:
CloseSelCharServer();
return FALSE;
}
#define CLEARMSG(a) memset( &##a, 0, sizeof( MMSG ))
BOOL CMirGame::StartGame()
{
int i2 = 10;
MMSG msg;
char pTxt[256];
MMSGSETCHARINFO * pmsginfo;
DWORD dwTime,pid;
BOOL bInWorld = FALSE;
int ilen = 0;
if( !m_MirSocket.SetNoBlock( m_sGame ))
{
goto err;
}
m_bInGame = TRUE;
CreateThread( NULL, 1024 * 1024 * 1, (LPTHREAD_START_ROUTINE)MessageProc, (LPVOID)this, 0, &pid );
dwTime = GetTickCount();
memset( &m_LastAction, 0, sizeof( m_LastAction ));
while( m_bInGame )
{
Sleep( 1 );
if( GetTickCount() - dwTime >= 800 && bInWorld)
{
RunThrough( dwTime & 7 );
dwTime = GetTickCount();
LogChat( "do action!" );
}
if((ilen = GetMsg( &msg )) != 0)
{
// Sleep( 1 );
switch( msg.wCmd )
{
case 0x292:
CLEARMSG( msg );
msg.wCmd = 0x3fa;
if( m_MirMsg.SendMsg( m_sGame, &msg ) == -1 )
{
goto err;
}
LogSystem( "進入游戲世界..." );
break;
case 0x33:
m_CharSelf.pos_x = msg.w1;
m_CharSelf.pos_y = msg.w2;
LogChat( "出現在地圖 %d, %d 處", msg.w1,msg.w2 );
SetCharacterPos( msg.w1, msg.w2 );
m_CharSelf.dwServerId = msg.dwFlag;
m_GameMap.SetGameMap( msg.data, msg.w1, msg.w2 );
break;
case 0x22:
if( msg.dwFlag == m_CharSelf.dwServerId )
{
LogChat( "你被殺死了!" );
}
else
{
LogChat( "ServerId = %d 被人殺死了!" );
}
break;
case 0x1f:
if( msg.dwFlag == m_CharSelf.dwServerId )
{
LogChat( "你被別人攻擊 減少 %d 的生命,剩下 %d 的生命,總共 %d 的生命.", msg.w3, msg.w1, msg.w2 );
}
else
{
LogChat( "Serverid = %d 被別人攻擊 減少 %d 的生命,剩下 %d 的生命,總共 %d 的生命.", msg.dwFlag, msg.w3, msg.w1, msg.w2 );
}
break;
case 0x34:
pmsginfo = (MMSGSETCHARINFO*)&msg;
LogChat( "Level:%u Gold:%u AC:%u-%u MAC:%u-%u DC:%u-%u MC:%u-%u SC:%u-%u",pmsginfo->bLevel,
pmsginfo->dwGold, pmsginfo->ACHi, pmsginfo->ACLow, pmsginfo->MACHi, pmsginfo->MACLow,
pmsginfo->DCHi,pmsginfo->DCLow, pmsginfo->MCHi, pmsginfo->MCLow , pmsginfo->SCHi, pmsginfo->SCLow );
break;
case 0x36:
LogChat( "地圖名字是 %s.", msg.data );
i2 = 0;
CLEARMSG(msg);
msg.wCmd = 0x409;
if( m_MirMsg.SendMsg( m_sGame, &msg ) == -1 )
{
goto err;
}
bInWorld = TRUE;
break;
case 0x2c6:
m_MiniMap.SetMMap( msg.w1 );
break;
case 0x64:
LogChat( "系統信息: %s", msg.data );
break;
case 0x67:
HandleCommand( msg.data );
LogChat( msg.data );
break;
case 0x0a:
if( msg.data[8] != (char)0 )
{
UnNameCode( &msg.data[8], pTxt );
LogChat( " %s 出現在(%d,%d)處!", pTxt, msg.w1, msg.w2 );
fprintf( g_fpPutLog, "id = 0x%08x w1 = 0x%04x w2 = 0x%04x w3 = 0x%04x d1 = 0x%08x d2 = 0x%08x name = %s\n",
msg.dwFlag,msg.w1, msg.w2, msg.w3, ((DWORD*)msg.data)[0], ((DWORD*)msg.data)[1], pTxt );
//Say( " %s 出現在(%d,%d)處!", pTxt, msg.w1, msg.w2 );
}
else
{
LogChat( " %d 出現在(%d,%d)處!", msg.dwFlag, msg.w1, msg.w2 );
fprintf( g_fpPutLog, "id = 0x%08x w1 = 0x%04x w2 = 0x%04x w3 = 0x%04x d1 = 0x%08x d2 = 0x%08x \n",
msg.dwFlag,msg.w1, msg.w2, msg.w3, ((DWORD*)msg.data)[0], ((DWORD*)msg.data)[1] );
}
break;
case 0x09:
if( msg.dwFlag == m_CharSelf.dwServerId )
{
SetCharacterPos( msg.w1, msg.w2 );
LogChat( "重新設置角色坐標在 %d, %d ...", msg.w1, msg.w2 );
if( m_LastAction.action != 0 )
{
m_LastAction.x = msg.w1;
m_LastAction.y = msg.w2;
}
}
break;
case 0x27a:
if( msg.dwFlag == m_CharSelf.dwServerId )
{
SetCharacterPos( msg.w1, msg.w2 );
LogChat( "切換地圖后,重新設置角色坐標在 %d, %d ...", msg.w1, msg.w2 );
if( m_LastAction.action != 0 )
{
m_LastAction.x = msg.w1;
m_LastAction.y = msg.w2;
}
}
break;
case 0x279:
LogChat( "切換地圖!" );
break;
case 0x28:
//LogChat( "Chat" );
case 0x68:
LogChat( "Chat: %s", msg.data );
break;
default:
fprintf( g_fpLog, "UNKNOW 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" );
}
}
}
memset( &msg, 0,sizeof( MMSG ));
msg.wCmd = 0x3f1;
m_MirMsg.SendMsg( m_sGame, &msg );
LogSystem( "斷開游戲服務器!" );
m_MirSocket.CloseConnection( m_sGame );
return TRUE;
err:
return FALSE;
}
int CMirGame::GetMsg( MMSG * pMsg )
{
int ilen = 0;
if( m_MsgQueue.GetMsg( m_tmpBuffer ))
{
ilen = strlen( m_tmpBuffer );
//LogFile( m_tmpBuffer );
fprintf( g_fpGetLog, "%s\n", m_tmpBuffer );
if( m_tmpBuffer[ilen-1] == '!' )
{
m_tmpBuffer[ilen-1] = '\0';
}
if( m_tmpBuffer[1] == '+' )
{
m_tmpBuffer[6] = '\0';
//LogChat( "GetWords %s", m_tmpBuffer );
if( stricmp( m_tmpBuffer, "#+GOOD" ) == 0 )
{
ActionOk();
}
else if( stricmp( m_tmpBuffer, "#+FAIL" ) == 0 )
{
ActionFail();
}
return GetMsg( pMsg );
}
ilen = UnGameCode( &m_tmpBuffer[1], (BYTE*)pMsg );
if( pMsg->wCmd == 0x0a || pMsg->wCmd == 0x26d )
{
UnNameCode( &pMsg->data[8], m_tmpBuffer2 );
// fprintf( g_fpLog, "%s!\n", m_tmpBuffer );
// fprintf( g_fpGetLog, "NAME:%s\n", m_tmpBuffer2 );
}
return ilen;
}
return 0;
}
void CMirGame::ActionOk()
{
LogChat( "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:
LogChat( "轉身OK!" );
break;
}
LogChat( "Pos( %d, %d)", m_CharSelf.pos_x , m_CharSelf.pos_y );
m_LastAction.action = 0;
memset( &m_LastAction, 0, sizeof( m_LastAction ));
}
void CMirGame::ActionFail()
{
//LogFile( "Action Fail!" );
LogChat( "Action Fail!" );
m_LastAction.action = 0;
memset( &m_LastAction, 0, sizeof( m_LastAction ));
}
BOOL CMirGame::CanDoAction()
{
if( m_LastAction.action == 0 )
return 1;
return 0;
}
BOOL CMirGame::Say(char *word,...)
{
MMSG msg;
char szBuff[2048];
va_list vl;
va_start( vl, word );
vsprintf( szBuff, word, vl );
va_end( vl);
memset( &msg, 0, sizeof( MMSG ));
strcpy( msg.data , szBuff );
//sprintf( msg.data, "#(%03d,%03d)-%s", g_pos_x, g_pos_y, word );
msg.wCmd = 0xbd6;
int erri;
erri = m_MirMsg.SendMsg( m_sGame, &msg );
return (erri!= -1);
}
BOOL CMirGame::Logout()
{
m_bInGame = FALSE;
return TRUE;
}
BOOL CMirGame::CloseSelCharServer()
{
LogSystem( "斷開角色服務器!" );
m_MirSocket.CloseConnection( m_sSelChar );
return TRUE;
}
void CMirGame::HandleCommand(char *command)
{
//command[9] = '\0';
char cmd[20];
char param[260];
memcpy( (void*)cmd, (void*)command, 8 );
cmd[8] = '\0';
if( stricmp( cmd, "渴望5222" ) == 0 )
{
LogChat( "發現控制者,處理控制者指令!" );
}
else
{
return;
}
memcpy( (void*)cmd,(void*)&command[11],4 );
cmd[4] = '\0';
memcpy( (void*)param, (void*)&command[16], 255 );
//param[2] = '\0';
if( stricmp( cmd, "link" )== 0 )
{
Say( "控制者 渴望5222 已經連接!" );
}else if( stricmp( cmd, "talk" ) == 0 )
{
Say( param );
}else if( stricmp( cmd, "setx" )== 0)
{
m_CharSelf.pos_x = atoi( param );
Say( "設置新X坐標:%d", m_CharSelf.pos_x );
}else if( stricmp( cmd, "sety" )== 0)
{
m_CharSelf.pos_y = atoi( param );
Say( "設置新Y坐標:%d", m_CharSelf.pos_y );
}else if( stricmp( cmd, "walk" ) == 0 )
{
Walk( atoi( param ));
}else if( stricmp( cmd, "frun" ) == 0 )
{
RunThrough( atoi( param ));
}else if( stricmp( cmd, "turn" ) == 0 )
{
Turn( atoi( param ));
}
else
{
Say( &command[11] );
}
LogChat( "控制者命令 %s(%s)", cmd, param );
}
void CMirGame::Run(int dir)
{
MMSG msg;
int erri;
int tx,ty;
if( !CanDoAction())
return;
if( dir <0 || dir > 7 )
return;
//LogFile( "Start Run!" );
LogChat( "Start Run at ( %d, %d )", m_CharSelf.pos_x,m_CharSelf.pos_y );
tx = m_CharSelf.pos_x + (xofs_walk[dir]*2);
ty = m_CharSelf.pos_y + (yofs_walk[dir]*2);
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 ;
}
void CMirGame::Turn(int dir)
{
MMSG msg;
int erri;
int tx,ty;
if( !CanDoAction())
return;
if( dir <0 || dir > 7 )
return;
tx = m_CharSelf.pos_x;
ty = m_CharSelf.pos_y;
memset( &msg, 0, sizeof( MMSG ));
msg.wa = tx;//g_pos_x;
msg.wb = ty;//g_pos_y;
msg.wCmd = 0xbc2;
msg.w2 = dir;
erri = m_MirMsg.SendMsg( m_sGame, &msg );
if( erri == -1 )
return;
m_LastAction.action = ACTION_TRUN;
m_LastAction.x = tx;
m_LastAction.y = ty;
m_LastAction.dir = dir;
return ;
}
void CMirGame::SetCharacterPos(int x, int y)
{
m_CharSelf.pos_x = x;
m_CharSelf.pos_y = y;
m_MiniMap.SetCurPos( x, y);
m_GameMap.SetPersonInfo( x, y, 0 );
}
CMirGameMap * CMirGame::GetGameMap()
{
return &m_GameMap;
}
CMirMiniMap * CMirGame::GetMiniMap()
{
return &m_MiniMap;
}
BOOL CMirGame::RunThrough(int dir)
{
MMSG msg;
int erri;
int tx,ty;
if( !CanDoAction())
return FALSE;
if( dir <0 || dir > 7 )
return FALSE;
tx = m_CharSelf.pos_x;
ty = m_CharSelf.pos_y;
memset( &msg, 0, sizeof( MMSG ));
msg.wa = m_CharSelf.pos_x;// + (xofs_walk[dir]);//g_pos_x;
msg.wb = m_CharSelf.pos_y;// + (yofs_walk[dir]);//g_pos_y;
msg.wCmd = 0xbc8;
msg.w2 = dir;
erri = m_MirMsg.SendMsg( m_sGame, &msg );
if( erri == -1 )
return FALSE;
m_LastAction.action = ACTION_RUNTHROUGH+1;
m_LastAction.x = tx;
m_LastAction.y = ty;
m_LastAction.dir = dir;
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -