⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemap.pas

📁 网络游戏神迹脱机外挂PASCAL源码
💻 PAS
字号:
unit GameMap;

interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs, GameCode;

type
Point=record
  x:word;
  y:word;
  end;

type
TTmpPnt=class(TObject)
  private
  PntList:TList;
  public
  constructor Create  ;
  Destructor Destroy  ; override;
  function GetHeader  : Point;
  function GetSize    : Integer;
  procedure AddTail(x:word;y:word);
  function GetTail    : point;
  end;

type
TMapInfo=class (TObject)
  private
  MapName : string;
  MapXMax : word;
  MapYMax : word;
  MapData : array[0..1048576] of byte;

  public
  
  constructor Create;
  destructor Destroy;override;
  function ReadMap(FName:string):Integer;
  //从文件中读入游戏地图
  function TestPoint(x:word;y:word):Boolean;
  //测试地图上的点是否是障碍,true为没有,false为有
  //头一个点为(1,1)不存在(0,0)点,注意。
  procedure DrawMap();
  //测试用
  function GetXMax:word;
  //取得XMax
  function GetYMax:word;
  //取得YMax
  function GetMapName:string;
  //取得地图文件名字 但带有文件夹,需要修改的。
  function FindRoad(StartP:Point;EndP:Point;RPList:TTmpPnt):Boolean;
  //寻路函数
  end;



implementation

const
xofd1:array[0..7] of Integer=(0,1,1,1,0,-1,-1,-1);
yofd1:array[0..7] of Integer=(1,1,0,-1,-1,-1,0,1);
xofd2:array[0..7] of Integer=(0,2,2,2,0,-2,-2,-2);
yofd2:array[0..7] of Integer=(2,2,0,-2,-2,-2,0,2);

constructor TTmpPnt.Create;
begin   
inherited Create;
PntList:=TList.Create;
end;

destructor TTmpPnt.Destroy;
var
i : Integer;
pt: ^Point;
begin
for i:=0 to PntList.Count-1 do
  begin
  pt:=PntList.Items[i];
  Dispose(pt);
  PntList.Delete(i);
  end;
PntList.Free;
end;

function TTmpPnt.GetSize:Integer;
begin
GetSize:=PntList.Count;
end;

function TTmpPnt.GetHeader:Point;
var
pp1 : ^point;
p2 : Point;
begin
if PntList.Count>0 then
  begin
  pp1:=PntList.Items[0];
  p2:=pp1^;
  Dispose(pp1);
  pntList.Delete(0);
  end
else
  begin
  p2.x:=0;
  p2.y:=0;
  end;
GetHeader:=p2;
end;

procedure TTmpPnt.AddTail(x:word;y:word);
var
pp1 : ^point;
begin
new(pp1);
pp1.x:=x;
pp1.y:=y;
PntList.Add(pp1);
end;

function TTmpPnt.GetTail:point;
var
c   : Integer;
pp  : ^point;
begin
c   :=  PntList.Count-1;
if c=-1 then
  begin
  DBGOut('Empty TTmpPnt',[]);
  exit;
  end;
pp  :=  PntList.Items[c];
GetTail:=PP^;
dispose(pp);
PntList.Delete(c);
end;


constructor TMapInfo.Create;
begin
Inherited Create;
MapXMax:=0;
MapYMax:=0;
end;

destructor TMapInfo.Destroy;
begin
end;

function TMapInfo.ReadMap(FName:string):Integer;
var
MapFile:file;
Buf:array[0..16] of word;//存放地图头信息
NUM:integer;
begin
MapName:=FName;
assignfile(MapFile,FName);
if FName='' then
  begin
  ReadMap:=0;
  exit;
  end;
try
  reset(MapFile,1);
  seek(MapFile,0);
except on EInOutError do
    begin
    ReadMap:=-1;
    DBGOut('ReadMap:EInOutError ',[]);
    exit;
    end;
  end;
try
  BlockRead(MapFile,Buf,32);
  MapXMax:=Buf[4];
  MapYMax:=Buf[6];
  BlockRead(MapFile,MapData,MapXMax*MapYMax,NUM);
  if NUM<>(MapXMax*MapYMax) then
    ShowMessage('TMapInfo.ReadMap:ReadError');
finally
  CloseFile(MapFile);
  end;
ReadMap:=1;
end;

function TMapInfo.TestPoint(x:word;y:word):Boolean;
begin
if MapData[(x-1)+(y-1)*MapXMax]=0 then
  TestPoint:=true
else
  TestPoint:=false;
end;

procedure TMapInfo.DrawMap;
begin
end;

function TMapInfo.GetXMax:word;
begin
GetXMax:=MapXMax;
end;

function TMapInfo.GetYMax:word;
begin
GetYMax:=MapYMax;
end;

function TMapInfo.GetMapName:string;
begin
GetMapName:=MapName;
end;


function TMapInfo.FindRoad(StartP:Point;EndP:Point;RPList:TTmpPnt):Boolean;
//这个是寻路函数,但没写完
var
SList       : array of integer;
i,m,d,nx,ny : Integer;
Tp          : TTmpPnt;
p1          : Point;
nstep       : Integer;
nflag       : Boolean;
begin

nflag:=false;

if (StartP.x>MapXMax) or (EndP.x>MapXMax) or (StartP.y>MapYMax) or (EndP.y>MapYMax) then
  begin
  FindRoad:=false;
  ShowMessage('地图未包含该点');
  exit;
  end;

if (StartP.x=EndP.x) and (StartP.y=EndP.y) then
  begin
  FindRoad:=false;
  ShowMessage('两点相同');
  exit;
  end;

if TestPoint(StartP.x,StartP.y)=false then
  begin
  FindRoad:=false;
  ShowMessage('起点是障碍物');
  exit;
  end;

if Testpoint(EndP.x,EndP.y)=false then
  begin
  FindRoad:=false;
  ShowMessage('终点是障碍物');
  exit;
  end;

//最大化SList
d:=MapXMax*MapYMax-1;
SetLength(SList,MapXMax*MapYMax);
for i:=0 to d do
  begin
  SList[i]:=d;
  end;
Tp:=TTmpPnt.Create;
Tp.AddTail(StartP.x,StartP.y);
SList[StartP.x-1+(StartP.y-1)*MapXMax]:=0;


while Tp.GetSize>0 do
  begin
  nflag:=false;
  p1:=Tp.GetHeader;
  for m:=0 to 7 do
    begin
    nx:=p1.x+xofd1[m];
    ny:=p1.y+yofd1[m];
    nstep:=SList[(p1.x-1)+(p1.y-1)*MapXMax];
    if (nx>0) and (nx<=MapXMax) and (ny>0) and (ny<MapYMax) then
      begin
      if testpoint(nx,ny)=true and (SList[(nx-1)+(ny-1)*MapXMax]=d)then
        begin
        nflag:=true;
        SList[(nx-1)+(ny-1)*MapXMax]:=nstep+1;
        //DBGOut('nstep=%d,x=%d,y=%d',[SList[(nx-1)+(ny-1)*MapXMax],nx,ny]);
        Tp.AddTail(nx,ny);
        if (nx=EndP.x) and (ny=EndP.y) then
          begin
          RPList.AddTail(nx,ny);
          break;
          end;
        end;
      end;
    end;

  end;

nx:=  EndP.x;
ny:=  EndP.y;
nstep:=SList[(nx-1)+(ny-1)*MapXMax];
while (nx<>StartP.x) or (ny<>StartP.y) do
  begin
  nflag:=false;
 // DBGOut('2',[]);
  for m:=0 to 7 do
    begin
    if SList[nx-1+xofd2[m]+(ny-1+yofd2[m])*MapXMax]=nstep-2 then
      begin
      if TestPoint(nx+xofd1[m],ny+yofd1[m])=true then
        begin
        nflag:=true;
        //DBGOut('x=%d,y=%d',[nx,ny]);
        nstep:=nstep-2;
       // DBGOut('nstep=%d',[nstep]);
        nx:=nx+xofd2[m];
        ny:=ny+yofd2[m];
        RPList.AddTail(nx,ny);
        break;
        end;
      end;
    end;
    
  if nflag=false then
    begin
    for m:=0 to 7 do
      begin
      if SList[nx-1+xofd1[m]+(ny-1+yofd1[m])*MapXMax]=nstep-1 then
        begin
        nflag:=true;
      //DBGOut('x=%d,y=%d',[nx,ny]);
        nstep:=nstep-1;
        nx:=nx+xofd1[m];
        ny:=ny+yofd1[m];
        RPList.AddTail(nx,ny);
        break;
        end;
      end;
    if nflag=false then
      begin
      FindRoad:=false;
      exit;
      end;
    end;
  end;

Tp.Free;
FindRoad:=true;
end;


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -