uses screen, memory, crt, dos;

const
VGA=$a000;

procedure setonecolor(c,r,g,b:byte);assembler;
asm
  mov dx,3c8h
  mov al,c
  out dx,al
  inc dx
  mov al,r
  out dx,al
  mov al,g
  out dx,al
  mov al,b
  out dx,al
end;

procedure readonecolor(c:byte;var r,g,b:byte);assembler;
asm
  mov dx,3c7h
  mov al,c
  out dx,al
  add dx,2
  in  al,dx
  les di,r
  mov [es:di],al
  in  al,dx
  les di,g
  mov [es:di],al
  in  al,dx
  les di,b
  mov [es:di],al
end;


var palitra, palitra2:array[0..767] of byte;

Procedure LoadTga(TgaName : String; size, where:word);
Var
  InF   : File of byte; Loop : Word;
  Dummy : Array[1..18] of Byte;
  Pal   : Array[1..768] of Byte;
  n:word;
  tmp,r,g,b:byte;
Begin
  Assign(Inf,TgaName);
  Reset(Inf);
  for n:=0 to 17 do read(inf,tmp);
  for n:=1 to 768 do read(inf,pal[n]);
  for n:=0 to size do read(inf, mem[where:n]);
  Close(Inf);
{  Port[$3C8]:=0;}
  Loop:=1;
  n:=0;
  While Loop<768 Do
    Begin
      r:=Pal[Loop+2] Div 4;
      g:=Pal[Loop+1] Div 4;
      b:=Pal[Loop] Div 4;
      palitra[n]:=r;
      palitra[n+1]:=g;
      palitra[n+2]:=b;

      Port[$3C9]:=r;
      Port[$3C9]:=g;
      Port[$3C9]:=b;
      n:=n+3;
      Inc(Loop,3);
    End;
End;

procedure save_palitra(name:string);
var fw1:file;
i:word;
begin
Assign(fw1, name);
Rewrite(fw1,1);
Reset(fw1, 1);
for i:=0 to 767 do BlockWrite(fw1, palitra2[i], 1);
Close(fw1);
end;

procedure save_texture(size:word; buf:word; name:string);
var fw1:file;
i,n:word;
begin
Assign(fw1, name);
Rewrite(fw1,1);
Reset(fw1, 1);
n:=0;
for i:=1 to size do begin
 BlockWrite(fw1, mem[buf:n], 1);
 inc(n);
end;
Close(fw1);
end;

Procedure MapGen64(max, mask_color, shift_x,shift_y, border:integer; scale:single; map :word);
var
n,c,rr:word;
col, r,xt,yt,z, m, dx, dy, x,y,xx,yy:integer;
begin
r:=64;
n:=0;
rr:=r*r;
for c:=0 to 128*128 do mem[map:c]:=byte(255);

{-----------------Mask-----------------------}
for y:=0 to 127 do begin
   dy:=R-y;
   dy:=dy*dy;
   for x:=0 to 127 do begin
     dx:= R-x;
     M:=dx*dx;
     M:=M+dy;
     if M < R*R then mem[map:n]:=mask_color;
     inc(n);
   end;
   end;
{----------------Light map-------------------}
n:=0;
for y:=0 to 127 do begin
   dy:=R-y;
   dy:=dy*dy;
   for x:=0 to 127 do begin
     dx:= R-x;
     M:=dx*dx;
     M:=M+dy;
     if M < R*R then begin
        Z:=byte(255-M div (R div 4));

        if z>border then begin
           xx:=x+shift_x;
           yy:=y+shift_y;

           if (xx<128) and (yy>0) then
           if mem[map:word(xx+yy*128)]=mask_color then
              begin
                col:=round(z*scale);
                if col>max then col:=max;
                mem[map:word(xx+yy*128)]:=byte(col) and 252;
              end;
        end;

      end;
      inc(n);
   end;
   end;

end;


var
fw1,fw2:file;

c, i, x,y,n,m:word;
fr:file;
r,g,b:byte;
size:word;

begin
SetUpVirtual(scr,vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr3, vaddr3);

asm
mov ax, 13h
int 10h
end;

r:=128; {diametr}
MapGen64(190, 4,  16,-16, 0, 1, vaddr);


{-----------Convert and save ---------------------------}

{for n:=0 to r*r-1 do mem[vaddr2:n*2]:=mem[vaddr:n];
save_texture(r*r*2, vaddr2, 'lightmap.bin');{}
{-------------------------------------------------------}
save_texture(r*r, vaddr, 'blmap2.bin');{}


{--------------------preview---------------------------}
setonecolor(0,0,0,30);
for n:=1 to 255 do setonecolor(n, n shr 2, n shr 2, n shr 2);
for y:=190 to 199 do for x:=0 to 255 do mem[vga:x+y*320]:=x;

n:=0;
for y:=0 to r-1 do for x:=0 to r-1 do begin
mem[vga:x+y*320] := mem[vaddr:word(n)];
inc(n);
end;

readkey;

asm
mov ax, 3h
int 10h
end;

end.