{컴컴컴컴컴컴컴컴커}
{ june'97 by Cazz �}
{컴컴컴컴컴컴컴컴켸}
{$A+,B-,D-,E-,F-,G+,I-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,X+}
{$M 2048,0,655360}
unit part3;

     interface

procedure dopart3;

     implementation

uses vga256,pcx,keyboard,misc;

var     a,b:integer;
        c,d:word;
        scr:pointer;
        scr2:pointer;
        scrseg:word;
        palet:palette;
        ang,aa:byte;
        light:array[0..128*128-1] of byte;
        f:file;
        p1,p2,p3,p4:integer;
        ps1,ps2:word;

procedure dopart3;
var     lx,ly:integer;
        vlx,vly:integer;
        nx,ny:integer;
        x,y:integer;
        next,inf:pointer;
begin
{ INIT }
  getmem(scr,65535);
  scrseg:=seg(scr^);
  clear(0,scrseg);
  getmem(scr2,65535);
  vidseg:=seg(scr2^);
  clear(0,vidseg);


{ load data }
  load_pcx_datafile(5,320,vidseg,0,false);
  getimage(0,0,116,17,320,scr2,next,true);
  load_pcx_datafile(6,320,vidseg,0,false);
  getimage(0,0,179,15,320,scr2,inf,true);
  load_pcx_datafile(4,320,vidseg,0,false);
  loaddata(addr(light),10);

  fillchar(pal,sizeof(pal),0);
  fillchar(palet,sizeof(palet),0);
  for a:=0 to 63 do
    begin
      pal[a+64,1]:=a;
      pal[a+64,2]:=a;
      pal[a+64,3]:=a;
    end;
  for a:=128 to 255 do
    begin
      pal[a,1]:=63;
      pal[a,2]:=63;
      pal[a,3]:=63;
    end;

  initkey;


  d:=0;
  aa:=127;
  ang:=0;
  p4:=32;
  repeat
    lfrm:=frm;
{ fading stuff }
    if d<63 then
      begin
        for b:=0 to 255 do
          begin
            if palet[b,1]<pal[b,1] then inc(palet[b,1]);
            if palet[b,2]<pal[b,2] then inc(palet[b,2]);
            if palet[b,3]<pal[b,3] then inc(palet[b,3]);
          end;
        initpalette(palet);
      end;
    if (d>600-64) then
      begin
        for b:=0 to 255 do
          begin
            if palet[b,1]>0 then dec(palet[b,1]);
            if palet[b,2]>0 then dec(palet[b,2]);
            if palet[b,3]>0 then dec(palet[b,3]);
          end;
        initpalette(palet);
      end;
    p1:=96+_cos[ang] div 3;
    p2:=32+_sin[ang] div 5;
    p3:=96+_cos[aa] div 3;
    ps1:=p2*320+p1;
    ps2:=p4*320+p3;
    for a:=0 to 127 do
      begin
        c:=a*128;
        if p2>=p4 then
          begin
            for b:=0 to 127 do
              if light[c+b]>0 then
                begin
                  mem[scrseg:ps1+b]:=mem[vidseg:ps1+b]+light[c+b];
                  if mem[scrseg:ps2+b]>0 then
                    inc(mem[scrseg:ps2+b],light[c+b])
                  else
                    mem[scrseg:ps2+b]:=mem[vidseg:ps2+b]+light[c+b]
                end
          end
        else
          for b:=0 to 127 do
            if light[c+b]>0 then
              begin
                mem[scrseg:ps2+b]:=mem[vidseg:ps2+b]+light[c+b];
                if mem[scrseg:ps1+b]>0 then
                  inc(mem[scrseg:ps1+b],light[c+b])
                else
                  mem[scrseg:ps1+b]:=mem[vidseg:ps1+b]+light[c+b]
              end;
        inc(ps1,320);
        inc(ps2,320);
      end;
    page(scr,vidram);
    clear(0,scrseg);
    inc(ang,2);
    inc(aa,2);
    inc(d);
    repeat until frm>(lfrm+1);
    if key[1] then begin over:=true; break; end;
  until d>600;
  c:=0;




  fillchar(palet,sizeof(palet),0);
  fillchar(pal,sizeof(pal),0);
  for a:=0 to 127 do                    { palette }
    begin
      pal[a+128,1]:=a div 2;
      pal[a+128,2]:=a div 2;
      pal[a+128,3]:=a div 2;
    end;
  c:=0;
  ang:=0;

{ original pascal version of this loop coded by HELiX }
{ MAIN LOOP }
  if not over then
  repeat
    lfrm:=frm;
{ fade on }
    if c<63 then
      begin
        for b:=255 downto 255-c*2 do
          begin
            if palet[b,1]<pal[b,1] then inc(palet[b,1]);
            if palet[b,2]<pal[b,2] then inc(palet[b,2]);
            if palet[b,3]<pal[b,3] then inc(palet[b,3]);
          end;
        initpalette(palet);
      end;
{ fade off }
    if (c>550-64) then
      begin
        for b:=128 to 255 do
          begin
            if palet[b,1]>0 then dec(palet[b,1]);
            if palet[b,2]>0 then dec(palet[b,2]);
            if palet[b,3]>0 then dec(palet[b,3]);
          end;
        initpalette(palet);
      end;
{ do the main part }
    lx:=_cos[ang] div 3+160;
    ly:=_sin[byte(ang*2)] div 4+100;
    vlx:=-lx;
    for x:=1 to 318 do
      begin
        inc(vlx);
        vly:=-ly;
        if (vlx<130) and (vlx>-130) then
            asm
                  push ds
                  mov ax,vidseg
                  mov es,ax
                  mov ax,scrseg
                  mov ds,ax
                  mov di,320
                  add di,x
                  mov cx,198

                  mov ax,vly
                  cmp ax,129           { too far light }
                  jg @not_at_all
@inner1:
                  inc vly
                  cmp vly,-130
                  jl @dont
                  xor ax,ax
                  mov al,es:[di+1]
                  mov bx,ax
                  mov al,es:[di-1]
                  sub bx,ax
                  mov word ptr nx,bx
                  mov al,es:[di+320]
                  mov bx,ax
                  mov al,es:[di-320]
                  sub bx,ax
                  mov word ptr ny,bx

                  mov ax,vlx
                  sub ax,nx
                  cwd           { abs }
                  xor ax,dx     { abs }
                  sub ax,dx     { abs }
                  mov bx,ax     { save it }
                  mov ax,vly
                  sub ax,ny
                  cwd
                  xor ax,dx
                  sub ax,dx
                  add ax,bx
                  cmp ax,127
                  jle @no_o127
                  mov ax,127
@no_o127:
                  sub ax,255
                  neg ax
                  mov ds:[di],al
@dont:
                  add di,320
                  dec cx
                  jnz @inner1
@not_at_all:
                  pop ds
            end;
      end;
    inc(c);
    inc(ang);
    if (c>140) and (c<160) then putimage(180,339-c,next,scr);
    if (c>=160) and (c<300) then putimage(180,180,next,scr);
    if (c>=300) and (c<320) then putimage(180,c-120,next,scr);
    if (c>=320) and (c<340) then putimage(120,524-c,inf,scr);
    if (c>=340) and (c<500) then putimage(120,185,inf,scr);
    if (c>=500) and (c<520) then putimage(120,c-315,inf,scr);
    page(scr,vidram);
    clear(0,scrseg);
    repeat until frm>(lfrm+1);
    if key[1] then begin over:=true; break; end;
  until (c>550);
  disabkey;
  clear(0,$a000);
  freemem(inf,2884);
  freemem(next,2110);
  freemem(scr2,65535);
  freemem(scr,65535);
end;

end.