MirGameMap.cpp
资源名称:盛大传奇外挂原代码.rar [点击查看]
上传用户:marco7834
上传日期:2010-02-24
资源大小:82k
文件大小:4k
源码类别:
游戏引擎
开发平台:
Visual C++
- // MirGameMap.cpp: implementation of the CMirGameMap class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "MirGameMap.h"
- #include <stdio.h>
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMirGameMap::CMirGameMap()
- {
- m_pData = 0;
- m_DrawWidth = 16;
- m_DrawHeight = 16;
- m_DrawMapWidth = 11;
- m_DrawMapHeight = 11;
- memset( &m_Header, 0, sizeof( m_Header ) );
- }
- CMirGameMap::~CMirGameMap()
- {
- if( m_pData != 0 )
- delete []m_pData;
- }
- void CMirGameMap::Draw(HDC dc, int dx, int dy)
- {
- int i = 0;
- int j = 0;
- if( m_pData == NULL )
- return;
- //HPEN hp = CreatePen( PS_SOLID, 4, RGB( 0, 0, 255 ));
- HBRUSH hbrold = (HBRUSH)GetCurrentObject( dc, OBJ_BRUSH );
- LOGBRUSH lb;
- lb.lbColor = RGB( 255, 0, 0 );
- lb.lbStyle = BS_SOLID;
- lb.lbHatch = 0;
- HBRUSH hbr1 = CreateBrushIndirect( &lb );
- lb.lbColor = RGB( 255, 255, 255 );
- HBRUSH hbr2 = CreateBrushIndirect( &lb );
- lb.lbColor = RGB( 0, 0, 255 );
- HBRUSH hbr3 = CreateBrushIndirect( &lb );
- for( i = 0;i < m_DrawMapWidth;i ++ )
- {
- for( j = 0;j < m_DrawMapHeight;j ++ )
- {
- if( TestMap( i + m_xpos - m_DrawMapWidth/2, j + m_ypos-m_DrawMapHeight/2))
- {
- SelectObject( dc, hbr2 );
- }
- else
- {
- SelectObject( dc, hbr1 );
- }
- PatBlt( dc, i * m_DrawWidth + dx, j * m_DrawHeight + dy, m_DrawWidth, m_DrawHeight, PATCOPY );
- }
- }
- SelectObject( dc, hbr3 );
- PatBlt( dc, (m_DrawMapWidth / 2 ) * m_DrawWidth + dx, ( m_DrawMapHeight / 2 ) * m_DrawHeight + dy, m_DrawWidth, m_DrawHeight, PATCOPY );
- SelectObject( dc, hbrold );
- DeleteObject( hbr1 );
- DeleteObject( hbr2 );
- DeleteObject( hbr3 );
- }
- BOOL CMirGameMap::SetGameMap(char *mapname, DWORD x, DWORD y)
- {
- if( m_pData != NULL )
- delete []m_pData;
- m_pData = Loadmap( mapname );
- if( m_pData == NULL )
- return FALSE;
- m_xpos = x;
- m_ypos = y;
- return TRUE;
- }
- MIRMAPBLOCK* CMirGameMap::Loadmap(char *map)
- {
- char tmpFilename[256];
- FILE * fpmap = NULL;
- MIRMAPBLOCK * pdata = NULL;
- DWORD dsize = 0;
- sprintf( tmpFilename, "%s.map", map );
- fpmap = fopen( tmpFilename, "rb" );
- if( fpmap == NULL )
- {
- sprintf( tmpFilename, "map\%s.map", map );
- fpmap = fopen( tmpFilename, "rb" );
- if( fpmap == NULL )
- {
- MessageBox( 0, "未发现传奇地图信息,自动寻路功能和怪物行动预测功能将无法使用。", "提示", 0 );
- return NULL;
- }
- }
- fread( &m_Header, sizeof( MIRMAPHEADER), 1, fpmap );
- if( m_Header.height == 0 || m_Header.width == 0 )
- return NULL;
- dsize = m_Header.width;
- dsize *= m_Header.height;
- pdata = new MIRMAPBLOCK[dsize];
- if( pdata == NULL )
- return NULL;
- fread( (void*)pdata, dsize * sizeof( MIRMAPBLOCK ), 1, fpmap );
- fclose( fpmap );
- return pdata;
- }
- void CMirGameMap::SetPersonInfo(DWORD x, DWORD y, DWORD dir)
- {
- m_xpos = x;
- m_ypos = y;
- m_dir = dir;
- }
- int xofs_walk2[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
- int yofs_walk2[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
- BOOL CMirGameMap::TestMapTo(int x, int y, int dir)
- {
- int tx, ty;
- if( dir < 0 || dir > 7 )
- return FALSE;
- tx = x + xofs_walk2[dir];
- ty = y + yofs_walk2[dir];
- return TestMap(tx,ty);
- }
- BOOL CMirGameMap::TestMap(int x, int y)
- {
- if( x >= m_Header.width || y >= m_Header.height || x < 0 || y < 0 )
- {
- return FALSE;
- }
- if( m_pData == NULL )
- return TRUE;
- if( m_pData[x * m_Header.height + y].tile & 0x8000 )
- {
- return FALSE;
- }
- return TRUE;
- }