program Vstarfield;

uses mode13h,crt;

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

var xms:array [1..1000] of xtra;
    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 movestars3d;
var a:word;
var z:integer;
begin
         for a:=1 to 1000 do
             begin
             {putpixel(xms[a].x,xms[a].y,0,vp[1]);}
             cls (0,vp[1]);
             if stars[a].z > 0 then
               begin

             xms[a].x:= stars[a].x SHL 8 div stars[a].z + 160;
             xms[a].y:= stars[a].y SHL 8 div stars[a].z + 100;

             putpixel(xms[a].x,xms[a].y,17,vp[1]);
             copypage (vp[1],vga);
             stars[a].z:=stars[a].z -1;
             {putpixel(xms[a].x,xms[a].y,0,vp[1]);
             if stars[a].z <1 then stars[a].z:=250;
             if stars[a].z >250 then stars[a].z:=1;
             xms[a].x:=(stars[a].x -160)* (stars[a].z div 10) +160;
             xms[a].y:=(stars[a].y -100)* (stars[a].z div 10) +100;
             copypage (vp[1],vga);
             stars[a].z :=stars[a].z -1;
             if stars[a].z <1 then stars[a].z :=250;
             if stars[a].z >249 then stars[a].z :=1;}
             end;
             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 v2.2');
     writeln;
     textcolor (8);
     writeln ('Have a nice day!');
end;

procedure appeardot;
begin
     putpixel (160,100,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,2,vp[1]);
     putpixel (159,100,1,vp[1]);
     putpixel (161,100,1,vp[1]);
     putpixel (160,101,1,vp[1]);
     putpixel (160,99,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,3,vp[1]);
     putpixel (159,100,2,vp[1]);
     putpixel (161,100,2,vp[1]);
     putpixel (160,101,2,vp[1]);
     putpixel (160,99,2,vp[1]);
     putpixel (159,101,1,vp[1]);
     putpixel (161,101,1,vp[1]);
     putpixel (159,99,1,vp[1]);
     putpixel (159,99,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,4,vp[1]);
     putpixel (159,100,3,vp[1]);
     putpixel (161,100,3,vp[1]);
     putpixel (160,101,3,vp[1]);
     putpixel (160,99,3,vp[1]);
     putpixel (159,101,1,vp[1]);
     putpixel (161,101,1,vp[1]);
     putpixel (159,99,1,vp[1]);
     putpixel (161,99,1,vp[1]);
     putpixel (158,100,1,vp[1]);
     putpixel (162,100,1,vp[1]);
     putpixel (160,102,1,vp[1]);
     putpixel (160,98,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,5,vp[1]);
     putpixel (159,100,4,vp[1]);
     putpixel (161,100,4,vp[1]);
     putpixel (160,101,4,vp[1]);
     putpixel (160,99,4,vp[1]);
     putpixel (159,101,2,vp[1]);
     putpixel (161,101,2,vp[1]);
     putpixel (161,99,2,vp[1]);
     putpixel (159,99,2,vp[1]);
     putpixel (158,100,2,vp[1]);
     putpixel (162,100,2,vp[1]);
     putpixel (160,102,2,vp[1]);
     putpixel (160,98,2,vp[1]);
     putpixel (157,100,1,vp[1]);
     putpixel (163,100,1,vp[1]);
     putpixel (160,103,1,vp[1]);
     putpixel (160,97,1,vp[1]);
     delay (400);
     putpixel (160,100,6,vp[1]);
     putpixel (159,100,5,vp[1]);
     putpixel (161,100,5,vp[1]);
     putpixel (160,101,5,vp[1]);
     putpixel (160,99,5,vp[1]);
     putpixel (159,101,3,vp[1]);
     putpixel (161,101,3,vp[1]);
     putpixel (161,99,3,vp[1]);
     putpixel (159,99,3,vp[1]);
     putpixel (158,100,3,vp[1]);
     putpixel (162,100,3,vp[1]);
     putpixel (160,102,3,vp[1]);
     putpixel (160,98,3,vp[1]);
     putpixel (157,100,2,vp[1]);
     putpixel (163,100,2,vp[1]);
     putpixel (160,103,2,vp[1]);
     putpixel (160,97,2,vp[1]);
     putpixel (156,100,1,vp[1]);
     putpixel (164,100,1,vp[1]);
     putpixel (160,104,1,vp[1]);
     putpixel (160,96,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,10,vp[1]);
     putpixel (159,100,8,vp[1]);
     putpixel (161,100,8,vp[1]);
     putpixel (160,101,8,vp[1]);
     putpixel (160,99,8,vp[1]);
     putpixel (159,101,4,vp[1]);
     putpixel (161,101,4,vp[1]);
     putpixel (161,99,4,vp[1]);
     putpixel (159,99,4,vp[1]);
     putpixel (158,100,4,vp[1]);
     putpixel (162,100,4,vp[1]);
     putpixel (160,102,4,vp[1]);
     putpixel (160,98,4,vp[1]);
     putpixel (157,100,2,vp[1]);
     putpixel (163,100,2,vp[1]);
     putpixel (160,103,2,vp[1]);
     putpixel (160,97,2,vp[1]);
     putpixel (156,100,2,vp[1]);
     putpixel (164,100,2,vp[1]);
     putpixel (160,104,2,vp[1]);
     putpixel (160,96,2,vp[1]);
     putpixel (155,100,1,vp[1]);
     putpixel (165,100,1,vp[1]);
     putpixel (160,105,1,vp[1]);
     putpixel (160,95,1,vp[1]);
     copypage (vp[1],vga);
     delay (400);
     putpixel (160,100,12,vp[1]);
     putpixel (159,100,10,vp[1]);
     putpixel (161,100,10,vp[1]);
     putpixel (160,101,10,vp[1]);
     putpixel (160,99,10,vp[1]);
     putpixel (159,101,6,vp[1]);
     putpixel (161,101,6,vp[1]);
     putpixel (161,99,6,vp[1]);
     putpixel (159,99,6,vp[1]);
     putpixel (158,100,6,vp[1]);
     putpixel (162,100,6,vp[1]);
     putpixel (160,102,6,vp[1]);
     putpixel (160,98,6,vp[1]);
     putpixel (157,100,4,vp[1]);
     putpixel (163,100,4,vp[1]);
     putpixel (160,103,4,vp[1]);
     putpixel (160,97,4,vp[1]);
     putpixel (156,100,4,vp[1]);
     putpixel (164,100,4,vp[1]);
     putpixel (160,104,4,vp[1]);
     putpixel (160,96,4,vp[1]);
     putpixel (155,100,2,vp[1]);
     putpixel (165,100,2,vp[1]);
     putpixel (160,105,2,vp[1]);
     putpixel (160,95,2,vp[1]);
     putpixel (154,100,1,vp[1]);
     putpixel (166,100,1,vp[1]);
     putpixel (160,106,1,vp[1]);
     putpixel (160,94,1,vp[1]);
     putpixel (158,102,2,vp[1]);
     putpixel (158,98,2,vp[1]);
     putpixel (162,102,2,vp[1]);
     putpixel (162,98,2,vp[1]);
     copypage (vp[1],vga);
     delay (400);
end;

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

begin
     startinit;
     loadpcx ('dat01.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 ('dat02.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;
     appeardot;
     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);
     endinit;
end.