{ Number 05 }
{ Level 1 }
program Main;
uses crt,x05int;
type Point=record
             X,Y:integer;
             end;
     Rect=record
             A,B:Point;
             Index:byte;
             end;
     Line=record
            x,y:integer;
            Vert:boolean;
            Size:integer;
            end;
     Borders=array[1..1000] of Line;
     PBorders=^Borders;
var i,t:integer;
    x,y,st:word;
    Level:byte;
    FileName,s:string;
    f:text;
    Color:byte;
    Areas:array[1..1000] of Rect;
    Count:integer;
    OldArea:integer;
    OldRegion:integer;
    Regions:array[1..239] of record
                                Bord:PBorders;
                                Count:integer;
                                end;
procedure Rectangle(r:Rect);
var i,t,x,y:integer;
 begin
 HideMouse;
 with r do
 for i:=a.x to b.x do
  for t:=a.y to b.y do
   begin
   mem[$A000+20*t:i]:=Index;
   end;
 ShowMouse;
 end;

procedure PutPixel(x,y:integer;c:byte);
 begin
 mem[$A000+y*20:x]:=c;
 end;

function GetPixel(x,y:integer):byte;
 begin
 GetPixel:=mem[$A000+y*20:x]
 end;

procedure PutLine(x1,y1,x2,y2:integer);
 begin
 if x1=x2 then
  for t:=y1+1 to y2 do
   begin
   PutPixel(x1,t,Color+240);
   Color:=(Color+1) mod 2;
   end
  else
  for t:=x1+1 to x2 do
   begin
   PutPixel(t,y1,Color+240);
   Color:=(Color+1) mod 2;
   end
 end;

function GetAreaNumber(q,w:word):integer;
 begin
 for i:=1 to Count do
{  with Areas[i]. do}
   if (Areas[i].a.x<=q)and (q<=Areas[i].b.x) and (Areas[i].a.y<=w)and (w<=Areas[i].b.y)  then
    begin
    GetAreaNumber:=i;
    break;
    end;
 end;

procedure Change(x,y:word);
 begin
 HideMouse;
 Rectangle(Areas[OldArea]);
 OldArea:=GetAreaNumber(x,y);
 with Areas[OldArea] do
  begin
  PutLine(a.x,a.y,a.x,b.y);
  PutLine(a.x,b.y,b.x,b.y);
  PutLine(b.x,a.y,b.x,b.y);
  PutLine(a.x,a.y,b.x,a.y);
  end;
 ShowMouse;
 end;

procedure AddBord(Reg:integer;L:Line);
var i:integer;
 begin
 inc(Count);
 Regions[Reg].Bord^[Count]:=L;
 end;

procedure Include(r:Rect);
var reg:byte;
    i:integer;
    Lin:Line;
 begin
 reg:=r.index;
 if r.a.x=r.b.x then
  begin
  Lin.x:=r.a.x;
  Lin.y:=r.a.y;
  Lin.Vert:=False;
  Lin.Size:=r.b.y-r.a.y;
  end
 else
  begin
  Lin.x:=r.a.x;
  Lin.y:=r.a.y;
  Lin.Vert:=True;
  Lin.Size:=r.b.x-r.a.x;
  end;
 AddBord(reg,Lin);
{ with Regions[reg],r do
  begin
  for i:=1 to Count do
   begin
   if a.x-1=Bord^[i].x then
    begin

    end;
   end;
  end;}
 end;

procedure PutLine1(l:Line;c:byte);
var i:integer;
 begin
 with L do
  if Vert then
   for i:=y to y+Size-1 do mem[$A000+i*20:x]:=c
  else
   for i:=x to x+Size-1 do mem[$A000+y*20:i]:=c;
 end;

procedure DrawBorder(Reg:integer);
 begin
 with Regions[Reg] do
  for i:=1 to Count do
   PutLine1(Bord^[i],Reg);
 end;

begin
if ParamCount<>0 then
 FileName:=ParamStr(1)
else
 begin
 writeln('Using: 05MView[.exe] <filename>');
 halt;
 end;
{$I-}
assign(f,filename);
reset(f);
if IOResult<>0 then
 begin
 writeln('File open error');
 halt;
 end;
{$I+}
readln(f,s);
if copy(s,1,4)='MDF/' then delete(s,1,4);
val(s,Level,i);
InitGraph;
InitMouse;
ShowMouse;
Count:=0;
while not eof(f) do
 begin
 inc(Count);
 with Areas[Count] do
 readln(f,Index,a.x,a.y,b.x,b.y);
 rectangle(Areas[Count]);
 end;

if Level=1 then
 begin
 repeat
 if LeftButton then
  Change(GetMouseX,GetMouseY);
 if keypressed then if readkey=#27 then halt;
 until False;
 end
else
 begin
 for i:=1 to Count do
  begin
  if Regions[Areas[i].Index].Bord=nil then
   begin
   new(Regions[Areas[i].Index].Bord);
   Regions[Areas[i].Index].Count:=0;
   end;
  Include(Areas[i]);
  end;
 repeat
 if LeftButton then
  begin
  if OldRegion<>0 then DrawBorder(OldRegion);
  OldRegion:=GetPixel(GetMouseX,GetMouseY);
  if OldRegion<>0 then DrawBorder(OldRegion);
  end;
 if keypressed then if readkey=#27 then halt;
 until False;
 end;

end.