unit UCheckmaze;

interface

uses Unit1;
               
procedure InitValidate;
procedure Validate(X,Z:Integer; Height:THeight);
 
var valid:array [-mapsize..mapsize,-mapsize..mapsize,THeight] of Boolean;
var validated:array [-mapsize..mapsize,-mapsize..mapsize,THeight] of Boolean;
var validating:array [-mapsize..mapsize,-mapsize..mapsize,THeight] of Boolean;

implementation
uses UCreateRaum;

procedure InitValidate;
var C1,C2:Integer;
  C3: THeight;
begin
  for C1:=-mapsize to mapsize do
  for C2:=-mapsize to mapsize do
  for C3:=Down to Up do begin
    valid[C1,C2,C3]:=false;
    validated[C1,C2,C3]:=false;
    validating[C1,C2,C3]:=false;
  end;
end;

function Walkable(FromX,FromZ:Integer; Dir:TRichtung; Height:THeight):Boolean;
begin
  Result:=not (
  (Dir in Map[FromX,FromZ].Walls)
  or ((Dir in Map[FromX,FromZ].UpWalls) and (Height=Up))
  or ((Dir in Map[FromX,FromZ].DownWalls) and (Height=Down)));
end;

procedure Validate(X,Z:Integer; Height:THeight);
begin
  if not Map[X,Z].created then
  CreateRaum(X,Z);

  if GetRT(X,Z).PortTo[Up] and (Height=Down) then begin
    Height:=Up;
  end;

  if GetRT(X,Z).PortTo[Up] and (Height=Down) then begin
    Height:=Down;
  end;

  if validating[X,Z,Height] then Exit;
  if validated[X,Z,Height] then Exit;
  
  if GetRT(X,Z).WinAtArrival
  then begin
    valid[X,Z,Height]:=true;
    validated[X,Z,Height]:=true;
    Exit;
  end;

  if not GetRT(X,Z).Passierbar then begin
    valid[X,Z,Height]:=false;
    validated[X,Z,Height]:=true;
    Exit;
  end;


  validating[X,Z,Height]:=true;



  if (X<>mapsize)
  and Walkable(X,Z,px,Height) then begin
    Validate(X+1,Z,Height);
    if valid[X+1,Z,Height] then begin
      valid[X,Z,Height]:=true;
//      validated[X,Z,Height]:=true;
//      validating[X,Z,Height]:=false;
//      Exit;
    end;
  end;

  if (X<>-mapsize)
  and Walkable(X,Z,nx,Height) then begin
    Validate(X-1,Z,Height);
    if valid[X-1,Z,Height] then begin
      valid[X,Z,Height]:=true;
//      validated[X,Z,Height]:=true;
//      validating[X,Z,Height]:=false;
//      Exit;
    end;
  end;

  if (Z<>mapsize)
  and Walkable(X,Z,pz,Height) then begin
    Validate(X,Z+1,Height);
    if valid[X,Z+1,Height] then begin
      valid[X,Z,Height]:=true;
//      validated[X,Z,Height]:=true;
//      validating[X,Z,Height]:=false;
//      Exit;
    end;
  end;

  if (Z<>-mapsize)
  and Walkable(X,Z,nz,Height) then begin
    Validate(X,Z-1,Height);
    if valid[X,Z-1,Height] then begin
      valid[X,Z,Height]:=true;
//      validated[X,Z,Height]:=true;
//      validating[X,Z,Height]:=false;
//      Exit;
    end;
  end;

  validated[X,Z,Height]:=true;
  validating[X,Z,Height]:=false;
  Exit;
end;





end.
