{13}
{13mview,_13asm,_13mouse}
{1,2,3}

program Map_viewer;
uses _13asm,crt,_13mouse;
type parea=^area;
     area=record        {suradnice area}
          x,xx:word;
          y,yy:byte;
          next:parea;
          end;


var fn:string;          {MDF file name}
    heapp:pointer;      {ukazovatel prazdneho zasobnika}
    f:text;             {vstupny subor}
    level:byte;         {level suboru MDF}
    reg:array[1..239] of record  {ukazatele na prvu a poslednu areu regionu}
                         pa,aa:parea;
                         end;

    q,oznaceny:byte;



procedure vstup;        {nacita udaje zo vstupneho suboru}
var s:string[4];
    col:byte;
    na:parea;
begin
read(f,s);
readln(f,level);
while not(eof(f)) do begin
      new(na);
      read(f,col,na^.x,na^.y,na^.xx,na^.yy);
      if reg[col].pa=nil then reg[col].pa:=na else
         reg[col].aa^.next:=na;
      reg[col].aa:=na;
      reg[col].aa^.next:=nil;
      end;
end;

procedure ramuj(x,xx,y,yy:word; c:byte);
var ox,oy:word;
begin
oy:=y;
for ox:=x to xx do if get(oy-1,ox)<>c then pix(ox,oy,240);
oy:=yy;
for ox:=x to xx do if get(oy+1,ox)<>c then pix(ox,oy,240);
ox:=x;
for oy:=y to yy do if get(oy,ox-1)<>c then pix(ox,oy,240);
ox:=xx;
for oy:=y to yy do if get(oy,ox+1)<>c then pix(ox,oy,240);
end;

procedure oramuj(c:byte);
var ra:parea;
begin
ra:=reg[c].pa;
vycisti(oznaceny);
    while ra<>nil do begin
          ramuj(ra^.x,ra^.xx,ra^.y,ra^.yy,c);
          ra:=ra^.next;
          end;
end;

procedure oznacuj;
var zn:char;
    xm,ym:word;
    stlac:boolean;
    o:byte;
    ra:parea;
begin
oznaceny:=0;
resetm;
show;
repeat
      scanm(stlac,xm,ym);
      if stlac then begin
               getcol(xm,ym,o);
               if o<>oznaceny then begin
                  oznaceny:=o;
                  oramuj(o);
                  end;
               end;
if keypressed then zn:=readkey;
until zn=#27;
hidemouse;
end;


procedure zobraz;
var r:byte;
    za:parea;
begin
initgraph;
setcolors;

for r:=1 to 239 do if reg[r].pa<>nil then begin
    za:=reg[r].pa;
    while za<>nil do begin
          utvar(r,za^.x,za^.xx,za^.y,za^.yy);
          za:=za^.next;
          end;
    end;
oznacuj;
closegraph;
end;

BEGIN
mark(heapp);
for q:=1 to 239 do begin
                   reg[q].pa:=nil;
                   reg[q].aa:=nil;
                   end;
if paramcount=1 then fn:=paramstr(1)
                else begin
                     writeln('No map file selected!');
                     halt(1);
                     end;
assign(f,fn);
reset(f);
vstup;
close(f);
zobraz;
release(heapp);
end.