program palsort;

uses crush13h,crt;

var x,y:integer;
    count1,add1:integer;
    count2,add2:integer;
    c:char;

type pos= record
                c:integer;
                x,y,x1,y1:real;
                end;

var position: array [0..50] of pos;
    sc1,sc2:word;
    a,aa,count: integer;


procedure blur4(from,too:word);
var x,y,x1,y1,aaa,ccc,counter:integer;
    cccc: array [1..9] of integer;
begin
     for x:=0 to 320 do for y:=0 to 200 do
         begin

          cccc[2]:=mem[from:(x)+(y+1)*320];

          cccc[4]:=mem[from:(x+1)+(y)*320];
          cccc[5]:=mem[from:(x)+(y)*320];
          cccc[6]:=mem[from:(x-1)+(y)*320];

          cccc[8]:=mem[from:(x)+(y-1)*320];

          mem[too:x+y*320]:=((cccc[2]+cccc[4]+cccc[6]+cccc[8] div 4) + cccc[5]) div 2;
      end;
end;

procedure putball (x,y,col:integer;where:word);
var aaa:integer;
begin
     for aaa:=-2 to 2 do mem[where:(x+aaa)+(y-5)*320]:=mem[where:(x+aaa)+(y-5)*320]+col;
     for aaa:=-3 to 3 do mem[where:(x+aaa)+(y-4)*320]:=mem[where:(x+aaa)+(y-4)*320]+col;
     for aaa:=-4 to 4 do mem[where:(x+aaa)+(y-3)*320]:=mem[where:(x+aaa)+(y-3)*320]+col;
     for aaa:=-5 to 5 do mem[where:(x+aaa)+(y-2)*320]:=mem[where:(x+aaa)+(y-2)*320]+col;
     for aaa:=-5 to 5 do mem[where:(x+aaa)+(y-1)*320]:=mem[where:(x+aaa)+(y-1)*320]+col;
     for aaa:=-5 to 5 do mem[where:(x+aaa)+(y)*320]:=mem[where:(x+aaa)+(y)*320]+col;
     for aaa:=-5 to 5 do mem[where:(x+aaa)+(y+1)*320]:=mem[where:(x+aaa)+(y+1)*320]+col;
     for aaa:=-5 to 5 do mem[where:(x+aaa)+(y+2)*320]:=mem[where:(x+aaa)+(y+2)*320]+col;
     for aaa:=-4 to 4 do mem[where:(x+aaa)+(y+3)*320]:=mem[where:(x+aaa)+(y+3)*320]+col;
     for aaa:=-3 to 3 do mem[where:(x+aaa)+(y+4)*320]:=mem[where:(x+aaa)+(y+4)*320]+col;
     for aaa:=-2 to 2 do mem[where:(x+aaa)+(y+5)*320]:=mem[where:(x+aaa)+(y+5)*320]+col;
     for aaa:=-2 to 2 do if mem[where:(x+aaa)+(y-5)*320]>255 then mem[where:(x+aaa)+(y-5)*320]:=255;
     for aaa:=-3 to 3 do if mem[where:(x+aaa)+(y-4)*320]>255 then mem[where:(x+aaa)+(y-4)*320]:=255;
     for aaa:=-4 to 4 do if mem[where:(x+aaa)+(y-3)*320]>255 then mem[where:(x+aaa)+(y-3)*320]:=255;
     for aaa:=-5 to 5 do if mem[where:(x+aaa)+(y-2)*320]>255 then mem[where:(x+aaa)+(y-2)*320]:=255;
     for aaa:=-5 to 5 do if mem[where:(x+aaa)+(y-1)*320]>255 then mem[where:(x+aaa)+(y-1)*320]:=255;
     for aaa:=-5 to 5 do if mem[where:(x+aaa)+(y)*320]>255 then mem[where:(x+aaa)+(y)*320]:=255;
     for aaa:=-5 to 5 do if mem[where:(x+aaa)+(y+1)*320]>255 then mem[where:(x+aaa)+(y+1)*320]:=255;
     for aaa:=-5 to 5 do if mem[where:(x+aaa)+(y+2)*320]>255 then mem[where:(x+aaa)+(y+2)*320]:=255;
     for aaa:=-4 to 4 do if mem[where:(x+aaa)+(y+3)*320]>255 then mem[where:(x+aaa)+(y+3)*320]:=255;
     for aaa:=-3 to 3 do if mem[where:(x+aaa)+(y+4)*320]>255 then mem[where:(x+aaa)+(y+4)*320]:=255;
     for aaa:=-2 to 2 do if mem[where:(x+aaa)+(y+5)*320]>255 then mem[where:(x+aaa)+(y+5)*320]:=255;
end;

begin
     video_mode ( $13);
     initvirt;

     cls (0,vp[2]);
     cls (0,vga);


      for x:=0 to 320 do for y:=0 to 200 do putpixel (x,y,1,vp[2]);

     for x:=0 to 255 do setcolor (x,63,63,63);

     for x:=0 to 7 do setcolor (x*16+0,x*8,x*8,0);
     for x:=8 to 15 do setcolor (x*16+0,63,63,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+1,x*8,0,x*8);
     for x:=8 to 15 do setcolor (x*16+1,63,(x-8)*8,63);

     for x:=0 to 7 do setcolor (x*16+2,0,x*8,x*8);
     for x:=8 to 15 do setcolor (x*16+2,(x-8)*8,63,63);

     for x:=0 to 7 do setcolor (x*16+3,x*8,0,0);
     for x:=8 to 15 do setcolor (x*16+3,63,(x-8)*8,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+4,0,x*8,0);
     for x:=8 to 15 do setcolor (x*16+4,(x-8)*8,63,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+5,0,0,x*8);
     for x:=8 to 15 do setcolor (x*16+5,(x-8)*8,(x-8)*8,63);

     for x:=0 to 7 do setcolor (x*16+6,x*4,x*4,0);
     for x:=8 to 15 do setcolor (x*16+6,32+(x-8)*4,32+(x-8)*4,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+7,x*4,0,x*4);
     for x:=8 to 15 do setcolor (x*16+7,32+(x-8)*4,(x-8)*8,32+(x-8)*4);

     for x:=0 to 7 do setcolor (x*16+8,0,x*4,x*4);
     for x:=8 to 15 do setcolor (x*16+8,(x-8)*8,32+(x-8)*4,32+(x-8)*4);

     for x:=0 to 7 do setcolor (x*16+9,x*4,0,0);
     for x:=8 to 15 do setcolor (x*16+9,32+(x-8)*4,(x-8)*8,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+10,0,x*4,0);
     for x:=8 to 15 do setcolor (x*16+10,(x-8)*8,32+(x-8)*4,(x-8)*8);

     for x:=0 to 7 do setcolor (x*16+11,0,0,x*4);
     for x:=8 to 15 do setcolor (x*16+11,(x-8)*8,(x-8)*8,32+(x-8)*4);

     for x:=0 to 7 do setcolor (x*16+12,x*4,x*4,x*4);
     for x:=8 to 15 do setcolor (x*16+12,32+(x-8)*4,32+(x-8)*4,32+(x-8)*4);


     count1:=0;
     add1:=10;

     for a:=0 to 50 do
         begin
              position[a].x:=random(320)-160;
              position[a].y:=random(200)-100;
              position[a].x1:=(random(11)/10)*random(3)-1 ;
              position[a].y1:=(random(11)/10)*random(3)-1 ;
              position[a].c:=((a * 12) div 50) +128;
         end;

     count:=0;

       repeat
           count:=count+1;
           if count>round(3.1415926535*40) then count:=0;
           for a:=0 to 50 do
               begin
                    {position[a].x:=position[a].x+position[a].x1;
                    if position[a].x>160 then position[a].x:=-160;
                    if position[a].x<-160 then position[a].x:=160;

                    position[a].y:=position[a].y+position[a].y1;
                    if position[a].y>100 then position[a].y:=-100;
                    if position[a].y<-100 then position[a].y:=100;}

{                    position[a].x:=5*a*cos(count/10);
                    position[a].y:=5*a*sin(count/10);}

                    {position[a].x:=5*a*cos(count/10);
                    position[a].y:=5*a*sin(count/5);}

                    {position[a].x:=a*cos(count/10);
                    position[a].y:=5*a*sin(count/10);}

{                    position[a].x:=5*a*cos(count/10)*cos(count/5);
                    position[a].y:=5*a*sin(count/10)*cos(count/5);}

{                    position[a].x:=5*a*cos(count/10)*sin(a);
                    position[a].y:=5*a*sin(count/10)*cos(a);}

                    position[a].x:=2*a*sin(a)*cos(count/10);
                    position[a].y:=2*a*cos(a)*sin(count/10);

{                    position[a].x:=2*a*sin(a)+sin(count)*5;
                    position[a].y:=2*a*cos(a)+cos(count)*5;}


               end;




      count1:=count1+add1;
      if count1>1799 then count1:=count1-1259;

      cls (0,vp[2]);
      for x:=0 to 15 do
      for y:=0 to 15 do putpixel (x,y,y*16+x,vp[2]);


      for aa:=0 to 50 do putball(160+round(position[aa].x),100-round(position[aa].y),position[aa].c,vp[2]);


{      blur4(vp[2],vp[1]);}

       for x:=0 to 320 do for y:=0 to 200 do


           if mem[vp[2]:x+y*320]<16
              then mem[vp[1]:x+y*320]:=0
              else mem[vp[1]:x+y*320]:=mem[vp[2]:x+y*320]-16;


      move(mem[vp[1]:0],mem[vga:0],64000);


      waitvbl;

      if keypressed=true then c:=readkey;
     until c=#27;

     closevirt;
     video_mode ( 03);
end.