uses crt,gfx3;

var
 Virscr2 : VirtPtr;                     { Our first Virtual screen }
 Vaddr2  : word;                        { The segment of our virtual screen}
 Virscr3 : VirtPtr;                     { Our first Virtual screen }
 Vaddr3  : word;                        { The segment of our virtual screen}
 time:longint absolute $40:$6c;
 x,y,            {These two are used in the main loop}
 vlx,vly,        {The Light Vector (Origin 0,0)}
 lx,ly,          {This is a point representing the position of the light source}
 Nx,ny:integer;  {This will be used to store the Pseudo-normal}
 l1:word;        {This is for general use}
 f:text;







procedure optdobump; {Now u guess what this one does..hehehe}
var difx,   {The X axis difference}
    dify,   {The Y axis difference}
    col:byte; {Used in many points..}
    l1:integer; {General use}
    o:word;
    Loop:word;
    r,g,b:byte;
begin
  Loop:=0;
  Time:=0;
  lx:=-160; {The starting position of the light source}
  ly:=100; {>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  l1:=0;
  repeat
    Inc(Loop);
    inc(l1);
    lx:=round(cos(l1/13)*96+160);
    ly:=round(sin(l1/23)*96+100);
    x:=0;
    y:=0;
      For o:=640 to 63678 do
       Begin
        Inc(x);
        If X=320 Then
         Begin
          Inc(Y);
          X:=0;
         End;
{        vlx:=X-lx; {Calculate the L vector}
{        vly:=Y-ly; {>>>>>>>>>>>>>>>>>>>>>>}
         Vlx:=(sqr(x-lx) shr 4) shr 3;
         Vly:=(sqr(y-ly) shr 4) shr 3;

        if (vlx<130) and (vlx>-130) and (vly>-130) and (vly<130 ) then begin
          Asm
           Mov Es,Vaddr
           Mov Di,o
           Inc Di
           Mov Al,Es:[Di]
           xor ah,ah
           Sub Di,2
           Mov Bl,Es:[Di]
           xor Bh,Bh
           Mov Nx,Ax
           Sub Nx,Bx
           Add Di,321
           Mov Al,Es:[Di]
           Sub Di,640  (******)
           Mov Bl,Es:[Di]
           Mov Ny,Ax
           Sub Ny,Bx

           Mov Ax,Vlx
           Cmp Ax,Nx
           Jl  @jumpit
           sub Ax,Nx
           Mov Col,Al
           Jmp @nextt
           @jumpit:
           Mov Bx,Nx
           Sub Bl,Al
           Mov Col,Bl
           @nextt:
          End;
{          nx:=mem[vaddr:o+1]-mem[vaddr:o-1];
          ny:=mem[vaddr:o+320]-mem[vaddr:o-320];
          col:=abs(vlx-nx);}
          if col>127 then col:=127;
          difx:=127-col;
          if difx<0 then difx:=1;
          col:=byte(abs(vly-ny));
         if col>127 then col:=127;
         dify:=127-col;
        if dify<0 then dify:=1;
        col:=(difx+dify);
        If Col>185 Then
         Begin
          {Col:=Col-185;}
{          If Mem[Vaddr3:o]=0 Then Col:=Col+85;
          If Mem[Vaddr3:o]=1 Then Col:=Col+170;}
          mem[vaddr2:o]:=col;
         End;
      end;
    End;
   HLine(0,319,199,0,vaddr2);
   HLine(0,319,0,0,vaddr2);
   Line(0,0,0,199,0,Vaddr2);
   Line(319,0,319,199,0,Vaddr2);
   flip32(vaddr2,sega000);
   cls32(vaddr2,0);
  until Keypressed;
  SetText;
  Writeln(l1/Time :0 :3);
  Readln;
end;




Procedure GetMPallete(Fil1,Fil2:String;Where:Word);
Var TCol:Byte;
    Pale:Array[1..3,0..256] of Byte;
Begin
 LPcx(Fil1,Where);
 For TCol:=0 to 128 do
   GetPal(Getpixel(160-TCol,100,Where),Pale[1,TCol],Pale[2,TCol],Pale[3,TCol]);

 LPcx(Fil2,Where);
 For TCol:=0 to 128 do
   GetPal(Getpixel(160-TCol,100,Where),Pale[1,TCol+128],Pale[2,TCol+128],Pale[3,TCol+128]);
 For TCol:=128 to 255 do

  Pal(TCol ,Pale[1,Abs(TCol-255)+128],Pale[2,Abs(TCol-255)+128],Pale[3,Abs(TCol-255)+128]);
 For TCol:=0 to 128 do
  Pal(TCol,Pale[1,128-TCol],Pale[2,128-TCol],Pale[3,128-TCol]);
End;

procedure savepal(s:string);
var f:text;
    l1,l2:integer;
    r,g,b:byte;

begin
  assign(f,s);
  rewrite(f);
  for l1:=1 to 255 do begin
    getpal(l1,r,g,b);
    writeln(f,'Pal(',l1,',',r,',',g,',',b,');');
  end;
  close(f);
end;


Procedure GetMPallete3(Fil1,Fil2,Fil3:String;Where:Word);
Var TCol:Byte;
    Pale:Array[1..3,0..256] of Byte;
    xou:integer;
Begin
 LPcx(Fil1,Where);
 Readln;
 For TCol:=0 to 85 do begin
   GetPal(Getpixel(160-(TCol*2),100,Where),Pale[1,TCol],Pale[2,TCol],Pale[3,TCol]);
   putpixel(160-tcol,100,0,vga);
   delay(1);
 end;
 LPcx(Fil3,Where);
 Readln;
 For TCol:=0 to 85 do begin
   xou:=160-round(tcol*2);
   if xou<0 then xou:=0;

   GetPal(Getpixel(xou,100,Where),Pale[1,TCol+170],Pale[2,TCol+170],Pale[3,TCol+170]);
 end;
 LPcx(Fil2,Where);
 Readln;
 For TCol:=1 to 85 do
   GetPal(Getpixel(160-round(TCol *2.3),100,Where),Pale[1,TCol+85],Pale[2,TCol+85],Pale[3,TCol+85]);

 For TCol:=85 to 170 do
  Pal(TCol ,Pale[1,170-round((TCol))+85],Pale[2,170-TCol+85],Pale[3,170-TCol+85]);
 For TCol:=0 to 85 do
  Pal(TCol,Pale[1,85-TCol],Pale[2,85-TCol],Pale[3,85-TCol]);
 For TCol:=0 to 85 do
  Pal(TCol+170,Pale[1,85-TCol+170],Pale[2,85-TCol+170],Pale[3,85-TCol+170]);
  savepal('pal.txt');
End;




begin
  setmcga;
  setupvirtual;
  GetMem (VirScr2,64000);
  vaddr2 := seg (virscr2^);
  GetMem (VirScr3,64000);
  vaddr3 := seg (virscr3^);
  Lpcx('forbump.pcx',Vaddr3);
  lpcx('BMAP2.pcx',vaddr);
  asmfade(virscr);
  asmfade(virscr);
    asmfade(virscr);
  asmfade(virscr);
  GetMPallete3('lenpal2.pcx','lenpal4.pcx','pmAP3.pcx',sega000);
  Pal(0,0,0,0);
{  Cls(vga,0);
  For l1:=0 to 255 do
   Putpixel(l1,100,l1,vga);
  Readln;}
  savepal('bpal.txt');
  optdobump;   {do it.. :)}
  shutdown;
  FreeMem (VirScr2,64000);
  FreeMem (VirScr3,64000);
  settext;
end.
