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:longint; where:word);
Var
  InF   : File of byte; Loop : Word;
  Dummy : Array[1..18] of Byte;
  Pal   : Array[1..768] of Byte;
  n,l:longint;
  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]);
  l:=0;
  for n:=1 to size do begin
      read(inf, mem[where:l]);
      inc(l);
  end;
  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;
z:byte;
begin
Assign(fw1, name);
Rewrite(fw1,1);
Reset(fw1, 1);
z:=0;
for i:=0 to 767 do begin
   BlockWrite(fw1, z, 1);
   BlockWrite(fw1, palitra[i], 1);
end;
Close(fw1);
end;

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


var
fw1,fw2:file;
x,y:integer;
c, i,n,m,yy,xx:word;
fr:file;
col, r,g,b:byte;
size:word;
num_dots, rand_seg, coord_seg, vect_seg:word;

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

asm
mov ax, 13h
int 10h
end;

LoadTga('presents.tga', 320*200, vaddr);
for y:=0 to 199 do for x:=0 to 319 do mem[vga:x+y*320]:=mem[vaddr:x+y*320];


num_dots:=0;

vect_seg :=vaddr2;
coord_seg:=vaddr3;
n:=0;
randseed:=1;
for m:=1 to 2 do
for yy:=0 to 199 do for xx:=0 to 319 do begin
 if mem[vaddr:xx+yy*320]<>0 then begin

   memw[coord_seg:n shl 2]  :=word(xx * 32);
   memw[coord_seg:n shl 2+2]:=word(yy * 32);

   repeat x:=random(5)-3; until x<>0;
   repeat y:=random(5)-3; until y<>0;

   memw[vect_seg:n shl 2]:=  word(x);
   memw[vect_seg:n shl 2+2]:=word(y);

   if n<16734 then inc(n);
end;
end;

num_dots:=n;

rand_seg:=vaddr3+num_dots*4 div 16 +2;

for n:=0 to 255 do
begin
 r:=random(16);
 if r=1 then m:=1
 else if r=7 then m:=3
 else m:=0;
 mem[rand_seg:n]:=byte(m);
end;

{vect for blast}
for n:=0 to num_dots-1 do begin
   r:=random(9)+1;

   repeat x:=random(30)-15; until x<>0;
   y:=-random(10)-20;

   memw[vaddr:n shl 2]:=  word(x*r);
   memw[vaddr:n shl 2+2]:=word(y*r);
end;


save_bitmap('pres1.bin', num_dots*4, vaddr2);
save_bitmap('pres2.bin', num_dots*4+256+34+2, vaddr3);
save_bitmap('pres3.bin', num_dots*4, vaddr);


readkey;

asm
mov ax, 3h
int 10h
end;

WriteLn(num_dots);

readkey;
end.