program lightthingies;

uses crush13h,crt;

type lightrecord= record x,y,x1,y1:integer end;

var c: char;
    light: array [0..100] of lightrecord;
    flare: pointer;
    a,aa:integer;

procedure putball (x,y,col:integer;where:word);
var aaa,c: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;
end;

Procedure rotatepal;
var aaa:integer;

    r,g,b,d,e,f:byte;
begin
     getcolor (255,d,e,f);
     for aaa:=255 downto 1 do
         begin
              getcolor (aaa,r,g,b);
              setcolor (aaa+1,r,g,b);
         end;
     setcolor (1,d,e,f);
end;

begin

     writeln ('er... some keys involved...');
     writeln ('1 activates random xdir for random values');
     writeln ('2 activates random ydir for random values');
     writeln ('3 activates random xdir for -1 or 1 values');
     writeln ('4 activates random ydir for -1 or 1 values');
     writeln ('5 activates pal rotation');
     writeln ('6 activates pal rotation (slower)');
     writeln ('Esc exits');
     writeln ('HINT- press other key after 1/2/3/4 to stabilize!');
     writeln ('EX: if you press 1 and then 2! xdir is stable while ydir is beeing randomly generated!');
     writeln ('starts with -1/1 x and y dir and stabilized!');
     readkey;

     writeln ('er... some keys involved...');
     writeln ('er... some keys involved...');
     writeln ('er... some keys involved...');

     video_mode ( $13);
     initvirt;
     inittables;

     randomize;
     c:=' ';

     for a:=0 to 100 do
         begin
              light[a].x:=random(319);
              light[a].y:=random(199);
              light[a].x1:=random(2)-1;
              light[a].y1:=random(2)-1;
              if light[a].x1=0 then light[a].x1:=1;
              if light[a].y1=0 then light[a].y1:=1;
         end;
     for a:=0 to 128 do setcolor (a,a div 2, a div 2,63-(a div 2));
     for a:=0 to 127 do setcolor (255-a,a div 2, a div 2,63-(a div 2));

     cls (16,vp[2]);
     circle(6,6,6,16+14,vp[2]);
     circle(6,6,5,16+15,vp[2]);
     for a:=0 to 4 do circle(6,6,a,16+16,vp[2]);
     getimage (0,0,12,12,flare,vp[2]);

     cls (16,vp[2]);

     repeat

     for a:=0 to 100 do
      begin
           with light[a] do
                begin
                     if c='1' then x1:=(random(30)-15);
                     if c='2' then y1:=(random(30)-15);
                     if c='3' then x1:=(random(3)-1);
                     if c='4' then y1:=(random(3)-1);
                     if x1=0 then x1:=1;
                     if y1=0 then y1:=1;
                     if c='5' then if random(16*16)=0 then rotatepal;
                     if c='6' then if random(16*16*2)=0 then rotatepal;
                     x:=x+x1{*(random(30)-15)};
                     if x>319 then x:=0;
                     if x<0 then x:=319;
                     y:=y+y1{*(random(30)-15)};
                     if y>199 then y:=0;
                     if y<0 then y:=199;

                     putball(x,y,1,vp[2]);
                     {if random(256)=0 then rotatepal;}

                     {if random(16*16*4)=0 then blur(vp[2]);}
                end;
      end;
      move(mem[vp[2]:0],mem[vga:0],64000);

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

     killimage(flare);
     cleartables;
     closevirt;
     video_mode ( 03);
     writeln ('you just saw some shadebob effect...');
     writeln ('Psychic Symphony / Label');
end.
