📄 mapinfo.pas
字号:
unit MapInfo;
interface
uses Windows,SysUtils,Classes,Dialogs;
type
TBmpHeader = packed record //位图文件 定义
// 1. 位图文件头 共14字节
bmType : word;
bfSize : DWORD;
bfReserved0 : word;
bfReserved1 : word;
bfOffBits : DWORD;
//2. 位图信息头 共40字节
BHSize : DWORD;
Width : DWORD;
Height : DWORD;
Planes : word ;
Pixel : word ;
biCompression: DWORD;
biSizeImage : DWORD;
biXPelsPerMeter : Dword;
biYPelsPerMeter : Dword;
biClrUsed : DWORD;
biClrImportant : DWORD;
//3. 彩色表 黑白图有两个表
rgbBlue : byte;
rgbGreen : byte;
rgbRed : byte;
rgbReserved : byte;
rgbBlue1 : byte;
rgbGreen1 : byte;
rgbRed1 : byte;
rgbReserved1 : byte;
end;
TMapHeader = record //千年地图文件头 28字节
name : array [0..15] of char;
d1 : dword;
Width : dword;
Height : dword;
end;
type
TMapInfo = class
public
MapData : array of array of byte;
function LoadMap(FileName:string) :bool; //加载地图文件。获取数据
function GetMapHeight():integer; //获取地图宽度
function GetMapWidth():integer; //获取地图长度
procedure SaveToBmp(Filename :string); //保存地图到位图文件
procedure SaveToFile(Filename:string); //保存地图到文本文件
constructor Create;
Destructor Destroy; override;
private
MapWidth,MapHeight : integer;
end;
var
MapInfor : TMapInfo;
implementation
constructor TMapInfo.Create;
begin
inherited Create;
MapWidth := 400;
MapHeight := 400;
end;
Destructor TmapInfo.Destroy;
begin
end;
///////////////////////////////////////////////////////////
//函数:LoadMap
//功能:提取千年地图信息
//参数:地图文件名 string
//返回:true 成功 false 失败
//说明:千年地图数据格式为 40 * 40
// 从左到右 从上到下 如一个 200 * 200 的地图存储如下
// 1 2 3 4 5
//
// 6 7 8 9 10
//
// 11 12 13 14 15
//
// 16 17 18 19 20
//
// 21 22 23 24 25
//
// 每格之间用20个字节的0 组成
///////////////////////////////////////////////////////////
Function Tmapinfo.LoadMap(FileName: string):bool;
var
MapHeader : TMapHeader;
MapFile : File;
x,y,i,j,k,Size: integer;
count : integer;
temp : array [0..$4b14] of byte;
begin
if FileName <> '' then
begin
try
AssignFile(MapFile,FileName); //建立文件关联
Reset(MapFile,1); //打开文件
Seek(MapFile,0); //移动指针
Blockread(MapFile,MapHeader,28,size); //read map header information
if Size <> 28 then
begin
ShowMessage ('读取文件头出错,请检查文件的有效性!');
LoadMap := False;
exit;
end;
MapWidth := MapHeader.Width;
MapHeight:= MapHeader.Height;
j := 0;
k := 0;
MapData := nil;
// 40*40个坐标点 每点 12个字节 开头20个0 40*40*12+20= 4B14
count := MapHeight * MapWidth div 1600; //循环次数 地图数据数量
SetLength(MapData, MapWidth+1, MapHeight+1);
for i := 1 to count do
begin
BlockRead(MapFile, temp, $4B14, size); //read map node information
if Size <> $4B14 then
begin
ShowMessage('读取的内容长度出错');
LoadMap := False;
exit;
end;
for x := 0 to 39 do
begin
for y := 0 to 39 do
begin
MapData[x+j, y+k] := temp[31 + x * 480 + y * 12 ];
end;
end;
k := k + 40; //坐标轴转换
if k = MapWidth then
begin
k := 0;
j := j + 40;
end;
end;
Finally
CloseFile(MapFile);
result := True;
end;
end else
result := False;
end;
///////////////////////////////////////////////////////////
//函数:SaveToBmp
//功能:保存处理后地图为bmp格式
//参数:地图文件名 string
//返回:
//说明:
///////////////////////////////////////////////////////////
procedure Tmapinfo.SaveToBmp(Filename :string );
var
BmpHeader : TBmpHeader;
BufSize : Cardinal;
FileHwnd : integer;
filesize : integer;
i,x,y : integer;
begin
filesize := Mapwidth * MapHeight + 64;
with BmpHeader do
begin
bmType :=$4D42;
bfSize :=sizeof(BmpHeader) + Mapwidth * MapHeight; //注意 可能出错
bfReserved0 :=0;
bfReserved1 :=0;
bfOffBits :=$40;
BHSize :=$28;
Width :=Mapwidth;
Height :=MapHeight;
Planes :=1;
Pixel :=1; // 1为两色 4 为16色 8 256色
biCompression:=BI_RGB;
biSizeImage :=Mapwidth * MapHeight;
biXPelsPerMeter :=MapWidth;
biYPelsPerMeter :=MapHeight;
biClrUsed :=0;
biClrImportant :=0;
//彩色表 黑白图 共有 两个彩色表
rgbBlue := 0; //白色
rgbGreen := 0;
rgbRed := 0;
rgbReserved := 0;
rgbBlue1 := $FF; //黑色
rgbGreen1 := $FF;
rgbRed1 := $FF;
rgbReserved1 := 0;
end;
FileHwnd := CreateFile(pchar(Filename), GENERIC_WRITE,FILE_SHARE_WRITE, nil,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if FileHwnd = INVALID_HANDLE_VALUE then
begin
ShowMessage('创建文件出错');
CloseHandle(FileHwnd);
exit;
end else
WriteFile(FileHwnd, BmpHeader, sizeof(BmpHeader), BufSize, nil);
x := 0;
y := 0;
for i := 1 to MapWidth * MapHeight do
begin
WriteFile(FileHwnd, MapData[x,y], 1, BufSize, nil);
y := y + 1;
if y = MapWidth then
begin
y := 0;
x := x + 1;
end;
end;
CloseHandle(FileHwnd);
end;
///////////////////////////////////////////////////////////
//函数:SaveToFile
//功能:保存处理后地图后的地图文件供外挂调用
//参数:地图文件名 string
//返回:
//说明:地图数据格式为
// 文件头 长+宽
// 地图数据 每个节点用一个字节表标。从左到右。从上到下。
///////////////////////////////////////////////////////////
procedure Tmapinfo.SaveToFile(Filename:string);
var
FileHwnd : integer;
BufSize : Cardinal;
i,x,y : integer;
begin
FileHwnd := CreateFile(pchar(Filename), GENERIC_WRITE,FILE_SHARE_WRITE, nil,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if FileHwnd = INVALID_HANDLE_VALUE then
begin
ShowMessage('创建文件出错');
CloseHandle(FileHwnd);
exit;
end else
try
WriteFile(FileHwnd, MapWidth, 4, BufSize, nil);
WriteFile(FileHwnd, MapHeight, 4, BufSize, nil);
x := 0;
y := 0;
for i := 0 to MapWidth * MapHeight do
begin
WriteFile(FileHwnd, MapData[x,y], 1, Bufsize, nil);
y := y + 1;
if y = MapWidth then
begin
y := 0;
x := x + 1;
end;
end;
except
showmessage('写入出错');
end;
CloseHandle(FileHwnd);
end;
function Tmapinfo.GetMapWidth :integer;
begin
GetMapWidth := MapWidth;
end;
function Tmapinfo.GetMapHeight :integer;
begin
GetMapHeight := MapHeight;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -