program Vstarfield;

uses mode13h,crt;

type    star=record
          x,y,z:integer;
          color:byte;
          end;

var stars:array [1..1000] of star;
    a:integer;

procedure initstars;
var a:integer;
begin
      setcolor (17,0,63,0);
      cls (0,vp[1]);
      for a:=0 to 15 do setcolor (a,a*4,a*4,a*4);
      for a:=1 to 1000 do
      begin
           stars[a].x:=random(320);
           stars[a].y:=random(200);
           stars[a].z:=random(255);
           stars[a].color:=random(15)+1;
      end;
end;

procedure clearstars;
var a:integer;
begin
     for a:=1 to 1000 do putpixel(stars[a].x,stars[a].y,0,vp[1]);
end;

procedure movestars2d(ix,iy:integer);
var a:word;
begin
      for a:=1 to 1000 do
      begin
            stars[a].x:=stars[a].x+ix*(stars[a].color div 4);
            stars[a].y:=stars[a].y+iy*(stars[a].color div 4);
            if stars[a].x<0 then stars[a].x:=319;
            if stars[a].x>319 then stars[a].x:=0;
            if stars[a].y<0 then stars[a].y:=199;
            if stars[a].y>199 then stars[a].y:=0;
      end;
end;

procedure drawstars;
var a:integer;
begin
      for a:=1 to 1000 do
      putpixel(stars[a].x,stars[a].y,stars[a].color,vp[1]);
end;

procedure movearound2d(ix,iy:integer);
begin
     clearstars;
     movestars2d (ix,iy);
     drawstars;
     waitvbl;
     copypage(vp[1],vga);
end;

procedure startinit;
begin
     initgraph;
     initvirt;
end;

procedure endinit;
begin
     closegraph;
     closevirt;
     textcolor (15);
     writeln ('Starfield v3.0');
     writeln;
     textcolor (8);
     writeln ('Have a very nice day!');
end;

procedure appeardot(x,y,n:integer);

begin
     putpixel (x,y,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,2,vp[1]);
     putpixel (x-1,y,1,vp[1]);
     putpixel (x+1,y,1,vp[1]);
     putpixel (x,y+1,1,vp[1]);
     putpixel (x,y-1,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,3,vp[1]);
     putpixel (x-1,y,2,vp[1]);
     putpixel (x+1,y,2,vp[1]);
     putpixel (x,y+1,2,vp[1]);
     putpixel (x,y-1,2,vp[1]);
     putpixel (x-1,y+1,1,vp[1]);
     putpixel (x+1,y+1,1,vp[1]);
     putpixel (x+1,y-1,1,vp[1]);
     putpixel (x-1,y-1,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,4,vp[1]);
     putpixel (x-1,y,3,vp[1]);
     putpixel (x+1,y,3,vp[1]);
     putpixel (x,y+1,3,vp[1]);
     putpixel (x,y-1,3,vp[1]);
     putpixel (x-1,y+1,1,vp[1]);
     putpixel (x+1,y+1,1,vp[1]);
     putpixel (x-1,y-1,1,vp[1]);
     putpixel (x+1,y-1,1,vp[1]);
     putpixel (x-2,y,1,vp[1]);
     putpixel (x+2,y,1,vp[1]);
     putpixel (x,y+2,1,vp[1]);
     putpixel (x,y-2,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,5,vp[1]);
     putpixel (x-1,y,4,vp[1]);
     putpixel (x+1,y,4,vp[1]);
     putpixel (x,y+1,4,vp[1]);
     putpixel (x,y-1,4,vp[1]);
     putpixel (x-1,y+1,2,vp[1]);
     putpixel (x+1,y+1,2,vp[1]);
     putpixel (x+1,y-1,2,vp[1]);
     putpixel (x-1,y-1,2,vp[1]);
     putpixel (x-2,y,2,vp[1]);
     putpixel (x+2,y,2,vp[1]);
     putpixel (x,y+2,2,vp[1]);
     putpixel (x,y-2,2,vp[1]);
     putpixel (x-3,y,1,vp[1]);
     putpixel (x+3,y,1,vp[1]);
     putpixel (x,y+3,1,vp[1]);
     putpixel (x,y-3,1,vp[1]);
     delay (n);
     putpixel (x,y,6,vp[1]);
     putpixel (x-1,y,5,vp[1]);
     putpixel (x+1,y,5,vp[1]);
     putpixel (x,y+1,5,vp[1]);
     putpixel (x,y-1,5,vp[1]);
     putpixel (x-1,y+1,3,vp[1]);
     putpixel (x+1,y+1,3,vp[1]);
     putpixel (x+1,y-1,3,vp[1]);
     putpixel (x-1,y-1,3,vp[1]);
     putpixel (x-2,y,3,vp[1]);
     putpixel (x+2,y,3,vp[1]);
     putpixel (x,y+2,3,vp[1]);
     putpixel (x,y-2,3,vp[1]);
     putpixel (x-3,y,2,vp[1]);
     putpixel (x+3,y,2,vp[1]);
     putpixel (x,y+3,2,vp[1]);
     putpixel (x,y-3,2,vp[1]);
     putpixel (x-4,y,1,vp[1]);
     putpixel (x+4,y,1,vp[1]);
     putpixel (x,y+4,1,vp[1]);
     putpixel (x,y-4,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,10,vp[1]);
     putpixel (x-1,y,8,vp[1]);
     putpixel (x+1,y,8,vp[1]);
     putpixel (x,y+1,8,vp[1]);
     putpixel (x,y-1,8,vp[1]);
     putpixel (x-1,y+1,4,vp[1]);
     putpixel (x+1,y+1,4,vp[1]);
     putpixel (x+1,y-1,4,vp[1]);
     putpixel (x-1,y-1,4,vp[1]);
     putpixel (x-2,y,4,vp[1]);
     putpixel (x+2,y,4,vp[1]);
     putpixel (x,y+2,4,vp[1]);
     putpixel (x,y-2,4,vp[1]);
     putpixel (x-3,y,2,vp[1]);
     putpixel (x+3,y,2,vp[1]);
     putpixel (x,y+3,2,vp[1]);
     putpixel (x,y-3,2,vp[1]);
     putpixel (x-4,y,2,vp[1]);
     putpixel (x+4,y,2,vp[1]);
     putpixel (x,y+4,2,vp[1]);
     putpixel (x,y-4,2,vp[1]);
     putpixel (x-5,y,1,vp[1]);
     putpixel (x+5,y,1,vp[1]);
     putpixel (x,y+5,1,vp[1]);
     putpixel (x,y-5,1,vp[1]);
     copypage (vp[1],vga);
     delay (n);
     putpixel (x,y,12,vp[1]);
     putpixel (x-1,y,10,vp[1]);
     putpixel (x+1,y,10,vp[1]);
     putpixel (x,y+1,10,vp[1]);
     putpixel (x,y-1,10,vp[1]);
     putpixel (x-1,y+1,6,vp[1]);
     putpixel (x+1,y+1,6,vp[1]);
     putpixel (x+1,y-1,6,vp[1]);
     putpixel (x-1,y-1,6,vp[1]);
     putpixel (x-2,y,6,vp[1]);
     putpixel (x+2,y,6,vp[1]);
     putpixel (x,y+2,6,vp[1]);
     putpixel (x,y-2,6,vp[1]);
     putpixel (x-3,y,4,vp[1]);
     putpixel (x+3,y,4,vp[1]);
     putpixel (x,y+3,4,vp[1]);
     putpixel (x,y-3,4,vp[1]);
     putpixel (x-4,y,4,vp[1]);
     putpixel (x+4,y,4,vp[1]);
     putpixel (x,y+4,4,vp[1]);
     putpixel (x,y-4,4,vp[1]);
     putpixel (x-5,y,2,vp[1]);
     putpixel (x+5,y,2,vp[1]);
     putpixel (x,y+5,2,vp[1]);
     putpixel (x,y-5,2,vp[1]);
     putpixel (x-6,y,1,vp[1]);
     putpixel (x+6,y,1,vp[1]);
     putpixel (x,y+6,1,vp[1]);
     putpixel (x,y-6,1,vp[1]);
     putpixel (x-2,y+2,2,vp[1]);
     putpixel (x-2,y-2,2,vp[1]);
     putpixel (x+2,y+2,2,vp[1]);
     putpixel (x+2,y-2,2,vp[1]);
     copypage (vp[1],vga);
     delay (n);
end;

procedure kaboom;
begin
     for a:=15 downto 0 do
         begin
         cls (a,vp[1]);
         copypage (vp[1],vga);
         delay (100);
         end;
end;

procedure appearunt;
begin
     appeardot(170,80,50);
     appeardot(125,125,50);
     appeardot(70,80,50);
     appeardot(70,110,50);
     appeardot(255,110,50);
     appeardot(125,95,50);
     appeardot(170,110,50);
     appeardot(40,125,50);
     appeardot(140,95,50);
     appeardot(25,95,50);
     appeardot(25,80,50);
     appeardot(255,80,50);
     appeardot(240,80,50);
     appeardot(255,95,50);
     appeardot(25,110,50);
     appeardot(255,125,50);
     appeardot(70,95,50);
     appeardot(170,125,50);
     appeardot(270,80,50);
     appeardot(155,110,50);
     appeardot(170,95,50);
     appeardot(125,110,50);
     appeardot(55,125,50);
     appeardot(125,80,50);
end;

procedure appearsmile;
begin
     appeardot (135,75,50);
     appeardot (185,75,50);
     appeardot (100,100,10);
     appeardot (105,110,10);
     appeardot (115,120,10);
     appeardot (125,125,10);
     appeardot (140,126,10);
     appeardot (155,127,10);
     appeardot (170,126,10);
     appeardot (185,125,10);
     appeardot (195,120,10);
     appeardot (205,110,10);
     appeardot (210,100,10);
end;

begin
     startinit;
     loadpcx ('data01.dat',vp[2]);
     for a:=1 to 150 do
         begin
         copypage (vp[2],vga);
         delay (20);
         move(mem[vp[2]:320],mem[vp[2]:0],47680);
         end;
     loadpcx ('data02.dat',vp[3]);
     for a:=1 to 130 do
         begin
         copypage (vp[3],vga);
         delay (20);
         move(mem[vp[3]:320],mem[vp[3]:0],41680);
         end;
     initstars;
     appearunt;
     kaboom;
     for a:=1 to 100 do movearound2d(-1,0);
     for a:=1 to 100 do movearound2d(-1,1);
     for a:=1 to 100 do movearound2d(0,1);
     for a:=1 to 100 do movearound2d(1,1);
     for a:=1 to 100 do movearound2d(1,0);
     for a:=1 to 100 do movearound2d(1,-1);
     for a:=1 to 100 do movearound2d(0,-1);
     for a:=1 to 100 do movearound2d(-1,-1);
     appearsmile;
     delay (100);
     endinit;
end.