{$N+,A+,G+,F+,D-,E-,L-,R-,S-,Q-,Y-}
unit wizard4;

interface

procedure scene_wizard;

implementation

uses global, dither, math2, screen, memory, resource, palette;

const
pattern: array[0..4*4-1] of byte =
(0,8,2,10, 12,4,14,6, 3,11,1,9, 15,7,13,5);


var
Aantal, T1, start_time, old_time, elapsed_time:word;



var
y_fire, qq,q,xx,yy:single;
speed_scroll, col:byte;
d,x4,y4, x3,y3, x1,x2,y1,y2:integer;
complete_scroll, height_scroll, segs,offs, n :word;
wizard_data, x,y:integer;
offset_di, offset_dx, f, r,g,b:word;


procedure fill_dither_buffer(where:word);
begin
if b=1 then
{for y:=0 to 3 do for x:=0 to 319 do
mem[vaddr:x+y*320 +64000]:=pattern[(x and 3)+(y and 3)*4];{}
asm
 push ds
 mov es, segs
 mov si, offs

 mov di, 64000 + 8*4-1
 mov ds, where
 mov dx, 3
 @y:
   mov ax, dx
   and ax, 3
   shl ax, 2
   mov cx, 7
   @x:
      mov bx, cx
      add bx, 2
      and bx, 3
      add bx, ax
      mov bl, es:[bx+si]
      mov ds:[di], bl
      dec di
      dec cx
   jns @x
 dec dx
jns @y
pop ds
end else
asm
mov di, 64000
mov es, where
db $66; xor ax, ax
mov cx, 8
db $66; rep stosw
end;
end;

procedure Vscroll_buffer(height,where:word);
var vsi:word;
begin
  vsi:=320*height;
  asm
  mov es, where
  xor di, di
  mov si, vsi
  push ds
  mov ds, where
  mov dx, 200
  @y:
      mov cx, 320/4
      db $66; rep movsw
      cmp si, 320*200
      jb @skip
        mov si, 320*200
      @skip:
  dec dx
  jnz @y
  pop ds
  end;
end;

procedure Append_Buffer(height_scrll, complete_scrll, where: word);
begin
   if height_scrll=0 then exit;
   if complete_scrll>199 then complete_scrll:=199;
   if longint((complete_scrll+height_scrll))*320 <= 64000
   then LoadBinAdvance(wizard_data,
   complete_scrll*320, height_scrll*320, where, 64000-height_scrll*320);
end;


procedure Scene_Wizard;
begin
segs:=seg(pattern); offs:=ofs(pattern);

Clear(0, 65535, vaddr2);
Clear(0, 65535, vaddr3);
Clear(0, 64000, vga);

LoadBinUnpack('wizard1.bin',  {load->}vaddr,{-unpack->}vaddr3,  0);
Clear(0, 65535, vaddr);


wizard_data:=index_resource('wizard2.bin');
if (wizard_data<0) then ErrorLoading('Resource not found!', 0);


for n:=0 to 255 do setonecolor2memw(n, 0,0,0, palette1, seg_palitra,ofs_palitra);
Midas_WaitVR;
setpalette_from_memW(palette1,seg_palitra,ofs_palitra);
set_border_color(0);


n:=0;
for r:=0 to 7 do
  for g:=0 to 7 do
    for b:=0 to 3 do
     begin
     setonecolor2memW(n, r*9,g*9,b*21, palette2, seg_palitra,ofs_palitra);
     inc(n);
end;

SetOneColor2memW(1,  13, 16, 19, palette2, seg_palitra,ofs_palitra); {hood}
SetOneColor2memW(2,  15, 10, 20, palette2, seg_palitra,ofs_palitra); {blink}
SetOneColor2memW(3,  15, 10, 10, palette2, seg_palitra,ofs_palitra); {hand}
SetOneColor2memW(15, 10, 12, 26, palette2, seg_palitra,ofs_palitra); {back}

init_pal2pal(seg_palitra,ofs_palitra);
palette_process[0]:=1;
palette_process[1]:=1;


Wait_music_pos(56, 32); if ESC_PRESSED=1 then exit;


q:=0;
d:=35;
f:=0;
b:=0;

y_fire:=260;
complete_scroll:=0;
height_scroll:=0;
speed_scroll:=1;

Aantal:=0; T1:=mTimer;
old_time:=mtimer;
start_time:=mtimer;

repeat
elapsed_time:=(mtimer-old_time);
old_time:=mtimer;

q:=(mtimer-start_time)*0.2;


if (elapsed_time>0) or (aantal=0) then begin

       MP^.GetInformation(@info);


       if q>1 then begin
          height_scroll:=elapsed_time*2;
          speed_scroll:=1;
          if complete_scroll>190 then speed_scroll:=3;

          if y_fire>160 then y_fire:=y_fire-elapsed_time/2;

       end;


       xx:=sin_(pi*2 * (q/10) * 0.64543324)*32+160;
       yy:=cos_(pi*2 * (q/10) * 0.38457832)*15+y_fire;

       qq:=q/2;
       x1:=round(cos_(qq)*d+xx);
       y1:=round(sin_(qq)*d+yy);
       x2:=round(cos_(qq+1.5)*d+xx);
       y2:=round(sin_(qq+1.5)*d+yy);
       x3:=round(cos_(qq+2.5)*d+xx);
       y3:=round(sin_(qq+2.5)*d+yy);
       x4:=round(cos_(qq+4.5)*d+xx);
       y4:=round(sin_(qq+4.5)*d+yy);

       d:=round(cos_(q/4)*8+20);
       col:=round(-cos_(q)*7+8);
       CircleRGB_add(x1,y1,   d,  col,0,0,   48, vaddr, vaddr2, vaddr3);
       CircleRGB_add(x2,y2,   d,  0,col,0,   48, vaddr, vaddr2, vaddr3);
       CircleRGB_add(x4,y3,   d,  col,col,0, 48, vaddr, vaddr2, vaddr3);{}

       d:=round(cos_(q/4+2)*8+20);
       col:=round(-cos_(q+2)*7+8);
       CircleRGB(x2,y3,   d shr 1,  col shl 2,0,0, vaddr, vaddr2, vaddr3);
       CircleRGB(x3,y4,   d shr 1,  col shl 2,col shl 1,0, vaddr, vaddr2, vaddr3);{}

       {--------------------------------------------------------}
       if (aantal and speed_scroll = 1) then
       if (f=1) or (f=3) then
       begin
           if (height_scroll>0) and (complete_scroll<=199) then begin
                if complete_scroll+height_scroll>200 then height_scroll:=1;
                Vscroll_buffer(height_scroll, vaddr3);
                Append_buffer(height_scroll, complete_scroll, vaddr3);
                complete_scroll:=complete_scroll+height_scroll;
           end;
       end;
       {--------------------------------------------------------}


       inc(b);
       if b>1 then b:=0;

       if b=0 then fblur2_(vaddr);
       if b=1 then fblur2_(vaddr2);{}


       Fill_dither_buffer(vaddr);


       inc(f);
       f:=f and 3;
       if f=0 then begin offset_di:=0;   offset_dx:=199; end;
       if f=1 then begin offset_di:=320; offset_dx:=198; end;
       if f=2 then begin offset_di:=1;   offset_dx:=199; end;
       if f=3 then begin offset_di:=321; offset_dx:=198; end;

       {if Vsync_mode=1 then Midas_WaitVR;{}

       if Aantal and 1 = 0 then
       begin
           if palette_process[0]=1 then Pal2Pal_asm(palette_process[0], 0, 127, elapsed_time*8, seg_palitra,ofs_palitra)
       end
       else begin
           if palette_process[1]=1 then Pal2Pal_asm(palette_process[1], 128, 255, elapsed_time*8, seg_palitra,ofs_palitra);
       end;

       if complete_scroll>100 then SetOneColor(2, 15,10,20+round(cos(d)*8));{}


       {-------------------Draw frame -------------------------------}
       Draw_screen(f, offset_di, offset_dx, vaddr,vaddr2,vaddr3, vga);


       Inc(Aantal);
       if FPS_Mode=1 then WriteFps(100, mtimer, T1, Aantal, vga);

end
else begin

Midas_WaitVR;{}

end;

until (esc_test) or (music_pos(58,32));
end;

end.