program circlepoints;

uses crush13h,crt;


var x,y:integer;
    c:char;
    stuff:pointer;
    counter1:integer;

procedure rcp(x1,y1,r,n,c:integer;where:word);
var aa,value1,value2:integer;
begin
          for aa:=0 to n do
              begin
                   value1:=random(1259);
                   value2:=random(r);
                   mem[where:(round(cosines^[value1]*value2)+x1)+(round(sines^[value1]*value2)+y1)*320]:=c;
{                   putpixel (round(cosines^[value1]*value2)+x1,round(sines^[value1]*value2)+y1,c,where);   }
              end;
end;

procedure blur(from,too:word);
var x,y,aaa,ccc,ccc2,counter:integer;
    cccc: array [1..9] of integer;
begin
     for x:=1 to 318 do for y:=1 to 198 do
         begin
          cccc[1]:=mem[from:(x+1)+(y+1)*320];
          cccc[2]:=mem[from:(x)+(y+1)*320];
          cccc[3]:=mem[from:(x-1)+(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[7]:=mem[from:(x+1)+(y-1)*320];
          cccc[8]:=mem[from:(x)+(y-1)*320];
          cccc[9]:=mem[from:(x-1)+(y-1)*320];

          ccc2:=cccc[1];
          counter:=0;
          for aaa:=1 to 9 do if cccc[aaa]<>ccc2 then counter:=1;

          ccc:=0;
          if counter=1
           then
             begin
                  counter:=0;
                  for aaa:=1 to 9 do
                   begin
                           counter:=counter+1;
                           ccc:=ccc+cccc[aaa];
                   end;
                  mem[too:x+y*320]:=trunc(ccc div counter);
             end
           else mem[too:x+y*320]:=ccc2;
         end;
end;

procedure blur_f(from,too:word);
var x,y,aaa,ccc,ccc2,counter:integer;
    cccc: array [1..9] of integer;
begin
     for x:=1 to 318 do for y:=1 to 198 do
         begin
          cccc[1]:=mem[from:(x)+(y+1)*320];
          cccc[2]:=mem[from:(x+1)+(y)*320];
          cccc[3]:=mem[from:(x)+(y)*320];
          cccc[4]:=mem[from:(x-1)+(y)*320];
          cccc[5]:=mem[from:(x)+(y-1)*320];

               ccc2:=(((cccc[1]+cccc[2]+cccc[4]+cccc[5]) div 4) + cccc[3]) div 2;
               mem[too:x+y*320]:=ccc2;

         end;
end;

procedure blur_t(from,too:word);
var x2,y2,aaa,ccc:integer;
    cccc: array [1..9] of integer;
begin
     for x2:=1 to 318 do for y2:=1 to 198 do
         begin
          cccc[1]:=mem[from:(x2+1)+(y2+1)*320];
          cccc[2]:=mem[from:(x2)+(y2+1)*320];
          cccc[3]:=mem[from:(x2-1)+(y2+1)*320];
          cccc[4]:=mem[from:(x2+1)+(y2)*320];
          cccc[5]:=mem[from:(x2)+(y2)*320];
          cccc[6]:=mem[from:(x2-1)+(y2)*320];
          cccc[7]:=mem[from:(x2+1)+(y2-1)*320];
          cccc[8]:=mem[from:(x2)+(y2-1)*320];
          cccc[9]:=mem[from:(x2-1)+(y2-1)*320];

          ccc:=cccc[1];
          for aaa:=2 to 9 do if cccc[aaa]>ccc then ccc:=cccc[aaa];

          if ccc<>0 then if ccc>2 then mem[too:x2+y2*320]:=ccc-2;
      end;
end;

procedure blur_w(from,too:word);
var x2,y2,aaa,ccc,ccc2:integer;
begin
     for x2:=1 to 318 do for y2:=1 to 198 do
         begin
          ccc:=random(9)+1;
          for aaa:=1 to 9 do
              begin
                   if ccc=1 then ccc2:=mem[from:(x2+1)+(y2+1)*320];
                   if ccc=2 then ccc2:=mem[from:(x2)+(y2+1)*320];
                   if ccc=3 then ccc2:=mem[from:(x2-1)+(y2+1)*320];
                   if ccc=4 then ccc2:=mem[from:(x2+1)+(y2)*320];
                   if ccc=5 then ccc2:=mem[from:(x2)+(y2)*320];
                   if ccc=6 then ccc2:=mem[from:(x2-1)+(y2)*320];
                   if ccc=7 then ccc2:=mem[from:(x2+1)+(y2-1)*320];
                   if ccc=8 then ccc2:=mem[from:(x2)+(y2-1)*320];
                   if ccc=9 then ccc2:=mem[from:(x2-1)+(y2-1)*320];
              end;

          mem[too:x2+y2*320]:=ccc2;
      end;
end;

Procedure palrotation;
var a:integer;
    r,g,b,d,e,f:byte;
begin
     getcolor (255,d,e,f);
     for a:=254 downto 0 do
         begin
              getcolor (a,r,g,b);
              setcolor (a+1,r,g,b);
         end;
     setcolor (0,d,e,f);
end;

begin
     video_mode ( $13);
     initvirt;
     inittables;

     counter1:=0;
     for x:=0 to 63 do setcolor(x, 0 , x , x );
     for x:=64 to 128 do setcolor(x, (x-64) , 63-(x-64), 63-(x-64) );
     for x:=128 to 192 do setcolor(x, 63-(x-128) , (x-128), 0);
     for x:=192 to 255 do setcolor(x, 0 , 63-(x-192) , 0 );
     loadpcx_nopal ('cool.pcx',vp[2]);

     cls (0,vp[1]);
     getimage (80,80,220,110,stuff,vp[2]);

     repeat

      counter1:=counter1+20;
      if counter1>1259 then counter1:=counter1-1259;

{      for x:=0 to 319 do for y:=0 to 199 do if mem[vp[2]:x+y*320]<>0 then mem[vp[1]:x+y*320]:=mem[vp[2]:x+y*320];}
      {for x:=0 to 320 do putpixel (x,100+round(sines^[x*8]*50),4,vp[1]);}

      rcp(150+round(cosines^[counter1]*100),95+round(sines^[counter1]*50),15,1500,170,vp[1]);
{      putimage (80,80,0,stuff,vp[1]);}
      {putimage (80,80*0,0,stuff,vp[1]);}

      blur_t (vp[1],vp[2]);
{      move(mem[vp[1]:0],mem[vga:0],64000); {put it on screen}
{      for x:=0 to 255 do mem[vga:x]:=x;}  {check the pal}
{      cls (0,vp[1]);}                     {screen clean up}
      blur_f (vp[2],vp[1]);
      move(mem[vp[2]:0],mem[vga:0],64000); {put it on screen}
{      move(mem[vp[2]:0],mem[vp[1]:0],64000);} {no effect from vp2 to vp1}

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

     killimage (stuff);
     cleartables;
     closevirt;
     video_mode ( 03);
end.