{ FLIPPER1.PAS   - (c) Ansgar Scherp, Joachim Gelhaus
  All rights reserved / vt'95

  1 Parameter = 'abc'

  a = 1-4  ->> set overscan on/off and highres. on/off
  b = 1-2  ->> 1 = SB-Sound   2 = No Sound
  c = 1-6  ->> Players

  }

{$M 65520,0,655360}
{$P+,G+}
uses dos,crt,soundkit,audiotpu;

const N1 = ' PCS-PINBALL  - Version 1.1 written by A.Scherp and J.Gelhaus ';
      N2 = ' (c)opyrights reserved by PC Spiel and vIRTUAL tECHNOLOGIES GbR';

const {LabelName  : string[9] = 'SPECIAL27';}
      Bits       : array[0..9] of byte = (128,64,32,16,8,4,2,1,0,0);
      VSeg       : word = $A000;
      speedmaxy  : byte = 100; {max. bally-speed. (( 45}

      tnr  : char = '1'; {tablenr}

      ArmBreiteLinks : byte = 56;
      ArmHoeheLinks  : byte = 48;
      ArmXLinks      : word = 79;
      ArmYLinks      : word = 400+135;

      ArmBreiteRechts : byte = 56;
      ArmHoeheRechts  : byte = 48;
      ArmXRechts      : word = 159;
      ArmYRechts      : word = 400+135;

      FederBreite     : word = 8;
      FederX          : word = 302;

      no         : boolean = false;
      yes        : boolean = true;
      rahmen     : byte = 255;
      arm        : byte = 128;

const snd1 = 1;
      snd2 = 2;
      snd3 = 3;
      snd4 = 4;
      snd5 = 5;
      snd6 = 6;
      snd7 = 7;
      snd8 = 8;

      SetSprite_VGAADR : array[0..6] of word = (258,514,1026,2050,258,514,1026);
      GetSprite_VGAADR : array[0..6] of word = ($4,$104,$204,$304,$04,$104,$204);

      MaxBalls = 4;


{typedeclaration for the mask}
type ttableground1=array[0..319,0..199] of byte;
type ttableground2=array[0..319,200..399] of byte;
type ttableground3=array[0..319,400..599] of byte;
type reihe = array[1..15360] of byte;



var  OldHeapLimit: pointer;
     OldHeapSize : Longint;

     ledseg,
     armlinksseg,armrechtsseg,
     armlinks_mskseg,armrechts_mskseg,
     ballseg,
     groundseg,
     ballspriteseg,
     undergroundseg,
     tablegroundseg,federseg:word;

     led_display,
     ball,
     ground,
     ball_sprite,
     underground,
     tableground,feder:pointer;
     arm_links           : ^reihe;
     arm_rechts          : ^reihe;

     arm_links_msk       : ^reihe;
     arm_rechts_msk      : ^reihe;


     tableground1:^ttableground1;
     tableground2:^ttableground2;
     tableground3:^ttableground3;

     ch:char;

     led_hoehe:byte;
     led_color_1, led_color_2:byte;
     led_funktion, led_parameter,led_timer,led_x,led_Y,led_status:word;
     led_anzeige_text:string;
     led_f_status_1,led_f_status_2:byte;

     {SEG und OFS of the FONT}
     Fseg,Fofs : word;
     Fdata : array[1..4096] of byte;

     arm_links_status, arm_rechts_status:byte;
     arm_links_old_status, arm_rechts_old_status:byte;

     {BALL X and Y Coordinates }
     ballx,bally,bx_old,by_old:integer;
     {x und y - speed}
     ballspeed_y,ballspeed_x:integer;

     {a few randoms}
     ran255:array[0..255] of byte;
     ran255z:byte;

     {counter for the ball-gravitiy left, right, up,down}
     l1,l2,r1,r2,u1,u2,o1,o2 : byte;
     fu,fo,fl,fr,fm : byte;
     fh : byte;

     kraft : integer;

     overscan, highres : boolean;

     UseSound  : boolean;
     sounds:array[1..10] of pointer;   { Samples}
     soundlength:array[1..10] of word;

     score:array [1..6] of longint;

     StartPow  : word ;

     NormalPos : integer;
     CurrentPos : integer;

     path : string;

     MAXfarbe: byte;

     OldFileMode : byte;

     VideoMode : char;

     bende  : boolean; {ende -> true }

     pal : array[0..255] of record { palette }
             r : byte;
             g : byte;
             b : byte;
           end;

     ruetteln : byte;

     FederY : word;
     FederHoehe : word;

     hilfsb:byte;


{*** TABLE1  ************************************************************** }

     Kurven:word;
     Lichter1 : array[250..252] of byte;
     Lichter2 : array[247..249] of byte;
     Lichter3 : array[244..246] of byte;
     Licht4   : byte;
     PushUp   : boolean;
     Bonus    : byte;
     Balls    : array[1..6] of integer;
     MaxPlayer   : byte;
     ActPlayer : byte;
     PCSspe : array[1..3] of byte;
     special : byte;
     temp : byte;
     BumpCount : Byte;

procedure calc_page_pos_of_ballpos; forward;
procedure display(t : string); forward;
procedure check_flipper_arms; forward;
procedure analyse_arms; forward;
procedure senk_arms; forward;

{*** FONTS ***************************************************************** }
{$F+}
procedure font; external;
{$L FONTS\BLCKSNSF.OBJ}
{$F-}

{*** INCLUDEN ************************************************************** }
{$I _RANDOM .PAS} {short random number list}
{$I _VIDEO  .PAS} {all video functions // // and arm_draw}
{$I _LOADPRC.PAS} {all loadingroutines}
{$I _LEDANZ .PAS} {all routines for the led}
{$I _AUTODRA.PAS} {procedure for automatic-draw // chose the right plane}
{$I _KEYS   .PAS}
{$I _SOUND1 .PAS} {soundkit}
{$I _INI_CLO.PAS} {init_all & close}
{I _INTEGRI.PAS} {check for integrity // read volumelabel of cd-rom}
{$I _CDPLAYR.PAS} {audio-cd-player-routines}
{$I _TISCH1 .PAS}

procedure senk_arms;
  var t : byte;
begin
  for t := 5 downto 0 do begin
       if arm_rechts_status>1 then dec(arm_rechts_status);
       if arm_links_status>1 then dec(arm_links_status);
       Check_Flipper_Arms;
       arm_links_old_status:=arm_links_status;
       arm_rechts_old_status:=arm_rechts_status;
     end;
  draw_ground_auto;
end;

procedure do_fire_stuff;
begin
  if random > 0.60 then begin
      if temp < 6 then begin
          inc(temp);
          display('Temperature is');
          repeat led_anzeige; until led_status = 0;
          delay(1500);
          display(':-) DECREASING (-:');
          repeat led_anzeige; until led_status = 0;
          delay(1500);
        end
    end else
      if temp > 0 then begin
           dec(temp);
           display('Temperature is');
           repeat led_anzeige; until led_status = 0;
           delay(1500);
           display(':-(  RISING  )-:');
           repeat led_anzeige; until led_status = 0;
           delay(1500);
         end;
  thermo(temp);
end;

procedure move_left;
  var alt : byte;
      a : word;
begin
  alt := led_funktion;
  led_anzeige_6_init;
  for a := 0 to 80 do begin led_anzeige; retrace; end;
  led_funktion := alt;
end;

procedure IncScore(points:word);
begin
 score[actplayer] := score[actplayer] + points;
 led_anzeige_5_init(0,0,'Score'+IntToStr1(score[actplayer])+
                        ' Ball '+inttostr(balls[ActPlayer]));
end;

procedure display(t : string);
  var a : byte;
      z : string[20];
begin
 z := '                     ';
 for a := 1 to length(t) do z[a + 10 - length(t) div 2 ] := t[a];
 led_anzeige_5_init(0,0,z);
end;

procedure Check_Ball; forward;

procedure move_ball;
begin
  draw_ground_auto; get_ground_auto; draw_ball_auto;
  bx_old:=ballx; by_old:=bally;
end;

procedure calc_page_pos_of_ballpos;
var y2:word; {longint;}
begin
  {y2:=bally-100;}
  asm mov ax, bally; sub ax, 100; mov y2, ax; end;
{  if y2<1 then y2:=1;} if y2 > 1000 then y2 := 1;
  if y2>421 then y2:=421;
  { y2:=y2+48;}
  asm mov ax,y2; add ax,48; mov y2,ax; end;
  if HighRes then if y2> 270 then y2 := 270;
  {80*y2}
  asm mov ax,y2; mov bx,80; mul bx; mov y2,ax; end;
  setaddress(y2);
end;

procedure Check_Flipper_Arms;

begin
  {check if left flipper-arm is moved}
  if arm_links_old_status<>arm_links_status then
    if (bally+16>armYlinks) and (bally<armYlinks+armHoeheLinks) and
       (ballx+16>armXlinks) and (ballx<armXlinks+armBreitelinks) then begin
        draw_ground_auto;{}
        draw_arm_links;
        get_ground_auto;{}
        draw_ball_auto;
      end else draw_arm_links;
  {check if right flipper-arm is moved}
if (arm_rechts_old_status<>arm_rechts_status) then
    if (bally+16>armYrechts) and (bally<armYrechts+armHoeherechts) and
       (ballx+16>armXrechts) and (ballx<armXrechts+armBreiterechts) then begin
        draw_ground_auto;
        draw_arm_rechts;
        get_ground_auto;
        draw_ball_auto;
      end else draw_arm_rechts;
end;

procedure analyse_arms;
begin
  if (fo > 0) and (ballspeed_y < 0) then begin
      ballspeed_y := abs(ballspeed_y);
      kraft := 0;
      exit;
    end;
  if ballx < 142{152} then begin
    if arm_links_old_status < arm_links_status then begin
        draw_arm_links;
        Check_Ball;
        ballspeed_y := - abs(Ballx+4 - ArmXLinks);
      if ballspeed_y < - 50 then ballspeed_y := -50;
        bally := bally + ballspeed_y;
        kraft := abs(ballspeed_y);
        case arm_links_status of
          1 : inc(ballspeed_x,7+random(4));
          2 : inc(ballspeed_x,5+random(4));
          4 : dec(ballspeed_x,5+random(4));
          5 : dec(ballspeed_x,7+random(4));
        end;
      end else
        case arm_links_status of
          1 : inc(ballspeed_x,abs(ballspeed_y) div 4);
          2 : inc(ballspeed_x,abs(ballspeed_y) div 4);
          4 : dec(ballspeed_x,abs(ballspeed_y) div 4);
          5 : dec(ballspeed_x,abs(ballspeed_y) div 4);
        end;
  end else
    if arm_rechts_old_status < arm_rechts_status then begin
        draw_arm_rechts;
        Check_Ball;
        kraft := 50;
        ballspeed_y := - abs(ArmBreiteRechts - (Ballx+4{+8} - ArmXRechts));
       if ballspeed_y < - 50 then ballspeed_y := -50;
        bally := bally + ballspeed_y;
        kraft := abs(ballspeed_y);
      case arm_rechts_status of
        1 : dec(ballspeed_x,7+random(4));
        2 : dec(ballspeed_x,5+random(4));
        4 : inc(ballspeed_x,5+random(4));
        5 : inc(ballspeed_x,7+random(4));
      end;
    end else
        case arm_rechts_status of
          1 : dec(ballspeed_x,abs(ballspeed_y) div 4);
          2 : dec(ballspeed_x,abs(ballspeed_y) div 4);
          4 : inc(ballspeed_x,abs(ballspeed_y) div 4);
          5 : inc(ballspeed_x,abs(ballspeed_y) div 4);
        end;
  fl := 0; fr := 0; fu := 0; fo := 0;
end;

{ *** EVENTS ***************************************************************}

procedure analyse_crash;
var fg,a,b,c,d:byte;
begin
  if fr>0 then fg:=fr;
  if fl>0 then fg:=fl;
  if fo>0 then fg:=fo;
  if fu>0 then fg:=fu;
  case fg of
    254 : begin
            play(snd2); incscore(100);
            if ballspeed_x <= 0 then ballspeed_x := -10;
            if ballspeed_x > 0 then ballspeed_x := 10;
            dec(ballspeed_y,4); kraft := 10;
          end;
    253 : begin
            if bally < 300 then begin
                b := random(20)+40;
                c := random(10)+50;
                d := random(10)+40;
                for a := 240 to 248 do set_rgb_color(a,b,c,d);
              end;
          retrace;
          play(snd7); incscore(50);
          if ballspeed_x < 0 then ballspeed_x := -8-random(4);
          if ballspeed_x > 0 then ballspeed_x := 8+random(4);
          if ballspeed_y < 0 then ballspeed_y := -8-random(4);
          if ballspeed_y > 0 then ballspeed_y := 8+random(4);
          BumpCount := 7;
           kraft := kraft div 2;
        end;
    250,251,252 : begin
                    PLAY(snd4);
                    Lichter1[fg] := 1;
                    set_rgb_color(fg-250+53,40,20,50);
                    display('Light '+inttostr(fg-249)+' turned on!');
                    if (lichter1[250] = 1) and (lichter1[251]=1) and
                       (lichter1[252] = 1) then begin
                         display('COMPLETE BONUS 9999!');
                         inc(score[actplayer],9999);
                         repeat led_anzeige; until led_status = 0;
                         lichter1[250] := 0;
                         lichter1[251] := 0;
                         lichter1[252] := 0;
                         set_rgb_color(53,pal[53].r,pal[53].g,pal[53].b);
                         set_rgb_color(54,pal[54].r,pal[54].g,pal[54].b);
                         set_rgb_color(55,pal[55].r,pal[55].g,pal[55].b);
                       end;
                  end;
    249,248,247 : begin
                    Lichter2[fg] := 1;
                    Inc(Score[actplayer],100);
                    display('Spot '+inttostr(fg-246)+' touched!');
                  end;
    246,245,244 : begin
                    PLAY(snd5);
                    Lichter3[fg] := 1;
                    inc(score[actplayer],1000);
                  end;
    243 : begin

          play(snd3);  inc(score[actplayer],50);
          if ballspeed_x < 0 then ballspeed_x := -15;
          if ballspeed_x > 0 then ballspeed_x := 15;
       end;
    242 : begin
          display('Yuppieeee...');
          play(snd6);
          inc(score[actplayer],500);
          ballspeed_y := -80-random(100);
          ballx:=8;
          kraft:=240;
          ballspeed_x := 3;
        end;
    241 : begin
           display(')-: ball  lost :-(');
           repeat led_anzeige; until led_status=0;
           senk_arms;
           asm cli end;
           delay(1000);
           move_left;
           display('B O N U S');
           repeat led_anzeige; until led_status=0;
           delay(1000);
           move_left;

           {1000xball}
           Display('Balls 1000 x '+InttoStr(balls[actplayer]));
           repeat led_anzeige; until led_status = 0;
           inc(score[Actplayer],balls[actplayer]*1000);
           delay(500);
           move_left;

           {10000x}
           if kurven > 0 then begin
               for kurven := kurven downto 1 do begin
                  Display('Loop 10000 x '+InttoStr(Kurven));
                  repeat led_anzeige; until led_status = 0;
                  inc(score[Actplayer],kurven*10000);
                  delay(500);
               end;
               move_left;
             end;

           {PCS special}
           if (PCSspe[1] = 1) and (PCSspe[2] = 1) and (PCSspe[3] =1)
               and (special =1 ) then begin
                   display('PCSspecial full!');
                   repeat led_anzeige; until led_status = 0;
                   delay(500);
                   move_left;
                   inc(score[actplayer],100000);
                   display('>>> 100000 <<<');
                   repeat led_anzeige; until led_status = 0;
                   delay(500);
                   move_left;
                end;

           {total}
           display('Total '+inttostr(score[actplayer]));
           repeat led_anzeige; until led_status = 0;
           delay(1000);
           move_left;

           inc(balls[Actplayer]);
           if balls[actplayer] = MaxBalls then begin
                 display('> G A M E  O V E R <');
                 repeat led_anzeige; until led_status = 0;
                 delay(1000);
                 bende := true;
               end;
           inc(actplayer);
           if actplayer > Maxplayer then actplayer := 1;
           if balls[actplayer] < MaxBalls then
             if MaxPlayer > 1 then
              begin
                display('Next Player '+inttostr(actplayer));
                repeat led_anzeige; until led_status=0;
                delay(1000);
                move_left;
                display('Ball '+inttostr(balls[actplayer]));
                repeat led_anzeige; until led_status=0;
                delay(1000);
                move_left;
                bende := false;
              end;
           init_ball_values;
           init_tisch1;
           for a := 0 to 250 do begin
                CTRL_Shift_Keys;
                arm_links_old_status:=arm_links_status;
                arm_rechts_old_status:=arm_rechts_status;
                Check_Flipper_Arms;
              end;
            senk_arms;
          end;
    239 : begin
            senk_arms;
            asm cli end;
            ballx := 23; bally := 411; move_ball;
            inc(score[actplayer],10000);
            ballspeed_y := random(15) + 15;
            ballspeed_x := random(10) + 20;
            display('PREPARE');
            repeat led_anzeige; until led_status = 0;
            delay(500);
            display('FOR');
            repeat led_anzeige; until led_status = 0;
            delay(500);
            display('BATTLE...');
            repeat led_anzeige; until led_status = 0;
            delay(500);
            display('NOW!');
            repeat led_anzeige; until led_status = 0;
            if random > 0.7 then begin
                delay(700);
                display('NO! WAIT...');
                ballspeed_x := 0;
                ballspeed_y := 0;
                repeat led_anzeige; until led_status = 0;
                delay(1200);
                do_fire_stuff;
              end;
          end;
    238 : begin
            ballx := 102; bally := 74; move_ball;
            do_fire_stuff;
            ballspeed_y := - random(15) - 15;
            ballspeed_x := - random(15) - 15;
          end;
    237 : begin
            if ballx < 160 then begin
                if PushUp then begin
                    ballspeed_y := -30-random(15);
                    inc(ballspeed_x,4);
                    kraft := 100;
                  end
               end else if PushUp then begin
                            ballspeed_y := -30-random(15);
                            dec(ballspeed_x,4);
                            kraft := 100;
                          end;
          end;
    236 : begin
            ballspeed_x := ballspeed_x + ballspeed_y div 4;
          end;
  end;
end;

{ *** ARREAS *************************************************************** }

procedure analyse_boden;
var fg:byte;
    h1,h2 :  byte;
begin
  case fm of
       1,2,3 : begin
             Set_RGB_color(fm,100,100,0);
             PCSspe[fm] := 1;
             incscore(75);
             if (PCSspe[1] = 1) and (PCSspe[2] = 1) and (PCSspe[3] =1)
                and (special =1 ) then begin
                   display('*** PCS  special ***');
                   inc(score[actplayer],10000);
                end;
           end;
       4 : begin
             incscore(1000);
           end;
       5 : begin
             senk_arms;
             asm cli end;
             play(snd7);
             display('WORM-HOLE:');
             repeat led_anzeige; until led_status = 0;
             delay(500);
             move_left;
             h2 := random(4);
             for h1 := 8 to random(15)+15 do begin
                 inc(h2);
                 if h2 > 3 then h2 := 0;
                 case h2 of
                   0 : display('ACCESS PERMITTED');
                   1 : display('Icy-Bonus 10000');
                   2 : display('ACCESS DENIED!');
                   3 : display('25000 Bonus');
                 end;
                 repeat led_anzeige; until led_status = 0;
                 play(snd4);
                 delay(h1*7);
               end;
             case h2 of
               0 : begin
                     repeat
                       retrace;
                       calc_page_pos_of_ballpos;
                       dec(bally,2);
                     until bally < 85;
                     ballx:=160;
                     bally:=85;
                     display('Bonus 150000');
                     inc(score[actplayer],150000);
                     repeat led_anzeige; until led_status = 0;
                     delay(500);
                   end;
               1 : inc(score[actplayer],10000);
               3 : inc(score[actplayer],25000);
             end;
             ballspeed_x := -2;
           end;
       6 : begin
             ballspeed_y := ballspeed_y + random(6) - 3;
             ballspeed_x := ballspeed_x + random(6) - 3;
             display('ICE SLIDDERING...!');
           end;
       7 : begin
             ballspeed_y := ballspeed_y + random(6) - 3;
             ballspeed_x := ballspeed_x + random(6) - 3;
             display('WARNING: slippery!');
           end;
       8 : begin
             incscore(300);
             if ballspeed_x > 0 then begin
                inc(Kurven);
                Display('Loops : '+InttoStr(Kurven));
                inc(score[Actplayer],kurven*1000);
                if kurven = 11 then begin
                    dec(balls[actplayer]);
                    display('/ Loop-Bonus Ball \');
                    delay(1500);
                    repeat led_anzeige; until led_status = 0;
                  end;
              end else begin
                Display('Wrong way, dude!');
                repeat led_anzeige; until led_status = 0;
              end;
           end;
       9 : begin
             incscore(800);
           end;
       10 : begin
              ballspeed_y := ballspeed_y + random(6) - 3;
              ballspeed_x := ballspeed_x + random(6) - 3;
              display('ICE SLIDDERING...!');
            end;
       11 : begin
             incscore(75);
             if (PCSspe[1] = 1) and (PCSspe[2] = 1) and (PCSspe[3] =1) then
                begin Set_RGB_color(fm,100,100,0); special := 1;
                   display('*** PCS  special ***');
                   inc(score[actplayer],10000);
                end else
                display('PCS is incomplete!');
           end;
       12 : begin
              if balls[actplayer] < maxballs then display('>Hit SPACE to start<');
            end;
       13 : begin
              incscore(1000);
              play(snd4);
            end;
  end;
end;

function gettablepixel(x,y:word):byte;
begin
  if y<200 then gettablepixel:=tableground1^[x,y]
    else if y<400 then gettablepixel:=tableground2^[x,y]
        else if y < 600 then gettablepixel:=tableground3^[x,y];
end;

procedure check_ball_oben;
var x,y,z:integer;
    contact:boolean;
begin
  y:=bally;
  contact:=false;
  repeat
    {0&16 / 1&15 / 2&14 / 3&13 / 4&12 / 5&11 / 6&10 / 7&9 / 8&8}
    for x:=ballx+ 4 to ballx+12 do begin
        if gettablepixel(x,y)>127 then begin
           contact:=true;
           if x <= ballx+8 then inc(o1) else inc(o2);
           fo := gettablepixeL(x,y);
         end;
      end;
    dec(y);
  until (y<=bally+ballspeed_y div 2) or (contact);
  inc(y); bally:=y;
end;

procedure check_ball_unten;
var x,y,z:integer;
    contact:boolean;
begin
  y:=bally;
  contact:=false;
  repeat
    for x:=ballx+ 4 to ballx+12 do
    begin
      if gettablepixel(x,y+14)>127 then begin
          contact:=true;
          if x <= ballx+8 then inc(u1) else inc(u2);
          fu := gettablepixeL(x,y+14);
        end;
    end;
    inc(y);
  until (y>=bally+ballspeed_y div 2) or (contact);
  dec(y); bally:=y;
end;

procedure check_ball_links;
var x,y,z:integer;
    contact:boolean;
begin
  x:=ballx;
  contact:=false;
  repeat
    for y:=bally+ 4 to bally+12 do
    begin
      if gettablepixel(x,y)>127 then begin
          contact:=true;
          if y <= bally+8 then inc(l1) else inc(l2);
          fl :=  gettablepixeL(x,y);
        end;
    end;
    dec(x);
    if (x<0) then begin x:=0; contact:=true; end;
  until (x<=ballx+ballspeed_x div 2) or (contact);
  inc(x); ballx:=x;
end;

procedure check_ball_rechts;
var x,y,z:integer;
    contact:boolean;
begin
  x:=ballx;
  contact:=false;
  repeat
    for y:=bally+ 4 to bally+12 do
    begin
      if gettablepixel(x+14,y)>127 then begin
          contact:=true;
          if y <= bally+8 then inc(r1) else inc(r2);
          fr :=  gettablepixeL(x,y+14);
        end;
    end;
    inc(x);
    if (x>304) then begin x:=304; contact:=true; end;

  until (x>=ballx+ballspeed_x div 2) or (contact);
  dec(x); ballx:=x;
end;

procedure Check_Ball;
begin
  o1 := 0; o2 := 0; u1 := 0; u2 := 0;
  l1 := 0; l2 := 0; r1 := 0; r2 := 0;

  fu := 0; fo := 0; fl := 0; fr := 0;
  if ballspeed_y < 0 then begin
      check_ball_oben;
      if (o1 + o2 > 0) then begin
          dec(kraft);
          if kraft < 0 then
           ballspeed_y := -(ballspeed_y{+abs(ballspeed_y)div 2) div 4}div 2);
        end;
      if (o1 > 0) and (o2 = 0) then if ballspeed_x < 4 then inc(ballspeed_x);
      if (o1 = 0) and (o2 > 0) then if ballspeed_x > -4 then dec(ballspeed_x);
    end;
  if ballspeed_y >= 0 then begin
      check_ball_unten;
      if (u1 + u2 > 0) then begin
          ballspeed_y := -(ballspeed_y{+abs(ballspeed_y)div 2) div 4}div 2);
          kraft := abs(ballspeed_y div 2);
        end;                                         {4}
      if (u1 > 0) and (u2 = 0) then if ballspeed_x < 4 then
          begin if random > 0.3 then inc(ballspeed_x) else inc(ballx); end;
      if (u1 = 0) and (u2 > 0) then if ballspeed_x > -4 then
          begin if random > 0.3 then dec(ballspeed_x) else dec(ballx); end;
    end;
  if ballspeed_x <= 0 then begin
      check_ball_links;
      if (l1 + l2 > 0) then begin
          ballspeed_x := abs((ballspeed_x+abs(ballspeed_y)div 3) div 4){+1};
          {if ballspeed_x < 4 then inc(ballspeed_x,2);}
        end;
      if (l1 > 0) and (l2 = 0) then inc(ballspeed_y);
      if (l1 = 0) and (l2 > 0) then dec(ballspeed_y);
    end;
  if ballspeed_x >= 0 then begin
      check_ball_rechts;
      if (r1 + r2 > 0) then begin
          ballspeed_x := -((ballspeed_x+abs(ballspeed_y)div 2) div 4){-1};
          {if ballspeed_x > -4 then dec(ballspeed_x,2);}
        end;
      if (r1 > 0) and (r2 = 0) then inc(ballspeed_y);
      if (r1 = 0) and (r2 > 0) then dec(ballspeed_y);
    end;


  if (l2 > 0) and (l1 = 0) then
      if (u1 > 0) or (u2=0) then if ballspeed_x <= 0 then begin
              inc(ballspeed_y); inc(ballspeed_x); end;
  if (r2 > 0) and (r1 = 0) then
      if (u1 = 0) and (u2>0) then if ballspeed_x >= 0 then
          begin inc(ballspeed_y); dec(ballspeed_x); end;

  if (l1 > 0) and (l2 = 0) then
      if (o1 > 0) or (o2=0) then if ballspeed_x >= 0 then begin
              {inc(bally);} dec(ballx); end;
  if (r1 > 0) and (r2 = 0) then
      if (o1 = 0) and (o2>0) then if ballspeed_x <= 0 then begin
              {inc(bally);} inc(ballx); end;

{  if kraft < 0 then } inc(ballspeed_y);
  if ballspeed_y > speedmaxy then ballspeed_y := speedmaxy;

end;

  var a,b,c,d : byte;

begin
  asm cli end;
  checkbreak := false;
  if (paramcount <> 1) or (length(paramstr(1)) <> 3) then halt(0);
  {search for right cd-rom // canceld here!}
  {CheckCDROM;}
  {detect soundblaster and initialize the values}
  textcolor(black);
  textbackground(black);
  detect_soundblaster;
  {initialise}
  Init_All;
  Init_Tisch1;

  FederY:=400+205+startpow div 5;
  FederHoehe:=40-startpow div 5;
  Set_Feder;
  move_ball;

  {main-loop}
  repeat
    {get key}
    keyboard; ch := upcase(CH);
    case ch of
      'K' : begin
              ballspeed_y := ballspeed_y - 10;
              ballspeed_x := ballspeed_x - 6 + random(12);
              inc(ruetteln);
              display('DER '+inttostr(ruetteln)+'.RšTTLER!');
              if ruetteln = 5 then begin
                  bende := true;
                  display('T I L T !');
                end;
              repeat led_anzeige; until led_status = 0;
              delay(200);
            end;
      'P' : StartCDPlayer;
      'Q',#27 :
          if NormalPos =  CurrentPos then begin
              Display('Really quit ?');
              repeat led_anzeige; until led_status = 0;
              repeat keyboard; ch := upcase(ch);
              until (ch = 'Y') or (ch = 'N') or (ch = 'Z');
              case ch of
                'N' : begin
                    bende := false;
                    Display('No!');
                    repeat led_anzeige; until led_status = 0;
                  end;
                'Y','Z' : begin
                    bende := true;
                    Display('Yes!');
                    repeat led_anzeige; until led_status = 0;
                  end;
              end;
          end;
      ' ' : if (fm = 12) and (normalpos = currentpos) and (balls[actplayer] < MaxBalls)then begin
                display('Release to start!');
                repeat led_anzeige; until led_status = 0;
                StartPow := 0;
                repeat
                  if startpow < 75 then begin
                      inc(startpow,2);
                      FederY:=400+205+startpow div 5;
                      FederHoehe:=40-startpow div 5;
                      retrace;
                      Set_Feder;
                      move_ball;
                    end else play(snd1);
                  if keypressed then readkey;
                  Check_Flipper_Arms;
                until port[$60] <> 57;

                repeat
                  dec(FederY,2);
                  if FederHoehe<40 then inc(FederHoehe,2)
                                   else federhoehe := 40;
                  Set_Feder;
                  move_ball;
                  retrace;
                until FederY<=400+205;
                Ballspeed_y := -StartPow; kraft := 200;
                display(#24+' GO FOR THE ICE '+#24);
                repeat led_anzeige; until led_status = 0;
              end;
    end;
    {get extended key}
    CTRL_Shift_Keys;
    {arms}
    Check_Flipper_Arms;
    {calc_new_ball_pos // check border etc. // main proc}
    Check_Ball;
    {if (fr=arm) or (fl=arm) or (fu=arm) or (fo=arm) or
       (fm=arm) or (fh=arm) then analyse_arms;}
    asm
      mov al,arm;  cmp al,fr; jz @analyse;
      mov al,arm;  cmp al,fl; jz @analyse;
      mov al,arm;  cmp al,fu; jz @analyse;
      mov al,arm;  cmp al,fo; jz @analyse;
      mov al,arm;  cmp al,fm; jz @analyse;
      mov al,arm;  cmp al,fh; jz @analyse;
      jmp @ende
    @analyse: call analyse_arms
    @ende:
    end;
    arm_links_old_status:=arm_links_status;
    arm_rechts_old_status:=arm_rechts_status;
    {final check routine}
    if CurrentPos > NormalPos then begin
        if (ballx = bx_old) and (bally = by_old) then retrace;
        dec(CurrentPos); SetLineComp(CurrentPos);
      end;
    if (ballx <> bx_old) or (bally <> by_old) then begin
         retrace;
        {set ball}
        calc_page_pos_of_ballpos;
        move_ball;
        {}
        if (fr>0) or (fl>0) or (fu>0) or (fo>0) then analyse_crash;
        if bende then break;
        fh := gettablepixel(ballx+8,bally+8);
        if fh <> fm then begin
            if (fh>0) and (fh<128) then begin fm := fh; analyse_boden; end
                      else if fh = 0 then fm := 0;
          end;
      end;
     asm cli end;

    led_anzeige;

    case BumpCount of
      0 : begin end;
      1 : begin
            for a := 240 to 248 do
                set_rgb_color(a,pal[a].r,pal[a].g,pal[a].b);
            BumpCount := 0;
          end;
      else  dec(BumpCount)
    end;
    {fireflacker}
                 if (PushUp = true) then flameflacker;
  until bende = true;
  {}
  for b := 0 to 63 do
    for a := 0 to 255 do begin
        if pal[a].r > 0 then dec(pal[a].r);
        if pal[a].g > 0 then dec(pal[a].g);
        if pal[a].b > 0 then dec(pal[a].b);
        set_rgb_color(a,pal[a].r,pal[a].g,pal[a].b);
      end;
  Close_All;
  asm sti end;
  halt(1);
end.