unit GMGame;

interface
   uses Windows;
    
const
  MaxMapW=27;
  MaxMapH=30;
  CellWH=28;
  CellWH2=14;

   mpcZero=0;
   mpcPoint=1;
   mpcSuperPoint=2;
   mpcStartGhost=3;
   mpcMagnetGhost=4;
   mpcStartPacman=5;
   mpcBonusPoint=6;
   mpcWall=7;
   mpcHull=69;

   c_unit=1;
   c_building=2;
   c_tree=3;
   c_rock=4;
   c_mon=5;

  mvUp=1;
  mvRight=2;
  mvDown=4;
  mvLeft=8;

  NBTEST = 18;


type
  TMapCell=record
	mark:Byte;
	layer:Byte;
	x:Byte;
	y:Byte;
        lay2type:Byte;
        lay2number:Byte;
end;

type
  TOPlayer=object
   x,y:Integer;
   NewDir:Byte;
   Dir:Byte;
   startx,starty:Integer; 
   More: byte;
   Pic: byte;

   aTimer:Integer;

   procedure Init(nx,ny:Integer);
   procedure Update;
   function CanChange:Integer;
   function CanContinue: boolean;
   procedure Draw;

end;

type
  TOGhost=object
   x,y:Integer;
   NewDir:Byte;
   Dir:Byte;

   AItype:Byte;

   Speed:Integer;


   More: byte;
   Pic: byte;

   aTimer:Integer;

   BlockedTimer:Integer; 
   isBlocked:Boolean;
   isSeek:Boolean;

   Trace: byte;
   aWantDirs: array[1..NBTEST] of Byte; //путь
   aWantTDirs: array[1..NBTEST] of TPoint; //путь

  procedure Init(nx,ny:integer;AI:Byte);
  function CanContinue: boolean;
  function CanChange:Integer;
  procedure Update;
  procedure SeekPt;
  procedure Draw;

end;

type
  TOGameMap=object

  Width:Byte;	//ширина карты
  Height:Byte;	//высота карты
  offsetx:Integer;
  offsety:Integer;

  isFindmode:Boolean;

  PointsCount:Integer;

  Texture:array[0..12] of cardinal;
  HeroTexture:array[0..20] of cardinal;
  TreeTexture:array[0..12] of Cardinal;
  PointsTexture:array[0..10] of Cardinal;

  GhostPos:array of TPoint;
  BonusPos:array of TPoint;

  map:array of array of TMapCell;

  gmap_block,
  map_block:array of array of Byte;



  procedure Procces();
  procedure Draw();
  procedure Load(Name:string);
  procedure Init;
  procedure Clear;
  procedure UnLoad;
  function  GetBlockValue(x,y:Integer):Integer;
  procedure SetBlockValue(x,y:Integer;value:Integer);
  procedure CloneBlockMap;

  procedure NewGame;
  procedure Refresh;
end;


var
   MyGameMap:TOGameMap;
   MyPacMan :TOPlayer;
   MyGhost:array[0..2] of TOGhost;

   GhostA:array[0..2]of Cardinal;
   GhostB:array[0..2]of Cardinal;
   GhostC:array[0..2]of Cardinal;
   GhostD:array[0..2]of Cardinal;
   GhostE:array[0..2]of Cardinal;

   MylifePic:Cardinal;

   GhostCount:Integer=2;
   GhostIndex:Integer=-1;

   ImGhost: boolean;
   ImImmortal:Boolean;

   BonusTimer:Integer;
   nBonus:Integer;
   isBonusActive:Boolean;

   GhostSeekTimer:Cardinal;

   Myscore:Integer=0;
   MyLife:Integer=0;

   HeroSpeed:Integer=2;
   DGhostSpeed:Single=0.0;

   DrawUpLayer:Boolean=True;

   WinPic:array[0..2] of Cardinal;
   BonusTexture:array[0..5] of Cardinal;

   isWinstate:Integer=0;

procedure UpdateGhost();

implementation
  uses
  IDX_sound,
  GMVars,
  Textures,
  GMUtilite;


function GetTileFromPx(value:Integer):Integer;
begin
  Result:=Trunc(value/CellWH);
end;


procedure UpdateGhost();
var
  i:integer;
begin

   for i:=0 to GhostCount do
   begin
    MyGhost[i].Update;
   end;
end;


procedure TOGameMap.NewGame();
begin
 BonusTimer:=GetTickCount;
 ImGhost:=False;
 ImImmortal:=False;

 Load('data\levels\001.map');
end;

procedure TOGameMap.Procces();
var
  dx,dy:integer;
begin

 if (GetTickCount-BonusTimer)> 20000 then
 begin
   dx:=BonusPos[0].X;
   dy:=BonusPos[0].Y;
   if isBonusActive=False then
   begin
       BonusTimer:=GetTickCount;
       nBonus := Random(5);
       ImGhost:=False;
       ImImmortal:=False;
       isBonusActive:=True;

     if (GetBlockValue(dx,dy) = mpcPoint) then Dec(PointsCount);

      SetBlockValue(dx,dy, mpcBonusPoint);

      if PointsCount <=0 then begin isWinstate:=3; PlaySoundFromMEM(4); end;

      Exit;

   end;
    ImGhost:=False;
    ImImmortal:=False;
 end;

end;

procedure TOGameMap.Refresh;
var
  p0,p1:Integer; 
begin
  MyPacMan.Init(MyPacMan.startx,MyPacMan.starty);

 for p0:=0 to GhostCount do
 begin
   p1:=Random(High(GhostPos));
   MyGhost[p0].Init(GhostPos[p1].X,GhostPos[p1].Y,p0);
 end;

 ImGhost:=False;
 ImImmortal:=False;
 isBonusActive:=True;


end;

procedure TOGameMap.Draw();
var
  xx,yy,x,y,r,n:Integer;
  s:string;
  m_x,m_y,kadr:Integer;//положение карты на экране

  TileY:Integer;
   TileXOffset,
 TileYOffset:Integer;
begin

 TileY:= Trunc(offsety / CellWH);

 glBlend(1);
 for x:=0 to Width do
 for y:=TileY to Range(TileY + 20, 0, Height) do
 begin
   TileXOffset := (X * CellWH)+offsetx;// - blocksize;
   TileYOffset := ((Y - TileY) * CellWH) - (offsety Mod CellWH);

   DrawTextureBoxIJ(TileXOffset,TileYOffset,
                   CellWH,CellWH,
                   Texture[map[x,y].layer-1],
                   $FFFFFF,
                   255,
                   False,
                   0,
                   map[x,y].x,
                   map[x,y].y);

   if map_block[x,y]=mpcPoint then
   DrawTextureBox(TileXOffset,TileYOffset, 32,32,PointsTexture[0]);

   if map_block[x,y]=mpcSuperPoint then
   DrawTextureBox(TileXOffset-2,TileYOffset-4,32,32,PointsTexture[1]);

   if map_block[x,y]=mpcBonusPoint then
   DrawTextureBox(TileXOffset-2,TileYOffset-4,32,32,BonusTexture[nBonus]);


 end;



 m_x:= GetTileFromPx(MyPacMan.x);
 m_y:= GetTileFromPx(MyPacMan.y);

 for y:=TileY to Range(TileY + 20, 0, Height) do
 for x:=Width downto  0 do
 begin

   TileXOffset := (X * CellWH)+offsetx;// - blocksize;
   TileYOffset := ((Y - TileY) * CellWH) - (offsety Mod CellWH);

    r:=map[x,y].lay2type;
    n:=map[x,y].lay2number;


    if (x=m_x) and (y=m_y) then MyPacMan.Draw;


    for n:=0 to GhostCount do
      if (x=GetTileFromPx(MyGhost[n].x)) and (y=GetTileFromPx(MyGhost[n].y)) then MyGhost[n].Draw;

    if DrawUpLayer then
            if(r=c_tree) then
            begin
              DrawTextureBox(TileXOffset-16,TileYOffset-(128-16),
                   64,128,TreeTexture[n]);
            end;


 end;

   DrawRectangle2D(0,SCRH-CellWH-10,SCRW,SCRH,$ffffff,255,0,True);
   SText2D(MySysfont[2],IntToStr(PointsCount),10,SCRH-15 ,Rgb(255,0,0),255);

   SText2D(MySysfont[2],IntToStr(Myscore),SCR2W,SCRH-15 ,Rgb(255,0,0),255);

   for n:=0 to MyLife do
   begin
      DrawTextureBox(SCRW-(32*(n+1)),SCRH-32,32,32,MylifePic);
   end;

end;

procedure TOGameMap.Load(Name:string);
var
  F2:TextFile;
  F1: File of Byte;
  x,y:Integer;
  p0,p1,p2,p3,p4:integer;
  s:string;
begin
   PointsCount:=0;
   GhostIndex:=-1;
   isFindmode:=False;
   isBonusActive:=False;
   Myscore:=0;
   MyLife:=2;
   isWinstate:=0;

    AssignFile(F1, Name);
    Reset(F1);

    Read(F1, Width);
    Read(F1, Height);

    SetLength(map,Width+1,Height+1);
    SetLength(map_block,Width+1,Height+1);
    SetLength(GhostPos,0);
    SetLength(BonusPos,0);

    for y:=0 to height do
    for x:=0 to Width do
    begin
     Read(f1,map[x,y].mark);
     Read(f1,map[x,y].layer);
     Read(f1,map[x,y].x);
     Read(f1,map[x,y].y);
     Read(f1,map[x,y].lay2type);
     Read(f1,map[x,y].lay2number);
     Read(f1,map_block[x,y]);

     case map_block[x,y] of
       mpcPoint: begin Inc(PointsCount);end;
       mpcStartPacman:
                      begin
                        SetBlockValue(x,y,mpcZero);
                        MyPacMan.Init(x,y);
                      end;
       mpcStartGhost: begin
                        SetBlockValue(x,y,mpcZero);
                        p0:=High(GhostPos)+1;
                        SetLength(GhostPos,p0+1);
                        GhostPos[p0].X:=x;
                        GhostPos[p0].Y:=y;
                      end;
       mpcBonusPoint: begin
                        SetBlockValue(x,y,mpcZero);
                        p0:=High(BonusPos)+1;
                        SetLength(BonusPos,p0+1);
                        BonusPos[p0].X:=x;
                        BonusPos[p0].Y:=y;
                      end;
     end;

    end;

 CloseFile(F1);

 offsetx:= (SCRW - (Width+1)*CellWH) div 2;

 for p0:=0 to GhostCount do
 begin
   p1:=Random(High(GhostPos));
   MyGhost[p0].Init(GhostPos[p1].X,GhostPos[p1].Y,p0);
 end;

end;

procedure TOGameMap.Init;
var
  i:Integer;
begin
  Width:=MaxMapW;
  Height:=MaxMapH;
  offsetx:=0;
  offsety:=0;
  
  

 LoadTexture('data\Gfx\snow.png',Texture[0]);
 LoadTexture('data\Gfx\snow2.png',Texture[1]);
 LoadTexture('data\Gfx\Point1.png',PointsTexture[0]);
 LoadTexture('data\Gfx\SPoint1.png',PointsTexture[1]);
 LoadTexture('data\Gfx\pack.png',HeroTexture[0]);

 for i:=0 to 4 do
  LoadTexture('data\gfx\tree'+inttostr(i+1)+'.png',TreeTexture[i]);

 LoadTexture('data\Gfx\gh10.png',GhostA[0]);
 LoadTexture('data\Gfx\gh11.png',GhostA[1]);
 LoadTexture('data\Gfx\gh12.png',GhostA[2]);

 LoadTexture('data\Gfx\gh20.png',GhostB[0]);
 LoadTexture('data\Gfx\gh21.png',GhostB[1]);
 LoadTexture('data\Gfx\gh22.png',GhostB[2]);

 LoadTexture('data\Gfx\gh30.png',GhostC[0]);
 LoadTexture('data\Gfx\gh31.png',GhostC[1]);
 LoadTexture('data\Gfx\gh32.png',GhostC[2]);

 LoadTexture('data\Gfx\gh40.png',GhostD[0]);
 LoadTexture('data\Gfx\gh41.png',GhostD[1]);
 LoadTexture('data\Gfx\gh42.png',GhostD[2]);

 LoadTexture('data\Gfx\gh50.png',GhostE[0]);
 LoadTexture('data\Gfx\gh51.png',GhostE[1]);
 LoadTexture('data\Gfx\gh52.png',GhostE[2]);

 LoadTexture('data\Gfx\Life.png',MylifePic);


 LoadTexture('data\Gfx\LoseLife.png',WinPic[0]);
 LoadTexture('data\Gfx\LoseGame.png',WinPic[1]);
 LoadTexture('data\Gfx\Win.png',WinPic[2]);


 LoadTexture('data\Gfx\b1.png',BonusTexture[0]);
 LoadTexture('data\Gfx\b2.png',BonusTexture[1]);
 LoadTexture('data\Gfx\b3.png',BonusTexture[2]);
 LoadTexture('data\Gfx\b4.png',BonusTexture[3]);
 LoadTexture('data\Gfx\b5.png',BonusTexture[4]);

 PlayMusic('sound517.mp3');

end;

procedure TOGameMap.Clear;
begin

end;

procedure TOGameMap.UnLoad;
begin

end;  

procedure TOGameMap.CloneBlockMap;
begin
  gmap_block:=map_block;
  SetLength(gmap_block,Width+1,Height+1);
  isFindmode:=True;
end;

function TOGameMap.GetBlockValue(x,y:Integer):Integer;
begin
 Result:=mpcWall;//mpcHull;

 if x<0 then Exit;
 if x>Width then Exit;
 if y<0 then Exit;
 if y>Height then Exit;

 if isFindmode then
  Result:=gmap_block[x,y]
 else
  Result:=map_block[x,y];
end;

procedure TOGameMap.SetBlockValue(x,y:Integer;value:Integer);
begin

 if x<0 then Exit;
 if x>Width then Exit;
 if y<0 then Exit;
 if y>Height then Exit;

 if isFindmode then
  gmap_block[x,y]:=value
 else
 map_block[x,y]:=value;

end;

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

procedure TOPlayer.Init(nx,ny:Integer);
begin
  x:=nx*CellWH;
  y:=ny*CellWH;
  Dir:=0;
  NewDir:=0;
  More:=0;
  Pic:=0;

  startx:=nx;
  starty:=ny;
  
end;

function TOPlayer.CanContinue: boolean;
var
  dx, dy: integer;
begin
  result := true;

  if (x+y) mod CellWH = 0 then
  begin

    dx := GetTileFromPx(x);
    dy := GetTileFromPx(y);

    case dir of
      mvRight: begin dx := GetTileFromPx(x+CellWH); dy := GetTileFromPx(y); end;
      mvLeft: begin dx := GetTileFromPx(x-1); dy := GetTileFromPx(y); end;
      mvUp: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y-1); end;
      mvDown: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y+CellWH); end;
    end;

    result := (MyGameMap.GetBlockValue(dx,dy)<>mpcWall)or(ImGhost);
  end;
end;

function TOPlayer.CanChange:Integer;
var
  dx, dy: integer;
begin
 Result := dir;

  if ((x+y) mod CellWH = 0) then
  begin
  
    dx := GetTileFromPx(x);
    dy := GetTileFromPx(y);

    case NewDir of
      mvRight: begin dx := GetTileFromPx(x+CellWH); dy := GetTileFromPx(y); end;
      mvLeft: begin dx := GetTileFromPx(x-1); dy := GetTileFromPx(y); end;
      mvUp: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y-1); end;
      mvDown: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y+CellWH); end;
    end;

    if MyGameMap.GetBlockValue(dx,dy)<>mpcWall then Result:=NewDir;

  end;

end;

procedure TOPlayer.Draw;
begin
  DrawTextureBox(x+MyGameMap.offsetx-16,y-MyGameMap.offsety-(64-20),
                   64,64,
                   MyGameMap.HeroTexture[0]);

end;

procedure TOPlayer.Update;
var
  dx,dy,nVar:Integer;
begin

  if Keys[VK_Left] then
   begin
     newDir:=mvLeft;
     Keys[VK_Left]:=false;
   end;

   if Keys[VK_Right] then
   begin
      newDir:=mvRight;
      Keys[VK_Right]:=false;
   end;

   if Keys[VK_Up] then
   begin
    newDir:=mvUp;
    Keys[VK_Up]:=false;
   end;

   if Keys[VK_Down] then
   begin
     newDir:=mvDown;
     Keys[VK_Down]:=false;
   end;



  dir:= CanChange;

  if CanContinue then
  begin

    case dir of
        mvRight:
        begin
          Inc(x,HeroSpeed);
          if (x>((MyGameMap.Width)*CellWH -MyGameMap.offsetx )) then x:= 0;
        end;
        mvLeft:
        begin
          Dec(x,HeroSpeed);
          if (x<0) then x:=(MyGameMap.Width+1)*CellWH;
        end;
        mvUp:
        begin
          Dec(y,HeroSpeed);
          if (y<0) then y:= (MyGameMap.Height+1)*CellWH;
        end;
        mvDown:
        begin
          Inc(y,HeroSpeed);
          if (y>(MyGameMap.Height)*CellWH) then y:=0;
        end;
    end;

    ////////////////////////////

       MyGameMap.offsety:=Range((y - (9 * CellWH)), 0, (MyGameMap.Height - 19) * CellWH);

      if (x+y) mod CellWH = 0 then
      begin

        dx:=GetTileFromPx(x);
        dy:=GetTileFromPx(y);

        if (MyGameMap.GetBlockValue(dx,dy) = mpcPoint) then
        begin
          Inc(Myscore,10);
          PlaySoundFromMEM(Random(3));
          Dec(MyGameMap.PointsCount);
          MyGameMap.SetBlockValue(dx,dy,mpcZero);
          if MyGameMap.PointsCount <=0 then begin isWinstate:=3;  PlaySoundFromMEM(4); end;
        end
        else
        if (MyGameMap.GetBlockValue(dx,dy) = mpcSuperPoint) then
        begin
          Inc(Myscore,100);
          PlaySoundFromMEM(3);
          MyGameMap.SetBlockValue(dx,dy,mpcZero);

          for nVar:=0 to GhostCount do
          begin
            MyGhost[nVar].isSeek:=True;
            MyGhost[nVar].BlockedTimer:=GetTickCount;
          end;  

        end
        else
        if (MyGameMap.GetBlockValue(dx,dy)  = mpcBonusPoint) then
        begin
          Inc(Myscore,10);
          isBonusActive:=False;
          BonusTimer:=GetTickCount;
          MyGameMap.SetBlockValue(dx,dy,mpcZero);
          
          case nBonus of
            0:
            begin
               for nVar:=0 to GhostCount do
               begin
                 MyGhost[nVar].isBlocked:=True;
                 MyGhost[nVar].BlockedTimer:=GetTickCount;
               end;  

               Inc(Myscore,1500);
               PlaySoundFromMEM(3);
            end;
            1:
            begin
                ImGhost := true;
                PlaySoundFromMEM(3);
            end;
            2:
            begin
                Inc(Myscore,2000);
                PlaySoundFromMEM(3);
            end;
            3:
            begin
                Inc(MyLife);
                if MyLife>2 then MyLife:=2;
                PlaySoundFromMEM(3);
            end;
            4:
            begin
                ImImmortal:= true;
                PlaySoundFromMEM(3);
            end;
          end;

          if MyGameMap.PointsCount <=0 then begin isWinstate:=3;  PlaySoundFromMEM(4); end;
        end;


      end;

  end;

end;



////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

procedure TOGhost.Init(nx,ny:integer;AI:Byte);
begin
  x:=nx*CellWH;
  y:=ny*CellWH;
  Dir:=0;
  NewDir:=0;
  More:=0;
  Pic:=0;
  AItype:=AI;
  Trace:= NBTEST-(AItype*3);
  if Trace<=0 then Trace:=1;
  Speed:=2;
  isBlocked:=False;
  isSeek:=False;
end;

function TOGhost.CanContinue: boolean;
var
  dx, dy: integer;
begin
  result := true;

  if (x+y) mod CellWH = 0 then
  begin
    dx := GetTileFromPx(x);
    dy := GetTileFromPx(y);
    case dir of
      mvRight: begin dx := GetTileFromPx(x+CellWH); dy := GetTileFromPx(y); end;
      mvLeft: begin dx := GetTileFromPx(x-1); dy := GetTileFromPx(y); end;
      mvUp: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y-1); end;
      mvDown: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y+CellWH); end;
    end;
    result := (MyGameMap.GetBlockValue(dx,dy)<>mpcWall);
  end;
end;

function TOGhost.CanChange:Integer;
var
  dx, dy: integer;
begin
 Result := dir;

  if ((x+y) mod CellWH = 0) then
  begin
    dx := GetTileFromPx(x);
    dy := GetTileFromPx(y);

    case NewDir of
      mvRight: begin dx := GetTileFromPx(x+CellWH); dy := GetTileFromPx(y); end;
      mvLeft: begin dx := GetTileFromPx(x-1); dy := GetTileFromPx(y); end;
      mvUp: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y-1); end;
      mvDown: begin dx := GetTileFromPx(x); dy := GetTileFromPx(y+CellWH); end;
    end;

   // if MyGameMap.GetBlockValue(dx,dy)=-1 then Result:=Dir;
    if MyGameMap.GetBlockValue(dx,dy)<>mpcWall then Result:=NewDir;

     Inc(More);
     if aWantDirs[More] = 0 then SeekPt;
     NewDir:= aWantDirs[More];

  end;

end;

procedure TOGhost.Update;
var
  dx,dy,nSpeed:Integer;
begin

 if (GetTickCount-aTimer)> 400 then
 begin
   aTimer:=GetTickCount;
   Inc(Pic);
   if Pic>2 then Pic:=0;
 end;

 if isBlocked then
 begin
   if (GetTickCount-BlockedTimer)> 8000 then
   begin
     isBlocked:=False;
   end;
   Exit;
 end;

 if isSeek then
 begin
   if (GetTickCount-BlockedTimer)> 8000 then
   begin
     isSeek:=False;
   end;
 end;

  nSpeed:=Trunc(Speed+DGhostSpeed);
  dir:= CanChange;

 // Exit;

  if CanContinue then
  begin

    case dir of
        mvRight:
        begin
          Inc(x,nSpeed);
          if (x > (MyGameMap.Width+1)*CellWH) then X:= -CellWH+nSpeed;
        end;
        mvLeft:
        begin
          Dec(x,nSpeed);
          if (X +CellWH < 0) then X:= (MyGameMap.Width+1)*CellWH-nSpeed;
        end;
        mvUp:
        begin
          Dec(y,nSpeed);
          if (Y +CellWH < 0) then Y:= (MyGameMap.Height+1)*CellWH-nSpeed;
        end;
        mvDown:
        begin
          Inc(y,nSpeed);
          if (Y > (MyGameMap.Height+1)*CellWH) then Y:= -CellWH+nSpeed;
        end;
    end;
  end;


   if SQR(x-MyPacMan.x)+SQR(y-MyPacMan.y) < SQR(CellWH) then
   begin
     if isSeek then
     begin
       Inc(Myscore,500);
       BlockedTimer:=GetTickCount;
       isBlocked:=True;
       Exit;
     end;

     if not ImImmortal then
     begin
       Dec(MyLife);
       if MyLife<-1 then
       begin
         PlaySoundFromMEM(5);
        isWinstate:=2;
       end
       else
       begin
         PlaySoundFromMEM(5);
        isWinstate:=1;
       end;

     end;
   end;
end;

procedure TOGhost.SeekPt;
var
  pt,ar:TPoint;
  Possible: Array[1..8] of TPoint;
  nbPos: byte;
  count,i,poss: byte;
  pl,pc: integer;
  List:array[0..50] of TPoint;
  ListCount:Integer;
  NewPt: TPoint;
  mdist: integer;

begin

  pt.x := GetTileFromPx(x);
  pt.y := GetTileFromPx(y);
  ar.x := GetTileFromPx(MyPacMan.x);
  ar.y := GetTileFromPx(MyPacMan.y);

  nbPos := 1;

  MyGameMap.CloneBlockMap;
  ListCount:=-1;

  while (nbPos < Trace) do
  begin
    count:=0;

      FOR pl:= -1 TO 1 do
      FOR pc:= -1 TO 1 do
      begin
        if (abs(pl+pc) = 1) and (MyGameMap.GetBlockValue(Pt.x+pc,Pt.y+pl)<>mpcWall)then
        begin
          inc(count);
          Possible[count].X:=Pt.x+pc;
          Possible[count].Y:=Pt.y+pl;
        end;
      end;


      if count=1 then
      begin
        Inc(ListCount);
        List[ListCount].X:=pt.x;
        List[ListCount].Y:=pt.y;
        NewPt:=Possible[1];
      end
      else
      if count>1 then
      begin
        mdist:=Distance(Possible[1],ar);
        poss:=1;

        for i:=2 to count do
        if Distance(Possible[i],ar)<mdist then
        begin
            mdist:=Distance(Possible[i],ar);
            poss:=i;
        end;

        Inc(ListCount);
        List[ListCount].X:=pt.x;
        List[ListCount].Y:=pt.y;

        NewPt:=Possible[poss];
      end
      else
      if (count=0) and (ListCount=-1) then
       break
      else
      if (count=0) and (ListCount>=0) then
      begin
        NewPt:=List[ListCount];
        Dec(ListCount);
      end;

    if NewPt.x-Pt.x = -1 then
      aWantDirs[nbPos] := mvLeft
    else
    if NewPt.x-Pt.x = 1 then
     aWantDirs[nbPos] := mvRight
    else
    if NewPt.y-Pt.y = -1 then
     aWantDirs[nbPos] := mvUp
    else
     aWantDirs[nbPos] := mvDown;

    aWantTDirs[nbPos]:= Pt;

    inc(nbPos);
    if (NewPt.x=ar.x) and (NewPt.y=ar.y) then break;

    Pt := NewPt;
    MyGameMap.SetBlockValue(Pt.x,Pt.y,mpcWall);

  end;

  aWantDirs[nbPos] := 0;
  More := 1;
  MyGameMap.isFindmode:=False;
end;

procedure TOGhost.Draw;
var
  i,dx,dy,index:Integer;
begin

 if AItype=0 then index:= GhostA[Pic];
 if AItype=1 then index:= GhostB[Pic];
 if AItype=2 then index:= GhostC[Pic];

 if isSeek then  index:= GhostD[Pic];
 if isBlocked then  index:= GhostE[Pic];

 DrawTextureBox(x+MyGameMap.offsetx,y-MyGameMap.offsety-10, 28,28,index);

end;

end.
