( bridge scene 9/13/83 )
forget it
: it ;

4 mode
17 0 3 color
10 vtab 10 htab
print " One moment, please ... "

0 variable noise
17 variable bc
0 variable pc
5000 string tune
3000 string bridge.image
200 string car
400 string plane
24 string cars
24 string planes
4 variable plane.number ( half )
10 variable car.number ( half )
100 variable car.speed
200 variable plane.speed
20 variable xr
-60 variable yr
250 variable sc
8 variable sz
0 variable key.pressed

: clrkey begin getkey 0 = until ;

: check.key
  getkey if
  1 -> key.pressed then ;

: draw.it
  check.key
  key.pressed 0 = if
  draw then ;

: root.load
  memory pad -> read ;

: wait
  0 do
    0 begin
    1 + dup 1075 >
    check.key
    key.pressed
    or until drop
  loop ; 

: load.objects
  pad assign " bridge.img "
  0 bridge.image 2800 root.load
  pad assign " car.img "
  0 car 100 root.load 
  pad assign " tinyplane.img "
  0 plane 300 root.load ;

: locate.end 0 tune begin
  dup peek 0 <> while
  3 + repeat ;

: set.repeat
  0 tune ( address of beginning )
  locate.end 1 + ( address of end )
  pokew ; ( put address of beginning at end of music )

: load.music
  pad assign " prelude.mus "
  0 tune 3000 root.load 
  set.repeat ;

: set.time
  0 -> dx
  11520 -> ax
  33 int ;

: get.time
  11264 -> ax
  33 int dx ;

: setup.bridge
  15 object
  sc scale
  sz scalz
  0 bridge.image objadr
  xr xrot
  yr yrot ;

: setup.cars
  car.number 2 / 0 do
  i object
  0 car objadr
  xr xrot
  yr yrot
  -20 ytran
  -100 i 2 mod if chs then ztran
  sc scale
  sz scalz
    -4800 i 2 * cars pokew
    4 objcolor
  loop ;

: setup.planes
  plane.number 2 / 0 do
  i 8 + object
  0 plane objadr
  xr xrot
  yr yrot
  -1000 ytran
  sz scalz
  sc scale
  300 i 2 mod if chs then xtran
    -4800 i 2 * planes pokew
    4 objcolor
  loop ;

: random.plane
  rnd abs
  1000 <
  if
    0 begin
    dup plane.number <
    over planes peekw
    4600 < and
    while 2 + repeat
    plane.number =
    if ( no collisions )
      0 begin
      dup plane.number <
      over planes peekw
      -4799 > and
      while 2 + repeat
      dup plane.number <
      if ( available slot )
          dup planes 4800 swap pokew
          2 / 8 + object
          rnd abs 3 mod 1 + objcolor
      else drop
      then
    then
  then ;

: fly.planes
  random.plane
  plane.number 0 do
  i 2 / 8 + object
  i planes peekw
  dup -4800 >
  if
    plane.speed -
    dup i planes pokew
    dup -4800 >
    if
     i 4 mod if chs then ztran
    else
     drop
     4 objcolor
    then
  else drop
  then 2 +loop ;

: random.car
  rnd abs 5000 <
  if
    0 begin
    dup car.number <
    over cars peekw
    4600 < and
    while 2 + repeat
    car.number =
    if ( no collisions )
      0 begin
      dup car.number <
      over cars peekw
      -4799 > and while
      2 + repeat
      dup car.number <
      if ( available slot )
          dup cars 4800
          swap pokew
          2 / object
          rnd abs 3 mod 1 + objcolor
      else drop
      then
    then
  then ;

: drive.cars
  random.car
  car.number 0 do
  i 2 / object
  i cars peekw
  dup -4800 >
  if
    car.speed -
    dup i cars pokew
    dup -4800 >
    if
      i 4 mod if chs then xtran
    else
      drop
      4 objcolor
    then
  else drop
  then 2 +loop ;

: bridge
  pad 126 + peek 78 =
  if 0 else 1 then -> noise
  clrkey
  0 -> key.pressed
  0 autodraw
  ormode
  objerase
  -60 -> yr
  4 mode
  bc pc 3 color cls
  8 38 8 24 window
  3 gcolor
  56 56 plot
  296 128 box
  print " The Bridge " cr cr
  bc pc 2 color
  print " Music: Prelude No. 3 " cr
  print " Well-Tempered Clavier Book 2 " cr
  print " J.S. Bach " cr cr
  bc pc 1 color
  print " Images: P. Lutus "
  load.objects
  load.music
  0 39 0 24 window
  10 wait
  ormode 1 sequence
  setup.cars
  setup.planes
  setup.bridge
  key.pressed 0 = if
  noise if 0 tune -> play then
  then
  yr 360 - begin
  dup yrot draw.it
  15 + dup yr >
  check.key
  key.pressed
  or until drop
  0 begin
  objerase
  setup.cars
  setup.planes
  setup.bridge
  ormode 1 sequence
  draw.it
  exmode 0 sequence
  0 begin
  set.time
  drive.cars
  fly.planes
  draw.it
  begin get.time ( equalize times )
  12 > until ( 120 ms minimum )
  1 +
  dup 300 >
  check.key
  key.pressed
  or until
  drop
  yr 15 + -> yr
  1 + dup 3 >=
  key.pressed or
  until
  drop 
  pad 127 + peek 68 =
    if
      key.pressed
        if read " menu "
        else read " happy "
        then 
    else 4 mode 
    then 0 -> play ;
  close run
