console.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:33k
- #include "global.h"
- #include "console.h"
- #include "network.h"
- #include "dxutil.h"
- #include "ddutil.h"
- #include "dsutil.h"
- #define DEFTURNSTEP 8
- #define DEFPROTECTTIME 600 // 20*30fps
- #define WAITTIMEOUT 5000 // second
- #define BULLET_REBOUND
- #define _TK_DEBUG
- //-----------------------------------------------------------------------------
- // Global variables
- //-----------------------------------------------------------------------------
- bool bRecvCmd = false;
- TANKCMD m_pCmd[20];
- CDisplay Display;
- CSurface *pResource, *pMapGrass, *pMapSolid, *pRes2;
- CSurface *pText;
- LPDIRECTDRAWSURFACE7 pddsSolid, pddsGrass, pddsRes, pddsRes2;
- CSoundManager SoundManager;
- CSound *pFire, *pBoom;
- char *szFireWave = "Wave/bfire.wav";
- char *szBoomWave = "Wave/lboom.wav";
- char fps[20] = "FPS: ";
- int g_FrameBig = 640, g_FrameSmall = 32, g_FrameLeft = 608,
- g_FrameWidth = 640, g_FrameHeight = 640, g_FrameBasis = 16;
- //-----------------------------------------------------------------------------
- // Variables defined in other files
- //-----------------------------------------------------------------------------
- extern CMsgList NetList;
- extern HWND hMainWnd, hChildWnd;
- extern unsigned int uiCurrentCmd;
- extern bool bFired;
- unsigned int cmdbuf;
- bool firebuf;
- bool bFresh = true;
- //-----------------------------------------------------------------------------
- //
- //-----------------------------------------------------------------------------
- #ifdef _TK_DEBUG
- #include <time.h>
- #include <sys/timeb.h>
- #include <stdio.h>
- #define MAXDEBUG 128
- HANDLE hfile;
- char szDebugFile[20];
- void DebugOutput( int num, ... ) {
- char *s = new char[MAXDEBUG];
- s[0] = 0;
- // add time stamp
- struct _timeb tstruct;
- _ftime( &tstruct );
- _strtime( s );
- lstrcat(s, ":");
- sprintf( s+strlen(s), "%u", tstruct.millitm );
- lstrcat(s, ": ");
- // add messages
- va_list vl;
- va_start( vl, num );
- for( int i = 0; i<num ; i++ )
- lstrcat( s, va_arg( vl, char * ) );
- va_end( vl );
- lstrcat( s, "rn" );
- // output msg
- if ( hfile ) {
- DWORD ret;
- WriteFile( hfile, s, strlen(s)+1, &ret, NULL );
- }
- }
- #else
- #define DebugOutput
- #endif
- //-----------------------------------------------------------------------------
- // Functions defined in other files
- //-----------------------------------------------------------------------------
- bool SendMsg(int msg, LPVOID param, int size);
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool SendCmdMsg( TANKCMD * pcmds, int num, int local ) {
- int size = sizeof(int)*2 + num * sizeof(TANKCMD);
- char *param = new char[size];
- int *ibuf = (int *)param;
- ibuf[0] = local;
- ibuf[1] = num;
- memcpy( ibuf+2, pcmds, num*sizeof(TANKCMD));
- SendMsg( NETMSGTK_CMDINFO, param, size );
-
- delete[] param;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- Console::Console() {
- con_version = ASIDER_GAME_TANK_VER_ALPHA3;
- err_code = 0;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- Console::~Console() {
- #ifdef _TK_DEBUG
- CloseHandle( hfile );
- #endif
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void Console::ErrorTrigger( unsigned int error ) {
- if ( error != 0 )
- MessageBox( NULL, "Something Error :)", "Error", MB_ICONERROR );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- ConsoleNet::ConsoleNet() {
- m_dwStatus = CONSTAT_NOTHING;
- m_nPlayers = 0;
- m_nComputers = 0;
- m_nComPerTeam = 0;
- m_nLocal = 0;
- m_bHomeTeam = true;
- m_pProCounter[0] = -1;
- m_pProCounter[1] = -1;
- m_nMsgCounter = 0;
- m_pRiver = NULL;
- m_pFood = NULL;
- ZeroMemory( m_ppTanks, sizeof(LPVOID)*DEFTANKNUM );
- ZeroMemory( m_ppBullets, sizeof(LPVOID)*DEFTANKNUM );
- ZeroMemory( m_ppBases, sizeof(LPVOID)*2);
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- ConsoleNet::~ConsoleNet() {
- DELETE_ARRAY( m_pRiver );
- DELETE_PTR( m_pFood );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::ErrorTrigger( unsigned int error ) {
- Console::ErrorTrigger( error );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::LoadMap( const char *mapname ) {
- return MapInfo.LoadMap( mapname );
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::RenderMap( bool hometeam ) {
- if ( !hometeam )
- MapInfo.CenterRotate180();
- // Set class properties
- m_bHomeTeam = hometeam;
- m_dwStatus = CONSTAT_WAITBEGIN;
- m_nNumTanks = MapInfo.GetObjectNum( OBJTYPE_TANK );
- m_nNumRiver = MapInfo.BlockCounter( LAND_RIVER, NULL );
- m_pRiver = new CObjBlock[m_nNumRiver];
- MapInfo.BlockCounter( LAND_RIVER, m_pRiver );
- // Create bases
- m_ppBases[0] = new CObjBase();
- m_ppBases[1] = new CObjBase();
- m_ppBases[0]->CreateBase( MapInfo.m_ptBase[0].x, MapInfo.m_ptBase[0].y, true);
- m_ppBases[1]->CreateBase( MapInfo.m_ptBase[1].x, MapInfo.m_ptBase[1].y, false);
- // Blt blocks
- int x, y, xl, yl, level;
- RECT *prc = new RECT, *prcTrans = new RECT;
- SetRect( prcTrans, 0, 32, 32, 64 );
- for ( y = 0, yl = 0; y < MapInfo.m_cyBlocks-1; y++, yl += g_FrameSmall )
- for ( x = 0, xl = 0; x < MapInfo.m_cxBlocks-1; x++, xl += g_FrameSmall ) {
- MapInfo.BlockLocator( x, y, prc, level );
- if ( level == LAYER_GROUND ) {
- if ( FAILED( pddsSolid->BltFast( xl, yl, pddsRes, prc, 0 ) ) )
- return false;
- if ( FAILED( pddsGrass->BltFast( xl, yl, pddsRes, prcTrans, 0 ) ) )
- return false;
- }else if ( level == LAYER_TREE ) {
- if ( FAILED( pddsSolid->BltFast( xl, yl, pddsRes, prcTrans, 0 ) ) )
- return false;
- if ( FAILED( pddsGrass->BltFast( xl, yl, pddsRes, prc, 0 ) ) )
- return false;
- }
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::InitDisplay( HWND hwnd ) {
- if ( !hwnd )
- return false;
- // Create display
- if ( FAILED( Display.CreateWindowedDisplay( hwnd, g_FrameBig, g_FrameBig ) ) )
- return false;
-
- // Create surfaces
- pResource = new CSurface();
- if ( FAILED( Display.CreateSurfaceFromBitmap( &pResource,
- "Res/res3.bmp", 512, 640 ) ) )
- return false;
- pRes2 = new CSurface();
- if ( FAILED( Display.CreateSurfaceFromBitmap( &pRes2,
- "Res/res2.bmp", 640, 480 ) ) )
- return false;
- pMapSolid = new CSurface();
- if ( FAILED( Display.CreateSurface( &pMapSolid, g_FrameBig, g_FrameBig ) ) )
- return false;
-
- pMapGrass = new CSurface();
- if ( FAILED( Display.CreateSurface( &pMapGrass, g_FrameBig, g_FrameBig ) ) )
- return false;
- pText = new CSurface();
- if ( FAILED( Display.CreateSurface( &pText, 80, 30 ) ) )
- return false;
- // Set color key
- pMapSolid->SetColorKey( 0 );
- pMapGrass->SetColorKey( 0 );
- pResource->SetColorKey( 0 );
- pRes2->SetColorKey( 0 );
- // Render the scene according to the map data
- pddsSolid = pMapSolid->GetDDrawSurface();
- pddsGrass = pMapGrass->GetDDrawSurface();
- pddsRes = pResource->GetDDrawSurface();
- pddsRes2 = pRes2->GetDDrawSurface();
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::InitSound( HWND hwnd ) {
- if ( FAILED(SoundManager.Initialize( hwnd, DSSCL_PRIORITY, 1, 22050, 8 )) )
- return false;
- if ( FAILED(SoundManager.Create( &pFire, szFireWave )) )
- return false;
- if ( FAILED(SoundManager.Create( &pBoom, szBoomWave )) )
- return false;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::InitObjects() {
- int i;
- for ( i=0; i<DEFTANKNUM/2; i++ ) { // !TODO: because of the map setting position
- m_ppTanks[i] = new CObjTank();
- m_ppTanks[i]->CreateTank(MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.x,
- MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.y,true);
- m_ppBullets[i] = new CObjBullet();
- m_ppBullets[i]->CreateBullet( NULL );
- m_ppTanks[i]->m_uiDirection = DIR_UP;
- }
- for ( ; i<DEFTANKNUM; i++ ) {
- m_ppTanks[i] = new CObjTank();
- m_ppTanks[i]->CreateTank(MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.x,
- MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.y,false);
- m_ppBullets[i] = new CObjBullet();
- m_ppBullets[i]->CreateBullet( NULL );
- m_ppTanks[i]->m_uiDirection = DIR_DOWN;
- }
- if ( !m_bHomeTeam ) {
- for ( i=0; i<DEFTANKNUM; i++ ) {
- MapInfo.ConvertCoord( m_ppTanks[i] );
- m_ppTanks[i]->m_uiDirection = 5 - m_ppTanks[i]->m_uiDirection;
- }
- }
-
- // Initialize AI command generator
- RECT scope;
- SetRect( &scope, DEFTRACKSCOPE, DEFTRACKSCOPE, DEFTRACKSCOPE, DEFTRACKSCOPE );
- CObjTank **pptk = m_ppTanks+m_nLocal+1;
- for ( i=0; i<DEFAINUM; i++ ) {
- m_ppAiGen[i].CreateGenerator( *(pptk+i), scope );
- m_ppAiGen[i].SetAim( m_ppBases[m_bHomeTeam], -1 );
- m_ppAiGen[i].SetAvoid( m_ppBases[1-m_bHomeTeam]->GetSurroundRect() );
- }
- // food
- m_pFood = new CObjFood();
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::CreateConsole( HWND hwnd, const char *mapname, bool hometeam ) {
- if ( !LoadMap( mapname ) || !InitDisplay( hwnd ) ||
- !InitSound( hwnd ) || !RenderMap( hometeam ) )
- return false;
- #ifdef _TK_DEBUG
- lstrcpy(szDebugFile, "debug0.txt");
- hfile = CreateFile( szDebugFile, GENERIC_WRITE,
- FILE_SHARE_READ, NULL, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL );
- if ( hfile == INVALID_HANDLE_VALUE ) {
- lstrcpy(szDebugFile, "debug1.txt");
- hfile = CreateFile( szDebugFile, GENERIC_WRITE,
- FILE_SHARE_READ, NULL, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL );
- if ( hfile == INVALID_HANDLE_VALUE ) {
- CloseHandle(hfile);
- hfile = NULL;
- }
- }
- #endif
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::CreateAICmd() {
- TANKCMD *pcmd = m_pCmd+m_nLocal+1;
- for ( int i=0; i<DEFAINUM; i++ ) {
- if ( m_ppTanks[m_nLocal+1+i]->GetStat() & OBJSTAT_LIVE )
- *(pcmd+i) = m_ppAiGen[i].Generator( NULL, 0 );
- }
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::GameMain() {
- static int counter = 0;
- static DWORD start_time = 0, last_get;
- static DWORD frame_start = 0;
- DWORD end_time;
- if ( m_dwStatus == CONSTAT_ENDGAME )
- return ;
- // Peek and process messages
- if ( !NetList.IsEmpty() )
- MsgProcessor( &NetList );
- // Waiting
- if ( m_dwStatus < CONSTAT_WAITMORE ) {
- // TODO: here we should set a time-out event,
- // and display like this, " Now, connecting... "
- return ;
- } else if ( m_dwStatus == CONSTAT_WAITBEGIN ) {
- last_get = timeGetTime();
- return ;
- } else if ( m_dwStatus == CONSTAT_WAITPLAYER ) {
- if ( bFresh ) {
- cmdbuf = uiCurrentCmd;
- firebuf = bFired;
- bFresh = false;
- }
- if ( bRecvCmd ) {
- last_get = timeGetTime();
- m_dwStatus = CONSTAT_RUNNING;
- } else {
- end_time = timeGetTime();
- if( end_time - last_get > WAITTIMEOUT ) {
- // TODO: Record the lag
- // m_dwStatus = CONSTAT_WAITERROR;
- DebugOutput( 1, "Wait error!" );
- m_dwStatus = CONSTAT_WAITERROR;
- }
- return ;
- }
- }
- // FPS control, 1000/x in theory
- // x = 20, 30 fps
- while ( timeGetTime() - frame_start < 30 );
- frame_start = timeGetTime();
-
- // Running
- if ( m_dwStatus == CONSTAT_RUNNING ) {
- // Excute commands
- BulletsProc(); // 1- Excute bullets earlier than tanks
- #ifdef _DEBUG_CMDOUTPUT
- char temp[4];
- DebugOutput( 1, "*****Last Get:*****" );
- for ( int i=0; i<DEFTANKNUM; i++ ) {
- itoa(m_pCmd[i].cmd,temp,10);
- DebugOutput( 2, temp, "t" );
- }
- DebugOutput( 1, "rn" );
- #endif
- TanksProc(); // 2- Excute tanks
- FoodProc(); // 3- Excute food
- // Exchange command messages
- m_pCmd[m_nLocal].cmd = cmdbuf;
- m_pCmd[m_nLocal].fire = firebuf;
- bFresh = true;
- CreateAICmd();
- #ifdef _DEBUG_CMDOUTPUT
- DebugOutput( 1, "*****Generate:*****" );
- for ( int k=m_nLocal; k<m_nLocal+DEFAINUM+1; k++ ) {
- itoa(m_pCmd[k].cmd,temp,10);
- DebugOutput( 2, temp, "t" );
- }
- #endif
- SendCmdMsg( &m_pCmd[m_nLocal], DEFAINUM+1, m_nLocal ); // home-0, enemy-5
-
- // wait next message
- m_dwStatus = CONSTAT_WAITPLAYER;
- m_nMsgCounter = 0;
- ZeroMemory( m_pMsgFlag,sizeof(bool)*m_nPlayers);
- bRecvCmd = false;
- }
- // The app-window is not activated, skip refresh screen
- if ( !g_bActive )
- return ;
- // Animations, frame roller
- RollBlockObjects( m_pRiver, m_nNumRiver );
-
- // Redraw land layer
- Display.Clear(0); // level ground
- Display.Blt(0,0,pMapSolid->GetDDrawSurface(), NULL, DDBLTFAST_SRCCOLORKEY);
- // objects layer
- BlitBullets( m_ppBullets, DEFTANKNUM );
- BlitTanks( m_ppTanks, DEFTANKNUM );
- BlitBases( m_ppBases, 2 );
- // Redraw grass layer, level trees
- Display.Blt(0,0,pMapGrass->GetDDrawSurface(), NULL, DDBLTFAST_SRCCOLORKEY);
- BlitFood();
- // FPS display
- counter++;
- end_time = timeGetTime();
- if ( end_time - start_time >= 1000 ) {
- start_time = end_time;
- itoa( counter, fps+5, 10 );
- counter = 0;
- }
- pText->DrawText( NULL, " ", 5, 5, RGB(0,0,0), RGB(0,0,0) );
- pText->DrawText( NULL, fps, 5, 5, RGB(0,0,0), RGB(255,0,0) );
- Display.Blt(0,0,pText->GetDDrawSurface(), NULL, 0);
- // Flip
- Display.Present();
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- int ConsoleNet::BulletsProc()
- {
- RECT rt_mobile;
- POINT step, pt_block;
- DWORD overland;
- int hit_blt, hit_tk, hit_block, hit_base;
- POINT ptChanged[4];
- for ( int i=0; i<DEFTANKNUM; i++ )
- {
- // Create bullet
- if ( m_pCmd[i].fire && !(m_ppTanks[i]->HaveBullet()) ) {
- m_ppBullets[i]->CreateBullet( m_ppTanks[i] );
- pFire->Play();
- }
- else if ( m_ppBullets[i]->GetStat() == OBJSTAT_CADAVER )
- continue;
- // Collision test
- if ( m_ppBullets[i]->GetStat() & OBJSTAT_LIVE )
- {
- // Set coltest parameters
- step = m_ppBullets[i]->GetDirectionUnit();
- rt_mobile = m_ppBullets[i]->GetNextRect();
- // Collistion test of bases
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppBases, 2, -1,
- m_ppBullets[i]->m_uiTeamType, true, hit_base );
-
- // Collistion test of bullets
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppBullets, DEFTANKNUM, i,
- m_ppBullets[i]->m_uiTeamType, true, hit_blt );
- // Collistion test of bullets and tanks
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppTanks, DEFTANKNUM, i,
- m_ppBullets[i]->m_uiTeamType, true, hit_tk );
- pt_block.x = rt_mobile.left;
- pt_block.y = rt_mobile.top;
- // Collistion test of tanks and blocks
- overland = 0;
- hit_block = 4; // don't need to remove blocks
- MapInfo.ColRToB( pt_block, WIDTH_16, step, COL_IRON,
- overland, COL_BRICK, ptChanged, hit_block );
- // Reset and render map
- int level;
- RECT *prc = new RECT;
- for( int j=0; j<hit_block; j++ ) {
- MapInfo.BlockLocator( ptChanged[j].x, ptChanged[j].y, prc, level );
- // if ( level == LAYER_GROUND )
- pddsSolid->BltFast( ptChanged[j].x<<5, ptChanged[j].y<<5, pddsRes, prc, 0 );
- // else if ( level == LAYER_TREE )
- // pddsGrass->BltFast( ptChanged[i].x<<5, ptChanged[i].y<<5, pddsRes, prc, 0 );
- }
- // Boom and destroy
- m_ppBullets[i]->SetPos( pt_block.x, pt_block.y );
- if ( pt_block.x != rt_mobile.left || pt_block.y != rt_mobile.top ||
- hit_block > 0 ) {
- m_ppBullets[i]->NextStat();
- } else if ( hit_tk >= 0 ) {
- m_ppBullets[i]->Hit( m_ppTanks[hit_tk] );
- } else if ( hit_blt >= 0 ) {
- m_ppBullets[i]->Hit( m_ppBullets[hit_blt] );
- } else if ( hit_base >= 0 ) {
- if ( !( m_ppBases[hit_base]->m_ptyBlood.Minus( m_ppBullets[i]->GetAttack() ) ) )
- SendMsg( NETMSGTK_TEAMVICTORY, &hit_base, sizeof(int) );
- m_ppBullets[i]->NextStat();
-
- }
- }
- else
- m_ppBullets[i]->NextStat();
- }
- return 0;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- int ConsoleNet::TanksProc()
- {
- RECT rt_mobile;
- POINT step, pt_block;
- int hit_blt, hit_tk, hit_block, hit_base;
- DWORD overland, blockland;
- bool movetest;
- for ( int i=0; i<DEFTANKNUM; i++ )
- {
- if ( m_ppTanks[i]->GetStat() & OBJSTAT_CADAVER )
- continue;
- if ( m_ppTanks[i]->GetStat() & OBJSTAT_DIE ) {
- m_ppTanks[i]->NextStat();
- pBoom->Play();
- continue;
- }
- // Convert direction
- if ( m_ppTanks[i]->IsHomeTeam() ^ m_bHomeTeam )
- m_pCmd[i].cmd = 5 - m_pCmd[i].cmd;
- // Direction command to excute,
- if ( m_ppTanks[i]->m_uiIceDir != DIR_NONE ) // still sliding
- {
- movetest = true;
- // if on ice, modify slide speed each time
- if ( m_pCmd[i].cmd > DIR_NONE && m_pCmd[i].cmd < DIR_MAX ) {
- // After all, reset main direction
- m_ppTanks[i]->m_uiDirection = m_pCmd[i].cmd;
- // Affect the speed by direction
- if ( m_ppTanks[i]->m_uiIceDir == m_pCmd[i].cmd ) { // speed up
- if ( m_ppTanks[i]->m_nIceStep < MAXSPEEDONICE-2 )
- m_ppTanks[i]->m_nIceStep += 2;
- else
- m_ppTanks[i]->m_nIceStep = MAXSPEEDONICE;
- } else {
- if ( m_ppTanks[i]->m_uiIceDir == 5 - m_pCmd[i].cmd ) // opposite
- m_ppTanks[i]->m_nIceStep -= 4;
- else // others, no effect
- m_ppTanks[i]->m_nIceStep -= 2;
- if ( m_ppTanks[i]->m_nIceStep <= 0 ) {
- movetest = false;
- m_ppTanks[i]->m_uiIceDir = DIR_NONE;
- }
- }
- } else { // natural friction
- m_ppTanks[i]->m_nIceStep -= 2;
- if ( m_ppTanks[i]->m_nIceStep <= 0 ) {
- movetest = false;
- m_ppTanks[i]->m_uiIceDir = DIR_NONE;
- }
- }
- // Set coltest parameters
- step = m_ppTanks[i]->GetDirUnitOnIce();
- rt_mobile = m_ppTanks[i]->GetNextRectOnIce();
- }
- // Test command to see if it's none
- else if ( m_pCmd[i].cmd == DIR_NONE || m_pCmd[i].cmd == DIR_MAX )
- continue;
- else if ( m_ppTanks[i]->m_uiDirection == m_pCmd[i].cmd ) // move forward
- {
- // Set moving animation flag
- m_ppTanks[i]->m_bMoving = true;
- movetest = true;
- // Set coltest parameters
- step = m_ppTanks[i]->GetDirectionUnit();
- rt_mobile = m_ppTanks[i]->GetNextRect();
- }
- else // just turn around
- {
- movetest = false;
- bool dirtest = true;
- int residue;
- // Count a integer step
- rt_mobile = m_ppTanks[i]->GetRect();
- step = m_ppTanks[i]->GetDirectionUnit();
- if ( step.y != 0 ) {
- residue = m_ppTanks[i]->GetTop() % DEFTURNSTEP;
- if ( residue == 0 )
- dirtest = false;
- else if ( residue > DEFTURNSTEP/2 - IsHomeTeam()) { // down
- residue = DEFTURNSTEP - residue;
- rt_mobile.top += residue;
- rt_mobile.bottom += residue;
- } else { // up
- rt_mobile.top -= residue;
- rt_mobile.bottom -= residue;
- }
- } else if ( step.x != 0 ) {
- residue = m_ppTanks[i]->GetLeft() % DEFTURNSTEP;
- if ( residue == 0 )
- dirtest = false;
- else if ( residue > DEFTURNSTEP/2 - IsHomeTeam()) { // right
- residue = DEFTURNSTEP - residue;
- rt_mobile.left += residue;
- rt_mobile.right += residue;
- } else { // left
- rt_mobile.left -= residue;
- rt_mobile.right -= residue;
- }
- }
- // Test if it can move
- if ( dirtest ) {
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppTanks, DEFTANKNUM, i,
- m_ppTanks[i]->m_uiTeamType, false, hit_tk );
- if ( hit_tk < 0 )
- m_ppTanks[i]->SetPos( rt_mobile.left, rt_mobile.top );
- }
- // change direction
- m_ppTanks[i]->m_uiDirection = m_pCmd[i].cmd;
- }
- // Move collision test
- if ( movetest ) {
- // Collistion test of bases
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppBases,
- 2, -1, 0, false, hit_base );
-
- // Collistion test of bullets
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppBullets, DEFTANKNUM, i,
- m_ppTanks[i]->m_uiTeamType, true, hit_blt );
- // Collistion test of tanks
- MapInfo.ColRToMobile( rt_mobile, step, (CMobileObject **)m_ppTanks, DEFTANKNUM, i,
- m_ppTanks[i]->m_uiTeamType, false, hit_tk );
- // Collistion test of tanks and blocks
- pt_block.x = rt_mobile.left;
- pt_block.y = rt_mobile.top;
- overland = COL_ICE;
- blockland = COL_BRICK|COL_IRON;
- if ( !(m_ppTanks[i]->m_dwEquip & EQUIP_SHIP) )
- blockland |= COL_RIVER;
- hit_block = 0; // don't need to remove blocks
- MapInfo.ColRToB( pt_block, WIDTH_32, step, blockland,
- overland, 0, NULL, hit_block );
- // Set ice flag
- if ( overland & COL_ICE ) {
- m_ppTanks[i]->m_bOnIce = true;
- if ( m_ppTanks[i]->m_uiIceDir == DIR_NONE ) {
- m_ppTanks[i]->m_nIceStep = m_ppTanks[i]->GetStep();
- m_ppTanks[i]->m_uiIceDir = m_ppTanks[i]->m_uiDirection;
- }
- }
- else {
- m_ppTanks[i]->m_bOnIce = false;
- m_ppTanks[i]->m_uiIceDir = DIR_NONE;
- }
- // Choose a fewest step
- if ( pt_block.x * step.x < rt_mobile.left * step.x ||
- pt_block.y * step.y < rt_mobile.top * step.y )
- m_ppTanks[i]->SetPos( pt_block.x, pt_block.y );
- else {
- m_ppTanks[i]->SetPos( rt_mobile.left, rt_mobile.top );
- if ( hit_blt >= 0 )
- m_ppBullets[hit_blt]->Hit( m_ppTanks[i] );
- }
- }
- } // for, tanks, num 0-9
- return 0;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::FoodProc() {
- // Collision test
- if ( m_pFood->IsExsit() ) {
- RECT rt_eat = m_pFood->GetPermitRect();
- POINT step = {0,0};
- int hit_tk;
- // Collistion test of tanks
- MapInfo.ColRToMobile( rt_eat, step, (CMobileObject **)m_ppTanks, DEFTANKNUM, -1,
- 0, false, hit_tk );
- if ( hit_tk >= 0 ) { // reset its property
- if ( m_pFood->GetFoodType() == FOOD_KEEPBASE ) {
- POINT pts[9];
- int changed = 9;
- int i = 1-m_ppTanks[hit_tk]->IsHomeTeam();
- m_pProCounter[i] = DEFPROTECTTIME;
- MapInfo.Protect( m_ppBases[i]->GetRect(), true, pts, changed );
- int level;
- RECT *prc = new RECT;
- for( int j=0; j<changed; j++ ) {
- MapInfo.BlockLocator( pts[j].x, pts[j].y, prc, level );
- pddsSolid->BltFast( pts[j].x<<5, pts[j].y<<5, pddsRes, prc, 0 );
- }
- } else {
- m_pFood->Benefit( m_ppTanks[hit_tk] );
- }
- m_pFood->SetExsit(false);
- }
- }
- if ( m_pProCounter[0] == 0 ) {
- POINT pts[9];
- int changed = 9;
- MapInfo.Protect( m_ppBases[0]->GetRect(), false, pts, changed );
- int level;
- RECT *prc = new RECT;
- for( int j=0; j<changed; j++ ) {
- MapInfo.BlockLocator( pts[j].x, pts[j].y, prc, level );
- pddsSolid->BltFast( pts[j].x<<5, pts[j].y<<5, pddsRes, prc, 0 );
- }
- }
- if ( m_pProCounter[0] >= 0 )
- m_pProCounter[0]--;
- if ( m_pProCounter[1] == 0 ) {
- POINT pts[9];
- int changed = 9;
- MapInfo.Protect( m_ppBases[1]->GetRect(), false, pts, changed );
- int level;
- RECT *prc = new RECT;
- for( int j=0; j<changed; j++ ) {
- MapInfo.BlockLocator( pts[j].x, pts[j].y, prc, level );
- pddsSolid->BltFast( pts[j].x<<5, pts[j].y<<5, pddsRes, prc, 0 );
- }
- }
- if ( m_pProCounter[1] >= 0 )
- m_pProCounter[1]--;
-
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::MsgProcessor( CMsgList *ml ) {
- if ( !ml )
- return ;
- CMsgElem *pelem,*peek;
- int lastmsg = 0;
- int *ibuf;
- ml->Lock();
- while ( !ml->IsEmpty() ) {
- peek = ml->Peek();
- if ( !peek || peek->GetMsg() == lastmsg ) {
- ml->UnLock();
- return;
- }
- pelem = ml->Pop();
- lastmsg = pelem->GetMsg();
- // Messages from server
- if ( pelem->GetMsgType() == MSG_NET ) {
- switch ( lastmsg )
- {
- case NETMSGTK_ANSWERSEATINFO:
- ibuf = (int *)(pelem->GetParam());
- m_nPlayers = DEFTANKNUM/(DEFAINUM+1);
- m_nComPerTeam = DEFAINUM;
- m_nLocal = ibuf[0]*(DEFAINUM+1);
- if ( m_nLocal >= DEFTANKNUM/2 )
- m_bHomeTeam = false;
- else
- m_bHomeTeam = true;
- m_dwStatus = CONSTAT_WAITMORE;
- m_pMsgFlag = new bool[m_nPlayers];
- ZeroMemory( m_pMsgFlag,sizeof(bool)*m_nPlayers);
- if ( !CreateConsole( hChildWnd, "Map/Map_02.idx", m_bHomeTeam ) ) {
- MessageBox( NULL, "Create Console Error!", "Sorry", MB_OK );
- m_dwStatus = CONSTAT_ENDGAME;
- DestroyWindow( hMainWnd );
- }
- break;
- case NETMSGTK_WAITMOREPLAYER:
- break;
- case NETMSGTK_MOREPLAYER:
- break;
- case NETMSGTK_GAMEREADY:
- InitObjects();
- SendMsg( NETMSGTK_PLAYERREADY, NULL, 0 );
- m_dwStatus = CONSTAT_WAITBEGIN;
- break;
- case NETMSGTK_GAMESTART:
- SendCmdMsg( &m_pCmd[m_nLocal], 0, m_nLocal );
- m_dwStatus = CONSTAT_WAITPLAYER;
- break;
- case NETMSGTK_CMDFOODDELETE:
- m_pFood->SetExsit(false);
- break;
- case NETMSGTK_CMDFOODCREATE:
- ibuf = (int *)(pelem->GetParam());
- if ( !m_bHomeTeam ) {
- ibuf[1] = g_FrameLeft - ibuf[1];
- ibuf[2] = g_FrameLeft - ibuf[2];
- }
- m_pFood->CreateFood(ibuf[0], ibuf[1], ibuf[2] );
-
- break;
- case NETMSGTK_CMDINFO:
- if ( m_dwStatus != CONSTAT_WAITPLAYER )
- ml->Push(pelem);
- else {
- ibuf = (int *)(pelem->GetParam());
- if ( m_pMsgFlag[ibuf[0]/(m_nComPerTeam+1)] ) {
- ml->Push(pelem);
- break;
- }
- else {
- m_pMsgFlag[ibuf[0]/(m_nComPerTeam+1)] = true;
- memcpy(m_pCmd+ibuf[0], ibuf+2, sizeof(TANKCMD)*ibuf[1]);
- if ( ++m_nMsgCounter == (int)(m_nPlayers-1))
- bRecvCmd = true;
- #ifdef _DEBUG_CMDOUTPUT
- char temp[4];
- DebugOutput( 1, "*****Message Get:*****" );
- for ( int i=ibuf[0]; i<ibuf[0]+DEFAINUM+1; i++ ) {
- itoa(m_pCmd[i].cmd,temp,10);
- DebugOutput( 2, temp, "t" );
- }
- #endif
- }
- }
- break;
- case NETMSGTK_TEAMVICTORY:
- ibuf = (int *)(pelem->GetParam());
- if ( ibuf[0] )
- MessageBox( NULL, "Home Team Win!", "Congratulations", MB_OK );
- else
- MessageBox( NULL, "Enemy Team Win!", "Congratulations", MB_OK );
- m_dwStatus = CONSTAT_ENDGAME;
- DestroyWindow( hMainWnd );
- break;
- case MSGNET_RECEIVEERROR:
- ERRORMSG( pelem->GetParam() );
- break;
- }
- }
- // delete pelem;
- }
- ml->UnLock();
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::BlitFood() {
- if ( m_pFood->IsExsit() ) {
- RECT *prc = new RECT();
- prc->left = 32 + m_pFood->GetFoodType() * 32;
- prc->top = 256;
- prc->right = prc->left + 32;
- prc->bottom = prc->top + 32;
- Display.Blt( m_pFood->GetLeft(), m_pFood->GetTop(), pddsRes,
- prc, DDBLTFAST_SRCCOLORKEY);
- }
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::BlitTanks( CObjTank **pptks, int num ) {
- if ( pptks ) {
- RECT *prc = new RECT();
- int *pres;
- int len;
- DWORD time = timeGetTime();
- for ( int i=0; i<num; i++ ) {
- if ( pptks[i]->GetStat() & OBJSTAT_LIVE ) {
- pres = &ResArray[pptks[i]->GetFrameType()][0];
- prc->left = (*(pres+RESPOS_LEFT))+(pptks[i]->m_uiDirection-1) *
- (*(pres+RESSIZE_WIDTH));
- if ( pptks[i]->IsHomeTeam() )
- prc->left += 256;
- else
- prc->left += 384;
- if ( pptks[i]->m_bMoving ) {
- pptks[i]->NextFrame(time);
- pptks[i]->m_bMoving = false;
- }
- prc->top = *(pres+RESPOS_TOP) + pptks[i]->GetFrameCurrent()*32;
- prc->right = prc->left + (*(pres+RESSIZE_WIDTH));
- prc->bottom = prc->top + (*(pres+RESSIZE_HEIGHT));
-
- Display.Blt( pptks[i]->GetLeft(), pptks[i]->GetTop(), pddsRes,
- prc, DDBLTFAST_SRCCOLORKEY);
- // draw blood
- prc->left = 256;
- prc->top = 552;
- prc->right = prc->left + 31;
- prc->bottom = prc->top + 5;
- Display.Blt( pptks[i]->GetLeft(), pptks[i]->GetTop(), pddsRes,
- prc, DDBLTFAST_NOCOLORKEY);
- len = pptks[i]->GetBloodPercent(29);
- prc->left = 256;
- prc->top = 560;
- prc->right = prc->left + len;
- prc->bottom = prc->top + 3;
- Display.Blt( pptks[i]->GetLeft()+1, pptks[i]->GetTop()+1, pddsRes,
- prc, DDBLTFAST_NOCOLORKEY );
- }
- else if ( pptks[i]->GetStat() & OBJSTAT_DIE ) {
- prc->left = 0 + 32 * pptks[i]->GetFrameCurrent();
- prc->top = 544;
- prc->right = prc->left + 32;
- prc->bottom = prc->top + 32;
- Display.Blt( pptks[i]->GetLeft(), pptks[i]->GetTop(), pddsRes,
- prc, DDBLTFAST_SRCCOLORKEY);
- pptks[i]->NextFrame( time );
- }
- }
- }
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- void ConsoleNet::BlitBullets( CObjBullet **ppbts, int num ) {
- if ( ppbts ) {
- RECT *prc = new RECT();
- DWORD time = timeGetTime();
- // int *pres;
- for ( int i=0; i<num; i++ ) {
- if ( ppbts[i]->GetStat() & OBJSTAT_LIVE ) {
- prc->left = 96 + 32 * ppbts[i]->m_uiDirection;
- prc->top = 544;
- prc->right = prc->left + 16;
- prc->bottom = prc->top + 16;
-
- Display.Blt( ppbts[i]->GetLeft(), ppbts[i]->GetTop(), pddsRes,
- prc, DDBLTFAST_SRCCOLORKEY);
- }else if ( ppbts[i]->GetStat() & OBJSTAT_DIE ) {
- prc->left = 0 + 32 * ppbts[i]->GetFrameCurrent();
- prc->top = 544;
- prc->right = prc->left+32;
- prc->bottom = prc->top+32;
- Display.Blt( ppbts[i]->GetLeft()-8*(1+ppbts[i]->GetDirectionUnit().x),
- ppbts[i]->GetTop()-8*(1+ppbts[i]->GetDirectionUnit().y), pddsRes,
- prc, DDBLTFAST_SRCCOLORKEY);
- ppbts[i]->NextFrame(time);
- }
- }
- }
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::BlitBases( CObjBase **ppbs, int num ) {
- if ( !ppbs )
- return false;
- RECT prc;
- int level;
- for ( int i=0; i<num; i++ ) {
- MapInfo.ObjectLocator( ppbs[i], &prc, level );
- Display.Blt( ppbs[i]->GetLeft(), ppbs[i]->GetTop(),
- pResource->GetDDrawSurface(), &prc, DDBLTFAST_SRCCOLORKEY);
- // draw blood
- prc.left = 256;
- prc.top = 552;
- prc.right = prc.left + 31;
- prc.bottom = prc.top + 5;
- Display.Blt( ppbs[i]->GetLeft(), ppbs[i]->GetTop(), pddsRes,
- &prc, DDBLTFAST_NOCOLORKEY);
- prc.left = 256;
- prc.top = 560;
- prc.right = prc.left + ppbs[i]->m_ptyBlood.GetPercent(30);
- prc.bottom = prc.top + 3;
- Display.Blt( ppbs[i]->GetLeft()+1, ppbs[i]->GetTop()+1, pddsRes,
- &prc, DDBLTFAST_NOCOLORKEY );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------
- bool ConsoleNet::RollBlockObjects( CObjBlock *poblk, int num ) {
- if ( !poblk || num < 0 )
- return false;
- RECT *prc = new RECT;
- int level;
- DWORD time = timeGetTime();
- for ( int i=0; i<num ; i++ ) {
- poblk[i].NextFrame( time );
- MapInfo.ObjBlockLocator( poblk+i, prc, level );
- if ( FAILED( pddsSolid->BltFast( poblk[i].GetLeft(), poblk[i].GetTop(),
- pddsRes, prc, 0 ) ) )
- return false;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- // Name:
- // Desc:
- //-----------------------------------------------------------------------------