program MapViewer;
uses CRT;
type
  RegionData = array [1..1024] of record
                                    index:byte;
                                    X1,Y1,X2,Y2:integer;
                                  end;
  RegionDataP = ^RegionData;

var
  MX,MY,MB:word;
  RDP:RegionDataP;
  oCCC,CCC:byte;
  L:byte;
  k:char;
  RDPCnt,cpp:word;
  a,b:integer;
  procedure ReadFile (S:string;var PegionData;var Lev:byte;var Cnt:word);
    type
      Cfile=file of char;
    var
      F:Cfile;
      Line,Oline:string;
      Cp:word;
      Cp2,C,CO:byte;
      code:integer;
      procedure ReadLine (var F:CFile;var L:string);
        var
          Cp:byte;
          C:char;
        begin
          L:='';
          Cp:=1;
          if FilePos(F) < FileSize (F) then Read (F,C);
          While (ord (C)<>$0D) and (FilePos(F)<FileSize(F))do
            begin
              L[Cp]:=C;
              Read (F,C);
              if (ord (C)>96) and (ord(C)<123) then C:=chr (ord(c)-32);
              Cp:=Cp+1;
            end;
          if ord(c)=$0d then L[0]:=chr (Cp-1) else L[0]:=chr (Cp);
          if FilePos(F)< FileSize(F) then Read (F,C);
        end;
      procedure CutLine (L:string;a,b:byte;var O:string);
        var
          Cp:byte;
        begin
          O[0]:=Chr (b-a+1);
          for Cp:=a to b do
            begin
              O[Cp-a+1]:=L[Cp];
            end;
        end;
      procedure FindSpace (L:string;s:byte;var c:byte);
        var
          Cp:byte;
        begin
          C:=s;
          While (C<Length (L)+1) and (L[C]<>' ') do C:=C+1;
        end;
    begin
      Assign (F,S);
      Reset (F);
      ReadLine (F,Line);
      case Line[5] of
        '1': Lev:=1;
        '2': Lev:=2;
        '3': Lev:=3;
        '4': Lev:=4;
        else Lev:=255
      end;
      Cp:=1;
      While FilePos(F)<FileSize(f) do
      begin
        ReadLine (F,Line);
        CO:=1;
        C:=0;
        Cp2:=1;
        While C<>255 do
          begin
            FindSpace (Line,CO,C);
            CutLine (Line,CO,C-1,Oline);
            case Cp2 of
              1:Val (Oline,RDP^[Cp].index,code);
              2:Val (Oline,RDP^[cp].x1,code);
              3:Val (Oline,RDP^[cp].y1,code);
              4:Val (Oline,RDP^[cp].x2,code);
              5:Val (Oline,RDP^[cp].y2,code);
            end;
            CO:=C+1;
            Cp2:=Cp2+1;
          end;
        Cp:=Cp+1;
      end;
      Cnt:=Cp-1;
      Close (F);
    end;
  procedure ShowMouse;
    begin
      asm
        mov ax,0001h
        int 33h
      end;
    end;
  procedure HideMouse;
    begin
      asm
        mov ax,0002h
        int 33h
      end;
    end;
  procedure GetMousePosAndButtons (var X,Y,B:word);
    var
      xp,yp,bpc:word;
    begin
      asm
        mov ax,0003h
        int 33h
        mov xp,cx
        mov yp,dx
        mov bpc,bx
      end;
      X:=Xp div 2;
      Y:=Yp;
      B:=Bpc;
    end;
  procedure SetVideo;
    begin
      asm
        mov ax,0013h
        int 10h
        mov ax,0000h
        int 33h
      end;
      ShowMouse;
    end;
  procedure DeSetVideo;
    begin
      asm
        mov ax,0003h
        int 10h
        mov ax,0002h
        int 33h
      end;
      HideMouse;
    end;
  procedure SetPalette;
    var
      Table:array [0..255] of record
                                R,G,B:byte;
                              end;
      Cp:byte;
      S,O:word;
    begin
      S:=seg (Table);
      O:=ofs (Table);
      Table[0].R:=0;
      Table[0].G:=0;
      Table[0].B:=0;
      Table[255].R:=63;
      Table[255].G:=63;
      Table[255].B:=63;
      for Cp:=1 to 239 do
        begin
          Table[Cp].R:=(((Cp shr 5)*255) div 7) div 4;
          Table[Cp].G:=((((Cp shr 2) and 7) *255)div 7) div 4;
          Table[Cp].B:=(((Cp and 3)*255) div 3) div 4;
        end;
      asm
        mov ax,1012h
        mov bx,0
        mov cx,256
        mov dx,s
        mov es,dx
        mov dx,o
        int 10h
      end;
    end;
  procedure GetPixel (X,Y:word;var C:byte);
    var
      A:word;
      cp:byte;
    begin
      A:=Y*320+X;
      asm
        mov ax,0A000h
        mov es,ax
        mov bx,A
        mov dh,es:[bx]
        mov cp,dh
      end;
      C:=cp;
    end;
  procedure PutPixel (X,Y:word;C:byte);
    var
      A:word;
      cp:byte;
    begin
      A:=Y*320+X;
      Cp:=C;
      asm
        mov ax,0A000h
        mov es,ax
        mov bx,A
        mov dh,cp
        mov es:[bx],dh
      end;
    end;
  procedure DrawPolly (X1,Y1,X2,Y2:integer;c:byte);
    var
      X,Y:integer;
      A,Cn:word;
      Cl:byte;
    begin
      HideMouse;
      cn:=X2-X1+1;
      for Y:=Y1 to Y2 do
        begin
          A:=320*Y + X1;
          cl:=c;
          asm
            mov di,A
            mov CX,Cn
            mov dx,$A000
            mov al,c
            mov es,dx
            rep stosb
          end;
        end;
      ShowMouse;
    end;
  procedure HighLight (CCC:byte);
    var
      x,y:word;
      c:byte;
      TF,OTF:boolean;
    begin
      HideMouse;
      TF:=False;
      OTF:=False;
      for y:=0 to 199 do
        for x:=0 to 319 do
          begin
            GetPixel (x,y,c);
            if CCC = C then TF:=True else TF:=False;
            if (TF=True) and (OTF=False) then PutPixel (x,y,255);
            if (TF=False) and (OTF=True) then PutPixel (x-1,y,255);
            OTF:=TF;
          end;
      for x:=0 to 319 do
        for y:=0 to 199 do
          begin
            GetPixel (x,y,c);
            if CCC = C then TF:=True else TF:=False;
            if (TF=True) and (OTF=False) then PutPixel (x,y,255);
            if (TF=False) and (OTF=True) then PutPixel (x,y-1,255);
            OTF:=TF;
          end;
      ShowMouse;
    end;
  procedure DeHighLight (occc:byte);
    var
      ccc:byte;
      x,y:word;
    begin
      HideMouse;
      for y:=0 to 199 do
        for x:=0 to 319 do
          begin
            GetPixel (x,y,ccc);
            if ccc=255 then
              begin
                PutPixel (x,y,occc);
              end;
          end;
      ShowMouse;
    end;
begin
  if ParamCount<>1 then Exit;
  New (RDP);
  ReadFile (ParamStr(1),RDP,L,RDPCnt);
  SetVideo;
  SetPalette;
  for Cpp:=1 to RDPCnt do
    begin
      DrawPolly (RDP^[Cpp].X1,RDP^[Cpp].Y1,RDP^[Cpp].X2,RDP^[Cpp].Y2,RDP^[Cpp].Index);
    end;
  k:=' ';
  repeat
    GetMousePosAndButtons (MX,MY,MB);
    if MB<>0 then
      begin
        DeHighLight (OCCC);
        GetPixel (MX,MY,CCC);
        if CCC<>0 then HighLight (CCC);
        OCCC:=CCC;
      end;
    if keypressed=true then
      begin
        k:=readkey;
        if keypressed=true then K:=readkey;
      end;
  until ord (k)=27;
  DeSetVideo;
  Dispose (RDP);
end.