    DEVICE ZXSPECTRUM48
  ORG $5B00

; The stack
;
; The game's stack, in the printer buffer. The start (start) sets SP to
; loader_entry, so it grows down from there; the game runs with interrupts off,
; so nothing else ever lands on it. All zero in the snapshot.
stack_space:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00

; The top of the stack
;
; The landscape renderer (after_step_drawn) does not save and restore SP around
; its POP blitter: it loads SP with this address after each piece, which must
; be where the stack stands inside draw_landscape when that is called from the
; play loop -- three calls deep, so that these six bytes are three return
; addresses and the renderer's own RET still finds its caller's (inferred from
; the reset; the chain of calls was not traced here).
stack_top:
  DEFB $00,$00,$00,$00,$00,$00

; Move the game block down and undo its nibble shift
;
; The tape's BASIC calls this once the game block has loaded, 512 bytes above
; where it runs. It ran during loading, which the build does not see, so it is
; described from reading alone. loader_copy, the copy routine the earlier PIC
; block left at the top of memory, moves the block down 512 bytes to
; game_entry; then an RRD walk down from the block's top moves every byte's
; nibbles along by one -- the tape holds the game four bits out of step -- and
; the game starts at game_entry.
;
; After the game starts these 25 bytes are dead: the view's page's
; collision-box list (see ground_cell_top_left_rest) is written over them, and
; the stack grows down from just below them.
loader_entry:
  LD SP,loader_leftover_charset ; Stack under the copy routine's home;
  DI                            ; interrupts off for good
  CALL loader_copy        ; Move the block down 512 bytes to game_entry
                          ; (loader_copy)
  LD HL,shift_tables      ; From the top of the block down, 158 times 256
  XOR A                   ; bytes; A starts at 0, so the top byte's high nibble
  LD BC,$9E00             ; becomes a zero
loader_entry_0:
  RRD                     ; RRD: (HL)'s low nibble to A, A's old low nibble to
                          ; (HL)'s high nibble, (HL)'s high nibble to its low
                          ; nibble
  DEC HL                  ; Down a byte, for every byte of the block: the whole
  DEC C                   ; block and the system variables under it, all but
  JR NZ,loader_entry_0    ; their first byte (hence the nonsense in them)
  DJNZ loader_entry_0     ;
  JP game_entry           ; Start the game

; The rest of the view's page's collision-box list
;
; The landscape renderer (draw_landscape) draws the four map cells (pages) the
; view straddles in turn -- the view's own page at the map position
; (ground_cell = 0), the one below it (1), the one to its right (2) and the one
; below and right (3) -- and keeps a list of collision boxes, 37 bytes, for
; each: loader_entry (written over the loader), ground_cell_bottom_left,
; ground_cell_top_right and ground_cell_bottom_right. find_page_boxes finds the
; list for a map column and row by comparing them with the view's own.
;
; The list: +0 the number of pieces the page's tile has (zero for a page with
; none, and for all four while the map position's row is 0, 1 or 2, when
; scroll_landscape clears them and does not call draw_landscape at all); then,
; only while record_ground is set, five bytes for each piece as it was drawn,
; written through the pointer at collision_object by draw_landscape: +0 its x,
; +1 its y, +2 and +3 its extent (worked out from the piece's direction and its
; number of steps), +4 a byte looked up by the direction. These are the
; ground's collision boxes, read by leave_slope and place_collision_box
; (through find_page_boxes and move_across). The meaning of +2 to +4 in detail
; is for those routines.
ground_cell_top_left_rest:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; Bytes 25-36 of the
                                                       ; list (its first 25 are
                                                       ; at loader_entry)

; The collision-box list of the page below the view's
;
; See ground_cell_top_left_rest for the layout. The page at the map position's
; column and the row under it (ground_cell = 1).
ground_cell_bottom_left:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00

; The collision-box list of the page right of the view's
;
; See ground_cell_top_left_rest for the layout. The page at the next column
; (wrapping from 95 to 0) and the map position's row (ground_cell = 2).
ground_cell_top_right:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00

; The collision-box list of the page below and right of the view's
;
; See ground_cell_top_left_rest for the layout. The page at the next column and
; the next row (ground_cell = 3). The byte after it is not used (searched).
ground_cell_bottom_right:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$FF,$00,$00,$F0,$0F,$30,$D2,$D0,$30,$52,$00,$00,$00,$60,$31
  DEFB $11,$00,$60,$00,$B0
  DEFB $00                ; Not used (searched)

; A saved step across
;
; In the system variables' area, which the game has no use for, like all the
; variables from here to fuel_saver. Many of them are scratch: one routine's
; loop counter is another's flag, and routines in unrelated parts of the game
; share a byte. Each is named by its main use and its other users are listed.
; The snapshot's values are the BASIC system variables put through the loader's
; nibble shift, and mean nothing.
;
; This word is saved by bounce_shallow_negate from a moving thing's record (+7
; and +8, put through three_eighths) and added back further on in the same
; routine.
saved_step_x:
  DEFB $10,$00

; A saved step down
;
; The same as saved_step_x for the record's +9 and +10: saved and added back by
; bounce_shallow_negate. The byte after it is not used (searched).
saved_step_y:
  DEFB $10,$00
  DEFB $60                ; Not used

; How far a touch went in
;
; Written by test_landscape_piece and test_slope_reversed, which test a part of
; an object against a slope's shape: how far into the shape the part is, made
; positive. leave_slope reads it, negated, as a new step to push the object
; back out. after_step_drawn also uses it as a flag between its own
; instructions. The byte after it is not used (searched).
touch_depth:
  DEFB $00
  DEFB $00                ; Not used

; The busiest scratch byte
;
; Used by more than forty routines as a counter or a holding place. Among them:
; the slope tests (test_landscape_piece, test_slope_reversed) leave 1 in it for
; a touch, and load_map_window keeps in it the object kind it gives the pick-up
; places it puts into object slots ($0C for the first eleven, $1E for the last
; eight).
scratch_count:
  DEFB $01

; Which map cell is being drawn
;
; Which of the four map cells draw_landscape is drawing, 0-3 (see
; ground_cell_top_left_rest): bit 0 set for the lower row, bit 1 for the
; right-hand column. Elsewhere a scratch byte (ground_object_fires,
; start_attacker, next_part, place_collision_box, draw_carried_piece and
; others).
ground_cell:
  DEFB $00

; A row count
;
; Scratch, mostly a count of rows or lines: the shift-table builders
; (read_piece_header, next_shift, shift_slope_piece) count rows in it,
; reloading it from row_count_spare; start_attacker, object_box and
; test_player_shots keep a line in it; draw_beams reads it.
row_count:
  DEFB $00

; The row count's spare
;
; The copy row_count is reloaded from (read_piece_header, next_shift,
; shift_slope_piece); also scratch for start_attacker and toggle_carried_part.
row_count_spare:
  DEFB $00

; Pieces left in the tile
;
; The number of pieces left to draw in the map cell's tile: draw_landscape sets
; it from the tile's record and after_step_drawn counts it down. Also scratch
; for toggle_carried_part, next_part_boxes, hit_slope, downhill and
; shift_flat_piece. The byte after it is not used (searched).
pieces_left:
  DEFB $00
  DEFB $00                ; Not used

; Steps in the piece
;
; The number of steps in the piece being drawn: the low nibble of its third
; byte, 16 when that is 0 (draw_landscape); read again by flat_step_clip. The
; byte after it is not used (searched).
piece_steps:
  DEFB $00
  DEFB $00                ; Not used

; The piece's step down
;
; The piece's step in y, from its direction (load_piece_steps, given the sign
; of the step in x); used by after_step_drawn and fill_behind_moving_piece.
; Also written by landscape_end_pieces, and by colour_piece as a width less
; one.
piece_step_y:
  DEFB $00

; The piece's step across
;
; The size of the piece's step in x (load_piece_steps), added to its x by
; piece_ends and draw_landscape. Also a mode byte of the collision routines
; (collide_with_landscape, next_box, hit_block, hit_slope, leave_slope,
; ship_on_slope_response, place_collision_box), written by move_bouncing,
; move_or_explode, update_shot, ship_pass and build_shifted_pieces and read by
; shift_slope_piece.
piece_step_x:
  DEFB $00

; The tile is upside down
;
; $99 while the tile being drawn is turned upside down (its record starts $99),
; 0 otherwise (draw_landscape). load_piece_steps then swaps each piece's
; direction between floor and ceiling through the second half of the table at
; mirror_table, and piece_ends turns its y over (XOR $0C) and its extra bytes
; (XOR 1). Floors become ceilings: the caverns.
tile_upside_down:
  DEFB $00

; A step kept for the column fill
;
; Cleared for each piece by draw_landscape; set by flat_step_clip when it keeps
; a step's address and x for the fill behind a piece; read by
; fill_flat_end_column, which then uses the kept step.
piece_kept:
  DEFB $00

; A width in bytes
;
; Scratch, as a byte and as a word with sprite_width_drawn. The sprite routines
; keep a width less one in bytes here (draw_sprite_pair copies it and
; sprite_width_drawn before turning them into pixels); also used by
; set_old_sprite, set_direct_old, clear_sprite_pair, draw_direct_pair,
; direct_erase_row, collect_pickup, mirror_ship_sprite, carried_part,
; draw_carried_piece and draw_ship.
sprite_width:
  DEFB $00

; The width a sprite was drawn with
;
; The width a sprite was drawn with, kept to rub it out (set_new_sprite,
; set_direct_new, draw_direct_pair, direct_draw_row, colour_sprite); the high
; byte of sprite_width as a word. Also read by ship_contact, object_box,
; set_mirrored_drawer and draw_carried_piece.
sprite_width_drawn:
  DEFB $00

; How far a box sticks out to the right
;
; Kept by place_collision_box, the collision box's placement, for the part that
; sticks out past the right of the page into the next (read by part_right and
; next_part).
stick_out_right:
  DEFB $00

; How far a box sticks out above
;
; The same for what sticks out above the page's top (place_collision_box, read
; by part_above and next_part).
stick_out_up:
  DEFB $00

; The x of the piece's step
;
; Moved on by the step in x at each step of a piece (landscape_step_loop); also
; used by draw_sprite_pair, dissolve_sprite, buffer_address, part_above,
; part_right, place_collision_box, slope_step_at_left_edge, flat_step_clip and
; step_screen_address.
piece_x:
  DEFB $00

; The y of the piece's step
;
; Moved by the step in y at each step of a piece (landscape_step_loop); also
; used by buffer_address, part_above, part_right, next_part,
; place_collision_box, flat_step_clip, draw_flat_end_lip and
; step_screen_address.
piece_y:
  DEFB $00

; A move across
;
; With piece_steps_done, a word: the move across that place_collision_box keeps
; (beside moved_x) and part_right and next_part read. slope_step_at_left_edge
; writes this byte as one of its own, a count of bytes to skip.
move_across_kept:
  DEFB $00

; Steps of the piece drawn
;
; How many steps of the piece have been drawn: cleared for each piece by
; draw_landscape and counted up by after_step_drawn, which draws nothing more
; for a piece with none; also read by edge_column, draw_flat_end_lip and
; step_screen_address. Its other use is as the high byte of move_across_kept.
piece_steps_done:
  DEFB $00

; The tile is mirrored left-right
;
; $FF while the tile being drawn is mirrored left-right (its record has an $FF
; before its piece count), 0 otherwise (draw_landscape). piece_ends then
; reflects each piece's x, swaps its two extra bytes (XOR 2) and turns its
; direction round through the first half of the table at mirror_table. Also the
; low byte of a move down place_collision_box keeps for part_above and
; next_part.
tile_mirrored_left_right:
  DEFB $00

; The view's edge flags
;
; Which of the view's edges a step or an object has crossed: cleared at each
; step by landscape_step_loop, set and tested by the clipping routines
; column_at_view_bottom, column_at_view_top, column_at_view_left and
; column_at_view_right; also written by prepare_draw and read by steer_to_view.
edge_flags:
  DEFB $00

; The piece reverses
;
; Set by column_at_view_bottom and column_at_view_top when a piece's steps
; reverse at an edge, cleared for each piece by draw_landscape, read by
; fill_behind_moving_piece.
piece_clip:
  DEFB $00

; A flat piece's first extra byte
;
; The first of the two extra bytes a piece of direction 0 or 7 carries
; (piece_ends; with the tile mirrored left-right it is the second, XOR 2;
; upside down, XOR 1). Read by flat_step_clip and fill_flat_end_column, and by
; landscape_end_pieces and first_piece_shift as a word with piece_extra_b.
piece_extra_a:
  DEFB $00

; A flat piece's second extra byte
;
; The second; a $16 is left as it is by both reversals. Read by colour_piece,
; flat_step_clip, draw_flat_end_lip, fill_flat_end_column,
; fill_behind_moving_piece and last_piece_width.
piece_extra_b:
  DEFB $00

; The piece is a ceiling
;
; $80 when the piece's direction is 7 or more, the ceiling shapes
; (set_piece_shape); cleared for each piece by draw_landscape; read by
; after_step_drawn and fill_behind_moving_piece. The collision routines
; (next_box, hit_block, hit_slope, place_collision_box) also keep in it what
; has happened this pass, bit 7 a block hit and bit 4 a slope.
piece_ceiling:
  DEFB $00

; Which way the ship was pushed
;
; Cleared by move_ship every pass and set there from the controls: $80 pushed
; left, 3 right, 0 not. draw_ship turns the ship's facing towards it;
; accelerate_across reads it too.
steer_push:
  DEFB $C0

; This pass's controls
;
; In the Kempston layout (bit 0 right, 1 left, 2 down, 3 up, 4 fire), as
; read_controls leaves them; move_ship forces 8 (thrust) while the stage is
; ending. Read by fire, special_beam_key, end_special_beam, draw_ship,
; lift_jet, and by random, which stirs it into the random number.
controls_now:
  DEFB $03

; The ship's line
;
; The ship's line on the screen, the line its shots fly along: set by
; build_ship_sprite from the ship's record (+$1E, less the ship's height, plus
; 9) and by toggle_carried_part; read by ship_contact, draw_beams, update_shot,
; fire, place_shot, carried_part, draw_carried_piece, draw_ship, lift_jet and
; start_landscape.
ship_line:
  DEFB $04

; The gun's cooling count
;
; Set to 6 by fire when it fires a shot and counted down there every pass; it
; fires again only once the count is 0. draw_ship clears it.
gun_cooling:
  DEFB $F0

; A colour for the view's page row
;
; Written by set_landscape_colours from one of two bytes kept with the view's
; four tile numbers (see load_map_window), chosen by whether the map row is
; above 12 or not; read by colour_piece.
map_row_colour:
  DEFB $DF

; A colour for the page row below
;
; The same as map_row_colour for the row below; read by colour_piece.
map_row_colour_next:
  DEFB $0C

; The view is being built
;
; Nonzero while the view is being built afresh rather than played: $FF from
; start_landscape at the start of a life, 3 from fly_in_step, 0 when play goes
; on. While it is set draw_beams returns at once and load_map_window puts no
; objects into slots; with bit 7 set, load_map_window draws map tile $3E as
; tile $AA (two of its six pieces). draw_landscape copies a nonzero value to
; record_ground.
map_rebuild:
  DEFB $D0

; Record the collision boxes
;
; Set from map_rebuild by draw_landscape and by scroll_landscape: while it is
; nonzero, draw_landscape writes a collision box for each piece it draws into
; the map cell's list (ground_cell_top_left_rest), so the ground's boxes are
; made when the view is built rather than at every pass.
record_ground:
  DEFB $DF

; The ship's top speed across
;
; 2, or 4 when +$0A of the current player's record (score) (an add-on) is set
; (build_ship_sprite); read by draw_ship, accelerate_across and
; clamp_speed_across, which hold the speed to it.
top_speed:
  DEFB $05

; The ship's top climb
;
; 2, or 4 when +$0B of the record (an add-on) is set (build_ship_sprite); set
; to 4 by move_ship while the stage is ending, and read there and by
; clamp_climb.
top_climb:
  DEFB $00

; Halve the fuel burn
;
; Set to $FF by ship_contact, and every pass copied away (to last_pass_contact)
; and cleared by update_objects; read by move_ship and ship_on_slope_response.
; While it is set the fuel burn at the end of read_controls is halved (again,
; after any halving for +$0C of the current player's record).
fuel_saver:
  DEFB $00

; Unused: what is left of the system variables
;
; Nothing refers to these bytes (searched). They are the BASIC system variables
; the loader found here, put through its nibble shift.
basic_leftovers:
  DEFB $10,$00,$F0,$1F,$00,$C0,$70,$00,$00,$80,$D5,$05,$00,$60,$CB,$B5
  DEFB $CB,$B5,$CC,$85,$D5,$A5,$CC,$95,$D5,$C5,$D5,$75,$D5,$35,$DB,$E5
  DEFB $D5,$E5,$D5,$E5,$D5,$D5,$22,$C9,$05,$20,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$D0,$A9,$01,$00,$B0,$00,$00,$80,$F5,$0F,$00,$10,$02

; Game entry
;
; Used by the routine at loader_entry.
;
; Where the loader (loader_entry) jumps once the game is in place: interrupts
; off -- the game never turns them on -- and on to the start.
game_entry:
  DI                      ; Interrupts off
  JP start                ; Start the game (start)

; Bit-doubling table
;
; Sixteen bytes, one for each value of a nibble: each of the nibble's four bits
; doubled into a pair, so that bit n of the index sets bits 2n and 2n+1.
; print_char_double prints characters twice as wide this way (and twice as
; tall, by writing each row twice), looking up each half of a font byte: it
; adds the nibble to $84 with B holding $5C, so the table has to sit within one
; page.
double_bits:
  DEFB $00,$03,$0C,$0F,$30,$33,$3C,$3F
  DEFB $C0,$C3,$CC,$CF,$F0,$F3,$FC,$FF

; The things lying in the world: their kinds
;
; The world's 290 objects -- the things lying in the landscape at fixed places,
; as opposed to the attackers -- are kept in three parallel tables: this one,
; one byte each; map_object_places, two bytes each (where); and
; map_objects_per_column, how many there are in each map column. They are in
; column order, so the objects of column C start at the sum of the counts of
; the columns before it (spawn_column_objects).
;
; Each byte here: bits 0-5 the object's kind, 0-16, which
; spawn_objects_from_list copies into the object slot's +$13 and looks up in
; the table at object_kind_flags (bits 5-7 of that entry go into the slot's
; +$18, and with bit 4 set the slot's graphic is $1B instead of $0D, an
; animated object whose frame byte and frame range come from
; object_part_pairs); bit 7 set once player 1 has taken or destroyed the
; object, bit 6 once player 2 has (ship_hits_ground_objects and
; beam_hits_ground_object find the byte again from the slot's +$18 bit 0 and
; +$19, which hold its number), after which it never comes back for that
; player: play patches the BIT 7 in spawn_objects_from_list and the SET 7s in
; ship_hits_ground_objects and beam_hits_ground_object to bit 6 while player 2
; plays. mask_world_objects ANDs every byte with a mask: $3F at a new game
; (new_game), so bits 6 and 7 are cleared and everything comes back; at the end
; of a planet (planet_secured) a mask from the player in play (player_in_play),
; $7F for player 1 and $BF for player 2, so that only that player's objects
; come back.
;
; Read by spawn_column_objects and spawn_objects_from_list each time the screen
; moves to a new map column. Laid out below a line per map column, the kinds of
; that column's objects, from the game's bytes when the disassembly is built.
map_object_kinds:
  DEFB $06,$08            ; Column 0: kinds 6, 8
  DEFB $02,$0D            ; Column 1: kinds 2, 13
  DEFB $0F,$07,$03,$0E,$10,$0A ; Column 2: kinds 15, 7, 3, 14, 16, 10
  DEFB $02,$05            ; Column 3: kinds 2, 5
  DEFB $01,$04            ; Column 4: kinds 1, 4
  DEFB $06,$00,$10        ; Column 5: kinds 6, 0, 16
  DEFB $02,$05            ; Column 6: kinds 2, 5
  DEFB $0B                ; Column 7: kind 11
  DEFB $07,$04,$0E        ; Column 8: kinds 7, 4, 14
  DEFB $0F,$04            ; Column 9: kinds 15, 4
  DEFB $0B,$06,$05,$0D    ; Column 10: kinds 11, 6, 5, 13
  DEFB $10,$10            ; Column 11: kinds 16, 16
  DEFB $0F,$03,$05,$10,$08 ; Column 12: kinds 15, 3, 5, 16, 8
  DEFB $01,$05            ; Column 13: kinds 1, 5
  DEFB $07,$02            ; Column 14: kinds 7, 2
  DEFB $06,$0A            ; Column 15: kinds 6, 10
  DEFB $0F,$01            ; Column 16: kinds 15, 1
  DEFB $02,$0A,$0E,$04    ; Column 17: kinds 2, 10, 14, 4
  DEFB $04,$0E,$0C,$0E    ; Column 18: kinds 4, 14, 12, 14
  DEFB $07,$04,$01        ; Column 19: kinds 7, 4, 1
  DEFB $08                ; Column 20: kind 8
  DEFB $0B,$06,$05        ; Column 21: kinds 11, 6, 5
  DEFB $02,$09            ; Column 22: kinds 2, 9
  DEFB $0B,$04,$02,$05,$0D,$08 ; Column 23: kinds 11, 4, 2, 5, 13, 8
  DEFB $04,$06,$02,$05,$0D ; Column 24: kinds 4, 6, 2, 5, 13
  DEFB $0F,$04,$04        ; Column 25: kinds 15, 4, 4
  DEFB $07,$01            ; Column 26: kinds 7, 1
  DEFB $0F,$02,$0D,$10    ; Column 27: kinds 15, 2, 13, 16
  DEFB $07,$03            ; Column 28: kinds 7, 3
  DEFB $0C                ; Column 29: kind 12
  DEFB $06,$0E,$02,$00    ; Column 30: kinds 6, 14, 2, 0
  DEFB $0D                ; Column 31: kind 13
  DEFB $0F,$08,$05        ; Column 32: kinds 15, 8, 5
  DEFB $0C                ; Column 33: kind 12
  DEFB $07,$09,$08        ; Column 34: kinds 7, 9, 8
  DEFB $0D                ; Column 35: kind 13
  DEFB $0B,$0F,$0E,$00    ; Column 36: kinds 11, 15, 14, 0
  DEFB $06,$01,$05        ; Column 37: kinds 6, 1, 5
  DEFB $0F                ; Column 38: kind 15
  DEFB $0F,$02,$0A,$04    ; Column 39: kinds 15, 2, 10, 4
  DEFB $04,$01,$0E,$0D,$00 ; Column 40: kinds 4, 1, 14, 13, 0
  DEFB $07                ; Column 41: kind 7
  DEFB $0F,$02,$03,$04    ; Column 42: kinds 15, 2, 3, 4
  DEFB $06,$0A            ; Column 43: kinds 6, 10
  DEFB $02,$04,$05,$08    ; Column 44: kinds 2, 4, 5, 8
  DEFB $0B,$0B,$02,$04,$0D ; Column 45: kinds 11, 11, 2, 4, 13
  DEFB $02,$0A,$05        ; Column 46: kinds 2, 10, 5
  DEFB $0F,$02,$07,$05,$0E ; Column 47: kinds 15, 2, 7, 5, 14
  DEFB $02                ; Column 48: kind 2
  DEFB $0B,$02            ; Column 49: kinds 11, 2
  DEFB $0F,$02            ; Column 50: kinds 15, 2
  DEFB $06,$04,$05,$04,$04 ; Column 51: kinds 6, 4, 5, 4, 4
  DEFB $06,$02,$09,$0C    ; Column 52: kinds 6, 2, 9, 12
  DEFB $0B,$01            ; Column 53: kinds 11, 1
  DEFB $02,$0C            ; Column 54: kinds 2, 12
  DEFB $04                ; Column 55: kind 4
  DEFB $0F,$03,$0E,$09    ; Column 56: kinds 15, 3, 14, 9
  DEFB $07,$0F,$0E,$04    ; Column 57: kinds 7, 15, 14, 4
  DEFB $0E,$05,$05,$00    ; Column 58: kinds 14, 5, 5, 0
  DEFB $0F,$10            ; Column 59: kinds 15, 16
  DEFB $06,$10            ; Column 60: kinds 6, 16
  DEFB $0B,$05,$04        ; Column 61: kinds 11, 5, 4
  DEFB $02,$04,$0D,$10    ; Column 62: kinds 2, 4, 13, 16
  DEFB $0B                ; Column 63: kind 11
  DEFB $07,$02,$04,$0D,$04 ; Column 64: kinds 7, 2, 4, 13, 4
  DEFB $07,$00,$0A        ; Column 65: kinds 7, 0, 10
  DEFB $0B,$00,$0D,$0A    ; Column 66: kinds 11, 0, 13, 10
  DEFB $04,$0C,$0D        ; Column 67: kinds 4, 12, 13
  DEFB $04                ; Column 68: kind 4
  DEFB $08                ; Column 69: kind 8
  DEFB $06,$02,$01,$08    ; Column 70: kinds 6, 2, 1, 8
  DEFB $05,$0D,$0D        ; Column 71: kinds 5, 13, 13
  DEFB $06,$04            ; Column 72: kinds 6, 4
  DEFB $0B,$05,$04        ; Column 73: kinds 11, 5, 4
  DEFB $04,$02,$0E,$0E    ; Column 74: kinds 4, 2, 14, 14
  DEFB $04,$05,$10        ; Column 75: kinds 4, 5, 16
  DEFB $04,$04,$02,$04,$05,$0A ; Column 76: kinds 4, 4, 2, 4, 5, 10
  DEFB $07,$04,$04        ; Column 77: kinds 7, 4, 4
  DEFB $0F,$04,$00,$09    ; Column 78: kinds 15, 4, 0, 9
  DEFB $0F,$0C            ; Column 79: kinds 15, 12
  DEFB $02,$06,$04        ; Column 80: kinds 2, 6, 4
  DEFB $0F,$0E,$04,$0D,$0C ; Column 81: kinds 15, 14, 4, 13, 12
  DEFB $07,$0F,$0D,$0D    ; Column 82: kinds 7, 15, 13, 13
  DEFB $04,$0E,$10,$04    ; Column 83: kinds 4, 14, 16, 4
  DEFB $04,$10,$10        ; Column 84: kinds 4, 16, 16
  DEFB $0B,$06,$06,$00,$0D ; Column 85: kinds 11, 6, 6, 0, 13
  DEFB $07,$02,$0E,$10    ; Column 86: kinds 7, 2, 14, 16
  DEFB $04,$0D,$05        ; Column 87: kinds 4, 13, 5
  DEFB $07,$04,$02,$10,$0D,$04,$0E ; Column 88: kinds 7, 4, 2, 16, 13, 4, 14
  DEFB $06,$03            ; Column 89: kinds 6, 3
  DEFB $05,$0C,$0D        ; Column 90: kinds 5, 12, 13
  DEFB $04,$02,$07,$04,$00 ; Column 91: kinds 4, 2, 7, 4, 0
  DEFB $02                ; Column 92: kind 2
  DEFB $02,$00,$0C        ; Column 93: kinds 2, 0, 12
  DEFB $07,$04,$00,$04    ; Column 94: kinds 7, 4, 0, 4
  DEFB $02,$0A            ; Column 95: kinds 2, 10

; The number of objects in each map column
;
; One byte for each of the 96 map columns: how many of the objects at
; map_object_kinds and map_object_places lie in it. They add up to 290.
; spawn_column_objects adds up the counts of the columns before the screen's
; column to find its first object, and takes the counts of that column and the
; next.
;
; A 97th byte follows, a copy of column 0's count: when the screen is at column
; 95, the next column is column 0 again, and spawn_column_objects reads its
; count from here before it starts again at the beginning of the tables.
map_objects_per_column:
  DEFB $02                ; Column 0: 2 objects (objects 0-1)
  DEFB $02                ; Column 1: 2 objects (objects 2-3)
  DEFB $06                ; Column 2: 6 objects (objects 4-9)
  DEFB $02                ; Column 3: 2 objects (objects 10-11)
  DEFB $02                ; Column 4: 2 objects (objects 12-13)
  DEFB $03                ; Column 5: 3 objects (objects 14-16)
  DEFB $02                ; Column 6: 2 objects (objects 17-18)
  DEFB $01                ; Column 7: 1 object (object 19)
  DEFB $03                ; Column 8: 3 objects (objects 20-22)
  DEFB $02                ; Column 9: 2 objects (objects 23-24)
  DEFB $04                ; Column 10: 4 objects (objects 25-28)
  DEFB $02                ; Column 11: 2 objects (objects 29-30)
  DEFB $05                ; Column 12: 5 objects (objects 31-35)
  DEFB $02                ; Column 13: 2 objects (objects 36-37)
  DEFB $02                ; Column 14: 2 objects (objects 38-39)
  DEFB $02                ; Column 15: 2 objects (objects 40-41)
  DEFB $02                ; Column 16: 2 objects (objects 42-43)
  DEFB $04                ; Column 17: 4 objects (objects 44-47)
  DEFB $04                ; Column 18: 4 objects (objects 48-51)
  DEFB $03                ; Column 19: 3 objects (objects 52-54)
  DEFB $01                ; Column 20: 1 object (object 55)
  DEFB $03                ; Column 21: 3 objects (objects 56-58)
  DEFB $02                ; Column 22: 2 objects (objects 59-60)
  DEFB $06                ; Column 23: 6 objects (objects 61-66)
  DEFB $05                ; Column 24: 5 objects (objects 67-71)
  DEFB $03                ; Column 25: 3 objects (objects 72-74)
  DEFB $02                ; Column 26: 2 objects (objects 75-76)
  DEFB $04                ; Column 27: 4 objects (objects 77-80)
  DEFB $02                ; Column 28: 2 objects (objects 81-82)
  DEFB $01                ; Column 29: 1 object (object 83)
  DEFB $04                ; Column 30: 4 objects (objects 84-87)
  DEFB $01                ; Column 31: 1 object (object 88)
  DEFB $03                ; Column 32: 3 objects (objects 89-91)
  DEFB $01                ; Column 33: 1 object (object 92)
  DEFB $03                ; Column 34: 3 objects (objects 93-95)
  DEFB $01                ; Column 35: 1 object (object 96)
  DEFB $04                ; Column 36: 4 objects (objects 97-100)
  DEFB $03                ; Column 37: 3 objects (objects 101-103)
  DEFB $01                ; Column 38: 1 object (object 104)
  DEFB $04                ; Column 39: 4 objects (objects 105-108)
  DEFB $05                ; Column 40: 5 objects (objects 109-113)
  DEFB $01                ; Column 41: 1 object (object 114)
  DEFB $04                ; Column 42: 4 objects (objects 115-118)
  DEFB $02                ; Column 43: 2 objects (objects 119-120)
  DEFB $04                ; Column 44: 4 objects (objects 121-124)
  DEFB $05                ; Column 45: 5 objects (objects 125-129)
  DEFB $03                ; Column 46: 3 objects (objects 130-132)
  DEFB $05                ; Column 47: 5 objects (objects 133-137)
  DEFB $01                ; Column 48: 1 object (object 138)
  DEFB $02                ; Column 49: 2 objects (objects 139-140)
  DEFB $02                ; Column 50: 2 objects (objects 141-142)
  DEFB $05                ; Column 51: 5 objects (objects 143-147)
  DEFB $04                ; Column 52: 4 objects (objects 148-151)
  DEFB $02                ; Column 53: 2 objects (objects 152-153)
  DEFB $02                ; Column 54: 2 objects (objects 154-155)
  DEFB $01                ; Column 55: 1 object (object 156)
  DEFB $04                ; Column 56: 4 objects (objects 157-160)
  DEFB $04                ; Column 57: 4 objects (objects 161-164)
  DEFB $04                ; Column 58: 4 objects (objects 165-168)
  DEFB $02                ; Column 59: 2 objects (objects 169-170)
  DEFB $02                ; Column 60: 2 objects (objects 171-172)
  DEFB $03                ; Column 61: 3 objects (objects 173-175)
  DEFB $04                ; Column 62: 4 objects (objects 176-179)
  DEFB $01                ; Column 63: 1 object (object 180)
  DEFB $05                ; Column 64: 5 objects (objects 181-185)
  DEFB $03                ; Column 65: 3 objects (objects 186-188)
  DEFB $04                ; Column 66: 4 objects (objects 189-192)
  DEFB $03                ; Column 67: 3 objects (objects 193-195)
  DEFB $01                ; Column 68: 1 object (object 196)
  DEFB $01                ; Column 69: 1 object (object 197)
  DEFB $04                ; Column 70: 4 objects (objects 198-201)
  DEFB $03                ; Column 71: 3 objects (objects 202-204)
  DEFB $02                ; Column 72: 2 objects (objects 205-206)
  DEFB $03                ; Column 73: 3 objects (objects 207-209)
  DEFB $04                ; Column 74: 4 objects (objects 210-213)
  DEFB $03                ; Column 75: 3 objects (objects 214-216)
  DEFB $06                ; Column 76: 6 objects (objects 217-222)
  DEFB $03                ; Column 77: 3 objects (objects 223-225)
  DEFB $04                ; Column 78: 4 objects (objects 226-229)
  DEFB $02                ; Column 79: 2 objects (objects 230-231)
  DEFB $03                ; Column 80: 3 objects (objects 232-234)
  DEFB $05                ; Column 81: 5 objects (objects 235-239)
  DEFB $04                ; Column 82: 4 objects (objects 240-243)
  DEFB $04                ; Column 83: 4 objects (objects 244-247)
  DEFB $03                ; Column 84: 3 objects (objects 248-250)
  DEFB $05                ; Column 85: 5 objects (objects 251-255)
  DEFB $04                ; Column 86: 4 objects (objects 256-259)
  DEFB $03                ; Column 87: 3 objects (objects 260-262)
  DEFB $07                ; Column 88: 7 objects (objects 263-269)
  DEFB $02                ; Column 89: 2 objects (objects 270-271)
  DEFB $03                ; Column 90: 3 objects (objects 272-274)
  DEFB $05                ; Column 91: 5 objects (objects 275-279)
  DEFB $01                ; Column 92: 1 object (object 280)
  DEFB $03                ; Column 93: 3 objects (objects 281-283)
  DEFB $04                ; Column 94: 4 objects (objects 284-287)
  DEFB $02                ; Column 95: 2 objects (objects 288-289)
  DEFB $02                ; Column 0's count again (2), read when the next
                          ; column after 95 wraps round to 0

; The things lying in the world: their places
;
; Two bytes for each of the 290 objects, in the same order as map_object_kinds.
; Their column is the one map_objects_per_column puts them in; these give the
; rest, as spawn_objects_from_list unpacks them into an object slot.
;
; First byte: bits 2-7, less 1, the y within the map cell (slot +5); bits 0-1
; are bits 3-4 of the x within the cell. Second byte: bits 5-7 are bits 5-7 of
; the x (slot +2), and bits 0-4 the map row, 1-28 (slot +4). An object is put
; into a slot only when its row is the screen's map row or the one below.
;
; So objects are placed on an 8-unit grid in x and a 4-unit grid in y within
; their cell. Laid out below a line per map column, each object's kind (from
; map_object_kinds), row and place decoded, from the game's bytes when the
; disassembly is built.
map_object_places:
  DEFB $2A,$C8            ; Column 0: kind 6 in row 8 at x 208, y 39; kind 8 in
  DEFB $42,$56            ; row 22 at x 80, y 63
  DEFB $B0,$69            ; Column 1: kind 2 in row 9 at x 96, y 175; kind 13
  DEFB $81,$B6            ; in row 22 at x 168, y 127
  DEFB $80,$62            ; Column 2: kind 15 in row 2 at x 96, y 127; kind 7
  DEFB $29,$08            ; in row 8 at x 8, y 39; kind 3 in row 10 at x 80, y
  DEFB $36,$4A            ; 51; kind 14 in row 18 at x 192, y 147; kind 16 in
  DEFB $94,$D2            ; row 21 at x 112, y 111; kind 10 in row 26 at x 80,
  DEFB $72,$75            ; y 147
  DEFB $96,$5A            ;
  DEFB $B0,$66            ; Column 3: kind 2 in row 6 at x 96, y 175; kind 5 in
  DEFB $96,$12            ; row 18 at x 16, y 147
  DEFB $76,$28            ; Column 4: kind 1 in row 8 at x 48, y 115; kind 4 in
  DEFB $50,$3B            ; row 27 at x 32, y 79
  DEFB $2A,$C3            ; Column 5: kind 6 in row 3 at x 208, y 39; kind 0 in
  DEFB $97,$47            ; row 7 at x 88, y 147; kind 16 in row 14 at x 168, y
  DEFB $71,$AE            ; 111
  DEFB $B0,$65            ; Column 6: kind 2 in row 5 at x 96, y 175; kind 5 in
  DEFB $35,$47            ; row 7 at x 72, y 51
  DEFB $89,$21            ; Column 7: kind 11 in row 1 at x 40, y 135
  DEFB $29,$03            ; Column 8: kind 7 in row 3 at x 8, y 39; kind 4 in
  DEFB $31,$6F            ; row 15 at x 104, y 47; kind 14 in row 24 at x 128,
  DEFB $70,$98            ; y 111
  DEFB $B0,$63            ; Column 9: kind 15 in row 3 at x 96, y 175; kind 4
  DEFB $42,$74            ; in row 20 at x 112, y 63
  DEFB $37,$41            ; Column 10: kind 11 in row 1 at x 88, y 51; kind 6
  DEFB $2A,$C4            ; in row 4 at x 208, y 39; kind 5 in row 17 at x 72,
  DEFB $35,$51            ; y 51; kind 13 in row 21 at x 96, y 175
  DEFB $B0,$75            ;
  DEFB $70,$4E            ; Column 11: kind 16 in row 14 at x 64, y 111; kind
  DEFB $70,$59            ; 16 in row 25 at x 64, y 111
  DEFB $A2,$82            ; Column 12: kind 15 in row 2 at x 144, y 159; kind 3
  DEFB $55,$25            ; in row 5 at x 40, y 83; kind 5 in row 19 at x 88, y
  DEFB $37,$53            ; 51; kind 16 in row 21 at x 144, y 127; kind 8 in
  DEFB $82,$95            ; row 27 at x 80, y 87
  DEFB $5A,$5B            ;
  DEFB $97,$A7            ; Column 13: kind 1 in row 7 at x 184, y 147; kind 5
  DEFB $95,$72            ; in row 18 at x 104, y 147
  DEFB $29,$04            ; Column 14: kind 7 in row 4 at x 8, y 39; kind 2 in
  DEFB $B0,$66            ; row 6 at x 96, y 175
  DEFB $2A,$C1            ; Column 15: kind 6 in row 1 at x 208, y 39; kind 10
  DEFB $96,$57            ; in row 23 at x 80, y 147
  DEFB $60,$23            ; Column 16: kind 15 in row 3 at x 32, y 95; kind 1
  DEFB $96,$47            ; in row 7 at x 80, y 147
  DEFB $B0,$65            ; Column 17: kind 2 in row 5 at x 96, y 175; kind 10
  DEFB $94,$8F            ; in row 15 at x 128, y 147; kind 14 in row 18 at x
  DEFB $62,$32            ; 48, y 95; kind 4 in row 23 at x 160, y 127
  DEFB $80,$B7            ;
  DEFB $34,$66            ; Column 18: kind 4 in row 6 at x 96, y 51; kind 14
  DEFB $32,$27            ; in row 7 at x 48, y 47; kind 12 in row 9 at x 64, y
  DEFB $94,$49            ; 147; kind 14 in row 23 at x 24, y 127
  DEFB $83,$17            ;
  DEFB $29,$01            ; Column 19: kind 7 in row 1 at x 8, y 39; kind 4 in
  DEFB $46,$A4            ; row 4 at x 176, y 67; kind 1 in row 9 at x 128, y
  DEFB $94,$89            ; 147
  DEFB $4A,$5A            ; Column 20: kind 8 in row 26 at x 80, y 71
  DEFB $89,$42            ; Column 21: kind 11 in row 2 at x 72, y 135; kind 6
  DEFB $2A,$C6            ; in row 6 at x 208, y 39; kind 5 in row 9 at x 24, y
  DEFB $97,$09            ; 147
  DEFB $B0,$68            ; Column 22: kind 2 in row 8 at x 96, y 175; kind 9
  DEFB $49,$97            ; in row 23 at x 136, y 71
  DEFB $74,$21            ; Column 23: kind 11 in row 1 at x 32, y 115; kind 4
  DEFB $56,$85            ; in row 5 at x 144, y 83; kind 2 in row 9 at x 96, y
  DEFB $60,$69            ; 95; kind 5 in row 16 at x 128, y 147; kind 13 in
  DEFB $94,$90            ; row 20 at x 160, y 159; kind 8 in row 27 at x 80, y
  DEFB $A0,$B4            ; 91
  DEFB $5E,$5B            ;
  DEFB $75,$05            ; Column 24: kind 4 in row 5 at x 8, y 115; kind 6 in
  DEFB $2A,$C7            ; row 7 at x 208, y 39; kind 2 in row 9 at x 96, y
  DEFB $B0,$69            ; 175; kind 5 in row 11 at x 32, y 147; kind 13 in
  DEFB $94,$2B            ; row 26 at x 96, y 175
  DEFB $B0,$7A            ;
  DEFB $92,$83            ; Column 25: kind 15 in row 3 at x 144, y 143; kind 4
  DEFB $69,$73            ; in row 19 at x 104, y 103; kind 4 in row 26 at x
  DEFB $38,$DA            ; 192, y 55
  DEFB $29,$07            ; Column 26: kind 7 in row 7 at x 8, y 39; kind 1 in
  DEFB $76,$29            ; row 9 at x 48, y 115
  DEFB $7A,$21            ; Column 27: kind 15 in row 1 at x 48, y 119; kind 2
  DEFB $92,$64            ; in row 4 at x 112, y 143; kind 13 in row 19 at x
  DEFB $B2,$13            ; 16, y 175; kind 16 in row 24 at x 32, y 103
  DEFB $68,$38            ;
  DEFB $29,$06            ; Column 28: kind 7 in row 6 at x 8, y 39; kind 3 in
  DEFB $34,$47            ; row 7 at x 64, y 51
  DEFB $55,$78            ; Column 29: kind 12 in row 24 at x 104, y 83
  DEFB $2A,$C2            ; Column 30: kind 6 in row 2 at x 208, y 39; kind 14
  DEFB $62,$65            ; in row 5 at x 112, y 95; kind 2 in row 7 at x 96, y
  DEFB $B0,$67            ; 175; kind 0 in row 8 at x 8, y 147
  DEFB $95,$08            ;
  DEFB $61,$77            ; Column 31: kind 13 in row 23 at x 104, y 95
  DEFB $52,$43            ; Column 32: kind 15 in row 3 at x 80, y 79; kind 8
  DEFB $52,$53            ; in row 19 at x 80, y 79; kind 5 in row 24 at x 120,
  DEFB $57,$78            ; y 83
  DEFB $57,$45            ; Column 33: kind 12 in row 5 at x 88, y 83
  DEFB $29,$02            ; Column 34: kind 7 in row 2 at x 8, y 39; kind 9 in
  DEFB $41,$93            ; row 19 at x 136, y 63; kind 8 in row 26 at x 48, y
  DEFB $76,$3A            ; 115
  DEFB $60,$34            ; Column 35: kind 13 in row 20 at x 32, y 95
  DEFB $A2,$01            ; Column 36: kind 11 in row 1 at x 16, y 159; kind 15
  DEFB $B0,$63            ; in row 3 at x 96, y 175; kind 14 in row 17 at x 96,
  DEFB $60,$71            ; y 95; kind 0 in row 25 at x 88, y 147
  DEFB $97,$59            ;
  DEFB $2A,$C5            ; Column 37: kind 6 in row 5 at x 208, y 39; kind 1
  DEFB $97,$66            ; in row 6 at x 120, y 147; kind 5 in row 28 at x 32,
  DEFB $94,$3C            ; y 147
  DEFB $83,$43            ; Column 38: kind 15 in row 3 at x 88, y 127
  DEFB $A2,$81            ; Column 39: kind 15 in row 1 at x 144, y 159; kind 2
  DEFB $B0,$66            ; in row 6 at x 96, y 175; kind 10 in row 15 at x
  DEFB $94,$8F            ; 128, y 147; kind 4 in row 28 at x 16, y 47
  DEFB $32,$1C            ;
  DEFB $A0,$68            ; Column 40: kind 4 in row 8 at x 96, y 159; kind 1
  DEFB $94,$49            ; in row 9 at x 64, y 147; kind 14 in row 16 at x
  DEFB $50,$B0            ; 160, y 79; kind 13 in row 19 at x 32, y 111; kind 0
  DEFB $70,$33            ; in row 22 at x 64, y 147
  DEFB $94,$56            ;
  DEFB $29,$05            ; Column 41: kind 7 in row 5 at x 8, y 39
  DEFB $B2,$83            ; Column 42: kind 15 in row 3 at x 144, y 175; kind 2
  DEFB $B0,$67            ; in row 7 at x 96, y 175; kind 3 in row 10 at x 120,
  DEFB $97,$6A            ; y 147; kind 4 in row 14 at x 104, y 119
  DEFB $79,$6E            ;
  DEFB $2A,$C9            ; Column 43: kind 6 in row 9 at x 208, y 39; kind 10
  DEFB $96,$54            ; in row 20 at x 80, y 147
  DEFB $9E,$65            ; Column 44: kind 2 in row 5 at x 112, y 155; kind 4
  DEFB $95,$0B            ; in row 11 at x 8, y 147; kind 5 in row 24 at x 8, y
  DEFB $95,$18            ; 147; kind 8 in row 26 at x 80, y 71
  DEFB $4A,$5A            ;
  DEFB $98,$61            ; Column 45: kind 11 in row 1 at x 96, y 151; kind 11
  DEFB $3A,$62            ; in row 2 at x 112, y 55; kind 2 in row 8 at x 40, y
  DEFB $9D,$28            ; 155; kind 4 in row 10 at x 200, y 175; kind 13 in
  DEFB $B1,$CA            ; row 23 at x 96, y 175
  DEFB $B0,$77            ;
  DEFB $9A,$A6            ; Column 46: kind 2 in row 6 at x 176, y 151; kind 10
  DEFB $95,$31            ; in row 17 at x 40, y 147; kind 5 in row 25 at x
  DEFB $97,$B9            ; 184, y 147
  DEFB $90,$A2            ; Column 47: kind 15 in row 2 at x 160, y 143; kind 2
  DEFB $89,$66            ; in row 6 at x 104, y 135; kind 7 in row 9 at x 8, y
  DEFB $29,$09            ; 39; kind 5 in row 10 at x 136, y 147; kind 14 in
  DEFB $95,$8A            ; row 19 at x 200, y 55
  DEFB $39,$D3            ;
  DEFB $7A,$46            ; Column 48: kind 2 in row 6 at x 80, y 119
  DEFB $9F,$81            ; Column 49: kind 11 in row 1 at x 152, y 155; kind 2
  DEFB $41,$08            ; in row 8 at x 8, y 63
  DEFB $82,$63            ; Column 50: kind 15 in row 3 at x 112, y 127; kind 2
  DEFB $8D,$06            ; in row 6 at x 8, y 139
  DEFB $2A,$C1            ; Column 51: kind 6 in row 1 at x 208, y 39; kind 4
  DEFB $A3,$48            ; in row 8 at x 88, y 159; kind 5 in row 9 at x 80, y
  DEFB $36,$49            ; 51; kind 4 in row 18 at x 200, y 63; kind 4 in row
  DEFB $41,$D2            ; 21 at x 184, y 103
  DEFB $6B,$B5            ;
  DEFB $2A,$C4            ; Column 52: kind 6 in row 4 at x 208, y 39; kind 2
  DEFB $B0,$C6            ; in row 6 at x 192, y 175; kind 9 in row 18 at x
  DEFB $59,$92            ; 136, y 87; kind 12 in row 22 at x 136, y 115
  DEFB $75,$96            ;
  DEFB $43,$62            ; Column 53: kind 11 in row 2 at x 120, y 63; kind 1
  DEFB $95,$47            ; in row 7 at x 72, y 147
  DEFB $97,$85            ; Column 54: kind 2 in row 5 at x 152, y 147; kind 12
  DEFB $95,$78            ; in row 24 at x 104, y 147
  DEFB $60,$98            ; Column 55: kind 4 in row 24 at x 128, y 95
  DEFB $BB,$43            ; Column 56: kind 15 in row 3 at x 88, y 183; kind 3
  DEFB $95,$05            ; in row 5 at x 8, y 147; kind 14 in row 18 at x 160,
  DEFB $40,$B2            ; y 63; kind 9 in row 27 at x 8, y 119
  DEFB $79,$1B            ;
  DEFB $29,$01            ; Column 57: kind 7 in row 1 at x 8, y 39; kind 15 in
  DEFB $B0,$64            ; row 4 at x 96, y 175; kind 14 in row 23 at x 72, y
  DEFB $41,$57            ; 63; kind 4 in row 28 at x 104, y 87
  DEFB $59,$7C            ;
  DEFB $AF,$25            ; Column 58: kind 14 in row 5 at x 56, y 171; kind 5
  DEFB $96,$46            ; in row 6 at x 80, y 147; kind 5 in row 17 at x 72,
  DEFB $95,$51            ; y 147; kind 0 in row 26 at x 120, y 147
  DEFB $97,$7A            ;
  DEFB $90,$83            ; Column 59: kind 15 in row 3 at x 128, y 143; kind
  DEFB $3A,$16            ; 16 in row 22 at x 16, y 55
  DEFB $2A,$C6            ; Column 60: kind 6 in row 6 at x 208, y 39; kind 16
  DEFB $30,$90            ; in row 16 at x 128, y 47
  DEFB $B8,$62            ; Column 61: kind 11 in row 2 at x 96, y 183; kind 5
  DEFB $94,$74            ; in row 20 at x 96, y 147; kind 4 in row 27 at x 32,
  DEFB $30,$3B            ; y 47
  DEFB $B0,$67            ; Column 62: kind 2 in row 7 at x 96, y 175; kind 4
  DEFB $92,$CB            ; in row 11 at x 208, y 143; kind 13 in row 16 at x
  DEFB $B0,$70            ; 96, y 175; kind 16 in row 23 at x 32, y 103
  DEFB $68,$37            ;
  DEFB $92,$61            ; Column 63: kind 11 in row 1 at x 112, y 143
  DEFB $29,$04            ; Column 64: kind 7 in row 4 at x 8, y 39; kind 2 in
  DEFB $B0,$68            ; row 8 at x 96, y 175; kind 4 in row 11 at x 8, y
  DEFB $91,$0B            ; 143; kind 13 in row 21 at x 48, y 143; kind 4 in
  DEFB $92,$35            ; row 25 at x 72, y 107
  DEFB $6D,$59            ;
  DEFB $29,$06            ; Column 65: kind 7 in row 6 at x 8, y 39; kind 0 in
  DEFB $96,$49            ; row 9 at x 80, y 147; kind 10 in row 16 at x 104, y
  DEFB $35,$70            ; 51
  DEFB $92,$82            ; Column 66: kind 11 in row 2 at x 144, y 143; kind 0
  DEFB $94,$D2            ; in row 18 at x 192, y 147; kind 13 in row 21 at x
  DEFB $92,$75            ; 112, y 143; kind 10 in row 24 at x 128, y 147
  DEFB $94,$98            ;
  DEFB $92,$45            ; Column 67: kind 4 in row 5 at x 80, y 143; kind 12
  DEFB $56,$47            ; in row 7 at x 80, y 83; kind 13 in row 24 at x 96,
  DEFB $B0,$78            ; y 175
  DEFB $6F,$79            ; Column 68: kind 4 in row 25 at x 120, y 107
  DEFB $42,$57            ; Column 69: kind 8 in row 23 at x 80, y 63
  DEFB $2A,$C3            ; Column 70: kind 6 in row 3 at x 208, y 39; kind 2
  DEFB $B0,$66            ; in row 6 at x 96, y 175; kind 1 in row 8 at x 160,
  DEFB $94,$A8            ; y 147; kind 8 in row 18 at x 80, y 71
  DEFB $4A,$52            ;
  DEFB $95,$08            ; Column 71: kind 5 in row 8 at x 8, y 147; kind 13
  DEFB $B0,$71            ; in row 17 at x 96, y 175; kind 13 in row 25 at x
  DEFB $42,$19            ; 16, y 63
  DEFB $2A,$C7            ; Column 72: kind 6 in row 7 at x 208, y 39; kind 4
  DEFB $72,$39            ; in row 25 at x 48, y 111
  DEFB $72,$62            ; Column 73: kind 11 in row 2 at x 112, y 111; kind 5
  DEFB $95,$89            ; in row 9 at x 136, y 147; kind 4 in row 20 at x
  DEFB $60,$94            ; 128, y 95
  DEFB $50,$26            ; Column 74: kind 4 in row 6 at x 32, y 79; kind 2 in
  DEFB $B0,$69            ; row 9 at x 96, y 175; kind 14 in row 16 at x 32, y
  DEFB $40,$30            ; 63; kind 14 in row 26 at x 16, y 55
  DEFB $3A,$1A            ;
  DEFB $90,$69            ; Column 75: kind 4 in row 9 at x 96, y 143; kind 5
  DEFB $97,$AB            ; in row 11 at x 184, y 147; kind 16 in row 20 at x
  DEFB $73,$34            ; 56, y 111
  DEFB $A1,$45            ; Column 76: kind 4 in row 5 at x 72, y 159; kind 4
  DEFB $9C,$88            ; in row 8 at x 128, y 155; kind 2 in row 9 at x 96,
  DEFB $B0,$69            ; y 175; kind 4 in row 10 at x 168, y 139; kind 5 in
  DEFB $8D,$AA            ; row 11 at x 8, y 147; kind 10 in row 15 at x 80, y
  DEFB $95,$0B            ; 147
  DEFB $96,$4F            ;
  DEFB $29,$03            ; Column 77: kind 7 in row 3 at x 8, y 39; kind 4 in
  DEFB $A3,$09            ; row 9 at x 24, y 159; kind 4 in row 19 at x 16, y
  DEFB $52,$13            ; 79
  DEFB $B2,$A1            ; Column 78: kind 15 in row 1 at x 176, y 175; kind 4
  DEFB $B1,$26            ; in row 6 at x 40, y 175; kind 0 in row 9 at x 192,
  DEFB $94,$C9            ; y 147; kind 9 in row 19 at x 136, y 79
  DEFB $51,$93            ;
  DEFB $92,$21            ; Column 79: kind 15 in row 1 at x 48, y 143; kind 12
  DEFB $94,$29            ; in row 9 at x 32, y 147
  DEFB $A3,$46            ; Column 80: kind 2 in row 6 at x 88, y 159; kind 6
  DEFB $2A,$C8            ; in row 8 at x 208, y 39; kind 4 in row 9 at x 160,
  DEFB $60,$A9            ; y 95
  DEFB $43,$83            ; Column 81: kind 15 in row 3 at x 152, y 63; kind 14
  DEFB $59,$CA            ; in row 10 at x 200, y 87; kind 4 in row 17 at x 40,
  DEFB $79,$31            ; y 119; kind 13 in row 24 at x 80, y 175; kind 12 in
  DEFB $B2,$58            ; row 27 at x 112, y 51
  DEFB $36,$7B            ;
  DEFB $29,$08            ; Column 82: kind 7 in row 8 at x 8, y 39; kind 15 in
  DEFB $84,$6A            ; row 10 at x 96, y 131; kind 13 in row 15 at x 128,
  DEFB $70,$8F            ; y 111; kind 13 in row 26 at x 168, y 127
  DEFB $81,$BA            ;
  DEFB $72,$A6            ; Column 83: kind 4 in row 6 at x 176, y 111; kind 14
  DEFB $5A,$0A            ; in row 10 at x 16, y 87; kind 16 in row 22 at x
  DEFB $6B,$B6            ; 184, y 103; kind 4 in row 28 at x 184, y 87
  DEFB $5B,$BC            ;
  DEFB $8E,$26            ; Column 84: kind 4 in row 6 at x 48, y 139; kind 16
  DEFB $7B,$AE            ; in row 14 at x 184, y 119; kind 16 in row 17 at x
  DEFB $82,$71            ; 112, y 127
  DEFB $80,$82            ; Column 85: kind 11 in row 2 at x 128, y 127; kind 6
  DEFB $2A,$C5            ; in row 5 at x 208, y 39; kind 6 in row 9 at x 208,
  DEFB $2A,$C9            ; y 39; kind 0 in row 24 at x 48, y 147; kind 13 in
  DEFB $96,$38            ; row 27 at x 176, y 87
  DEFB $5A,$BB            ;
  DEFB $29,$07            ; Column 86: kind 7 in row 7 at x 8, y 39; kind 2 in
  DEFB $B0,$69            ; row 9 at x 96, y 175; kind 14 in row 17 at x 48, y
  DEFB $4A,$31            ; 71; kind 16 in row 21 at x 112, y 119
  DEFB $7A,$75            ;
  DEFB $B2,$0B            ; Column 87: kind 4 in row 11 at x 16, y 175; kind 13
  DEFB $B0,$77            ; in row 23 at x 96, y 175; kind 5 in row 25 at x 88,
  DEFB $37,$59            ; y 51
  DEFB $29,$05            ; Column 88: kind 7 in row 5 at x 8, y 39; kind 4 in
  DEFB $B8,$68            ; row 8 at x 96, y 183; kind 2 in row 9 at x 96, y
  DEFB $B0,$69            ; 175; kind 16 in row 16 at x 24, y 119; kind 13 in
  DEFB $7B,$10            ; row 23 at x 96, y 175; kind 4 in row 24 at x 104, y
  DEFB $B0,$77            ; 143; kind 14 in row 27 at x 32, y 63
  DEFB $91,$78            ;
  DEFB $40,$3B            ;
  DEFB $2A,$C2            ; Column 89: kind 6 in row 2 at x 208, y 39; kind 3
  DEFB $34,$4A            ; in row 10 at x 64, y 51
  DEFB $96,$14            ; Column 90: kind 5 in row 20 at x 16, y 147; kind 12
  DEFB $34,$76            ; in row 22 at x 96, y 51; kind 13 in row 24 at x 32,
  DEFB $68,$38            ; y 103
  DEFB $9E,$24            ; Column 91: kind 4 in row 4 at x 48, y 155; kind 2
  DEFB $80,$87            ; in row 7 at x 128, y 127; kind 7 in row 9 at x 8, y
  DEFB $29,$09            ; 39; kind 4 in row 11 at x 112, y 63; kind 0 in row
  DEFB $42,$6B            ; 21 at x 80, y 147
  DEFB $96,$55            ;
  DEFB $96,$49            ; Column 92: kind 2 in row 9 at x 80, y 147
  DEFB $A3,$28            ; Column 93: kind 2 in row 8 at x 56, y 159; kind 0
  DEFB $96,$31            ; in row 17 at x 48, y 147; kind 12 in row 26 at x
  DEFB $77,$7A            ; 120, y 115
  DEFB $29,$02            ; Column 94: kind 7 in row 2 at x 8, y 39; kind 4 in
  DEFB $B2,$A5            ; row 5 at x 176, y 175; kind 0 in row 11 at x 120, y
  DEFB $97,$6B            ; 147; kind 4 in row 20 at x 16, y 47
  DEFB $32,$14            ;
  DEFB $B0,$69            ; Column 95: kind 2 in row 9 at x 96, y 175; kind 10
  DEFB $97,$B2            ; in row 18 at x 184, y 147

; The places the pick-ups can be dealt to
;
; At every new game deal_places deals each player nineteen places for the
; pick-ups, chosen at random from this pool and copied into the player's record
; (from +$1D of score or other_player_record); load_map_window later puts the
; ones in the screen's cells into object slots. Each place is four bytes: the
; map column, the map row, the x within the cell (to the slot's +2) and the y
; within the cell (to the slot's +5). While dealing, bit 7 of the first byte
; marks a place already given out.
;
; Five groups, one for each value in the dealing list (dealing_list). No table
; says where they are: deal_places picks a group by its own code, loading its
; first place and a mask one less than its size. Within each group the places
; are in column order. Laid out below a line per place, decoded from the game's
; bytes when the disassembly is built.
place_pool_kind3:
  DEFB $04,$08,$38,$58    ; Group 3, place 0: column 4, row 8, x 56, y 88; a
                          ; player's places 11-17 are dealt from this group
  DEFB $09,$03,$68,$9B    ; Group 3, place 1: column 9, row 3, x 104, y 155
  DEFB $0A,$15,$68,$9B    ; Group 3, place 2: column 10, row 21, x 104, y 155
  DEFB $11,$05,$68,$97    ; Group 3, place 3: column 17, row 5, x 104, y 151
  DEFB $17,$09,$68,$47    ; Group 3, place 4: column 23, row 9, x 104, y 71
  DEFB $1B,$13,$18,$9B    ; Group 3, place 5: column 27, row 19, x 24, y 155
  DEFB $1F,$17,$70,$4B    ; Group 3, place 6: column 31, row 23, x 112, y 75
  DEFB $24,$03,$68,$9B    ; Group 3, place 7: column 36, row 3, x 104, y 155
  DEFB $25,$06,$80,$78    ; Group 3, place 8: column 37, row 6, x 128, y 120
  DEFB $2D,$17,$68,$9B    ; Group 3, place 9: column 45, row 23, x 104, y 155
  DEFB $39,$04,$68,$9B    ; Group 3, place 10: column 57, row 4, x 104, y 155
  DEFB $3E,$10,$68,$9B    ; Group 3, place 11: column 62, row 16, x 104, y 155
  DEFB $40,$08,$68,$97    ; Group 3, place 12: column 64, row 8, x 104, y 151
  DEFB $47,$11,$68,$9B    ; Group 3, place 13: column 71, row 17, x 104, y 155
  DEFB $57,$17,$68,$9B    ; Group 3, place 14: column 87, row 23, x 104, y 155
  DEFB $58,$17,$68,$9B    ; Group 3, place 15: column 88, row 23, x 104, y 155
place_pool_kind1:
  DEFB $01,$09,$68,$97    ; Group 1, place 0: column 1, row 9, x 104, y 151; a
                          ; player's places 0, 6, 8 and 9 are dealt from this
                          ; group
  DEFB $03,$06,$68,$97    ; Group 1, place 1: column 3, row 6, x 104, y 151
  DEFB $06,$05,$68,$97    ; Group 1, place 2: column 6, row 5, x 104, y 151
  DEFB $13,$09,$88,$78    ; Group 1, place 3: column 19, row 9, x 136, y 120
  DEFB $27,$06,$68,$97    ; Group 1, place 4: column 39, row 6, x 104, y 151
  DEFB $28,$09,$48,$78    ; Group 1, place 5: column 40, row 9, x 72, y 120
  DEFB $2A,$07,$68,$97    ; Group 1, place 6: column 42, row 7, x 104, y 151
  DEFB $35,$07,$50,$78    ; Group 1, place 7: column 53, row 7, x 80, y 120
  DEFB $3E,$07,$68,$97    ; Group 1, place 8: column 62, row 7, x 104, y 151
  DEFB $46,$06,$68,$97    ; Group 1, place 9: column 70, row 6, x 104, y 151
  DEFB $46,$08,$A8,$78    ; Group 1, place 10: column 70, row 8, x 168, y 120
  DEFB $4A,$09,$68,$97    ; Group 1, place 11: column 74, row 9, x 104, y 151
  DEFB $4C,$09,$68,$97    ; Group 1, place 12: column 76, row 9, x 104, y 151
  DEFB $58,$09,$68,$97    ; Group 1, place 13: column 88, row 9, x 104, y 151
  DEFB $5C,$09,$58,$7B    ; Group 1, place 14: column 92, row 9, x 88, y 123
  DEFB $5F,$09,$68,$97    ; Group 1, place 15: column 95, row 9, x 104, y 151
place_pool_kind5:
  DEFB $0D,$07,$B8,$78    ; Group 5, place 0: column 13, row 7, x 184, y 120; a
                          ; player's places 3 and 4 are dealt from this group
  DEFB $10,$07,$50,$78    ; Group 5, place 1: column 16, row 7, x 80, y 120
place_pool_kind2:
  DEFB $FF,$07,$C0,$78    ; Group 2, place 0: never dealt, since deal_places
                          ; marks it dealt before it starts (its bytes: $FF,
                          ; row 7, x 192, y 120); a player's places 5 and 7 are
                          ; dealt from this group
  DEFB $0E,$06,$68,$97    ; Group 2, place 1: column 14, row 6, x 104, y 151
place_pool_kind2_third:
  DEFB $FF,$07,$58,$78    ; Group 2, place 2: never dealt, since deal_places
                          ; marks it dealt before it starts (its bytes: $FF,
                          ; row 7, x 88, y 120)
  DEFB $16,$08,$68,$97    ; Group 2, place 3: column 22, row 8, x 104, y 151
  DEFB $18,$09,$68,$97    ; Group 2, place 4: column 24, row 9, x 104, y 151
  DEFB $1A,$09,$38,$58    ; Group 2, place 5: column 26, row 9, x 56, y 88
  DEFB $1E,$07,$68,$97    ; Group 2, place 6: column 30, row 7, x 104, y 151
  DEFB $56,$09,$68,$97    ; Group 2, place 7: column 86, row 9, x 104, y 151
place_pool_kind4:
  DEFB $01,$16,$B0,$6B    ; Group 4, place 0: column 1, row 22, x 176, y 107; a
                          ; player's place 18 is dealt from this group
  DEFB $18,$1A,$68,$9B    ; Group 4, place 1: column 24, row 26, x 104, y 155
  DEFB $43,$18,$68,$9B    ; Group 4, place 2: column 67, row 24, x 104, y 155
  DEFB $51,$18,$58,$9B    ; Group 4, place 3: column 81, row 24, x 88, y 155

; Deal a player the places of the pick-ups
;
; Used by the routine at new_game.
;
; Called twice by new_game at a new game, once for each player's record (score,
; other_player_record). Goes through the dealing list at dealing_list and, for
; each entry, writes a four-byte place (see place_pool_kind3) to the record and
; moves on four bytes. An entry of 1 to 5 deals a place at random from that
; group of the pool, one not dealt already; an entry of 0 deals nothing,
; leaving the four bytes as they were ($FF, no place) -- unless the next entry
; is 3, when it copies the place before it, 16 to the right and 35 up, so the
; two pick-ups lie side by side. The list ends with its entry of 4, which is
; dealt too.
;
; Every place in the pool is free again at the start, so the two players are
; dealt independently and may be given the same places.
;
; DE The player record's first place (+$1D)
deal_places:
  LD IY,dealing_list      ; The whole list, nineteen entries
; This entry point is used by the routine at planet_secured.
;
; With IY at the list's last entry alone (planet_secured, at the end of a
; stage), this deals the record's last place again.
deal_places_0:
  LD HL,place_pool_kind3  ; Mark all 46 places in the pool free
  LD B,$2E                ;
deal_places_1:
  LD A,(HL)               ;
  AND $7F                 ;
  LD (HL),A               ;
  INC HL                  ;
  INC HL                  ;
  INC HL                  ;
  INC HL                  ;
  DJNZ deal_places_1      ;
  LD A,$FF                      ; Except the first and third places of kind 2,
  LD (place_pool_kind2),A       ; never dealt
  LD (place_pool_kind2_third),A ;
deal_places_2:
  LD A,(IY+$00)           ; An entry of 1-5: deal from a group
  AND A                   ;
  JR NZ,deal_places_3     ;
  LD A,(IY+$01)           ; An entry of 0: unless the next entry is 3, leave
  CP $03                  ; this place alone
  JR NZ,deal_places_6     ;
  PUSH DE                 ; Copy the place before it
  POP HL                  ;
  DEC HL                  ;
  DEC HL                  ;
  DEC HL                  ;
  DEC HL                  ;
  LD BC,$0004             ;
  LDIR                    ;
  DEC DE                  ; Its y 35 less
  LD A,(DE)               ;
  SUB $23                 ;
  LD (DE),A               ;
  DEC DE                  ; Its x 16 more
  LD A,(DE)               ;
  ADD A,$10               ;
  LD (DE),A               ;
  JR deal_places_7        ; Next entry
deal_places_3:
  LD C,$0F                ; Pick the kind's group and a mask one less than its
  LD HL,place_pool_kind1  ; size: kind 1 the second group of place_pool_kind3,
  CP $01                  ; 3 the first, 2 the fourth, 4 the fifth, 5 the third
  JR Z,deal_places_4      ;
  LD HL,place_pool_kind3  ;
  CP $03                  ;
  JR Z,deal_places_4      ;
  LD C,$07                ;
  LD HL,place_pool_kind2  ;
  CP $02                  ;
  JR Z,deal_places_4      ;
  LD C,$03                ;
  LD HL,place_pool_kind4  ;
  CP $04                  ;
  JR Z,deal_places_4      ;
  LD HL,place_pool_kind5  ;
  LD C,$01                ;
deal_places_4:
  PUSH HL                 ; A place of the group chosen by the random number
  LD A,(random_a)         ; (random_a)
  AND C                   ;
  ADD A,A                 ;
  ADD A,A                 ;
  CALL add_a_to_hl        ;
  BIT 7,(HL)              ; Already dealt: stir the random number (random) and
  JR Z,deal_places_5      ; try again
  CALL random             ;
  POP HL                  ;
  JR deal_places_4        ;
deal_places_5:
  LD BC,$0004             ; Copy it to the record
  LDIR                    ;
  DEC HL                  ; Mark it dealt
  DEC HL                  ;
  DEC HL                  ;
  DEC HL                  ;
  SET 7,(HL)              ;
  POP HL                  ; Next entry
  JR deal_places_8        ;
deal_places_6:
  INC DE                  ; Leave four bytes
  INC DE                  ;
deal_places_7:
  INC DE                  ;
  INC DE                  ;
deal_places_8:
  LD A,(IY+$00)           ; Stop after the entry of 4
  CP $04                  ;
  RET Z                   ;
  INC IY                  ; Next entry
  JR deal_places_2        ;

; The dealing list
;
; One byte for each of the nineteen places deal_places deals a player, in the
; order they go into the record: the kind of the pool group (place_pool_kind3)
; each is dealt from, or 0 for one dealt from nowhere. load_map_window gives
; the first eleven places object kind $0C and the last eight kind $1E, and puts
; the place's number, counted again from 0 at the twelfth, in the slot's +$13.
;
; Laid out below a line per entry, decoded from the game's bytes when the
; disassembly is built.
dealing_list:
  DEFB $01                ; Place 0 (add-on 1's): a place from group 1
  DEFB $00                ; Place 1 (add-on 2's): none: its four bytes are left
                          ; $FF, no place
  DEFB $00                ; Place 2 (add-on 3's): none: its four bytes are left
                          ; $FF, no place
  DEFB $05                ; Place 3 (add-on 4's): a place from group 5
  DEFB $05                ; Place 4 (add-on 5's): a place from group 5
  DEFB $02                ; Place 5 (add-on 6's): a place from group 2
  DEFB $01                ; Place 6 (add-on 7's): a place from group 1
  DEFB $02                ; Place 7 (add-on 8's): a place from group 2
  DEFB $01                ; Place 8 (add-on 9's): a place from group 1
  DEFB $01                ; Place 9 (add-on 10's): a place from group 1
  DEFB $00                ; Place 10 (add-on 11's): a copy of the place before,
                          ; 16 to the right and 35 up (the entry is 0 and the
                          ; next is 3)
  DEFB $03                ; Place 11 (rocket part 0's): a place from group 3
  DEFB $03                ; Place 12 (rocket part 1's): a place from group 3
  DEFB $03                ; Place 13 (rocket part 2's): a place from group 3
  DEFB $03                ; Place 14 (rocket part 3's): a place from group 3
  DEFB $03                ; Place 15 (rocket part 4's): a place from group 3
  DEFB $03                ; Place 16 (rocket part 5's): a place from group 3
  DEFB $03                ; Place 17 (rocket part 6's): a place from group 3
dealing_list_last:
  DEFB $04                ; Place 18 (rocket part 7's): a place from group 4;
                          ; the last, dealt again at the end of a planet
                          ; (planet_secured)

; The landscape map
;
; The world: 96 columns by 28 rows of map cells, a byte each, a row after
; another (96 bytes a row). Each byte is the number of a tile, 0-$A9, which
; picks the tile's record through tile_table. A cell is a page the size of the
; play area; the view scrolls across the pages, so it straddles up to four of
; them, two across and two down, from the map position (a column and a row kept
; as a word by load_map_window) -- load_map_window collects their four tile
; numbers and draw_landscape draws them. Tiles turned upside down (see
; tile_records) give the caverns their ceilings.
;
; map_cell_address finds a cell: row R (1-28) is at this table plus (R-1)*96.
; Row 0, the sky, is not stored: read_map_cell gives tile 0 for it. Below the
; last row load_map_window uses tile $20: for the lower cells when the map
; position is on row 28, and for all four when it is on row 29. Columns wrap:
; column -1 is 95 and column 96 is 0, so the world is a ring.
;
; Tiles $00, $20, $2A and $A6 have no pieces, so a cell with one draws nothing:
; between them they fill most of the map. Laid out below a line per map row,
; from the game's bytes when the disassembly is built: rows 1-11 are the
; surface, in the planet's first colour, and rows 12-28 the caverns, in its
; second (set_landscape_colours).
landscape_map:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; Row 1, the surface: 0
  DEFB $00,$00,$00,$00,$A6,$A6,$A6,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; pieces
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$A6,$A6,$A6,$A6,$A6,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; Row 2, the surface: 0
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$00,$00,$00,$00,$A6,$A6,$A6,$00,$00 ; pieces
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$A6,$A6,$A6,$A6,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$A6,$A6,$00,$00,$00,$00 ; Row 3, the surface: 0
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; pieces
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$A6 ;
  DEFB $A6,$A6,$A6,$A6,$A6,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$09,$0A,$00,$A6 ; Row 4, the surface: 4
  DEFB $A6,$A6,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$07,$0A ; pieces
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$A6,$A6,$A6,$A6,$A6,$A6,$A6 ;
  DEFB $A6,$A6,$A6,$A6,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$08,$2B,$23,$0D ; Row 5, the surface: 12
  DEFB $0E,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$0D,$15,$2A,$2B ; pieces
  DEFB $23,$00,$A6,$A6,$A6,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$18,$19,$0A,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$A6,$A6,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$05,$2A,$2A,$3C,$0C ; Row 6, the surface: 13
  DEFB $2F,$00,$00,$00,$00,$00,$00,$00,$00,$00,$A6,$A6 ; of the 96 cells have
  DEFB $A6,$A6,$A6,$A6,$00,$00,$00,$00,$14,$2A,$2A,$2A ; pieces
  DEFB $36,$27,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$13,$17,$2A,$2B,$27,$00 ;
  DEFB $00,$A6,$A6,$A6,$A6,$00,$00,$0D,$0E,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$07,$04,$2A,$2A,$2A,$2A,$2A ; Row 7, the surface: 16
  DEFB $2A,$31,$16,$16,$37,$00,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$A6,$00,$09,$0A,$00,$00,$05,$2A,$2A,$2A,$2A ; pieces
  DEFB $2A,$2A,$3A,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$12,$10,$2A,$2A,$2A,$2A,$24 ;
  DEFB $23,$00,$00,$00,$00,$00,$00,$14,$2F,$00,$00,$00 ;
  DEFB $00,$A6,$A6,$A6,$A6,$A6,$A6,$A6,$A6,$A6,$A6,$A6 ;
  DEFB $A6,$A6,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$A6,$00,$00,$06,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; Row 8, the surface: 16
  DEFB $2A,$2A,$2A,$2A,$36,$33,$00,$00,$00,$00,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$00,$08,$2B,$38,$11,$2A,$2A,$2A,$2A,$2A ; pieces
  DEFB $2A,$2A,$2B,$33,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$06,$2A,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $36,$33,$00,$00,$00,$00,$05,$2A,$2A,$31,$16,$32 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$A6,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$05,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; Row 9, the surface: 18
  DEFB $2A,$2A,$2A,$2A,$2A,$30,$16,$16,$16,$37,$00,$00 ; of the 96 cells have
  DEFB $00,$00,$06,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; pieces
  DEFB $2A,$2A,$2A,$30,$16,$37,$00,$00,$A6,$A6,$A6,$00 ;
  DEFB $00,$00,$03,$04,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $2A,$3B,$00,$00,$00,$1B,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $24,$27,$00,$00,$00,$00,$18,$32,$00,$00,$00,$00 ;
  DEFB $00,$00,$A6,$A6,$A6,$A6,$A6,$00,$00,$00,$00,$00 ;
  DEFB $00,$03,$04,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; Row 10, the surface:
  DEFB $2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$36,$33,$00 ; 23 of the 96 cells
  DEFB $00,$05,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; have pieces
  DEFB $2A,$2A,$2A,$2A,$2A,$36,$27,$00,$00,$00,$00,$12 ;
  DEFB $16,$01,$02,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $2A,$2F,$00,$00,$13,$0B,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $2A,$2A,$25,$00,$00,$13,$17,$2A,$31,$16,$16,$32 ;
  DEFB $00,$00,$00,$00,$13,$0A,$00,$00,$00,$00,$00,$00 ;
  DEFB $01,$02,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; Row 11, the surface:
  DEFB $2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$30,$1A ; 27 of the 96 cells
  DEFB $11,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ; have pieces
  DEFB $2A,$2A,$2A,$2A,$2A,$2A,$2A,$31,$A7,$A8,$11,$2A ;
  DEFB $2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $2A,$2A,$31,$A9,$10,$2A,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $2A,$2A,$2A,$31,$1A,$10,$2A,$2A,$2A,$2A,$2A,$2A ;
  DEFB $24,$38,$A7,$A8,$10,$2B,$38,$39,$16,$1A,$16,$1A ;
  DEFB $3E,$3E,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; Row 12, the caverns:
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$3E,$3E,$5A ; 35 of the 96 cells
  DEFB $3E,$3E,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; have pieces
  DEFB $20,$20,$20,$20,$20,$20,$3E,$3E,$40,$A5,$3E,$3E ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$3E,$3E,$5A,$3E,$3E,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$3E,$3E,$5A,$3E,$3E,$20,$20,$20,$20,$20 ;
  DEFB $3E,$3E,$40,$A5,$3E,$3E,$3E,$5A,$3E,$5A,$3E,$5A ;
  DEFB $91,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; Row 13, the caverns:
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$41 ; 17 of the 96 cells
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; have pieces
  DEFB $20,$20,$20,$20,$46,$91,$20,$20,$42,$41,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$41,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$41,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$42,$8D,$49,$A2,$20,$8D,$91,$8D,$91,$8D ;
  DEFB $92,$62,$49,$4C,$93,$48,$48,$49,$4C,$4C,$93,$48 ; Row 14, the caverns:
  DEFB $49,$4A,$20,$20,$4E,$4C,$4A,$20,$20,$20,$20,$5D ; 41 of the 96 cells
  DEFB $9C,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; have pieces
  DEFB $20,$20,$20,$67,$5B,$28,$A3,$4F,$57,$5C,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$44,$A4,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$44,$A4,$20,$20,$20,$20,$20,$20,$4E ;
  DEFB $93,$3D,$4B,$20,$36,$A1,$51,$20,$9D,$51,$28,$73 ;
  DEFB $51,$20,$22,$60,$17,$20,$20,$22,$21,$19,$17,$4E ; Row 15, the caverns:
  DEFB $4D,$4B,$20,$96,$00,$18,$4B,$20,$20,$20,$96,$64 ; 47 of the 96 cells
  DEFB $6D,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; have pieces
  DEFB $20,$4E,$4F,$56,$50,$20,$31,$16,$4B,$5D,$9C,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$7E ;
  DEFB $87,$87,$6A,$00,$A3,$A2,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$4E,$4C,$4D,$37,$A3,$4A,$20,$20,$4E,$4F,$07 ;
  DEFB $17,$20,$20,$20,$20,$97,$57,$20,$A0,$57,$20,$9F ;
  DEFB $57,$20,$20,$5D,$8F,$20,$20,$20,$94,$4C,$4F,$07 ; Row 16, the caverns:
  DEFB $17,$20,$20,$63,$52,$17,$20,$20,$20,$20,$63,$12 ; 55 of the 96 cells
  DEFB $32,$6E,$20,$20,$20,$20,$20,$20,$20,$20,$20,$44 ; have pieces
  DEFB $6C,$52,$04,$2B,$38,$1E,$49,$4A,$74,$64,$6D,$20 ;
  DEFB $20,$20,$7D,$20,$20,$20,$20,$20,$20,$20,$67,$85 ;
  DEFB $84,$83,$00,$12,$19,$0A,$73,$20,$20,$20,$20,$4E ;
  DEFB $4F,$12,$19,$17,$36,$A1,$57,$20,$96,$52,$04,$20 ;
  DEFB $20,$20,$4E,$93,$9E,$0E,$50,$20,$A0,$50,$20,$63 ;
  DEFB $57,$94,$4F,$00,$90,$20,$20,$20,$97,$03,$04,$20 ; Row 17, the caverns:
  DEFB $20,$65,$66,$56,$61,$54,$93,$48,$48,$9E,$15,$20 ; 63 of the 96 cells
  DEFB $20,$9F,$6F,$8F,$20,$20,$20,$20,$20,$20,$4E,$4D ; have pieces
  DEFB $32,$50,$20,$20,$20,$20,$45,$1C,$7B,$1F,$3F,$73 ;
  DEFB $20,$74,$53,$54,$4A,$20,$20,$94,$4C,$4F,$07,$17 ;
  DEFB $20,$7B,$58,$20,$20,$2B,$A1,$A3,$4A,$20,$96,$00 ;
  DEFB $75,$20,$20,$20,$20,$A0,$57,$20,$59,$61,$54,$93 ;
  DEFB $48,$3D,$19,$17,$20,$2F,$00,$51,$30,$32,$8C,$64 ;
  DEFB $4B,$95,$16,$16,$4B,$20,$20,$96,$52,$17,$20,$4E ; Row 18, the caverns:
  DEFB $4F,$07,$04,$2B,$38,$01,$02,$20,$20,$20,$20,$20 ; 57 of the 96 cells
  DEFB $20,$63,$64,$70,$20,$20,$20,$20,$94,$4F,$13,$17 ; have pieces
  DEFB $20,$35,$62,$91,$20,$20,$63,$0F,$7C,$08,$28,$00 ;
  DEFB $A3,$4D,$37,$18,$4B,$20,$20,$95,$16,$11,$20,$20 ;
  DEFB $20,$7C,$1C,$20,$20,$20,$95,$1A,$4B,$20,$95,$3F ;
  DEFB $90,$20,$20,$20,$20,$63,$57,$20,$36,$38,$19,$17 ;
  DEFB $20,$20,$20,$20,$20,$20,$9F,$1C,$20,$20,$9F,$1F ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$3B,$61,$54,$4F,$03 ; Row 19, the caverns:
  DEFB $04,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; 37 of the 96 cells
  DEFB $67,$12,$32,$71,$20,$20,$20,$20,$95,$1A,$4B,$20 ; have pieces
  DEFB $20,$20,$20,$28,$A3,$4F,$05,$67,$58,$20,$20,$31 ;
  DEFB $19,$17,$45,$90,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $96,$00,$0F,$20,$20,$20,$20,$41,$20,$20,$20,$28 ;
  DEFB $8B,$72,$20,$20,$67,$12,$4B,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$97,$0F,$20,$20,$63,$08 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$2F,$00,$56,$52,$17 ; Row 20, the caverns:
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$65,$66 ; 38 of the 96 cells
  DEFB $05,$20,$20,$26,$68,$72,$20,$20,$20,$8D,$49,$4C ; have pieces
  DEFB $4C,$4A,$20,$20,$31,$11,$7A,$56,$50,$20,$20,$20 ;
  DEFB $20,$20,$A0,$50,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $97,$1B,$20,$20,$20,$20,$20,$5D,$8F,$20,$20,$20 ;
  DEFB $24,$38,$1E,$3D,$11,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$46,$3D,$11,$4E,$93,$9E,$15,$20 ;
  DEFB $94,$93,$48,$49,$4A,$20,$20,$20,$55,$99,$61,$54 ; Row 21, the caverns:
  DEFB $93,$91,$20,$20,$6B,$9B,$20,$20,$20,$96,$18,$11 ; 51 of the 96 cells
  DEFB $20,$20,$20,$20,$24,$23,$6E,$20,$20,$20,$36,$23 ; have pieces
  DEFB $00,$79,$20,$94,$4C,$4F,$06,$2B,$A1,$51,$20,$20 ;
  DEFB $20,$20,$95,$32,$73,$20,$20,$20,$20,$20,$7E,$82 ;
  DEFB $07,$0B,$20,$94,$4C,$4C,$4F,$12,$98,$A2,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$46,$48,$9E,$47,$65,$66,$07,$17,$20,$20,$20 ;
  DEFB $95,$98,$4C,$4D,$4B,$20,$20,$20,$96,$00,$12,$19 ; Row 22, the caverns:
  DEFB $17,$28,$68,$69,$6A,$53,$54,$4A,$20,$97,$90,$20 ; 49 of the 96 cells
  DEFB $20,$20,$20,$20,$20,$45,$64,$73,$20,$20,$20,$22 ; have pieces
  DEFB $60,$17,$20,$95,$16,$11,$20,$20,$63,$50,$20,$20 ;
  DEFB $20,$20,$20,$20,$26,$6E,$20,$20,$20,$67,$07,$04 ;
  DEFB $20,$20,$20,$63,$12,$16,$11,$20,$36,$A1,$A3,$4A ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$46 ;
  DEFB $9E,$47,$20,$4E,$4F,$52,$04,$20,$20,$20,$20,$20 ;
  DEFB $20,$22,$60,$17,$20,$20,$20,$20,$97,$58,$20,$20 ; Row 23, the caverns:
  DEFB $20,$20,$31,$37,$00,$00,$18,$4B,$96,$18,$4B,$20 ; 39 of the 96 cells
  DEFB $20,$20,$20,$20,$20,$63,$12,$32,$6E,$20,$20,$20 ; have pieces
  DEFB $5D,$9C,$20,$20,$20,$20,$20,$96,$00,$00,$73,$20 ;
  DEFB $20,$20,$20,$20,$20,$25,$A3,$4C,$4F,$06,$20,$20 ;
  DEFB $4E,$93,$3D,$11,$20,$20,$7E,$7F,$20,$95,$37,$50 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$4E,$4F,$08 ;
  DEFB $20,$20,$67,$00,$00,$50,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$5C,$20,$20,$20,$65,$66,$56,$50,$20,$20 ; Row 24, the caverns:
  DEFB $20,$20,$20,$36,$38,$19,$17,$20,$A0,$90,$20,$20 ; 44 of the 96 cells
  DEFB $20,$20,$46,$48,$9E,$15,$20,$20,$35,$62,$49,$4F ; have pieces
  DEFB $64,$6D,$20,$20,$20,$20,$20,$95,$37,$00,$00,$51 ;
  DEFB $20,$20,$20,$20,$20,$20,$3A,$00,$58,$20,$20,$74 ;
  DEFB $13,$17,$20,$20,$4E,$4F,$56,$00,$73,$20,$80,$00 ;
  DEFB $A3,$A2,$20,$20,$20,$20,$20,$20,$96,$00,$05,$20 ;
  DEFB $5F,$3D,$32,$00,$00,$00,$A3,$93,$91,$20,$20,$20 ;
  DEFB $20,$20,$5D,$8F,$44,$6C,$03,$04,$2B,$38,$1E,$48 ; Row 25, the caverns:
  DEFB $91,$20,$20,$20,$20,$20,$20,$20,$A0,$57,$20,$20 ; 49 of the 96 cells
  DEFB $4E,$4F,$5B,$20,$20,$20,$20,$20,$20,$20,$36,$38 ; have pieces
  DEFB $16,$32,$73,$20,$20,$20,$20,$20,$45,$00,$12,$10 ;
  DEFB $20,$20,$20,$20,$20,$20,$2B,$38,$4B,$20,$4E,$4D ;
  DEFB $10,$20,$20,$67,$18,$11,$2B,$27,$00,$A3,$4D,$32 ;
  DEFB $00,$00,$51,$20,$20,$20,$20,$20,$3B,$58,$20,$20 ;
  DEFB $20,$20,$20,$24,$A1,$1F,$19,$17,$28,$A3,$4A,$20 ;
  DEFB $20,$7A,$9A,$2E,$6A,$03,$17,$20,$20,$20,$20,$20 ; Row 26, the caverns:
  DEFB $9D,$51,$20,$20,$20,$20,$20,$20,$95,$4B,$20,$96 ; 43 of the 96 cells
  DEFB $00,$56,$50,$20,$20,$20,$20,$20,$20,$20,$5F,$49 ; have pieces
  DEFB $A2,$20,$78,$51,$20,$20,$20,$20,$95,$11,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$65,$66,$07,$17 ;
  DEFB $4E,$4C,$4F,$07,$17,$20,$20,$20,$31,$19,$17,$20 ;
  DEFB $31,$16,$4B,$20,$20,$20,$20,$20,$2F,$61,$54,$A2 ;
  DEFB $20,$20,$20,$20,$97,$08,$20,$20,$20,$26,$50,$20 ;
  DEFB $3D,$19,$0B,$31,$19,$17,$20,$20,$20,$20,$20,$20 ; Row 27, the caverns:
  DEFB $95,$4B,$20,$20,$20,$20,$20,$20,$20,$20,$20,$95 ; 36 of the 96 cells
  DEFB $11,$2B,$A1,$51,$20,$20,$20,$20,$20,$20,$20,$36 ; have pieces
  DEFB $A1,$76,$77,$57,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$96,$88,$89,$8A,$66 ;
  DEFB $12,$19,$04,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$24,$27,$64 ;
  DEFB $A3,$4C,$93,$9E,$15,$20,$20,$20,$20,$20,$35,$62 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; Row 28, the caverns:
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ; 15 of the 96 cells
  DEFB $20,$20,$95,$4B,$20,$20,$20,$20,$20,$20,$20,$20 ; have pieces
  DEFB $95,$16,$16,$4B,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$95,$16,$16,$16,$11 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;
  DEFB $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$31 ;
  DEFB $16,$19,$17,$20,$20,$20,$20,$20,$20,$20,$20,$20 ;

; The landscape tiles
;
; A word for each of the 171 tiles, $00-$AA: the address of its record in
; tile_records. Tile $AA is never in the map: load_map_window draws it instead
; of tile $3E while map_rebuild has bit 7 set.
;
; Many tiles are other tiles turned upside down or mirrored left-right, and
; their records share bytes: the address of such a tile is one, two or three
; bytes before the plain tile's, where the markers that say so lie (see
; tile_records).
;
; Laid out below a word per tile, each with how the tile reads its record and
; how often the map uses it, from the game's bytes when the disassembly is
; built.
tile_table:
  DEFW tile_records       ; Tile $00: no pieces; in the map 671 times
  DEFW $6DDE              ; Tile $01: 3 pieces; in the map 3 times
  DEFW $6DEC              ; Tile $02: 1 piece; in the map 3 times
  DEFW $6DF1              ; Tile $03: 2 pieces; in the map 6 times
  DEFW $6DFB              ; Tile $04: 3 pieces; in the map 12 times
  DEFW $6E0A              ; Tile $05: 2 pieces; in the map 8 times
  DEFW $6E14              ; Tile $06: 3 pieces; in the map 5 times
  DEFW $6E23              ; Tile $07: 3 pieces; in the map 11 times
  DEFW $6E30              ; Tile $08: 2 pieces; in the map 6 times
  DEFW $6E38              ; Tile $09: 1 piece; in the map 2 times
  DEFW $6E3D              ; Tile $0A: 3 pieces; in the map 6 times
  DEFW $6E4A              ; Tile $0B: 1 piece; in the map 3 times
  DEFW $6E4F              ; Tile $0C: 3 pieces; in the map 1 time
  DEFW $6E5D              ; Tile $0D: 1 piece; in the map 3 times
  DEFW $6E61              ; Tile $0E: 4 pieces; in the map 3 times
  DEFW $6E73              ; Tile $0F: 1 piece; in the map 3 times
  DEFW $6E7A              ; Tile $10: 2 pieces; in the map 6 times
  DEFW $6E86              ; Tile $11: 2 pieces; in the map 15 times
  DEFW $6E92              ; Tile $12: 2 pieces; in the map 13 times
  DEFW $6E9D              ; Tile $13: 2 pieces; in the map 6 times
  DEFW $6EA7              ; Tile $14: 4 pieces; in the map 2 times
  DEFW $6EB9              ; Tile $15: 3 pieces; in the map 5 times
  DEFW $6EC6              ; Tile $16: 1 piece; in the map 26 times
  DEFW $6ECF              ; Tile $17: 1 piece; in the map 28 times
  DEFW $6ED4              ; Tile $18: 2 pieces; in the map 8 times
  DEFW $6EE0              ; Tile $19: 2 pieces; in the map 15 times
  DEFW $6EF4              ; Tile $1A: 4 pieces; in the map 6 times
  DEFW $6F06              ; Tile $1B: 3 pieces; in the map 2 times
  DEFW $6F15              ; Tile $1C: 1 piece; in the map 3 times
  DEFW $6F1A              ; Tile $1D: 2 pieces; not in the map
  DEFW $6F23              ; Tile $1E: 3 pieces; in the map 3 times
  DEFW $6F32              ; Tile $1F: 3 pieces; in the map 3 times
  DEFW tile_records       ; Tile $20: no pieces; in the map 885 times
  DEFW $6DDD              ; Tile $21: 3 pieces, mirrored left-right (the pieces
                          ; of tile $01); in the map 1 time
  DEFW $6DEB              ; Tile $22: 1 piece, mirrored left-right (the pieces
                          ; of tile $02); in the map 4 times
  DEFW $6DF0              ; Tile $23: 2 pieces, mirrored left-right (the pieces
                          ; of tile $03); in the map 5 times
  DEFW $6DF9              ; Tile $24: 3 pieces, mirrored left-right (the pieces
                          ; of tile $04); in the map 7 times
  DEFW $6E08              ; Tile $25: 2 pieces, mirrored left-right (the pieces
                          ; of tile $05); in the map 2 times
  DEFW $6E12              ; Tile $26: 3 pieces, mirrored left-right (the pieces
                          ; of tile $06); in the map 3 times
  DEFW $6E21              ; Tile $27: 3 pieces, mirrored left-right (the pieces
                          ; of tile $07); in the map 6 times
  DEFW $6E2F              ; Tile $28: 2 pieces, mirrored left-right (the pieces
                          ; of tile $08); in the map 7 times
  DEFW $6E37              ; Tile $29: 1 piece, mirrored left-right (the pieces
                          ; of tile $09); not in the map
  DEFW tile_records       ; Tile $2A: no pieces; in the map 211 times
  DEFW $6E49              ; Tile $2B: 1 piece, mirrored left-right (the pieces
                          ; of tile $0B); in the map 14 times
  DEFW $6E4E              ; Tile $2C: 3 pieces, mirrored left-right (the pieces
                          ; of tile $0C); not in the map
  DEFW $6E5C              ; Tile $2D: 1 piece, mirrored left-right (the pieces
                          ; of tile $0D); not in the map
  DEFW $6E77              ; Tile $2E: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $10); in the map 1
                          ; time
  DEFW $6E71              ; Tile $2F: 1 piece, mirrored left-right (the pieces
                          ; of tile $0F); in the map 6 times
  DEFW $6E78              ; Tile $30: 2 pieces, mirrored left-right (the pieces
                          ; of tile $10); in the map 4 times
  DEFW $6E84              ; Tile $31: 2 pieces, mirrored left-right (the pieces
                          ; of tile $11); in the map 14 times
  DEFW $6E90              ; Tile $32: 2 pieces, mirrored left-right (the pieces
                          ; of tile $12); in the map 12 times
  DEFW $6E9B              ; Tile $33: 2 pieces, mirrored left-right (the pieces
                          ; of tile $13); in the map 4 times
  DEFW $6EA5              ; Tile $34: 4 pieces, mirrored left-right (the pieces
                          ; of tile $14); not in the map
  DEFW $6EB7              ; Tile $35: 3 pieces, mirrored left-right (the pieces
                          ; of tile $15); in the map 3 times
  DEFW $6ECD              ; Tile $36: 1 piece, mirrored left-right (the pieces
                          ; of tile $17); in the map 13 times
  DEFW $6ED3              ; Tile $37: 2 pieces, mirrored left-right (the pieces
                          ; of tile $18); in the map 8 times
  DEFW $6EDE              ; Tile $38: 2 pieces, mirrored left-right (the pieces
                          ; of tile $19); in the map 11 times
  DEFW $6EF3              ; Tile $39: 4 pieces, mirrored left-right (the pieces
                          ; of tile $1A); in the map 1 time
  DEFW $6F05              ; Tile $3A: 3 pieces, mirrored left-right (the pieces
                          ; of tile $1B); in the map 2 times
  DEFW $6F13              ; Tile $3B: 1 piece, mirrored left-right (the pieces
                          ; of tile $1C); in the map 3 times
  DEFW $6F19              ; Tile $3C: 2 pieces, mirrored left-right (the pieces
                          ; of tile $1D); in the map 1 time
  DEFW $6F22              ; Tile $3D: 3 pieces, mirrored left-right (the pieces
                          ; of tile $1E); in the map 7 times
  DEFW $6DA2              ; Tile $3E: 6 pieces; in the map 25 times
  DEFW $6F31              ; Tile $3F: 3 pieces, mirrored left-right (the pieces
                          ; of tile $1F); in the map 2 times
  DEFW $6DC1              ; Tile $40: 4 pieces; in the map 2 times
  DEFW $6F3E              ; Tile $41: 4 pieces; in the map 5 times
  DEFW $6DD0              ; Tile $42: 4 pieces; in the map 2 times
  DEFW $6F5E              ; Tile $43: 4 pieces; not in the map
  DEFW $6F6C              ; Tile $44: 1 piece; in the map 4 times
  DEFW $6F73              ; Tile $45: 2 pieces; in the map 4 times
  DEFW $6F7B              ; Tile $46: 5 pieces; in the map 5 times
  DEFW $6F90              ; Tile $47: 2 pieces; in the map 2 times
  DEFW $6F97              ; Tile $48: 2 pieces; in the map 10 times
  DEFW $6FA3              ; Tile $49: 3 pieces; in the map 9 times
  DEFW $6FB4              ; Tile $4A: 4 pieces; in the map 11 times
  DEFW $6FC5              ; Tile $4B: 3 pieces; in the map 19 times
  DEFW $6FD2              ; Tile $4C: 1 piece; in the map 16 times
  DEFW $6FD9              ; Tile $4D: 5 pieces; in the map 7 times
  DEFW $6FEE              ; Tile $4E: 3 pieces; in the map 18 times
  DEFW $6FFB              ; Tile $4F: 2 pieces; in the map 20 times
  DEFW $7005              ; Tile $50: 2 pieces; in the map 12 times
  DEFW $700F              ; Tile $51: 3 pieces; in the map 10 times
  DEFW $701B              ; Tile $52: 2 pieces; in the map 6 times
  DEFW $7022              ; Tile $53: 1 piece; in the map 2 times
  DEFW $7026              ; Tile $54: 1 piece; in the map 7 times
  DEFW $702D              ; Tile $55: 3 pieces; in the map 1 time
  DEFW $703B              ; Tile $56: 3 pieces; in the map 7 times
  DEFW $7049              ; Tile $57: 1 piece; in the map 10 times
  DEFW $704F              ; Tile $58: 2 pieces; in the map 5 times
  DEFW $6F70              ; Tile $59: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $45); in the map 1
                          ; time
  DEFW $6D95              ; Tile $5A: 4 pieces; in the map 6 times
  DEFW $7057              ; Tile $5B: 3 pieces; in the map 2 times
  DEFW $7061              ; Tile $5C: 2 pieces; in the map 2 times
  DEFW $7069              ; Tile $5D: 4 pieces; in the map 6 times
  DEFW $7076              ; Tile $5E: 4 pieces; not in the map
  DEFW $7084              ; Tile $5F: 5 pieces; in the map 2 times
  DEFW $7098              ; Tile $60: 4 pieces; in the map 3 times
  DEFW $6DEA              ; Tile $61: 1 piece, upside down and mirrored
                          ; left-right (the pieces of tile $02); in the map 5
                          ; times
  DEFW $70A6              ; Tile $62: 5 pieces; in the map 4 times
  DEFW $70BA              ; Tile $63: 2 pieces; in the map 10 times
  DEFW $70C1              ; Tile $64: 6 pieces; in the map 7 times
  DEFW $6E22              ; Tile $65: 3 pieces, upside down (the pieces of tile
                          ; $07); in the map 5 times
  DEFW $6DFA              ; Tile $66: 3 pieces, upside down (the pieces of tile
                          ; $04); in the map 6 times
  DEFW $6E09              ; Tile $67: 2 pieces, upside down (the pieces of tile
                          ; $05); in the map 8 times
  DEFW $6DF8              ; Tile $68: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $04); in the map 2
                          ; times
  DEFW $6EDD              ; Tile $69: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $19); in the map 1
                          ; time
  DEFW $6E79              ; Tile $6A: 2 pieces, upside down (the pieces of tile
                          ; $10); in the map 3 times
  DEFW $6E9C              ; Tile $6B: 2 pieces, upside down (the pieces of tile
                          ; $13); in the map 1 time
  DEFW $6EB8              ; Tile $6C: 3 pieces, upside down (the pieces of tile
                          ; $15); in the map 2 times
  DEFW $6EA4              ; Tile $6D: 4 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $14); in the map 3
                          ; times
  DEFW $6E11              ; Tile $6E: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $06); in the map 4
                          ; times
  DEFW $6EB6              ; Tile $6F: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $15); in the map 1
                          ; time
  DEFW $6F12              ; Tile $70: 1 piece, upside down and mirrored
                          ; left-right (the pieces of tile $1C); in the map 1
                          ; time
  DEFW $6E70              ; Tile $71: 1 piece, upside down and mirrored
                          ; left-right (the pieces of tile $0F); in the map 1
                          ; time
  DEFW $6E20              ; Tile $72: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $07); in the map 2
                          ; times
  DEFW $6E07              ; Tile $73: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $05); in the map 8
                          ; times
  DEFW $704E              ; Tile $74: 2 pieces, upside down (the pieces of tile
                          ; $58); in the map 3 times
  DEFW $700C              ; Tile $75: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $51); in the map 1
                          ; time
  DEFW $6E83              ; Tile $76: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $11); in the map 1
                          ; time
  DEFW $6FC4              ; Tile $77: 3 pieces, upside down (the pieces of tile
                          ; $4B); in the map 1 time
  DEFW $700E              ; Tile $78: 3 pieces, upside down (the pieces of tile
                          ; $51); in the map 1 time
  DEFW $6F72              ; Tile $79: 2 pieces, upside down (the pieces of tile
                          ; $45); in the map 1 time
  DEFW $6E13              ; Tile $7A: 3 pieces, upside down (the pieces of tile
                          ; $06); in the map 2 times
  DEFW $6F14              ; Tile $7B: 1 piece, upside down (the pieces of tile
                          ; $1C); in the map 2 times
  DEFW $6E72              ; Tile $7C: 1 piece, upside down (the pieces of tile
                          ; $0F); in the map 2 times
  DEFW $703A              ; Tile $7D: 3 pieces, upside down (the pieces of tile
                          ; $56); in the map 1 time
  DEFW $6E91              ; Tile $7E: 2 pieces, upside down (the pieces of tile
                          ; $12); in the map 3 times
  DEFW $6E8F              ; Tile $7F: 2 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $12); in the map 1
                          ; time
  DEFW $7048              ; Tile $80: 1 piece, upside down (the pieces of tile
                          ; $57); in the map 1 time
  DEFW $701A              ; Tile $81: 2 pieces, upside down (the pieces of tile
                          ; $52); not in the map
  DEFW $6E85              ; Tile $82: 2 pieces, upside down (the pieces of tile
                          ; $11); in the map 1 time
  DEFW $6FB3              ; Tile $83: 4 pieces, upside down (the pieces of tile
                          ; $4A); in the map 1 time
  DEFW $6FD1              ; Tile $84: 1 piece, upside down (the pieces of tile
                          ; $4C); in the map 1 time
  DEFW $6FB1              ; Tile $85: 4 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $4A); in the map 1
                          ; time
  DEFW $6ECE              ; Tile $86: 1 piece, upside down (the pieces of tile
                          ; $17); not in the map
  DEFW $6EC5              ; Tile $87: 1 piece, upside down (the pieces of tile
                          ; $16); in the map 2 times
  DEFW $7083              ; Tile $88: 5 pieces, upside down (the pieces of tile
                          ; $5F); in the map 1 time
  DEFW $6F21              ; Tile $89: 3 pieces, upside down and mirrored
                          ; left-right (the pieces of tile $1E); in the map 1
                          ; time
  DEFW $6EDF              ; Tile $8A: 2 pieces, upside down (the pieces of tile
                          ; $19); in the map 1 time
  DEFW $6ECC              ; Tile $8B: 1 piece, upside down and mirrored
                          ; left-right (the pieces of tile $17); in the map 1
                          ; time
  DEFW $702C              ; Tile $8C: 3 pieces, upside down (the pieces of tile
                          ; $55); in the map 1 time
  DEFW $6F4B              ; Tile $8D: 4 pieces, mirrored left-right; in the map
                          ; 5 times
  DEFW $6F5D              ; Tile $8E: 4 pieces, mirrored left-right (the pieces
                          ; of tile $43); not in the map
  DEFW $6F6B              ; Tile $8F: 1 piece, mirrored left-right (the pieces
                          ; of tile $44); in the map 4 times
  DEFW $6F71              ; Tile $90: 2 pieces, mirrored left-right (the pieces
                          ; of tile $45); in the map 5 times
  DEFW $6F7A              ; Tile $91: 5 pieces, mirrored left-right (the pieces
                          ; of tile $46); in the map 8 times
  DEFW $6F8F              ; Tile $92: 2 pieces, mirrored left-right (the pieces
                          ; of tile $47); in the map 1 time
  DEFW $6FA2              ; Tile $93: 3 pieces, mirrored left-right (the pieces
                          ; of tile $49); in the map 12 times
  DEFW $6FB2              ; Tile $94: 4 pieces, mirrored left-right (the pieces
                          ; of tile $4A); in the map 7 times
  DEFW $6FC3              ; Tile $95: 3 pieces, mirrored left-right (the pieces
                          ; of tile $4B); in the map 17 times
  DEFW $700D              ; Tile $96: 3 pieces, mirrored left-right (the pieces
                          ; of tile $51); in the map 13 times
  DEFW $7004              ; Tile $97: 2 pieces, mirrored left-right (the pieces
                          ; of tile $50); in the map 7 times
  DEFW $6FD8              ; Tile $98: 5 pieces, mirrored left-right (the pieces
                          ; of tile $4D); in the map 2 times
  DEFW $6EA6              ; Tile $99: 4 pieces, upside down (the pieces of tile
                          ; $14); in the map 1 time
  DEFW $7039              ; Tile $9A: 3 pieces, mirrored left-right (the pieces
                          ; of tile $56); in the map 1 time
  DEFW $6E3C              ; Tile $9B: 3 pieces, upside down (the pieces of tile
                          ; $0A); in the map 1 time
  DEFW $6E5B              ; Tile $9C: 1 piece, upside down and mirrored
                          ; left-right (the pieces of tile $0D); in the map 3
                          ; times
  DEFW $7056              ; Tile $9D: 3 pieces, mirrored left-right (the pieces
                          ; of tile $5B); in the map 2 times
  DEFW $70A5              ; Tile $9E: 5 pieces, mirrored left-right (the pieces
                          ; of tile $62); in the map 7 times
  DEFW $704D              ; Tile $9F: 2 pieces, mirrored left-right (the pieces
                          ; of tile $58); in the map 4 times
  DEFW $7047              ; Tile $A0: 1 piece, mirrored left-right (the pieces
                          ; of tile $57); in the map 6 times
  DEFW $7019              ; Tile $A1: 2 pieces, mirrored left-right (the pieces
                          ; of tile $52); in the map 8 times
  DEFW $6FED              ; Tile $A2: 3 pieces, mirrored left-right (the pieces
                          ; of tile $4E); in the map 6 times
  DEFW $6FFA              ; Tile $A3: 2 pieces, mirrored left-right (the pieces
                          ; of tile $4F); in the map 13 times
  DEFW $7068              ; Tile $A4: 4 pieces, mirrored left-right (the pieces
                          ; of tile $5D); in the map 2 times
  DEFW $6DC0              ; Tile $A5: 4 pieces, mirrored left-right (the pieces
                          ; of tile $40); in the map 2 times
  DEFW $6D93              ; Tile $A6: no pieces; in the map 76 times
  DEFW $6EEA              ; Tile $A7: 2 pieces; in the map 2 times
  DEFW $6EE9              ; Tile $A8: 2 pieces, mirrored left-right (the pieces
                          ; of tile $A7); in the map 2 times
  DEFW $6EF4              ; Tile $A9: 4 pieces; in the map 1 time
  DEFW $6DB9              ; Tile $AA: 2 pieces; not in the map

; The landscape tiles' records
;
; Each tile is drawn by draw_landscape as a list of pieces of ground, each a
; line of steps of one piece graphic. A record:
;
; Up to three optional markers first. $99: upside down -- floors become
; ceilings: each piece's direction is swapped through the second half of the
; table at mirror_table (0 and 7, 1 and 8 ... 6 and 13 swap places, 14 and 15
; swap), its y is turned over (XOR $0C) and its extra bytes XOR 1. $FF:
; mirrored left-right -- each piece's x is reflected, its two extra bytes are
; swapped (XOR 2) and its direction turned round through the table's first
; half. Then any byte of $95 or more is padding, skipped. Then the number of
; pieces; a record that is just 0 has none.
;
; Each piece: its x and y in the page; a byte with the direction in bits 4-7
; and the number of steps in bits 0-3 (0 means 16); and, only for directions 0
; and 7, two extra bytes (piece_extra_a, piece_extra_b). The direction picks
; three bytes from piece_shapes by set_piece_shape: the step in x, the step in
; y, and a third that is 0 for directions 0 and 7 (the flat pieces) and 8 or 16
; for the others. Directions 7 to 13 are the ceiling shapes of 0 to 6 and use
; the entry seven less, with piece_ceiling set.
;
; The padding rule is what lets four tiles share a record: a plain record of n
; pieces at A can be preceded by $99 (A-1, upside down), $FF $99 (A-2, mirrored
; left-right: the $99 is padding) and $99 $FF $99 (A-3, both). So 72 records,
; one after another with nothing between, serve all 171 tiles: 74 tiles have no
; marker, 27 are upside down, 51 mirrored left-right and 19 both. Read in
; Python, every byte from here to the tune is used except one (an unused $99,
; the third byte of the block).
;
; Laid out below a line per record, from the game's bytes when the disassembly
; is built: each marker a byte of its own, so that every tile's word in
; tile_table lands on one, then the count and the pieces; the comment names the
; tiles that read the record, and how, and decodes each piece as its (x,y) in
; the page, its direction and its steps, and a flat piece's two end codes.
tile_records:
  DEFB $00                ; Tiles $00, $20 and $2A. No pieces
  DEFB $00                ; Tile $A6. No pieces
  DEFB $99                ; Tile $5A (the marker $99 at its start is read by no
  DEFB $04                ; tile). 4 pieces: (8,63) direction 9 x 5 steps;
  DEFB $08,$3F,$95        ; (224,63) direction 12 x 5 steps; (40,183) direction
  DEFB $E0,$3F,$C5        ; 5 x 5 steps; (192,183) direction 2 x 5 steps
  DEFB $28,$B7,$55        ;
  DEFB $C0,$B7,$25        ;
  DEFB $06                 ; Tile $3E. 6 pieces: (24,99) direction 7 x 8 steps,
  DEFB $18,$63,$78,$11,$13 ; ends $11 $13; (24,111) direction 0 x 8 steps, ends
  DEFB $18,$6F,$08,$11,$13 ; $11 $13; (16,95) direction 13 x 2 steps; (216,95)
  DEFB $10,$5F,$D2         ; direction 10 x 2 steps; (8,143) direction 3 x 2
  DEFB $D8,$5F,$A2         ; steps; (224,143) direction 6 x 2 steps
  DEFB $08,$8F,$32         ;
  DEFB $E0,$8F,$62         ;
  DEFB $02                ; Tile $AA. 2 pieces: (216,95) direction 10 x 2
  DEFB $D8,$5F,$A2        ; steps; (16,95) direction 13 x 2 steps
  DEFB $10,$5F,$D2        ;
  DEFB $FF                 ; Tiles $40 and $A5 mirrored left-right. 4 pieces:
  DEFB $04                 ; (8,63) direction 9 x 5 steps; (40,183) direction 5
  DEFB $08,$3F,$95         ; x 5 steps; (208,151) direction 0 x 1 step, ends
  DEFB $28,$B7,$55         ; $11 $11; (192,183) direction 3 x 2 steps
  DEFB $D0,$97,$01,$11,$11 ;
  DEFB $C0,$B7,$32         ;
  DEFB $04                ; Tile $42. 4 pieces: (40,103) direction 15 x 5
  DEFB $28,$67,$F5        ; steps; (8,183) direction 10 x 5 steps; (192,135)
  DEFB $08,$B7,$A5        ; direction 14 x 7 steps; (168,183) direction 3 x 3
  DEFB $C0,$87,$E7        ; steps
  DEFB $A8,$B7,$33        ;
  DEFB $FF                 ; Tiles $01 and $21 mirrored left-right. 3 pieces:
  DEFB $03                 ; (8,151) direction 0 x 4 steps, ends $00 $12;
  DEFB $08,$97,$04,$00,$12 ; (104,151) direction 1 x 4 steps; (168,119)
  DEFB $68,$97,$14         ; direction 2 x 8 steps
  DEFB $A8,$77,$28         ;
  DEFB $99                ; Tiles $02, $22 mirrored left-right and $61 upside
  DEFB $FF                ; down and mirrored left-right. 1 piece: (8,55)
  DEFB $01                ; direction 1 x 4 steps
  DEFB $08,$37,$14        ;
  DEFB $FF                ; Tiles $03 and $23 mirrored left-right. 2 pieces:
  DEFB $02                ; (72,183) direction 1 x 8 steps; (200,119) direction
  DEFB $48,$B7,$18        ; 2 x 4 steps
  DEFB $C8,$77,$24        ;
  DEFB $99                 ; Tiles $04, $66 upside down, $24 mirrored
  DEFB $FF                 ; left-right and $68 upside down and mirrored
  DEFB $99                 ; left-right. 3 pieces: (72,55) direction 0 x 4
  DEFB $03                 ; steps, ends $11 $12; (8,87) direction 1 x 4 steps;
  DEFB $48,$37,$04,$11,$12 ; (168,55) direction 1 x 4 steps
  DEFB $08,$57,$14         ;
  DEFB $A8,$37,$14         ;
  DEFB $99                ; Tiles $05, $67 upside down, $25 mirrored left-right
  DEFB $FF                ; and $73 upside down and mirrored left-right. 2
  DEFB $99                ; pieces: (8,183) direction 1 x 8 steps; (136,119)
  DEFB $02                ; direction 2 x 12 steps
  DEFB $08,$B7,$18        ;
  DEFB $88,$77,$2C        ;
  DEFB $99                 ; Tiles $06, $7A upside down, $26 mirrored
  DEFB $FF                 ; left-right and $6E upside down and mirrored
  DEFB $99                 ; left-right. 3 pieces: (40,119) direction 0 x 4
  DEFB $03                 ; steps, ends $11 $12; (8,183) direction 3 x 4
  DEFB $28,$77,$04,$11,$12 ; steps; (136,119) direction 2 x 12 steps
  DEFB $08,$B7,$34         ;
  DEFB $88,$77,$2C         ;
  DEFB $99                 ; Tiles $07, $65 upside down, $27 mirrored
  DEFB $FF                 ; left-right and $72 upside down and mirrored
  DEFB $99                 ; left-right. 3 pieces: (72,151) direction 0 x 4
  DEFB $03                 ; steps, ends $11 $12; (8,183) direction 1 x 4
  DEFB $48,$97,$04,$11,$12 ; steps; (168,151) direction 2 x 8 steps
  DEFB $08,$B7,$14         ;
  DEFB $A8,$97,$28         ;
  DEFB $FF                ; Tiles $08 and $28 mirrored left-right. 2 pieces:
  DEFB $02                ; (8,183) direction 2 x 12 steps; (104,87) direction
  DEFB $08,$B7,$2C        ; 3 x 4 steps
  DEFB $68,$57,$34        ;
  DEFB $FF                ; Tiles $09 and $29 mirrored left-right. 1 piece:
  DEFB $01                ; (136,183) direction 2 x 12 steps
  DEFB $88,$B7,$2C        ;
  DEFB $99                 ; Tiles $0A and $9B upside down. 3 pieces: (40,55)
  DEFB $03                 ; direction 0 x 4 steps, ends $11 $13; (8,87)
  DEFB $28,$37,$04,$11,$13 ; direction 2 x 4 steps; (192,183) direction 6 x 8
  DEFB $08,$57,$24         ; steps
  DEFB $C0,$B7,$68         ;
  DEFB $FF                ; Tiles $0B and $2B mirrored left-right. 1 piece:
  DEFB $01                ; (8,87) direction 3 x 4 steps
  DEFB $08,$57,$34        ;
  DEFB $FF                 ; Tiles $0C and $2C mirrored left-right. 3 pieces:
  DEFB $03                 ; (40,151) direction 0 x 4 steps, ends $10 $12;
  DEFB $28,$97,$04,$10,$12 ; (32,151) direction 5 x 4 steps; (136,151)
  DEFB $20,$97,$54         ; direction 3 x 8 steps
  DEFB $88,$97,$38         ;
  DEFB $99                ; Tiles $0D, $2D mirrored left-right and $9C upside
  DEFB $FF                ; down and mirrored left-right. 1 piece: (200,183)
  DEFB $01                ; direction 3 x 4 steps
  DEFB $C8,$B7,$34        ;
  DEFB $04                 ; Tile $0E. 4 pieces: (40,87) direction 0 x 2 steps,
  DEFB $28,$57,$02,$11,$13 ; ends $11 $13; (8,119) direction 2 x 4 steps;
  DEFB $08,$77,$24         ; (112,119) direction 5 x 4 steps; (144,183)
  DEFB $70,$77,$54         ; direction 6 x 4 steps
  DEFB $90,$B7,$64         ;
  DEFB $99                ; Tiles $0F, $7C upside down, $2F mirrored left-right
  DEFB $FF                ; and $71 upside down and mirrored left-right. 1
  DEFB $99                ; piece: (8,183) direction 3 x 10 steps
  DEFB $01                ;
  DEFB $08,$B7,$3A        ;
  DEFB $99                 ; Tiles $10, $6A upside down, $30 mirrored
  DEFB $FF                 ; left-right and $2E upside down and mirrored
  DEFB $99                 ; left-right. 2 pieces: (8,151) direction 0 x 4
  DEFB $02                 ; steps, ends $00 $12; (104,151) direction 3 x 8
  DEFB $08,$97,$04,$00,$12 ; steps
  DEFB $68,$97,$38         ;
  DEFB $99                 ; Tiles $11, $82 upside down, $31 mirrored
  DEFB $FF                 ; left-right and $76 upside down and mirrored
  DEFB $99                 ; left-right. 2 pieces: (8,151) direction 0 x 4
  DEFB $02                 ; steps, ends $00 $12; (104,151) direction 2 x 16
  DEFB $08,$97,$04,$00,$12 ; steps
  DEFB $68,$97,$20         ;
  DEFB $99                 ; Tiles $12, $7E upside down, $32 mirrored
  DEFB $FF                 ; left-right and $7F upside down and mirrored
  DEFB $99                 ; left-right. 2 pieces: (40,151) direction 0 x 8
  DEFB $02                 ; steps, ends $11 $00; (8,183) direction 2 x 4 steps
  DEFB $28,$97,$08,$11,$00 ;
  DEFB $08,$B7,$24         ;
  DEFB $FF                ; Tiles $13, $6B upside down and $33 mirrored
  DEFB $99                ; left-right. 2 pieces: (168,183) direction 3 x 4
  DEFB $02                ; steps; (200,119) direction 2 x 4 steps
  DEFB $A8,$B7,$34        ;
  DEFB $C8,$77,$24        ;
  DEFB $99                 ; Tiles $14, $99 upside down, $34 mirrored
  DEFB $FF                 ; left-right and $6D upside down and mirrored
  DEFB $99                 ; left-right. 4 pieces: (72,87) direction 0 x 4
  DEFB $04                 ; steps, ends $11 $12; (8,183) direction 2 x 4
  DEFB $48,$57,$04,$11,$12 ; steps; (40,151) direction 3 x 4 steps; (168,87)
  DEFB $08,$B7,$24         ; direction 3 x 4 steps
  DEFB $28,$97,$34         ;
  DEFB $A8,$57,$34         ;
  DEFB $99                 ; Tiles $15, $6C upside down, $35 mirrored
  DEFB $FF                 ; left-right and $6F upside down and mirrored
  DEFB $99                 ; left-right. 3 pieces: (72,87) direction 0 x 4
  DEFB $03                 ; steps, ends $11 $12; (8,119) direction 1 x 4
  DEFB $48,$57,$04,$11,$12 ; steps; (168,87) direction 2 x 8 steps
  DEFB $08,$77,$14         ;
  DEFB $A8,$57,$28         ;
  DEFB $99                 ; Tiles $16 and $87 upside down. 1 piece: (8,151)
  DEFB $01                 ; direction 0 x 10 steps, ends $00 $16
  DEFB $08,$97,$0A,$00,$16 ;
  DEFB $99                ; Tiles $17, $86 upside down, $36 mirrored left-right
  DEFB $FF                ; and $8B upside down and mirrored left-right. 1
  DEFB $99                ; piece: (8,87) direction 2 x 8 steps
  DEFB $01                ;
  DEFB $08,$57,$28        ;
  DEFB $FF                 ; Tiles $18 and $37 mirrored left-right. 2 pieces:
  DEFB $02                 ; (88,151) direction 0 x 6 steps, ends $11 $00;
  DEFB $58,$97,$06,$11,$00 ; (72,183) direction 3 x 2 steps
  DEFB $48,$B7,$32         ;
  DEFB $99                 ; Tiles $19, $8A upside down, $38 mirrored
  DEFB $FF                 ; left-right and $69 upside down and mirrored
  DEFB $99                 ; left-right. 2 pieces: (8,151) direction 0 x 4
  DEFB $02                 ; steps, ends $00 $12; (104,151) direction 1 x 8
  DEFB $08,$97,$04,$00,$12 ; steps
  DEFB $68,$97,$18         ;
  DEFB $FF                 ; Tiles $A7 and $A8 mirrored left-right. 2 pieces:
  DEFB $02                 ; (8,151) direction 0 x 1 step, ends $13 $13;
  DEFB $08,$97,$01,$13,$13 ; (40,183) direction 6 x 2 steps
  DEFB $28,$B7,$62         ;
  DEFB $FF                 ; Tiles $1A, $A9 and $39 mirrored left-right. 4
  DEFB $04                 ; pieces: (8,151) direction 0 x 1 step, ends $13
  DEFB $08,$97,$01,$13,$13 ; $13; (208,151) direction 0 x 1 step, ends $11 $11;
  DEFB $D0,$97,$01,$11,$11 ; (40,183) direction 6 x 2 steps; (192,183)
  DEFB $28,$B7,$62         ; direction 3 x 2 steps
  DEFB $C0,$B7,$32         ;
  DEFB $FF                 ; Tiles $1B and $3A mirrored left-right. 3 pieces:
  DEFB $03                 ; (72,151) direction 0 x 4 steps, ends $11 $12;
  DEFB $48,$97,$04,$11,$12 ; (40,183) direction 2 x 4 steps; (168,151)
  DEFB $28,$B7,$24         ; direction 3 x 8 steps
  DEFB $A8,$97,$38         ;
  DEFB $99                ; Tiles $1C, $7B upside down, $3B mirrored left-right
  DEFB $FF                ; and $70 upside down and mirrored left-right. 1
  DEFB $99                ; piece: (88,183) direction 3 x 10 steps
  DEFB $01                ;
  DEFB $58,$B7,$3A        ;
  DEFB $FF                ; Tiles $1D and $3C mirrored left-right. 2 pieces:
  DEFB $02                ; (8,119) direction 3 x 4 steps; (40,55) direction 2
  DEFB $08,$77,$34        ; x 4 steps
  DEFB $28,$37,$24        ;
  DEFB $99                 ; Tiles $1E, $3D mirrored left-right and $89 upside
  DEFB $FF                 ; down and mirrored left-right. 3 pieces: (40,59)
  DEFB $03                 ; direction 7 x 8 steps, ends $11 $00; (8,151)
  DEFB $28,$3B,$78,$11,$00 ; direction 0 x 10 steps, ends $00 $16; (32,55)
  DEFB $08,$97,$0A,$00,$16 ; direction 12 x 4 steps
  DEFB $20,$37,$C4         ;
  DEFB $FF                 ; Tiles $1F and $3F mirrored left-right. 3 pieces:
  DEFB $03                 ; (160,151) direction 0 x 3 steps, ends $11 $00;
  DEFB $A0,$97,$03,$11,$00 ; (136,183) direction 3 x 1 step; (144,167)
  DEFB $88,$B7,$31         ; direction 2 x 2 steps
  DEFB $90,$A7,$22         ;
  DEFB $04                ; Tile $41. 4 pieces: (64,71) direction 6 x 3 steps;
  DEFB $40,$47,$63        ; (168,71) direction 3 x 3 steps; (64,183) direction
  DEFB $A8,$47,$33        ; 15 x 7 steps; (168,183) direction 14 x 7 steps
  DEFB $40,$B7,$F7        ;
  DEFB $A8,$B7,$E7        ;
  DEFB $FF                 ; Tile $8D mirrored left-right. 4 pieces: (32,55)
  DEFB $04                 ; direction 10 x 2 steps; (128,151) direction 3 x 8
  DEFB $20,$37,$A2         ; steps; (8,59) direction 7 x 1 step, ends $13 $13;
  DEFB $80,$97,$38         ; (8,151) direction 0 x 5 steps, ends $00 $12
  DEFB $08,$3B,$71,$13,$13 ;
  DEFB $08,$97,$05,$00,$12 ;
  DEFB $FF                ; Tiles $43 and $8E mirrored left-right. 4 pieces:
  DEFB $04                ; (8,119) direction 9 x 4 steps; (40,87) direction 8
  DEFB $08,$77,$94        ; x 8 steps; (72,183) direction 2 x 4 steps;
  DEFB $28,$57,$88        ; (104,151) direction 1 x 8 steps
  DEFB $48,$B7,$24        ;
  DEFB $68,$97,$18        ;
  DEFB $FF                ; Tiles $44 and $8F mirrored left-right. 1 piece:
  DEFB $01                ; (168,183) direction 9 x 8 steps
  DEFB $A8,$B7,$98        ;
  DEFB $99                ; Tiles $45, $79 upside down, $90 mirrored left-right
  DEFB $FF                ; and $59 upside down and mirrored left-right. 2
  DEFB $99                ; pieces: (160,87) direction 15 x 4 steps; (72,183)
  DEFB $02                ; direction 9 x 12 steps
  DEFB $A0,$57,$F4        ;
  DEFB $48,$B7,$9C        ;
  DEFB $FF                 ; Tiles $46 and $91 mirrored left-right. 5 pieces:
  DEFB $05                 ; (136,59) direction 7 x 4 steps, ends $10 $00;
  DEFB $88,$3B,$74,$10,$00 ; (184,151) direction 0 x 2 steps, ends $11 $00;
  DEFB $B8,$97,$02,$11,$00 ; (8,183) direction 9 x 16 steps; (152,167)
  DEFB $08,$B7,$90         ; direction 1 x 2 steps; (136,183) direction 2 x 2
  DEFB $98,$A7,$12         ; steps
  DEFB $88,$B7,$22         ;
  DEFB $FF                ; Tiles $47 and $92 mirrored left-right. 2 pieces:
  DEFB $02                ; (72,87) direction 2 x 8 steps; (8,119) direction 1
  DEFB $48,$57,$28        ; x 4 steps
  DEFB $08,$77,$14        ;
  DEFB $02                 ; Tile $48. 2 pieces: (8,59) direction 7 x 10 steps,
  DEFB $08,$3B,$7A,$00,$16 ; ends $00 $16; (8,151) direction 0 x 10 steps, ends
  DEFB $08,$97,$0A,$00,$16 ; $00 $16
  DEFB $FF                 ; Tiles $49 and $93 mirrored left-right. 3 pieces:
  DEFB $03                 ; (8,59) direction 7 x 10 steps, ends $00 $16;
  DEFB $08,$3B,$7A,$00,$16 ; (8,151) direction 0 x 4 steps, ends $00 $13;
  DEFB $08,$97,$04,$00,$13 ; (152,183) direction 4 x 4 steps
  DEFB $98,$B7,$44         ;
  DEFB $99                 ; Tiles $4A, $83 upside down, $94 mirrored
  DEFB $FF                 ; left-right and $85 upside down and mirrored
  DEFB $99                 ; left-right. 4 pieces: (8,59) direction 7 x 3
  DEFB $04                 ; steps, ends $00 $12; (112,79) direction 11 x 3
  DEFB $08,$3B,$73,$00,$12 ; steps; (160,119) direction 12 x 5 steps; (168,183)
  DEFB $70,$4F,$B3         ; direction 14 x 4 steps
  DEFB $A0,$77,$C5         ;
  DEFB $A8,$B7,$E4         ;
  DEFB $FF                 ; Tiles $4B, $77 upside down and $95 mirrored
  DEFB $99                 ; left-right. 3 pieces: (8,151) direction 0 x 4
  DEFB $03                 ; steps, ends $00 $12; (168,119) direction 14 x 6
  DEFB $08,$97,$04,$00,$12 ; steps; (104,151) direction 1 x 4 steps
  DEFB $A8,$77,$E6         ;
  DEFB $68,$97,$14         ;
  DEFB $99                 ; Tiles $4C and $84 upside down. 1 piece: (8,59)
  DEFB $01                 ; direction 7 x 10 steps, ends $00 $16
  DEFB $08,$3B,$7A,$00,$16 ;
  DEFB $FF                 ; Tiles $4D and $98 mirrored left-right. 5 pieces:
  DEFB $05                 ; (8,59) direction 7 x 6 steps, ends $00 $13;
  DEFB $08,$3B,$76,$00,$13 ; (136,151) direction 0 x 4 steps, ends $11 $00;
  DEFB $88,$97,$04,$11,$00 ; (160,39) direction 15 x 1 step; (152,55) direction
  DEFB $A0,$27,$F1         ; 9 x 2 steps; (72,183) direction 1 x 4 steps
  DEFB $98,$37,$92         ;
  DEFB $48,$B7,$14         ;
  DEFB $FF                 ; Tiles $4E and $A2 mirrored left-right. 3 pieces:
  DEFB $03                 ; (184,59) direction 7 x 2 steps, ends $10 $00;
  DEFB $B8,$3B,$72,$10,$00 ; (8,183) direction 9 x 10 steps; (88,103) direction
  DEFB $08,$B7,$9A         ; 8 x 6 steps
  DEFB $58,$67,$86         ;
  DEFB $FF                 ; Tiles $4F and $A3 mirrored left-right. 2 pieces:
  DEFB $02                 ; (8,59) direction 7 x 8 steps, ends $00 $13;
  DEFB $08,$3B,$78,$00,$13 ; (200,55) direction 9 x 4 steps
  DEFB $C8,$37,$94         ;
  DEFB $FF                ; Tiles $50 and $97 mirrored left-right. 2 pieces:
  DEFB $02                ; (168,55) direction 14 x 2 steps; (224,183)
  DEFB $A8,$37,$E2        ; direction 13 x 8 steps
  DEFB $E0,$B7,$D8        ;
  DEFB $99                ; Tiles $51, $78 upside down, $96 mirrored left-right
  DEFB $FF                ; and $75 upside down and mirrored left-right. 3
  DEFB $99                ; pieces: (120,87) direction 11 x 8 steps; (160,119)
  DEFB $03                ; direction 12 x 4 steps; (168,183) direction 14 x 4
  DEFB $78,$57,$B8        ; steps
  DEFB $A0,$77,$C4        ;
  DEFB $A8,$B7,$E4        ;
  DEFB $FF                ; Tiles $52, $81 upside down and $A1 mirrored
  DEFB $99                ; left-right. 2 pieces: (168,119) direction 1 x 4
  DEFB $02                ; steps; (168,183) direction 14 x 4 steps
  DEFB $A8,$77,$14        ;
  DEFB $A8,$B7,$E4        ;
  DEFB $01                ; Tile $53. 1 piece: (224,55) direction 12 x 4 steps
  DEFB $E0,$37,$C4        ;
  DEFB $01                 ; Tile $54. 1 piece: (8,59) direction 7 x 10 steps,
  DEFB $08,$3B,$7A,$11,$16 ; ends $11 $16
  DEFB $99                 ; Tiles $55 and $8C upside down. 3 pieces: (72,55)
  DEFB $03                 ; direction 0 x 4 steps, ends $10 $12; (56,55)
  DEFB $48,$37,$04,$10,$12 ; direction 4 x 4 steps; (168,55) direction 1 x 4
  DEFB $38,$37,$44         ; steps
  DEFB $A8,$37,$14         ;
  DEFB $FF                 ; Tiles $56, $7D upside down and $9A mirrored
  DEFB $99                 ; left-right. 3 pieces: (72,151) direction 0 x 4
  DEFB $03                 ; steps, ends $11 $13; (8,183) direction 1 x 4
  DEFB $48,$97,$04,$11,$13 ; steps; (192,183) direction 5 x 4 steps
  DEFB $08,$B7,$14         ;
  DEFB $C0,$B7,$54         ;
  DEFB $FF                ; Tiles $57, $80 upside down and $A0 mirrored
  DEFB $99                ; left-right. 1 piece: (168,183) direction 14 x 10
  DEFB $01                ; steps
  DEFB $A8,$B7,$EA        ;
  DEFB $FF                ; Tiles $58, $74 upside down and $9F mirrored
  DEFB $99                ; left-right. 2 pieces: (168,55) direction 1 x 4
  DEFB $02                ; steps; (168,183) direction 14 x 8 steps
  DEFB $A8,$37,$14        ;
  DEFB $A8,$B7,$E8        ;
  DEFB $FF                ; Tiles $5B and $9D mirrored left-right. 3 pieces:
  DEFB $03                ; (136,87) direction 14 x 4 steps; (160,119)
  DEFB $88,$57,$E4        ; direction 12 x 4 steps; (168,183) direction 14 x 4
  DEFB $A0,$77,$C4        ; steps
  DEFB $A8,$B7,$E4        ;
  DEFB $02                ; Tile $5C. 2 pieces: (64,183) direction 15 x 10
  DEFB $40,$B7,$FA        ; steps; (168,183) direction 14 x 10 steps
  DEFB $A8,$B7,$EA        ;
  DEFB $FF                ; Tiles $5D and $A4 mirrored left-right. 4 pieces:
  DEFB $04                ; (64,119) direction 15 x 6 steps; (8,183) direction
  DEFB $40,$77,$F6        ; 9 x 8 steps; (168,55) direction 14 x 2 steps;
  DEFB $08,$B7,$98        ; (224,119) direction 12 x 8 steps
  DEFB $A8,$37,$E2        ;
  DEFB $E0,$77,$C8        ;
  DEFB $04                ; Tile $5E. 4 pieces: (72,119) direction 9 x 12
  DEFB $48,$77,$9C        ; steps; (72,183) direction 15 x 4 steps; (168,151)
  DEFB $48,$B7,$F4        ; direction 2 x 8 steps; (168,183) direction 14 x 2
  DEFB $A8,$97,$28        ; steps
  DEFB $A8,$B7,$E2        ;
  DEFB $99                 ; Tiles $5F and $88 upside down. 5 pieces: (64,59)
  DEFB $05                 ; direction 7 x 7 steps, ends $10 $00; (64,151)
  DEFB $40,$3B,$77,$10,$00 ; direction 0 x 7 steps, ends $10 $00; (32,127)
  DEFB $40,$97,$07,$10,$00 ; direction 15 x 3 steps; (56,151) direction 5 x 3
  DEFB $20,$7F,$F3         ; steps; (40,79) direction 9 x 3 steps
  DEFB $38,$97,$53         ;
  DEFB $28,$4F,$93         ;
  DEFB $04                ; Tile $60. 4 pieces: (64,119) direction 5 x 8 steps;
  DEFB $40,$77,$58        ; (64,183) direction 15 x 4 steps; (168,151)
  DEFB $40,$B7,$F4        ; direction 2 x 8 steps; (168,183) direction 14 x 2
  DEFB $A8,$97,$28        ; steps
  DEFB $A8,$B7,$E2        ;
  DEFB $FF                 ; Tiles $62 and $9E mirrored left-right. 5 pieces:
  DEFB $05                 ; (64,59) direction 7 x 7 steps, ends $11 $00;
  DEFB $40,$3B,$77,$11,$00 ; (40,151) direction 0 x 8 steps, ends $10 $00;
  DEFB $28,$97,$08,$10,$00 ; (40,47) direction 11 x 3 steps; (56,55) direction
  DEFB $28,$2F,$B3         ; 12 x 1 step; (32,151) direction 5 x 4 steps
  DEFB $38,$37,$C1         ;
  DEFB $20,$97,$54         ;
  DEFB $02                ; Tile $63. 2 pieces: (64,119) direction 15 x 6
  DEFB $40,$77,$F6        ; steps; (8,183) direction 9 x 8 steps
  DEFB $08,$B7,$98        ;
  DEFB $06                 ; Tile $64. 6 pieces: (96,55) direction 0 x 2 steps,
  DEFB $60,$37,$02,$11,$13 ; ends $11 $13; (96,155) direction 7 x 2 steps, ends
  DEFB $60,$9B,$72,$11,$13 ; $11 $13; (72,103) direction 3 x 3 steps; (88,151)
  DEFB $48,$67,$33         ; direction 13 x 3 steps; (160,103) direction 6 x 3
  DEFB $58,$97,$D3         ; steps; (144,151) direction 10 x 3 steps
  DEFB $A0,$67,$63         ;
  DEFB $90,$97,$A3         ;

; The title tune
;
; The tune the menu (menu) plays through play_tune_until_key, which stops at
; any key. 250 notes and an end marker, running on past this range. Each note
; is played by play_note: bits 0-5 a note number, the note's three bytes in the
; table at note_table (0 is a rest); bits 6-7 how many times the note's length
; to hold it, 1-4. $FF ends the tune.
title_tune:
  DEFB $27,$27,$29,$2A,$29,$29,$2A,$2C,$2A,$2A,$2C,$2E ; The notes
  DEFB $2C,$2C,$2A,$29,$27,$27,$29,$2A,$29,$29,$2A,$2C ;
  DEFB $2A,$2A,$2C,$2E,$2C,$2C,$2A,$29,$27,$1B,$29,$2A ;
  DEFB $29,$1D,$2A,$2C,$2A,$1E,$2C,$2E,$2C,$20,$2A,$25 ;
  DEFB $27,$1B,$29,$2A,$29,$1D,$2A,$2C,$2A,$1E,$2C,$2E ;
  DEFB $2C,$20,$2A,$25,$27,$1B,$27,$1B,$27,$1B,$27,$29 ;
  DEFB $2A,$1E,$25,$1E,$25,$1E,$25,$27,$23,$20,$23,$20 ;
  DEFB $23,$20,$63,$1E,$22,$65,$19,$22,$66,$1B,$2C,$1B ;
  DEFB $1B,$2A,$1B,$29,$1B,$27,$27,$1B,$27,$27,$1B,$27 ;
  DEFB $27,$1B,$2C,$1B,$1B,$2A,$1B,$29,$1B,$27,$27,$1B ;
  DEFB $27,$27,$1B,$27,$27,$2E,$1E,$2E,$2F,$31,$1E,$31 ;
  DEFB $2E,$2C,$1D,$2C,$2E,$2F,$1D,$2F,$2C,$2A,$1B,$2A ;
  DEFB $2C,$2E,$1B,$2E,$2A,$29,$1A,$29,$2A,$2C,$1A,$2A ;
  DEFB $29,$27,$27,$29,$2A,$29,$29,$2A,$2C,$2A,$2A,$2C ;
  DEFB $2E,$2C,$2C,$2A,$29,$27,$27,$29,$2A,$29,$29,$2A ;
  DEFB $2C,$2A,$2A,$2C,$2E,$2C,$2C,$2A,$29,$27,$1B,$27 ;
  DEFB $1B,$27,$1B,$27,$29,$2A,$1E,$25,$1E,$25,$1E,$25 ;
  DEFB $27,$23,$20,$23,$20,$23,$20,$63,$1E,$22,$65,$19 ;
  DEFB $22,$66,$1B,$2C,$1B,$1B,$2A,$1B,$29,$1B,$27,$27 ;
  DEFB $1B,$27,$27,$1B,$27,$27,$1B,$2C,$1B,$1B,$2A,$1B ;
  DEFB $29,$1B,$27,$27,$1B,$27,$27,$1B,$27,$E7         ;
  DEFB $FF                ; End

; Sound one beeper pulse: B loops on, D loops off
;
; Used by the routines at crackle and sound_beams.
;
; The building block of the crackles (sound_crackle, sound_beams). The game
; runs with interrupts off and makes every sound by timing loops like these, so
; a sound costs its length in processor time: about 13 T-states for each loop
; of B and of D, here and in beep_cycle.
;
; The border stays black: OUT ($FE) writes $10 (speaker on) and 0.
;
; B Loops with the speaker on (0 = 256)
; D Loops with it off (0 = 256)
beep_pulse:
  LD A,$10                ; Speaker on, wait B loops
  OUT ($FE),A             ;
beep_pulse_0:
  DJNZ beep_pulse_0       ;
  XOR A                   ; Speaker off, wait D loops
  OUT ($FE),A             ;
  LD B,D                  ;
beep_pulse_1:
  DJNZ beep_pulse_1       ;
  RET                     ;

; Sound one square-wave cycle: B loops on, B loops off
;
; Used by the routines at rising_sweep, warble, sound_flutter, zap_sweep and
; sound_buzz.
;
; The building block of the tones and sweeps. One cycle takes about 26 x B
; T-states, so the pitch falls as B grows. B comes back unchanged, so a caller
; can loop on it.
;
; B Half the period, in loops (0 = 256)
beep_cycle:
  LD A,$10                ; Speaker on for B loops, keeping B
  OUT ($FE),A             ;
  LD A,B                  ;
beep_cycle_0:
  DJNZ beep_cycle_0       ;
  LD B,A                  ;
  XOR A                   ; Speaker off for B loops, B restored
  OUT ($FE),A             ;
  LD A,B                  ;
beep_cycle_1:
  DJNZ beep_cycle_1       ;
  LD B,A                  ;
  RET                     ;

; Sound a rising whine
;
; Used by the routine at fly_in_step.
;
; Called by fly_in_step. 32 cycles whose half period falls from $90 to $71
; loops, so the note rises. The entry at rising_sweep is the general sweep: C
; cycles, each of half period C + D, C counting down. sound_pick_up,
; sound_ship_colour, sound_hum and sound_collect are this with other numbers.
sound_rising_whine:
  LD C,$20                ; 32 cycles from a half period of $90
  LD D,$70                ;

; Sound a sweep of C cycles
;
; Used by the routines at sound_pick_up, sound_ship_colour, sound_hum and
; sound_collect.
;
; The general sweep, entered from sound_rising_whine, sound_pick_up,
; sound_ship_colour, sound_hum and sound_collect: C cycles, each of half period
; C + D, C counting down, so the note rises.
;
; C Number of cycles, and the part of the period that shrinks
; D The fixed part of the period
rising_sweep:
  LD A,C                  ; Half period C + D
  ADD A,D                 ;
  LD B,A                  ;
  CALL beep_cycle         ; One cycle (beep_cycle); next C
  DEC C                   ;
  JR NZ,rising_sweep      ;
  RET                     ;

; Sound a short rising chirp
;
; Used by the routine at collect_pickup.
;
; Called by collect_pickup when the ship picks up a pick-up. 16 cycles from a
; half period of $50 down to $41 (rising_sweep).
sound_pick_up:
  LD C,$10                ; C=16, D=$40
  LD D,$40                ;
  JR rising_sweep         ;

; An unused sound: a crackle like sound_crackle's
;
; Nothing calls these six bytes (sna2ctl took them for data): LD C,$17, LD E,C
; and a jump into the crackle loop at crackle, so 23 clicks with C = E = $17 to
; start.
sound_unused:
  LD C,$17                ; C = E = $17, then the crackle loop
  LD E,C                  ;
  JP crackle              ;

; Sound the wreck's debris
;
; Used by the routine at update_debris.
;
; Called by update_debris, the update routine of type $15 (debris), for each
; piece each pass while the death byte (death_count) is below 10. The piece's
; path step (+$15, bits 0-5) halved picks the note, so the sound changes as the
; pieces fly.
;
; The entry at warble is the warble itself, also used by the death sound
; (sound_death): for A of 16 or more, and for 7 and 8, nothing; otherwise four
; cycles alternating between two half periods, B = 2 x A + $48 and that XOR
; $2A.
sound_debris:
  LD A,(death_count)      ; Not once the death byte is 10 or more
  CP $0A                  ;
  RET NC                  ;
  LD A,(IX+$15)           ; The piece's path step, halved
  AND $3F                 ;
  RR A                    ;

; Sound a warble
;
; Used by the routine at sound_death.
;
; Entered from sound_debris and the death sound (sound_death). For A of 16 or
; more, and for 7 and 8, nothing; otherwise four cycles alternating between two
; half periods, B = 2 x A + $48 and that XOR $2A.
;
; A The note, 0-15
warble:
  CP $10                  ; No sound for 16 and over, nor for 7 and 8
  RET NC                  ;
  CP $07                  ;
  JR C,warble_0           ;
  CP $09                  ;
  RET C                   ;
warble_0:
  AND $0F                 ; B = 2 x (A AND 15) + $48
  ADD A,A                 ;
  ADD A,$48               ;
  LD B,A                  ;
  LD C,$04                ; Four cycles, B toggling by $2A between them
warble_1:
  LD A,B                  ;
  XOR $2A                 ;
  LD B,A                  ;
  CALL beep_cycle         ;
  DEC C                   ;
  JR NZ,warble_1          ;
  RET                     ;

; Sound a flutter
;
; Used by the routine at update_special_beam.
;
; Called by update_special_beam while its record's +$0E is not zero. D = 8 x (1
; + the pass counter's low three bits), so 4 to 32 cycles, longer on some
; passes than others; each cycle's half period is (D XOR 7) + 1 as D counts
; down by two.
sound_flutter:
  LD A,(pass_counter)     ; D = 8 x (pass AND 7, plus 1)
  AND $07                 ;
  INC A                   ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD D,A                  ;
sound_flutter_0:
  LD A,D                  ; A cycle of (D XOR 7) + 1; D down by two until it
  XOR $07                 ; reaches 0
  INC A                   ;
  LD B,A                  ;
  CALL beep_cycle         ;
  DEC D                   ;
  RET Z                   ;
  DEC D                   ;
  RET Z                   ;
  JR sound_flutter_0      ;

; Sound a falling zap
;
; Used by the routines at planet_secured, ship_contact, contact_damage,
; contact_drain_fuel, contact_extra_life, test_player_shots,
; update_tough_attacker and draw_rocket.
;
; The commonest effect: called by planet_secured (the bonus count),
; ship_contact (the shield starting), contact_damage (damage),
; contact_drain_fuel (fuel), contact_extra_life (an extra life),
; test_player_shots (a shot hits), update_tough_attacker and draw_rocket. 16
; cycles whose half period is (NOT (4 x C)) AND $7F as C counts down from 16:
; $3F, $43, ... $7B, so the note falls.
;
; The entry at zap_sweep takes the number of cycles in C (sound_death uses 7).
sound_zap:
  LD C,$10                ; Sixteen cycles

; Sound a falling zap of C cycles
;
; Used by the routine at sound_death.
;
; Entered from sound_zap (16 cycles) and sound_death (7). Each cycle has half
; period (NOT (4 x C)) AND $7F as C counts down, so the note falls.
;
; C Number of cycles
zap_sweep:
  LD A,C                  ; Half period (NOT 4C) AND $7F; next C
  ADD A,A                 ;
  ADD A,A                 ;
  CPL                     ;
  AND $7F                 ;
  LD B,A                  ;
  CALL beep_cycle         ;
  DEC C                   ;
  JR NZ,zap_sweep         ;
  RET                     ;

; Sound a short whine when the ship changes colour
;
; Used by the routine at colour_ship.
;
; Called by colour_ship whenever the ship's colour changes: it works out $47
; (white), $46 (yellow, when the ore tank is full: the ore count in ore is 18)
; or $45 (cyan, with the shield on, flashing in its last $20 passes), and
; sounds this when that differs from last time (the attribute in the patch at
; ship_colour_patch, its third byte). Ten cycles of half period $8A down to $81
; (rising_sweep). It never ran in the build's sessions, none of which had the
; shield or a full tank; a recording of a staged shield running out, made for
; the Sounds page, bears the reading out.
sound_ship_colour:
  LD C,$0A                ; C=10, D=$80
  LD D,$80                ;
  JP rising_sweep         ;

; Sound a buzz
;
; Used by the routines at hit_block and ship_on_slope_response.
;
; Called by hit_block and ship_on_slope_response. 15 cycles whose half period
; is ((C XOR 2) AND 4) x 8: either 32 loops or 0, which DJNZ counts as 256, in
; runs of four: four of 32, four of 256, four of 32 and the last three of 256
; -- a rough two-note buzz.
sound_buzz:
  LD C,$0F                ; 15 cycles, half period 32 or 256 loops
sound_buzz_0:
  LD A,C                  ;
  XOR $02                 ;
  AND $04                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  CALL beep_cycle         ;
  DEC C                   ;
  JR NZ,sound_buzz_0      ;
  RET                     ;

; Sound the tough attacker's hum
;
; Used by the routine at update_tough_attacker.
;
; Called by update_tough_attacker, the update routine of type $19, each pass.
; Silent on even passes; otherwise four cycles of half period $13 down to $10
; (rising_sweep). With bit 6 of the record's +$16 set it would play three
; cycles of half period $1E to $1C instead, but only on a pass whose count AND
; 3 is 0 -- and every even pass has returned already, so that branch can never
; sound. Bit 6 was never set in the build's sessions; eight calls with it set,
; recorded for the Sounds page, were all silent.
;
; IX The attacker's record
sound_hum:
  LD A,(pass_counter)     ; Only on odd passes
  AND $01                 ;
  RET Z                   ;
  LD C,$04                ; Four short cycles, unless bit 6 of +$16
  LD D,$0F                ;
  BIT 6,(IX+$16)          ;
  JP Z,rising_sweep       ;
  LD A,(pass_counter)     ; Otherwise three lower cycles when the pass count
  AND $03                 ; AND 3 is 0, which no odd pass gives: never
  RET NZ                  ;
  LD C,$03                ;
  LD D,$1B                ;
  JP rising_sweep         ;

; Sound a crackle
;
; Used by the routine at hit_block.
;
; Called by hit_block. The crackle loop at crackle makes C clicks: for each, B
; = 2 x (C XOR E) and D = B AND $1F, one pulse of B loops on and D off
; (beep_pulse). The XOR scrambles the lengths, so the clicks sound like noise;
; E picks the scramble. sound_tick, sound_death, sound_crackle_b, sound_launch
; and sound_fire enter it with other numbers. Here C = $1C, E = $27.
sound_crackle:
  LD C,$1C                ; 28 clicks, scramble $27
  LD E,$27                ;

; Sound a crackle of C clicks
;
; Used by the routines at sound_unused, sound_tick, sound_death,
; sound_crackle_b, sound_launch and sound_fire.
;
; The crackle loop, entered from sound_unused, sound_crackle, sound_tick,
; sound_death, sound_crackle_b, sound_launch and sound_fire. For each click B =
; 2 x (C XOR E) and D = B AND $1F: one pulse of B loops on and D off
; (beep_pulse). The XOR scrambles the lengths, so the clicks sound like noise;
; E picks the scramble.
;
; C Number of clicks
; E The scramble
crackle:
  LD A,C                  ; A click of 2(C XOR E) loops on, the low five bits
  XOR E                   ; of that off
  ADD A,A                 ;
  LD B,A                  ;
  AND $1F                 ;
  LD D,A                  ;
  CALL beep_pulse         ;
  DEC C                   ;
  JR NZ,crackle           ;
  RET                     ;

; Sound a short crackle
;
; Used by the routines at move_or_explode and probe_shot.
;
; Called by move_or_explode and probe_shot. Eight clicks, scramble 3 (crackle).
sound_tick:
  LD C,$08                ; C=8, E=3
  LD E,$03                ;
  JR crackle              ;

; Sound the ship's death
;
; Used by the routine at update_objects.
;
; Called by update_objects each pass once the death byte (death_count) has
; reached 10, until it reaches $2D: from nine passes after the ship is
; destroyed. Docking with the finished rocket does not play it: draw_rocket
; sets the death byte to 10 only for the two calls that put the ship away, and
; back to 0 in the same call, before update_objects, its only caller, runs in
; the pass. It plays the warble (warble) with the death byte - 10, then, on
; every eighth count, a seven-cycle zap (zap_sweep); on the other odd counts,
; eleven clicks with the death byte halved as the scramble (crackle); on even
; counts nothing more.
sound_death:
  LD A,(death_count)      ; Nothing from $2D on
  CP $2D                  ;
  RET NC                  ;
  SUB $0A                 ; The warble, note = death byte - 10
  CALL warble             ;
  LD A,(death_count)      ; A multiple of 8: the zap below
  LD B,A                  ;
  AND $07                 ;
  JR Z,sound_death_0      ;
  BIT 0,B                 ; Even: nothing more
  RET Z                   ;
  LD A,B                  ; Odd: eleven clicks, scramble = death byte / 2
  SRL A                   ;
  LD E,A                  ;
  LD C,$0B                ;
  JP crackle              ;
sound_death_0:
  LD C,$07                ; Seven cycles of the zap
  JP zap_sweep            ;

; Sound the flickering beams
;
; Used by the routine at draw_beams.
;
; Jumped to from the end of draw_beams when it has drawn a beam. The record in
; IX sets the sound: C = ((+0 XOR +1) AND 15) + 8 pulses, E = +2 AND $1F; for
; each pulse B = ((C + E) AND 15) x 2 loops on and D = 2(E + B) - B off
; (beep_pulse).
;
; IX The beam's record
sound_beams:
  LD A,(IX+$00)           ; 8-23 pulses
  XOR (IX+$01)            ;
  AND $0F                 ;
  ADD A,$08               ;
  LD C,A                  ;
  LD A,(IX+$02)           ; E from +2
  AND $1F                 ;
  LD E,A                  ;
sound_beams_0:
  LD A,C                  ; The pulse's on and off lengths
  ADD A,E                 ;
  AND $0F                 ;
  ADD A,A                 ;
  LD B,A                  ;
  LD A,E                  ;
  ADD A,B                 ;
  ADD A,A                 ;
  SUB B                   ;
  LD D,A                  ;
  CALL beep_pulse         ; One pulse; next C
  DEC C                   ;
  JR NZ,sound_beams_0     ;
  RET                     ;

; Sound a crackle, chosen by the record
;
; Used by the routine at fire.
;
; Jumped to by fire. 15 clicks with scramble $17 (crackle); or, when bit 7 of
; the record's +$16 is set, 23 clicks with scramble $20 -- a missile's, which
; the build's every-add-on session fired.
;
; IX The record
sound_crackle_b:
  LD C,$0F                ; 15 clicks, scramble $17...
  LD E,$17                ;
  BIT 7,(IX+$16)          ;
  JP Z,crackle            ;
  LD C,E                  ; ...or 23, scramble $20
  LD E,$20                ;
  JP crackle              ;

; Sound the rocket's launch
;
; Used by the routine at draw_rocket.
;
; Called by draw_rocket each pass while the rocket is launching, with the pass
; counter's low two bits. C = (A + 2) x 4 clicks, 8 to 20, and the scramble
; (NOT C + 6) AND $1F (crackle), so the roar changes from pass to pass.
;
; A 0-3
sound_launch:
  ADD A,$02               ; C = 4(A + 2)
  ADD A,A                 ;
  ADD A,A                 ;
  LD C,A                  ;
  CPL                     ; The scramble from C
  ADD A,$06               ;
  AND $1F                 ;
  LD E,A                  ;
  JP crackle              ;

; Sound something being fired or thrown
;
; Used by the routine at fire_from.
;
; Called by fire_from (which ground_object_fires falls into and attacker_fires
; jumps to) when a ground object or an attacker has fired a bullet or thrown a
; pick-up. Ten clicks, scramble $22 (crackle).
sound_fire:
  LD C,$0A                ; C=10, E=$22
  LD E,$22                ;
  JP crackle              ;

; Sound an object being collected
;
; Used by the routine at contact_collect.
;
; Called by contact_collect (contact effect bit 3). Ten cycles of half period
; $22 down to $19 (rising_sweep): a short high rising note.
sound_collect:
  LD C,$0A                ; C=10, D=$18
  LD D,$18                ;
  JP rising_sweep         ;

; Rocket part: a barred band
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; The sprite graphics, from here to the code at landscape_end_pieces, mostly in
; the form width in bytes, height in lines, then the lines bottom line first,
; with no mask. The ship's add-ons 1-9 (addon_bar_graphic describes them) also
; have a height byte before and $FF or a mask after; the five pictures from
; puff_graphic (the jets') and the four frames from ring_graphic (the special
; beam's) have a height byte only. Which object shows which picture is set by
; the per-type table in object_handlers: each type's word points at a list of
; frame addresses, and the record's +$13 (low nibble) picks the frame. Names
; here describe what each picture looks like; what the scenery pictures are in
; the game is not yet worked out.
;
; 3 bytes by 8 lines, no mask, bottom line first. Type $1E's frame 0. The
; rocket parts are type $1E, and a part's frame is its number.
rocket_bars_graphic:
  DEFB $03,$08            ; Width in bytes, height in lines
  DEFB $56,$E7,$6A        ; The lines, bottom first
  DEFB $56,$E7,$6A        ;
  DEFB $56,$E7,$6A        ;
  DEFB $56,$E7,$6A        ;
  DEFB $56,$E7,$6A        ;
  DEFB $56,$E7,$6A        ;
  DEFB $56,$E7,$6A        ;
  DEFB $7F,$FF,$FE        ;

; Rocket part: a chequered band
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 8 lines, no mask, bottom line first. Type $1E's frame 1.
rocket_checks_graphic:
  DEFB $03,$08            ; Width in bytes, height in lines
  DEFB $66,$3C,$66        ; The lines, bottom first
  DEFB $66,$3C,$66        ;
  DEFB $66,$3C,$66        ;
  DEFB $66,$3C,$66        ;
  DEFB $59,$C3,$9A        ;
  DEFB $59,$C3,$9A        ;
  DEFB $59,$C3,$9A        ;
  DEFB $59,$C3,$9A        ;

; Rocket part: a lettered crate
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 17 lines, no mask, bottom line first. Type $1E's frame 2.
rocket_crate_graphic:
  DEFB $03,$11            ; Width in bytes, height in lines
  DEFB $30,$FF,$FC        ; The lines, bottom first
  DEFB $30,$FF,$FC        ;
  DEFB $30,$FF,$FC        ;
  DEFB $30,$FF,$FC        ;
  DEFB $30,$FF,$FC        ;
  DEFB $37,$88,$8C        ;
  DEFB $36,$E9,$AC        ;
  DEFB $36,$89,$AC        ;
  DEFB $36,$A9,$AC        ;
  DEFB $37,$A9,$8C        ;
  DEFB $30,$FF,$FC        ;
  DEFB $30,$FF,$FC        ;
  DEFB $30,$FF,$FC        ;
  DEFB $2F,$00,$04        ;
  DEFB $3C,$7F,$FC        ;
  DEFB $03,$9F,$C0        ;
  DEFB $00,$28,$00        ;

; Rocket part: a second lettered crate
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 16 lines, no mask, bottom line first. Type $1E's frame 3.
rocket_crate_b_graphic:
  DEFB $03,$10            ; Width in bytes, height in lines
  DEFB $3C,$00,$3C        ; The lines, bottom first
  DEFB $71,$C1,$FE        ;
  DEFB $71,$FF,$FA        ;
  DEFB $71,$FF,$FC        ;
  DEFB $71,$FF,$FA        ;
  DEFB $71,$FF,$FE        ;
  DEFB $6E,$84,$C2        ;
  DEFB $6A,$9C,$EC        ;
  DEFB $6A,$9C,$CA        ;
  DEFB $6A,$98,$6C        ;
  DEFB $71,$FF,$FA        ;
  DEFB $71,$FF,$FC        ;
  DEFB $70,$00,$3A        ;
  DEFB $78,$00,$14        ;
  DEFB $70,$00,$0A        ;
  DEFB $20,$00,$04        ;

; Rocket part: a fin
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1E's frame 4.
rocket_fin_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $0F,$00            ; The lines, bottom first
  DEFB $0F,$80            ;
  DEFB $0F,$C0            ;
  DEFB $07,$E0            ;
  DEFB $07,$FC            ;
  DEFB $07,$7C            ;
  DEFB $03,$3C            ;
  DEFB $03,$8C            ;
  DEFB $01,$8C            ;
  DEFB $01,$CC            ;
  DEFB $00,$EC            ;
  DEFB $00,$FC            ;
  DEFB $00,$7C            ;
  DEFB $00,$3C            ;
  DEFB $00,$1C            ;
  DEFB $00,$0C            ;

; Rocket part: the other fin
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1E's frame 5.
rocket_fin_b_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $00,$F0            ; The lines, bottom first
  DEFB $01,$F0            ;
  DEFB $03,$F0            ;
  DEFB $07,$E0            ;
  DEFB $3F,$E0            ;
  DEFB $3E,$E0            ;
  DEFB $3C,$C0            ;
  DEFB $31,$C0            ;
  DEFB $31,$80            ;
  DEFB $33,$80            ;
  DEFB $37,$00            ;
  DEFB $3F,$00            ;
  DEFB $3E,$00            ;
  DEFB $3C,$00            ;
  DEFB $38,$00            ;
  DEFB $30,$00            ;

; Rocket part: a pointed badge
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 18 lines, no mask, bottom line first. Type $1E's frame 7.
rocket_nose_graphic:
  DEFB $03,$12            ; Width in bytes, height in lines
  DEFB $10,$00,$08        ; The lines, bottom first
  DEFB $18,$00,$18        ;
  DEFB $1C,$00,$38        ;
  DEFB $1E,$00,$78        ;
  DEFB $1F,$FF,$F8        ;
  DEFB $1F,$FF,$F8        ;
  DEFB $1F,$60,$F8        ;
  DEFB $0F,$60,$F0        ;
  DEFB $0F,$20,$F0        ;
  DEFB $0F,$B1,$F0        ;
  DEFB $07,$91,$E0        ;
  DEFB $07,$D3,$E0        ;
  DEFB $03,$D3,$C0        ;
  DEFB $01,$E7,$80        ;
  DEFB $00,$E7,$00        ;
  DEFB $00,$7E,$00        ;
  DEFB $00,$3C,$00        ;
  DEFB $00,$18,$00        ;

; Add-on 10: a large slab (its size)
;
; The width and height of add-on 10 (5 bytes by 35 lines), the tenth entry of
; the add-ons' list (addon_graphics), as type $0C objects (an add-on lying on
; the planet) read it. carried_part draws it apart from the ship from
; addon_slab_picture, with sizes of its own. Add-ons 10 and 11 have no height
; byte before them and no mask after.
addon_slab:
  DEFB $05,$23            ; Width in bytes, height in lines

; Add-on 10's picture
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 35 lines of 5 bytes, bottom line first: the picture carried_part draws from
; here, past addon_slab's size bytes.
addon_slab_picture:
  DEFB $00,$00,$00,$55,$40 ; The lines, bottom first
  DEFB $00,$00,$00,$AA,$80 ;
  DEFB $00,$00,$01,$15,$40 ;
  DEFB $00,$00,$07,$2A,$A0 ;
  DEFB $00,$00,$1F,$D5,$40 ;
  DEFB $00,$00,$7F,$CA,$A0 ;
  DEFB $00,$01,$FF,$D5,$40 ;
  DEFB $00,$4F,$FF,$EA,$A0 ;
  DEFB $00,$F7,$FF,$E5,$50 ;
  DEFB $01,$D7,$FF,$EA,$A0 ;
  DEFB $03,$9B,$FF,$F5,$50 ;
  DEFB $07,$6B,$FF,$F2,$A0 ;
  DEFB $0E,$ED,$FF,$F5,$50 ;
  DEFB $1D,$E5,$80,$32,$A0 ;
  DEFB $0B,$76,$C0,$19,$50 ;
  DEFB $0D,$BA,$FF,$1A,$A0 ;
  DEFB $05,$DB,$7F,$39,$50 ;
  DEFB $06,$ED,$7E,$3A,$A8 ;
  DEFB $02,$75,$BE,$7D,$50 ;
  DEFB $03,$3A,$BC,$7C,$A8 ;
  DEFB $01,$5C,$DC,$0D,$50 ;
  DEFB $01,$AE,$5C,$04,$A8 ;
  DEFB $00,$D7,$6F,$FD,$50 ;
  DEFB $00,$5B,$AF,$FC,$A8 ;
  DEFB $00,$6D,$B7,$FE,$50 ;
  DEFB $00,$36,$D7,$FE,$A8 ;
  DEFB $00,$10,$1B,$FE,$50 ;
  DEFB $00,$1F,$FB,$FE,$A8 ;
  DEFB $00,$00,$01,$FE,$50 ;
  DEFB $00,$02,$A8,$3E,$A8 ;
  DEFB $00,$05,$54,$86,$50 ;
  DEFB $00,$02,$AA,$50,$A8 ;
  DEFB $00,$01,$54,$AA,$50 ;
  DEFB $00,$02,$AA,$55,$28 ;
  DEFB $00,$01,$55,$2A,$50 ;

; Add-on 11: a tall spire (its size)
;
; The width and height of add-on 11 (3 bytes by 47 lines), the eleventh entry
; of addon_graphics. carried_part draws it from addon_spire_picture with sizes
; of its own.
addon_spire:
  DEFB $03,$2F            ; Width in bytes, height in lines

; Add-on 11's picture
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 47 lines of 3 bytes, bottom line first. The byte after it is add-on 8's
; height byte.
addon_spire_picture:
  DEFB $00,$15,$48        ; The lines, bottom first
  DEFB $FF,$4A,$60        ;
  DEFB $FF,$71,$70        ;
  DEFB $FF,$BC,$70        ;
  DEFB $7F,$BF,$70        ;
  DEFB $00,$1F,$30        ;
  DEFB $3F,$B7,$50        ;
  DEFB $3F,$B9,$60        ;
  DEFB $3F,$BE,$70        ;
  DEFB $1F,$DF,$70        ;
  DEFB $1F,$DE,$B0        ;
  DEFB $1F,$DE,$50        ;
  DEFB $0F,$DE,$A0        ;
  DEFB $0F,$DE,$40        ;
  DEFB $0F,$DE,$A0        ;
  DEFB $0F,$DE,$40        ;
  DEFB $07,$DE,$A0        ;
  DEFB $07,$DE,$40        ;
  DEFB $07,$DE,$A0        ;
  DEFB $07,$DE,$40        ;
  DEFB $03,$DE,$80        ;
  DEFB $03,$DD,$40        ;
  DEFB $03,$DC,$80        ;
  DEFB $03,$DD,$00        ;
  DEFB $03,$DC,$80        ;
  DEFB $01,$DD,$00        ;
  DEFB $01,$DC,$80        ;
  DEFB $01,$DD,$00        ;
  DEFB $01,$DA,$00        ;
  DEFB $01,$D9,$00        ;
  DEFB $01,$DA,$00        ;
  DEFB $01,$D8,$00        ;
  DEFB $01,$DA,$00        ;
  DEFB $01,$D8,$00        ;
  DEFB $01,$D2,$00        ;
  DEFB $01,$D4,$00        ;
  DEFB $01,$D0,$00        ;
  DEFB $01,$A8,$00        ;
  DEFB $01,$A8,$00        ;
  DEFB $01,$A8,$00        ;
  DEFB $01,$90,$00        ;
  DEFB $01,$10,$00        ;
  DEFB $01,$60,$00        ;
  DEFB $01,$20,$00        ;
  DEFB $01,$40,$00        ;
  DEFB $01,$40,$00        ;
  DEFB $01,$80,$00        ;
  DEFB $00                ; Add-on 8's height byte (see addon_bar_graphic)

; Add-on 8: a small bar
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; The add-ons (addon_graphics, drawn into the ship's sprite by fit_addon) have
; a byte before them, the height the ship's sprite must have to show the
; add-on; then width, height and the picture bottom line first; then $FF, or a
; mask of the same size that addon_mask_and_draw ANDs on before the picture is
; ORed. 2 bytes by 4 lines, masked.
addon_bar_graphic:
  DEFB $02,$04            ; Width in bytes, height in lines
  DEFB $00,$10            ; The lines, bottom first
  DEFB $01,$F6            ;
  DEFB $1F,$E0            ;
  DEFB $07,$C0            ;
  DEFB $FE,$01            ; The mask
  DEFB $E0,$00            ;
  DEFB $C0,$01            ;
  DEFB $E0,$1F            ;
  DEFB $10                ; Add-on 6's height byte

; Add-on 6: a small mound
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 3 bytes by 6 lines, masked (see addon_bar_graphic for the form).
addon_mound_graphic:
  DEFB $03,$06            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $01,$C0,$00        ;
  DEFB $02,$E1,$80        ;
  DEFB $05,$F2,$C0        ;
  DEFB $05,$F5,$E0        ;
  DEFB $00,$00,$00        ;
  DEFB $FE,$3F,$FF        ; The mask, then add-on 9's height byte
  DEFB $FC,$1E,$7F        ;
  DEFB $F8,$0C,$3F        ;
  DEFB $F0,$00,$1F        ;
  DEFB $F0,$00,$0F        ;
  DEFB $F8,$08,$1F        ;
  DEFB $00                ;

; Add-on 9: a small figure
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 2 bytes by 15 lines (see addon_bar_graphic for the form). There is no $FF
; after the picture, so fit_addon takes the next 30 bytes as its mask: the six
; below, then all of add-on 7 (addon_post_graphic: its height byte, size,
; picture and mask) and add-on 4's height byte. Whether that was meant is not
; known.
addon_figure_graphic:
  DEFB $02,$0F            ; Width in bytes, height in lines
  DEFB $00,$00            ; The lines, bottom first
  DEFB $01,$E0            ;
  DEFB $1F,$80            ;
  DEFB $E7,$00            ;
  DEFB $34,$80            ;
  DEFB $03,$80            ;
  DEFB $05,$C0            ;
  DEFB $06,$C0            ;
  DEFB $03,$3F            ;
  DEFB $00,$FE            ;
  DEFB $05,$F8            ;
  DEFB $0B,$E0            ;
  DEFB $1B,$80            ;
  DEFB $36,$00            ;
  DEFB $30,$00            ;
  DEFB $FE,$1F            ; The first six bytes of what is taken as its mask,
  DEFB $E0,$0F            ; then add-on 7's height byte
  DEFB $00,$1F            ;
  DEFB $11                ;

; Add-on 7: a small post
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 2 bytes by 5 lines, masked (see addon_bar_graphic). These bytes also serve as
; the end of add-on 9's mask (addon_figure_graphic).
addon_post_graphic:
  DEFB $02,$05            ; Width in bytes, height in lines
  DEFB $0E,$04            ; The lines, bottom first
  DEFB $1F,$4C            ;
  DEFB $17,$5C            ;
  DEFB $5B,$4C            ;
  DEFB $EE,$04            ;
  DEFB $E0,$B1            ; The mask, then add-on 4's height byte
  DEFB $C0,$01            ;
  DEFB $80,$01            ;
  DEFB $00,$01            ;
  DEFB $00,$B1            ;
  DEFB $10                ;

; Add-on 4: a small block
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 2 bytes by 14 lines, masked (see addon_bar_graphic).
addon_block_graphic:
  DEFB $02,$0E            ; Width in bytes, height in lines
  DEFB $9B,$00            ; The lines, bottom first
  DEFB $DB,$E0            ;
  DEFB $DB,$F8            ;
  DEFB $DB,$F8            ;
  DEFB $DB,$F8            ;
  DEFB $DB,$FC            ;
  DEFB $DB,$FC            ;
  DEFB $DB,$FC            ;
  DEFB $DB,$FC            ;
  DEFB $DB,$C0            ;
  DEFB $1A,$1E            ;
  DEFB $40,$FE            ;
  DEFB $DB,$E0            ;
  DEFB $9B,$00            ;
  DEFB $00,$1F            ; The mask, then add-on 5's height byte
  DEFB $00,$07            ;
  DEFB $00,$03            ;
  DEFB $00,$03            ;
  DEFB $00,$03            ;
  DEFB $00,$01            ;
  DEFB $00,$01            ;
  DEFB $00,$01            ;
  DEFB $00,$01            ;
  DEFB $00,$01            ;
  DEFB $00,$00            ;
  DEFB $00,$00            ;
  DEFB $00,$01            ;
  DEFB $00,$1F            ;
  DEFB $15                ;

; Add-on 5: two arches
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 4 bytes by 11 lines, masked (see addon_bar_graphic).
addon_arches_graphic:
  DEFB $04,$0B            ; Width in bytes, height in lines
  DEFB $06,$7F,$67,$F0    ; The lines, bottom first
  DEFB $04,$01,$40,$10    ;
  DEFB $02,$AA,$2A,$A0    ;
  DEFB $02,$AA,$2A,$A0    ;
  DEFB $02,$7E,$27,$E0    ;
  DEFB $02,$7E,$27,$E0    ;
  DEFB $01,$3D,$D3,$C0    ;
  DEFB $01,$BC,$1B,$C0    ;
  DEFB $00,$DB,$ED,$80    ;
  DEFB $00,$F8,$0F,$80    ;
  DEFB $00,$00,$00,$00    ;
  DEFB $F0,$00,$00,$07    ; The mask, then add-on 1's height byte
  DEFB $F0,$00,$00,$07    ;
  DEFB $F8,$00,$80,$0F    ;
  DEFB $F8,$00,$80,$0F    ;
  DEFB $F8,$00,$80,$0F    ;
  DEFB $F8,$00,$00,$0F    ;
  DEFB $FC,$00,$00,$1F    ;
  DEFB $FC,$00,$00,$1F    ;
  DEFB $FE,$00,$00,$3F    ;
  DEFB $FE,$00,$00,$3F    ;
  DEFB $FF,$07,$F0,$7F    ;
  DEFB $23                ;

; Add-on 1: a hook
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 2 bytes by 23 lines, no mask (see addon_bar_graphic).
addon_hook_graphic:
  DEFB $02,$17            ; Width in bytes, height in lines
  DEFB $00,$04            ; The lines, bottom first
  DEFB $00,$14            ;
  DEFB $00,$34            ;
  DEFB $00,$B4            ;
  DEFB $00,$D4            ;
  DEFB $00,$A4            ;
  DEFB $01,$34            ;
  DEFB $03,$94            ;
  DEFB $07,$04            ;
  DEFB $0E,$00            ;
  DEFB $1C,$00            ;
  DEFB $10,$00            ;
  DEFB $0C,$00            ;
  DEFB $12,$00            ;
  DEFB $12,$00            ;
  DEFB $0C,$00            ;
  DEFB $02,$00            ;
  DEFB $07,$80            ;
  DEFB $03,$E0            ;
  DEFB $01,$F8            ;
  DEFB $00,$7E            ;
  DEFB $00,$3F            ;
  DEFB $00,$1F            ;
  DEFB $FF,$23            ; $FF (no mask), then add-on 2's height byte

; Add-on 2: a hook, mirrored
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship. Add-on 2 comes and goes
; with add-on 1, so the ship has both.
;
; 2 bytes by 23 lines, no mask (see addon_bar_graphic).
addon_hook_b_graphic:
  DEFB $02,$17            ; Width in bytes, height in lines
  DEFB $20,$00            ; The lines, bottom first
  DEFB $28,$00            ;
  DEFB $2C,$00            ;
  DEFB $2D,$00            ;
  DEFB $2B,$00            ;
  DEFB $25,$00            ;
  DEFB $2C,$80            ;
  DEFB $29,$C0            ;
  DEFB $20,$E0            ;
  DEFB $00,$70            ;
  DEFB $00,$38            ;
  DEFB $00,$08            ;
  DEFB $00,$30            ;
  DEFB $00,$48            ;
  DEFB $00,$48            ;
  DEFB $00,$30            ;
  DEFB $00,$40            ;
  DEFB $01,$E0            ;
  DEFB $07,$C0            ;
  DEFB $1F,$80            ;
  DEFB $7E,$00            ;
  DEFB $FC,$00            ;
  DEFB $F8,$00            ;
  DEFB $FF,$28            ; $FF (no mask), then add-on 3's height byte

; Add-on 3 and rocket part 6: the ore tank
;
; Lying on the planet, as the compositor draw_sprite_pair draws a type $0C
; object; and fitted to the ship: build_ship_sprite builds the ship's picture
; with it (its mask, if it has one, ANDed on first by addon_mask_and_draw) and
; the direct drawer draw_direct_pair draws the ship.
;
; 3 bytes by 18 lines. Add-on 3 (addon_graphics) and type $1E's frame 6 (the
; rocket part, empty). No $FF follows, so as an add-on it is masked with the 54
; bytes from ore_rows_graphic on: the ore rows and the first 18 bytes of
; rocket_tank_full_graphic.
rocket_tank_graphic:
  DEFB $03,$12            ; Width in bytes, height in lines
  DEFB $03,$81,$C0        ; The lines, bottom first
  DEFB $05,$7E,$A0        ;
  DEFB $08,$81,$10        ;
  DEFB $10,$81,$08        ;
  DEFB $18,$42,$18        ;
  DEFB $16,$42,$68        ;
  DEFB $09,$FF,$90        ;
  DEFB $08,$42,$10        ;
  DEFB $08,$42,$10        ;
  DEFB $08,$42,$10        ;
  DEFB $08,$42,$10        ;
  DEFB $09,$FF,$90        ;
  DEFB $16,$42,$68        ;
  DEFB $18,$42,$18        ;
  DEFB $10,$81,$08        ;
  DEFB $08,$81,$10        ;
  DEFB $05,$7E,$A0        ;
  DEFB $03,$81,$C0        ;

; The ore in the tank
;
; The ship with the ore tank (add-on 3) fitted and 18 units of ore aboard:
; build_ship_sprite builds the ship's picture and draws the ore into it
; (load_ore_3), and the direct drawer draw_direct_pair draws the ship.
;
; Up to 18 rows of 2 bytes, bottom first: load_ore copies one row for each unit
; of ore collected (the ore count in bare_ship) into bytes 2 and 3 of the rows
; of the ship's sprite, so the tank fills as ore is picked up. The same bytes
; begin the mask fit_addon uses for add-on 3 (rocket_tank_graphic).
ore_rows_graphic:
  DEFB $28,$1C            ; The rows, bottom first
  DEFB $6F,$EA            ;
  DEFB $F7,$F1            ;
  DEFB $F7,$F0            ;
  DEFB $7B,$E1            ;
  DEFB $98,$26            ;
  DEFB $E7,$F9            ;
  DEFB $F6,$2B            ;
  DEFB $F4,$E5            ;
  DEFB $F4,$EB            ;
  DEFB $F6,$25            ;
  DEFB $E7,$E3            ;
  DEFB $98,$00            ;
  DEFB $7B,$D4            ;
  DEFB $F7,$EA            ;
  DEFB $F7,$E5            ;
  DEFB $6F,$F2            ;
  DEFB $28,$14            ;

; Rocket part: the ore tank, full
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 18 lines, no mask, bottom line first. Type $1E's frame 8: part 6
; once it holds its load of ore. Its first 18 bytes are also the end of add-on
; 3's mask.
rocket_tank_full_graphic:
  DEFB $03,$12            ; Width in bytes, height in lines
  DEFB $02,$81,$C0        ; The lines, bottom first
  DEFB $06,$FE,$A0        ;
  DEFB $0F,$7F,$10        ;
  DEFB $1F,$7F,$08        ;
  DEFB $17,$BE,$18        ;
  DEFB $19,$82,$68        ;
  DEFB $0E,$7F,$90        ;
  DEFB $0F,$62,$B0        ;
  DEFB $0F,$4E,$50        ;
  DEFB $0F,$4E,$B0        ;
  DEFB $0F,$62,$50        ;
  DEFB $0E,$7E,$30        ;
  DEFB $19,$80,$08        ;
  DEFB $17,$BD,$48        ;
  DEFB $1F,$7E,$A8        ;
  DEFB $0F,$7E,$50        ;
  DEFB $06,$FF,$20        ;
  DEFB $02,$80,$40        ;

; A flask
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 35 lines, no mask, bottom line first. Type $0D's frame 0 (the list
; in type0d_graphics).
fuel_flask_graphic:
  DEFB $03,$23            ; Width in bytes, height in lines
  DEFB $7F,$FF,$FE        ; The lines, bottom first
  DEFB $60,$81,$06        ;
  DEFB $30,$81,$0C        ;
  DEFB $18,$00,$18        ;
  DEFB $0C,$7E,$30        ;
  DEFB $01,$FF,$80        ;
  DEFB $07,$FF,$40        ;
  DEFB $0F,$FF,$A0        ;
  DEFB $0F,$FF,$D0        ;
  DEFB $1B,$9F,$E8        ;
  DEFB $19,$9F,$D0        ;
  DEFB $31,$9F,$E8        ;
  DEFB $31,$83,$D4        ;
  DEFB $31,$83,$E8        ;
  DEFB $31,$9F,$D4        ;
  DEFB $31,$9F,$E8        ;
  DEFB $39,$81,$D4        ;
  DEFB $19,$81,$A8        ;
  DEFB $18,$FF,$50        ;
  DEFB $0C,$7F,$A8        ;
  DEFB $0E,$7F,$50        ;
  DEFB $07,$3E,$A0        ;
  DEFB $01,$9D,$40        ;
  DEFB $00,$DA,$00        ;
  DEFB $00,$5E,$00        ;
  DEFB $00,$6E,$00        ;
  DEFB $00,$2C,$00        ;
  DEFB $00,$2C,$00        ;
  DEFB $00,$2C,$00        ;
  DEFB $00,$2C,$00        ;
  DEFB $01,$5E,$00        ;
  DEFB $01,$00,$00        ;
  DEFB $01,$28,$00        ;
  DEFB $01,$E8,$00        ;
  DEFB $00,$08,$00        ;

; A pedestal
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 4 bytes by 23 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
pedestal_graphic:
  DEFB $04,$17            ; Width in bytes, height in lines
  DEFB $E0,$FF,$FF,$55    ; The lines, bottom first
  DEFB $38,$7F,$FF,$AC    ;
  DEFB $0E,$7F,$FD,$50    ;
  DEFB $03,$3F,$FA,$C0    ;
  DEFB $00,$00,$00,$00    ;
  DEFB $00,$CF,$FA,$00    ;
  DEFB $00,$CF,$F5,$00    ;
  DEFB $00,$CF,$FA,$00    ;
  DEFB $00,$CF,$F5,$00    ;
  DEFB $00,$00,$00,$00    ;
  DEFB $00,$9F,$F5,$00    ;
  DEFB $03,$3F,$FA,$C0    ;
  DEFB $0E,$3F,$F5,$50    ;
  DEFB $00,$00,$00,$00    ;
  DEFB $1C,$7F,$FE,$A8    ;
  DEFB $1C,$7F,$FF,$58    ;
  DEFB $1C,$7F,$FE,$A8    ;
  DEFB $1C,$7F,$FF,$58    ;
  DEFB $1C,$7F,$FE,$A8    ;
  DEFB $00,$00,$00,$00    ;
  DEFB $0E,$3F,$F5,$50    ;
  DEFB $03,$3F,$FA,$C0    ;
  DEFB $00,$9F,$F5,$00    ;

; The side jet, 16 pixels wide
;
; As the direct drawer draw_direct_pair draws it, at the width the jet's own
; code gives it (side_jet), three times the size.
;
; 16 lines of 2 bytes, in the height-first form: no width byte. Graphic $0E,
; the first of the side jet's three flickering frames ($0E-$10, side_jet); its
; entry in the per-type table (in object_handlers) points here.
; sprite_graphic_width reads the first byte as the height and the drawer is
; entered at a fixed width.
puff_graphic:
  DEFB $10                ; Height in lines
  DEFB $00,$40            ; The lines
  DEFB $08,$14            ;
  DEFB $05,$0A            ;
  DEFB $10,$18            ;
  DEFB $02,$C4            ;
  DEFB $04,$B8            ;
  DEFB $15,$F0            ;
  DEFB $5E,$08            ;
  DEFB $10,$10            ;
  DEFB $2F,$F8            ;
  DEFB $0C,$B4            ;
  DEFB $02,$45            ;
  DEFB $00,$18            ;
  DEFB $05,$64            ;
  DEFB $00,$08            ;
  DEFB $00,$20            ;

; The side jet, 16 pixels wide, second frame
;
; As the direct drawer draw_direct_pair draws it, at the width the jet's own
; code gives it (side_jet), three times the size.
;
; 15 lines of 2 bytes, in the height-first form: no width byte. Graphic $0F,
; the side jet (side_jet); its entry in the per-type table points here.
puff_b_graphic:
  DEFB $0F                ; Height in lines
  DEFB $00,$00            ; The lines
  DEFB $00,$08            ;
  DEFB $00,$36            ;
  DEFB $01,$49            ;
  DEFB $0A,$D6            ;
  DEFB $0D,$3F            ;
  DEFB $39,$E3            ;
  DEFB $47,$18            ;
  DEFB $BC,$79            ;
  DEFB $67,$87            ;
  DEFB $2C,$FC            ;
  DEFB $13,$5D            ;
  DEFB $05,$2A            ;
  DEFB $01,$49            ;
  DEFB $00,$26            ;

; The side jet, 16 pixels wide, third frame
;
; As the direct drawer draw_direct_pair draws it, at the width the jet's own
; code gives it (side_jet), three times the size.
;
; 14 lines of 2 bytes, in the height-first form: no width byte. Graphic $10,
; the side jet (side_jet); its entry in the per-type table points here.
puff_c_graphic:
  DEFB $0E                ; Height in lines
  DEFB $00,$00            ; The lines
  DEFB $00,$00            ;
  DEFB $00,$00            ;
  DEFB $00,$54            ;
  DEFB $00,$2E            ;
  DEFB $01,$0A            ;
  DEFB $04,$A0            ;
  DEFB $52,$1E            ;
  DEFB $15,$7E            ;
  DEFB $08,$8E            ;
  DEFB $02,$6A            ;
  DEFB $04,$A6            ;
  DEFB $01,$56            ;
  DEFB $00,$2A            ;

; The lift jet while thrusting, 32 pixels wide
;
; As the direct drawer draw_direct_pair draws it, at the width the jet's own
; code gives it (lift_jet), three times the size.
;
; 16 lines of 4 bytes, in the height-first form: no width byte. Graphic $11,
; the lift jet under the ship while it thrusts (lift_jet); its entry in the
; per-type table points here.
puff_wide_graphic:
  DEFB $10                ; Height in lines
  DEFB $00,$20,$00,$00    ; The lines
  DEFB $00,$00,$00,$00    ;
  DEFB $00,$50,$00,$00    ;
  DEFB $00,$80,$00,$00    ;
  DEFB $00,$50,$02,$00    ;
  DEFB $00,$A0,$00,$00    ;
  DEFB $01,$50,$06,$00    ;
  DEFB $02,$04,$02,$00    ;
  DEFB $01,$58,$0D,$00    ;
  DEFB $02,$84,$03,$00    ;
  DEFB $00,$AA,$09,$80    ;
  DEFB $05,$24,$14,$00    ;
  DEFB $03,$6A,$0A,$C0    ;
  DEFB $05,$74,$1A,$80    ;
  DEFB $07,$7A,$12,$40    ;
  DEFB $00,$00,$0A,$80    ;

; The lift jet, 32 pixels wide
;
; As the direct drawer draw_direct_pair draws it, at the width the jet's own
; code gives it (lift_jet), three times the size.
;
; 8 lines of 4 bytes, in the height-first form: no width byte. Graphic $12, the
; lift jet when the ship is not thrusting, drawn with its bottom line 8 higher
; than the thrusting one's, so that both hang from the same place (lift_jet);
; its entry in the per-type table points here.
puff_wide_b_graphic:
  DEFB $08                ; Height in lines
  DEFB $00,$40,$00,$00    ; The lines
  DEFB $00,$80,$00,$00    ;
  DEFB $00,$50,$00,$00    ;
  DEFB $00,$A0,$00,$00    ;
  DEFB $01,$48,$02,$00    ;
  DEFB $00,$B0,$05,$00    ;
  DEFB $01,$68,$0A,$00    ;
  DEFB $00,$70,$06,$00    ;

; A spiky mound
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 13 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
spiky_mound_graphic:
  DEFB $05,$0D            ; Width in bytes, height in lines
  DEFB $45,$55,$14,$9E,$AA ; The lines, bottom first
  DEFB $2C,$C4,$4B,$32,$1C ;
  DEFB $16,$D2,$C5,$68,$CC ;
  DEFB $11,$43,$91,$C3,$18 ;
  DEFB $0A,$9B,$0D,$86,$50 ;
  DEFB $07,$AB,$89,$2C,$B0 ;
  DEFB $05,$A9,$9B,$39,$C0 ;
  DEFB $03,$CD,$B2,$33,$40 ;
  DEFB $01,$FC,$92,$72,$40 ;
  DEFB $01,$6C,$5B,$7E,$80 ;
  DEFB $00,$E4,$89,$5E,$80 ;
  DEFB $00,$B4,$9A,$5D,$00 ;
  DEFB $00,$92,$D0,$DD,$00 ;

; The small cloud
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 4 bytes by 12 lines, no mask, bottom line first. Type $13's frame 0.
cloud_small_graphic:
  DEFB $04,$0C            ; Width in bytes, height in lines
  DEFB $02,$A0,$A0,$00    ; The lines, bottom first
  DEFB $05,$51,$50,$00    ;
  DEFB $0A,$AA,$A8,$00    ;
  DEFB $05,$F5,$F4,$A0    ;
  DEFB $0B,$EF,$FB,$50    ;
  DEFB $1F,$6F,$FF,$EC    ;
  DEFB $3F,$77,$E3,$FC    ;
  DEFB $3C,$F9,$FD,$FC    ;
  DEFB $3F,$FF,$FD,$FC    ;
  DEFB $1F,$BF,$7F,$D8    ;
  DEFB $0F,$7E,$3F,$80    ;
  DEFB $06,$1C,$1F,$00    ;

; The big cloud
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 24 lines, no mask, bottom line first. Type $13's frame 1.
cloud_big_graphic:
  DEFB $05,$18            ; Width in bytes, height in lines
  DEFB $00,$00,$01,$80,$00 ; The lines, bottom first
  DEFB $00,$00,$0A,$A0,$00 ;
  DEFB $00,$00,$5D,$50,$40 ;
  DEFB $00,$02,$AE,$A2,$A0 ;
  DEFB $00,$05,$57,$55,$50 ;
  DEFB $01,$4A,$AB,$AA,$A8 ;
  DEFB $02,$A5,$54,$05,$50 ;
  DEFB $05,$50,$21,$52,$A8 ;
  DEFB $0A,$EA,$8A,$A5,$50 ;
  DEFB $1D,$F7,$D7,$D0,$28 ;
  DEFB $1B,$FF,$FF,$FF,$90 ;
  DEFB $1B,$FF,$FF,$FF,$E0 ;
  DEFB $0B,$E7,$BF,$FF,$FC ;
  DEFB $1D,$D8,$7F,$FF,$FC ;
  DEFB $1E,$3F,$FF,$FF,$FC ;
  DEFB $1F,$FF,$FF,$DF,$FC ;
  DEFB $0F,$F7,$FF,$DF,$E8 ;
  DEFB $07,$EB,$EF,$DF,$E0 ;
  DEFB $03,$DD,$97,$AF,$C0 ;
  DEFB $00,$3E,$78,$70,$00 ;
  DEFB $00,$3F,$FF,$FE,$00 ;
  DEFB $00,$1F,$EF,$FC,$00 ;
  DEFB $00,$0F,$C7,$F8,$00 ;
  DEFB $00,$07,$01,$E0,$00 ;

; A rotor craft
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 17 lines, no mask, bottom line first. Type $14's frame 0.
rotor_graphic:
  DEFB $03,$11            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $00,$00,$00        ;
  DEFB $01,$EF,$80        ;
  DEFB $0F,$9F,$F0        ;
  DEFB $7C,$3F,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $F0,$7F,$FF        ;
  DEFB $3E,$3F,$FC        ;
  DEFB $07,$9F,$E0        ;
  DEFB $00,$E7,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$3C,$00        ;
  DEFB $00,$E3,$00        ;
  DEFB $03,$81,$C0        ;
  DEFB $0E,$00,$70        ;
  DEFB $38,$00,$1C        ;

; A rotor craft, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 17 lines, no mask, bottom line first. Type $14's frames 1 and 3.
rotor_b_graphic:
  DEFB $03,$11            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $00,$00,$00        ;
  DEFB $01,$EF,$80        ;
  DEFB $0F,$9F,$F0        ;
  DEFB $7C,$3F,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $F0,$7F,$FF        ;
  DEFB $3E,$3F,$FC        ;
  DEFB $07,$9F,$E0        ;
  DEFB $00,$E7,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$3C,$00        ;
  DEFB $00,$66,$00        ;
  DEFB $00,$C3,$00        ;
  DEFB $01,$81,$80        ;

; A rotor craft, third frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 17 lines, no mask, bottom line first. Type $14's frame 2.
rotor_c_graphic:
  DEFB $03,$11            ; Width in bytes, height in lines
  DEFB $01,$EF,$80        ; The lines, bottom first
  DEFB $0F,$9F,$F0        ;
  DEFB $7C,$3F,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$7E,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $F0,$7F,$FF        ;
  DEFB $3E,$3F,$FC        ;
  DEFB $07,$9F,$E0        ;
  DEFB $00,$E7,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$18,$00        ;

; A small burst
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 14 lines, no mask, bottom line first. Type $15's frame 0.
burst_small_graphic:
  DEFB $02,$0E            ; Width in bytes, height in lines
  DEFB $08,$02            ; The lines, bottom first
  DEFB $0C,$44            ;
  DEFB $06,$54            ;
  DEFB $07,$14            ;
  DEFB $03,$B0            ;
  DEFB $7F,$BC            ;
  DEFB $1F,$F8            ;
  DEFB $06,$B0            ;
  DEFB $02,$20            ;
  DEFB $0F,$78            ;
  DEFB $1F,$F8            ;
  DEFB $1D,$BA            ;
  DEFB $31,$9A            ;
  DEFB $21,$08            ;

; A small burst, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 14 lines, no mask, bottom line first. Type $15's frame 1.
burst_small_b_graphic:
  DEFB $02,$0E            ; Width in bytes, height in lines
  DEFB $00,$80            ; The lines, bottom first
  DEFB $20,$86            ;
  DEFB $39,$CC            ;
  DEFB $1D,$F8            ;
  DEFB $0F,$F0            ;
  DEFB $0F,$C0            ;
  DEFB $07,$FE            ;
  DEFB $7F,$F8            ;
  DEFB $1F,$E0            ;
  DEFB $1F,$F0            ;
  DEFB $3F,$F8            ;
  DEFB $3C,$98            ;
  DEFB $70,$0C            ;
  DEFB $40,$00            ;

; A big burst
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 21 lines, no mask, bottom line first. Type $15's frames 2 and 4.
burst_big_graphic:
  DEFB $03,$15            ; Width in bytes, height in lines
  DEFB $40,$00,$00        ; The lines, bottom first
  DEFB $20,$01,$00        ;
  DEFB $30,$01,$00        ;
  DEFB $38,$23,$10        ;
  DEFB $1C,$23,$30        ;
  DEFB $1E,$37,$60        ;
  DEFB $0F,$77,$E0        ;
  DEFB $0F,$FF,$C0        ;
  DEFB $07,$FF,$9C        ;
  DEFB $7F,$03,$F0        ;
  DEFB $1F,$87,$C0        ;
  DEFB $03,$03,$80        ;
  DEFB $0F,$97,$E0        ;
  DEFB $17,$BB,$00        ;
  DEFB $07,$FF,$80        ;
  DEFB $07,$FD,$C0        ;
  DEFB $0F,$EE,$E0        ;
  DEFB $0E,$4E,$30        ;
  DEFB $0C,$86,$0C        ;
  DEFB $18,$02,$00        ;
  DEFB $10,$02,$00        ;

; A big burst, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 21 lines, no mask, bottom line first. Type $15's frames 3 and 5.
burst_big_b_graphic:
  DEFB $03,$15            ; Width in bytes, height in lines
  DEFB $00,$40,$40        ; The lines, bottom first
  DEFB $20,$60,$C0        ;
  DEFB $38,$61,$C0        ;
  DEFB $1E,$73,$80        ;
  DEFB $1F,$77,$80        ;
  DEFB $0F,$7F,$00        ;
  DEFB $07,$FF,$00        ;
  DEFB $03,$DF,$E0        ;
  DEFB $07,$93,$FC        ;
  DEFB $0F,$C3,$80        ;
  DEFB $1F,$63,$C0        ;
  DEFB $3F,$37,$E0        ;
  DEFB $21,$1F,$F0        ;
  DEFB $03,$3F,$F8        ;
  DEFB $03,$6E,$1C        ;
  DEFB $03,$EE,$00        ;
  DEFB $07,$C7,$00        ;
  DEFB $07,$87,$00        ;
  DEFB $07,$03,$00        ;
  DEFB $06,$03,$00        ;
  DEFB $04,$01,$00        ;

; A dome
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 25 lines, no mask, bottom line first. Type $1A's frame 0.
dome_graphic:
  DEFB $03,$19            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $0F,$00,$00        ;
  DEFB $10,$80,$30        ;
  DEFB $10,$80,$48        ;
  DEFB $10,$98,$48        ;
  DEFB $0F,$24,$30        ;
  DEFB $00,$24,$00        ;
  DEFB $41,$18,$00        ;
  DEFB $00,$00,$30        ;
  DEFB $0C,$02,$30        ;
  DEFB $0C,$00,$00        ;
  DEFB $00,$10,$04        ;
  DEFB $00,$00,$00        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $38,$0C,$0C        ;
  DEFB $3C,$0C,$0C        ;
  DEFB $1C,$7F,$F8        ;
  DEFB $0E,$7F,$F0        ;
  DEFB $06,$3F,$E0        ;
  DEFB $03,$3F,$C0        ;
  DEFB $01,$DF,$80        ;
  DEFB $00,$6E,$00        ;
  DEFB $00,$18,$00        ;

; A dome, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 25 lines, no mask, bottom line first. Type $1A's frame 1.
dome_b_graphic:
  DEFB $03,$19            ; Width in bytes, height in lines
  DEFB $00,$00,$20        ; The lines, bottom first
  DEFB $20,$00,$00        ;
  DEFB $00,$08,$88        ;
  DEFB $00,$00,$20        ;
  DEFB $1E,$22,$78        ;
  DEFB $21,$00,$84        ;
  DEFB $21,$08,$84        ;
  DEFB $21,$00,$84        ;
  DEFB $1E,$18,$78        ;
  DEFB $00,$18,$00        ;
  DEFB $00,$80,$00        ;
  DEFB $00,$00,$20        ;
  DEFB $00,$00,$00        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $38,$0C,$0C        ;
  DEFB $3C,$0C,$0C        ;
  DEFB $1C,$7F,$F8        ;
  DEFB $0E,$7F,$F0        ;
  DEFB $06,$3F,$E0        ;
  DEFB $03,$3F,$C0        ;
  DEFB $01,$DF,$80        ;
  DEFB $00,$6E,$00        ;
  DEFB $00,$18,$00        ;

; A dome, third frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 25 lines, no mask, bottom line first. Type $1A's frame 2.
dome_c_graphic:
  DEFB $03,$19            ; Width in bytes, height in lines
  DEFB $04,$00,$78        ; The lines, bottom first
  DEFB $00,$00,$84        ;
  DEFB $11,$00,$84        ;
  DEFB $00,$3C,$84        ;
  DEFB $04,$42,$78        ;
  DEFB $00,$42,$00        ;
  DEFB $0C,$42,$30        ;
  DEFB $12,$3C,$48        ;
  DEFB $12,$00,$48        ;
  DEFB $0C,$00,$30        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $78,$FF,$FE        ;
  DEFB $38,$0C,$0C        ;
  DEFB $3C,$0C,$0C        ;
  DEFB $1C,$7F,$F8        ;
  DEFB $0E,$7F,$F0        ;
  DEFB $06,$3F,$E0        ;
  DEFB $03,$3F,$C0        ;
  DEFB $01,$DF,$80        ;
  DEFB $00,$6E,$00        ;
  DEFB $00,$18,$00        ;

; A bullet
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1C's frame 0; frames
; 0-2 animate the bullet.
bullet_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $00,$00            ; The lines, bottom first
  DEFB $20,$00            ;
  DEFB $00,$88            ;
  DEFB $12,$00            ;
  DEFB $20,$24            ;
  DEFB $05,$EE            ;
  DEFB $03,$A4            ;
  DEFB $06,$B0            ;
  DEFB $45,$B2            ;
  DEFB $02,$A5            ;
  DEFB $05,$42            ;
  DEFB $20,$00            ;
  DEFB $04,$48            ;
  DEFB $0A,$02            ;
  DEFB $04,$20            ;
  DEFB $00,$00            ;

; A bullet, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1C's frame 1.
bullet_b_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $01,$00            ; The lines, bottom first
  DEFB $02,$84            ;
  DEFB $31,$00            ;
  DEFB $38,$00            ;
  DEFB $03,$11            ;
  DEFB $0A,$C0            ;
  DEFB $45,$E0            ;
  DEFB $07,$44            ;
  DEFB $25,$60            ;
  DEFB $57,$E0            ;
  DEFB $23,$44            ;
  DEFB $00,$10            ;
  DEFB $00,$28            ;
  DEFB $0D,$10            ;
  DEFB $0C,$00            ;
  DEFB $00,$00            ;

; A bullet, third frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1C's frame 2.
bullet_c_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $00,$00            ; The lines, bottom first
  DEFB $00,$40            ;
  DEFB $10,$0A            ;
  DEFB $39,$04            ;
  DEFB $10,$40            ;
  DEFB $83,$A2            ;
  DEFB $15,$05            ;
  DEFB $02,$D2            ;
  DEFB $25,$A0            ;
  DEFB $03,$A2            ;
  DEFB $05,$40            ;
  DEFB $10,$10            ;
  DEFB $28,$00            ;
  DEFB $10,$80            ;
  DEFB $00,$08            ;
  DEFB $04,$00            ;

; A pellet
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 1 bytes by 8 lines, no mask, bottom line first. Type $1C's frame 4.
pellet_graphic:
  DEFB $01,$08            ; Width in bytes, height in lines
  DEFB $38                ; The lines, bottom first
  DEFB $5C                ;
  DEFB $5C                ;
  DEFB $2C                ;
  DEFB $28                ;
  DEFB $18                ;
  DEFB $18                ;
  DEFB $08                ;

; The refuel pick-up
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 1 bytes by 8 lines, no mask, bottom line first. Type $1C's frame 3.
refuel_pick_up_graphic:
  DEFB $01,$08            ; Width in bytes, height in lines
  DEFB $10                ; The lines, bottom first
  DEFB $10                ;
  DEFB $38                ;
  DEFB $28                ;
  DEFB $6C                ;
  DEFB $5C                ;
  DEFB $5C                ;
  DEFB $28                ;

; The fuel thief
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 1 bytes by 7 lines, no mask, bottom line first. Type $1C's frame 6.
fuel_thief_graphic:
  DEFB $01,$07            ; Width in bytes, height in lines
  DEFB $14                ; The lines, bottom first
  DEFB $36                ;
  DEFB $77                ;
  DEFB $00                ;
  DEFB $77                ;
  DEFB $36                ;
  DEFB $14                ;

; A tiny pellet
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 1 bytes by 3 lines, no mask, bottom line first. Type $1C's frames 5 and 7.
pellet_b_graphic:
  DEFB $01,$03            ; Width in bytes, height in lines
  DEFB $E0                ; The lines, bottom first
  DEFB $E0                ;
  DEFB $E0                ;

; A tumbling rock
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 0; frames
; 0-5 turn it.
rock_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $03,$F0            ; The lines, bottom first
  DEFB $05,$5C            ;
  DEFB $3A,$AA            ;
  DEFB $55,$56            ;
  DEFB $6A,$AC            ;
  DEFB $55,$57            ;
  DEFB $6A,$AB            ;
  DEFB $55,$55            ;
  DEFB $AA,$AB            ;
  DEFB $D5,$55            ;
  DEFB $6A,$AB            ;
  DEFB $55,$55            ;
  DEFB $4A,$AE            ;
  DEFB $75,$50            ;
  DEFB $2A,$A8            ;
  DEFB $1F,$F0            ;

; A tumbling rock, frame 1
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 1.
rock_b_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $01,$80            ; The lines, bottom first
  DEFB $1E,$E0            ;
  DEFB $15,$60            ;
  DEFB $1A,$B8            ;
  DEFB $35,$56            ;
  DEFB $6A,$AB            ;
  DEFB $D5,$55            ;
  DEFB $AA,$AB            ;
  DEFB $D5,$57            ;
  DEFB $AA,$BF            ;
  DEFB $D5,$FF            ;
  DEFB $6A,$FE            ;
  DEFB $34,$F8            ;
  DEFB $2B,$E0            ;
  DEFB $37,$E0            ;
  DEFB $0F,$C0            ;

; A tumbling rock, frame 2
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 2.
rock_c_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $00,$40            ; The lines, bottom first
  DEFB $1C,$78            ;
  DEFB $2B,$AC            ;
  DEFB $35,$54            ;
  DEFB $6A,$AC            ;
  DEFB $55,$5C            ;
  DEFB $6A,$BE            ;
  DEFB $55,$FF            ;
  DEFB $67,$FF            ;
  DEFB $5F,$FE            ;
  DEFB $BF,$FC            ;
  DEFB $9F,$FE            ;
  DEFB $7F,$FE            ;
  DEFB $5F,$FE            ;
  DEFB $3F,$F8            ;
  DEFB $0F,$C0            ;

; A tumbling rock, frame 3
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 3.
rock_d_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $03,$80            ; The lines, bottom first
  DEFB $07,$F0            ;
  DEFB $1F,$F8            ;
  DEFB $7F,$FC            ;
  DEFB $FF,$FC            ;
  DEFB $FF,$FC            ;
  DEFB $7F,$FE            ;
  DEFB $7F,$FE            ;
  DEFB $7F,$FF            ;
  DEFB $FF,$FF            ;
  DEFB $FF,$FE            ;
  DEFB $FF,$FE            ;
  DEFB $7F,$FE            ;
  DEFB $3F,$FE            ;
  DEFB $3F,$F0            ;
  DEFB $1E,$00            ;

; A tumbling rock, frame 4
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 4.
rock_e_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $03,$C0            ; The lines, bottom first
  DEFB $1F,$F8            ;
  DEFB $7F,$F8            ;
  DEFB $7F,$FC            ;
  DEFB $7F,$FF            ;
  DEFB $7F,$FF            ;
  DEFB $7F,$FF            ;
  DEFB $7F,$FF            ;
  DEFB $FF,$FD            ;
  DEFB $FF,$FB            ;
  DEFB $FF,$F7            ;
  DEFB $7F,$AA            ;
  DEFB $7F,$56            ;
  DEFB $7E,$AA            ;
  DEFB $3D,$5C            ;
  DEFB $0F,$E0            ;

; A tumbling rock, frame 5
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1D's frame 5.
rock_f_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $00,$00            ; The lines, bottom first
  DEFB $07,$F8            ;
  DEFB $0F,$FC            ;
  DEFB $0F,$F6            ;
  DEFB $1F,$EA            ;
  DEFB $1F,$D6            ;
  DEFB $3F,$AB            ;
  DEFB $7F,$55            ;
  DEFB $FE,$AB            ;
  DEFB $FD,$55            ;
  DEFB $FA,$AB            ;
  DEFB $7D,$55            ;
  DEFB $7A,$AE            ;
  DEFB $55,$50            ;
  DEFB $7A,$A0            ;
  DEFB $0F,$C0            ;

; A comet
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 22 lines, no mask, bottom line first. Type $1D's frame 6.
comet_graphic:
  DEFB $03,$16            ; Width in bytes, height in lines
  DEFB $00,$00,$14        ; The lines, bottom first
  DEFB $00,$01,$6A        ;
  DEFB $00,$00,$DA        ;
  DEFB $00,$01,$FD        ;
  DEFB $00,$0A,$FE        ;
  DEFB $00,$05,$DD        ;
  DEFB $00,$22,$F4        ;
  DEFB $00,$06,$F8        ;
  DEFB $00,$1F,$22        ;
  DEFB $00,$6A,$A0        ;
  DEFB $00,$16,$D8        ;
  DEFB $00,$79,$80        ;
  DEFB $00,$9A,$40        ;
  DEFB $01,$35,$00        ;
  DEFB $02,$E8,$00        ;
  DEFB $05,$48,$00        ;
  DEFB $02,$90,$00        ;
  DEFB $0C,$A0,$00        ;
  DEFB $16,$40,$00        ;
  DEFB $0D,$80,$00        ;
  DEFB $1A,$00,$00        ;
  DEFB $40,$00,$00        ;

; A comet, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 23 lines, no mask, bottom line first. Type $1D's frame 7.
comet_b_graphic:
  DEFB $03,$17            ; Width in bytes, height in lines
  DEFB $00,$00,$12        ; The lines, bottom first
  DEFB $00,$02,$7D        ;
  DEFB $00,$01,$BA        ;
  DEFB $00,$00,$FC        ;
  DEFB $00,$05,$5F        ;
  DEFB $00,$01,$AB        ;
  DEFB $00,$13,$56        ;
  DEFB $00,$02,$F6        ;
  DEFB $00,$0D,$68        ;
  DEFB $00,$02,$B2        ;
  DEFB $00,$14,$A8        ;
  DEFB $00,$0E,$40        ;
  DEFB $00,$53,$00        ;
  DEFB $00,$0C,$20        ;
  DEFB $00,$A9,$00        ;
  DEFB $00,$04,$00        ;
  DEFB $02,$50,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $01,$40,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $04,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $20,$00,$00        ;

; A heap of debris
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 27 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
debris_graphic:
  DEFB $03,$1B            ; Width in bytes, height in lines
  DEFB $00,$38,$01        ; The lines, bottom first
  DEFB $00,$FB,$02        ;
  DEFB $00,$BB,$C5        ;
  DEFB $30,$DB,$F2        ;
  DEFB $28,$6B,$FD        ;
  DEFB $1C,$74,$FE        ;
  DEFB $0E,$4F,$39        ;
  DEFB $07,$3F,$C2        ;
  DEFB $03,$86,$05        ;
  DEFB $00,$39,$FA        ;
  DEFB $01,$FA,$49        ;
  DEFB $0F,$C7,$BE        ;
  DEFB $16,$28,$4D        ;
  DEFB $00,$EF,$52        ;
  DEFB $03,$F6,$DD        ;
  DEFB $0F,$E5,$EA        ;
  DEFB $3F,$D9,$F5        ;
  DEFB $1F,$3C,$0A        ;
  DEFB $2C,$67,$D5        ;
  DEFB $30,$9B,$CA        ;
  DEFB $00,$7B,$D5        ;
  DEFB $01,$F5,$82        ;
  DEFB $01,$ED,$B9        ;
  DEFB $00,$EE,$BA        ;
  DEFB $00,$DE,$39        ;
  DEFB $00,$5C,$28        ;
  DEFB $00,$70,$11        ;

; A small craft
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 13 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
small_craft_graphic:
  DEFB $03,$0D            ; Width in bytes, height in lines
  DEFB $00,$0C,$00        ; The lines, bottom first
  DEFB $00,$6C,$00        ;
  DEFB $00,$6D,$B8        ;
  DEFB $23,$6F,$7C        ;
  DEFB $33,$6C,$FE        ;
  DEFB $3B,$6D,$FF        ;
  DEFB $F7,$FD,$3F        ;
  DEFB $28,$6D,$3F        ;
  DEFB $10,$0C,$9E        ;
  DEFB $23,$03,$4C        ;
  DEFB $00,$61,$B8        ;
  DEFB $00,$6C,$00        ;
  DEFB $00,$0C,$00        ;

; The tough attacker
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 12 lines, no mask, bottom line first. Type $19's frame 0.
tough_attacker_graphic:
  DEFB $05,$0C            ; Width in bytes, height in lines
  DEFB $01,$F0,$F8,$00,$00 ; The lines, bottom first
  DEFB $1B,$99,$CD,$F8,$00 ;
  DEFB $17,$CD,$E5,$FF,$80 ;
  DEFB $37,$C5,$E5,$FF,$E0 ;
  DEFB $3F,$FF,$FF,$FF,$F8 ;
  DEFB $00,$00,$00,$00,$06 ;
  DEFB $3F,$FF,$FF,$FF,$F8 ;
  DEFB $3F,$E0,$07,$FF,$E0 ;
  DEFB $1F,$FF,$E4,$01,$80 ;
  DEFB $0F,$80,$3D,$FE,$00 ;
  DEFB $01,$FF,$3D,$F0,$00 ;
  DEFB $00,$03,$FD,$80,$00 ;

; The tough attacker, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 12 lines, no mask, bottom line first. Type $19's frame 1.
tough_attacker_b_graphic:
  DEFB $05,$0C            ; Width in bytes, height in lines
  DEFB $00,$00,$1F,$0F,$80 ; The lines, bottom first
  DEFB $00,$1F,$B3,$99,$D8 ;
  DEFB $01,$FF,$A7,$B3,$E8 ;
  DEFB $07,$FF,$A7,$A3,$EC ;
  DEFB $1F,$FF,$FF,$FF,$FC ;
  DEFB $60,$00,$00,$00,$00 ;
  DEFB $1F,$FF,$FF,$FF,$FC ;
  DEFB $07,$FF,$E0,$07,$FC ;
  DEFB $01,$80,$27,$FF,$F8 ;
  DEFB $00,$7F,$BC,$01,$F0 ;
  DEFB $00,$0F,$BC,$FF,$80 ;
  DEFB $00,$01,$BF,$C0,$00 ;

; An eye pod
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 14 lines, no mask, bottom line first. Type $17's frame 0.
eye_pod_graphic:
  DEFB $02,$0E            ; Width in bytes, height in lines
  DEFB $1E,$F8            ; The lines, bottom first
  DEFB $3D,$FC            ;
  DEFB $70,$02            ;
  DEFB $C3,$FF            ;
  DEFB $C3,$FF            ;
  DEFB $C3,$FF            ;
  DEFB $80,$01            ;
  DEFB $4C,$32            ;
  DEFB $4C,$32            ;
  DEFB $C3,$FF            ;
  DEFB $31,$FC            ;
  DEFB $0C,$F0            ;
  DEFB $01,$80            ;
  DEFB $01,$80            ;

; An eye pod, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 13 lines, no mask, bottom line first. Type $17's frame 1.
eye_pod_b_graphic:
  DEFB $02,$0D            ; Width in bytes, height in lines
  DEFB $00,$00            ; The lines, bottom first
  DEFB $00,$00            ;
  DEFB $1E,$F8            ;
  DEFB $3C,$FC            ;
  DEFB $70,$02            ;
  DEFB $C1,$FF            ;
  DEFB $00,$00            ;
  DEFB $B3,$CD            ;
  DEFB $B3,$CD            ;
  DEFB $C3,$FF            ;
  DEFB $33,$FC            ;
  DEFB $0C,$F0            ;
  DEFB $01,$80            ;

; A saucer
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 10 lines, no mask, bottom line first. Type $16's frame 0; frames
; 0-3 spin it.
saucer_graphic:
  DEFB $03,$0A            ; Width in bytes, height in lines
  DEFB $02,$83,$40        ; The lines, bottom first
  DEFB $06,$E7,$60        ;
  DEFB $0F,$66,$F0        ;
  DEFB $1F,$00,$F8        ;
  DEFB $7F,$FF,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $E0,$FF,$FF        ;
  DEFB $78,$7F,$FE        ;
  DEFB $0F,$3F,$F0        ;
  DEFB $00,$DF,$00        ;

; A saucer, frame 1
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 10 lines, no mask, bottom line first. Type $16's frame 1.
saucer_b_graphic:
  DEFB $03,$0A            ; Width in bytes, height in lines
  DEFB $01,$C3,$80        ; The lines, bottom first
  DEFB $03,$E7,$C0        ;
  DEFB $07,$EF,$E0        ;
  DEFB $00,$1F,$F0        ;
  DEFB $7F,$FF,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $E0,$FF,$FF        ;
  DEFB $78,$7F,$FE        ;
  DEFB $0F,$3F,$F0        ;
  DEFB $00,$DF,$00        ;

; A saucer, frame 2
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 10 lines, no mask, bottom line first. Type $16's frame 2.
saucer_c_graphic:
  DEFB $03,$0A            ; Width in bytes, height in lines
  DEFB $00,$3C,$00        ; The lines, bottom first
  DEFB $00,$7E,$00        ;
  DEFB $00,$FF,$00        ;
  DEFB $01,$FF,$80        ;
  DEFB $7F,$FF,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $E0,$FF,$FF        ;
  DEFB $78,$7F,$FE        ;
  DEFB $0F,$3F,$F0        ;
  DEFB $00,$DF,$00        ;

; A saucer, frame 3
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 10 lines, no mask, bottom line first. Type $16's frame 3.
saucer_d_graphic:
  DEFB $03,$0A            ; Width in bytes, height in lines
  DEFB $01,$C3,$80        ; The lines, bottom first
  DEFB $03,$E7,$C0        ;
  DEFB $07,$F7,$E0        ;
  DEFB $0F,$F8,$00        ;
  DEFB $7F,$FF,$FE        ;
  DEFB $00,$00,$00        ;
  DEFB $E0,$FF,$FF        ;
  DEFB $78,$7F,$FE        ;
  DEFB $0F,$3F,$F0        ;
  DEFB $00,$DF,$00        ;

; A pillar
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 18 lines, no mask, bottom line first. Type $18's frame 0.
pillar_graphic:
  DEFB $02,$12            ; Width in bytes, height in lines
  DEFB $01,$80            ; The lines, bottom first
  DEFB $03,$C0            ;
  DEFB $07,$E0            ;
  DEFB $07,$E0            ;
  DEFB $07,$E0            ;
  DEFB $07,$E0            ;
  DEFB $07,$E0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;
  DEFB $03,$C0            ;

; An arch
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 18 lines, no mask, bottom line first. Type $18's frame 2.
pillar_b_graphic:
  DEFB $02,$12            ; Width in bytes, height in lines
  DEFB $01,$80            ; The lines, bottom first
  DEFB $03,$C0            ;
  DEFB $06,$60            ;
  DEFB $0C,$30            ;
  DEFB $18,$18            ;
  DEFB $19,$98            ;
  DEFB $33,$CC            ;
  DEFB $37,$EC            ;
  DEFB $66,$66            ;
  DEFB $6C,$36            ;
  DEFB $6C,$36            ;
  DEFB $68,$16            ;
  DEFB $D8,$1B            ;
  DEFB $D8,$1B            ;
  DEFB $D8,$1B            ;
  DEFB $D8,$1B            ;
  DEFB $F8,$1F            ;
  DEFB $F0,$0F            ;

; A pillar, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 18 lines, no mask, bottom line first. Type $18's frames 1 and 3.
pillar_c_graphic:
  DEFB $02,$12            ; Width in bytes, height in lines
  DEFB $01,$80            ; The lines, bottom first
  DEFB $03,$C0            ;
  DEFB $07,$E0            ;
  DEFB $07,$E0            ;
  DEFB $0F,$F0            ;
  DEFB $0F,$F0            ;
  DEFB $0F,$F0            ;
  DEFB $0F,$F0            ;
  DEFB $1E,$78            ;
  DEFB $1E,$78            ;
  DEFB $1E,$78            ;
  DEFB $1E,$78            ;
  DEFB $1E,$78            ;
  DEFB $1C,$38            ;
  DEFB $1C,$38            ;
  DEFB $1C,$38            ;
  DEFB $1C,$38            ;
  DEFB $1C,$38            ;

; The special beam, frame 0
;
; Eight cells of the special beam in this frame: expanded by expand_beam_frame
; and drawn by draw_beam, as the beam is drawn in play (three times the size).
;
; The special beam's frames (update_special_beam) are these four. 5 lines of 2
; bytes, in the height-first form: no width byte. Entry 0 of the frame table at
; beam_frames.
ring_graphic:
  DEFB $05                ; Height in lines
  DEFB $00,$00            ; The lines
  DEFB $00,$00            ;
  DEFB $00,$00            ;
  DEFB $FF,$FF            ;
  DEFB $FF,$FF            ;

; The special beam, frame 1
;
; Eight cells of the special beam in this frame: expanded by expand_beam_frame
; and drawn by draw_beam, as the beam is drawn in play (three times the size).
;
; 6 lines of 2 bytes, in the height-first form: no width byte. Entries 1 and 5
; of the frame table at beam_frames.
ring_b_graphic:
  DEFB $06                ; Height in lines
  DEFB $00,$00            ; The lines
  DEFB $00,$00            ;
  DEFB $00,$3C            ;
  DEFB $C3,$FF            ;
  DEFB $FF,$C3            ;
  DEFB $3C,$00            ;

; The special beam, frame 2
;
; Eight cells of the special beam in this frame: expanded by expand_beam_frame
; and drawn by draw_beam, as the beam is drawn in play (three times the size).
;
; 7 lines of 2 bytes, in the height-first form: no width byte. Entries 2 and 4
; of the frame table at beam_frames.
ring_c_graphic:
  DEFB $07                ; Height in lines
  DEFB $00,$00            ; The lines
  DEFB $00,$18            ;
  DEFB $00,$7E            ;
  DEFB $81,$E7            ;
  DEFB $E7,$81            ;
  DEFB $7E,$00            ;
  DEFB $18,$00            ;

; The special beam, frame 3
;
; Eight cells of the special beam in this frame: expanded by expand_beam_frame
; and drawn by draw_beam, as the beam is drawn in play (three times the size).
;
; 8 lines of 2 bytes, in the height-first form: no width byte. Entry 3 of the
; frame table at beam_frames.
ring_d_graphic:
  DEFB $08                ; Height in lines
  DEFB $00,$3C            ; The lines
  DEFB $00,$7E            ;
  DEFB $00,$E7            ;
  DEFB $81,$C3            ;
  DEFB $C3,$81            ;
  DEFB $E7,$00            ;
  DEFB $7E,$00            ;
  DEFB $3C,$00            ;

; More debris
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 27 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
debris_b_graphic:
  DEFB $03,$1B            ; Width in bytes, height in lines
  DEFB $80,$1C,$00        ; The lines, bottom first
  DEFB $40,$DF,$00        ;
  DEFB $A3,$DD,$00        ;
  DEFB $4F,$DB,$0C        ;
  DEFB $BF,$D6,$14        ;
  DEFB $7F,$2E,$38        ;
  DEFB $9C,$F2,$70        ;
  DEFB $43,$FC,$E0        ;
  DEFB $A0,$61,$C0        ;
  DEFB $5F,$9C,$00        ;
  DEFB $92,$5F,$80        ;
  DEFB $7D,$E3,$F0        ;
  DEFB $B2,$14,$68        ;
  DEFB $4A,$F7,$00        ;
  DEFB $BB,$6F,$C0        ;
  DEFB $57,$A7,$F0        ;
  DEFB $AF,$9B,$FC        ;
  DEFB $50,$3C,$F8        ;
  DEFB $AB,$E6,$34        ;
  DEFB $53,$D9,$0C        ;
  DEFB $AB,$DE,$00        ;
  DEFB $41,$AF,$80        ;
  DEFB $9D,$B7,$80        ;
  DEFB $5D,$77,$00        ;
  DEFB $9C,$7B,$00        ;
  DEFB $14,$3A,$00        ;
  DEFB $88,$0E,$00        ;

; More debris
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 22 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
debris_c_graphic:
  DEFB $03,$16            ; Width in bytes, height in lines
  DEFB $0A,$AD,$AA        ; The lines, bottom first
  DEFB $C5,$50,$54        ;
  DEFB $A0,$A7,$80        ;
  DEFB $70,$6F,$AF        ;
  DEFB $39,$DF,$B7        ;
  DEFB $07,$DF,$B7        ;
  DEFB $1F,$D8,$30        ;
  DEFB $BF,$87,$D6        ;
  DEFB $BE,$27,$D7        ;
  DEFB $D9,$5B,$A6        ;
  DEFB $E7,$5B,$75        ;
  DEFB $1E,$DC,$F3        ;
  DEFB $1C,$EA,$F0        ;
  DEFB $3D,$A7,$2B        ;
  DEFB $3D,$87,$CB        ;
  DEFB $03,$48,$0B        ;
  DEFB $35,$4C,$6B        ;
  DEFB $2C,$8C,$6C        ;
  DEFB $22,$8C,$C3        ;
  DEFB $3E,$8C,$CE        ;
  DEFB $00,$0D,$86        ;
  DEFB $00,$01,$80        ;

; A small craft
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 13 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
small_craft_b_graphic:
  DEFB $03,$0D            ; Width in bytes, height in lines
  DEFB $00,$30,$00        ; The lines, bottom first
  DEFB $00,$36,$00        ;
  DEFB $1D,$B6,$00        ;
  DEFB $3E,$F6,$C4        ;
  DEFB $7F,$36,$CC        ;
  DEFB $BF,$B6,$DC        ;
  DEFB $9F,$BF,$EF        ;
  DEFB $9F,$B6,$14        ;
  DEFB $4F,$30,$08        ;
  DEFB $26,$C0,$C4        ;
  DEFB $1D,$86,$00        ;
  DEFB $00,$36,$00        ;
  DEFB $00,$30,$00        ;

; A craft in a cloud
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 20 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
cloud_craft_graphic:
  DEFB $05,$14            ; Width in bytes, height in lines
  DEFB $00,$14,$14,$05,$00 ; The lines, bottom first
  DEFB $00,$2A,$2A,$2A,$80 ;
  DEFB $00,$55,$55,$55,$40 ;
  DEFB $0A,$BE,$BE,$AA,$A0 ;
  DEFB $35,$FF,$FF,$DF,$D0 ;
  DEFB $2F,$FF,$FF,$FF,$E8 ;
  DEFB $7F,$FF,$FF,$FF,$E4 ;
  DEFB $7F,$FE,$EE,$FF,$EA ;
  DEFB $3F,$FC,$00,$FF,$DE ;
  DEFB $0E,$7C,$22,$7F,$BE ;
  DEFB $3E,$B1,$55,$3F,$7C ;
  DEFB $7E,$88,$88,$80,$FC ;
  DEFB $7E,$FF,$FF,$FF,$FC ;
  DEFB $7F,$00,$00,$00,$78 ;
  DEFB $37,$FF,$FF,$FF,$80 ;
  DEFB $07,$FF,$FF,$FF,$E0 ;
  DEFB $07,$FF,$FF,$DF,$00 ;
  DEFB $03,$FF,$FF,$CE,$00 ;
  DEFB $01,$DF,$BF,$80,$00 ;
  DEFB $00,$0F,$1F,$00,$00 ;

; A domed robot
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 15 lines, no mask, bottom line first. Type $1B's frame 2.
dome_robot_graphic:
  DEFB $02,$0F            ; Width in bytes, height in lines
  DEFB $F5,$51            ; The lines, bottom first
  DEFB $F2,$A1            ;
  DEFB $F5,$51            ;
  DEFB $00,$00            ;
  DEFB $4C,$32            ;
  DEFB $4C,$32            ;
  DEFB $4C,$32            ;
  DEFB $00,$00            ;
  DEFB $67,$FE            ;
  DEFB $27,$FC            ;
  DEFB $13,$F8            ;
  DEFB $0D,$F0            ;
  DEFB $00,$00            ;
  DEFB $02,$C0            ;
  DEFB $01,$80            ;

; A domed robot, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 15 lines, no mask, bottom line first. Type $1B's frame 3.
dome_robot_b_graphic:
  DEFB $02,$0F            ; Width in bytes, height in lines
  DEFB $F5,$51            ; The lines, bottom first
  DEFB $F2,$A1            ;
  DEFB $F5,$51            ;
  DEFB $00,$00            ;
  DEFB $B3,$CD            ;
  DEFB $B3,$CD            ;
  DEFB $B3,$CD            ;
  DEFB $00,$00            ;
  DEFB $67,$FE            ;
  DEFB $27,$FC            ;
  DEFB $13,$F8            ;
  DEFB $0D,$F0            ;
  DEFB $00,$00            ;
  DEFB $02,$C0            ;
  DEFB $01,$80            ;

; A lamp
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1B's frame 0.
lamp_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $02,$A0            ; The lines, bottom first
  DEFB $04,$80            ;
  DEFB $11,$50            ;
  DEFB $05,$40            ;
  DEFB $00,$00            ;
  DEFB $13,$F8            ;
  DEFB $09,$F0            ;
  DEFB $04,$E0            ;
  DEFB $70,$0E            ;
  DEFB $02,$C0            ;
  DEFB $0A,$C8            ;
  DEFB $11,$84            ;
  DEFB $24,$22            ;
  DEFB $09,$90            ;
  DEFB $09,$90            ;
  DEFB $01,$80            ;

; A lamp, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 2 bytes by 16 lines, no mask, bottom line first. Type $1B's frame 1.
lamp_b_graphic:
  DEFB $02,$10            ; Width in bytes, height in lines
  DEFB $02,$80            ; The lines, bottom first
  DEFB $05,$40            ;
  DEFB $02,$A0            ;
  DEFB $05,$40            ;
  DEFB $0A,$A0            ;
  DEFB $13,$F8            ;
  DEFB $09,$F0            ;
  DEFB $04,$E0            ;
  DEFB $70,$0E            ;
  DEFB $02,$C0            ;
  DEFB $0A,$C8            ;
  DEFB $11,$84            ;
  DEFB $24,$22            ;
  DEFB $09,$90            ;
  DEFB $09,$90            ;
  DEFB $01,$80            ;

; A fuel station
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 24 lines, no mask, bottom line first. Type $1B's frame 4.
fuel_station_graphic:
  DEFB $03,$18            ; Width in bytes, height in lines
  DEFB $00,$00,$40        ; The lines, bottom first
  DEFB $00,$01,$00        ;
  DEFB $02,$00,$A8        ;
  DEFB $04,$05,$40        ;
  DEFB $03,$00,$90        ;
  DEFB $0A,$83,$D8        ;
  DEFB $07,$86,$B0        ;
  DEFB $0D,$81,$38        ;
  DEFB $05,$05,$30        ;
  DEFB $00,$00,$00        ;
  DEFB $27,$E4,$FC        ;
  DEFB $27,$E4,$FC        ;
  DEFB $13,$C2,$78        ;
  DEFB $0B,$81,$70        ;
  DEFB $08,$81,$10        ;
  DEFB $1F,$FF,$F8        ;
  DEFB $3F,$FF,$FC        ;
  DEFB $4B,$88,$92        ;
  DEFB $9B,$AB,$B9        ;
  DEFB $99,$A9,$B9        ;
  DEFB $9B,$AB,$B9        ;
  DEFB $48,$A8,$B2        ;
  DEFB $3F,$FF,$FC        ;
  DEFB $1F,$FF,$F8        ;

; A fuel station, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 24 lines, no mask, bottom line first. Type $1B's frame 5.
fuel_station_b_graphic:
  DEFB $03,$18            ; Width in bytes, height in lines
  DEFB $01,$00,$00        ; The lines, bottom first
  DEFB $04,$80,$40        ;
  DEFB $12,$40,$00        ;
  DEFB $0F,$00,$40        ;
  DEFB $13,$C2,$88        ;
  DEFB $2E,$60,$A0        ;
  DEFB $1A,$C0,$50        ;
  DEFB $27,$21,$A0        ;
  DEFB $14,$C0,$A0        ;
  DEFB $00,$00,$00        ;
  DEFB $27,$E4,$FC        ;
  DEFB $27,$E4,$FC        ;
  DEFB $13,$C2,$78        ;
  DEFB $0B,$81,$70        ;
  DEFB $08,$81,$10        ;
  DEFB $1F,$FF,$F8        ;
  DEFB $3F,$FF,$FC        ;
  DEFB $4B,$88,$92        ;
  DEFB $9B,$AB,$B9        ;
  DEFB $99,$A9,$B9        ;
  DEFB $9B,$AB,$B9        ;
  DEFB $48,$A8,$B2        ;
  DEFB $3F,$FF,$FC        ;
  DEFB $1F,$FF,$F8        ;

; A craft hanging from a pylon
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 5 bytes by 16 lines, no mask, bottom line first. One of type $0D's frames
; (the list in type0d_graphics).
pylon_craft_graphic:
  DEFB $05,$10            ; Width in bytes, height in lines
  DEFB $00,$00,$2C,$00,$00 ; The lines, bottom first
  DEFB $00,$00,$2C,$00,$00 ;
  DEFB $00,$00,$2C,$00,$00 ;
  DEFB $00,$00,$6E,$00,$00 ;
  DEFB $00,$00,$CF,$00,$00 ;
  DEFB $00,$00,$9F,$00,$00 ;
  DEFB $00,$00,$00,$00,$00 ;
  DEFB $0C,$0B,$FF,$FF,$60 ;
  DEFB $18,$37,$FF,$FF,$D8 ;
  DEFB $00,$00,$00,$00,$00 ;
  DEFB $E0,$55,$FF,$FF,$FB ;
  DEFB $E0,$2A,$FF,$FF,$FB ;
  DEFB $E0,$55,$FF,$FF,$FB ;
  DEFB $00,$00,$00,$00,$00 ;
  DEFB $38,$15,$7F,$FF,$EC ;
  DEFB $0E,$0A,$BF,$FF,$B0 ;

; A ringed planet
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; 3 bytes by 22 lines, no mask, bottom line first. The last of type $0D's
; frames (the list in type0d_graphics).
ringed_planet_graphic:
  DEFB $03,$16            ; Width in bytes, height in lines
  DEFB $78,$00,$00        ; The lines, bottom first
  DEFB $67,$00,$00        ;
  DEFB $5B,$C0,$00        ;
  DEFB $5F,$E0,$00        ;
  DEFB $3D,$F4,$00        ;
  DEFB $38,$7B,$00        ;
  DEFB $3D,$95,$86        ;
  DEFB $1B,$EA,$C6        ;
  DEFB $0B,$F5,$40        ;
  DEFB $07,$9A,$A0        ;
  DEFB $07,$0D,$40        ;
  DEFB $07,$9E,$A0        ;
  DEFB $07,$FF,$50        ;
  DEFB $03,$FD,$A0        ;
  DEFB $03,$67,$90        ;
  DEFB $01,$E7,$A8        ;
  DEFB $00,$FF,$14        ;
  DEFB $00,$3C,$A0        ;
  DEFB $00,$03,$5A        ;
  DEFB $00,$01,$DA        ;
  DEFB $00,$00,$E6        ;
  DEFB $00,$00,$1E        ;

; Adjust a landscape run's start and width for its end pieces
;
; Used by the routine at fill_behind_moving_piece.
;
; Called by fill_behind_moving_piece (when the byte at piece_rise is 0) just
; before it draws a run of the landscape. piece_extra_a and piece_extra_b hold
; the codes of the run's first and last pieces (piece_ends sets them from the
; map); pieces $90-$93 are ends that need the run moved or widened, depending
; on the scroll step across (the second byte of scroll_down) and on E, which is
; 0 or $AA (fill_behind_moving_piece sets DE to 0 or $55AA, the dither's
; phase). B is added to the run's x and C to its width.
;
; The width's base is the operand of the LD A,n near the end of this routine,
; which fill_behind_moving_piece writes as its own width less one. The result
; goes to piece_step_y and HL is left at the run's screen position from
; saved_speed with x moved.
;
; How the pieces look and why each case needs its adjustment is not yet worked
; out; a first piece of $93 never came up in the build's sessions.
;
;   IX The landscape strip's record (nothing is done when +1 is 0)
;   E 0 or $AA
; O:HL The run's screen position, x moved by B
; O:A Its width, also in piece_step_y
landscape_end_pieces:
  LD A,(IX+$01)           ; Nothing for a strip whose +1 is 0
  AND A                   ;
  RET Z                   ;
  LD HL,(piece_extra_a)   ; The first and last pieces' codes; B and C from them
  LD BC,$0000             ; (first_piece_shift, last_piece_width)
  CALL first_piece_shift  ;
  CALL last_piece_width   ;
  LD A,L                       ; First piece $91 with the last below $80 and
  CP $91                       ; E=0: one wider
  JR NZ,landscape_end_pieces_0 ;
  BIT 7,H                      ;
  JR NZ,landscape_end_pieces_0 ;
  LD A,E                       ;
  AND A                        ;
  JR NZ,landscape_end_pieces_0 ;
  INC C                        ;
landscape_end_pieces_0:
  LD A,L                       ; First piece $93: one wider when E=0, if the
  CP $93                       ; landscape scrolls this pass or saved_speed is
  JR NZ,landscape_end_pieces_2 ; not on a byte boundary
  LD A,(scroll_across)         ;
  AND A                        ;
  JR NZ,landscape_end_pieces_1 ;
  LD A,(saved_speed)           ;
  AND $07                      ;
  JR Z,landscape_end_pieces_2  ;
landscape_end_pieces_1:
  LD A,E                       ;
  AND A                        ;
  JR NZ,landscape_end_pieces_2 ;
  INC C                        ;
landscape_end_pieces_2:
  LD HL,(saved_speed)     ; The run's position, x moved by B
  LD A,L                  ;
  ADD A,B                 ;
  LD L,A                  ;
  LD A,$00                ; Width = the base fill_behind_moving_piece wrote
  ADD A,C                 ; into the operand + C
  LD C,A                  ;
  LD (piece_step_y),A     ;
  RET                     ;

; Widen a landscape run for its last piece
;
; Used by the routine at landscape_end_pieces.
;
; Part of landscape_end_pieces: one wider when the last piece (piece_extra_b)
; is $93 and E is 0, or $92 and E is not 0.
;
; E 0 or $AA
; C The width so far
last_piece_width:
  LD A,(piece_extra_b)    ; Only pieces $92 and $93
  CP $93                  ;
  JR Z,last_piece_width_1 ;
  CP $92                  ;
  JR Z,last_piece_width_0 ;
  RET                     ;
last_piece_width_0:
  LD A,E                  ; $92: one wider unless E=0
  AND A                   ;
  RET Z                   ;
  INC C                   ;
  RET                     ;
last_piece_width_1:
  LD A,E                  ; $93: one wider when E=0
  AND A                   ;
  RET NZ                  ;
  INC C                   ;
  RET                     ;

; Move a landscape run's start for its first piece
;
; Used by the routine at landscape_end_pieces.
;
; Part of landscape_end_pieces, for the first piece (piece_extra_a). $90: B=7
; when E=0; otherwise B is minus the scroll step across (the second byte of
; scroll_down) when that is positive, with the run one wider, and 0 when it is
; not. $91: B=7 unless E=0, when it is minus the scroll step. Other pieces
; leave B at 0.
;
; L holds piece_extra_a itself here, so the AND 7 near the end always gives 0
; for piece $90 and the run is always one wider on that path.
;
;   E 0 or $AA
; O:B The shift in x
first_piece_shift:
  LD A,(piece_extra_a)     ; Only pieces $90 and $91
  CP $90                   ;
  JR Z,first_piece_shift_0 ;
  CP $91                   ;
  RET NZ                   ;
  LD A,E                   ; $91
  AND A                    ;
  JR Z,first_piece_shift_1 ;
  LD B,$07                 ;
  RET                      ;
first_piece_shift_0:
  LD B,$07                ; $90 with E=0: 7
  LD A,E                  ;
  AND A                   ;
  RET Z                   ;
  LD B,$00                ; Otherwise 0 unless the scroll step is positive...
  LD A,(scroll_across)    ;
  BIT 7,A                 ;
  RET NZ                  ;
  AND A                   ;
  RET Z                   ;
  LD A,L                    ; ...then one wider
  AND $07                   ;
  JR NZ,first_piece_shift_1 ;
  INC C                     ;
first_piece_shift_1:
  LD A,(scroll_across)    ; And B is minus the scroll step
  NEG                     ;
  LD B,A                  ;
  RET                     ;

; Print B strings
;
; Used by the routines at planet_secured, draw_panel and menu.
;
; Calls print_string B times. Each string is x, line (its bottom), attribute,
; length and the characters (0-25 the letters, $1A-$23 the digits, $26 a
; space), and print_string leaves DE at the next one, so a list of them prints
; in one call. Used by planet_secured, draw_panel and the menu (menu).
;
; DE The first string
; B How many
print_strings:
  PUSH BC                 ; Print one and move on
  CALL print_string       ;
  POP BC                  ;
  DJNZ print_strings      ;
  RET                     ;

; Show the planet-secured screen, award the bonus, and move on to the next
; planet
;
; Used by the routine at level_done.
;
; Called by level_done once the rocket has launched. Prints the four lines at
; planet_secured_lines and the name of the planet just finished (one of five,
; planet_names; +$12 of the current player's record, score, is its number) into
; planet_secured_name_text, then the four lines from next_planet_lines with the
; next planet's name copied into next_planet_name_text -- after the fifth
; planet the first comes round again.
;
; It then resets the rocket's base position (rocket_x), sets the player's +$1A
; and +$1B to 6, and counts a bonus of ten times 5000 (BCD, the score adder in
; test_player_shots) onto the score with a zap (sound_zap) and a pause after
; each. Last it deals the player's last pick-up place again (the second entry
; point of deal_places, with IY at the dealing list's last entry and DE at the
; current player's last place, +$65 of score) and clears the player's own gone
; bits in the 290 world objects' kinds (mask_world_objects) with a mask worked
; out from the player in play (player_in_play): $7F for player 1 (bit 7), $BF
; for player 2 (bit 6).
planet_secured:
  LD DE,planet_secured_lines ; The first four lines
  LD B,$04                   ;
  CALL print_strings         ;
  LD A,(level)                   ; Copy this planet's name (16 bytes) into
  ADD A,A                        ; planet_secured_name_text
  ADD A,A                        ;
  ADD A,A                        ;
  ADD A,A                        ;
  LD HL,planet_names             ;
  CALL add_a_to_hl               ;
  LD DE,planet_secured_name_text ;
  LD C,$10                       ;
  LDIR                           ;
  LD A,(level)            ; After the last planet the next is the first (number
  CP $04                  ; $FF, made 0 below)
  JR NZ,planet_secured_0  ;
  LD A,$FF                ;
  LD (level),A            ;
  LD HL,planet_names      ;
planet_secured_0:
  LD DE,next_planet_name_text ; Copy the next planet's name (15 bytes) into
  LD C,$0F                    ; next_planet_name_text
  LDIR                        ;
  LD DE,planet_secured_name ; Print this planet's name
  CALL print_string         ;
  LD HL,$5268             ; The rocket's base position
  LD (rocket_x),HL        ;
  LD HL,$0A73             ;
  LD (rocket_y),HL        ;
  LD A,(level)            ; Next planet
  INC A                   ;
  LD (level),A            ;
  LD B,$04                ; The four lines from next_planet_lines
  CALL print_strings      ;
  LD A,$06                   ; The player's +$1A and +$1B to 6
  LD (rocket_part_carried),A ;
  LD (parts_built),A         ;
  LD B,$0A                ; Ten times: 5000 points, a zap and a pause
planet_secured_1:
  PUSH BC                 ;
  LD BC,$5000             ;
  CALL add_score          ;
  CALL sound_zap          ;
  LD D,$FF                ;
  CALL delay              ;
  POP BC                  ;
  DJNZ planet_secured_1   ;
  LD DE,place_last        ; Deal one pick-up place again
  LD IY,dealing_list_last ;
  CALL deal_places_0      ;
  LD A,(player_in_play)   ; A mask from the player in play (player_in_play) for
  INC A                   ; the world objects' kinds (mask_world_objects): $7F
  XOR $03                 ; for player 1, $BF for player 2
  CPL                     ;
  RRCA                    ;
  RRCA                    ;
  LD E,A                  ;
  JP mask_world_objects   ;

; The five planets' names
;
; Five names of 16 characters in the game's character codes (0-25 the letters,
; $1A-$23 the digits, $26 a space), centred with spaces, in the order the
; planets are played. planet_secured copies the finished planet's into
; planet_secured_name_text and the next one's into next_planet_name_text.
planet_names:
  DEFB $26,$26,$26,$26,$13,$08,$13,$00,$0D,$26,$1E,$26,$26,$26,$26,$26
  DEFB $26,$26,$26,$26,$06,$00,$0C,$0C,$00,$26,$1C,$21,$26,$26,$26,$26
  DEFB $26,$26,$26,$0F,$11,$0E,$13,$0E,$0D,$08,$14,$0C,$26,$26,$26,$26
  DEFB $26,$0C,$04,$06,$00,$0B,$0E,$0D,$26,$0F,$11,$08,$0C,$04,$26,$26
  DEFB $19,$00,$11,$13,$00,$0D,$26,$0F,$04,$0D,$03,$14,$0B,$0E,$12,$26

; The planet-secured screen's first four lines
;
; Four strings for print_strings: x, line (bottom), attribute, length, then the
; characters (game codes, see planet_names). Congratulations, in white, then
; three lines in yellow.
planet_secured_lines:
  DEFB $40,$27,$47,$0F
  DEFB $02,$0E,$0D,$06,$11,$00,$13,$14,$0B,$00,$13,$08,$0E,$0D,$12
  DEFB $18,$3F,$46,$17
  DEFB $18,$0E,$14,$26,$07,$00,$15,$04,$26,$12,$04,$02
  DEFB $14,$11,$04,$03,$26,$00,$0B,$0B,$26,$0E,$05
  DEFB $20,$4F,$46,$16
  DEFB $13,$07,$04,$26,$02,$18,$01,$04,$11,$13,$11
  DEFB $0E,$0D,$26,$02,$11,$18,$12,$13,$00,$0B,$12
  DEFB $48,$5F,$46,$0C
  DEFB $00,$15,$00,$08,$0B,$00,$01,$0B,$04,$26,$0E,$0D

; The planet-secured screen's planet name
;
; The head of one string for print_string (see planet_secured_lines): x, line,
; attribute (white) and length, 16. The characters are
; planet_secured_name_text.
planet_secured_name:
  DEFB $38,$6F,$47,$10

; The name of the planet just finished
;
; 16 characters, which planet_secured copies from planet_names before printing
; planet_secured_name.
planet_secured_name_text:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00

; The planet-secured screen's last four lines
;
; Four strings for print_strings (see planet_secured_lines): a line saying
; where to go next, then the next planet's name in green, whose 15 characters
; are next_planet_name_text, then the two lines at next_planet_lines_end.
next_planet_lines:
  DEFB $50,$7F,$46,$0A                         ; Where to go next
  DEFB $0F,$11,$0E,$02,$04,$04,$03,$26,$13,$0E ;
  DEFB $38,$8F,$44,$0F    ; The head of the next planet's name: x, line,
                          ; attribute (green), length 15

; The name of the next planet
;
; 15 characters, which planet_secured copies from planet_names (the next name's
; first 15) before printing next_planet_lines.
next_planet_name_text:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00

; The planet-secured screen's last two lines
;
; Two strings for print_strings (see planet_secured_lines), printed after
; next_planet_name_text's line, finishing the sentence.
next_planet_lines_end:
  DEFB $48,$9F,$46,$0C
  DEFB $00,$0D,$03,$26,$02,$0E,$0D,$13,$08,$0D,$14,$04
  DEFB $30,$AF,$46,$12
  DEFB $02,$11,$18,$12,$13,$00,$0B,$26,$02,$0E,$0B,$0B,$04,$02,$13,$08,$0E,$0D

; Which spawn points each tile allows
;
; One byte for each tile number: bit n set allows spawn point n (spawn_points)
; in a map cell holding that tile. start_attacker takes the tile of the cell it
; will put an attacker in from the four tile bytes at map_window_tile_bytes
; (the four cells the view overlaps, which load_map_window keeps from the map),
; and tries the allowed points starting from a random one. The table covers
; tiles $00-$A8; the map's one cell of tile $A9 reads its mask from the first
; byte of spawn_points, and $AA, which load_map_window puts in place of $3E
; during the fly-in (map_rebuild bit 7), the second.
;
; This is level data: a build-time generator should lay it out by tile number,
; with the tiles.
spawn_point_masks:
  DEFB $FF,$15,$00,$B7,$01,$15,$15,$B5,$01,$BF,$80,$00,$15,$BF,$F1,$01
  DEFB $15,$15,$B5,$3F,$11,$11,$B5,$00,$B7,$95,$BD,$35,$15,$00,$24,$BF
  DEFB $00,$B0,$00,$F5,$00,$B0,$90,$B5,$80,$F5,$00,$00,$B0,$FD,$07,$80
  DEFB $B0,$B0,$B5,$FD,$90,$90,$00,$F5,$B5,$BD,$F4,$F0,$00,$24,$00,$F5
  DEFB $3C,$18,$14,$86,$00,$C0,$28,$01,$24,$64,$0E,$15,$6E,$66,$60,$6E
  DEFB $3F,$0E,$9F,$7F,$6E,$00,$F5,$1F,$1F,$F0,$3C,$0F,$18,$78,$A8,$24
  DEFB $B9,$00,$24,$F8,$C3,$00,$6E,$48,$6E,$02,$E0,$00,$6A,$0A,$0A,$4A
  DEFB $07,$3F,$02,$0A,$40,$13,$02,$C0,$D0,$17,$48,$E0,$F8,$00,$00,$00
  DEFB $C0,$40,$40,$D0,$00,$13,$FE,$00,$03,$10,$40,$7F,$6E,$30,$25,$00
  DEFB $03,$0C,$80,$26,$68,$B0,$68,$F8,$65,$68,$B5,$0E,$00,$F8,$24,$F8
  DEFB $F8,$FD,$0A,$6E,$1E,$3C,$FF,$FD,$BF

; The eight spawn points
;
; Where in a screen start_attacker may put a new attacker: eight pairs of x
; (8-$E7 across the screen) and line ($18-$B7), copied to the record's +2 and
; +5. Which of them a screen allows is in spawn_point_masks.
spawn_points:
  DEFB $10,$3F
  DEFB $18,$AF
  DEFB $38,$6F
  DEFB $60,$A7
  DEFB $70,$37
  DEFB $88,$77
  DEFB $B0,$9F
  DEFB $B8,$47

; Run the menu until 0 is pressed
;
; Used by the routine at menu.
;
; Called by the menu (menu) after the title tune. Each pass: a new random
; number (random), the pass counter (in object_records) up by one, the two
; players' control-method pointers moved (move_method_arrow), the
; one-or-two-player lines coloured (colour_player_line, which flashes the
; chosen one), a pause of $18 x 255 loops (delay), and then key 0 ends it;
; otherwise the menu's keys are read (menu_keys) and round again.
;
; Like every keyboard read in the game, the port is selected with OUT ($FD)
; first.
menu_loop:
  CALL random             ; A random number
  LD HL,(pass_counter)    ; Count the pass
  INC HL                  ;
  LD (pass_counter),HL    ;
  LD IX,player1_pointer    ; Move player 1's pointer, then player 2's
  LD DE,pointer_p1_graphic ;
  CALL move_method_arrow   ;
  LD IX,player2_pointer    ;
  LD DE,pointer_p2_graphic ;
  CALL move_method_arrow   ;
  LD HL,$2F70             ; Colour the two lines
  CALL colour_player_line ;
  LD HL,$3F70             ;
  CALL colour_player_line ;
  LD D,$18                ; Pause
  CALL delay              ;
  LD A,$EF                ; Key 0 (row $EF, bit 0) ends the menu
  OUT ($FD),A             ;
  IN A,($FE)              ;
  AND $01                 ;
  RET Z                   ;
  CALL menu_keys          ; Otherwise read the other keys and loop
  JR menu_loop            ;

; Colour the one-player and two-player lines
;
; Used by the routine at menu.
;
; Called by the menu (menu) before and after menu_loop. Sets bit 2 of the pass
; counter, so the chosen line is drawn white, and colours both lines.
menu_show_players:
  LD A,(pass_counter)     ; Bit 2 of the pass counter set: the chosen line
  OR $04                  ; white
  LD (pass_counter),A     ;
  LD HL,$2F70             ; Both lines
  CALL colour_player_line ;
  LD HL,$3F70             ;
  JP colour_player_line   ;

; Colour a one-or-two-player line
;
; Used by the routines at menu_loop and menu_show_players.
;
; Called by menu_loop and menu_show_players. Colours one line nine characters
; wide from the screen position in HL (line $2F for one player, $3F for two, x
; $70): bright magenta ($43), or bright white ($47) if it is the one chosen
; (player_count: 0 one player, 1 two) and bit 2 of the pass counter (in
; object_records) is set. In the menu loop the counter's bit 2 changes every
; four passes, so the chosen line flashes.
;
; HL Screen position (x, bottom line)
colour_player_line:
  LD B,$43                  ; Magenta unless the flash is on...
  LD A,(pass_counter)       ;
  AND $04                   ;
  JR Z,colour_player_line_0 ;
  LD A,H                     ; ...and this is the chosen line: (line - $2F) /
  SUB $2F                    ; 16 is its number
  LD C,A                     ;
  SRL C                      ;
  SRL C                      ;
  SRL C                      ;
  SRL C                      ;
  LD A,(player_count)        ;
  CP C                       ;
  JR NZ,colour_player_line_0 ;
  LD B,$47                ; White
colour_player_line_0:
  LD A,B                   ; Put the attribute into the patch and colour it
  LD ($868F),A             ; (fill_attributes)
  LD DE,player_line_colour ;
  JP fill_attributes       ;

; The colour patch for a one-or-two-player line
;
; For fill_attributes: nine characters wide, one high, and the attribute, which
; colour_player_line writes before each use.
player_line_colour:
  DEFB $09,$01,$00

; Move a player's control-method pointer one step and draw it
;
; Used by the routines at menu_loop and menu.
;
; Each player has a pointer on the menu (P1 from the left, P2 from the right)
; that slides to the line of the control method chosen. The line wanted is the
; method's entry in method_lines; the pointer moves down one line a pass
; towards it and up four a pass, so it creeps down and snaps back up when the
; method wraps round from Interface II to Keyboard. It is drawn with
; draw_bitmap, which writes the graphic over what is there: the blank lines at
; the graphic's foot and head rub out the trail.
;
; IX The player's pointer record (player1_pointer or player2_pointer)
; DE The pointer's graphic (pointer_p1_graphic or pointer_p2_graphic)
move_method_arrow:
  LD A,(IX+$02)            ; The line wanted against the line it is at
  LD HL,method_lines       ;
  CALL add_a_to_hl         ;
  LD A,(HL)                ;
  SUB (IX+$01)             ;
  JR Z,move_method_arrow_1 ;
  BIT 7,A                   ; Below where it is: one line down
  JR NZ,move_method_arrow_0 ;
  INC (IX+$01)              ;
  JR move_method_arrow_1    ;
move_method_arrow_0:
  LD A,(IX+$01)           ; Above: four lines up
  SUB $04                 ;
  LD (IX+$01),A           ;
move_method_arrow_1:
  LD L,(IX+$00)           ; Draw it at its x and line
  LD H,(IX+$01)           ;
  JP draw_bitmap          ;

; Read the menu's keys
;
; Used by the routine at menu_loop.
;
; A (row $FD, bit 0) chooses one player, B (row $7F, bit 4) two (player_count =
; 0 or 1). Keys 1 and 2 (row $F7, bits 0 and 1) step player 1's and player 2's
; control method on through 0-3 (Keyboard, Kempston, Cursor, Interface II), but
; only once that player's pointer has reached the line of the method it has, so
; a held key steps once per arrival rather than once a pass.
;
; The entry at cycle_method steps one player's method: IX the pointer record, B
; the key's bit.
menu_keys:
  LD A,$FD                ; A: one player
  OUT ($FD),A             ;
  IN A,($FE)              ;
  AND $01                 ;
  JR NZ,menu_keys_0       ;
  LD (player_count),A     ;
menu_keys_0:
  LD A,$7F                ; B: two players
  OUT ($FD),A             ;
  IN A,($FE)              ;
  BIT 4,A                 ;
  JR NZ,menu_keys_1       ;
  LD A,$01                ;
  LD (player_count),A     ;
menu_keys_1:
  LD IX,player1_pointer   ; Player 1 and key 1
  LD B,$01                ;
  CALL cycle_method       ;
  LD IX,player2_pointer   ; Player 2 and key 2
  LD B,$02                ;

; Step a player's control method
;
; Used by the routine at menu_keys.
;
; Called and fallen into by menu_keys: IX the pointer record, B the key's bit
; in row $F7. Only once the pointer has reached the line of the method it has,
; and only while the key is pressed, the method goes on by one, 0-3.
;
; IX The player's pointer record
; B The key's bit in row $F7
cycle_method:
  LD A,(IX+$02)           ; Not while the pointer is still moving
  LD HL,method_lines      ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  SUB (IX+$01)            ;
  RET NZ                  ;
  LD A,$F7                ; The key not pressed: nothing
  OUT ($FD),A             ;
  IN A,($FE)              ;
  AND B                   ;
  RET NZ                  ;
  LD A,(IX+$02)           ; Next method, 0-3
  INC A                   ;
  AND $03                 ;
  LD (IX+$02),A           ;
  RET                     ;

; The lines of the four control methods on the menu
;
; The screen line (bottom) each player's pointer goes to for control method 0
; (Keyboard), 1 (Kempston), 2 (Cursor) and 3 (Interface II). Read by
; move_method_arrow and menu_keys.
method_lines:
  DEFB $6F,$83,$9B,$AF

; Player 1's control-method pointer
;
; Three bytes: the pointer's x, the line it is at, and player 1's control
; method (0-3), which new_game copies into the player record at a new game
; (+$1C of score and other_player_record). Moved by move_method_arrow, stepped
; by menu_keys.
player1_pointer:
  DEFB $38,$6F,$00

; Player 2's control-method pointer
;
; As player1_pointer, for player 2, on the right of the screen.
player2_pointer:
  DEFB $B8,$6F,$00

; Player 1's pointer: an arrow to the right marked P1
;
; As draw_bitmap draws it (three times the size).
;
; 3 bytes by 21 lines, bottom line first (draw_bitmap); four blank lines at the
; foot and one at the head rub out the trail as it moves up four or down one.
pointer_p1_graphic:
  DEFB $03,$15            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$01,$80        ;
  DEFB $00,$01,$C0        ;
  DEFB $00,$01,$E0        ;
  DEFB $7F,$FF,$F0        ;
  DEFB $7F,$FF,$F8        ;
  DEFB $67,$87,$FC        ;
  DEFB $67,$CF,$FE        ;
  DEFB $61,$CF,$FF        ;
  DEFB $64,$CF,$FF        ;
  DEFB $64,$8F,$FE        ;
  DEFB $61,$CF,$FC        ;
  DEFB $7F,$FF,$F8        ;
  DEFB $7F,$FF,$F0        ;
  DEFB $00,$01,$E0        ;
  DEFB $00,$01,$C0        ;
  DEFB $00,$01,$80        ;
  DEFB $00,$00,$00        ;

; Player 2's pointer: an arrow to the left marked P2
;
; As draw_bitmap draws it (three times the size).
;
; As pointer_p1_graphic.
pointer_p2_graphic:
  DEFB $03,$15            ; Width in bytes, height in lines
  DEFB $00,$00,$00        ; The lines, bottom first
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $00,$00,$00        ;
  DEFB $01,$80,$00        ;
  DEFB $03,$80,$00        ;
  DEFB $07,$80,$00        ;
  DEFB $0F,$FF,$FE        ;
  DEFB $1F,$FF,$FE        ;
  DEFB $3F,$CF,$06        ;
  DEFB $7F,$CF,$3E        ;
  DEFB $FF,$C3,$8E        ;
  DEFB $FF,$C9,$E6        ;
  DEFB $7F,$C9,$26        ;
  DEFB $3F,$C3,$8E        ;
  DEFB $1F,$FF,$FE        ;
  DEFB $0F,$FF,$FE        ;
  DEFB $07,$80,$00        ;
  DEFB $03,$80,$00        ;
  DEFB $01,$80,$00        ;
  DEFB $00,$00,$00        ;

; Count down to the next attacker
;
; spawn_tick counts it down each pass: at 7 a cloud may start (spawn_cloud), at
; 0 an attacker (spawn_attacker), and it starts again from 8.
spawn_countdown:
  DEFB $10

; How many records attackers may use
;
; Always 4: spawn_attacker looks for a free record among the first four object
; records (object_records). Nothing writes it.
attacker_records:
  DEFB $04

; Start a cloud, when the view is in the sky
;
; Used by the routines at spawn_tick and update_objects.
;
; Called by spawn_tick (when its count reaches 7) and by update_objects (when
; the load is light). Only while the view's screen row (view_row) is 3-9: looks
; for a free record among the two clouds' records, 7 and 8 of object_records
; (find_free_record), and puts a cloud (attacker kind 1, type $13) in it at a
; screen edge (start_at_edge, all four screens allowed).
spawn_cloud:
  LD A,(view_row)         ; Only in screen rows 3-9
  CP $0A                  ;
  RET NC                  ;
  CP $03                  ;
  RET C                   ;
  LD A,$02                ; A free record among records 7 and 8
  LD IX,cloud_records     ;
  CALL find_free_record   ;
  RET NZ                  ;
  LD A,$01                ; Kind 1, any of the four screens
  LD (attacker_kind),A    ;
  LD B,$03                ;
  JP start_at_edge        ;

; Count towards the next attacker, and run the meteor-shower timer
;
; Used by the routine at play.
;
; Called by the play loop (play) once a pass. Nothing while the load
; (object_load) is 8 or more.
;
; The meteor-shower timer (meteor_shower): when it is 0, a shower starts on the
; passes when the random byte random_a is $6A and the low two bits of its
; second byte are 0 -- one pass in 1024 -- and lasts 128 passes, during which
; start_attacker puts rocks and comets near the ship instead of attackers.
;
; Every eighth pass (spawn_countdown at 7) a cloud may start (spawn_cloud); at
; 0 the count starts again from 8 and the entry at spawn_attacker looks for a
; free record among the attackers' four (attacker_records) and fills it
; (start_attacker). update_objects also jumps to spawn_attacker when the load
; is light.
spawn_tick:
  LD A,(object_load)      ; Not when the load is 8 or more
  CP $08                  ;
  RET NC                  ;
  LD A,(meteor_shower)    ; A shower running: count it down below
  AND A                   ;
  JR NZ,spawn_tick_0      ;
  LD A,(random_a)         ; Start one at random: 128 passes
  CP $6A                  ;
  JR NZ,spawn_tick_1      ;
  LD A,(random_b)         ;
  AND $03                 ;
  JR NZ,spawn_tick_1      ;
  OR $80                  ;
  LD (meteor_shower),A    ;
  JR spawn_tick_1         ;
spawn_tick_0:
  DEC A                   ; Count the shower down
  LD (meteor_shower),A    ;
spawn_tick_1:
  LD A,(spawn_countdown)  ; Count the passes; at 7, a cloud
  DEC A                   ;
  LD (spawn_countdown),A  ;
  CP $07                  ;
  JP Z,spawn_cloud        ;
  AND A                   ; At 0, from 8 again, and an attacker
  RET NZ                  ;
  LD A,$08                ;
  LD (spawn_countdown),A  ;

; Start an attacker if one of their records is free
;
; Used by the routine at update_objects.
;
; Fallen into from spawn_tick every eighth pass, and jumped to by
; update_objects when the load is light. Looks for a free record among the
; first four (attacker_records) and fills it (start_attacker).
spawn_attacker:
  LD A,(attacker_records) ; A free record among the first four: fill it
  LD IX,object_records    ;
  CALL find_free_record   ;
  JP Z,start_attacker     ;
  RET                     ;

; The meteor-shower timer
;
; 0 none; otherwise the passes left (from 128) of a meteor shower (spawn_tick),
; during which start_attacker aims rocks and comets near the ship. Cleared by
; reset_for_life.
meteor_shower:
  DEFB $00

; Find a free object record
;
; Used by the routines at spawn_cloud, spawn_attacker and fire_from.
;
; Looks along B records of 24 bytes from IX for one whose type (+0) is $FF.
; Gives up at once while the load (object_load) is 7 or more or the ship is
; dying (death_count not 0).
;
;   A How many records to look at
;   IX The first
; O:F Z set if one was found
; O:IX The free record
; O:DE $0018 when one was found (start_attacker relies on D being 0)
find_free_record:
  LD B,A                   ; None when the load is 7 or more, or while dying
  LD A,(object_load)       ;
  CP $07                   ;
  JR NC,find_free_record_1 ;
  LD A,(death_count)       ;
  AND A                    ;
  JR NZ,find_free_record_1 ;
  LD DE,$0018             ; Find one with type $FF
find_free_record_0:
  LD A,(IX+$00)           ;
  INC A                   ;
  RET Z                   ;
  ADD IX,DE               ;
  DJNZ find_free_record_0 ;
find_free_record_1:
  OR $01                  ; None: NZ
  RET                     ;

; Let an attacker fire, now and then
;
; Used by the routines at update_cloud and update_tough_attacker.
;
; Called by the update routines update_cloud and update_tough_attacker with the
; kind of shot in scratch_count (7 or 8: a horizontal shot right or left, see
; shot_kind_flags). Only when the attacker was drawn last pass
; (old_sprite_rows), and then one pass in 32 at random (random_a AND $2F zero):
; see fire_from.
;
; IX The attacker's record
attacker_fires:
  LD A,(old_sprite_rows)  ; Drawn last pass, and a random chance
  AND A                   ;
  RET Z                   ;
  LD A,(random_a)         ;
  AND $2F                 ;
  RET NZ                  ;
  JR fire_from            ;

; Let a ground object fire or throw something
;
; Used by the routine at update_ground_objects.
;
; Called by update_ground_objects for each ground object whose +$18 bits 1-3
; give a kind of shot (1-7, in scratch_count). One pass in 16 at random
; (random_a AND $5A zero), and only when the object is on the screen (+$10 bit
; 7).
;
; fire_from, also used by attackers (attacker_fires), fires the shot. It needs
; a free record among the three for shots, 4-6 of object_records, and the
; shooter between x $18 and $BF on the screen; the kind's flags
; (shot_kind_flags) may also want it in the top or bottom part of the screen.
; The new record is a type $1C object at the shooter's place, given the kind's
; template (shot_kind_templates), its colour, and then moved once by the
; template's numbers (move_object), which puts the shot at the gun's mouth.
; Then its real velocity is set by the frame it has:
;
; Frame 0 (kind 1, a bullet thrown up): a random spray upwards (the spray code
; in fire_from) that gravity brings down (Y acceleration $0020); one throw in
; eight is the shield ball (frame 9) instead, and every (lives x 4)th throw is
; the extra-life ship icon (frame 8, counted by extra_life_countdown) -- so the
; fewer lives the player has, the more often extra lives come up. Frames 5 and
; 6 (kinds 4 and 5): a random spray upwards. Frame 7 (kind 6): the same,
; downwards when the shooter is in the upper part of the screen
; (fire_spray_down). Frame 10 (kinds 7 and 8, the attackers' shots): straight
; across at 6 pixels a pass, right for kind 7 and left for 8. Any other frame
; (kinds 2 and 3, the refuel pick-up thrown up and a pellet dropped): half the
; shooter's X velocity and 5 pixels a pass up, or down when the kind's flags
; have bit 7. A shot is sounded (sound_fire) unless the spray came to nothing.
;
; IX The ground object's record (26 bytes, ground_objects)
ground_object_fires:
  LD A,(random_a)         ; One pass in 16
  AND $5A                 ;
  RET NZ                  ;
  BIT 7,(IX+$10)          ; Only while on the screen
  RET Z                   ;

; Fire a shot from an object
;
; Used by the routine at attacker_fires.
;
; Fallen into from ground_object_fires (ground objects) and jumped to from
; attacker_fires (attackers), with the kind of shot in scratch_count. See
; ground_object_fires for what each kind does.
;
; IX The shooter's record
fire_from:
  PUSH IX                 ; A free record among 4-6, as IY
  LD A,$03                ;
  LD IX,drop_records      ;
  CALL find_free_record   ;
  PUSH IX                 ;
  POP IY                  ;
  POP IX                  ;
  RET NZ                  ;
  LD A,(IX+$11)           ; The shooter between x $18 and $BF
  CP $C0                  ;
  RET NC                  ;
  CP $18                  ;
  RET C                   ;
  LD A,(scratch_count)    ; The kind's flags (shot_kind_flags) to ground_cell
  LD HL,$89A6             ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (ground_cell),A      ;
  LD A,(IX+$12)           ; Bit 7: only from above line $58
  BIT 7,(HL)              ;
  JR Z,fire_from_0        ;
  CP $58                  ;
  RET NC                  ;
fire_from_0:
  BIT 6,(HL)              ; Bit 6: only from line $50 down
  JR Z,fire_from_1        ;
  CP $50                  ;
  RET C                   ;
fire_from_1:
  CALL swap_to_shot       ; The new record at the shooter's place, cleared
  CALL free_object        ;
  LD (IX+$00),$1C         ; Type $1C; +$10 = $20 (shot, top speed 1); the
  LD (IX+$10),$20         ; template's four bytes to +7, +9, +$13, +$14
  LD HL,$89AB             ;
  LD A,(scratch_count)    ;
  ADD A,A                 ;
  ADD A,A                 ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (IX+$07),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$09),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$13),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$14),A           ;
  LD A,(ground_cell)      ; Its colour: the flags' low nibble in both halves of
  AND $0F                 ; +$0D
  LD B,A                  ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  OR B                    ;
  LD (IX+$0D),A           ;
  LD (IX+$15),$38         ; +$15 = $38; move it by the template's numbers
  CALL move_object        ; (move_object), which places it
  PUSH IY                 ; IX the shooter again, IY the shot
  PUSH IX                 ;
  POP IY                  ;
  POP IX                  ;
  LD A,(IY+$13)           ; By the shot's frame
  AND $0F                 ;
  JP Z,fire_from_6        ;
  CP $05                  ;
  JP Z,fire_from_4        ;
  CP $06                  ;
  JP Z,fire_from_4        ;
  CP $07                  ;
  JP Z,fire_spray_down    ;
  CP $0A                  ;
  JP Z,fire_from_3        ;
  LD H,(IX+$07)           ; Others: X velocity half the shooter's...
  LD L,(IX+$08)           ;
  SRA H                   ;
  RR L                    ;
  LD (IY+$07),H           ;
  LD (IY+$08),L           ;
  LD A,(ground_cell)      ; ...and Y velocity 5 up, or down for flags bit 7
  LD (IY+$09),$05         ;
  BIT 7,A                 ;
  JR NZ,fire_from_2       ;
  LD (IY+$09),$FB         ;
; This entry point is used by the routine at fire_spray_down.
fire_from_2:
  LD A,(IY+$00)           ; Sound it, unless it was freed
  INC A                   ;
  CALL NZ,sound_fire      ;
  RET                     ;
fire_from_3:
  LD (IY+$07),$06         ; Frame 10: 6 pixels a pass across, right for kind 7,
  LD (IY+$08),$00         ; left otherwise
  LD (IY+$09),$00         ;
  LD (IY+$0A),$00         ;
  LD A,(scratch_count)    ;
  CP $07                  ;
  JR Z,fire_from_2        ;
  LD (IY+$07),$FA         ;
  JR fire_from_2          ;
; This entry point is used by the routine at fire_spray_down.
fire_from_4:
  LD A,(random_b)         ; Frames 5 and 6, and the others' start: Y velocity
  LD B,A                  ; 0, -2, -4 or -6 at random...
  AND $06                 ;
  NEG                     ;
  LD C,A                  ;
  LD (IY+$09),A           ;
  LD A,B                  ; ...X velocity -6 to +6 from the random byte's top
  RLCA                    ; three bits; 7 there, or no velocity at all, frees
  RLCA                    ; it
  RLCA                    ;
  AND $07                 ;
  CP $07                  ;
  JP Z,fire_from_5        ;
  SUB $03                 ;
  ADD A,A                 ;
  LD B,A                  ;
  LD (IY+$07),A           ;
  OR C                    ;
  JR NZ,fire_from_2       ;
fire_from_5:
  LD (IY+$00),$FF         ; Free it
  JR fire_from_2          ;
fire_from_6:
  CALL fire_from_4        ; Frame 0: the spray; none frees it
  LD A,(IY+$09)           ;
  AND A                   ;
  JR Z,fire_from_5        ;
  LD A,R                  ; One in eight: frame 9, the shield ball
  AND $07                 ;
  JR NZ,fire_from_7       ;
  LD (IY+$13),$09         ;
fire_from_7:
  LD (IY+$10),$25         ; Top speed 6, still a shot
  LD A,(IY+$00)           ;
  INC A                   ;
  JP Z,fire_from_2        ;
  LD A,(extra_life_countdown) ; Every (lives x 4)th: frame 8, the extra life
  DEC A                       ;
  LD (extra_life_countdown),A ;
  JR NZ,fire_from_8           ;
  LD A,(lives)                ;
  ADD A,A                     ;
  ADD A,A                     ;
  LD (extra_life_countdown),A ;
  LD (IY+$13),$08             ;
fire_from_8:
  LD (IY+$0F),$20         ; Gravity: Y acceleration $0020
  JP fire_from_2          ;

; Throws left before the next extra life
;
; Counted down by ground_object_fires at each bullet a ground object throws
; (kind 1); at 0 that throw is the extra-life icon and the count starts again
; from the current lives x 4.
extra_life_countdown:
  DEFB $08

; The shield timer
;
; 0 off; set to $FF when the ship touches the shield ball (ship_contact),
; counted down every other pass by ship_pass. While it is not zero the ship
; takes no damage (contact_damage); the ship flashes in its last $20
; (colour_ship). Cleared at a new game.
shield_timer:
  DEFB $00

; Fire a spray of shots downwards from high up
;
; Used by the routine at fire_from.
;
; Part of fire_from, for frame 7 (kind 6), jumped to from its test of the
; frame. sna2ctl took these bytes for data, since no session ran them: they
; call fire_from's random spray, then, if the shooter is above line $67 on the
; screen, its Y velocity turned round so the spray goes down. Then back to the
; sound near the end of fire_from.
;
; IX The shooter
; IY The shot
fire_spray_down:
  CALL fire_from_4        ; The random spray
  LD A,(IX+$12)           ; Shooter at line $67 or below: as it is
  CP $67                  ;
  JP NC,fire_from_2       ;
  LD A,(IY+$09)           ; Higher: downwards
  NEG                     ;
  LD (IY+$09),A           ;
  JP fire_from_2          ; Sound it

; Swap IX and IY, and copy the position across
;
; Used by the routine at fire_from.
;
; Used by ground_object_fires to put the new shot at the shooter. copy_position
; then copies the screen column and pixel (+1, +2) and the screen row and line
; (+4, +5) of the record at IY into the record at IX, leaving the fractions;
; start_attacker and place_at_ship use it to start something at the ship.
;
; IX The shooter
; IY The new record
swap_to_shot:
  PUSH IX                 ; Swap them: IX the new record
  PUSH IY                 ;
  POP IX                  ;
  POP IY                  ;

; Copy a record's place to another
;
; Used by the routines at start_at_edge and place_at_ship.
;
; Fallen into from swap_to_shot, and called by start_at_edge and place_at_ship:
; copies the screen column and pixel (+1, +2) and the screen row and line (+4,
; +5) of the record at IY into the record at IX, leaving the fractions.
;
; IX The record to place
; IY The record whose place it takes
copy_position:
  LD A,(IY+$01)           ; X and Y, without the fractions
  LD (IX+$01),A           ;
  LD A,(IY+$02)           ;
  LD (IX+$02),A           ;
  LD A,(IY+$04)           ;
  LD (IX+$04),A           ;
  LD A,(IY+$05)           ;
  LD (IX+$05),A           ;
  RET                     ;

; Flags for the eight kinds of shot
;
; One byte for each of kinds 1-8 (fire_from reads the byte before this table
; plus the kind): bit 7, only from a shooter above line $58 (and, for the
; frames with no velocity of their own, fire downwards); bit 6, only from line
; $50 down; low nibble the colour, in both halves of +$0D. Kinds 1 and 2 bright
; yellow, from low down; 3 bright cyan, from high up; 4 and 5 bright white,
; from low down; 6 bright cyan; 7 and 8 bright white.
shot_kind_flags:
  DEFB $4E
  DEFB $4E
  DEFB $8D
  DEFB $4F
  DEFB $4F
  DEFB $0D
  DEFB $0F
  DEFB $0F

; Templates for the eight kinds of shot
;
; Four bytes for each of kinds 1-8 (fire_from reads four bytes on per kind from
; the fifth byte of shot_kind_flags): the gun's mouth as an X and Y offset (put
; in +7 and +9 for one move by move_object, then replaced), the frame byte
; (+$13: low nibble the frame of type $1C, high nibble its timing) and the
; frame range (+$14).
;
; Kind 1 frames 0-2 (the bullet), 2 frame 3 (the refuel pick-up), 3 frame 4, 4
; frame 5, 5 frame 6 (the fuel thief), 6 frame 7, 7 and 8 frame 10 (the dash at
; dash_graphic), kind 7 from 32 pixels to the right of the attacker.
shot_kind_templates:
  DEFB $0C,$F5,$50,$02
  DEFB $08,$E2,$F3,$33
  DEFB $10,$00,$F4,$44
  DEFB $07,$F8,$F5,$55
  DEFB $07,$F8,$F6,$66
  DEFB $07,$FC,$F7,$77
  DEFB $20,$FB,$FA,$AA
  DEFB $00,$FB,$FA,$AA

; The attackers' shot: a dash
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; Type $1C's frame 10: 1 byte by 1 line, eight pixels across. Fired across the
; screen by kinds 7 and 8 (ground_object_fires).
dash_graphic:
  DEFB $01,$01            ; Width in bytes, height in lines
  DEFB $FF                ; The line

; The kind of attacker being started
;
; 0-9, an entry of the wave table wave_table: set by start_attacker (from the
; wave counter there) and spawn_cloud (1, a cloud), and 8 or 9 for the meteor
; shower's rocks and comets.
attacker_kind:
  DEFB $00

; Start an attacker in a free record
;
; Used by the routine at spawn_attacker.
;
; Called by spawn_tick with IX a free record among the first four. The kinds of
; attacker are taken in turn (the wave counter wave_counter counts round 0-5,
; or 0-7 once +$0A and +$0B of the player's record, score, are both set); kind
; 4, the tough attacker, is usually turned into kind 3 (unless the refresh
; register's bits $32 are all 0), and kinds 1 and 5 (clouds) become kind 0
; outside screen rows 3-9. While a meteor shower is on (meteor_shower) and the
; view is above screen row $0B, a rock or comet starts near the ship instead
; (at the end of start_at_edge).
;
; start_at_edge chooses where. The view overlaps four screens: its own
; (view_column, view_row), the one to its right (right_map_column), the one
; below and the one right and below; bit 1 of scratch_count picks right, bit 0
; below. B says which bits may be chosen at random, C which are forced: when
; the ship moves fast across (the high byte of its speed across, +$19 of its
; record in bare_ship, not 0 or $FF), the screen to the right if the scroll
; step across (the second byte of scroll_down) is negative and the view's own
; column if not; likewise in Y with its speed down (+$1B) and the first byte of
; scroll_down (the screen below when that is negative). Up to four screens are
; tried in turn (speed_limit_down).
;
; Then one screen is tried: the screen's tile (its byte in
; map_window_tile_bytes) gives a mask of allowed spawn points
; (spawn_point_masks); a random start point (the instruction shown as RLC B in
; start_at_edge is rewritten to BIT n,A) is followed round the mask to the
; first allowed one (spawn_points), which sets the record's +2 and +5. The
; record is cleared (free_object), given the kind's four bytes from the wave
; table wave_table (type, +$10, +$13, +$14) and a colour ($FF, or $77 now and
; then; for most types one of the eight colours in attacker_colours, the first
; four only in screen rows 4-11), and put in the chosen screen's column and
; row. If it would be in view now (place_on_screen sets +$10 bit 7) it is freed
; and the next screen tried; otherwise it is turned towards the view
; (head_for_view).
;
; place_on_screen is given DE pointing at the screen's kind's byte in the ROM's
; first bytes ($0000-$0003, D being 0), not at a graphic, so the size it tests
; with is not the attacker's: a large one. Whether that is meant is not known;
; it keeps attackers from starting near the view.
;
; A cloud (type $13) takes frame 0 or, one in eight, the big cloud; in screen
; rows 0-4 it is put at line $1F of row 4 instead, level with the view's x, and
; not tested. Kinds 8 and 9 (type $1D) move 2 or 4 pixels a pass each way,
; right in the view's own column and left in the next.
;
; IX The free record
start_attacker:
  LD B,$06                ; Six kinds, or eight
  LD A,(addon_four)       ;
  AND A                   ;
  JR Z,start_attacker_0   ;
  LD A,(addon_five)       ;
  AND A                   ;
  JR Z,start_attacker_0   ;
  LD B,$08                ;
start_attacker_0:
  LD A,(wave_counter)     ; The next kind in turn
  INC A                   ;
  LD (wave_counter),A     ;
  CP B                    ;
  JR C,start_attacker_1   ;
  XOR A                   ;
  LD (wave_counter),A     ;
start_attacker_1:
  CP $04                  ; Kind 4 mostly becomes 3
  JR NZ,start_attacker_2  ;
  LD B,A                  ;
  LD A,R                  ;
  AND $32                 ;
  LD A,B                  ;
  JR Z,start_attacker_2   ;
  DEC A                   ;
start_attacker_2:
  LD (attacker_kind),A    ; Clouds only in screen rows 3-9; elsewhere kind 0
  CP $01                  ;
  JR Z,start_attacker_3   ;
  CP $05                  ;
  JR NZ,start_attacker_5  ;
start_attacker_3:
  LD A,(view_row)         ;
  CP $03                  ;
  JR C,start_attacker_4   ;
  CP $0A                  ;
  JR C,start_attacker_5   ;
start_attacker_4:
  XOR A                   ;
  LD (attacker_kind),A    ;
start_attacker_5:
  LD A,(view_row)         ; A meteor shower above row $0B
  CP $0B                  ;
  JR NC,start_attacker_6  ;
  LD A,(meteor_shower)    ;
  AND A                   ;
  JP NZ,start_at_edge_17  ;
start_attacker_6:
  LD B,$03                ; Any screen

; Start an attacker at a screen near the view
;
; Used by the routine at spawn_cloud.
;
; Fallen into from start_attacker, and jumped to by spawn_cloud for a cloud.
; Chooses one of the four screens the view overlaps, then a spawn point in it,
; fills the record, and gives up on a screen where it would be seen: see
; start_attacker.
start_at_edge:
  LD C,$00                 ; Moving fast across: the screen ahead in X
  LD A,(ship_across_speed) ;
  AND A                    ;
  JR Z,start_at_edge_1     ;
  INC A                    ;
  JR Z,start_at_edge_1     ;
  LD A,(scroll_across)     ;
  BIT 7,A                  ;
  JR NZ,start_at_edge_0    ;
  LD A,B                   ;
  AND $01                  ;
  LD B,A                   ;
  JR start_at_edge_1       ;
start_at_edge_0:
  LD C,$02                 ;
start_at_edge_1:
  LD A,(ship_down_speed)  ; Moving fast up or down: the screen ahead in Y
  AND A                   ;
  JR Z,start_at_edge_2    ;
  INC A                   ;
  JR Z,start_at_edge_2    ;
  INC C                   ;
  LD A,(scroll_down)      ;
  BIT 7,A                 ;
  JR NZ,start_at_edge_2   ;
  DEC C                   ;
  LD A,B                  ;
  AND $02                 ;
  LD B,A                  ;
start_at_edge_2:
  LD A,$04                ; Four tries; the screen at random within B, forced
  LD (speed_limit_down),A ; by C
  LD A,(random_a)         ;
  AND B                   ;
  OR C                    ;
start_in_screen:
  LD (scratch_count),A        ; The screen's tile gives the allowed points
  LD HL,map_window_tile_bytes ; (spawn_point_masks)
  CALL add_a_to_hl            ;
  LD E,(HL)                   ;
  LD HL,spawn_point_masks     ;
  ADD HL,DE                   ;
  LD A,(HL)                   ;
  LD (ground_cell),A          ;
  LD A,R                  ; A random point to start from; the rewritten BIT
  AND $07                 ; below tests it
  LD (row_count),A        ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,$47               ;
  LD ($8A8B),A            ;
  XOR A                   ;
  LD (row_count_spare),A  ;
start_at_edge_3:
  LD A,(ground_cell)      ; Round the eight points to an allowed one; none:
  RLC B                   ; next screen
  JR NZ,start_at_edge_4   ;
  LD A,(ground_cell)      ;
  RRCA                    ;
  LD (ground_cell),A      ;
  LD A,(row_count_spare)  ;
  INC A                   ;
  LD (row_count_spare),A  ;
  CP $08                  ;
  JR NZ,start_at_edge_3   ;
  JP start_at_edge_14     ;
start_at_edge_4:
  LD A,(row_count)        ; The point's x and line (spawn_points)
  LD B,A                  ;
  LD A,(row_count_spare)  ;
  ADD A,B                 ;
  AND $07                 ;
  ADD A,A                 ;
  LD HL,spawn_points      ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (IX+$02),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$05),A           ;
  LD (IX+$0D),$FF         ; Colour $FF, or $77 one time in four
  LD A,(random_b)         ;
  AND $48                 ;
  JR NZ,start_at_edge_5   ;
  LD (IX+$0D),$77         ;
start_at_edge_5:
  LD A,(attacker_kind)    ; Clear the record and fill it from the wave table
start_at_edge_6:
  ADD A,A                 ; (wave_table)
  ADD A,A                 ;
  LD HL,wave_table        ;
  CALL add_a_to_hl        ;
  CALL free_object        ;
  LD (IX+$15),$01         ;
  LD A,(HL)               ;
  LD (IX+$00),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$10),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$13),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$14),A           ;
  LD A,(IX+$00)           ;
  CP $1D                  ;
  JP Z,start_at_edge_19   ;
  LD HL,(view_column)     ; In the chosen screen's column and row
  LD A,(scratch_count)    ;
  AND A                   ;
  JR Z,start_at_edge_8    ;
  BIT 0,A                 ;
  JR Z,start_at_edge_7    ;
  INC H                   ;
start_at_edge_7:
  BIT 1,A                 ;
  JR Z,start_at_edge_8    ;
  LD A,(right_map_column) ;
  LD L,A                  ;
start_at_edge_8:
  LD (IX+$01),L           ;
  LD (IX+$04),H           ;
  LD A,(IX+$00)           ; A cloud: frame 0, or 1 now and then; screen rows
  CP $13                  ; 0-4 below
  JR NZ,start_at_edge_11  ;
  LD (IX+$13),$00         ;
  LD A,R                  ;
  AND $1C                 ;
  JR NZ,start_at_edge_9   ;
  INC (IX+$13)            ;
start_at_edge_9:
  LD A,H                  ;
  CP $05                  ;
  JR NC,start_at_edge_13  ;
  LD (IX+$10),$23         ; Row 4, line $1F, level with the view
  LD (IX+$04),$04         ;
  LD (IX+$05),$1F         ;
  LD (IX+$0D),$FF         ;
  LD A,(right_map_column) ;
  CP L                    ;
  JR Z,start_at_edge_10   ;
  LD A,(view_x)           ;
  SUB $30                 ;
  JR C,start_at_edge_16   ;
  ADD A,$08               ;
  LD (IX+$02),A           ;
  RET                     ;
start_at_edge_10:
  LD A,(view_x)           ;
  LD (IX+$02),A           ;
  RET                     ;
start_at_edge_11:
  LD A,R                  ; Others: a colour from attacker_colours
  AND $07                 ;
  LD B,A                  ;
  LD A,H                  ;
  CP $0C                  ;
  JR NC,start_at_edge_12  ;
  CP $04                  ;
  JR C,start_at_edge_12   ;
  LD A,B                  ;
  AND $03                 ;
  LD B,A                  ;
start_at_edge_12:
  LD A,B                  ;
  LD HL,attacker_colours  ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (IX+$0D),A           ;
start_at_edge_13:
  CALL place_on_screen    ; In view: free it...
  BIT 7,(IX+$10)          ;
  JR Z,start_at_edge_15   ;
  LD (IX+$00),$FF         ;
start_at_edge_14:
  LD A,(speed_limit_down) ; ...and try the next screen
  DEC A                   ;
  LD (speed_limit_down),A ;
  RET Z                   ;
  LD A,(scratch_count)    ;
  INC A                   ;
  AND $03                 ;
  JP start_in_screen      ;
start_at_edge_15:
  JP head_for_view        ; Turn it towards the view
start_at_edge_16:
  LD (IX+$00),$FF         ; Free it
  RET                     ;
; This entry point is used by the routine at start_attacker.
start_at_edge_17:
  LD IY,ship_record       ; Meteor shower: at the ship, moved once by an X
  CALL copy_position      ; offset of -$30 to +$48 and a Y offset of $10 less
  LD A,(random_a)         ; the ship's +$1E (its line on the screen)
  LD B,A                  ;
  AND $30                 ;
  ADD A,$18               ;
  BIT 7,B                 ;
  JR Z,start_at_edge_18   ;
  SUB $18                 ;
  NEG                     ;
start_at_edge_18:
  LD (IX+$07),A           ;
  LD A,(IY+$1E)           ;
  SUB $10                 ;
  NEG                     ;
  LD (IX+$09),A           ;
  CALL move_object        ;
  LD A,(view_column)      ; A comet (kind 9) in the view's own column, a rock
  CP (IX+$01)             ; (8) otherwise
  LD A,$09                ;
  JP Z,start_at_edge_6    ;
  DEC A                   ;
  JP start_at_edge_6      ;
start_at_edge_19:
  LD A,(random_a)         ; Type $1D: 2 or 4 pixels a pass down, 2 or 4
  AND $08                 ; right...
  SRL A                   ;
  SRL A                   ;
  ADD A,$02               ;
  LD (IX+$09),A           ;
  LD A,(random_b)         ;
  AND $08                 ;
  SRL A                   ;
  SRL A                   ;
  ADD A,$02               ;
  LD (IX+$07),A           ;
  NEG                     ; ...or left, outside the view's own column
  LD B,A                  ;
  LD A,(view_column)      ;
  CP (IX+$01)             ;
  LD (IX+$0D),$FE         ;
  RET Z                   ;
  LD (IX+$07),B           ;
  LD (IX+$0D),$EE         ;
  RET                     ;

; The speed limit while accelerating
;
; Set by accelerate from the record's top speed, read by limit_speed.
speed_limit:
  DEFB $00

; The negative speed limit; the spawn's tries
;
; Set by accelerate to minus the top speed, minus one, and read by limit_speed.
; start_attacker also borrows it to count the screens it tries.
speed_limit_down:
  DEFB $00

; Add an object's acceleration to its velocity, within its top speed
;
; Used by the routines at move_missile, update_cloud, update_small_object,
; update_tough_attacker, update_attacker and update_rocket_part.
;
; Called by the update routines move_missile, update_cloud,
; update_small_object, update_tough_attacker and update_attacker. Velocity (+7
; +8 in X, +9 +A in Y) and acceleration (+B +C, +E +F) are signed 8.8 numbers,
; high byte first. The top speed is (+$10 AND 7) + 1 whole pixels a pass; a
; velocity that goes past it is set to exactly that (limit_speed).
;
; IX The object
accelerate:
  LD A,(IX+$10)           ; The limits, n and -(n + 1)
  AND $07                 ;
  INC A                   ;
  LD (speed_limit),A      ;
  CPL                     ;
  LD (speed_limit_down),A ;
  LD H,(IX+$07)           ; X velocity plus X acceleration, limited
  LD L,(IX+$08)           ;
  LD D,(IX+$0B)           ;
  LD E,(IX+$0C)           ;
  ADD HL,DE               ;
  CALL limit_speed        ;
  LD (IX+$07),H           ;
  LD (IX+$08),L           ;
  LD H,(IX+$09)           ; Y velocity plus Y acceleration, limited
  LD L,(IX+$0A)           ;
  LD D,(IX+$0E)           ;
  LD E,(IX+$0F)           ;
  ADD HL,DE               ;
  CALL limit_speed        ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  RET                     ;

; Keep a velocity within the speed limit
;
; Used by the routine at accelerate.
;
; A positive velocity of more than n.0 becomes n.0; a negative one below -n
; becomes -n.0 (n from speed_limit).
;
;   HL The velocity
; O:HL The velocity, limited
limit_speed:
  BIT 7,H                 ; Positive: no more than n
  JR NZ,limit_speed_0     ;
  LD A,(speed_limit)      ;
  DEC A                   ;
  CP H                    ;
  RET NC                  ;
  INC A                   ;
  LD H,A                  ;
  LD L,$00                ;
  RET                     ;
limit_speed_0:
  LD A,(speed_limit_down) ; Negative: no less than -n
  CP H                    ;
  RET C                   ;
  INC A                   ;
  LD H,A                  ;
  LD L,$00                ;
  RET                     ;

; Turn an object that is out of view back towards it
;
; Used by the routine at place_object.
;
; Jumped to by place_object. Not for types $1D and $1E, nor while the object is
; in view (edge_flags, its +$10 as last drawn, bit 7). Otherwise it runs on
; into head_for_view, which turns it.
;
; IX The object
steer_to_view:
  LD A,(IX+$00)           ; Not types $1D, $1E
  CP $1D                  ;
  RET Z                   ;
  CP $1E                  ;
  RET Z                   ;
  LD A,(edge_flags)       ; Not while in view
  AND $80                 ;
  RET NZ                  ;

; Turn an object towards the view
;
; Used by the routine at start_at_edge.
;
; Fallen into from steer_to_view, and jumped to by start_at_edge for a new
; attacker. Stops the object and sets its acceleration: $20 (an eighth of a
; pixel a pass, each pass) to the right if it is in the view's own screen
; column, otherwise to the left; down if it is in the view's screen row above
; the view's top line; up if it is in another screen row below that line;
; otherwise none in Y. (The view is one screen in size, so it covers the part
; of its own screen from that line down and the part of the screen below above
; it.)
head_for_view:
  LD (IX+$0B),$00         ; Accelerate right in the view's column, left in the
  LD (IX+$0C),$20         ; next
  LD A,(view_column)      ;
  CP (IX+$01)             ;
  JR Z,head_for_view_0    ;
  LD (IX+$0C),$E0         ;
  LD (IX+$0B),$FF         ;
head_for_view_0:
  XOR A                   ; Stop it
  LD (IX+$09),A           ;
  LD (IX+$0A),A           ;
  LD (IX+$0E),A           ;
  LD (IX+$0F),A           ;
  LD A,(view_row)         ; Its screen row against the view's
  CP (IX+$04)             ;
  LD A,(view_y)           ;
  JR NZ,head_for_view_1   ;
  CP (IX+$05)             ; The view's row, above the view: down
  RET C                   ;
  LD (IX+$0E),$00         ;
  LD (IX+$0F),$20         ;
  RET                     ;
head_for_view_1:
  CP (IX+$05)             ; The row below, beneath the view: up
  RET NC                  ;
  LD (IX+$0E),$FF         ;
  LD (IX+$0F),$E0         ;
  RET                     ;

; Last pass's cloud-contact flag
;
; update_objects copies fuel_saver here each pass before clearing it;
; fuel_saver is set when a cloud touches what the ship carries (ship_contact).
; While it is set contact_slow_ship does nothing.
last_pass_contact:
  DEFB $00

; Break the ship up
;
; Used by the routine at update_objects.
;
; Called by update_objects when the death byte (death_count) reaches 10. Fills
; all nine object records (object_records): the first four as debris (type $15,
; made by the second entry point of check_shot) 28 pixels to the right of the
; ship and above it by half the ship sprite's height (ship_height), +$15 0,
; $40, $80 and $C0 so that each piece takes a different part of the debris
; path; the other five as type $1C bullets (frame 0) at the same place (+$14 =
; 2, frames 0-2, which also marks them as pieces of the wreck), thrown out with
; the velocities at wreck_velocities and pulled down by gravity (Y acceleration
; $0020).
wreck_ship:
  LD IY,ship_record       ; Four records from the first; the offset above the
  LD BC,$0400             ; ship
  LD IX,object_records    ;
  LD A,(ship_height)      ;
  SRL A                   ;
  NEG                     ;
  LD (ground_cell),A      ;
wreck_ship_0:
  PUSH BC                 ; At the ship (place_at_ship); debris (type $15)
  CALL place_at_ship      ;
  CALL make_debris        ;
  LD (IX+$0D),$FF         ; Colour white, path phase C, next
  POP BC                  ;
  LD A,C                  ;
  LD (IX+$15),A           ;
  ADD A,$40               ;
  LD C,A                  ;
  LD DE,$0018             ;
  ADD IX,DE               ;
  DJNZ wreck_ship_0       ;
  LD BC,$0500             ; Five more, at the ship
wreck_ship_1:
  PUSH BC                 ;
  CALL place_at_ship      ;
  LD (IX+$00),$1C         ; Type $1C, frame 0, top speed 7, wreck piece, colour
  LD (IX+$10),$06         ; yellow
  LD (IX+$13),$50         ;
  LD (IX+$14),$02         ;
  LD (IX+$15),$40         ;
  LD (IX+$0D),$EE         ;
  POP BC                  ; Velocity from wreck_velocities
  LD A,C                  ;
  LD HL,wreck_velocities  ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (IX+$07),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$09),A           ;
  INC C                   ; Gravity; next
  INC C                   ;
  LD (IX+$0F),$20         ;
  LD DE,$0018             ;
  ADD IX,DE               ;
  DJNZ wreck_ship_1       ;
  RET                     ; Done

; Velocities of the five pieces of the ship's wreck
;
; X and Y (high bytes, pixels a pass) for wreck_ship: two to the left, one
; straight up, two to the right, all upwards.
wreck_velocities:
  DEFB $FA,$FD
  DEFB $FD,$FA
  DEFB $00,$FC
  DEFB $03,$FA
  DEFB $06,$FD

; Put a record at the ship, offset
;
; Used by the routine at wreck_ship.
;
; Clears the record (free_object), copies the ship's place into it
; (copy_position with IY the ship's record), and moves it once by 28 pixels in
; X and ground_cell in Y (move_object), using the velocity as the offset.
;
; IX The record
; IY The ship's record (ship_record)
place_at_ship:
  CALL free_object        ; Clear it; at the ship
  CALL copy_position      ;
  LD (IX+$07),$1C         ; Moved by $1C across, ground_cell down
  LD A,(ground_cell)      ;
  LD (IX+$09),A           ;
  JP move_object          ;

; The rocket parts' share of the load
;
; Half the number of rocket parts delivered, rounded up, while draw_rocket is
; drawing them, else 0. update_objects starts each pass's load (object_load)
; from it.
rocket_parts_load:
  DEFB $00

; Skip a free record in the object loop
;
; Used by the routine at update_objects.
;
; Jumped to by update_objects for a record of type $FF: drop the record pointer
; it pushed and go on to the next record.
skip_free_record:
  POP IX                  ; Restore IX and go on
  JP next_object          ;

; Update the nine object records, once a pass
;
; Used by the routine at play.
;
; Called from the play loop (play) once a pass, and the most expensive thing in
; it: for each of the nine records at object_records that is not free it rubs
; out last pass's picture, runs the type's update routine through
; run_sky_object_handler (which moves, animates and redraws it), tests it
; against the ship (ship_contact) and against the player's shots
; (test_player_shots). It also keeps the death byte (death_count) counting, and
; at the end starts a new cloud or attacker when the load (object_load) is
; light.
;
; The load is the throttle on spawning: one for every record in use, one more
; for each that was on the screen last pass, one more for a big cloud, and half
; the number of rocket parts delivered (rocket_parts_load), since draw_rocket
; draws those in the same pass. Below 4 this routine spawns; spawn_tick stops
; at 8 and find_free_record at 7.
update_objects:
  LD A,(death_count)      ; Count the death byte up once a pass while it is not
  AND A                   ; zero
  JR Z,update_objects_0   ;
  INC A                   ;
  LD (death_count),A      ;
update_objects_0:
  CP $0A                  ; At 10 the ship breaks up into nine pieces
  CALL Z,wreck_ship       ; (wreck_ship)
  LD A,(death_count)      ; From 10 on, the death sound (sound_death)
  CP $0A                  ;
  CALL NC,sound_death     ;
  LD A,(fuel_saver)        ; Keep last pass's contact flag (last_pass_contact)
  LD (last_pass_contact),A ; and clear it for this pass
  XOR A                    ;
  LD (object_load),A       ;
  LD (fuel_saver),A        ;
  LD A,(rocket_parts_load) ; The load starts at the rocket parts' share
  LD (object_load),A       ; (rocket_parts_load)
  LD IX,object_records    ; Nine records
  LD B,$09                ;
update_objects_1:
  PUSH BC                 ; A free record (type $FF) is skipped
  PUSH IX                 ; (skip_free_record)
  LD A,(IX+$00)           ;
  INC A                   ;
  JP Z,skip_free_record   ;
  LD A,(IX+$17)           ; Count the contact cool-down in the high nibble of
  AND $F0                 ; +$17 down by one
  JR Z,update_objects_2   ;
  SUB $10                 ;
  LD B,A                  ;
  LD A,(IX+$17)           ;
  AND $0F                 ;
  OR B                    ;
  LD (IX+$17),A           ;
update_objects_2:
  LD A,(death_count)      ; While the ship is beginning to die (death byte 1-3)
  AND A                   ; every object explodes
  JR Z,update_objects_3   ;
  CP $04                  ;
  JR NC,update_objects_3  ;
  SET 3,(IX+$17)          ;
update_objects_3:
  LD A,$50                ; The default contact effect: damage and push ($50,
  LD (contact_effect),A   ; see ship_contact)
  LD A,(object_load)      ; Count the record in the load
  INC A                   ;
  LD (object_load),A      ;
  CALL clear_sprite_pair  ; No sprite sizes yet this pass (clear_sprite_pair)
  BIT 7,(IX+$10)          ; Drawn last pass: count it again and rub it out
  CALL NZ,count_and_erase ; (count_and_erase)
  RES 6,(IX+$10)          ; Clear the straddling flag; placing it sets it again
  EXX                     ; The update routines run in the other register set
  CALL run_sky_object_handler ; The type's update routine
                              ; (run_sky_object_handler)
  CALL ship_contact       ; Contact with the ship (ship_contact)
  POP IX                  ; Clouds (type $13), type $1D and type $1C...
  LD A,(IX+$00)           ;
  CP $13                  ;
  JR Z,update_objects_5   ;
  CP $1C                  ;
  JR Z,update_objects_4   ;
  CP $1D                  ;
  JR Z,update_objects_5   ;
  JR update_objects_6     ;
update_objects_4:
  LD A,(IX+$14)           ; ...but not a piece of the ship's wreck (+$14 =
  CP $02                  ; 2)...
  JR Z,update_objects_6   ;
update_objects_5:
  LD A,(old_sprite_rows)  ; ...are freed once they have left the view: drawn
  AND A                   ; last pass and not this one (the two heights in
  JR Z,update_objects_6   ; old_sprite_x)
  LD A,(new_sprite_rows)  ;
  AND A                   ;
  JR NZ,update_objects_6  ;
  LD (IX+$00),$FF         ;
update_objects_6:
  BIT 5,(IX+$10)          ; Shot already, or exploding: no shot test
  JR NZ,next_object       ;
  BIT 3,(IX+$17)          ;
  JR NZ,next_object       ;
  CALL object_box         ; Its box (object_box) against the player's shots
  CALL test_player_shots  ; (test_player_shots)
; This entry point is used by the routine at skip_free_record.
next_object:
  LD BC,$0018             ; Next record
  ADD IX,BC               ;
  POP BC                  ;
  DEC B                   ;
  JP NZ,update_objects_1  ;
  LD A,(object_load)      ; A load of 4 or more: nothing new this pass
  CP $04                  ;
  RET NC                  ;
  LD A,R                  ; Half the time, while the view is in page rows 3-10,
  AND $08                 ; a cloud (spawn_cloud, which makes one only in rows
  JR Z,update_objects_7   ; 3-9)
  LD A,(view_row)         ;
  CP $0B                  ;
  JR NC,update_objects_7  ;
  CP $03                  ;
  JR C,update_objects_7   ;
  JP spawn_cloud          ;
update_objects_7:
  JP spawn_attacker       ; Otherwise an attacker from the wave table (the
                          ; entry point in spawn_tick)

; Count a drawn object in the load again and rub it out
;
; Used by the routine at update_objects.
;
; Used by update_objects for an object that was drawn last pass (bit 7 of
; +$10): an object on the screen weighs two in the load (object_load). Then
; rubs out its old picture (prepare_erase), which also clears bit 7.
count_and_erase:
  LD A,(object_load)      ; One more in the load
  INC A                   ;
  LD (object_load),A      ;
  JP prepare_erase        ; Rub out last pass's picture

; The load
;
; Worked out afresh each pass by update_objects: the rocket parts' share
; (rocket_parts_load), one for each record in use, one more for each drawn last
; pass (count_and_erase) and one more for a big cloud (update_cloud). The
; spawners read it: update_objects spawns below 4, spawn_tick not at 8 or more,
; find_free_record not at 7 or more.
object_load:
  DEFB $00

; The current object's contact effect
;
; What the current object does to the ship when they touch, one bit per effect:
; set to $50 for each object by update_objects, changed by its update routine,
; and acted on by ship_contact. Bit 7 slows the ship (contact_slow_ship), 6
; damages it (contact_damage), 5 refuels (contact_refuel), 4 pushes
; (contact_push), 3 collects the object (contact_collect), 2 drains fuel
; (contact_drain_fuel), 1 gives a life (contact_extra_life), 0 destroys the
; object (contact_destroy); $FF is a shield, and 0 is harmless.
contact_effect:
  DEFB $00

; Test the current object against the ship and apply its contact effect
;
; Used by the routine at update_objects.
;
; Called by update_objects after each object's update. Nothing happens while
; the rocket is launching (launch_state), while the ship is dying
; (death_count), when the object is harmless (contact effect 0, object_load),
; when it was not drawn this pass, or when it is exploding. Its box -- the
; screen x and bottom line it was drawn at (+$11, +$12), the width
; (sprite_width_drawn) and height (in old_sprite_x) it was drawn with -- goes
; to collision_box, and an object whose x is outside $28-$8D is too far from
; the ship to touch it. The ship's box is fixed in x: 40 pixels wide from
; x=100, 16 lines high, its bottom 8 lines below the ship's line (ship_line).
; boxes_overlap tests the two.
;
; On a touch the effect bits are acted on from bit 7 down (see object_load), IX
; the object and IY the ship's record (in bare_ship). $FF is different: it
; starts the shield (shield_timer = $FF, counted down every other pass by
; ship_pass, so about 25 seconds), and frees the object with a sound.
;
; Without a touch, a cloud may still touch what the ship is carrying
; (carried_part keeps a flag in the player record, score, while it carries
; something): the carried thing's box (from carried_part_facing) is tested, and
; on a touch the ship's X velocity becomes minus half the cloud's, and
; fuel_saver is set. That branch never ran in the build's sessions; it rests on
; reading alone.
ship_contact:
  LD A,(launch_state)     ; Nothing during the launch or while dying, for a
  AND A                   ; harmless object, or for one not drawn this pass
  RET NZ                  ;
  LD A,(death_count)      ;
  AND A                   ;
  RET NZ                  ;
  LD A,(contact_effect)   ;
  AND A                   ;
  RET Z                   ;
  LD A,(new_sprite_rows)  ;
  AND A                   ;
  RET Z                   ;
  LD IX,(current_object)  ; Nor while it explodes
  BIT 3,(IX+$17)          ;
  RET NZ                  ;
  LD L,(IX+$11)           ; Its box at collision_box: x and bottom line; too
  LD H,(IX+$12)           ; far left or right of the ship to touch
  LD IX,collision_box     ;
  LD (IX+$03),A           ;
  LD A,L                  ;
  CP $8E                  ;
  RET NC                  ;
  CP $28                  ;
  RET C                   ;
  LD (IX+$00),L             ; Width (the height went in above)
  LD (IX+$01),H             ;
  LD A,(sprite_width_drawn) ;
  LD (IX+$02),A             ;
  LD A,(ship_line)        ; The ship's box: x=100, 40 wide, 16 high, bottom at
  ADD A,$08               ; the ship's line + 8
  LD H,A                  ;
  LD L,$64                ;
  LD D,$28                ;
  LD E,$10                ;
  CALL boxes_overlap      ;
  LD IX,(current_object)  ; IY the ship's record; no touch: the cloud test
  LD IY,ship_record       ; below
  LD A,(scratch_count)    ;
  AND A                   ;
  JP Z,ship_contact_1     ;
  LD A,$80                ; Touched: the ship is to be redrawn (a flag,
  LD (trail_collision),A  ; trail_collision, read by draw_ship)
  LD A,(contact_effect)   ; The effect, $FF the shield
  LD C,A                  ;
  INC A                   ;
  JR NZ,ship_contact_0    ;
  DEC A                   ; The shield (shield_timer) counts down from $FF; the
  LD (shield_timer),A     ; object is freed, with a sound
  SET 4,(IX+$10)          ;
  JP sound_zap            ;
ship_contact_0:
  BIT 7,C                   ; Slow, damage, refuel, push, collect
  CALL NZ,contact_slow_ship ;
  BIT 6,C                   ;
  CALL NZ,contact_damage    ;
  BIT 5,C                   ;
  CALL NZ,contact_refuel    ;
  BIT 4,C                   ;
  CALL NZ,contact_push      ;
  BIT 3,C                   ;
  CALL NZ,contact_collect   ;
  BIT 2,C                    ; Drain fuel; an extra life
  CALL NZ,contact_drain_fuel ;
  PUSH BC                    ;
  BIT 1,C                    ;
  CALL NZ,contact_extra_life ;
  POP BC                     ;
  BIT 0,C                 ; And destroy the object
  JP NZ,contact_destroy   ;
  RET                     ;
ship_contact_1:
  LD A,(IX+$00)            ; No touch: only a cloud, and only while the ship
  CP $13                   ; carries something
  RET NZ                   ;
  LD A,(player_count_nine) ;
  AND A                    ;
  RET Z                    ;
  LD A,(ship_facing)      ; Only when the ship faces the way the cloud moves
  XOR (IX+$07)            ; (ship_facing)
  BIT 7,A                 ;
  RET NZ                  ;
  LD A,(carried_big_y)    ; Test the carried thing's box against the cloud
  LD H,A                  ;
  LD A,(carried_small_x)  ;
  ADD A,$08               ;
  LD L,A                  ;
  LD A,$50                ;
  CALL clip_height_to_top ;
  LD E,B                  ;
  LD D,$0C                ;
  LD IX,collision_box     ;
  CALL boxes_overlap      ;
  LD A,(scratch_count)    ; Touched
  AND A                   ;
  RET Z                   ;
  LD A,$80                ;
  LD (trail_collision),A  ;
  LD IX,(current_object)  ; The ship's X velocity becomes minus half the
  LD H,(IX+$07)           ; cloud's
  LD L,(IX+$08)           ;
  CALL negate_hl          ;
  SRA H                   ;
  RR L                    ;
  LD (IY+$19),H           ;
  LD (IY+$1A),L           ;
  LD A,$FF                ; Note it for the fuel burn (read_controls) and for
  LD (fuel_saver),A       ; the slowing (contact_slow_ship)
  RET                     ;

; Contact effect bit 7: halve the ship's velocity
;
; Used by the routine at ship_contact.
;
; A cloud's effect ($80). Halves each component of the ship's velocity (the
; words at +$19 and +$1B of its record) that has the same sign as the object's
; velocity in that axis, so flying through a cloud is slow. Not done in a pass
; after the carried thing was pushed by a cloud (last_pass_contact).
;
; IX The object
; IY The ship's record (ship_record)
contact_slow_ship:
  LD A,(last_pass_contact) ; Not after a push last pass
  AND A                    ;
  RET NZ                   ;
  LD D,(IY+$19)             ; X: halve it when the signs agree
  LD E,(IY+$1A)             ;
  LD A,D                    ;
  XOR (IX+$07)              ;
  BIT 7,A                   ;
  JR NZ,contact_slow_ship_0 ;
  SRA D                     ;
  RR E                      ;
  LD (IY+$19),D             ;
  LD (IY+$1A),E             ;
contact_slow_ship_0:
  LD D,(IY+$1B)           ; Y: the same
  LD E,(IY+$1C)           ;
  LD A,D                  ;
  XOR (IX+$09)            ;
  BIT 7,A                 ;
  RET NZ                  ;
  SRA D                   ;
  RR E                    ;
  LD (IY+$1B),D           ;
  LD (IY+$1C),E           ;
  RET

; The death byte
;
; 0 while the ship is alive. A third hit, from an attacker or a beam
; (contact_damage), or an empty fuel gauge (check_fuel_empty) sets it to 1, and
; update_objects counts it up by one a pass. 1-3: every object explodes
; (update_objects). 10: the ship breaks up into pieces (wreck_ship). 12 and
; over: the play loop runs the death sequence (play), which ends at $50. While
; it is not zero nothing touches the ship (ship_contact) and nothing new is
; spawned (find_free_record).
;
; draw_rocket sets it to 10 for the length of two calls when the ship docks
; with the finished rocket, and puts it back to 0 at once: that is not a death.
death_count:
  DEFB $00

; Contact effect bit 6: damage the ship
;
; Used by the routine at ship_contact.
;
; The attackers' effect. An object damages the ship at most once every 16
; passes: it sets the cool-down in the high nibble of its +$17 to 15
; (update_objects counts it down) and does nothing while it is not zero. With
; the shield up (shield_timer) an ordinary attacker (effect $50 or $51) is
; destroyed instead, with a sound.
;
; damage_ship (the entry point at the middle of this routine), reached also
; from a beam (beam_hits_ship) and from ship_hits_ground_objects, does nothing
; with the shield up or while the ship is already dying. Otherwise it counts
; the hit (the last byte of ship_icon_positions) and recolours the ship icon on
; the panel: yellow ($46) after the first hit, magenta ($43) after the second,
; and black after the third, which sets the death byte (death_count) to 1. It
; is also the repair: a refuel pad (ship_hits_ground_objects) sets the count to
; $FF first, so it wraps to 0 and the icon goes back to white ($47). The icon
; is a 4 by 2 character patch (ship_icon_positions) at x=$30 for player 1 and
; x=$B0 for player 2 (the player number is in player_in_play), painted by
; fill_attributes.
;
; IX The object
; C The contact effect
contact_damage:
  LD A,(IX+$17)           ; Still cooling down from the last touch
  AND $F0                 ;
  RET NZ                  ;
  LD A,(IX+$17)           ; Cool-down 15
  ADD A,$F0               ;
  LD (IX+$17),A           ;
  LD A,(shield_timer)     ; No shield: damage
  AND A                   ;
  JR Z,damage_ship        ;
  LD A,C                  ; Shielded, and an ordinary attacker: it is shot
  AND $FE                 ; instead
  CP $50                  ;
  JR NZ,damage_ship       ;
  SET 5,(IX+$10)          ;
  CALL sound_zap          ;
; This entry point is used by the routines at beam_hits_ship and
; ship_hits_ground_objects.
damage_ship:
  LD A,(shield_timer)     ; No damage with the shield up, or while dying
  AND A                   ;
  RET NZ                  ;
  LD A,(death_count)      ;
  AND A                   ;
  RET NZ                  ;
  CALL sound_zap          ; A sound, and one more hit
  LD A,(hit_count)        ;
  INC A                   ;
  LD (hit_count),A        ;
  PUSH BC                 ; White after a repair (count 0), yellow after one
  LD B,$47                ; hit, magenta after two
  AND A                   ;
  JR Z,contact_damage_0   ;
  LD B,$46                ;
  CP $01                  ;
  JR Z,contact_damage_0   ;
  LD B,$43                ;
contact_damage_0:
  CP $03                  ; The third: the ship dies, and the icon goes black
  JR NZ,contact_damage_1  ;
  LD A,$01                ;
  LD (death_count),A      ;
  LD B,$00                ;
contact_damage_1:
  LD A,B                  ; The patch's colour
  LD ($8FC1),A            ;
  LD HL,$0F30             ; Paint it over this player's ship icon
  LD A,(player_in_play)   ;
  AND A                   ;
  JR Z,contact_damage_2   ;
  LD HL,$0FB0             ;
contact_damage_2:
  LD DE,icon_patch_record ;
  CALL fill_attributes    ;
  POP BC
  RET

; The two ship icons' positions (unused)
;
; The panel positions of the two players' ship icons as words, x then line
; ($0F30 and $0FB0); but contact_damage loads them as constants, and nothing
; reads these.
ship_icon_positions:
  DEFB $30,$0F,$B0,$0F

; The ship icon's colour patch
;
; The patch record fill_attributes paints over the current player's ship icon
; (contact_damage): width 4 characters, height 2, and the colour, which
; contact_damage writes. The fourth byte is a $C9 that nothing uses.
icon_patch_record:
  DEFB $04,$02,$00,$C9    ; Width, height, colour; and an unused byte

; Hits taken by this ship
;
; 0-3, counted up by each hit (contact_damage); the third kills the ship.
; Cleared for a new ship (reset_for_life), and set to $FF by a refuel pad's
; repair (ship_hits_ground_objects), so that the damage code wraps it to 0.
hit_count:
  DEFB $00

; Contact effect bit 5: refuel
;
; Used by the routine at ship_contact.
;
; The refuel pick-up's effect ($28 with bit 3: refuel and collect). Adds -$1C00
; to the fuel word through add_fuel, which clamps it at full: the gauge's high
; byte runs from $42 (full) to $BE (empty), so a pick-up is worth 28 of its 124
; steps.
contact_refuel:
  LD DE,$E400
  JP add_fuel

; Contact effect bit 4: bounce the object off the ship
;
; Used by the routine at ship_contact.
;
; Part of the default effect. For each axis the object's velocity is halved and
; negated, so it bounces away at half speed, and the same amount is added to
; the ship's velocity word in that axis (+$19, +$1B of its record).
;
; IX The object
; IY The ship's record (ship_record)
contact_push:
  LD D,(IY+$19)           ; X: the object's velocity becomes minus half of
  LD E,(IY+$1A)           ; itself; the ship's has that added
  LD H,(IX+$07)           ;
  LD L,(IX+$08)           ;
  SRA H                   ;
  RR L                    ;
  CALL negate_hl          ;
  LD (IX+$07),H           ;
  LD (IX+$08),L           ;
  ADD HL,DE               ;
  LD (IY+$19),H           ;
  LD (IY+$1A),L           ;
  LD D,(IY+$1B)           ; Y: the same
  LD E,(IY+$1C)           ;
  LD H,(IX+$09)           ;
  LD L,(IX+$0A)           ;
  SRA H                   ;
  RR L                    ;
  CALL negate_hl          ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  ADD HL,DE               ;
  LD (IY+$1B),H           ;
  LD (IY+$1C),L           ;
  RET

; Contact effect bit 3: collect the object
;
; Used by the routine at ship_contact.
;
; Marks the object to be freed (bit 4 of +$10, which place_object acts on next
; pass) and makes the pick-up sound (sound_collect).
contact_collect:
  SET 4,(IX+$10)
  PUSH BC
  CALL sound_collect
  POP BC
  RET

; Contact effect bit 2: take fuel
;
; Used by the routine at ship_contact.
;
; A sound, then adds $1000 to the fuel word (add_fuel): towards empty, 16 of
; the gauge's 124 steps. The effect of the pick-up with frame 6 ($1C: push,
; collect, take fuel, small_object_effects). Never ran in the build's sessions.
contact_drain_fuel:
  PUSH BC
  CALL sound_zap
  POP BC
  LD DE,$1000
  JP add_fuel

; Contact effect bit 1: an extra life
;
; Used by the routine at ship_contact.
;
; The ship-icon pick-up's effect ($1A). One more life (in the player record,
; score), but never more than 9; a sound, and the panel's count redrawn
; (print_lives). Never ran in the build's sessions.
contact_extra_life:
  LD A,(lives)
  INC A
  CP $0A
  RET Z
  LD (lives),A
  CALL sound_zap
  JP print_lives

; Contact effect bit 0: the object is destroyed
;
; Used by the routine at ship_contact.
;
; Marks the object shot (bit 5 of +$10), as a shot would: its update routine
; scores it and turns it into debris next pass (check_shot). The effect of type
; $1D ($51), which damages the ship and dies doing it.
contact_destroy:
  SET 5,(IX+$10)
  RET

; Score a shot object and turn it into debris
;
; Used by the routines at update_rammer and update_attacker.
;
; Used by the attackers' update routines (update_attacker, update_rammer). If
; the object has been shot (bit 5 of +$10) it is scored (score_object, in
; test_player_shots) and becomes debris: type $15 (update_debris), frame count
; $50, frames 0-5, and the path step at the start of a random quarter of the
; path's mirror images (bits 6-7 of +$15). The zero flag tells the caller
; which.
;
; make_debris is also how the ship's wreck starts (wreck_ship), which sets the
; four quarters itself.
;
;   IX The object
; O:F Z if it had not been shot
check_shot:
  BIT 5,(IX+$10)          ; Not shot
  RET Z                   ;
  CALL score_object       ; Score it
; This entry point is used by the routine at wreck_ship.
make_debris:
  LD (IX+$00),$15         ; Debris: type $15, frame count $50, frames 0 to 5
  LD (IX+$13),$50         ;
  LD (IX+$14),$05         ;
  LD A,(random_b)         ; A random mirror image of the path
  AND $03                 ;
  RRCA                    ;
  RRCA                    ;
  LD (IX+$15),A           ;
  OR $01                  ; NZ: it was shot
  RET                     ;

; Test an object against the landscape, a little down and to the left
;
; Used by the routines at move_bouncing, move_or_explode and probe_shot.
;
; Moves the object 4 pixels left and 4 down (by setting its velocity to that,
; moving it with move_object and putting the velocity back), tests it against
; the landscape with collide_with_landscape, and moves it back. The 4-pixel
; offset lines the object's box up with its sprite. The mode
; collide_with_landscape tests in (piece_step_x) is the caller's: see
; move_bouncing and move_or_explode; the result is in scratch_count.
;
; nudge_object moves the object by A pixels down and -A across, keeping its
; velocity, with the velocity saved in the four bytes at current_piece while it
; does.
;
; BC The box's height and width in pixels, for collide_with_landscape
test_landscape_at:
  LD A,$04                ; Nudge it left and down
  PUSH BC                 ;
  CALL nudge_object       ;
  POP BC                  ;
  CALL collide_with_landscape ; Test the landscape
  LD A,$FC                ; And back
nudge_object:
  LD H,(IX+$07)           ; Keep its velocity
  LD L,(IX+$08)           ;
  LD (current_piece),HL   ;
  LD H,(IX+$09)           ;
  LD L,(IX+$0A)           ;
  LD ($DEDE),HL           ;
  LD (IX+$09),A           ; Velocity -A across, A down
  LD (IX+$0A),$00         ;
  NEG                     ;
  LD (IX+$07),A           ;
  LD (IX+$08),$00         ;
  CALL move_object        ; Move it
  LD HL,(current_piece)   ; Put the velocity back
  LD (IX+$07),H           ;
  LD (IX+$08),L           ;
  LD HL,($DEDE)           ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  RET                     ;

; Move an object that bounces off the landscape
;
; Used by the routines at move_missile and update_attacker.
;
; Used by the attackers, the debris and the homing missiles. Tests the
; landscape in mode 0 (piece_step_x) and then moves the object by its velocity
; (move_object). In mode 0 place_collision_box negates the object's velocity
; before the test, and this routine negates it again after; how a touch turns
; into a bounce is collide_with_landscape's business and not yet worked out
; here.
;
; BC The box's height and width in pixels
move_bouncing:
  XOR A                   ; Test the landscape in mode 0
  LD (piece_step_x),A     ;
  CALL test_landscape_at  ;
  CALL negate_velocity    ; Negate the velocity again, and move
  JP move_object          ;

; Move an object that explodes on the landscape
;
; Used by the routines at update_small_object and update_rammer.
;
; Used by the small objects (update_small_object) and type $1D (update_rammer).
; Tests the landscape in mode $80 (piece_step_x); on a touch (scratch_count)
; the object starts exploding (bit 3 of +$17), stops, and makes a sound
; (sound_tick). Then it is moved by its velocity (move_object).
;
; BC The box's height and width in pixels
move_or_explode:
  LD A,$80                ; Test the landscape in mode $80
  LD (piece_step_x),A     ;
  CALL test_landscape_at  ;
  LD A,(scratch_count)    ; No touch: just move it
  AND A                   ;
  JP Z,move_object        ;
  SET 3,(IX+$17)          ; Touched: it explodes, and stops
  XOR A                   ;
  LD (IX+$07),A           ;
  LD (IX+$08),A           ;
  LD (IX+$09),A           ;
  LD (IX+$0A),A           ;
  CALL sound_tick         ; A sound, and move it
  JP move_object          ;

; Negate an object's velocity
;
; Used by the routines at move_bouncing and place_collision_box.
;
; Both components, through negate_hl.
;
; IX The object
negate_velocity:
  LD H,(IX+$07)
  LD L,(IX+$08)
  CALL negate_hl
  LD (IX+$07),H
  LD (IX+$08),L
  LD H,(IX+$09)
  LD L,(IX+$0A)
  CALL negate_hl
  LD (IX+$09),H
  LD (IX+$0A),L
  RET

; Place an object on the screen, or free it
;
; Used by the routines at update_attacker, update_rocket_part and draw_rocket.
;
; Remembers the object in current_object. An object marked to be freed (bit 4
; of +$10) is freed now. One with bit 3 of +$10 set has it cleared and is not
; placed this pass. Otherwise prepare_draw works out where on the screen it is
; (or frees it when it is far from the view), with its sprite and colour, and
; steer_to_view turns an attacker that is off the screen back towards the view.
;
; IX The object
place_object:
  LD (current_object),IX  ; Remembered for the routines after the update
  BIT 4,(IX+$10)          ; To be freed
  JR Z,place_object_0     ;
  LD (IX+$00),$FF         ;
  RET                     ;
place_object_0:
  BIT 3,(IX+$10)          ; Skip one placing
  RES 3,(IX+$10)          ;
  RET NZ                  ;
  CALL prepare_draw       ; Place it, and steer it back if it is off the screen
  JP steer_to_view        ;

; Step an object's animation
;
; Used by the routines at update_small_object, update_rammer, update_attacker
; and update_ground_objects.
;
; The frame byte +$13: bits 0-3 the frame, bits 4-5 a count, bits 6-7 the
; count's reload. Each call counts bits 4-5 down; when they read 1 the frame
; steps on, from the last frame (low nibble of +$14) back to the first (high
; nibble of +$14), and the count is reloaded from bits 6-7. So a type's
; animation runs at one frame every one to three passes over any run of its
; frame list. (A count of 0 sets bits 4-7 all, and the animation goes on at the
; slowest rate.)
;
; IX The object
animate_object:
  LD A,(IX+$13)           ; Count 1: step the frame
  LD B,A                  ;
  AND $30                 ;
  SUB $10                 ;
  LD D,A                  ;
  JR NZ,animate_object_1  ;
  LD A,B                  ; The next frame, or the first after the last
  AND $0F                 ;
  INC A                   ;
  LD E,A                  ;
  LD A,(IX+$14)           ;
  AND $0F                 ;
  INC A                   ;
  CP E                    ;
  JR NZ,animate_object_0  ;
  LD E,(IX+$14)           ;
  SRL E                   ;
  SRL E                   ;
  SRL E                   ;
  SRL E                   ;
animate_object_0:
  LD A,B                  ; Reload the count from bits 6-7
  AND $F0                 ;
  OR E                    ;
  LD B,A                  ;
  LD A,B                  ;
  AND $C0                 ;
  SRL A                   ;
  SRL A                   ;
  LD D,A                  ;
animate_object_1:
  LD A,B                  ; Count down, or the reloaded count, into bits 4-5
  AND $CF                 ;
  OR D                    ;
  LD (IX+$13),A           ;
  RET

; Make the current object's box for the shot tests
;
; Used by the routines at update_objects and beam_hits_ground_object.
;
; The box at collision_box from where the object was drawn this pass
; (current_object): x, bottom line, width and height. A sprite straddling the
; left edge of the view (a flag in old_sprite_width) has its x moved in and its
; width cut, and one running off the right is cut at the right edge ($EE).
; row_count gets its top line. Nothing is done when it was not drawn (height 0
; in old_sprite_x); the height left in D tells test_player_shots the same.
;
; O:D The height drawn (0: not drawn)
object_box:
  LD IY,(current_object)    ; Drawn at x, bottom line; drawn this wide
  LD L,(IY+$11)             ;
  LD H,(IY+$12)             ;
  LD A,(sprite_width_drawn) ;
  LD E,A
  LD A,(new_sprite_rows)  ; This high; not drawn: no box
  LD D,A                  ;
  LD A,D                  ;
  DEC A                   ;
  RET M                   ;
  LD A,(sprite_left_clip) ; Straddling the left edge: x moves in by 8 and the
  AND A                   ; width is cut
  LD A,L                  ;
  JR Z,object_box_0       ;
  ADD A,E                 ;
  LD L,$08                ;
  SUB L                   ;
  LD E,A                  ;
  JR object_box_1         ;
object_box_0:
  ADD A,$17               ; Running off the right: the width ends at x=$EE
  ADD A,E                 ;
  JR NC,object_box_1      ;
  LD A,$EE                ;
  SUB L                   ;
  LD E,A                  ;
object_box_1:
  LD (collision_box),HL      ; The box
  LD (collision_box_size),DE ;
  LD A,H                  ; Its top line
  SUB D                   ;
  LD (row_count),A        ;
  RET                     ;

; Test the current object against the player's four shots
;
; Used by the routine at update_objects.
;
; The object's box is at collision_box (object_box). The shots are four records
; of 24 bytes at shot_records (updated by update_shots). Two kinds: a homing
; missile (bit 7 of its +$16; update_missile) is a box 8 wide and 5 high at its
; drawn position (+$11, +$12), tested only while drawn. The other kind, the
; laser, is a line: x in its +$0B (0 when there is none), line in +$0C, length
; in characters in +$0E; when bit 7 of its +$15 is set it is 8 lines high and
; the line test is skipped. The test is boxes_overlap.
;
; A hit marks the shot (bit 7 of +$10; a missile is also freed) and the object
; shot (bit 5 of +$10), which its update routine turns into a score and debris
; (check_shot). A missile hitting type $19 counts a hit on it directly (+$16)
; and scores it. A shot with bit 7 of +$15 set destroys any object outright
; instead (bit 3 of +$17), scoring it double; type $19 gives its points that
; way only when it is already worn down (+$D = $FF), and then also credits ore
; to the part being carried (the second entry point of load_ore). Only the
; first shot that hits counts.
;
; score_object adds an object's points to the score: 250 for types $0D and $1B,
; otherwise from type_points, doubled for one destroyed outright. add_score
; adds BC (BCD, B the high pair) to the current player's score (the first three
; bytes of the player record, score, BCD, low first), and prints it on the
; panel with print_string: the digits are font codes $1A-$23 (font), written
; into the panel's text (p1_score_digits, the record in front).
;
; IX The object's record
; D The height it was drawn with (from object_box)
test_player_shots:
  LD A,D                  ; Not drawn: no test
  DEC A                   ;
  RET M                   ;
  PUSH IX                 ; Four shots (shot_records), the object's box at
  LD IX,collision_box     ; collision_box
  LD A,$04                ;
  LD IY,shot_records      ;
; This entry point is used by the routine at test_shot_box.
test_shot_list:
  EX AF,AF'                 ; A missile, or the laser
  BIT 7,(IY+$16)            ;
  JR NZ,test_player_shots_0 ;
  LD L,(IY+$0B)            ; The laser: its x, 0 when there is none
  LD A,L                   ;
  AND A                    ;
  JR Z,test_player_shots_6 ;
  JR test_player_shots_1   ;
test_player_shots_0:
  BIT 7,(IY+$10)           ; A missile, only while drawn: its x and line
  JR Z,test_player_shots_6 ;
  LD L,(IY+$11)            ;
  LD H,(IY+$12)            ;
  JR test_player_shots_2   ;
test_player_shots_1:
  LD H,(IY+$0C)             ; The laser's line; a tall one skips the line test
  INC H                     ;
  BIT 7,(IY+$15)            ;
  JR NZ,test_player_shots_3 ;
test_player_shots_2:
  LD A,(row_count)          ; The line within the object's height: 5 high
  INC H                     ;
  CP H                      ;
  JR NC,test_player_shots_6 ;
  DEC H                     ;
  LD A,(IX+$01)             ;
  ADD A,$03                 ;
  CP H                      ;
  JR C,test_player_shots_6  ;
  LD E,$05                  ;
  JR test_player_shots_4    ;
test_player_shots_3:
  LD E,$08                ; Tall: 8
test_player_shots_4:
  LD D,$08                  ; Missile: 8 wide; laser: its length in characters
  BIT 7,(IY+$16)            ; times 8
  JR NZ,test_player_shots_5 ;
  LD A,(IY+$0E)             ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,A                   ;
  JR Z,test_player_shots_6  ;
  LD D,A                    ;
test_player_shots_5:
  CALL boxes_overlap        ; The test
  LD A,(scratch_count)      ;
  AND A                     ;
  JR NZ,test_player_shots_7 ;
test_player_shots_6:
  LD BC,$0018             ; Next shot
  ADD IY,BC               ;
  EX AF,AF'               ;
  DEC A                   ;
  JR NZ,test_shot_list    ;
  POP IX                  ;
  RET                     ;
test_player_shots_7:
  POP IX                    ; A missile hit: it is freed; on type $19 a hit
  BIT 7,(IY+$16)            ; counted, with a sound and the points
  JR Z,test_player_shots_8  ;
  SET 4,(IY+$10)            ;
  LD A,(IX+$00)             ;
  CP $19                    ;
  JR NZ,test_player_shots_8 ;
  INC (IX+$16)              ;
  CALL sound_zap            ;
  CALL score_object         ;
test_player_shots_8:
  SET 7,(IY+$10)          ; The shot has hit; the object is shot
  SET 5,(IX+$10)          ;
  BIT 7,(IY+$15)          ; A heavy shot destroys it outright
  RET Z                   ;
  SET 3,(IX+$17)          ;
  RES 5,(IX+$10)          ;
  LD A,(IX+$00)           ; Type $19 scores...
  CP $19                  ;
  JR NZ,score_object      ;
  LD A,(IX+$0D)           ; ...only when worn down: points, and ore for part 6
  INC A                   ;
  RET NZ                  ;
  CALL score_object       ;
  LD A,R                  ;
  JP load_ore_0           ;
; This entry point is used by the routines at check_shot and
; update_tough_attacker.
score_object:
  LD A,(IX+$00)              ; Types $0D and $1B: 250
  CP $0D                     ;
  JR Z,test_player_shots_9   ;
  CP $1B                     ;
  JR NZ,test_player_shots_10 ;
test_player_shots_9:
  LD BC,$0250                ;
  JR add_score               ;
test_player_shots_10:
  SUB $13                 ; The rest from the table, by type
  ADD A,A                 ;
  LD HL,type_points       ;
  CALL add_a_to_hl        ;
  LD B,(HL)               ;
  INC HL                  ;
  LD C,(HL)               ;
  BIT 3,(IX+$17)          ; Doubled when destroyed outright
  JR Z,add_score          ;
  LD A,C                  ;
  ADD A,A                 ;
  DAA                     ;
  LD C,A                  ;
  LD A,B                  ;
  ADC A,B                 ;
  DAA                     ;
  LD B,A                  ;
; This entry point is used by the routines at planet_secured,
; update_rocket_part and collect_pickup.
add_score:
  LD HL,score             ; Add BC to the score
  LD A,C                  ;
  ADD A,(HL)              ;
  DAA                     ;
  LD (HL),A               ;
  INC HL                  ;
  LD A,B                  ;
  ADC A,(HL)              ;
  DAA                     ;
  LD (HL),A               ;
  INC HL                  ;
  LD A,$00                ;
  ADC A,(HL)              ;
  DAA                     ;
  LD (HL),A               ;
  LD DE,p1_score_digits     ; Six digits, high first, as font codes
  LD B,$03                  ;
test_player_shots_11:
  LD A,(HL)                 ;
  RRCA                      ;
  RRCA                      ;
  RRCA                      ;
  RRCA                      ;
  AND $0F                   ;
  ADD A,$1A                 ;
  LD (DE),A                 ;
  INC DE                    ;
  LD A,(HL)                 ;
  AND $0F                   ;
  ADD A,$1A                 ;
  LD (DE),A                 ;
  INC DE                    ;
  DEC HL                    ;
  DJNZ test_player_shots_11 ;
  LD DE,p1_score_text     ; Print them
  EXX                     ;
  PUSH HL                 ;
  PUSH DE                 ;
  PUSH BC                 ;
  LD DE,p1_score_text     ;
  CALL print_string       ;
  POP BC                  ;
  POP DE                  ;
  POP HL                  ;
  EXX                     ;
  RET                     ;

; Points by type
;
; For types $13 to $1E, two BCD bytes each, the high pair first (score_object
; in test_player_shots): cloud 0, $14 250, debris 0, $16 200, $17 150, $18 100,
; $19 75 a hit, $1A 90, $1B 0 (it scores 250 as a special case), $1C 0, $1D
; 350, $1E 0.
type_points:
  DEFB $00,$00
  DEFB $02,$50
  DEFB $00,$00
  DEFB $02,$00
  DEFB $01,$50
  DEFB $01,$00
  DEFB $00,$75
  DEFB $00,$90
  DEFB $00,$00
  DEFB $00,$00
  DEFB $03,$50
  DEFB $00,$00

; Pick a target for a homing missile
;
; Used by the routine at update_missile.
;
; Called by update_missile when the missile has no target. The type is first
; tested for a free record by INC A, so the SUB $14 after it leaves the type
; less $13, the same index as type_points's. Goes through the first seven
; records at object_records and picks, of those drawn this pass, the one whose
; type is worth most in missile_target_values, putting its record number in the
; high nibble of the missile's +$17 (the low nibble is cleared with it). None
; found: +$17 stays $F0. The best value so far is kept in scratch_count.
;
; IX The missile's record
find_missile_target:
  LD IY,object_records    ; Seven records; best value 0
  LD B,$07                ;
  XOR A                   ;
  LD (scratch_count),A    ;
find_missile_target_0:
  BIT 7,(IY+$10)             ; Drawn this pass, and in use
  JR Z,find_missile_target_1 ;
  LD A,(IY+$00)              ;
  INC A                      ;
  JR Z,find_missile_target_1 ;
  SUB $14                     ; Its value, by the type less $13 (the INC A
  LD HL,missile_target_values ; above); not more than the best: skip
  CALL add_a_to_hl            ;
  LD A,(scratch_count)        ;
  CP (HL)                     ;
  JR NC,find_missile_target_1 ;
  LD A,(HL)               ; The best so far
  LD (scratch_count),A    ;
  LD A,B                  ; Its record number, in the high nibble of +$17
  XOR $07                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD (IX+$17),A           ;
find_missile_target_1:
  LD DE,$0018                ; Next record
  ADD IY,DE                  ;
  DJNZ find_missile_target_0 ;
  RET                        ;

; The homing missiles' target values
;
; By type less $13 (find_missile_target adds one to the type to test for a free
; record before it subtracts $14), the indexing of type_points, and in the
; order of the points there: $13 (a cloud) 0, $14 (the rotor craft, 250 points)
; 8, $15 (debris) 0, $16 (the saucer, 200) 7, $17 (the eye pod, 150) 6, $18
; (the pillar, 100) 5, $19 (the tough attacker, 75 a hit) 2, $1A (the dome, 90)
; 4, $1B 0, $1C (bullets and other thrown things) 0, $1D (rocks, 350) 9, $1E (a
; falling rocket part) 0. A value of 0 is never picked, so a cloud, which can
; come into records 0-3 through the wave table, reads the table's first byte
; and is passed over, as are debris, thrown things and rocket parts.
;
; In 240 seconds of simulated flying with missiles, run for the reference
; pages, all 285 targets chosen were the best on the screen by this ranking,
; and clouds, debris and bullets were never chosen.
missile_target_values:
  DEFB $00,$08,$00,$07,$06,$05,$02,$04,$00,$00,$09,$00

; Face an object the way it moves
;
; Used by the routines at move_missile and update_tough_attacker.
;
; Frame 1 when its X velocity is negative (moving left), 0 otherwise.
;
; IX The object
set_facing:
  LD (IX+$13),$00
  BIT 7,(IX+$07)
  RET Z
  INC (IX+$13)
  RET

; Update a homing missile
;
; Used by the routine at update_shots.
;
; Called by update_shots for each of the player's shots that is a homing
; missile (bit 7 of +$16) and is flying (first byte 0; $FF when spent). With no
; target (the high nibble of +$17 is $F) it looks for one (the code at offset
; 24 of type_points). With one it steers at it (steer_towards), and gives it up
; when the target's record is freed. Then it moves (move_missile).
;
; IX The missile's record
update_missile:
  LD A,(IX+$17)              ; No target: look for one
  AND $F0                    ;
  CP $F0                     ;
  CALL Z,find_missile_target ;
  LD A,(IX+$17)           ; Still none: just move
  AND $F0                 ;
  CP $F0                  ;
  JP Z,move_missile       ;
  RLCA                    ; The target's record
  RLCA                    ;
  RLCA                    ;
  RLCA                    ;
  LD B,A                  ;
  LD DE,$0018             ;
  LD IY,object_records    ;
  AND A                   ;
  JR Z,update_missile_1   ;
update_missile_0:
  ADD IY,DE               ;
  DJNZ update_missile_0   ;
update_missile_1:
  LD A,(IY+$00)           ; Freed: no target
  INC A                   ;
  JR NZ,update_missile_2  ;
  LD (IX+$17),$F0         ;
  JP move_missile         ;
update_missile_2:
  CALL steer_towards      ; Steer at it, and move
  JP move_missile         ;

; Accelerate an object towards another
;
; Used by the routines at update_missile, home_on_ship and turn_at_ship.
;
; Sets the acceleration (+$0B, +$0E) to $C0 (three quarters of a pixel a pass,
; each pass) towards the target in each axis. In X: the same page column
; decides by the pixel; otherwise the target is taken to be to the left when
; the object is in the column after the view's (kept in start_filled), and to
; the right otherwise. In Y, by the whole position.
;
; IX The object
; IY The target
steer_towards:
  LD (IX+$0B),$00         ; X: right, unless...
  LD (IX+$0C),$C0         ;
  LD A,(IX+$01)           ;
  CP (IY+$01)             ;
  JR NZ,steer_towards_0   ;
  LD A,(IX+$02)           ;
  CP (IY+$02)             ;
  JR NC,steer_towards_1   ;
  JR steer_towards_2      ;
steer_towards_0:
  LD A,(right_map_column) ; ...in the other page column
  CP (IX+$01)             ;
  JR Z,steer_towards_1    ;
  JR steer_towards_2      ;
steer_towards_1:
  LD (IX+$0B),$FF         ; Left
  LD (IX+$0C),$40         ;
steer_towards_2:
  LD H,(IX+$04)           ; Y: compare the positions (page row and pixel)
  LD L,(IX+$05)           ;
  LD D,(IY+$04)           ;
  LD E,(IY+$05)           ;
  SBC HL,DE               ;
  LD (IX+$0E),$00         ; Down
  LD (IX+$0F),$C0         ;
  RET C                   ;
  LD (IX+$0E),$FF         ; Up
  LD (IX+$0F),$40         ;
  RET                     ;

; Move, draw and time out a homing missile
;
; Used by the routine at update_missile.
;
; Rubs out its old picture, accelerates it (accelerate), faces it (set_facing),
; counts its life down in +$15 (freed at 0, or when marked by a hit), moves it
; bouncing off the landscape in a 16 by 11 box (move_bouncing), and draws it.
;
; One odd case: when the landscape piece under the missile is $3E (looked up in
; the four in map_window_pointer by which quarter of the view it is in) and its
; line is between $18 and $D7, its line is set to $6A. Not yet worked out.
;
; IX The missile's record
move_missile:
  CALL clear_sprite_pair  ; Rub out last pass's picture
  BIT 7,(IX+$10)          ;
  CALL NZ,prepare_erase   ;
  RES 6,(IX+$10)          ;
  EXX                     ; Accelerate, and face the way it moves
  CALL accelerate         ;
  CALL set_facing         ;
  DEC (IX+$15)            ; Its life is out: free it
  JR NZ,move_missile_1    ;
move_missile_0:
  LD (IX+$00),$FF         ;
  JP move_missile_5       ;
move_missile_1:
  BIT 4,(IX+$10)          ; Hit something: free it
  JR NZ,move_missile_0    ;
  LD BC,$0B10             ; Move it, bouncing
  CALL move_bouncing      ;
  LD HL,(view_column)         ; The landscape piece in its quarter of the view
  LD B,$00                    ;
  LD A,L                      ;
  CP (IX+$01)                 ;
  JR Z,move_missile_2         ;
  INC B                       ;
  INC B                       ;
move_missile_2:
  LD A,H                      ;
  CP (IX+$04)                 ;
  JR Z,move_missile_3         ;
  INC B                       ;
move_missile_3:
  LD A,B                      ;
  LD HL,map_window_tile_bytes ;
  CALL add_a_to_hl            ;
  LD A,(HL)                   ;
  CP $3E                  ; Piece $3E
  JR NZ,move_missile_4    ;
  LD A,(IX+$02)           ;
  CP $18                  ;
  JR C,move_missile_4     ;
  CP $D8                  ;
  JR NC,move_missile_4    ;
  LD (IX+$05),$6A         ;
move_missile_4:
  CALL prepare_draw       ; Place it
move_missile_5:
  CALL draw_object_images ; Draw it
  JP colour_sprite        ;

; Update routine for type $13, the clouds
;
; A cloud slows the ship (contact effect $80, contact_slow_ship), cannot be
; shot (its +$10 has bit 5 set from the start, spawn_tick), and explodes when
; it drifts below the bottom of page row 10 or into row 3. Clouds made high up
; (top speed 4) do not drift in Y. White clouds (+$D = $77) drop things on the
; ship (attacker_fires, kind 3). The big one (frame 1, one in eight) counts
; twice in the load. It moves bouncing off the landscape in a 48 by 32 box.
; Identified as clouds by their place in the sky, their colours and their
; harmlessness, not by name.
;
; IX The cloud's record
update_cloud:
  LD A,$80                ; Its contact effect
  LD (contact_effect),A   ;
  LD A,(IX+$04)           ; Below row 10's line $A0: it explodes
  CP $0A                  ;
  JR NZ,update_cloud_0    ;
  LD A,(IX+$05)           ;
  CP $A0                  ;
  JR C,update_cloud_1     ;
  SET 3,(IX+$17)          ;
  JR update_cloud_1       ;
update_cloud_0:
  CP $03                  ; In row 3: it explodes
  JR NZ,update_cloud_1    ;
  SET 3,(IX+$17)          ;
update_cloud_1:
  LD A,$03                ; White ones drop things
  LD (scratch_count),A    ;
  LD A,(IX+$0D)           ;
  CP $77                  ;
  PUSH IY                 ;
  CALL Z,attacker_fires   ;
  POP IY                  ;
  LD A,(IX+$10)           ; The high ones keep their height
  AND $03                 ;
  CP $03                  ;
  JR NZ,update_cloud_2    ;
  LD (IX+$0E),$00         ;
  LD (IX+$0F),$00         ;
update_cloud_2:
  CALL accelerate         ; Accelerate
  LD BC,$2030             ; 48 by 32; the big one counts twice; move and draw
  LD A,(IX+$13)           ; (update_attacker)
  AND A                   ;
  JP Z,move_draw          ;
  LD A,(object_load)      ;
  INC A                   ;
  LD (object_load),A      ;
  JP move_draw            ;

; Contact effects of type $1C, by frame
;
; One byte for each of the frames 0-10 of type $1C (update_small_object), the
; effect ship_contact acts on (see object_load): $50 damage and push for the
; bullets and the wreck's pieces; frame 3 $28, refuel and collect; frame 6 $1C,
; push, collect and take fuel; frame 8 (the ship icon) $1A, push, collect and
; an extra life; frame 9 (the ball) $FF, the shield.
small_object_effects:
  DEFB $50,$50,$50,$28,$58,$58 ; Frames 0-5
  DEFB $1C,$58,$1A,$FF,$58 ; Frames 6-10

; Update routine for type $1C, the bullets, pick-ups and wreck pieces
;
; The small things: what the attackers drop (attacker_fires), the pick-ups, and
; the five pieces of the ship's wreck (wreck_ship). The frame picks the contact
; effect (small_object_effects). A wreck piece (+$14 = 2) accelerates
; (accelerate); the others fly straight. The ship icon and the ball (frames 8
; and 9) keep their frame; the rest animate. Each lives as many passes as +$15
; says, then explodes, and explodes at once on touching the landscape
; (move_or_explode).
;
; IX The record
update_small_object:
  LD A,(IX+$13)              ; Contact effect by frame
  AND $0F                    ;
  LD HL,small_object_effects ;
  CALL add_a_to_hl           ;
  LD A,(HL)                  ;
  LD (contact_effect),A      ;
  LD A,(IX+$14)           ; A wreck piece accelerates
  CP $02                  ;
  CALL Z,accelerate       ;
  LD A,(IX+$13)              ; The ship icon and the ball: a 42-wide box, no
  AND $0F                    ; animation
  CP $08                     ;
  LD C,$2A                   ;
  JR Z,update_small_object_0 ;
  CP $09                     ;
  JR Z,update_small_object_0 ;
  CALL animate_object     ; The rest animate, 24 wide
  LD C,$18                ;
update_small_object_0:
  LD B,$18                ; 24 high; move, or explode on the landscape
  CALL move_or_explode    ;
  DEC (IX+$15)            ; Count its life down; at 0 it explodes; place and
  JP NZ,place_draw        ; draw it
  SET 3,(IX+$17)          ;
  JP place_draw           ;

; Update routine for type $1D
;
; Damages the ship and dies doing it (contact effect $51: damage, push,
; destroyed). Scored and turned to debris when shot (check_shot), animated,
; moved in a 32 by 32 box, exploding on the landscape (move_or_explode). Fast
; (top speed 7, from the wave table wave_table) and worth 350.
;
; IX The record
update_rammer:
  LD A,$51                ; Its contact effect
  LD (contact_effect),A   ;
  CALL check_shot         ; Shot: score and debris
  CALL animate_object     ; Animate, move, place, draw
  LD BC,$2020             ;
  CALL move_or_explode    ;
  JP place_draw           ;

; Update routine for type $19, the tough attacker
;
; Takes many hits: a shot (bit 5 of +$10) counts as one only on every eighth
; pass, in +$16, and a missile counts one directly (test_player_shots); each
; scores 75. From ten hits it flashes white (+$D = $FF instead of $55), and at
; fourteen it explodes, scored. It has an engine sound (sound_hum), drops
; things on the ship (attacker_fires, kind 7 or 8 by its facing), changes
; course now and then (change_course, steer_tough_attacker), faces the way it
; moves, and moves bouncing in a 48 by 24 box.
;
; IX The record
update_tough_attacker:
  CALL sound_hum          ; Engine sound; its colour
  LD (IX+$0D),$55         ;
  BIT 5,(IX+$10)                ; Shot, on every eighth pass: one more hit, a
  JR Z,update_tough_attacker_0  ; sound, 75 points
  LD A,(pass_counter)           ;
  AND $07                       ;
  JR NZ,update_tough_attacker_0 ;
  INC (IX+$16)                  ;
  CALL score_object             ;
  CALL sound_zap                ;
update_tough_attacker_0:
  RES 5,(IX+$10)          ; The shot is used up
  LD A,(IX+$16)                ; Ten hits: flashing white
  AND $3F                      ;
  CP $0A                       ;
  JR C,update_tough_attacker_1 ;
  LD (IX+$0D),$FF              ;
update_tough_attacker_1:
  CP $0E                       ; Fourteen: it explodes, scored
  JR C,update_tough_attacker_2 ;
  SET 3,(IX+$17)               ;
  CALL score_object            ;
update_tough_attacker_2:
  PUSH IY                 ; Drop something on the ship
  LD A,(IX+$13)           ;
  AND $01                 ;
  ADD A,$07               ;
  LD (scratch_count),A    ;
  CALL attacker_fires     ;
  POP IY                  ;
  CALL change_course      ; Change course now and then; accelerate
  CALL accelerate         ;
  LD A,(IX+$07)           ; Face the way it moves
  OR (IX+$08)             ;
  CALL NZ,set_facing      ;
  LD BC,$1830             ; 48 by 24; move and draw
  JP move_draw            ;

; Update routine for types $14, $16, $17, $18 and $1A, the attackers
;
; A shot attacker is scored and turned to debris (check_shot). Otherwise it
; changes course by its type (change_course) and accelerates (accelerate).
; Then, as for most types: animate (animate_object), move bouncing off the
; landscape in a 34 by 35 box (move_bouncing), place it (place_object), and
; draw it (draw_object_images) with its colour (colour_sprite). The other
; update routines join this tail at its entry points.
;
; IX The record
update_attacker:
  CALL check_shot         ; Shot: score and debris, and no steering
  JR NZ,animate_move_draw ;
  CALL change_course      ; Change course; accelerate
  CALL accelerate         ;
; This entry point is used by the routine at update_debris.
animate_move_draw:
  CALL animate_object     ; Animate
  LD BC,$2322             ; 34 wide, 35 high
; This entry point is used by the routines at update_cloud and
; update_tough_attacker.
move_draw:
  CALL move_bouncing      ; Move, bouncing
; This entry point is used by the routines at update_small_object and
; update_rammer.
place_draw:
  CALL place_object       ; Place it on the screen
; This entry point is used by the routines at update_rocket_part and
; update_debris.
draw_object:
  CALL draw_object_images ; Draw it
  JP colour_sprite        ; And colour it

; Update routine for type $1E, a rocket part falling into place
;
; Reached through the type table at object_handlers (its last word). A part the
; ship has let go of (drop_part) falls under its own acceleration, harmless to
; touch apart from a push (effect $10), and does not test the landscape. It
; stops when it reaches its place -- page row +$15 and the line in +$16, set by
; drop_part -- and is freed: as soon as it is in that row with its line at or
; below the slot's, so a part let go no higher than its slot is freed on its
; first pass without falling (run in SkoolKit's simulator for the Animations
; page); the parts delivered (offset $1B of the player record, score) go up by
; one and the player scores 1500. Then it is drawn (draw_object, the tail of
; update_attacker).
;
; IX The part's record
update_rocket_part:
  LD A,$10                ; Push only
  LD (contact_effect),A   ;
  CALL accelerate         ; Accelerate, and move
  CALL move_object        ;
  LD A,(IX+$04)              ; At its page row and line: free it
  CP (IX+$15)                ;
  JR NZ,update_rocket_part_0 ;
  LD A,(IX+$05)              ;
  CP (IX+$16)                ;
  JR C,update_rocket_part_0  ;
  SET 4,(IX+$10)             ;
update_rocket_part_0:
  CALL place_object       ; Place it (freed now if marked)
  PUSH HL                    ; Freed?
  LD HL,(current_object)     ;
  LD A,(HL)                  ;
  INC A                      ;
  JR NZ,update_rocket_part_1 ;
  LD A,(parts_built)      ; One more part delivered, and 1500 points
  INC A                   ;
  LD (parts_built),A      ;
  LD BC,$1500             ;
  CALL add_score          ;
update_rocket_part_1:
  POP HL                  ; Draw it
  JP draw_object          ;

; Put the rocket's position in its drawing record
;
; Used by the routine at draw_rocket.
;
; The rocket's base position, X from rocket_x and Y from the two bytes after it
; (rocket_y), goes into the drawing record (rocket_record).
;
; IX The drawing record (rocket_record)
put_rocket_record:
  LD HL,(rocket_x)
  LD (IX+$02),L
  LD (IX+$01),H
  LD HL,(rocket_y)
  LD (IX+$05),L
  LD (IX+$04),H
  RET

; Draw the rocket parts delivered, and launch the rocket
;
; Used by the routine at update_ground_objects.
;
; Called by update_ground_objects. The rocket is built at a fixed place, page
; column $52 and row 10, from up to eight parts brought there by the ship
; (drop_part, update_rocket_part). Each pass the view is near it (columns
; $51-$52, rows 9-10) -- or the rocket is launching -- every part delivered is
; drawn from the one record rocket_record: the base position (rocket_x,
; rocket_y) plus the part's offset from rocket_part_table, frame the part's
; number (part 6 uses frame 8). The part's flags and drawn position are kept in
; its entry of the table between passes, so its old picture can be rubbed out.
; Half the parts' number counts in the load (rocket_parts_load).
;
; When all eight are there and the ship stops in place in the rocket (column
; $52 pixel $5C, row 10, line $63 or below), the launch starts: launch_state is
; set, the ship is put away (two routines inside draw_ship, run with the death
; byte at 10 for their sake), and from then on the base moves up each pass (at
; the view's vertical scroll speed (scroll_down), or 4 lines a pass in page row
; 0), with the launch sound (sound_launch). Once part 0 has left the screen
; launch_state is set to $FF, which ends the play loop (play) in the game's end
; sequence.
;
; It all ran in the build's sessions: the launch, and the deliveries of part 0
; and of the ore tank. In the staged launch the rocket kept its place on the
; screen while the view climbed from page row 10 to row 0, its base moving up
; by the view's own scroll, and then rose 4 lines a pass off the top; the
; launch took about 390 passes (run in SkoolKit's simulator for the Animations
; page).
draw_rocket:
  XOR A                    ; No share in the load yet
  LD (rocket_parts_load),A ;
  LD A,(launch_state)     ; Launching: draw
  AND A                   ;
  JR NZ,draw_rocket_1     ;
  LD HL,(view_column)     ; Otherwise only with the rocket's place in view
  LD A,L                  ;
  CP $51                  ;
  JR Z,draw_rocket_0      ;
  CP $52                  ;
  RET NZ                  ;
draw_rocket_0:
  LD A,H                  ;
  CP $09                  ;
  JR Z,draw_rocket_1      ;
  CP $0A                  ;
  RET NZ                  ;
draw_rocket_1:
  LD A,(parts_built)       ; No parts: nothing; half their number counts in the
  AND A                    ; load
  RET Z                    ;
  INC A                    ;
  SRL A                    ;
  LD (rocket_parts_load),A ;
  LD IX,rocket_record     ; The drawing record; part 0 first
  LD (IX+$13),$00         ;
  LD A,(launch_state)     ;
  AND A                   ;
  JR Z,draw_rocket_3      ;
  LD A,(pass_counter)     ; Launching: the sound
  AND $03                 ;
  CALL sound_launch       ;
  CALL put_rocket_record  ; Move the base up with the view, or by 4 at the top
  LD A,(scroll_down)      ;
  NEG                     ;
  LD (IX+$09),A           ;
  LD A,(view_row)         ;
  AND A                   ;
  JR NZ,draw_rocket_2     ;
  LD (IX+$09),$FC         ;
draw_rocket_2:
  CALL move_object        ;
  LD L,(IX+$02)           ; Keep the base
  LD H,(IX+$01)           ;
  LD (rocket_x),HL        ;
  LD L,(IX+$05)           ;
  LD H,(IX+$04)           ;
  LD (rocket_y),HL        ;
draw_rocket_3:
  CALL put_rocket_record  ; Each part: the base...
  LD A,(IX+$13)             ; ...its flags and drawn position from the table...
  LD B,A                    ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,B                   ;
  LD HL,rocket_part_table   ;
  CALL add_a_to_hl          ;
  LD DE,rocket_record_flags ;
  LD BC,$0003               ;
  LDIR                      ;
  LD A,(HL)               ; ...plus its offset
  LD (IX+$07),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$09),A           ;
  CALL move_object        ;
  LD (IX+$07),$00         ;
  LD (IX+$09),$00         ;
  LD A,(IX+$13)           ; Part 6 is drawn with frame 8
  CP $06                  ;
  JR NZ,draw_rocket_4     ;
  LD (IX+$13),$08         ;
draw_rocket_4:
  CALL clear_sprite_pair  ; Rub out its old picture
  BIT 7,(IX+$10)          ;
  CALL NZ,prepare_erase   ;
  RES 6,(IX+$10)          ;
  EXX                     ;
  CALL place_object       ; Place, draw and colour it
  CALL draw_object_images ;
  CALL colour_sprite      ;
  LD IX,rocket_record       ; Its flags and drawn position back to the table
  LD A,(IX+$13)             ;
  CP $08                    ;
  JR NZ,draw_rocket_5       ;
  LD A,$06                  ;
  LD (IX+$13),A             ;
draw_rocket_5:
  LD B,A                    ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,B                   ;
  LD HL,rocket_part_table   ;
  CALL add_a_to_hl          ;
  LD DE,rocket_record_flags ;
  EX DE,HL                  ;
  LD BC,$0003               ;
  LDIR                      ;
  INC (IX+$13)            ; Next part
  LD A,(parts_built)      ;
  CP (IX+$13)             ;
  JP NZ,draw_rocket_3     ;
  LD A,(parts_built)      ; All eight?
  CP $08                  ;
  RET NZ                  ;
  LD A,(launch_state)     ; Launching...
  AND A                   ;
  JR Z,draw_rocket_6      ;
  CALL lift_jet            ; ...until part 0 is off the screen: then the game's
  LD A,(rocket_part_table) ; end
  AND $80                  ;
  RET NZ                   ;
  DEC A                    ;
  LD (launch_state),A      ;
  RET                      ;
draw_rocket_6:
  LD HL,(view_page_column) ; The ship in place in the rocket?
  LD A,L                   ;
  CP $52                   ;
  RET NZ                   ;
  LD A,H                   ;
  CP $5C                   ;
  RET NZ                   ;
  LD HL,(view_page_row)    ;
  LD A,L                   ;
  CP $0A                   ;
  RET NZ                   ;
  LD A,H                   ;
  CP $63                   ;
  RET C                    ;
  LD (launch_state),A     ; The launch begins; the death byte 10 for the next
  LD A,$0A                ; two calls
  LD (death_count),A      ;
  CALL draw_ship_image    ; Put the ship away
  CALL draw_trail         ;
  XOR A                      ; The death byte back to 0; the carried thing's
  LD (death_count),A         ; state $80 (carried_part); the ship's X velocity
  LD A,$80                   ; 0; a sound
  LD (carried_part_redraw),A ;
  CALL carried_part          ;
  LD HL,$0000                ;
  LD (ship_across_speed),HL  ;
  JP sound_zap               ;

; The launch
;
; 0 in play. Set non-zero when the ship docks with the finished rocket
; (draw_rocket): the rocket rises, nothing touches the ship (ship_contact,
; beam_hits_ship), the controls are off (ship_pass). $FF when the rocket has
; left the screen, which ends the play loop (play). Cleared for a new game
; (reset_for_life).
launch_state:
  DEFB $00

; The rocket's base position in X
;
; A word: the pixel, then the page column ($68 in column $52), where
; draw_rocket draws the rocket's parts from (put_rocket_record). It does not
; change.
rocket_x:
  DEFW $5268

; The rocket's base position in Y
;
; A word: the line, then the page row ($73 in row 10). draw_rocket moves it up
; each pass of the launch.
rocket_y:
  DEFW $0A73

; The rocket parts' table
;
; Eight entries of five bytes, one a part, in the order they are delivered: the
; flags and drawn x and line draw_rocket keeps between passes (copied to and
; from +$10 to +$12 of rocket_record), then the part's offset from the base
; (rocket_x, rocket_y), X and Y, as signed pixels. The parts stack up: offsets
; in Y 0, -9, -18, -34, -30 (part 4, 14 left), -30 (part 5, 22 right), -47,
; -63. drop_part reads the Y offset to say where a falling part stops. Bit 7 of
; part 0's flags (drawn) tells draw_rocket when the launched rocket has left
; the screen.
rocket_part_table:
  DEFB $00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$F7
  DEFB $00,$00,$00,$00,$EE
  DEFB $00,$00,$00,$00,$DE
  DEFB $00,$00,$00,$F2,$E2
  DEFB $00,$00,$00,$16,$E2
  DEFB $00,$00,$00,$00,$D1
  DEFB $00,$00,$00,$00,$C1

; Update routine for type $15, debris
;
; Debris -- what is left of a shot attacker (check_shot) and four pieces of the
; ship's wreck (wreck_ship) -- is harmless, makes the debris sound
; (sound_debris), and flies a fixed path: the velocity is taken each pass from
; debris_path, the step in +$15 (bits 0-5) going on by two. Bit 7 of +$15
; mirrors the path in X and bit 6 in Y, so four pieces of the wreck flying the
; four mirror images make a burst. At the path's end ($80) it is freed. It
; animates, and bounces off the landscape (the tail of update_attacker).
;
; IX The record
update_debris:
  XOR A                   ; Harmless; the sound
  LD (contact_effect),A   ;
  CALL sound_debris       ;
  LD A,(IX+$15)           ; The path's next step
  AND $3F                 ;
  LD HL,debris_path       ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  CP $80                  ;
  JR NZ,update_debris_0   ;
  LD (IX+$00),$FF         ; The end: freed
  JP draw_object          ;
update_debris_0:
  LD (IX+$07),A           ; Its velocity, and the next step
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$09),A           ;
  INC (IX+$15)            ;
  INC (IX+$15)            ;
  BIT 7,(IX+$15)          ; Mirrored in X
  JR Z,update_debris_1    ;
  LD A,(IX+$07)           ;
  NEG                     ;
  LD (IX+$07),A           ;
update_debris_1:
  BIT 6,(IX+$15)          ; Mirrored in Y; animate, move and draw
  JP Z,animate_move_draw  ;
  LD A,(IX+$09)           ;
  NEG                     ;
  LD (IX+$09),A           ;
  JP animate_move_draw    ;

; Let a carried rocket part go over the rocket's place
;
; Used by the routine at draw_ship.
;
; Called by draw_ship. The part the ship carries is kept at offset $1A of the
; player record (score; $FF none). When the ship is over the rocket's place
; (page column $52 pixel $5C) in page row 10 or above, the part is let go: a
; record is found for it (find_record_for_part) and filled as type $1E
; (update_rocket_part) at column $52 pixel $68 and the ship's height, falling
; with a Y velocity made from the ship's (its Y velocity word in bare_ship,
; negated) and a slight downward acceleration ($1B), to stop at row 10 at line
; $73 plus the part's Y offset (rocket_part_table). Part 6 is let go only when
; full -- when the ore loaded into it (a count in bare_ship, filled by
; load_ore) is 18 -- and falls with frame 8; its load and a byte of the player
; record are cleared. Then the ship carries nothing.
;
; It all ran in the build's sessions (the deliveries of part 0 and of the ore
; tank).
drop_part:
  LD A,(rocket_part_carried) ; Carrying nothing
  INC A                      ;
  RET Z                      ;
  LD A,(view_page_row)    ; Not over the rocket's place
  CP $0B                  ;
  RET NC                  ;
  LD A,(view_page_column) ;
  CP $52                  ;
  RET NZ                  ;
  LD A,(view_page_x)      ;
  CP $5C                  ;
  RET NZ                  ;
  LD A,(rocket_part_carried) ; Part 6 only when loaded with 18
  CP $06                     ;
  JR NZ,drop_part_0          ;
  LD A,(ore)                 ;
  CP $12                     ;
  RET NZ                     ;
  LD A,$08                   ;
drop_part_0:
  OR $80                   ; +$1F of the ship's record (part_being_fitted) gets
  LD (part_being_fitted),A ; $80 plus the part (or 8)
  CALL find_record_for_part ; A record, emptied
  CALL free_object          ;
  LD HL,(ship_down_speed) ; Its Y velocity, from the ship's
  LD A,L                  ;
  LD L,H                  ;
  LD H,A                  ;
  CALL negate_hl          ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  LD (IX+$10),$23         ; Type $1E; top speed 4, and it cannot be shot;
  LD (IX+$0F),$1B         ; falling
  LD (IX+$00),$1E         ;
  LD A,(rocket_part_carried) ; Frame the part's number; it stops at line $73
  LD (IX+$13),A              ; plus the part's offset
  LD B,A                     ;
  ADD A,A                    ;
  ADD A,A                    ;
  ADD A,B                    ;
  ADD A,$04                  ;
  LD HL,rocket_part_table    ;
  CALL add_a_to_hl           ;
  LD B,(HL)                  ;
  LD A,$73                   ;
  ADD A,B                    ;
  LD (IX+$16),A              ;
  LD A,(view_page_row)    ; At the ship's height, column $52 pixel $68
  LD (IX+$04),A           ;
  LD (IX+$01),$52         ;
  LD (IX+$02),$68         ;
  LD A,(view_page_y)      ;
  LD (IX+$05),A           ;
  LD (IX+$0D),$FF         ; Flashing white; it stops in row 10
  LD (IX+$15),$0A         ;
  LD A,(rocket_part_carried) ; Part 6: frame 8, emptied
  CP $06                     ;
  JR NZ,drop_part_1          ;
  LD (IX+$13),$08            ;
  XOR A                      ;
  LD (ore),A                 ;
  LD (ore_kept),A            ;
drop_part_1:
  LD A,$FF                   ; Nothing carried
  LD (rocket_part_carried),A ;
  RET                        ;

; Find a record for a falling rocket part
;
; Used by the routine at drop_part.
;
; The first free of the nine at object_records; if none, the first not drawn
; this pass; if all are drawn, the ninth.
;
; O:IX The record
find_record_for_part:
  LD B,$09                    ; A free one
  LD IX,object_records        ;
  LD DE,$0018                 ;
find_record_for_part_0:
  LD A,(IX+$00)               ;
  INC A                       ;
  RET Z                       ;
  ADD IX,DE                   ;
  DJNZ find_record_for_part_0 ;
  LD IX,object_records        ; One not on the screen
  LD B,$08                    ;
find_record_for_part_1:
  BIT 7,(IX+$10)              ;
  RET Z                       ;
  ADD IX,DE                   ;
  DJNZ find_record_for_part_1 ;
  RET                         ;

; Change an attacker's course now and then
;
; Used by the routines at update_tough_attacker and update_attacker.
;
; Counts +$15 down and, at 0, sets it again to 1-32 passes and jumps to the
; type's steering routine through the table steering_routines. Once bit 7 of
; +$16 is set (home_on_ship), there is no wait: the steering runs every pass.
; The routines: $14 and $16 home in on the ship (home_on_ship), $17 turns at it
; every 10-25 passes (turn_at_ship), $18 and $1A drift diagonally at random
; (drift_at_random), and $19 turns at it or stops (steer_tough_attacker).
;
; IX The attacker
change_course:
  BIT 7,(IX+$16)          ; Wait
  JR NZ,change_course_0   ;
  DEC (IX+$15)            ;
  RET NZ                  ;
  LD A,(random_b)         ; The next wait, 1-32 passes
  AND $1F                 ;
  INC A                   ;
  LD (IX+$15),A           ;
change_course_0:
  LD A,(IX+$00)           ; The type's steering
  SUB $13                 ;
  LD C,A                  ;
  LD HL,steering_routines ;
  CALL table_word         ;
  JP (HL)                 ;

; Steering: home in on the ship
;
; Used by the routine at steer_tough_attacker.
;
; Accelerates at a quarter of steer_towards's rate ($30) towards the ship's
; record (ship_record), and sets bit 7 of +$16 so that change_course runs this
; every pass from now on: types $14 and $16 home in for good after their first
; wait.
;
; IX The attacker
home_on_ship:
  PUSH IY                 ; At the ship
  LD IY,ship_record       ;
  CALL steer_towards      ;
  POP IY                  ;
  SET 7,(IX+$16)          ; Every pass from now on
  LD H,(IX+$0B)           ; A quarter of the acceleration
  LD L,(IX+$0C)           ;
  SRA H                   ;
  RR L                    ;
  SRA H                   ;
  RR L                    ;
  LD (IX+$0B),H           ;
  LD (IX+$0C),L           ;
  LD H,(IX+$0E)           ;
  LD L,(IX+$0F)           ;
  SRA H                   ;
  RR L                    ;
  SRA H                   ;
  RR L                    ;
  LD (IX+$0E),H           ;
  LD (IX+$0F),L           ;
  RET                     ;

; Steering: turn at the ship now and then
;
; Type $17: full acceleration towards the ship (steer_towards), and the next
; turn in 10-25 passes.
;
; IX The attacker
turn_at_ship:
  PUSH IY
  LD IY,ship_record
  CALL steer_towards
  POP IY
  LD A,(random_a)
  AND $0F
  ADD A,$0A
  LD (IX+$15),A
  RET

; Steering: drift diagonally at random
;
; Types $18 and $1A: the acceleration in each axis is $20 or -$20, from bits 0
; and 1 of the random number (random_a).
;
; IX The attacker
drift_at_random:
  LD A,(random_a)
  AND $01
  CALL random_direction
  LD (IX+$0B),D
  LD (IX+$0C),E
  LD A,(random_a)
  AND $02
  CALL random_direction
  LD (IX+$0E),D
  LD (IX+$0F),E
  RET

; Return -$20 or +$20
;
; Used by the routine at drift_at_random.
;
; For drift_at_random: DE = -$20 when the flags are zero, +$20 otherwise.
;
; O:DE The acceleration
random_direction:
  LD D,$FF
  LD E,$E0
  RET Z
  LD DE,$0020
  RET

; The debris path
;
; Sixteen steps of the path debris flies (update_debris), as X and Y velocities
; in pixels a pass (signed bytes): out and up, round and down, a loop of about
; 60 by 40 pixels. $80 ends it.
debris_path:
  DEFB $02,$F8            ; Sixteen steps: X, Y
  DEFB $02,$F8            ;
  DEFB $04,$F9            ;
  DEFB $06,$FA            ;
  DEFB $06,$FC            ;
  DEFB $08,$02            ;
  DEFB $04,$07            ;
  DEFB $FE,$07            ;
  DEFB $FA,$04            ;
  DEFB $FA,$FF            ;
  DEFB $FC,$FB            ;
  DEFB $FE,$F9            ;
  DEFB $04,$F7            ;
  DEFB $06,$FA            ;
  DEFB $06,$FC            ;
  DEFB $08,$FE            ;
  DEFB $80                ; The end

; The frame lists of types $15 to $1A
;
; The frame lists, one a type, which the table at offset 24 of object_handlers
; points into: a word for each frame, the address of its graphic (a byte of
; width, a byte of height in lines, then the bitmap, bottom row first; no
; mask). The lists overlap: type $15 starts at the first word (frames 0-5), $16
; at the seventh, $17 at the eleventh, $18 at the thirteenth, $19 at the
; seventeenth, $1A at the nineteenth; the list of type $1A runs on into the
; clouds' (cloud_frames).
attacker_frames:
  DEFW burst_small_graphic
  DEFW burst_small_b_graphic
  DEFW burst_big_graphic
  DEFW burst_big_b_graphic
  DEFW burst_big_graphic
  DEFW burst_big_b_graphic
  DEFW saucer_graphic
  DEFW saucer_b_graphic
  DEFW saucer_c_graphic
  DEFW saucer_d_graphic
  DEFW eye_pod_graphic
  DEFW eye_pod_b_graphic
  DEFW pillar_graphic
  DEFW pillar_c_graphic
  DEFW pillar_b_graphic
  DEFW pillar_c_graphic
  DEFW tough_attacker_graphic
  DEFW tough_attacker_b_graphic
  DEFW dome_graphic
  DEFW dome_b_graphic
  DEFW dome_c_graphic

; The clouds' frame list
;
; Type $13's two frames (the table at offset 24 of object_handlers points
; here): the small cloud, 4 bytes by 12 lines, and the big one, 5 by 24.
cloud_frames:
  DEFW cloud_small_graphic
  DEFW cloud_big_graphic

; The frame list of type $1C
;
; Eleven frames (update_small_object): 0-2 the bullet, 3 the refuel pick-up, 4,
; 5 and 7 small ones, 6 the fuel thief, 8 the ship icon (the graphic the panel
; shows the lives with, inside bare_ship), 9 the ring (shield_ring), 10 a
; graphic inside shot_kind_flags.
small_thing_frames:
  DEFW bullet_graphic
  DEFW bullet_b_graphic
  DEFW bullet_c_graphic
  DEFW refuel_pick_up_graphic
  DEFW pellet_graphic
  DEFW pellet_b_graphic
  DEFW fuel_thief_graphic
  DEFW pellet_b_graphic
  DEFW score_bitmap
  DEFW shield_ring
  DEFW dash_graphic

; The ring, the shield pick-up
;
; Frame 9 of type $1C (small_thing_frames): a byte of width (2), a byte of
; height (16 lines), then the bitmap, bottom row first, no mask. Touching it
; raises the shield (ship_contact).
shield_ring:
  DEFB $02,$10            ; Width, height; then the bitmap
  DEFB $07,$E0            ;
  DEFB $18,$18            ;
  DEFB $20,$04            ;
  DEFB $40,$02            ;
  DEFB $41,$82            ;
  DEFB $83,$C1            ;
  DEFB $84,$21            ;
  DEFB $8E,$71            ;
  DEFB $9E,$79            ;
  DEFB $BE,$7D            ;
  DEFB $9C,$39            ;
  DEFB $4F,$F0            ;
  DEFB $40,$02            ;
  DEFB $20,$04            ;
  DEFB $18,$18            ;
  DEFB $07,$E0            ;

; The frame list of type $1D
;
; Eight frames of 2 bytes by 16 lines (update_rammer).
rammer_frames:
  DEFW rock_graphic
  DEFW rock_b_graphic
  DEFW rock_c_graphic
  DEFW rock_d_graphic
  DEFW rock_e_graphic
  DEFW rock_f_graphic
  DEFW comet_graphic
  DEFW comet_b_graphic

; The wave table
;
; Ten entries of four bytes, read by start_attacker (via spawn_tick): type,
; flags (+$10), frame byte (+$13) and frame range (+$14) for a new attacker of
; each kind. 0 type $1A; 1 and 5 clouds ($13, flags $21: top speed 2 and shot
; already, so they cannot be shot); 2 $17; 3 $18; 4 $19; 6 $14; 7 $16; 8 and 9
; type $1D. wave_counter says which comes next.
wave_table:
  DEFB $1A,$01,$50,$02
  DEFB $13,$21,$00,$00
  DEFB $17,$02,$F0,$01
  DEFB $18,$01,$F0,$03
  DEFB $19,$01,$00,$00
  DEFB $13,$21,$00,$00
  DEFB $14,$02,$F0,$03
  DEFB $16,$02,$F0,$03
  DEFB $1D,$06,$50,$05
  DEFB $1D,$06,$56,$67

; The wave counter
;
; The entry of wave_table that start_attacker spawns next: stepped from 0 to 5,
; or to 7 when two flags in the player record (score) are both set.
wave_counter:
  DEFB $00

; The attackers' colours
;
; Eight colour bytes for +$D (two inks, flashing), one picked at random for
; each new attacker by start_attacker; only the first four at middle heights.
attacker_colours:
  DEFB $FF,$EE,$FD,$DD,$CC,$BB,$66,$FF

; The steering routines by type
;
; For change_course, a word each from type $13: update_cloud (unused),
; home_on_ship, update_debris (unused), home_on_ship, turn_at_ship,
; drift_at_random, steer_tough_attacker, drift_at_random.
steering_routines:
  DEFW update_cloud
  DEFW home_on_ship
  DEFW update_debris
  DEFW home_on_ship
  DEFW turn_at_ship
  DEFW drift_at_random
  DEFW steer_tough_attacker
  DEFW drift_at_random

; Stop an object
;
; Used by the routine at steer_tough_attacker.
;
; Clears its acceleration (clear_screen_fields) and its velocity.
;
; IX The object
stop_object:
  CALL clear_screen_fields
  LD (IX+$07),A
  LD (IX+$08),A
  LD (IX+$09),A
  LD (IX+$0A),A
  RET

; Steering for type $19: turn at the ship, or stop
;
; The next change in 16-79 passes. One time in four it stops dead
; (stop_object); otherwise it homes in on the ship at the quarter rate
; (home_on_ship), but only until the next change (bit 7 of +$16 is cleared
; again).
;
; IX The attacker
steer_tough_attacker:
  LD A,(random_a)
  AND $3F
  ADD A,$10
  LD (IX+$15),A
  LD A,R
  AND $03
  JP Z,stop_object
  CALL home_on_ship
  RES 7,(IX+$16)
  RET

; The object records
;
; Nine records of 24 bytes, updated by update_objects; $FF in the first byte
; marks a free one. They are shared out: attackers from the wave table go in
; any (spawn_tick), what the attackers drop in records 4-6
; (ground_object_fires), clouds in records 7 and 8 (spawn_cloud); the homing
; missiles look for targets in records 0-6 (find_missile_target); the ship's
; wreck fills all nine (wreck_ship), and a falling rocket part takes any
; (find_record_for_part).
;
; The record: +0 the type ($FF free; $13-$1E, which picks the update routine in
; object_handlers and the frame list). +1 +2 +3 X: the page column (0-$5F,
; wrapping), the pixel in it ($08-$E7, a page being 224 wide) and a fraction.
; +4 +5 +6 Y: the page row, the pixel in it ($18-$B7, 160 lines) and a
; fraction; Y grows downwards. +7 +8 the X velocity and +9 +A the Y velocity,
; signed 8.8 pixels a pass, high byte first (move_object adds them). +B +C the
; X acceleration and +E +F the Y acceleration, the same form (accelerate). +D
; two colours: the low nibble on odd passes and the high nibble on even ones
; (bits 0-2 or 4-6 the ink, bit 3 or 7 BRIGHT), so a sprite can flash
; (prepare_draw).
;
; +10 flags: bits 0-2 the top speed less one (accelerate), bit 3 skip one
; placing (place_object), bit 4 to be freed, bit 5 shot, bit 6 straddles the
; left edge of the view, bit 7 drawn. +11 +12 where it was drawn: screen x and
; bottom line. +13 the frame: the low nibble the frame in the type's list, bits
; 4-5 a count down to the next frame and bits 6-7 what it is reloaded with
; (animate_object). +14 the frame range: the first frame in the high nibble,
; the last in the low. +15 +16 the type's own: a timer, a path step, a hit
; count. +17 the high nibble a count down after touching the ship
; (contact_damage), or on a homing missile the target's record number ($F none,
; update_missile); bit 3 exploding and bits 0-2 the explosion's frame
; (prepare_draw).
object_records:
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 0
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 1
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 2
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 3
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
drop_records:
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 4: from here, three for what
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; the attackers drop
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 5
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 6
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
cloud_records:
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 7: from here, two for clouds
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00 ; Record 8
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;

; The rocket parts' drawing record
;
; One object record (object_records), type $1E, that draw_rocket draws every
; rocket part with in turn: the base position (rocket_x, rocket_y) moved by the
; part's offset, and the part's flags and drawn position (+$10 to +$12, the
; third row here) copied in from rocket_part_table and back out after.
rocket_record:
  DEFB $1E,$52,$00,$00,$0A,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$FF,$00,$00
rocket_record_flags:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; +$10 to +$17

; The pass counter
;
; A word, counted up once a pass by ship_pass (and by the menu's loop). Its low
; bits time things that happen every other pass, every fourth or every eighth
; (the shield, the engine sounds, type $19's hits), and bit 0 picks the colour
; of a flashing sprite (prepare_draw).
pass_counter:
  DEFW $0000

; Start the game
;
; Used by the routine at game_entry.
;
; Reached from game_entry after the game block is decoded (game_entry). The
; stack goes into the printer buffer (below loader_entry), the border black;
; build_shift_tables builds the shift tables, and the game's outer loop takes
; over (game). Interrupts stay off throughout.
start:
  LD SP,loader_entry      ; The stack goes below loader_entry, in the printer
                          ; buffer
  XOR A                   ; A black border
  OUT ($FE),A             ;
  CALL build_shift_tables ; Build the shift tables; on to the outer loop
  JP game                 ;

; Set every attribute to white ink on black
;
; Used by the routines at menu and game.
;
; All 768 attribute bytes to $47 (bright white on black).
clear_attributes:
  LD HL,$5800
clear_attributes_0:
  LD (HL),$47
  INC HL
  LD A,H
  CP $5B
  JR NZ,clear_attributes_0
  RET

; Clear the screen's pixels
;
; Used by the routines at menu and game.
;
; All 6144 bytes of the bitmap to 0.
clear_pixels:
  LD HL,$4000
clear_pixels_0:
  LD (HL),$00
  INC HL
  LD A,H
  CP $58
  JR NZ,clear_pixels_0
  RET

; AND every byte of the objects-in-the-world table with E
;
; Used by the routines at planet_secured and new_game.
;
; map_object_kinds holds one byte for each object lying in the world, in
; map-column order: bits 0-5 its kind, bit 7 set once it is gone for player 1
; and bit 6 for player 2 (ship_hits_ground_objects, beam_hits_ground_object). A
; new game passes $3F (new_game), clearing bits 6 and 7 of each, so everything
; is back. planet_secured passes $7F for player 1 or $BF for player 2 at the
; end of a planet, bringing back only that player's objects.
;
; E The mask
mask_world_objects:
  LD BC,$0122
  LD HL,map_object_kinds
mask_world_objects_0:
  LD A,(HL)
  AND E
  LD (HL),A
  INC HL
  DEC BC
  LD A,B
  OR C
  JR NZ,mask_world_objects_0
  RET

; Play a tune until a key is pressed
;
; Used by the routine at menu.
;
; The title tune: plays the notes from DE until the $FF at the end, but stops
; at once, returning NZ, when any key is down (read_keyboard with every
; half-row selected).
;
;   DE The tune
; O:F NZ if a key stopped it
play_tune_until_key:
  XOR A
  CALL read_keyboard
  RET NZ
  LD A,(DE)
  CP $FF
  JR Z,play_tune_0
  CALL play_note
  JR play_tune_until_key

; Play a tune to the end
;
; Used by the routines at menu, play, game_over and level_done.
;
; The notes from DE to the $FF at the end (play_note).
;
; DE The tune
play_tune:
  LD A,(DE)
  CP $FF
  JR Z,play_tune_0
  CALL play_note
  JR play_tune
; This entry point is used by the routine at play_tune_until_key.
play_tune_0:
  RET

; Play one note on the beeper
;
; Used by the routines at play_tune_until_key and play_tune.
;
; A note is a byte: bits 0-5 the note (0 a rest), bits 6-7 its length less one,
; in units. A note's entry in note_table gives the half-period as a delay count
; and the number of cycles in one unit; the beeper is toggled between $00 and
; $10 (black border throughout) for length times that many cycles. A rest waits
; about 0.13 seconds a unit.
;
;   A The note
;   DE The tune, at the note
; O:DE The next note
play_note:
  AND $3F                 ; The note's entry
  JR Z,play_note_5        ;
  LD L,A                  ;
  LD H,$00                ;
  ADD HL,HL               ;
  CALL add_a_to_hl        ;
  LD BC,note_table        ;
  ADD HL,BC               ;
  LD B,(HL)               ; Delay count (B, C) and cycles in a unit
  INC HL                  ;
  LD C,(HL)               ;
  INC HL                  ;
  LD L,(HL)               ;
  LD H,$00                ;
  LD A,(DE)               ; Times the length
  RLCA                    ;
  RLCA                    ;
  AND $03                 ;
  INC A                   ;
  PUSH DE                 ;
  LD E,L                  ;
  LD D,H                  ;
play_note_0:
  DEC A                   ;
  JR Z,play_note_1        ;
  ADD HL,DE               ;
  JR play_note_0          ;
play_note_1:
  POP DE                  ;
play_note_2:
  PUSH BC                 ; Half a cycle low...
  XOR A                   ;
  OUT ($FE),A             ;
play_note_3:
  DJNZ play_note_3        ;
  DEC C                   ;
  JR NZ,play_note_3       ;
  POP BC                  ; ...and half high
  PUSH BC                 ;
  LD A,$10                ;
  OUT ($FE),A             ;
play_note_4:
  DJNZ play_note_4        ;
  DEC C                   ;
  JR NZ,play_note_4       ;
  POP BC                  ; Until the cycles are done
  DEC HL                  ;
  LD A,H                  ;
  OR L                    ;
  JR NZ,play_note_2       ;
  INC DE                  ;
  RET                     ;
play_note_5:
  LD A,(DE)               ; A rest: about 0.13 seconds a unit
  INC DE                  ;
  RLCA                    ;
  RLCA                    ;
  AND $03                 ;
  INC A                   ;
  LD L,A                  ;
  LD BC,$430B             ;
play_note_6:
  PUSH BC                 ;
play_note_7:
  DEC BC                  ;
  LD A,B                  ;
  OR C                    ;
  JR NZ,play_note_7       ;
  POP BC                  ;
  DEC L                   ;
  JR NZ,play_note_6       ;
  RET                     ;

; The notes
;
; 61 entries of three bytes (play_note): the half-period's delay count as B
; then C (the DJNZ count, then the number of passes round it, so about
; 256*(C-1)+B steps of 13 T-states), then the cycles in one unit of length.
; Entry 0 is unused (note 0 is a rest). Entries 1-60 rise a semitone each in
; loop counts -- 256*(C-1)+B is equal-tempered to within 7 cents -- except that
; entry 18 has entry 17's delay (only its cycle count differs), so note 18
; sounds as note 17. Each half-wave also spends a fixed time outside the loops,
; which bends the scale played from 37 cents sharp at entry 1 to 34 cents flat
; at entry 60, from about 53 Hz up to 1524 Hz (measured in SkoolKit's simulator
; for the Sounds page). The cycle counts rise with the pitch so that a unit
; lasts about the same time.
note_table:
  DEFB $00,$00,$00
  DEFB $F4,$0A,$08
  DEFB $65,$0A,$09
  DEFB $DE,$09,$09
  DEFB $5E,$09,$0A
  DEFB $E7,$08,$0A
  DEFB $75,$08,$0B
  DEFB $0A,$08,$0C
  DEFB $A5,$07,$0C
  DEFB $45,$07,$0D
  DEFB $EB,$06,$0E
  DEFB $96,$06,$0F
  DEFB $46,$06,$0F
  DEFB $FA,$05,$10
  DEFB $B3,$05,$11
  DEFB $6F,$05,$12
  DEFB $2F,$05,$13
  DEFB $F3,$04,$15
  DEFB $F3,$04,$16
  DEFB $85,$04,$17
  DEFB $52,$04,$19
  DEFB $23,$04,$1A
  DEFB $F6,$03,$1C
  DEFB $CB,$03,$1D
  DEFB $A3,$03,$1F
  DEFB $7D,$03,$21
  DEFB $59,$03,$23
  DEFB $38,$03,$25
  DEFB $18,$03,$27
  DEFB $FA,$02,$29
  DEFB $DD,$02,$2C
  DEFB $C2,$02,$2E
  DEFB $A9,$02,$31
  DEFB $91,$02,$34
  DEFB $7B,$02,$37
  DEFB $66,$02,$3A
  DEFB $51,$02,$3E
  DEFB $3F,$02,$41
  DEFB $2D,$02,$45
  DEFB $1C,$02,$49
  DEFB $0C,$02,$4E
  DEFB $FD,$01,$52
  DEFB $EF,$01,$57
  DEFB $E2,$01,$5D
  DEFB $D5,$01,$62
  DEFB $C9,$01,$68
  DEFB $BD,$01,$6E
  DEFB $B3,$01,$75
  DEFB $A9,$01,$7B
  DEFB $9F,$01,$83
  DEFB $96,$01,$8B
  DEFB $8E,$01,$93
  DEFB $86,$01,$9C
  DEFB $7E,$01,$A5
  DEFB $77,$01,$AF
  DEFB $71,$01,$B9
  DEFB $6A,$01,$C4
  DEFB $64,$01,$D0
  DEFB $5F,$01,$DC
  DEFB $59,$01,$E9
  DEFB $54,$01,$F7

; Read the keyboard
;
; Used by the routines at play_tune_until_key and read_control_method.
;
; Reads the half-rows A selects; returns the keys down as set bits 0-4. Every
; keyboard read in the game first writes A to port $FD; on a 48K Spectrum
; nothing answers that port, and why it is done is not yet worked out. On a
; 128K the same OUT is a write to the paging port whenever A's bit 7 is clear:
; the menu's test for B (menu_keys) writes $7F, which pages RAM bank 7 in at
; the top of memory and locks the paging, and the game then crashes on the
; menu's first pass (watched in SkoolKit's 128K memory for the reference
; pages). It must be run in 48 mode.
;
;   A The half-rows to read (0 bits select)
; O:A The keys down, a bit each
; O:F NZ if any
read_keyboard:
  OUT ($FD),A
  IN A,($FE)
  CPL
  AND $1F
  RET

; A random byte
;
; The first of the two random bytes the game reads (each attacker's choices,
; the beams, the drops), stepped once a pass by random: the byte at
; random_pointer's pointer and the controls (controls_now) are added in. The
; routines that want more randomness also read R.
random_a:
  DEFB $17

; A second random byte
;
; Stepped with random_a by random, from the complement of the same byte.
random_b:
  DEFB $8E

; The random number's pointer
;
; A pointer, high byte first, that random steps through memory once a pass (its
; high byte ANDed with $AF), so that the bytes it reads, code and data, stir
; the random bytes.
random_pointer:
  DEFB $A0,$00

; Add A to HL
;
; Used by the routines at deal_places, planet_secured, move_method_arrow,
; cycle_method, fire_from, start_at_edge, wreck_ship, test_player_shots,
; find_missile_target, move_missile, update_small_object, draw_rocket,
; update_debris, drop_part, play_note, draw_beams, clip_sprite_rows,
; dissolve_sprite, load_ore, toggle_carried_part, collect_pickup,
; map_square_bit, addon_mask_and_draw, draw_ship_image, side_jet,
; start_landscape, spawn_column_objects, spawn_objects_from_list,
; reset_for_life and clear_player_record.
;
; Used everywhere to index a table.
add_a_to_hl:
  ADD A,L
  LD L,A
  LD A,H
  ADC A,$00
  LD H,A
  RET

; The beams' pattern, colour and timer
;
; For draw_beams: the bit pattern every beam is drawn with this pass (0 while
; they are off), the colour (bright, ink 1-7), the passes left before the next
; change, and (the last byte) whether a beam was drawn this pass, for the
; sound.
beam_state:
  DEFB $00,$00,$01,$00

; Draw the flickering beams, and test them against the ship
;
; Used by the routine at scroll_landscape.
;
; Called by scroll_landscape, not while map_rebuild is set. A beam crosses a
; screen whose landscape piece is $A6, at a fixed height in it, unless the
; thing that makes it has been shot: a bit in the player's record, found by
; map_square_bit and tested by a BIT instruction this routine writes into
; itself (the third after the CALL to map_square_bit). Each pass the old beams
; are rubbed out (erase_flicker_beam) and drawn again in the four screens
; around the view (the four pieces in map_window_pointer, in the order 0, 2, 1,
; 3; bit 0 the page row below, bit 1 the column to the right). A beam is a
; one-line row of bytes ORed onto the screen in the current pattern, rotated to
; follow the fine scroll, its characters coloured (fill_attributes) and its
; extent kept in flicker_beam_records for rubbing out (draw_flicker_beam draws
; it, place_flicker_beam places it).
;
; The pattern changes when its timer runs out: half the time one of the eight
; at flicker_beam_patterns in a random bright colour, for 1-32 passes;
; otherwise none, for 1-16. A beam that touches the ship's box (40 by 16)
; damages it (beam_hits_ship), then not again for 16 passes.
;
; All of it, the pattern changes included, ran in the build's sessions.
draw_beams:
  LD A,(map_rebuild)      ; Not while map_rebuild is set
  AND A                   ;
  RET NZ                  ;
  LD A,(beam_grace)       ; Counting down after a hit: no box
  AND A                   ;
  JR Z,draw_beams_0       ;
  DEC A                   ;
  LD (beam_grace),A       ;
  JR draw_beams_1         ;
draw_beams_0:
  LD A,(ship_line)           ; The ship's box at collision_box
  ADD A,$08                  ;
  LD H,A                     ;
  LD L,$64                   ;
  LD D,$10                   ;
  LD E,$28                   ;
  LD (collision_box),HL      ;
  LD (collision_box_size),DE ;
draw_beams_1:
  LD B,$04                    ; No beam piece in view and none drawn: done
  LD IY,flicker_beam_records  ;
  LD HL,map_window_tile_bytes ;
draw_beams_2:
  LD A,(HL)                   ;
  CP $A6                      ;
  JR Z,draw_beams_3           ;
  INC HL                      ;
  LD A,(IY+$00)               ;
  AND A                       ;
  JR NZ,draw_beams_3          ;
  INC IY                      ;
  INC IY                      ;
  INC IY                      ;
  INC IY                      ;
  DJNZ draw_beams_2           ;
  RET                         ;
draw_beams_3:
  LD IX,beam_state        ; The timer
  DEC (IX+$02)            ;
  JR NZ,draw_beams_7      ;
  LD (IX+$00),$00         ; Half the time, no beam
  LD A,(random_a)         ;
  BIT 0,A                 ;
  JR Z,draw_beams_5       ;
  LD A,(random_b)         ; A random bright colour
  AND $07                 ;
  JR NZ,draw_beams_4      ;
  LD A,$05                ;
draw_beams_4:
  LD B,A                  ;
  ADD A,$40               ;
  LD (IX+$01),A           ;
  LD A,(random_a)             ; A random pattern, for up to 32 passes
  AND $07                     ;
  XOR B                       ;
  LD HL,flicker_beam_patterns ;
  CALL add_a_to_hl            ;
  LD A,(HL)                   ;
  LD (IX+$00),A               ;
  LD B,$1F                    ;
  JR draw_beams_6             ;
draw_beams_5:
  LD B,$0F                ; Or none, for up to 16
draw_beams_6:
  LD A,(random_b)         ; The timer
  CPL                     ;
  AND B                   ;
  INC A                   ;
  LD (IX+$02),A           ;
draw_beams_7:
  XOR A                      ; None drawn yet; the four screens
  LD ($9BD9),A               ;
  LD IY,flicker_beam_records ;
draw_beams_8:
  LD (row_count),A        ; Rub out the beam drawn there last pass
  LD A,(IY+$00)           ;
  AND A                   ;
  JR Z,draw_beams_9       ;
  LD L,(IY+$00)           ;
  LD H,(IY+$01)           ;
  LD B,(IY+$02)           ;
  LD C,(IY+$03)           ;
  CALL erase_flicker_beam ;
draw_beams_9:
  LD (IY+$00),$00         ; None now
  LD A,(row_count)        ; This screen's row and column
  LD HL,(view_column)     ;
  BIT 0,A                 ;
  JR Z,draw_beams_10      ;
  INC H                   ;
draw_beams_10:
  BIT 1,A                 ;
  JR Z,draw_beams_11      ;
  INC L                   ;
draw_beams_11:
  EX DE,HL                ;
  CALL map_square_bit     ; Its beam-maker shot? (the BIT is written here)
  LD A,B                  ;
  ADD A,$46               ;
  LD ($9C9F),A            ;
  BIT 0,(HL)              ;
  JR NZ,draw_beams_12     ;
  LD HL,map_window_tile_bytes ; A beam piece, and a pattern
  LD A,(row_count)            ;
  CALL add_a_to_hl            ;
  LD A,(HL)                   ;
  CP $A6                      ;
  JR NZ,draw_beams_12         ;
  LD A,(IX+$00)               ;
  AND A                       ;
  JR Z,draw_beams_12          ;
  LD HL,(view_x)          ; Where it is on the screen; none in view
  LD DE,(origin_x)        ;
  CALL place_flicker_beam ;
  LD A,B                  ;
  AND A                   ;
  JR Z,draw_beams_12      ;
  CALL draw_flicker_beam  ; Draw it
  LD HL,(sprite_screen_pos) ; Its width, for the colour and the test
  LD A,(IY+$02)             ;
  LD (shot_colour_patch),A  ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,A                   ;
  LD D,A                    ;
  LD A,(beam_grace)       ; Not counting down: test it against the ship
  AND A                   ;
  PUSH HL                 ;
  CALL Z,beam_hits_ship   ;
  POP HL                  ;
  LD A,(IX+$01)           ; Colour it; a beam drawn
  LD ($B627),A            ;
  LD DE,shot_colour_patch ;
  CALL fill_attributes    ;
  OR $01                  ;
  LD ($9BD9),A            ;
draw_beams_12:
  INC IY                  ; Next screen
  INC IY                  ;
  INC IY                  ;
  INC IY                  ;
  LD A,(row_count)        ;
  CP $03                  ;
  JR Z,draw_beams_13      ;
  XOR $02                 ;
  JP NZ,draw_beams_8      ;
  INC A                   ;
  JP draw_beams_8         ;
draw_beams_13:
  LD A,($9BD9)            ; The sound when any was drawn (sound_beams)
  AND A                   ;
  RET Z                   ;
  JP sound_beams          ;

; Test a beam against the ship
;
; Used by the routine at draw_beams.
;
; Not during the launch. The beam, D pixels wide and one line high at HL, is
; tested against the ship's box at collision_box (test_collision_box); a touch
; starts 16 passes of grace (beam_grace) and damages the ship (contact_damage).
;
; HL The beam's position
; D Its width in pixels
beam_hits_ship:
  LD A,(launch_state)
  AND A
  RET NZ
  LD E,$01
  CALL test_collision_box
  LD A,(scratch_count)
  AND A
  RET Z
  LD A,$10
  LD (beam_grace),A
  JP damage_ship

; Rub out a beam
;
; Used by the routine at draw_beams.
;
; ANDs B bytes along the screen line at HL with the inverse of the pattern C.
;
; HL The screen address
; B The length in bytes
; C The pattern
erase_flicker_beam:
  LD A,C
  CPL
  LD C,A
erase_flicker_beam_0:
  LD A,C
  AND (HL)
  LD (HL),A
  INC L
  DJNZ erase_flicker_beam_0
  RET

; The beams' grace count
;
; Counts down the passes after a beam has hurt the ship (beam_hits_ship), set
; to 16; while it is not zero draw_beams tests no beam against the ship.
; Cleared for a new ship (reset_for_life).
beam_grace:
  DEFB $00

; Draw a beam
;
; Used by the routine at draw_beams.
;
; Called by draw_beams. Records the beam in the four bytes at IY
; (flicker_beam_records) -- HL the screen address, B the length in bytes, C the
; pattern -- and ORs C into B bytes along the screen line from HL.
draw_flicker_beam:
  LD (IY+$00),L            ; Record it; OR the pattern along the line
  LD (IY+$01),H            ;
  LD (IY+$02),B            ;
  LD (IY+$03),C            ;
draw_flicker_beam_0:
  LD A,C                   ;
  OR (HL)                  ;
  LD (HL),A                ;
  INC L                    ;
  DJNZ draw_flicker_beam_0 ;
  RET                      ;

; Where the beams were drawn
;
; Four records of four bytes, one for each page around the view, in
; draw_beams's order (the view's page, the one to the right, below, below
; right): the screen address (low byte first), the length in bytes and the
; pattern of the beam drawn there (draw_flicker_beam), for rubbing it out next
; pass (erase_flicker_beam). A zero first byte means none.
flicker_beam_records:
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00

; Work out where a beam lies on the screen
;
; Used by the routine at draw_beams.
;
; Called by draw_beams. From the view's pixel position in its page (HL, from
; view_x), the origins (DE, from origin_x) and the page being drawn
; (draw_beams's quarter, in row_count: bit 0 the page below, bit 1 the page to
; the right), it works out the beam's x and line, kept in moved_x, and its
; length in bytes in B (0: not in view). The beam's pattern (from beam_state)
; is rotated right by the fine scroll (origin_x AND 7) into C, and HL becomes
; the beam's screen address (pixel_to_screen). A beam lies at page y $21, 9
; lines below the top of its tile-$A6 cell.
place_flicker_beam:
  LD B,$00                   ; Its line and x; its length; the pattern rotated
  LD A,(row_count)           ; with the scroll; its screen address
  BIT 0,A                    ;
  LD A,H                     ;
  JR NZ,place_flicker_beam_0 ;
  CP $21                     ;
  RET NC                     ;
  LD A,$81                   ;
  JR place_flicker_beam_1    ;
place_flicker_beam_0:
  CP $21                     ;
  RET C                      ;
  LD A,$21                   ;
place_flicker_beam_1:
  ADD A,D                    ;
  LD D,A                     ;
  LD A,(row_count)           ;
  BIT 1,A                    ;
  JR NZ,place_flicker_beam_2 ;
  LD A,$E8                   ;
  SUB L                      ;
  LD E,$08                   ;
  JR place_flicker_beam_3    ;
place_flicker_beam_2:
  LD A,$E8                   ;
  ADD A,E                    ;
  LD E,A                     ;
  LD A,L                     ;
  SUB $08                    ;
place_flicker_beam_3:
  LD (sprite_screen_pos),DE  ;
  LD B,A                     ;
  AND $07                    ;
  JR Z,place_flicker_beam_4  ;
  LD A,B                     ;
  ADD A,$08                  ;
  LD B,A                     ;
place_flicker_beam_4:
  SRL B                      ;
  SRL B                      ;
  SRL B                      ;
  RET Z                      ;
  LD C,(IX+$00)              ;
  LD A,(origin_x)            ;
  AND $07                    ;
  JR Z,place_flicker_beam_6  ;
place_flicker_beam_5:
  RRC C                      ;
  DEC A                      ;
  JR NZ,place_flicker_beam_5 ;
place_flicker_beam_6:
  LD HL,(sprite_screen_pos)  ;
  JP pixel_to_screen         ;

; The beams' patterns
;
; Eight bit patterns, one picked at random whenever the beams change
; (draw_beams).
flicker_beam_patterns:
  DEFB $E7,$E3,$C1,$81,$C3,$4D,$FF,$F7

; Test a box against another
;
; Used by the routines at ship_contact, test_player_shots,
; ship_hits_ground_objects, test_collision_box and hit_block.
;
; The box at IX: x, bottom line, width, height. The other: x in L, bottom line
; in H, width D, height E. They overlap when their x ranges and their line
; ranges do (x_overlap, y_overlap). scratch_count is 1 if so, 0 if not. Used
; for the ship's touches (ship_contact), the shots (test_player_shots) and the
; landscape.
;
; IX The box
; L The other's x
; H The other's bottom line
; D The other's width
; E The other's height
boxes_overlap:
  CALL x_overlap
  LD A,(scratch_count)
  AND A
  RET Z
  JR y_overlap

; Test two boxes' x ranges
;
; Used by the routine at boxes_overlap.
;
; They overlap when the box's x is within D after L, or L is within the box's
; width after the box's x. scratch_count is 1 if so.
x_overlap:
  XOR A                   ; The box starts in the other's width
  LD (scratch_count),A    ;
  LD A,(IX+$00)           ;
  SUB L                   ;
  JR C,x_overlap_0        ;
  CP D                    ;
  JR C,overlap_found      ;
x_overlap_0:
  LD A,(IX+$00)           ; Or the other starts in the box's
  CP L                    ;
  RET NC                  ;
  ADD A,(IX+$02)          ;
  CP L                    ;
  RET C                   ;
; This entry point is used by the routines at y_overlap, test_landscape_piece
; and test_slope_reversed.
overlap_found:
  LD A,$01                ; Overlap
  LD (scratch_count),A    ;
  RET                     ;

; Test two boxes' line ranges
;
; Used by the routine at boxes_overlap.
;
; A box spans its height upwards from its bottom line. They overlap when the
; other's bottom line is within the box's height above the box's, or the box's
; within E above the other's. scratch_count is 1 if so.
y_overlap:
  XOR A
  LD (scratch_count),A
  LD A,(IX+$01)
  SUB H
  CP (IX+$03)
  JR C,overlap_found
  LD A,H
  SUB (IX+$01)
  CP E
  RET NC
  JP overlap_found

; Test an object against a sloping landscape edge
;
; Used by the routines at test_landscape_piece, test_slope_swapped and
; test_slope_upside_down.
;
; Used by the landscape tests (test_landscape_piece and those after it). IX is
; a landscape piece's collision record: +0 and +2 its x range, +1 and +3 its
; bottom and top lines, +4 flags (bits 0-2 the steepness, bit 7 which way up).
; The object's box: x in L, bottom line in H, width D, height E
; (test_landscape_piece). When the line ranges meet, works out how far the
; object reaches into the slope at that line: B is the depth of that line under
; the slope's high edge, scaled by the steepness (2 as it is, 3 halved, others
; doubled). scratch_count is 1 if the object is in the piece's x range.
;
;   IX The piece's collision record
; O:B The slope's offset at the object's line
test_slope:
  LD A,H                  ; Above the piece: nothing
  CP (IX+$03)             ;
  RET C                   ;
  BIT 7,(IX+$04)          ; Which way up
  JR NZ,test_slope_0      ;
  CP (IX+$01)             ; The object's lowest line within the piece
  JR C,test_slope_1       ;
  SUB E                   ;
  CP (IX+$01)             ;
  RET NC                  ;
  LD H,(IX+$01)           ;
  JR test_slope_1         ;
test_slope_0:
  SUB E                   ; Upside down: its highest
  LD H,A                  ;
  CP (IX+$01)             ;
  RET NC                  ;
  CP (IX+$03)             ;
  JR NC,test_slope_1      ;
  LD H,(IX+$03)           ;
test_slope_1:
  LD A,L                  ; Within the piece's x range?
  CP (IX+$02)             ;
  JR Z,test_slope_2       ;
  RET NC                  ;
test_slope_2:
  LD A,$01                ; Yes
  LD (scratch_count),A    ;
  LD A,(IX+$01)           ; The depth, scaled by the steepness
  SUB H                   ;
  LD B,A                  ;
  LD A,C                  ;
  CP $02                  ;
  RET Z                   ;
  SRL B                   ;
  CP $03                  ;
  RET Z                   ;
  SLA B                   ;
  SLA B                   ;
  RET                     ;

; Test an object against one landscape piece
;
; Used by the routine at hit_slope.
;
; Called by hit_slope for each of a landscape piece's collision records. The
; object's box is in box_x-box_width, its x, bottom line, height and width
; (made by place_collision_box). Bits 6-7 of the record's flags pick one of
; four slope shapes -- the same slope mirrored left to right and upside down
; (test_slope_reversed, test_slope_upside_down, test_slope_both_ways); which
; way each faces is not yet worked out. A touch leaves scratch_count 1 and the
; depth into the slope in touch_depth.
;
; All four shapes ran in the build's sessions; the RET after the four tests
; cannot be reached.
;
; IX The collision record
test_landscape_piece:
  XOR A                   ; The object's box
  LD (scratch_count),A    ;
  LD HL,(box_x)           ;
  LD DE,(box_height)      ;
  LD A,(IX+$04)           ; The steepness
  AND $07                 ;
  LD C,A                  ;
  LD A,(IX+$04)               ; The shape
  AND $C0                     ;
  JR Z,test_landscape_piece_0 ;
  CP $40                      ;
  JP Z,test_slope_reversed    ;
  CP $80                      ;
  JP Z,test_slope_upside_down ;
  CP $C0                      ;
  JP Z,test_slope_both_ways   ;
  RET                         ;
test_landscape_piece_0:
  CALL test_slope         ; The first shape
; This entry point is used by the routine at test_slope_both_ways.
test_landscape_piece_1:
  LD A,(scratch_count)    ; In range: no touch yet
  AND A                   ;
  RET Z                   ;
  DEC A                   ;
  LD (scratch_count),A    ;
  LD A,D                  ; The object's right edge short of the slope: no
  ADD A,L                 ; touch
  LD L,A                  ;
  LD A,(IX+$00)           ;
  ADD A,B                 ;
  SUB L                   ;
  RET NC                  ;
  NEG                     ; How far in
  LD (touch_depth),A      ;
  JP overlap_found        ;

; Test a slope mirrored left to right
;
; Used by the routines at test_slope_reversed and test_slope_both_ways.
;
; Swaps the record's two x bounds, tests (test_slope), swaps them back, and
; drops a touch when the object ends before the piece's far x.
;
; IX The collision record
test_slope_swapped:
  LD A,(IX+$00)
  LD B,(IX+$02)
  LD (IX+$00),B
  LD (IX+$02),A
  CALL test_slope
  LD A,(IX+$00)
  LD C,(IX+$02)
  LD (IX+$00),C
  LD (IX+$02),A
  LD A,(scratch_count)
  AND A
  RET Z
  LD A,L
  ADD A,D
  CP (IX+$02)
  RET NC
  XOR A
  LD (scratch_count),A
  RET

; Test the second slope shape (flags $40)
;
; Used by the routine at test_landscape_piece.
;
; Mirrored left to right (test_slope_swapped); the touch measured from the
; other side. The entry point after it serves test_slope_upside_down.
;
; IX The collision record
test_slope_reversed:
  CALL test_slope_swapped
; This entry point is used by the routine at test_slope_upside_down.
test_slope_reversed_0:
  LD A,(scratch_count)
  AND A
  RET Z
  DEC A
  LD (scratch_count),A
  LD A,(IX+$00)
  SUB B
  SUB L
  RET C
  RET Z
  NEG
  LD (touch_depth),A
  JP overlap_found

; Test the third slope shape (flags $80)
;
; Used by the routine at test_landscape_piece.
;
; Upside down (test_slope, with the depth negated).
;
; IX The collision record
test_slope_upside_down:
  CALL test_slope
  LD A,B
  NEG
  LD B,A
  LD A,(scratch_count)
  AND A
  RET Z
  LD A,L
  ADD A,D
  CP (IX+$00)
  JP NC,test_slope_reversed_0
  XOR A
  LD (scratch_count),A
  RET

; Test the fourth slope shape (flags $C0)
;
; Used by the routine at test_landscape_piece.
;
; Mirrored and upside down, then measured as the first (test_landscape_piece).
;
; IX The collision record
test_slope_both_ways:
  CALL test_slope_swapped
  LD A,B
  NEG
  LD B,A
  JP test_landscape_piece_1

; Test SPACE and CAPS SHIFT
;
; Used by the routines at check_pause and check_pause_in_play.
;
; Zero if either is down.
;
; O:F Z if SPACE or CAPS SHIFT is down
test_pause_keys:
  LD A,$7F
  OUT ($FD),A
  IN A,($FE)
  AND $01
  RET Z
  LD A,$FE
  OUT ($FD),A
  IN A,($FE)
  AND $01
  RET

; Dissolve the play area to black, then clear it
;
; Used by the routine at play.
;
; The end of the death sequence (play): dissolve_play_area, then
; clear_play_area.
dissolve_and_clear:
  LD HL,$B708
  CALL dissolve_play_area
  JP clear_play_area

; Dissolve the play area to black
;
; Used by the routine at dissolve_and_clear.
;
; The play area is 28 bytes by 160 lines from x=8 at its bottom line $B7,
; walked from the bottom up (line_up_hl). Sixteen times over, every byte is
; ANDed with the next of the nine masks at dissolve_masks, each clearing one
; bit (one clears none), the masks running on from byte to byte and line to
; line, so the picture loses its pixels in a scattered pattern rather than line
; by line.
;
; HL The bottom left corner, as x and line
dissolve_play_area:
  CALL pixel_to_screen    ; Its screen address; sixteen times
  LD DE,dissolve_masks    ;
  LD (box_x),HL           ;
  LD A,$10                ;
dissolve_play_area_0:
  EX AF,AF'               ; Each line, 28 bytes
  LD HL,(box_x)           ;
  LD C,$A0                ;
dissolve_play_area_1:
  LD B,$1C                ;
  PUSH HL                 ;
dissolve_play_area_2:
  LD A,(DE)                  ; AND the bytes with the masks, round and round
  AND (HL)                   ;
  LD (HL),A                  ;
  INC L                      ;
  INC DE                     ;
  LD A,(DE)                  ;
  AND A                      ;
  JR NZ,dissolve_play_area_3 ;
  LD DE,dissolve_masks       ;
dissolve_play_area_3:
  DJNZ dissolve_play_area_2  ;
  POP HL                     ; The line above
  CALL line_up_hl            ;
  DEC C                      ;
  JR NZ,dissolve_play_area_1 ;
  EX AF,AF'                  ; Again
  DEC A                      ;
  JR NZ,dissolve_play_area_0 ;
  RET                        ;

; Clear the play area
;
; Used by the routines at dissolve_and_clear, game, game_over, level_done and
; swap_players.
;
; 28 bytes by 160 lines from the bottom left corner (x=8, line $B7), upwards.
clear_play_area:
  LD HL,$B708
  CALL pixel_to_screen
  LD C,$A0
clear_play_area_0:
  LD B,$1C
  PUSH HL
clear_play_area_1:
  LD (HL),$00
  INC HL
  DJNZ clear_play_area_1
  POP HL
  CALL line_up_hl
  DEC C
  JR NZ,clear_play_area_0
  RET

; The dissolve's masks
;
; Nine masks (dissolve_play_area), each with one bit clear but for $FF, and a 0
; that sends the walk back to the first.
dissolve_masks:
  DEFB $7F,$EF,$BF,$FF,$FE,$DF,$FB,$F7,$FD,$00

; Pause while SPACE or CAPS SHIFT is pressed
;
; Used by the routine at play.
;
; Called once a pass from the play loop (play). Pressing SPACE or CAPS SHIFT
; pauses the game: it waits for the key to be let go, then for a second press,
; then for that to be let go too, and play goes on.
check_pause:
  CALL test_pause_keys    ; Not pressed: go on
  RET NZ                  ;
  JR pause                ;

; Pause from the ship's routine
;
; Used by the routine at draw_trail.
;
; The same pause, called by draw_ship; before waiting it calls a routine inside
; draw_ship.
check_pause_in_play:
  CALL test_pause_keys
  RET NZ
  CALL side_jet
; This entry point is used by the routine at check_pause.
pause:
  CALL pause_delay        ; Wait for the key to be let go...
  CALL test_pause_keys    ;
  JR Z,pause              ;
check_pause_in_play_0:
  CALL pause_delay            ; ...then pressed again...
  CALL test_pause_keys        ;
  JR NZ,check_pause_in_play_0 ;
check_pause_in_play_1:
  CALL test_pause_keys       ; ...and let go
  JR Z,check_pause_in_play_1 ;
  RET                        ;

; A short delay
;
; Used by the routine at check_pause_in_play.
;
; delay with D=$40, between the pause's key tests, so that a bouncing key is
; read once.
pause_delay:
  LD D,$40
  JP delay

; The font
;
; The 45 characters in order, printed by print_string (three times the size).
;
; 45 characters of seven bytes, stored bottom row first and drawn by print_char
; from the bottom up: 0-25 the letters A to Z, 26-35 the digits 0-9 (so a
; digit's code is $1A plus the digit; add_score in test_player_shots), 36 and
; 38 spaces, 37 the II of INTERFACE II (both letters in one cell), 39 a full
; stop, 40 a dash, and 41-44 four pieces of a small picture. Each row's bit 0
; is clear, the space between characters.
font:
  DEFB $8E,$8E,$8E,$FE,$8E,$8E,$FE
  DEFB $FE,$E2,$E2,$FC,$E2,$E2,$FE
  DEFB $FE,$8E,$8E,$80,$8E,$8E,$FE
  DEFB $FC,$8E,$8E,$8E,$8E,$8E,$FE
  DEFB $FE,$E0,$E0,$FE,$E0,$E0,$FC
  DEFB $E0,$E0,$E0,$FE,$E0,$E0,$FE
  DEFB $FE,$8E,$9E,$80,$8E,$8E,$FE
  DEFB $E2,$E2,$E2,$FE,$E2,$E2,$E2
  DEFB $38,$38,$38,$38,$38,$38,$38
  DEFB $7C,$1C,$1C,$1C,$1C,$1C,$1C
  DEFB $8E,$8E,$8E,$FC,$E2,$E2,$E2
  DEFB $7C,$70,$70,$70,$70,$70,$70
  DEFB $BA,$BA,$BA,$BA,$BA,$BA,$FE
  DEFB $8E,$8E,$8E,$8E,$8E,$8E,$FE
  DEFB $FE,$E2,$E2,$E2,$E2,$E2,$FE
  DEFB $E0,$E0,$FE,$E2,$E2,$E2,$FE
  DEFB $FE,$EC,$EC,$EC,$EC,$FC,$FC
  DEFB $8E,$8E,$8E,$FC,$E2,$E2,$FE
  DEFB $FE,$8E,$8E,$1E,$E0,$E2,$FE
  DEFB $38,$38,$38,$38,$38,$38,$FE
  DEFB $FE,$E2,$E2,$E2,$E2,$E2,$E2
  DEFB $18,$3C,$7E,$E2,$E2,$E2,$E2
  DEFB $FE,$B9,$B9,$B9,$B9,$B9,$B9
  DEFB $8E,$8E,$8E,$7C,$E2,$E2,$E2
  DEFB $38,$38,$FE,$E2,$E2,$E2,$E2
  DEFB $FE,$8E,$E0,$38,$0E,$E2,$FE
  DEFB $FE,$F2,$EA,$EA,$EA,$E6,$FE
  DEFB $1C,$1C,$1C,$1C,$1C,$1C,$3C
  DEFB $FE,$8E,$80,$FE,$0E,$8E,$FC
  DEFB $FE,$8E,$0E,$3C,$0E,$8E,$FE
  DEFB $1C,$1C,$FE,$9C,$9C,$9C,$FC
  DEFB $FE,$8E,$0E,$FE,$80,$8E,$FE
  DEFB $FE,$8E,$8E,$FE,$80,$82,$FE
  DEFB $38,$38,$1C,$1C,$8E,$8E,$FE
  DEFB $FE,$8E,$8E,$7C,$E2,$E2,$FE
  DEFB $FE,$E2,$02,$FE,$E2,$E2,$FE
  DEFB $00,$00,$00,$00,$00,$00,$00
  DEFB $FE,$6C,$6C,$6C,$6C,$6C,$FE
  DEFB $00,$00,$00,$00,$00,$00,$00
  DEFB $18,$18,$00,$00,$00,$00,$00
  DEFB $00,$00,$7E,$7E,$00,$00,$00
  DEFB $00,$00,$00,$00,$04,$03,$03
  DEFB $00,$00,$0F,$1F,$0F,$00,$00
  DEFB $00,$00,$FF,$FF,$FF,$00,$00
  DEFB $00,$00,$F0,$F8,$F0,$00,$00

; Print one character
;
; Used by the routine at print_string.
;
; For print_string, which prints strings. The character's seven bytes are at
; font plus seven times its code. They are copied up the screen from the bottom
; row (print_char_rows), or, for a double-size string, drawn twice as wide and
; tall by print_char_double: the JR here is the switch, its offset written by
; print_string ($00 or $10). In the snapshot the offset is $FE, a jump to
; itself, but print_string always writes it first.
;
; DE The character code's address
; HL The screen address of its bottom row
print_char:
  PUSH DE                 ; The character's bytes
  LD A,(DE)               ;
  PUSH HL                 ;
  LD L,A                  ;
  LD H,$00                ;
  ADD HL,HL               ;
  ADD HL,HL               ;
  ADD HL,HL               ;
  LD E,A                  ;
  LD D,$00                ;
  AND A                   ;
  SBC HL,DE               ;
  LD DE,font              ;
  ADD HL,DE               ;
  EX DE,HL                ;
  POP HL                  ; Seven rows
  PUSH HL                 ;
  LD B,$07                ;
print_char_0:
  JR print_char_0         ; Normal or double size (written by print_string)

; Copy a character's rows up the screen
;
; The normal size: seven rows, the bottom first, each a line above the last
; (line_up_hl). Then the colour in the other register set's B goes into the
; attribute at its HL, which moves on; and HL moves to the next character cell.
;
; DE The character's bytes
; HL The screen address of its bottom row
; B 7
print_char_rows:
  LD A,(DE)               ; Seven rows, upwards
  LD (HL),A               ;
  INC DE                  ;
  CALL line_up_hl         ;
  DJNZ print_char_rows    ;
  EXX                     ; Its colour
  LD (HL),B               ;
  INC HL                  ;
  EXX                     ;
  POP HL                  ; The next cell; DE back to the code
  INC HL                  ;
  POP DE                  ;
  RET                     ;

; Print one character at double size
;
; The double-size half of the character printer print_char, reached through its
; self-modified JR at the end of print_char when print_string has patched that
; JR's displacement to $10 for a string whose attribute byte has bit 7 set.
; Each of the glyph's seven rows (stored bottom row first) is split into its
; two nibbles; each nibble is doubled to a whole byte through the table
; double_bits (bit n of the nibble becomes bits 2n and 2n+1), and the two bytes
; are written side by side on two pixel lines, so the character comes out 16
; pixels wide and 14 lines high, drawn upwards from the line HL starts on.
;
; HL must start on an odd pixel line within its cell (normally the bottom line,
; 7): the plain DEC H between the two lines of each row never crosses a cell
; boundary only because each pair starts on an odd line. Then a 2x2 block of
; attributes is coloured: the cell, the cell above, and the two to their right.
;
; DE The glyph (7 bytes, bottom row first)
; HL Screen address of the character's bottom-left byte
; B' The attribute
; DE' 32, one attribute row
; HL' Attribute address of the character's bottom-left cell
print_char_double:
  LD A,$07                ; Seven rows; B is the page of the nibble-doubling
  LD B,$5C                ; table double_bits
print_char_double_0:
  EX AF,AF'               ; Keep the row count in A'
  LD A,(DE)               ; The left half: the glyph byte's high nibble,
  SRL A                   ; doubled through the table
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  ADD A,$84               ;
  LD C,A                  ;
  LD A,(BC)               ;
  LD (HL),A               ; Write it on this line and the one above
  DEC H                   ;
  LD (HL),A               ;
  INC H                   ;
  INC HL                  ; The right half: the low nibble, doubled
  LD A,(DE)               ;
  AND $0F                 ;
  ADD A,$84               ;
  LD C,A                  ;
  LD A,(BC)               ;
  LD (HL),A               ; Write it on both lines too, and step back to the
  DEC H                   ; left column (now on the upper line)
  LD (HL),A               ;
  DEC HL                  ;
  INC DE                  ; Next glyph row; move up one more pixel line
  CALL line_up_hl         ;
  EX AF,AF'                 ; Until all seven rows are drawn
  DEC A                     ;
  JR NZ,print_char_double_0 ;
  EXX                     ; Colour the 2x2 block of cells: this one, the one
  LD (HL),B               ; above, above-right and right; HL' ends two cells on
  SBC HL,DE               ;
  LD (HL),B               ;
  INC HL                  ;
  LD (HL),B               ;
  ADD HL,DE               ;
  LD (HL),B               ;
  INC HL                  ;
  EXX                     ;
  POP HL                  ; Restore the glyph pointer and step the screen
  INC HL                  ; address two columns on, for the next character
  INC HL                  ;
  POP DE                  ;
  RET                     ;

; Print a string
;
; Used by the routines at print_strings, planet_secured, test_player_shots,
; print_lives, show_player and game_over.
;
; Prints one string record and returns DE pointing at the next, so a list of
; strings can be printed by calling this again. The record is: x in pixels, y
; in pixels (the pixel line the characters' bottom rows go on), an attribute
; byte, a character count, and that many character codes. Bit 7 of the
; attribute byte asks for double size (print_char_double); the other seven bits
; are the colour, so a string can never FLASH. Character n's glyph is the seven
; bytes at font plus 7n, bottom row first.
;
;   DE The string record
; O:DE The byte after it
print_string:
  XOR A                   ; Normal size: point the character printer's JR (at
  LD ($A0CA),A            ; the end of print_char) at the normal-size loop
  LD A,(DE)               ; The x and y coordinates
  LD L,A                  ;
  INC DE                  ;
  LD A,(DE)               ;
  LD H,A                  ;
  INC DE                  ;
  PUSH DE                 ; Keep the pointer and coordinates; HL becomes the
  PUSH HL                 ; screen address
  CALL pixel_to_screen    ;
  EXX                     ; In the other register set: HL' the attribute
  POP HL                  ; address, DE' the pointer to the attribute byte
  CALL pixel_to_attribute ;
  POP DE                  ;
  LD A,(DE)               ; Bit 7 of the attribute byte: double size?
  BIT 7,A                 ;
  JR Z,print_string_0     ;
  LD A,$10                ; Yes: patch the JR to reach print_char_double, strip
  LD ($A0CA),A            ; bit 7, and keep DE' at 32 to step an attribute row
  LD A,(DE)               ; up
  AND $7F                 ;
  LD DE,$0020             ;
print_string_0:
  LD B,A                  ; The colour in B'
  EXX                     ; Back in the main set: B is the character count
  INC DE                  ;
  LD A,(DE)               ;
  LD B,A                  ;
print_string_1:
  PUSH BC                 ; Print each character (print_char steps HL along)
  INC DE                  ;
  CALL print_char         ;
  POP BC                  ;
  DJNZ print_string_1     ;
  INC DE                  ; Point DE past the string
  RET                     ;

; Turn pixel coordinates into a screen address
;
; Used by the routines at place_flicker_beam, dissolve_play_area,
; clear_play_area, print_string, set_direct_new, record_screen_address,
; draw_sprite_pair, copy_box_in, load_ore, toggle_carried_part,
; draw_carried_piece, draw_ship_image, draw_trail, side_jet, lift_jet,
; draw_bitmap, update_fuel_gauge, step_screen_address, fill_behind_moving_piece
; and fly_in_step.
;
; The standard Spectrum screen-address calculation from a pixel position: L is
; x (0-255, its low three bits dropped) and H is y (0-191 from the top).
; Everything in the game that draws on the screen uses this.
;
;   L x in pixels
;   H y in pixels
; O:HL The screen byte containing the pixel
pixel_to_screen:
  LD A,L                  ; The column, x divided by 8
  RRC A                   ;
  RRC A                   ;
  RRC A                   ;
  AND $1F                 ;
  LD L,A                  ;
  LD A,H                  ; Bits 3-5 of y become bits 5-7 of the low byte
  RLC A                   ;
  RLC A                   ;
  AND $E0                 ;
  OR L                    ;
  LD L,A                  ;
  LD A,H                  ; Bits 0-2 of y are the pixel line within the cell
  AND $07                 ;
  EX AF,AF'               ;
  LD A,H                  ; Bits 6-7 of y pick the third of the screen; combine
  RRC A                   ; with the pixel line
  RRC A                   ;
  RRC A                   ;
  AND $18                 ;
  OR $40                  ;
  LD H,A                  ;
  EX AF,AF'               ;
  OR H                    ;
  LD H,A                  ;
  RET

; Turn pixel coordinates into an attribute address
;
; Used by the routines at print_string, draw_star_layer, draw_trail,
; fill_attributes and colour_piece.
;
; The attribute that colours the pixel at (x, y): 22528 + 32 * (y / 8) + x / 8.
;
;   L x in pixels
;   H y in pixels
; O:HL The attribute address
pixel_to_attribute:
  PUSH BC
  LD A,L                  ; The column, x divided by 8
  RRC A                   ;
  RRC A                   ;
  RRC A                   ;
  AND $1F                 ;
  LD L,A                  ;
  LD A,H                  ; Bits 3-5 of y become bits 5-7 of the low byte
  RLC A                   ;
  RLC A                   ;
  LD C,A                  ;
  AND $E0                 ;
  OR L                    ;
  LD L,A                  ;
  LD A,C                  ; Bits 6-7 of y become bits 0-1 of the high byte, in
  AND $03                 ; the attribute file
  OR $58                  ;
  LD H,A                  ;
  POP BC
  RET

; Run a sky object's update routine
;
; Used by the routine at update_objects.
;
; Jumps to the update routine for the object in record IX, chosen by its
; graphic number (+0), which for the sky objects runs from $13 to $1E: the
; first twelve words of run_sky_object_handler's table object_handlers are
; their handlers in that order. Called once for each live record of the nine
; sky-object records at object_records every pass (update_objects), between the
; setting up of its erase (prepare_erase) and the drawing (prepare_draw and
; draw_object_images, which the handlers end with).
;
; IX The object's record
run_sky_object_handler:
  LD A,(IX+$00)           ; Index the handler table by graphic number less $13,
  SUB $13                 ; and jump
  LD E,A                  ;
  LD D,$00                ;
  LD HL,object_handlers   ;
  ADD HL,DE               ;
  ADD HL,DE               ;
  LD A,(HL)               ;
  INC HL                  ;
  LD H,(HL)               ;
  LD L,A                  ;
  JP (HL)                 ;

; Point DE at the frame list of a record's graphic
;
; Used by the routines at draw_star_layer, draw_trail and sprite_graphic_width.
;
; Each graphic number has an entry in the graphic table, the words from the
; 25th byte of object_handlers on: the address of a list of words, one per
; frame or variant, each the address of a graphic. This returns that list for
; the record's graphic (+0). object_graphic goes one step further and picks the
; frame.
;
;   IX The record
; O:DE The graphic's frame list
graphic_frame_list:
  LD C,(IX+$00)
  LD HL,object_graphic_lists
  CALL table_word
  EX DE,HL
  RET

; Read word C of a table
;
; Used by the routines at change_course, graphic_frame_list, set_old_sprite,
; set_new_sprite, toggle_carried_part, fit_addon and beam_frame.
;
;   HL The table
;   C The index
; O:HL The word
table_word:
  LD B,$00
  ADD HL,BC
  ADD HL,BC
  LD A,(HL)
  INC HL
  LD H,(HL)
  LD L,A
  RET

; Move DE up one pixel line
;
; Used by the routines at load_ore and draw_frame.
;
; The same as line_up_hl, for a screen address in DE.
;
; DE A screen address
line_up_de:
  DEC D
  LD A,D
  INC A
  AND $07
  RET NZ
  LD A,E
  SUB $20
  LD E,A
  RET C
  LD A,D
  ADD A,$08
  LD D,A
  RET

; Move HL up one pixel line
;
; Used by the routines at dissolve_play_area, clear_play_area, print_char_rows,
; print_char_double, toggle_carried_part, draw_beam, erase_laser, draw_bitmap,
; draw_frame, draw_fuel_gauge, draw_flat_end_lip, fill_up_past_step,
; fill_row_pair, start_landscape and fill_under_slope.
;
; Moves a screen address one pixel line up the screen: the line above within
; the cell, or the bottom line of the cell above, crossing into the third above
; when the row was the top of a third. Everything in the game draws bottom-up
; -- the graphics are stored bottom row first -- so this, and copies of it
; written inline in the sprite routines, are what step the drawing. About 47
; T-states with the CALL, most of the time.
;
;   HL A screen address
; O:HL The address one pixel line above
line_up_hl:
  DEC H                   ; Up a line; done unless that was the top line of the
  LD A,H                  ; cell (the line number wrapped to 7)
  INC A                   ;
  AND $07                 ;
  RET NZ                  ;
  LD A,L                  ; Back up a character row; done if that borrowed,
  SUB $20                 ; which means the row is in the same third and H is
  LD L,A                  ; right as it is
  RET C                   ;
  LD A,H                  ; Otherwise put H back into the third the row is in
  ADD A,$08               ;
  LD H,A                  ;
  RET                     ;

; The old image's screen x
;
; The sprite compositor (draw_sprite_pair) handles a pair of images at a time:
; the image an object was drawn as last pass (the old, to be rubbed out) and
; the image it is to be drawn as now (the new). These six bytes, old_sprite_x
; to new_sprite_rows, hold them. This one is the old image's x on the screen,
; set by prepare_erase from the record's +$11 (and by draw_star_layer for a
; star), and read with the next byte as a word.
old_sprite_x:
  DEFB $00

; The old image's screen y
;
; The line of the old image's bottom row, from the record's +$12, clipped to
; the play area's lines $18-$B7 by clip_sprite_rows through old_sprite_header.
old_sprite_y:
  DEFB $00

; The new image's screen x
;
; Set by place_on_screen when it places an object on the screen, and kept in
; the record's +$11 for the next pass's erase.
new_sprite_x:
  DEFB $00

; The new image's screen y
;
; The line of the new image's bottom row: set by place_on_screen, clipped by
; clip_sprite_rows through new_sprite_header. trim_colour_patch moves it up
; afterwards for the colour patch alone.
new_sprite_y:
  DEFB $00

; The old image's height
;
; Rows to rub out, after clipping; 0 when there is no old image (cleared by
; clear_sprite_pair, set by set_old_sprite). Others read it to tell whether an
; object was on the screen last pass (collect_pickup, attacker_fires,
; update_objects).
old_sprite_rows:
  DEFB $00

; The new image's height
;
; Rows to draw, after clipping; 0 when there is no new image (cleared by
; clear_sprite_pair, set by set_new_sprite). trim_colour_patch trims it for the
; colour patch, which colour_sprite reads.
new_sprite_rows:
  DEFB $00

; Read the new image's graphic header and clip it
;
; Used by the routines at prepare_draw and draw_star_layer.
;
; Reads the width and height at the head of the new image's graphic and clips
; its rows to the play area (clip_sprite_rows), updating new_sprite_y.
;
;   DE The graphic
; O:DE Its first row of data to draw (clipped)
; O:H Its width in bytes
; O:B Rows to draw
; O:C Which row routine it needs (see read_graphic_header)
new_sprite_header:
  LD HL,(new_sprite_x)
  CALL read_graphic_header
  LD A,(new_sprite_y)
  CALL clip_sprite_rows
  LD A,L
  LD (new_sprite_y),A
  RET

; Read the old image's graphic header and clip it
;
; Used by the routines at prepare_erase and draw_star_layer.
;
; As new_sprite_header, for the old image (old_sprite_x, old_sprite_y).
;
; DE The graphic
old_sprite_header:
  LD HL,(old_sprite_x)
  CALL read_graphic_header
  LD A,(old_sprite_y)
  CALL clip_sprite_rows
  LD A,L
  LD (old_sprite_y),A
  RET

; Clip a sprite's rows to the play area
;
; Used by the routines at new_sprite_header and old_sprite_header.
;
; Sprites are drawn upwards from their bottom row, inside the lines $18-$B7 (24
; to 183): below the panel at the top and above the bottom row of the screen. A
; sprite reaching above line $18 has its height cut so that its top row is $18;
; one reaching below line $B7 has its bottom rows skipped (the graphic is
; stored bottom row first, so DE moves on by width times the rows cut) and its
; y put at $B7. A sprite wholly outside is not caught here; place_on_screen
; decides what is visible.
;
;   A y of the bottom row
;   B Height in rows
;   H Width in bytes
;   DE The graphic's rows
; O:L The y to draw from
; O:B The rows to draw
clip_sprite_rows:
  LD L,A                   ; Does the top row reach above line $18?
  SUB $17                  ;
  SUB B                    ;
  JR NC,clip_sprite_rows_0 ;
  LD A,L                  ; Yes: draw only the rows from y up to line $18
  SUB $17                 ;
  LD B,A                  ;
  RET                     ;
clip_sprite_rows_0:
  LD A,L                  ; Does the bottom row lie below line $B7? Return if
  SUB $B7                 ; not
  RET Z                   ;
  RET C                   ;
  LD L,A                  ; Rows below: take them off the height
  LD A,B                  ;
  SUB L                   ;
  LD B,A                  ;
  XOR A                    ; Skip them in the graphic: width times the rows cut
clip_sprite_rows_1:
  ADD A,H                  ;
  DEC L                    ;
  JR NZ,clip_sprite_rows_1 ;
  EX DE,HL                 ;
  CALL add_a_to_hl         ;
  EX DE,HL                 ;
  LD L,$B7                ; And draw from line $B7
  RET                     ;

; Read the width and height bytes at DE
;
; Used by the routine at draw_trail.
;
;   DE A graphic
; O:H Its width in bytes
; O:B Its height in rows
; O:DE Its data
read_graphic_size:
  LD A,(DE)
  LD H,A
  INC DE
  LD A,(DE)
  LD B,A
  INC DE
  RET

; Read a graphic's header for the compositor
;
; Used by the routines at new_sprite_header and old_sprite_header.
;
; A graphic is a width in bytes and a height in rows, then the rows, bottom row
; first, each the width in bytes, left to right; there is no mask. This also
; returns in C the index into the row-routine tables erase_row_routines and
; draw_row_routines: the width less one when the sprite is drawn shifted (x AND
; 6 not zero), the width plus four when it is byte-aligned -- so 0-4 are the
; shifted routines for widths 1-5 and 5-10 the aligned ones for widths 1-6.
;
;   DE The graphic
;   L The x it is to be drawn at
; O:H Width in bytes
; O:B Height in rows
; O:C The row routine's index
; O:DE The first row
read_graphic_header:
  LD A,(DE)
  LD H,A
  LD C,A
  DEC C
  INC DE
  LD A,(DE)
  LD B,A
  INC DE
  LD A,L                  ; Shifted: index width-1
  AND $06                 ;
  RET NZ                  ;
  LD A,$05                ; Byte-aligned: index width+4
  ADD A,C                 ;
  LD C,A                  ;
  RET

; Set up the old image's erase
;
; Used by the routines at prepare_erase and draw_star_layer.
;
; Records the old image's height (old_sprite_rows) and width (sprite_width,
; which draw_sprite_pair turns to pixels) and chooses the routine that rubs one
; of its rows out of the buffer (erase_row_routines) into IY, and its shift
; table page into B.
;
;   B Height
;   H Width in bytes
;   C Row routine index (read_graphic_header)
; O:IY The erase-row routine
; O:B The shift tables' page (shift_page)
set_old_sprite:
  LD A,B
  LD (old_sprite_rows),A
  LD A,H
  LD (sprite_width),A
  LD HL,erase_row_routines
  CALL table_word
  PUSH HL
  POP IY
  JR shift_page

; Set up the new image's draw
;
; Used by the routines at prepare_draw and draw_star_layer.
;
; As set_old_sprite for the new image: its height (new_sprite_rows) and width,
; its draw-row routine from draw_row_routines into IX, and its shift page into
; B.
;
;   B Height
;   H Width in bytes
;   C Row routine index (read_graphic_header)
; O:IX The draw-row routine
; O:B The shift tables' page
set_new_sprite:
  LD A,B
  LD (new_sprite_rows),A
  LD A,H
  LD (sprite_width_drawn),A
  LD HL,draw_row_routines
  CALL table_word
  PUSH HL
  POP IX
  LD A,(new_sprite_x)
  JR shift_page_0

; The shift tables' page for the old image
;
; Used by the routine at set_old_sprite.
;
; B becomes $F8 plus x AND 6: $FA, $FC or $FE for an image at 2, 4 or 6 pixels
; past a byte, the pages of the shift tables shift_tables that
; build_shift_tables builds (on the first page of each pair a byte shifted
; right that many bits, on the page after it the bits that fall into the next
; byte). $F8 for a byte-aligned image is never used as a table. The second
; instruction is also an entry, used by set_new_sprite: the same for the x in
; A.
;
; O:B The page
shift_page:
  LD A,(old_sprite_x)
; This entry point is used by the routine at set_new_sprite.
shift_page_0:
  AND $06
  ADD A,$F8
  LD B,A
  RET

; Set up the new image for the direct drawer
;
; Used by the routine at draw_trail.
;
; For draw_direct_pair, the drawer that works straight on the screen: the new
; image's bottom line (direct_new_line, from the sprite position word in
; moved_x), its height (ground_cell) and width (sprite_width_drawn), and the
; screen address of its bottom-left byte.
;
;   B Height
;   H Width
; O:HL Screen address
set_direct_new:
  LD A,($BF84)
  LD (direct_new_line),A
  LD A,B
  LD (ground_cell),A
  LD A,H
  LD (sprite_width_drawn),A
  LD HL,(sprite_screen_pos)
  JP pixel_to_screen

; Set up the old image for the direct drawer
;
; Used by the routine at draw_trail.
;
; As set_direct_new for the old image (direct_old_line), without the screen
; address.
;
; B Height
; H Width
set_direct_old:
  LD A,($BF84)
  LD (direct_old_line),A
  LD A,B
  LD (scratch_count),A
  LD A,H
  LD (sprite_width),A
  RET

; The direct drawer's old image's bottom line
;
; Set by set_direct_old or draw_ship; draw_direct_pair compares it with
; direct_new_line to line the two images up, then reuses it for the old image's
; rows left over.
direct_old_line:
  DEFB $00

; The direct drawer's new image's bottom line
;
; Set by set_direct_new or draw_ship; reused by draw_direct_pair for the new
; image's rows left over.
direct_new_line:
  DEFB $00

; The screen address of the position at +$0B and +$0C of record IX
;
; Used by the routines at update_special_beam, update_shot, draw_shot and
; update_left_shot.
;
; Used for the player's shots (update_special_beam, update_shot, draw_shot,
; update_left_shot), whose x and y are kept there.
;
;   IX The record
; O:HL The screen address
record_screen_address:
  LD L,(IX+$0B)
  LD H,(IX+$0C)
  JP pixel_to_screen

; The compositor's erase-row routines
;
; Eleven words, indexed by read_graphic_header's routine index (set_old_sprite
; reads them): the entry points into the unrolled loops that rub one row of an
; old image out of the buffer. Indexes 0-4 are the shifted loop
; erase_row_shifted for widths 1-5, entered so many byte-steps from its end;
; 5-10 the byte-aligned loop erase_aligned_row for widths 1-6.
erase_row_routines:
  DEFW $A719              ; Shifted widths 1-5, then aligned widths 1-6
  DEFW $A70B              ;
  DEFW $A6FD              ;
  DEFW $A6EF              ;
  DEFW erase_row_shifted  ;
  DEFW $A6D0              ;
  DEFW $A6CA              ;
  DEFW $A6C4              ;
  DEFW $A6BE              ;
  DEFW $A6B8              ;
  DEFW erase_aligned_row  ;

; The compositor's draw-row routines
;
; As erase_row_routines, for drawing the new image (set_new_sprite): the
; shifted loop draw_row_shifted for widths 1-5, the byte-aligned loop
; draw_aligned_row for widths 1-6.
draw_row_routines:
  DEFW $A78C              ; Shifted widths 1-5, then aligned widths 1-6
  DEFW $A780              ;
  DEFW $A774              ;
  DEFW $A768              ;
  DEFW draw_row_shifted   ;
  DEFW $A74C              ;
  DEFW $A747              ;
  DEFW $A742              ;
  DEFW $A73D              ;
  DEFW $A738              ;
  DEFW draw_aligned_row   ;

; Add or take off a record's size (unused)
;
; Two small routines that nothing in the program refers to, and that never ran
; in the build's sessions: this one adds +5 to +2 and +6 to +3 of record IX;
; the one 21 bytes on takes them off. A leftover.
;
; IX A record
add_record_size:
  LD A,(IX+$02)
  LD B,(IX+$05)
  ADD A,B
  LD (IX+$02),A
  LD A,(IX+$03)
  LD B,(IX+$06)
  ADD A,B
  LD (IX+$03),A
  RET
  LD A,(IX+$02)
  LD B,(IX+$05)
  SUB B
  LD (IX+$02),A
  LD A,(IX+$03)
  LD B,(IX+$06)
  SUB B
  LD (IX+$03),A
  RET

; Start a new sprite pair: no old image, no new image
;
; Used by the routines at update_objects, move_missile, draw_rocket,
; update_ground_objects and draw_star_layer.
;
; Called before each object's erase and draw is set up: both heights
; (old_sprite_rows, new_sprite_rows) and both widths 0, and the left-clip flag
; (sprite_left_clip) clear.
clear_sprite_pair:
  LD HL,$0000
  LD (old_sprite_rows),HL
  LD (sprite_width),HL
  XOR A
  LD (sprite_left_clip),A
  RET

; The old image's width in bytes
;
; Kept by draw_sprite_pair before it turns the working copies of both widths
; (sprite_width, sprite_width_drawn) into pixels; read by erase_old_draw_new.
; Written as a word with new_sprite_width.
old_sprite_width:
  DEFB $00

; The new image's width in bytes
;
; Kept by draw_sprite_pair; read by erase_old_draw_new and dissolve_sprite.
new_sprite_width:
  DEFB $00

; The compositor's left-clip flag
;
; Not zero while the pair is drawn straddling the left edge of the play area:
; set by place_on_screen for the new image and by prepare_erase (from +$10 bit
; 6) for the old, cleared by clear_sprite_pair. draw_sprite_pair then works
; with both x positions $60 less, and copies only the part of the box right of
; the edge. object_box and colour_sprite read it too.
sprite_left_clip:
  DEFB $00

; The dissolve masks
;
; Seven masks, one for each step of an object's dissolving (dissolve_sprite,
; indexed from sprite_left_clip so that step 1 is the first): each has one bit
; fewer set than the last, from seven bits down to one.
dissolve_mask_bits:
  DEFB $EF                ; Steps 1 to 7
  DEFB $AF                ;
  DEFB $AD                ;
  DEFB $8D                ;
  DEFB $89                ;
  DEFB $09                ;
  DEFB $08                ;

; The new image's dissolve step
;
; 0, or bit 7 and the step 1-7 while the object dissolves: set by prepare_draw
; from the record's +$17, read by dissolve_sprite.
dissolve_step:
  DEFB $00

; Rub out an object's old image and draw its new one, without flicker
;
; Used by the routines at draw_object_images and draw_star_layer.
;
; The sprite compositor, which draws every ground object and sky object, and
; the stars (draw_star_layer): about a third of all the game's time goes
; through here. Both images are worked on in a buffer, never on the screen, and
; the screen is written once, so nothing flickers. The steps:
;
; 1. The box: the smallest rectangle, whole bytes wide, covering both images:
; its left edge (piece_x) and bottom line (piece_y), and its width in bytes (at
; most 7) and height plus one, kept in piece_dx and piece_rise. If only one
; image is present, the other's position is taken as its own.
;
; 2. Copy the box from the screen into the buffer (copy_box_in), bottom row
; first, rows packed at the box's width. The buffer is page $F8, in the middle
; of the start-up code that runs once from build_shifted_pieces and is no
; longer wanted; the drawing addresses it by L alone, so the box can be no more
; than 256 bytes.
;
; 3. Rub out the old image in the buffer: AND each byte with the complement of
; the graphic, shifted to the image's x (erase_old_draw_new, erase_aligned_row,
; erase_row_shifted). This clears every pixel the graphic sets, whatever was
; under it; there is no mask, and nothing restores what the old image covered.
;
; 4. Draw the new image into the buffer: OR (draw_aligned_row,
; draw_row_shifted). Then, if the object is dissolving, AND its rows with that
; step's mask (dissolve_sprite).
;
; 5. Copy the box back to the screen (the end of dissolve_sprite onwards),
; clipped to the play area: column 0 is never written, nor anything right of
; x=$E8 (the fuel gauge's columns).
;
; Graphics are not pre-shifted. An image at x AND 6 = 2, 4 or 6 is shifted as
; it is drawn, each byte through a pair of 256-byte tables (shift_tables); x is
; always even. The loops are unrolled, with patched entry points: the row loops
; are entered so many steps from their ends (erase_row_routines,
; draw_row_routines), and the copies are LDI chains entered by DJNZs whose
; displacements are patched (see copy_box_in).
;
; A sprite straddling the left edge of the play area (sprite_left_clip) is
; handled by taking $60 off both x positions, which moves positions either side
; of 0 into one unbroken range; only the part of the box right of the edge, at
; x=8, is copied in and out (copy_in_clipped, copy_out_part_row).
;
; The cost, worked out from the instructions: for a 3-byte-wide sprite 16 rows
; tall, moved and shifted, a 5-byte box of 17 rows, about 2,500 T-states to
; copy in, 4,500 to rub out, 4,100 to draw, 2,500 to copy out -- some 13,500 in
; all, a fifth of a frame.
;
; DE The new image's first row (new_sprite_header)
; B The new image's shift page (set_new_sprite)
; IY The old image's erase-row routine (set_old_sprite)
; IX The new image's draw-row routine (set_new_sprite)
; DE' The old image's first row
; B' The old image's shift page
draw_sprite_pair:
  PUSH DE                 ; Keep the new image's graphic and page (restored at
  PUSH BC                 ; erase_old_draw_new)
  LD HL,(sprite_width)     ; Keep both widths in bytes; turn the working copies
  LD (old_sprite_width),HL ; into pixels
  ADD HL,HL                ;
  ADD HL,HL                ;
  ADD HL,HL                ;
  LD (sprite_width),HL     ;
  LD A,(old_sprite_rows)   ; No old image: give it the new one's position, so
  AND A                    ; the box is the new image's alone
  JR NZ,draw_sprite_pair_0 ;
  LD HL,(new_sprite_x)     ;
  LD (old_sprite_x),HL     ;
draw_sprite_pair_0:
  LD A,(new_sprite_rows)   ; No new image: give it the old one's
  AND A                    ;
  JR NZ,draw_sprite_pair_1 ;
  LD HL,(old_sprite_x)     ;
  LD (new_sprite_x),HL     ;
draw_sprite_pair_1:
  LD A,(sprite_left_clip) ; Left-clip mode: take $60 off both x positions
  AND A                   ;
  JR Z,draw_sprite_pair_2 ;
  LD A,(old_sprite_x)     ;
  SUB $60                 ;
  LD (old_sprite_x),A     ;
  LD A,(new_sprite_x)     ;
  SUB $60                 ;
  LD (new_sprite_x),A     ;
draw_sprite_pair_2:
  LD A,(old_sprite_y)      ; The box's bottom line is the lower of the two
  LD E,A                   ; bottom lines
  LD A,(new_sprite_y)      ;
  LD D,A                   ;
  CP E                     ;
  JR NC,draw_sprite_pair_3 ;
  LD A,E                   ;
draw_sprite_pair_3:
  LD (piece_y),A           ;
  LD H,A                   ;
  LD BC,(old_sprite_rows)  ; The line above the box's top: the smaller of each
  LD A,E                   ; image's y less its height
  SUB C                    ;
  LD E,A                   ;
  LD A,D                   ;
  SUB B                    ;
  CP E                     ;
  JR NC,draw_sprite_pair_4 ;
  LD E,A                   ;
draw_sprite_pair_4:
  LD A,H                  ; The box's rows, plus one
  SUB E                   ;
  INC A                   ;
  LD (piece_rise),A       ;
  LD A,(old_sprite_x)     ; The box's left edge: the smaller x, rounded down to
  LD E,A                  ; a byte
  LD A,(new_sprite_x)     ;
  LD D,A                  ;
  CP E                    ;
  JR C,draw_sprite_pair_5 ;
  LD A,E                  ;
draw_sprite_pair_5:
  AND $F8                 ;
  LD (piece_x),A          ;
  LD L,A                  ;
  LD BC,(sprite_width)     ; The further right edge of the two, from the box's
  LD A,E                   ; left edge
  SUB L                    ;
  ADD A,C                  ;
  LD E,A                   ;
  LD A,D                   ;
  SUB L                    ;
  ADD A,B                  ;
  CP E                     ;
  JR NC,draw_sprite_pair_6 ;
  LD A,E                   ;
draw_sprite_pair_6:
  ADD A,$06               ; The box's width in bytes, rounding up (x is always
  LD C,A                  ; even, so 6 is enough)
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD (piece_dx),A         ;
  LD A,(sprite_left_clip) ; Not straddling the left edge: copy the whole box in
  AND A                   ; (copy_box_in)
  JP Z,copy_box_in        ;
  LD A,L                  ; Straddling: the bytes of the box right of the play
  ADD A,C                 ; area's edge (x=8, now $A8)
  SUB $A8                 ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD B,A                  ;
  LD A,(piece_dx)         ; Skip the rest in the buffer at the end of each row;
  SUB B                   ; start that far into the buffer
  LD ($A531),A            ;
  LD ($A3DC),A            ;
  LD E,A                  ;
  LD A,B                  ; Patch the DJNZs of the clipped copies in and out to
  XOR $07                 ; copy that many bytes
  DEC A                   ;
  ADD A,A                 ;
  ADD A,$03               ;
  LD ($A520),A            ;
  LD ($A3CB),A            ;
  LD C,$FF                ; The screen address of the box's bottom row at the
  LD D,$F8                ; play area's left edge
  LD L,$08                ;
  CALL pixel_to_screen    ;
  LD (piece_page_x),HL    ;
  LD A,(piece_rise)       ; The box's rows plus one
  LD B,A                  ;
; This entry point is used by the routine at copy_in_clipped.
draw_sprite_pair_7:
  PUSH HL                 ; Keep this row's screen address
draw_sprite_pair_8:
  DJNZ draw_sprite_pair_8 ; The DJNZ whose patched displacement enters the
                          ; chain of LDIs at copy_in_clipped, and leaves when
                          ; the rows are done
  JP erase_old_draw_new   ; Then rub out and draw

; Copy a clipped row into the buffer
;
; The row loop of the clipped copy into the buffer, for a box straddling the
; left edge of the play area: entered from the DJNZ at the end of
; draw_sprite_pair, so many LDIs before the end of this chain as there are
; bytes on the screen, then the rest of the buffer's row skipped and the screen
; address moved up a line. The first LDI is reached only with the full six
; bytes, which never happened in the build's sessions.
copy_in_clipped:
  LDI                     ; Copy the row's bytes from the screen to the buffer
  LDI                     ; (C is $FF, so the LDIs never touch B)
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LD A,$00                ; Skip the part of the buffer's row left of the edge
  ADD A,E                 ; (the LD's operand is patched by draw_sprite_pair)
  LD E,A                  ;
  POP HL                   ; Back to the row's start, and up one pixel line;
  DEC H                    ; then round again
  LD A,H                   ;
  INC A                    ;
  AND $07                  ;
  JP NZ,draw_sprite_pair_7 ;
  LD A,L                   ;
  SUB $20                  ;
  LD L,A                   ;
  JP C,draw_sprite_pair_7  ;
  LD A,H                   ;
  ADD A,$08                ;
  LD H,A                   ;
  JP draw_sprite_pair_7    ;

; Copy the box from the screen into the buffer
;
; Used by the routine at draw_sprite_pair.
;
; The rows are copied bottom-up into the buffer at page $F8. The loop is three
; pieces: PUSH HL keeps the row's screen address; a DJNZ whose displacement is
; patched to land in the LDI chain of copy_in_row as many LDIs from its end as
; the box is bytes wide (7 less the width, times two, plus three to clear the
; JP); and the chain's end, which pops the address, moves it up a line and
; jumps back to the PUSH. When B runs out the DJNZ falls through to the JP
; instead, with one address left on the stack for erase_old_draw_new to drop. C
; is $FF so that the LDIs' count of BC never reaches B.
;
; H The box's bottom line
; L The box's left edge
copy_box_in:
  LD DE,$F800             ; Point DE at the buffer; patch this copy's DJNZ and
  LD A,(piece_dx)         ; the copy out's (copy_box_out) for the box's width
  XOR $07                 ;
  ADD A,A                 ;
  ADD A,$03               ;
  LD ($A553),A            ;
  LD ($A415),A            ;
  CALL pixel_to_screen    ; The screen address of the box's bottom-left byte,
  LD (piece_page_x),HL    ; kept for the copy back
  LD C,$FF                ; C $FF; B the rows plus one
  LD A,(piece_rise)       ;
  LD B,A                  ;
; This entry point is used by the routine at copy_in_row.
copy_box_in_0:
  PUSH HL                 ; Keep the address; copy a row (or, when the rows are
  DJNZ copy_box_in_0      ; done, go on to erase_old_draw_new)
  JP erase_old_draw_new   ;

; Copy a row of the box into the buffer
;
; The LDI chain and row step of copy_box_in, entered part way through.
copy_in_row:
  LDI                     ; Up to seven bytes from the screen to the buffer
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  POP HL                  ; Back to the row's start and up one pixel line; then
  DEC H                   ; round again
  LD A,H                  ;
  INC A                   ;
  AND $07                 ;
  JP NZ,copy_box_in_0     ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JP C,copy_box_in_0      ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
  JP copy_box_in_0        ;

; Rub out the old image in the buffer, then draw the new one
;
; Used by the routines at draw_sprite_pair and copy_box_in.
;
; The middle of draw_sprite_pair. The register sets are swapped so that the
; main set holds the old image's graphic and shift page (saved by the EXX after
; prepare_erase) while the new image's wait in the other set. Each row routine
; finishes by adding the patched step to L (the box's width less the bytes the
; routine covered), counting the rows in A', and either jumping back through IY
; (or IX) for the next row or on to the next stage.
erase_old_draw_new:
  POP HL                  ; Drop the unused screen address left by the copy in
  POP BC                  ; Restore the new image's graphic and page, and swap
  POP DE                  ; them out of the way
  EXX                     ;
  LD A,(old_sprite_rows)    ; No old image: straight on to drawing the new one
  AND A                     ;
  JP Z,erase_old_draw_new_1 ;
  LD HL,(old_sprite_x)    ; Where the old image starts in the buffer
  CALL buffer_address     ;
  LD A,(old_sprite_width)    ; The bytes a row covers: the width, less one if
  LD C,A                     ; byte-aligned (a shifted row spills into one more
  LD A,B                     ; byte)
  CP $F8                     ;
  JR NZ,erase_old_draw_new_0 ;
  DEC C                      ;
erase_old_draw_new_0:
  LD A,(piece_dx)         ; The step from the end of one row to the start of
  SUB C                   ; the next, into both erase loops
  LD ($A6D6),A            ;
  LD ($A728),A            ;
  LD A,(old_sprite_rows)  ; Rub out the old image's rows (A' counts them)
  EX AF,AF'               ;
  JP (IY)                 ;
; This entry point is used by the routines at erase_aligned_row and
; erase_row_shifted.
erase_old_draw_new_1:
  EXX                     ; Swap to the new image's graphic and page
  LD A,(new_sprite_rows)  ; No new image: copy the box back
  AND A                   ;
  JP Z,dissolve_sprite_2  ;
  LD HL,(new_sprite_x)    ; Where the new image starts in the buffer
  CALL buffer_address     ;
  LD A,(new_sprite_width)    ; The bytes a row covers
  LD C,A                     ;
  LD A,B                     ;
  CP $F8                     ;
  JR NZ,erase_old_draw_new_2 ;
  DEC C                      ;
erase_old_draw_new_2:
  LD A,(piece_dx)         ; The step to the next row, into both draw loops
  SUB C                   ;
  LD ($A751),A            ;
  LD ($A799),A            ;
  DEC A                   ; The dissolve's step (dissolve_sprite) is one less,
  LD ($A4C5),A            ; as it covers one byte more
  LD (saved_speed),HL     ; Keep the start for the dissolve, in saved_speed
  LD A,(new_sprite_rows)  ; Draw the new image's rows
  EX AF,AF'               ;
  JP (IX)                 ;

; Break up a dissolving object's image
;
; Used by the routines at draw_aligned_row and draw_row_shifted.
;
; An object that has been destroyed dissolves over seven passes (prepare_draw):
; its new image, just drawn into the buffer, is ANDed with that step's mask
; from dissolve_mask_bits, rotated a bit to the right at every byte so the gaps
; do not line up in columns. The masks keep seven bits, then six, down to one,
; so the object crumbles away.
dissolve_sprite:
  LD A,(dissolve_step)    ; Not dissolving: go and copy the box back
  AND A                   ;
  JP Z,dissolve_sprite_2  ;
  AND $0F                 ; D is the mask for this step
  LD HL,sprite_left_clip  ;
  CALL add_a_to_hl        ;
  LD D,(HL)               ;
  LD HL,(saved_speed)     ; The new image's start in the buffer
  LD A,(new_sprite_width) ; The bytes a row covers (one more if the image is
  LD E,A                  ; not on a byte boundary), and the rows
  LD A,(new_sprite_rows)  ;
  LD C,A                  ;
  LD A,(new_sprite_x)     ;
  AND $07                 ;
  JR Z,dissolve_sprite_0  ;
  INC E                   ;
dissolve_sprite_0:
  LD B,E                  ;
dissolve_sprite_1:
  LD A,D                  ; AND each byte with the mask, turning the mask a bit
  AND (HL)                ; each byte
  LD (HL),A               ;
  RRC D                   ;
  INC L                   ;
  DJNZ dissolve_sprite_1  ;
  LD A,$00                ; On to the next row (the LD's operand is patched by
  ADD A,L                 ; erase_old_draw_new)
  LD L,A                  ;
  DEC C                   ;
  JR NZ,dissolve_sprite_0 ;
; This entry point is used by the routine at erase_old_draw_new.
dissolve_sprite_2:
  LD DE,(piece_page_x)    ; The copy back: the screen address of the box, and
  LD HL,$F800             ; the buffer
  LD A,(sprite_left_clip) ; Straddling the left edge: copy just the part right
  AND A                   ; of it, as draw_sprite_pair worked out
  JR Z,dissolve_sprite_3  ;
  LD A,($A531)            ;
  LD L,A                  ;
  JP dissolve_sprite_7    ;
dissolve_sprite_3:
  LD A,(piece_dx)         ; B is the box's width in pixels
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  LD A,(piece_x)          ; Does the box start at column 0?
  AND A                   ;
  JR Z,dissolve_sprite_4  ;
  LD C,A                  ; Does it reach past x=$E8, the right edge of the
  ADD A,$17               ; play area? If not, copy all of it (copy_box_out)
  ADD A,B                 ;
  JP NC,copy_box_out      ;
  JR dissolve_sprite_5
dissolve_sprite_4:
  INC E                   ; From column 0: leave the frame column out, and copy
  INC L                   ; the rest
  LD A,(piece_dx)         ;
  DEC A                   ;
  JR dissolve_sprite_6    ;
dissolve_sprite_5:
  LD A,$EE                ; Past the right edge: copy only the bytes left of
  SUB C                   ; x=$E8
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
dissolve_sprite_6:
  LD B,A                  ; Patch the step and the DJNZ for that many bytes
  LD A,(piece_dx)         ;
  SUB B                   ;
  LD ($A531),A            ;
  LD A,B                  ;
  XOR $07                 ;
  DEC A                   ;
  ADD A,A                 ;
  ADD A,$03               ;
  LD ($A520),A            ;
dissolve_sprite_7:
  LD C,$FF                ; C $FF; B the rows plus one
  LD A,(piece_rise)       ;
  LD B,A                  ;
; This entry point is used by the routine at copy_out_part_row.
dissolve_sprite_8:
  PUSH DE                 ; Keep the row's screen address; copy a row
  DJNZ dissolve_sprite_8  ; (copy_out_part_row) or finish
  JP copy_out_done        ;

; Copy part of a row from the buffer to the screen
;
; The LDI chain of the clipped copy back (from the DJNZ at the end of
; dissolve_sprite), for a box cut by an edge of the play area.
copy_out_part_row:
  LDI                     ; Up to six bytes from the buffer to the screen
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LD A,$00                ; Skip the rest of the buffer's row (the LD's operand
  ADD A,L                 ; is patched)
  LD L,A                  ;
  POP DE                  ; Back to the row's start on the screen, up one pixel
  DEC D                   ; line, and round again
  LD A,D                  ;
  INC A                   ;
  AND $07                 ;
  JP NZ,dissolve_sprite_8 ;
  LD A,E                  ;
  SUB $20                 ;
  LD E,A                  ;
  JP C,dissolve_sprite_8  ;
  LD A,D                  ;
  ADD A,$08               ;
  LD D,A                  ;
  JP dissolve_sprite_8    ;

; Copy the whole box from the buffer to the screen
;
; Used by the routine at dissolve_sprite.
;
; DE The screen address of the box's bottom-left byte
; HL The buffer
copy_box_out:
  LD A,(piece_rise)       ; B the rows plus one; C $FF
  LD B,A                  ;
  LD C,$FF                ;
; This entry point is used by the routine at copy_out_row.
copy_box_out_0:
  PUSH DE                 ; Keep the row's screen address; copy a row (the
copy_box_out_1:
  DJNZ copy_box_out_1     ; DJNZ's displacement patched by copy_box_in) or
  JP copy_out_done        ; finish

; Copy a row of the box from the buffer to the screen
;
; Reached from copy_box_out by the DJNZ there, whose displacement copy_box_in
; patches so that it lands on the LDI that copies just the box's width: one LDI
; per byte, up to seven. Then the row's screen address comes back off the
; stack, moves up a pixel line (crossing a character row or a third of the
; screen as needed), and the loop goes round again at copy_box_out's DJNZ.
copy_out_row:
  LDI                     ; Up to seven bytes
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  LDI                     ;
  POP DE                  ; Back to the row's start, up one pixel line, and
  DEC D                   ; round again
  LD A,D                  ;
  INC A                   ;
  AND $07                 ;
  JP NZ,copy_box_out_0    ;
  LD A,E                  ;
  SUB $20                 ;
  LD E,A                  ;
  JP C,copy_box_out_0     ;
  LD A,D                  ;
  ADD A,$08               ;
  LD D,A                  ;
  JP copy_box_out_0       ;

; Finish the copy back
;
; Used by the routines at dissolve_sprite and copy_box_out.
;
; Drops the screen address the last time round the copy loop left on the stack,
; and returns from draw_sprite_pair.
copy_out_done:
  POP DE
  RET

; Turn a sprite position into an address in the buffer
;
; Used by the routine at erase_old_draw_new.
;
; The buffer holds the box bottom row first, each row the box's width: a
; position's byte is (x less the box's left edge) / 8, plus (the box's bottom
; line less y) rows.
;
;   L x
;   H y
; O:HL The byte in the buffer, page $F8
buffer_address:
  LD A,(piece_x)          ; The byte within the row
  LD C,A                  ;
  LD A,L                  ;
  SUB C                   ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD L,A                  ;
  LD A,(piece_y)          ; Add a row's width for each line above the box's
  SUB H                   ; bottom
  JR Z,buffer_address_1   ;
  LD C,A                  ;
  LD A,(piece_dx)         ;
  LD H,A                  ;
buffer_address_0:
  LD A,L                  ;
  ADD A,H                 ;
  LD L,A                  ;
  DEC C                   ;
  JR NZ,buffer_address_0  ;
buffer_address_1:
  LD H,$F8
  RET

; Rub out the old image and draw the new one, straight on the screen
;
; Used by the routines at draw_carried_piece, draw_ship_image, draw_trail,
; side_jet and lift_jet.
;
; The other sprite drawer, for the player's ship (draw_ship), the carried
; rocket part (draw_carried_piece) and the lift jet (lift_jet): no buffer, but
; the rows of the old image's erase and the new image's draw interleaved -- one
; row rubbed out, one drawn, both moving up the screen -- so no part of the
; ship is missing for long. The images are byte-aligned (no shift tables); a
; ship facing left is drawn right to left through the bit-reversing table on
; page $F9 (direct_erase_row_mirrored, direct_draw_row_mirrored). Erase is AND
; NOT the graphic, draw is OR, as in draw_sprite_pair.
;
; The old image is in the alternate register set (HL' its screen address, DE'
; its graphic, IY its row routine, its rows in scratch_count), the new in the
; main set (HL, DE, IX, its rows in ground_cell). When their bottom lines
; (direct_old_line, direct_new_line) differ, the rows below the higher one's
; bottom are done first on their own, so that the two then move up in step.
;
; HL The new image's screen address
; DE The new image's graphic
; IX The new image's draw-row routine
; HL' The old image's screen address
; DE' The old image's graphic
; IY The old image's erase-row routine
draw_direct_pair:
  LD A,L                    ; Keep each image's column, to come back to at
  LD (sprite_width_drawn),A ; every row
  EXX                       ;
  LD A,L                    ;
  LD (sprite_width),A       ;
  EXX                       ;
  LD A,(ground_cell)      ; A' counts the new image's rows
  EX AF,AF'               ;
  LD A,(scratch_count)    ; Either image absent, or both on the same bottom
  AND A                   ; line: interleave all the rows
  JP Z,direct_next_pair   ;
  LD A,(ground_cell)      ;
  AND A                   ;
  JP Z,direct_next_pair   ;
  LD A,(direct_old_line)  ;
  LD C,A                  ;
  LD A,(direct_new_line)  ;
  SUB C                   ;
  JP Z,direct_next_pair   ;
  JR NC,draw_direct_pair_1
  NEG                     ; The old image is lower: its rows below the new
  LD C,A                  ; one's bottom are rubbed out alone first
  LD A,(scratch_count)    ;
  SUB C                   ;
  LD (direct_old_line),A  ;
  JR C,draw_direct_pair_0 ;
  LD A,C                  ;
  LD (scratch_count),A    ;
draw_direct_pair_0:
  XOR A                   ; No new rows meanwhile
  EX AF,AF'               ;
  CALL direct_next_pair   ;
  EXX                     ; Then the rest of both, interleaved
  LD A,(direct_old_line)  ;
  LD (scratch_count),A    ;
  LD A,(ground_cell)      ;
  EX AF,AF'               ;
  JP direct_next_pair     ;
draw_direct_pair_1:
  LD C,A                  ; The new image is lower: its rows below the old
  LD A,(ground_cell)      ; one's bottom are drawn alone first
  SUB C                   ;
  LD (direct_new_line),A  ;
  JR C,draw_direct_pair_2 ;
  LD A,C                  ;
  EX AF,AF'               ;
draw_direct_pair_2:
  LD A,(scratch_count)    ; No old rows meanwhile
  LD (direct_old_line),A  ;
  XOR A                   ;
  LD (scratch_count),A    ;
  CALL direct_next_pair   ;
  LD A,(direct_old_line)  ; Then the rest of both
  LD (scratch_count),A    ;
  LD A,(direct_new_line)  ;
  EX AF,AF'               ;
  JP direct_next_pair     ;

; Rub out another row of the old image, the new one being finished
;
; Used by the routine at direct_then_new_row.
direct_old_rows_only:
  EX AF,AF'
  LD A,(scratch_count)
  DEC A
  RET M
  LD (scratch_count),A
  JP (IY)

; Rub out a row of the old image, then draw a row of the new
;
; Used by the routines at draw_direct_pair and direct_draw_one.
;
; The loop of draw_direct_pair: rub out a row of the old image (through IY, in
; the alternate set), then draw a row of the new (through IX, in the main set);
; when the old image's rows run out, draw the new one's alone
; (direct_new_rows_only), and when the new one's do, rub out the old one's
; alone (direct_old_rows_only).
direct_next_pair:
  LD A,(scratch_count)
  DEC A
  JP M,direct_new_rows_only
  LD (scratch_count),A
  EXX
  JP (IY)

; Rub a row of a 6-byte-wide image off the screen
;
; One row of the old image in draw_direct_pair, AND NOT each byte, left to
; right, in six byte-steps that run on into each other (direct_erase_row,
; direct_erase_five, direct_erase_four, direct_erase_three, direct_erase_two,
; direct_erase_one); then the screen address goes back to the image's column
; and up a line. IY enters at the step for the image's width: here for six
; bytes.
direct_erase_row:
  LD A,(DE)               ; The first byte, ANDed with its complement
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Rub a row of a 5-byte-wide image off the screen
;
; The entry into direct_erase_row's row for an image five bytes wide.
direct_erase_five:
  LD A,(DE)               ; Rub out a byte
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Rub a row of a 4-byte-wide image off the screen
;
; The entry into direct_erase_row's row for an image four bytes wide.
direct_erase_four:
  LD A,(DE)               ; Rub out a byte
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Rub a row of a 3-byte-wide image off the screen
;
; The entry into direct_erase_row's row for an image three bytes wide.
direct_erase_three:
  LD A,(DE)               ; Rub out a byte
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Rub a row of a 2-byte-wide image off the screen
;
; The entry into direct_erase_row's row for an image two bytes wide.
direct_erase_two:
  LD A,(DE)               ; Rub out a byte
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Rub a row of a 1-byte-wide image off the screen, and move up a line
;
; The last byte-step of direct_erase_row's row, and the row's end, which the
; mirrored erase (direct_erase_row_mirrored) shares.
direct_erase_one:
  LD A,(DE)               ; Rub out the last byte
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
; This entry point is used by the routine at mirrored_erase_one.
direct_erase_one_0:
  LD A,(sprite_width)       ; Back to the image's column, up one pixel line
  LD L,A                    ; (keeping the new column when a character row is
  DEC H                     ; crossed), then a row of the new image
  LD A,H                    ;
  INC A                     ;
  AND $07                   ;
  JP NZ,direct_then_new_row ;
  LD A,L                    ;
  SUB $20                   ;
  LD L,A                    ;
  LD (sprite_width),A       ;
  JP C,direct_then_new_row  ;
  LD A,H                    ;
  ADD A,$08                 ;
  LD H,A                    ;
  JP direct_then_new_row    ;

; Draw another row of the new image, the old one being finished
;
; Used by the routine at direct_next_pair.
direct_new_rows_only:
  EX AF,AF'
  DEC A
  RET M
  EX AF,AF'
  JP (IX)

; Count a row of the new image and draw it
;
; Used by the routine at direct_erase_one.
;
; After a row of the old image: if the new image has rows left, swap to its
; registers and draw one; if not, carry on with the old image alone
; (direct_old_rows_only).
direct_then_new_row:
  EX AF,AF'
  DEC A
  JP M,direct_old_rows_only
  EX AF,AF'
  EXX
  JP (IX)

; Draw a row of a 6-byte-wide image on the screen
;
; One row of the new image in draw_direct_pair, ORed onto the screen left to
; right in six byte-steps (direct_draw_row, direct_draw_five, direct_draw_four,
; direct_draw_three, direct_draw_two, direct_draw_one); then back to the
; image's column and up a line. IX enters at the step for the image's width:
; here for six bytes.
direct_draw_row:
  LD A,(DE)               ; The first byte ORed on
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Draw a row of a 5-byte-wide image on the screen
;
; The entry into direct_draw_row's row for an image five bytes wide.
direct_draw_five:
  LD A,(DE)               ; OR on a byte
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Draw a row of a 4-byte-wide image on the screen
;
; The entry into direct_draw_row's row for an image four bytes wide.
direct_draw_four:
  LD A,(DE)               ; OR on a byte
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Draw a row of a 3-byte-wide image on the screen
;
; The entry into direct_draw_row's row for an image three bytes wide.
direct_draw_three:
  LD A,(DE)               ; OR on a byte
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Draw a row of a 2-byte-wide image on the screen
;
; The entry into direct_draw_row's row for an image two bytes wide.
direct_draw_two:
  LD A,(DE)               ; OR on a byte
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;

; Draw a row of a 1-byte-wide image on the screen, and move up a line
;
; The last byte-step of direct_draw_row's row, and the row's end, which the
; mirrored draw (direct_draw_row_mirrored) shares.
direct_draw_one:
  LD A,(DE)               ; OR on the last byte
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
; This entry point is used by the routine at mirrored_draw_one.
direct_draw_one_0:
  LD A,(sprite_width_drawn) ; Back to the column, up one pixel line, then a row
  LD L,A                    ; of the old image
  DEC H                     ;
  LD A,H                    ;
  INC A                     ;
  AND $07                   ;
  JP NZ,direct_next_pair    ;
  LD A,L                    ;
  SUB $20                   ;
  LD L,A                    ;
  LD (sprite_width_drawn),A ;
  JP C,direct_next_pair     ;
  LD A,H                    ;
  ADD A,$08                 ;
  LD H,A                    ;
  JP direct_next_pair       ;

; Rub a byte-aligned row out of the buffer
;
; One row of the old image in draw_sprite_pair: each byte of the graphic,
; complemented, ANDed into the buffer. Entered so many steps from the end as
; the graphic is wide (erase_row_routines). The first step, for a graphic six
; bytes wide, was never run in the build's sessions.
erase_aligned_row:
  LD A,(DE)               ; Up to six bytes
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,$00                ; To the next row up (the step is patched by
  ADD A,L                 ; erase_old_draw_new)
  LD L,A                  ;
  EX AF,AF'                 ; Count the row; when they are done, go and draw
  DEC A                     ; the new image
  JP Z,erase_old_draw_new_1 ;
  EX AF,AF'                 ;
  JP (IY)                   ;

; Rub a shifted row out of the buffer
;
; One row of a shifted old image: each graphic byte is looked up in the shift
; tables (B their page, C the byte), the part that stays in this byte at B and
; the part that falls into the next at B+1, each complemented and ANDed in. So
; a row of n graphic bytes covers n+1 bytes. About 79 T-states a graphic byte.
erase_row_shifted:
  LD A,(DE)               ; Up to five bytes, each split over two
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  CPL                     ;
  DEC B                   ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  CPL                     ;
  DEC B                   ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  CPL                     ;
  DEC B                   ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  CPL                     ;
  DEC B                   ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,$00                ; To the next row up (the step is patched)
  ADD A,L                 ;
  LD L,A                  ;
  EX AF,AF'                 ; Count the row; when they are done, draw the new
  DEC A                     ; image
  JP Z,erase_old_draw_new_1 ;
  EX AF,AF'                 ;
  JP (IY)                   ;

; Draw a byte-aligned row into the buffer
;
; One row of the new image in draw_sprite_pair, ORed in, entered through
; draw_row_routines. The first step, for a graphic six bytes wide, was never
; run in the build's sessions.
draw_aligned_row:
  LD A,(DE)               ; Up to six bytes
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  INC L                   ;
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,$00                ; To the next row up (the step is patched)
  ADD A,L                 ;
  LD L,A                  ;
  EX AF,AF'               ; Count the row; when they are done, go on to the
  DEC A                   ; dissolve and the copy back
  JP Z,dissolve_sprite    ;
  EX AF,AF'               ;
  JP (IX)                 ;

; Draw a shifted row into the buffer
;
; As erase_row_shifted, ORing the two parts of each byte in. About 71 T-states
; a graphic byte.
draw_row_shifted:
  LD A,(DE)               ; Up to five bytes, each split over two
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC B                   ;
  INC L                   ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC B                   ;
  INC DE                  ;
  LD A,$00                ; To the next row up (the step is patched)
  ADD A,L                 ;
  LD L,A                  ;
  EX AF,AF'               ; Count the row; when they are done, go on
  DEC A                   ;
  JP Z,dissolve_sprite    ;
  EX AF,AF'               ;
  JP (IX)                 ;

; Rub a row of a mirrored 6-byte-wide image off the screen
;
; As direct_erase_row, for the ship facing left: each graphic byte is reversed
; through the table on page $F9 (B is $F9) and the row is worked right to left,
; in six byte-steps (direct_erase_row_mirrored, mirrored_erase_five,
; mirrored_erase_four, mirrored_erase_three, mirrored_erase_two,
; mirrored_erase_one), entered by width (mirror_ship_sprite and other callers).
direct_erase_row_mirrored:
  LD A,(DE)               ; The first byte, reversed and ANDed out
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Rub a row of a mirrored 5-byte-wide image off the screen
;
; The entry into direct_erase_row_mirrored's row for an image five bytes wide.
mirrored_erase_five:
  LD A,(DE)               ; Rub out a byte
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Rub a row of a mirrored 4-byte-wide image off the screen
;
; The entry into direct_erase_row_mirrored's row for an image four bytes wide.
mirrored_erase_four:
  LD A,(DE)               ; Rub out a byte
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Rub a row of a mirrored 3-byte-wide image off the screen
;
; The entry into direct_erase_row_mirrored's row for an image three bytes wide.
mirrored_erase_three:
  LD A,(DE)               ; Rub out a byte
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Rub a row of a mirrored 2-byte-wide image off the screen
;
; The entry into direct_erase_row_mirrored's row for an image two bytes wide.
mirrored_erase_two:
  LD A,(DE)               ; Rub out a byte
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Rub a row of a mirrored 1-byte-wide image off the screen
;
; The last byte-step of direct_erase_row_mirrored's row; the row ends in
; direct_erase_one.
mirrored_erase_one:
  LD A,(DE)               ; Rub out the last byte
  LD C,A                  ;
  LD A,(BC)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC DE                  ;
  JP direct_erase_one_0   ; Up a line, then a row of the new image

; Draw a row of a mirrored 6-byte-wide image on the screen
;
; As direct_draw_row, mirrored as direct_erase_row_mirrored, in six byte-steps
; (direct_draw_row_mirrored, mirrored_draw_five, mirrored_draw_four,
; mirrored_draw_three, mirrored_draw_two, mirrored_draw_one), entered by width
; (set_mirrored_drawer and other callers).
direct_draw_row_mirrored:
  LD A,(DE)               ; The first byte, reversed and ORed on
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Draw a row of a mirrored 5-byte-wide image on the screen
;
; The entry into direct_draw_row_mirrored's row for an image five bytes wide.
mirrored_draw_five:
  LD A,(DE)               ; OR on a byte
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Draw a row of a mirrored 4-byte-wide image on the screen
;
; The entry into direct_draw_row_mirrored's row for an image four bytes wide.
mirrored_draw_four:
  LD A,(DE)               ; OR on a byte
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Draw a row of a mirrored 3-byte-wide image on the screen
;
; The entry into direct_draw_row_mirrored's row for an image three bytes wide.
mirrored_draw_three:
  LD A,(DE)               ; OR on a byte
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Draw a row of a mirrored 2-byte-wide image on the screen
;
; The entry into direct_draw_row_mirrored's row for an image two bytes wide.
mirrored_draw_two:
  LD A,(DE)               ; OR on a byte
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  DEC L                   ;
  INC DE                  ;

; Draw a row of a mirrored 1-byte-wide image on the screen
;
; The last byte-step of direct_draw_row_mirrored's row; the row ends in
; direct_draw_one.
mirrored_draw_one:
  LD A,(DE)               ; OR on the last byte
  LD C,A                  ;
  LD A,(BC)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC DE                  ;
  JP direct_draw_one_0    ; Up a line, then a row of the old image

; Wait
;
; Used by the routines at planet_secured, menu_loop, pause_delay, game,
; game_over, level_done and swap_players.
;
; A delay loop: D times 255 turns of DEC E / JR NZ, about 4,100 T-states for
; each unit of D (D=0 counts 256).
;
; D How long
delay:
  LD E,$FF
delay_0:
  DEC E
  JR NZ,delay_0
  DEC D
  JR NZ,delay
  RET

; The ground-object records
;
; Six records of 26 bytes for the things lying on the landscape: these four for
; the objects placed from the map (map_object_kinds) -- the installations that
; can be shot, collided with, landed on, mined -- and two (pickup_record,
; second_pickup_record) for the pick-ups, which have bit 7 of +3 set.
; update_ground_objects updates and draws them, ship_hits_ground_objects tests
; the ship against these four, beam_hits_ground_object the special beam against
; them. The map code (load_map_window) fills them as the view reaches them.
;
; The fields, as far as this range uses them (the first $18 bytes are the same
; as the sky objects' records at object_records, and the same routines place
; and draw both):
;
; +0 the graphic number ($FF a free record), the index into the graphic table
; (the words from the 25th byte of object_handlers). +1 the page column and +2
; the x within it (bit 0 ignored); +4 the page row and +5 the y of the bottom
; row within it -- see place_on_screen for the pages. +3 bit 7: a pick-up. +7
; to +$0A and +$0B, +$0C, +$0E, +$0F: cleared when the record is freed
; (free_object; movement, not used here). +$0D the colours, two attributes in
; its nibbles, shown on alternate passes (prepare_draw). +$10 flags: bit 7
; drawn on the screen last pass, bit 6 drawn straddling the left edge, bit 4 to
; be rubbed out and freed. +$11 and +$12 the screen x and y it was drawn at.
; +$13 bits 0-3 the frame of the graphic, which for graphic $0D is also the
; object's kind (from map_object_kinds; kind $0B is never collided with); for
; graphic $1B, the animated objects, the frame byte from object_part_pairs --
; bits 4-5 a count and 6-7 its reload, with +$14 the frames it runs through
; (animate_object) -- and frames 4 and 5 are the fuel station's (map kind $0E),
; the refuel pad of ship_hits_ground_objects. +$15 bit 7: animate_object
; animates it. +$17 bit 3 dissolving and bits 0-2 the step. +$18 bits 1-3 not
; zero: the kind of shot it fires (ground_object_fires); +$18 bit 0 and +$19
; the number of its byte in map_object_kinds, where it is marked gone.
ground_objects:
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; One record every
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; two lines
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;

; The first pick-up record
;
; Laid out as ground_objects, with bit 7 of +3 set: a pick-up -- an add-on for
; the ship, a rocket part, or the slab and the spire of the load let go
; (carried_part) -- taken by touching it (collect_pickup) rather than shot or
; bumped. Filled by the map code (load_map_window) and place_dropped_part.
pickup_record:
  DEFB $FF,$00,$00,$80,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; The record
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ;

; The second pick-up record
;
; As pickup_record. Its 26 bytes run two past this block: its +$18 and +$19 are
; the first two bytes of the routine load_ore. Those two fields are read only
; for records without bit 7 of +3, so for a pick-up they are never looked at.
; collect_pickup empties it (bit 4 of +$10) when it takes the first piece of a
; part let go.
second_pickup_record:
  DEFB $FF,$00,$00,$80,$00,$00,$00,$00,$00,$00,$00,$00 ; The record, less its
  DEFB $00,$FF,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ; last two bytes

; Load ore into the carried ore tank
;
; Used by the routine at draw_shot.
;
; Only while the rocket part the ship carries (+$1A of the player's record
; score) is part 6, the ore tank: adds one or two units (one for an ore kind 8
; or 9, two for kind $0A, from bit 1 of A) to the ore aboard (the byte after
; the ship record in bare_ship), at most 18 -- a full tank, which drop_part
; waits for before letting the part go; clears the new rows on the screen, two
; bytes at x=$70 above the old level from the ship's bottom line; and redraws
; the level in the ship's picture (the entry at the eighth instruction from the
; end of the clearing): the ore's rows, bottom up, from the 2-byte-wide graphic
; after rocket_tank_graphic into bytes 2 and 3 of each row of ship_picture.
;
; Called with the special beam's record by draw_shot, which carries the ore
; kind in +$13 (beam_hits_ground_object), and with A by test_player_shots at
; the second instruction. Only the redraw ran in the build's sessions (from
; build_ship_sprite and start_landscape).
;
; A bug, read only: the clearing loop moves DE up a line (line_up_de) but
; writes through HL, so it clears the same two bytes every time.
;
; IX A record whose +$13 not zero lets it run
load_ore:
  LD A,(IX+$13)           ; Nothing to do if +$13 is zero
  AND A                   ;
  RET Z                   ;
; This entry point is used by the routine at test_player_shots.
load_ore_0:
  AND $02                 ; One unit for A=0 or 1 (with the kind's bits 0-3: 8
  SRL A                   ; or 9), two for 2 or 3 ($0A)
  INC A                   ;
  LD B,A                  ;
  LD A,(rocket_part_carried) ; Only while the ore tank is carried
  CP $06                     ;
  RET NZ                     ;
  LD A,(ore)              ; Load it, to 18 units at most
  LD E,A                  ;
  ADD A,B                 ;
  CP $12                  ;
  JR C,load_ore_1         ;
  LD A,$12                ;
load_ore_1:
  LD (ore),A              ;
  LD A,(ship_screen_y)    ; The screen address of the first new row in the
  SUB E                   ; ship, at x=$70
  LD H,A                  ;
  LD L,$70                ;
  CALL pixel_to_screen    ;
load_ore_2:
  LD (HL),$00             ; Clear two bytes for each new row (but see above)
  INC L                   ;
  LD (HL),$00             ;
  DEC L                   ;
  CALL line_up_de         ;
  DJNZ load_ore_2         ;
; This entry point is used by the routines at build_ship_sprite and
; start_landscape.
load_ore_3:
  LD HL,$F6BB             ; The entry from build_ship_sprite and
  LD DE,ore_rows_graphic  ; start_landscape: draw the ore aboard into the
  LD A,(ore)              ; ship's picture
  AND A                   ;
  RET Z                   ;
  LD B,A                  ; Two bytes a row from the ore graphic into bytes 2
load_ore_4:
  LD A,(DE)               ; and 3 of each six-byte row of the picture
  LD (HL),A               ;
  INC DE                  ;
  INC HL                  ;
  LD A,(DE)               ;
  LD (HL),A               ;
  INC DE                  ;
  LD A,$05                ;
  CALL add_a_to_hl        ;
  DJNZ load_ore_4         ;
  RET

; Test the ship against the ground objects
;
; Used by the routine at ship_pass.
;
; Once a pass (ship_pass). The ship's box is 40 pixels wide from x=$64 and as
; high as its picture, at its y, both less this pass's scroll steps
; (scroll_down: across, up and down). Each of the first four ground-object
; records that is on the screen, between line $28 and the bottom and between
; x=$30 and $A0, and not of kind $0B, is boxed by its last drawn position and
; its graphic's size, and tested (boxes_overlap). At the first one that
; touches:
;
; A refuel pad -- graphic $1B in frame 4 or 5, the fuel station's two frames
; (map kind $0E, object_part_pairs) -- fills the fuel (fuel), is used up (freed
; next pass and marked gone in map_object_kinds), and repairs the ship: the hit
; count (hit_count) is set to $FF before calling the damage handler in
; contact_damage, whose count then wraps to 0 and colours the ship white again.
; This branch never ran in the build's sessions.
;
; Anything else is solid: the collision flag trail_collision is set (so
; draw_ship redraws the ship's trail), and the ship bounces -- across
; (bounce_across) if the object lies beyond either side of the ship, up or down
; (bounce_up_down) if the ship's top is level with or below the object's
; bottom, and again up or down if, while the vertical scroll step is not
; positive, the ship's bottom is within nine lines below the object's top.
ship_hits_ground_objects:
  LD A,(launch_state)     ; Not once the rocket is launching (launch_state)
  AND A                   ;
  RET NZ                  ;
  LD B,$04                ; The four records
  LD IX,ground_objects    ;
  LD HL,(ship_screen_x)   ; The ship's box: x=$64 and y less the scroll steps
  LD L,$64                ;
  LD A,(scroll_across)    ;
  NEG                     ;
  ADD A,L                 ;
  LD L,A                  ;
  LD A,(scroll_down)      ;
  NEG                     ;
  ADD A,H                 ;
  LD H,A                  ;
  LD (box_x),HL           ;
  LD D,$28                 ; 40 pixels wide, the ship's height tall (kept in
  LD A,(ship_height)       ; collision_object)
  LD E,A                   ;
  LD (collision_object),DE ;
ship_hits_ground_objects_0:
  PUSH BC
  PUSH IX
  LD A,(IX+$00)                   ; A free record
  INC A                           ;
  JP Z,ship_hits_ground_objects_1 ;
  LD A,(IX+$13)                   ; Kind $0B is never collided with
  AND $0F                         ;
  CP $0B                          ;
  JP Z,ship_hits_ground_objects_1 ;
  BIT 7,(IX+$10)                  ; Not on the screen
  JP Z,ship_hits_ground_objects_1 ;
  LD A,(IX+$12)                    ; Only from line $28 down and between x=$30
  CP $28                           ; and $A0
  JP C,ship_hits_ground_objects_1  ;
  LD H,A                           ;
  LD A,(IX+$11)                    ;
  CP $30                           ;
  JP C,ship_hits_ground_objects_1  ;
  CP $A0                           ;
  JP NC,ship_hits_ground_objects_1 ;
  LD L,A                    ; Box the object: where it was drawn, its graphic's
  LD (collision_box),HL     ; width in pixels and height
  CALL object_graphic       ;
  LD A,(DE)                 ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,A                   ;
  LD (collision_box_size),A ;
  INC DE                    ;
  LD A,(DE)                 ;
  LD ($ABA7),A              ;
  LD HL,(box_x)            ; Test the two boxes
  LD DE,(collision_object) ;
  LD (current_object),IX   ;
  LD IX,collision_box      ;
  CALL boxes_overlap       ;
  LD A,(scratch_count)             ; Touching?
  AND A                            ;
  JR NZ,ship_hits_ground_objects_2 ;
ship_hits_ground_objects_1:
  POP IX                           ; Next record
  LD BC,$001A                      ;
  ADD IX,BC                        ;
  POP BC                           ;
  DEC B                            ;
  JP NZ,ship_hits_ground_objects_0 ;
  RET                              ;
ship_hits_ground_objects_2:
  LD IY,(current_object)           ; A refuel pad, graphic $1B in frame 4 or 5
  LD A,(IY+$00)                    ; (the fuel station)?
  CP $1B                           ;
  JR NZ,ship_hits_ground_objects_4 ;
  LD A,(IY+$13)                    ;
  AND $0F                          ;
  CP $04                           ;
  JR Z,ship_hits_ground_objects_3  ;
  CP $05                           ;
  JR NZ,ship_hits_ground_objects_4 ;
ship_hits_ground_objects_3:
  LD HL,$4200             ; Fill the fuel
  LD (fuel),HL            ;
  SET 4,(IY+$10)          ; Free the pad next pass
  LD A,(IY+$18)           ; And mark it gone in the world's object table
  AND $01                 ;
  LD H,A                  ;
  LD L,(IY+$19)           ;
  LD DE,map_object_kinds  ;
  ADD HL,DE               ;
  SET 7,(HL)              ;
  LD A,$FF                      ; Repair the ship: the hit count wraps to zero
  LD (hit_count),A              ; in the damage handler, which colours the ship
  CALL damage_ship              ; white
  JP ship_hits_ground_objects_5 ;
ship_hits_ground_objects_4:
  LD A,$FF                ; Something solid was hit
  LD (trail_collision),A  ;
  LD A,(IX+$00)           ; The object's box (IX is the box now) beyond x=$8B
  CP $8B                  ; or ending short of x=$65: bounce across
  CALL NC,bounce_across   ;
  LD A,(IX+$00)           ;
  ADD A,(IX+$02)          ;
  CP $65                  ;
  CALL C,bounce_across    ;
  LD A,(ship_screen_y)    ; The ship's top level with the object's bottom or
  LD H,A                  ; lower: bounce up or down
  SUB E                   ;
  INC A                   ;
  CP (IX+$01)             ;
  CALL NC,bounce_up_down  ;
  LD A,(scroll_down)               ; Not while the vertical scroll step is
  NEG                              ; positive
  BIT 7,A                          ;
  JR NZ,ship_hits_ground_objects_5 ;
  LD A,(IX+$01)           ; The ship's bottom within nine lines below the
  SUB (IX+$03)            ; object's top: bounce up or down
  ADD A,$09               ;
  CP H                    ;
  CALL NC,bounce_up_down  ;
ship_hits_ground_objects_5:
  POP IX
  POP BC
  RET

; Bounce the ship across
;
; Used by the routine at ship_hits_ground_objects.
;
; Reverses and halves the ship's step across (+7 of the ship record, ship_dx,
; reverse_speed) and reverses this pass's scroll step across (scroll_across).
bounce_across:
  PUSH HL
  LD HL,(ship_dx)
  CALL reverse_speed
  LD (ship_dx),HL
  POP HL
  LD A,(scroll_across)
  NEG
  LD (scroll_across),A
  RET

; Bounce the ship up or down
;
; Used by the routine at ship_hits_ground_objects.
;
; Reverses and halves the ship's step down (+9 of the ship record, ship_dy,
; reverse_speed), and passes the new value to the entry near the end of
; move_ship that moves the ship by it.
bounce_up_down:
  PUSH HL
  LD HL,(ship_dy)
  CALL reverse_speed
  LD (ship_dy),HL
  LD A,H
  LD H,L
  LD L,A
  CALL move_ship_7
  POP HL
  RET

; Reverse and halve a step
;
; Used by the routines at bounce_across and bounce_up_down.
;
; The ship's step words are kept high byte first, so the bytes are swapped
; round either side: negate (negate_hl), halve (halve_hl), and let dead_band
; cut a small result to nothing.
;
;   HL The step word as stored
; O:HL The new step, stored the same way
reverse_speed:
  LD A,H
  LD H,L
  LD L,A
  CALL negate_hl
  CALL halve_hl
  CALL dead_band
  LD A,H
  LD H,L
  LD L,A
  RET

; Hang a rocket part under the ship, or take it off
;
; Used by the routines at draw_ship_image and start_landscape.
;
; The ship's picture is built in RAM at ship_picture, six bytes a row, bottom
; row first, and a rocket part the ship carries hangs in its bottom rows. This
; XORs the part's graphic (from type $1E's frame list rocket_part_frames, by
; the part number in bits 0-3 of the ship record's +$1F) into the picture,
; shifted four pixels right through the shift tables' fourth and fifth pages
; (shift_tables) and one byte in -- so the same call puts it in and takes it
; out -- and clears the same place on the screen (four bytes from a byte in,
; the part's height up from the ship's bottom line), presumably because the
; direct drawer rubs the ship out with its picture as it now is, which would
; leave the old part's pixels behind. Called from draw_ship when bit 7 of +$1F
; asks for it.
;
; Then (also the entry at the 24th instruction, used by build_ship_sprite when
; it rebuilds the picture) the ship's height and the picture's start are set
; for whether a part hangs there (bit 6 of +$1F): five rows more and the
; picture from ship_picture, or five fewer and from 30 bytes into it; the
; ship's line for shots (ship_line) moves by three, and the ship record's
; screen y and position are moved to match (move_object,
; ship_speed_from_scroll).
;
; IX The ship's record (ship_record)
toggle_carried_part:
  LD HL,rocket_part_frames ; The part's graphic
  LD A,(part_being_fitted) ;
  AND $0F                  ;
  LD C,A                   ;
  CALL table_word          ;
  EX DE,HL                 ;
  LD A,(DE)               ; Its width and height; the picture's first row, a
  LD B,A                  ; byte in
  LD (row_count_spare),A  ;
  INC DE                  ;
  LD A,(DE)               ;
  LD (pieces_left),A      ;
  LD C,A                  ;
  LD HL,$F6BA             ;
toggle_carried_part_0:
  PUSH HL                    ; Each byte, shifted four pixels right, XORed into
toggle_carried_part_1:
  PUSH BC                    ; two bytes of the picture
  LD B,$FC                   ;
  INC DE                     ;
  LD A,(DE)                  ;
  LD C,A                     ;
  LD A,(BC)                  ;
  XOR (HL)                   ;
  LD (HL),A                  ;
  INC B                      ;
  INC HL                     ;
  LD A,(BC)                  ;
  XOR (HL)                   ;
  LD (HL),A                  ;
  POP BC                     ;
  DJNZ toggle_carried_part_1 ;
  POP HL                      ; Next row, six bytes on
  LD A,(row_count_spare)      ;
  LD B,A                      ;
  LD A,$06                    ;
  CALL add_a_to_hl            ;
  DEC C                       ;
  JR NZ,toggle_carried_part_0 ;
  LD HL,(ship_screen_x)      ; Clear the part's rows under the ship on the
  CALL pixel_to_screen       ; screen, four bytes a row
  LD A,(pieces_left)         ;
  LD B,A                     ;
  INC L                      ;
toggle_carried_part_2:
  LD (HL),$00                ;
  INC L                      ;
  LD (HL),$00                ;
  INC L                      ;
  LD (HL),$00                ;
  INC L                      ;
  LD (HL),$00                ;
  DEC L                      ;
  DEC L                      ;
  DEC L                      ;
  CALL line_up_hl            ;
  DJNZ toggle_carried_part_2 ;
  LD A,(map_rebuild)      ; Not while the view is being built (map_rebuild)
  AND A                   ;
  RET NZ                  ;
; This entry point is used by the routine at build_ship_sprite.
toggle_carried_part_3:
  LD A,(part_being_fitted) ; A part hangs there: the picture from its first
  LD B,A                   ; byte and five rows more
  LD A,(ship_height)       ;
  LD C,A                   ;
  LD (IX+$09),$08          ;
  LD E,$03                 ;
  LD HL,ship_picture       ;
  LD A,$05                 ;
  BIT 6,B                     ; None: from 30 bytes on and five rows fewer
  JR NZ,toggle_carried_part_4 ;
  LD HL,$F6D7                 ;
  LD E,$FD                    ;
  NEG                         ;
  LD (IX+$09),$F8             ;
toggle_carried_part_4:
  LD (ship_graphic),HL    ; The ship's picture and height (in star_offsets)
  ADD A,C                 ;
  LD (ship_height),A      ;
  LD A,(ship_line)          ; Adjust the ship's shot line and record to match,
  ADD A,E                   ; and recompute its position
  LD (ship_line),A          ;
  LD A,(IX+$09)             ;
  ADD A,(IX+$1E)            ;
  LD (IX+$1E),A             ;
  LD (IX+$07),$00           ;
  CALL move_object          ;
  JP ship_speed_from_scroll ;

; Pick up a pick-up the ship touches
;
; Used by the routine at update_ground_objects.
;
; For each pick-up record (bit 7 of +3) every pass (update_ground_objects). A
; pick-up is taken when it was on the screen last pass, between line $28 and
; the bottom and between x=$30 and $90, and its box touches a narrow box under
; the middle of the ship: 16 pixels from x=$70, the ship's height less ten.
; Then a sound (sound_pick_up), 1000 added to the score (the score adder near
; the end of test_player_shots), the record is emptied (bit 4 of +$10, so its
; image is rubbed out this pass), and:
;
; A rocket part (graphic $1E) is taken only when add-on 1 is fitted (+7 of the
; player's record score) and it is the part due next (its kind against +$1B,
; the parts delivered); its colours become $FA. It is hung under the ship: the
; ship record's +$1F gets the part number with bits 6 and 7 set (draw_ship then
; calls toggle_carried_part), +$1A of the player's record the part carried, and
; the part's 4-byte entry from +$49 of the player's record is marked taken.
;
; Any other pick-up is an add-on: +$1F gets its kind plus one, which draw_ship
; fits (build_ship_sprite), and its entry from +$1D of the player's record is
; marked. Kinds 9 and $0A are the slab and the spire (add-ons 10 and 11), the
; load carried_part puts down: kind $0A, the spire, is never taken; kind 9, the
; slab, only with add-on 9 fitted (+$0F), and taking it clears +$1F and
; carried_part_facing, sets +$10 of the player's record (player_count_nine) to
; 9 (the whole load is carried again) and empties second_pickup_record, which
; holds the spire. Much of this never ran in the build's sessions: the rocket
; parts' tests, their hanging and the kind-9 code.
;
; IX The pick-up's record
collect_pickup:
  LD A,(IX+$00)           ; A rocket part?
  CP $1E                  ;
  JR NZ,collect_pickup_0  ;
  LD A,(addon_slots)      ; Only with add-on 1 fitted and when it is the part
  AND A                   ; due next; recolour it
  RET Z                   ;
  LD A,(IX+$13)           ;
  AND $0F                 ;
  LD B,A                  ;
  LD A,(parts_built)      ;
  CP B                    ;
  RET NZ                  ;
  LD (IX+$0D),$FA         ;
collect_pickup_0:
  LD A,(IX+$13)           ; Kind $0A is never taken; kind 9 only with add-on 9
  AND $0F                 ;
  CP $0A                  ;
  RET Z                   ;
  CP $09                  ;
  JR NZ,collect_pickup_1  ;
  LD A,(addon_nine)       ;
  AND A                   ;
  RET Z                   ;
collect_pickup_1:
  LD A,(old_sprite_rows)  ; Not if it was not on the screen
  AND A                   ;
  RET Z                   ;
  LD ($ABA7),A            ; Its height, and where it was drawn: only from line
  LD HL,(old_sprite_x)    ; $28 down and between x=$30 and $90
  LD A,H                  ;
  CP $28                  ;
  RET C                   ;
  LD A,L                  ;
  CP $30                  ;
  RET C                   ;
  CP $90                  ;
  RET NC                  ;
  LD (collision_box),HL     ; Its box
  LD A,(sprite_width)       ;
  ADD A,A                   ;
  ADD A,A                   ;
  ADD A,A                   ;
  LD (collision_box_size),A ;
  LD HL,(ship_screen_x)   ; The ship's: 16 pixels from x=$70, its height less
  LD L,$70                ; ten
  LD D,$10                ;
  LD A,(ship_height)      ;
  SUB $0A                 ;
  LD E,A                  ;
  CALL test_collision_box ;
  LD A,(scratch_count)    ; Not touching
  AND A                   ;
  RET Z                   ;
  CALL sound_pick_up      ; Taken: a sound
  LD BC,$1000             ; 1000 to the score
  CALL add_score          ;
  LD A,(IX+$00)              ; A rocket part: hang it with bits 6 and 7 set,
  CP $1E                     ; and keep the part number
  JR NZ,collect_pickup_2     ;
  LD A,(IX+$13)              ;
  AND $0F                    ;
  OR $C0                     ;
  LD (part_being_fitted),A   ;
  LD HL,place_eleven         ;
  AND $0F                    ;
  LD (rocket_part_carried),A ;
  JR collect_pickup_3        ;
collect_pickup_2:
  LD A,(IX+$13)            ; An add-on: fit kind+1
  INC A                    ;
  LD (part_being_fitted),A ;
  DEC A                    ;
  LD HL,player_places      ;
collect_pickup_3:
  ADD A,A                 ; Mark it as taken in the player's record
  ADD A,A                 ;
  CALL add_a_to_hl        ;
  SET 7,(HL)              ;
  SET 4,(IX+$10)          ; Rub it out this pass and free the record
  LD (IX+$00),$FF         ;
  LD A,(IX+$13)              ; Kind 9, the slab: carry the load again, and
  CP $09                     ; empty the other pick-up record (the spire)
  RET NZ                     ;
  XOR A                      ;
  LD (part_being_fitted),A   ;
  LD (carried_part_facing),A ;
  LD A,$09                   ;
  LD (player_count_nine),A   ;
  LD A,($A89D)               ;
  OR $10                     ;
  LD ($A89D),A               ;
  LD HL,place_ten            ;
  SET 7,(HL)                 ;
  RET                        ;

; Test a box against the collision box
;
; Used by the routines at beam_hits_ship and collect_pickup.
;
; Calls boxes_overlap with IX at collision_box: the hit flag scratch_count is 1
; if the box given in HL and DE touches it.
;
; L x
; H y of the bottom row
; D Width in pixels
; E Height in rows
test_collision_box:
  PUSH IX
  LD IX,collision_box
  CALL boxes_overlap
  POP IX
  RET

; The collision box's place
;
; A box for boxes_overlap, with collision_box_size: x, then the y of its bottom
; row. Set from a ground object or pick-up (ship_hits_ground_objects,
; collect_pickup), an object for the shot tests (object_box), or the ship
; (draw_beams) before a test.
collision_box:
  DEFB $00,$00

; The collision box's size
;
; Width in pixels, then height in rows, of the box at collision_box.
collision_box_size:
  DEFB $00,$00

; Test the special beam against a ground object
;
; Used by the routine at update_ground_objects.
;
; For each ground object that is not a pick-up, is on the screen, is not
; already dissolving and is not of kind $0B, while the special beam is out
; (special_beam_on): its box is set from where it was drawn (object_box), the
; beam's record found (find_special_beam, which leaves IY at it) and tested
; against the box (the entry near the start of test_player_shots, through
; test_shot_box). Kind $0B (a ringed planet, by its picture) is the one kind
; the beam cannot destroy. If that started the object dissolving (bit 3 of
; +$17), it is marked gone in map_object_kinds. Then, for graphic $0D only: an
; ore deposit (kind 8 to $0A) gives its kind to the beam's +$13, which brings
; the ore back to the tank (load_ore); kinds 6 and 7 set the bit for their map
; square in the player's record (map_square_bit).
;
; IX The object's record
beam_hits_ground_object:
  BIT 7,(IX+$03)          ; Only for a ground object on the screen, not
  RET NZ                  ; dissolving and not of kind $0B, while the beam is
  LD A,(special_beam_on)  ; out
  AND A                   ;
  RET Z                   ;
  BIT 7,(IX+$10)          ;
  RET Z                   ;
  BIT 3,(IX+$17)          ;
  RET NZ                  ;
  LD A,(IX+$13)           ;
  CP $0B                  ;
  RET Z                   ;
  CALL object_box         ; Box it, find the beam, and test
  CALL find_special_beam  ;
  CALL test_shot_box      ;
  BIT 3,(IX+$17)          ; Not hit
  RET Z                   ;
  LD A,(IX+$18)           ; Hit: mark it gone
  AND $01                 ;
  LD H,A                  ;
  LD L,(IX+$19)           ;
  LD DE,map_object_kinds  ;
  ADD HL,DE               ;
  SET 7,(HL)              ;
  LD A,(IX+$00)           ; Graphic $0D only
  CP $0D                  ;
  RET NZ                  ;
  LD A,(IX+$13)                  ; Kinds 8 to $0A, ore: give the kind to the
  CP $08                         ; beam
  JR C,beam_hits_ground_object_0 ;
  CP $0B                         ;
  RET NC                         ;
  LD (IY+$13),A                  ;
  RET                            ;
beam_hits_ground_object_0:
  CP $06                  ; Kinds 6 and 7 only
  RET C                   ;
  LD E,(IX+$01)           ; Set the bit for its map square (the SET's bit
  LD D,(IX+$04)           ; number is patched in)
  CALL map_square_bit     ;
  LD A,B                  ;
  ADD A,$C6               ;
  LD ($AC06),A            ;
  SET 0,(HL)              ;
  RET

; Find a map square's bit in the player's record
;
; Used by the routines at draw_beams and beam_hits_ground_object.
;
; The player's record (score) keeps 24 bits at +3 to +5 (cleared at the end of
; each level, level_done), one for each of twelve page rows and two halves of
; the map across (columns below $30 and from $30), marking the squares whose
; beam generator has been destroyed. This returns the byte and the bit, the bit
; as eight times its number, ready to add into a BIT, SET or RES opcode.
; beam_hits_ground_object sets them and draw_beams tests them.
;
;   D The page row (1-12)
;   E The page column
; O:HL The byte
; O:B Eight times the bit number
map_square_bit:
  LD HL,player_flag_bits  ; The byte: page row less one, divided by four
  LD A,D                  ;
  SUB $01                 ;
  LD D,A                  ;
  AND $0C                 ;
  SRL A                   ;
  SRL A                   ;
  CALL add_a_to_hl        ;
  LD A,D                  ; The bit: twice the remainder...
  AND $03                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  LD A,E                  ; ...plus one for the right half of the map
  CP $30                  ;
  RET C                   ;
  LD A,B                  ;
  ADD A,$08               ;
  LD B,A                  ;
  RET

; Test a shot against the collision box
;
; Used by the routine at beam_hits_ground_object.
;
; Jumps into test_player_shots at its sixth instruction with A=1 and IX at
; collision_box, keeping IX on the stack for the end of that routine.
test_shot_box:
  PUSH IX
  LD IX,collision_box
  LD A,$01
  JP test_shot_list

; Free an object record
;
; Used by the routines at fire_from, start_at_edge, place_at_ship, drop_part,
; fire, place_dropped_part, spawn_objects_from_list and reset_for_life.
;
; Clears the record's movement and state fields and marks it free (+0 = $FF).
; Its position, colours and +$18/+$19 are left as they were.
;
; IX The record
free_object:
  CALL clear_screen_fields
  LD (IX+$07),A
  LD (IX+$08),A
  LD (IX+$09),A
  LD (IX+$0A),A
  LD (IX+$10),A
  LD (IX+$11),A
  LD (IX+$12),A
  LD (IX+$13),A
  LD (IX+$14),A
  LD (IX+$15),A
  LD (IX+$16),A
  LD (IX+$17),A
  LD (IX+$00),$FF
  RET

; Update and draw the ground objects
;
; Used by the routine at scroll_landscape.
;
; Once a pass, from the landscape's drawing (scroll_landscape): before or after
; the landscape is drawn depending on which way the screen is moving up or
; down. For each of the six records at ground_objects in use:
;
; A ground object (not a pick-up) whose +$18 bits 1-3 are set may fire
; (ground_object_fires). The pair of images is started (clear_sprite_pair) and,
; if it was drawn last pass, its erase set up (prepare_erase). animate_object
; animates it if bit 7 of +$15 is set; a pick-up is tested against the ship
; (collect_pickup). An object to be removed is freed and only rubbed out; any
; other is placed for this pass (prepare_draw). The compositor rubs out and
; draws (draw_object_images); a ground object's colour patch is trimmed
; (trim_colour_patch); the colours are put on (colour_sprite); and a ground
; object is tested against the special beam (beam_hits_ground_object).
;
; The ground objects do not move by themselves: their positions in the world
; are fixed, and they move on the screen as the view scrolls (place_on_screen).
update_ground_objects:
  LD A,(map_rebuild)      ; Unless the view is being built (map_rebuild), the
  AND A                   ; rocket first (draw_rocket)
  CALL Z,draw_rocket      ;
  LD IX,ground_objects    ; The six records
  LD B,$06                ;
update_ground_objects_0:
  PUSH BC
  PUSH IX
  LD A,(IX+$00)                ; Free
  CP $FF                       ;
  JR Z,update_ground_objects_4 ;
  BIT 7,(IX+$03)                ; A ground object: let it fire, if +$18 bits
  JR NZ,update_ground_objects_1 ; 1-3 give a kind of shot
  LD A,(IX+$18)                 ;
  AND $0E                       ;
  SRL A                         ;
  LD (scratch_count),A          ;
  CALL NZ,ground_object_fires   ;
update_ground_objects_1:
  CALL clear_sprite_pair  ; Start the pair; set up the erase if it was drawn
  BIT 7,(IX+$10)          ; last pass
  CALL NZ,prepare_erase   ;
  RES 6,(IX+$10)          ; Clear the left-clip mark (prepare_erase has passed
  EXX                     ; it on); keep the erase in the other registers
  BIT 7,(IX+$15)          ; Animate it if bit 7 of +$15
  CALL NZ,animate_object  ;
  BIT 7,(IX+$03)          ; A pick-up: is it taken?
  CALL NZ,collect_pickup  ;
  BIT 4,(IX+$10)               ; To be removed: free it, and draw nothing new
  JR Z,update_ground_objects_2 ;
  LD (IX+$00),$FF              ;
  LD (IX+$10),$00              ;
  JR update_ground_objects_3   ;
update_ground_objects_2:
  CALL prepare_draw       ; Otherwise place it for this pass
update_ground_objects_3:
  CALL draw_object_images ; Rub out and draw
  LD IY,(current_object)   ; A ground object: trim its colour patch
  BIT 7,(IY+$03)           ;
  CALL Z,trim_colour_patch ;
  CALL colour_sprite      ; Colour it
  LD IX,(current_object)       ; Test it against the special beam
  CALL beam_hits_ground_object ;
update_ground_objects_4:
  POP IX                       ; Next record
  LD BC,$001A                  ;
  ADD IX,BC                    ;
  POP BC                       ;
  DJNZ update_ground_objects_0 ;
  RET

; Trim a ground object's colour patch
;
; Used by the routine at update_ground_objects.
;
; For graphic $1B and the kinds 0, 1, 3, $0A and $0C, after the drawing: when
; the new image's bottom row is not the bottom line of a character cell, its
; height (new_sprite_rows) is cut by four and its y (new_sprite_y) moved up
; seven lines, so the colour patch (colour_sprite) stops a character row short
; and does not colour the cells its bottom shares with the ground; none at all
; if that would put it above line $18. Only the colouring reads them now. Why
; four and seven is not yet worked out.
;
; IY The record
trim_colour_patch:
  LD A,(IY+$00)            ; Only these graphics and kinds
  CP $1B                   ;
  JR Z,trim_colour_patch_0 ;
  LD A,(IY+$13)            ;
  AND $0F                  ;
  JR Z,trim_colour_patch_0 ;
  CP $01                   ;
  JR Z,trim_colour_patch_0 ;
  CP $03                   ;
  JR Z,trim_colour_patch_0 ;
  CP $0A                   ;
  JR Z,trim_colour_patch_0 ;
  CP $0C                   ;
  RET NZ                   ;
trim_colour_patch_0:
  LD A,(new_sprite_y)     ; Not when the bottom row is a cell's bottom line
  XOR $07                 ;
  AND $07                 ;
  RET Z                   ;
  LD A,(new_sprite_rows)  ; Four rows fewer
  SUB $04                 ;
  LD (new_sprite_rows),A  ;
  LD A,(new_sprite_y)       ; Seven lines up; no patch at all if that is above
  SUB $07                   ; line $18
  CP $18                    ;
  JR NC,trim_colour_patch_1 ;
  XOR A                     ;
  LD (new_sprite_rows),A    ;
trim_colour_patch_1:
  LD (new_sprite_y),A
  RET

; Rub out and draw an object's images, if it has any
;
; Used by the routines at move_missile, update_attacker, draw_rocket and
; update_ground_objects.
;
; Passes to the compositor draw_sprite_pair unless both the old and the new
; image are absent. The profile charges all of draw_sprite_pair's time to this
; routine, since it jumps there.
draw_object_images:
  LD A,(old_sprite_rows)
  LD C,A
  LD A,(new_sprite_rows)
  OR C
  RET Z
  JP draw_sprite_pair

; Set up the rubbing out of an object's old image
;
; Used by the routines at count_and_erase, move_missile, draw_rocket and
; update_ground_objects.
;
; For an object drawn last pass (bit 7 of +$10): the old image is its graphic,
; as it still is before the update has run, at the screen position it was drawn
; at (+$11, +$12), clipped (old_sprite_header) and given its row routine
; (set_old_sprite). The caller then swaps the registers with EXX so the old
; image's graphic and shift page wait in the alternate set. Bit 6 of +$10
; (drawn straddling the left edge) becomes the compositor's left-clip flag
; (sprite_left_clip).
;
; IX The record
prepare_erase:
  CALL object_graphic     ; The graphic
  LD L,(IX+$11)           ; Where it was drawn
  LD H,(IX+$12)           ;
  LD (old_sprite_x),HL    ;
  CALL old_sprite_header  ; Clip it and choose its row routine
  CALL set_old_sprite     ;
  RES 7,(IX+$10)          ; It is no longer on the screen, until placed again
  LD A,(IX+$10)           ; Whether it straddled the left edge
  AND $40                 ;
  LD (sprite_left_clip),A ;
  RET                     ;

; The object record being drawn
;
; IX of the object whose images are being worked on (prepare_draw), for the
; routines after the drawing that need it back (ship_hits_ground_objects,
; object_box, update_ground_objects).
current_object:
  DEFB $00,$00

; Place an object for this pass and set up its new image
;
; Used by the routines at place_object, move_missile and update_ground_objects.
;
; Works out where the object is on the screen (place_on_screen); if it is
; visible, sets up its new image for the compositor: its graphic clipped
; (new_sprite_header), its row routine (set_new_sprite), and its colour for
; colour_sprite (the third byte of colour_patch), one of the two in +$0D on
; alternate passes (bit 0 of the pass counter in pass_counter): on odd passes
; ink bits 0-2 with BRIGHT from bit 3, on even passes ink bits 4-6 with BRIGHT
; from bit 7, always on black paper, so an object can flicker between two
; colours.
;
; A dissolving object (bit 3 of +$17) counts its step in bits 0-2 of +$17 and
; passes it to dissolve_sprite in dissolve_step; at the eighth step it is freed
; instead.
;
; IX The record
prepare_draw:
  LD (current_object),IX  ; Keep the record; not dissolving yet
  XOR A                   ;
  LD (dissolve_step),A    ;
  CALL object_graphic     ; Its graphic; place it on the screen
  PUSH DE                 ;
  CALL place_on_screen    ;
  POP DE                  ;
  BIT 3,(IX+$17)          ; Dissolving?
  JR Z,prepare_draw_1     ;
  LD A,(IX+$17)           ; Count the step
  AND $07                 ;
  INC A                   ;
  LD B,A                  ;
  CP $08                  ;
  JR NZ,prepare_draw_0    ;
  LD (IX+$00),$FF         ; The eighth: free the record, and draw nothing
  RES 7,(IX+$10)          ;
  JR prepare_draw_1       ;
prepare_draw_0:
  LD A,(IX+$17)           ; Keep the step, and pass it to the dissolve
  AND $F0                 ;
  OR B                    ;
  OR $08                  ;
  LD (IX+$17),A           ;
  LD A,B                  ;
  OR $80                  ;
  LD (dissolve_step),A    ;
prepare_draw_1:
  LD A,(IX+$10)           ; Not visible: nothing to draw
  LD (edge_flags),A       ;
  BIT 7,A                 ;
  RET Z                   ;
  LD C,(IX+$0D)           ; Its colours: which one this pass?
  LD A,(pass_counter)     ;
  AND $01                 ;
  JR Z,prepare_draw_2     ;
  LD A,C                  ; Odd passes: BRIGHT from bit 3, ink from bits 0-2
  AND $08                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  LD A,C                  ;
  AND $07                 ;
  JR prepare_draw_3       ;
prepare_draw_2:
  LD A,C                  ; Even passes: BRIGHT from bit 7, ink from bits 4-6
  AND $70                 ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD B,A                  ;
  LD A,C                  ;
  AND $80                 ;
  SRL A                   ;
prepare_draw_3:
  OR B                    ; The colour for the patch
  LD ($AF03),A            ;
  CALL new_sprite_header  ; Clip the new image and choose its row routine
  JP set_new_sprite       ;

; Find a record's graphic
;
; Used by the routines at ship_hits_ground_objects, prepare_erase and
; prepare_draw.
;
; The graphic number (+0) picks a frame list from the graphic table (the words
; from the 25th byte of object_handlers), and bits 0-3 of +$13 the frame in it.
;
;   IX The record
; O:DE The graphic (width, height, rows)
object_graphic:
  LD C,(IX+$00)
  LD B,$00
  LD HL,object_graphic_lists
  ADD HL,BC
  ADD HL,BC
  LD A,(HL)
  INC HL
  LD H,(HL)
  LD L,A
  LD A,(IX+$13)
  AND $0F
  LD C,A
  ADD HL,BC
  ADD HL,BC
  LD E,(HL)
  INC HL
  LD D,(HL)
  RET

; Work out where an object is on the screen, and whether it can be seen
;
; Used by the routines at start_at_edge and prepare_draw.
;
; The world is a grid of pages the size of the play area, 224 pixels across and
; 160 lines down; an object's position is its page column (+1) and x within
; that page (+2), and its page row (+4) and y within it (+5). The view's page
; column and row are kept in view_column and view_row, and the x and y in those
; pages at the play area's left edge and top in view_x and view_y; the
; landscape's scroll (scroll_landscape) keeps the screen offsets in origin_x
; and origin_y at 8 less the view's x and $B7 less its y, so that the screen x
; is the page x plus the one and the screen y the page y plus the other. The
; page column to the right of the view's is kept in right_map_column.
;
; Across: an object in the view's page is visible from the view's x on, or
; straddling it -- and if it starts nine or more pixels left of it, so that it
; would be off the left of the screen, it is drawn in left-clip mode (+$10 bit
; 6, sprite_left_clip). One in the next page is visible if its x is short of
; the view's. One in the page to the left is kept only if it was drawn last
; pass and lies at the right of its page, when it is worked out as straddling
; the view's left edge (the $30 and $B0 keep the sums in range). Any other is
; freed (+0 = $FF): objects are dropped once they are out of the two pages the
; view spans.
;
; Down: an object in the view's page row is visible if its bottom is below the
; view's top; one in the row below if its top is above the view's bottom. Any
; other is freed. An object that is in range but out of sight returns without
; being marked as on the screen, and so is not drawn this pass. A visible one
; has its screen x and y put in new_sprite_x and new_sprite_y and in +$11 and
; +$12, and bit 7 of +$10 set.
;
; DE The graphic
; IX The record
place_on_screen:
  LD A,(DE)               ; Its width in pixels, and height
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  LD A,(view_x)           ; The view's x; the object's, made even
  LD E,A                  ;
  LD A,(IX+$02)           ;
  AND $FE                 ;
  LD L,A                  ;
  LD A,(right_map_column) ; In the next page to the right?
  CP (IX+$01)             ;
  JP Z,place_on_screen_5  ;
  LD A,(view_column)      ; In the view's page?
  CP (IX+$01)             ;
  JR Z,place_on_screen_0  ;
  LD A,(old_sprite_rows)  ; Neither: free it unless it was drawn last pass
  AND A                   ;
  JP Z,place_on_screen_9  ;
  LD A,L                  ; ...and it reaches the right end of the page to the
  ADD A,$17               ; left
  ADD A,B                 ;
  JP NC,place_on_screen_9 ;
  LD A,(IX+$02)           ;
  CP $80                  ;
  JP C,place_on_screen_9  ;
  LD A,E                  ; Shift both x's to keep the comparison in range
  ADD A,$B0               ;
  LD E,A                  ;
  LD A,L                  ;
  SUB $30                 ;
  LD L,A                  ;
place_on_screen_0:
  LD A,L                  ; From the view's x on: visible
  CP E                    ;
  JR NC,place_on_screen_2 ;
  ADD A,B                 ; Left of the view's x: wholly left of it is out of
  JR C,place_on_screen_1  ; sight
  DEC A                   ;
  CP E                    ;
  RET C                   ;
place_on_screen_1:
  LD A,E                  ; Straddling, by nine pixels or more: left-clip mode
  SUB L                   ;
  SUB $09                 ;
  JR C,place_on_screen_2  ;
  SET 6,(IX+$10)          ;
  LD A,$01                ;
  LD (sprite_left_clip),A ;
place_on_screen_2:
  LD A,(view_column)      ; The page to the left: screen x is x less the view's
  CP (IX+$01)             ; x plus 8
  JR Z,place_on_screen_3  ;
  LD A,(right_map_column) ;
  CP (IX+$01)             ;
  JR Z,place_on_screen_3  ;
  LD A,L                  ;
  SUB E                   ;
  ADD A,$08               ;
  LD L,A                  ;
  JR place_on_screen_4    ;
place_on_screen_3:
  LD A,(origin_x)         ; The view's page and the next: x plus the screen
  ADD A,L                 ; offset
place_on_screen_4:
  LD (new_sprite_x),A     ; The screen x
  LD (IX+$11),A           ;
  JP place_on_screen_6    ;
place_on_screen_5:
  LD A,L                  ; The next page: visible only short of the view's x;
  CP E                    ; 224 pixels on
  RET NC                  ;
  ADD A,$E0               ;
  LD L,A                  ;
  JR place_on_screen_2    ;
place_on_screen_6:
  LD L,(IX+$05)           ; Its y, and the view's
  LD A,(view_y)           ;
  LD E,A                  ;
  LD A,(view_row)         ; In the view's page row?
  CP (IX+$04)             ;
  JR NZ,place_on_screen_8 ;
  LD A,L                  ; Yes: visible if its bottom is below the view's top
  DEC A                   ;
  CP E                    ;
  RET C                   ;
  ADD A,$61               ;
  LD L,A                  ;
place_on_screen_7:
  LD A,(origin_y)         ; The screen y; it is on the screen
  ADD A,L                 ;
  LD (new_sprite_y),A     ;
  LD (IX+$12),A           ;
  SET 7,(IX+$10)          ;
  RET                     ;
place_on_screen_8:
  INC A                   ; In the row below? If not, free it
  CP (IX+$04)             ;
  JP NZ,place_on_screen_9 ;
  LD A,L                  ; Visible if its top is above the view's bottom
  SUB C                   ;
  JR C,place_on_screen_7  ;
  CP E                    ;
  RET NC                  ;
  JP place_on_screen_7    ;
place_on_screen_9:
  LD (IX+$00),$FF         ; Free the record
  RET                     ;

; Colour the cells under a sprite just drawn
;
; Used by the routines at move_missile, update_attacker, draw_rocket and
; update_ground_objects.
;
; The last step of drawing an object's sprite (move_missile, update_attacker,
; draw_rocket, update_ground_objects call it after draw_object_images has drawn
; it): works out the rectangle of character cells the sprite covers in the play
; area, and fills it with the attribute in colour_patch's third byte, set by
; prepare_draw from the record's two colours. The rectangle's width goes into
; colour_patch, its height into the next byte, and fill_attributes paints it,
; bottom row first.
;
; The sprite's position is the third and fourth bytes of old_sprite_x (its x in
; pixels, its bottom line), its width in pixels is sprite_width_drawn (the
; width in bytes, multiplied by 8 in draw_sprite_pair) and its height in lines
; in the last byte of old_sprite_x, as set_new_sprite left them after clipping.
; The play area runs across from x 8 to x $E7 and down from line $18 to line
; $B7; the cells are clipped to it. A cell holding only the sprite's top line
; (the top line being the last of its cell) is not counted: (bottom OR 7 less
; (top - 7)) / 8 rounds that one away.
;
; Two more entries: draw_shot comes in at the instruction that takes the width
; from B, with L, H, D and B already set (the special beam), and
; colour_carried_parts at the height's working, with the width already in
; colour_patch.
colour_sprite:
  LD A,(new_sprite_rows)  ; D = the height less one; nothing drawn, nothing to
  DEC A                   ; colour
  RET M                   ;
  LD D,A                    ; HL = the position (L x, H the bottom line); B =
  LD HL,(new_sprite_x)      ; the width in pixels
  LD A,(sprite_width_drawn) ;
  LD B,A                    ;
  LD A,(sprite_left_clip) ; cut by the play area's left edge (sprite_left_clip,
  AND A                   ; the record's +10 bit 6): the visible width is taken
  JR Z,colour_sprite_0    ; as x + width - $A4, from x 8
  LD A,L                  ;
  ADD A,B                 ;
  SUB $A4                 ;
  LD L,$08                ;
  JR colour_sprite_4      ;
colour_sprite_0:
  LD A,L                  ; starting in the left border column: start a cell
  CP $08                  ; in, a cell narrower
  JR NC,colour_sprite_1   ;
  ADD A,$08               ;
  LD L,A                  ;
  LD A,B                  ;
  SUB $08                 ;
  LD B,A                  ;
colour_sprite_1:
  LD A,L                  ; running past the right edge (x + width over $E8):
  ADD A,$17               ; only up to it
  ADD A,B                 ;
  JR NC,colour_sprite_2   ;
  LD A,$EE                ;
  SUB L                   ;
  JR colour_sprite_4      ;
colour_sprite_2:
  LD A,L                  ; not on a cell boundary: it spans one cell more
  AND $07                 ;
  JR Z,colour_sprite_3    ;
  LD A,B                  ;
  ADD A,$08               ;
  LD B,A                  ;
; This entry point is used by the routine at draw_shot.
colour_sprite_3:
  LD A,L                  ; A = B (the special beam joins here with B its width
  ADD A,B                 ; in pixels)
  SUB L                   ;
colour_sprite_4:
  SRL A                   ; the width in cells; none, nothing to do
  SRL A                   ;
  SRL A                   ;
  LD (colour_patch),A     ;
  DEC A                   ;
  RET M                   ;
; This entry point is used by the routine at colour_carried_parts.
colour_sprite_5:
  LD A,H                  ; E = the top line less 7, but not above line $17
  SUB D                   ; (the play area's top)
  SUB $07                 ;
  LD E,A                  ;
  CP $17                  ;
  JR NC,colour_sprite_6   ;
  LD E,$17                ;
colour_sprite_6:
  LD A,H                  ; the height in cells; none, nothing to do
  OR $07                  ;
  SUB E                   ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD ($AF02),A            ;
  DEC A                   ;
  RET M                   ;
  LD DE,colour_patch      ; paint it with fill_attributes, from the cell under
  JP fill_attributes      ; the bottom line upwards

; A colour patch: width and height in cells, and the attribute
;
; The record fill_attributes paints from: the width in cells, the height in
; cells, and the attribute. colour_sprite works out the first two for a sprite;
; prepare_draw sets the attribute for an object (one of its record's two
; colours, alternating every pass), draw_shot to $43 for the special beam,
; colour_carried_parts to $47.
colour_patch:
  DEFB $00,$00,$00

; Colour the two parts drawn by carried_part bright white
;
; Used by the routine at carried_part.
;
; The end of carried_part, which draws two big parts of the ship's add-ons
; (addon_slab and addon_spire, the last two in addon_graphics's list, drawn
; from their third byte) at the positions in carried_big_x and carried_small_x:
; this colours each, 5 cells wide and $23 lines high for the first, 3 cells
; wide and $2F lines for the second, clipped at the top of the play area by
; clip_height_to_top. It joins colour_sprite for the height and the painting.
;
; Never ran in the build's sessions: what the two parts are is read, not seen.
colour_carried_parts:
  LD A,$47                ; bright white on black
  LD ($AF03),A            ;
  LD HL,(carried_big_x)       ; the first part: 5 cells wide, $23 lines high
  LD B,$23                    ;
  LD A,$05                    ;
  CALL colour_carried_parts_0 ;
  LD HL,(carried_small_x) ; the second: 3 cells wide, $2F lines high
  LD B,$2F                ;
  LD A,$03                ;
colour_carried_parts_0:
  LD (colour_patch),A     ; the width; the height clipped at the play area's
  LD A,B                  ; top (clip_height_to_top) into D; colour it
  CALL clip_height_to_top ;
  LD D,B                  ;
  JP colour_sprite_5      ;

; The update routines for object types $13-$1E
;
; Twelve words, the update routines for object types $13 to $1E:
; run_sky_object_handler jumps through them by the record's type less $13. Type
; $1B's entry is type1b_graphics, a list of graphics, not code: a type $1B
; object updated through here would crash, so presumably none is. Type $1E, a
; rocket part falling into place, is update_rocket_part.
object_handlers:
  DEFW update_cloud          ; Update routines for types $13 to $1E
  DEFW update_attacker       ;
  DEFW update_debris         ;
  DEFW update_attacker       ;
  DEFW update_attacker       ;
  DEFW update_attacker       ;
  DEFW update_tough_attacker ;
  DEFW update_attacker       ;
  DEFW type1b_graphics       ;
  DEFW update_small_object   ;
  DEFW update_rammer         ;
  DEFW update_rocket_part    ;

; The graphic list of every object type
;
; One word for each object type from 0 to $1E: the address of a list of
; graphics, which object_graphic indexes by the low four bits of the record's
; +13 (the frame) and graphic_frame_list reads the first of. Each graphic is a
; byte of its width in bytes, a byte of its height in lines, then the rows,
; bottom row first.
;
; Type 0, the shots' missile, is missile_frames; type $0C, the add-ons lying on
; the planet, shares the ship's list of add-on graphics (addon_graphics); the
; lists of types $0D, $14, $1B and $1E follow below (type0d_graphics,
; type14_graphics, type1b_graphics, rocket_part_frames).
object_graphic_lists:
  DEFW missile_frames     ; Types 0 to $0D
  DEFW trail_images       ;
  DEFW $C7D2              ;
  DEFW $C7DC              ;
  DEFW $C7E5              ;
  DEFW $C7ED              ;
  DEFW $C7F4              ;
  DEFW star_images        ;
  DEFW $C193              ;
  DEFW $C196              ;
  DEFW $C19A              ;
  DEFW $C19E              ;
  DEFW addon_graphics     ;
  DEFW type0d_graphics    ;
  DEFW puff_graphic       ; Types $0E and $0F
  DEFW puff_b_graphic     ;
  DEFW puff_c_graphic      ; Types $10 to $1E
  DEFW puff_wide_graphic   ;
  DEFW puff_wide_b_graphic ;
  DEFW cloud_frames        ;
  DEFW type14_graphics     ;
  DEFW attacker_frames     ;
  DEFW $98A0               ;
  DEFW $98A8               ;
  DEFW $98AC               ;
  DEFW $98B4               ;
  DEFW $98B8               ;
  DEFW type1b_graphics     ;
  DEFW small_thing_frames  ;
  DEFW rammer_frames       ;
  DEFW rocket_part_frames  ;

; Graphics of the rocket parts
;
; Nine words: type $1E's list of frames (from object_graphic_lists), and the
; list toggle_carried_part draws into the ship's picture by part_being_fitted,
; the part being hung under the ship. Type $1E objects are the rocket parts the
; ship picks up and carries to the launch site; collect_pickup sets
; rocket_part_carried to the frame of the one collected. The graphics:
; rocket_bars_graphic, rocket_checks_graphic, rocket_crate_graphic,
; rocket_crate_b_graphic, rocket_fin_graphic, rocket_fin_b_graphic,
; rocket_tank_graphic, rocket_nose_graphic and rocket_tank_full_graphic.
rocket_part_frames:
  DEFW rocket_bars_graphic
  DEFW rocket_checks_graphic
  DEFW rocket_crate_graphic
  DEFW rocket_crate_b_graphic
  DEFW rocket_fin_graphic
  DEFW rocket_fin_b_graphic
  DEFW rocket_tank_graphic
  DEFW rocket_nose_graphic
  DEFW rocket_tank_full_graphic

; Graphics of the type $14 objects
;
; Type $14's list of frames (from object_graphic_lists): four words,
; rotor_graphic, rotor_b_graphic, rotor_c_graphic and rotor_b_graphic again, so
; the animation goes back and forth over three graphics.
type14_graphics:
  DEFW rotor_graphic
  DEFW rotor_b_graphic
  DEFW rotor_c_graphic
  DEFW rotor_b_graphic

; Graphics of the ship's add-ons
;
; Eleven words, the add-ons 1 to 11 in order. fit_addon draws add-ons 1 to 9
; into the ship's picture from here; type $0C objects, the add-ons lying on the
; planet waiting to be collected, use the same list as their frames
; (object_graphic_lists), their +13 being the add-on's number less one.
; carried_part draws 10 and 11 apart from the ship.
;
; Each graphic is preceded by a byte: the height the ship's picture has to be
; to show it, 40 less the buffer row it starts in (see build_ship_sprite). Then
; its width in bytes, its height in lines, the picture (width times height
; bytes, bottom row first), and either $FF (no mask) or a mask of the same
; size, ANDed onto the ship's picture before the graphic is ORed on.
addon_graphics:
  DEFW addon_hook_graphic   ; Add-ons 1 to 11
  DEFW addon_hook_b_graphic ;
  DEFW rocket_tank_graphic  ;
  DEFW addon_block_graphic  ;
  DEFW addon_arches_graphic ;
  DEFW addon_mound_graphic  ;
  DEFW addon_post_graphic   ;
  DEFW addon_bar_graphic    ;
  DEFW addon_figure_graphic ;
  DEFW addon_slab           ;
  DEFW addon_spire          ;

; Graphics of the type $0D objects
;
; Type $0D's list of frames (from object_graphic_lists): sixteen words, the
; frame being the low four bits of the record's +13.
type0d_graphics:
  DEFW fuel_flask_graphic
  DEFW pedestal_graphic
  DEFW cloud_craft_graphic
  DEFW spiky_mound_graphic
  DEFW lamp_graphic
  DEFW dome_robot_graphic
  DEFW small_craft_b_graphic
  DEFW small_craft_graphic
  DEFW debris_b_graphic
  DEFW debris_graphic
  DEFW debris_c_graphic
  DEFW ringed_planet_graphic
  DEFW dome_robot_graphic
  DEFW pylon_craft_graphic
  DEFW fuel_station_graphic
  DEFW pylon_craft_graphic

; Graphics of the type $1B objects
;
; Type $1B's list of frames (from object_graphic_lists): six words. The table
; of update routines (object_handlers) also points here for type $1B, by
; mistake or because no type $1B object reaches run_sky_object_handler.
type1b_graphics:
  DEFW lamp_graphic
  DEFW lamp_b_graphic
  DEFW dome_robot_graphic
  DEFW dome_robot_b_graphic
  DEFW fuel_station_graphic
  DEFW fuel_station_b_graphic

; Where the add-ons go on the ship
;
; Nine words, one for each of add-ons 1 to 9: the address in the ship's picture
; buffer (ship_picture, 6 bytes a row, 40 rows) where fit_addon draws its
; graphic. The first is the start of row 5.
addon_places:
  DEFW $F6D7
  DEFW $F6DB
  DEFW $F6BA
  DEFW $F749
  DEFW $F72C
  DEFW $F74A
  DEFW $F747
  DEFW $F783
  DEFW $F793

; Build the ship's sprite from its add-ons
;
; Used by the routines at draw_ship_image and start_landscape.
;
; The ship's sprite is put together in a buffer of 50 rows of 6 bytes (300
; bytes) at ship_picture, drawn from the bottom row up: the bare ship (84 bytes
; from bare_ship, 14 rows) goes in rows 26-39, the top of the picture without
; add-on 9, and every add-on fitted -- the nine bytes from addon_slots, add-on
; n's slot holding n or 0 -- is masked and drawn in at its own place
; (fit_addon), each hanging below the ship. The sprite's height in rows
; (ship_height) is the most any add-on needs, at least 14, and drawing starts
; that many rows below row 40 (ship_graphic). Add-on 9 sits on top of the ship,
; rows 36-50, and adds 11 rows to the height after the start is worked out, so
; the picture then ends at row 50 -- one row past the buffer, in
; spare_after_ship. Called from draw_ship when an add-on is fitted, and from
; start_landscape.
;
; The ship's top stays where it was on the screen and its bottom line
; (ship_screen_y) moves down by the rows added, and the ship's world position
; (its record at ship_record) moves with it, so the ship does not jump;
; ship_line, the line its shots come out of, is 8 below its top.
;
; The add-ons' effects set here: 4 doubles the top speed across (top_speed 4
; instead of 2); 5 doubles the top climb (top_climb); 9 adds 11 rows to the
; height, into which the tail of load_ore draws the ore collected (ore rows); a
; rocket part being carried (rocket_part_carried not $FF) is hung under the
; ship again by toggle_carried_part's second entry. Add-on 2 always comes with
; add-on 1: its slot is set to twice add-on 1's. All of it ran in the build's
; every-add-on session; with every add-on fitted the picture is 51 rows, rows
; 0-50, its top row in spare_after_ship (simulated for the Graphics page).
build_ship_sprite:
  LD A,(addon_slots)      ; add-on 2 comes and goes with add-on 1
  ADD A,A                 ;
  LD (addon_two),A        ;
  LD A,$02                ; the top speeds across and up: 2 a pass
  LD (top_speed),A        ;
  LD (top_climb),A        ;
  LD BC,$0054             ; the bare ship into the last 14 rows of the buffer
  LD DE,$F755             ;
  LD HL,bare_ship         ;
  LDIR                    ;
  LD IX,addon_slots       ; the nine add-on slots; 14 rows so far
  LD A,$0E                ;
  LD (ship_height),A      ;
  LD B,$09                ;
build_ship_sprite_0:
  PUSH BC                  ; draw in each one fitted (fit_addon), which may
  LD A,(IX+$00)            ; make the sprite taller
  AND A                    ;
  CALL NZ,fit_addon        ;
  INC IX                   ;
  POP BC                   ;
  DJNZ build_ship_sprite_0 ;
  LD A,(ship_height)      ; drawing starts at row 40 less the height:
  LD B,A                  ; ship_graphic = ship_picture + 6 * (40 - height)
  LD A,$28                ;
  SUB B                   ;
  ADD A,A                 ;
  LD E,A                  ;
  ADD A,A                 ;
  ADD A,E                 ;
  LD E,A                  ;
  LD D,$00                ;
  LD HL,ship_picture      ;
  ADD HL,DE               ;
  LD (ship_graphic),HL    ;
  LD A,(ship_screen_y)    ; the bottom line on screen: $6F plus the rows above
  LD B,A                  ; 14, to the bottom of its cell
  LD A,(ship_height)      ;
  SUB $0E                 ;
  ADD A,$6F               ;
  OR $07                  ;
  LD (ship_screen_y),A    ;
  SUB B                   ; the change, as the y speed of the ship's record:
  LD (ship_dy),A          ; move it that far
  LD IX,ship_record       ;
  CALL move_down          ;
  LD (IX+$04),H           ;
  LD (IX+$05),L           ;
  CALL ship_speed_from_scroll ; and clear its speed again
                              ; (ship_speed_from_scroll)
  LD A,(ship_height)      ; the line the shots come out of: 9 below the line
  LD B,A                  ; above the sprite
  LD A,(IX+$1E)           ;
  SUB B                   ;
  ADD A,$09               ;
  LD (ship_line),A        ;
  LD A,(addon_four)        ; add-on 4: top speed across 4
  AND A                    ;
  JR Z,build_ship_sprite_1 ;
  LD A,$04                 ;
  LD (top_speed),A         ;
build_ship_sprite_1:
  LD A,(addon_five)        ; add-on 5: top climb 4
  AND A                    ;
  JR Z,build_ship_sprite_2 ;
  LD A,$04                 ;
  LD (top_climb),A         ;
build_ship_sprite_2:
  LD A,(rocket_part_carried) ; carrying a rocket part: hang it under the ship
  INC A                      ; again
  JR Z,build_ship_sprite_3   ;
  DEC A                      ;
  OR $C0                     ;
  LD (part_being_fitted),A   ;
  CALL toggle_carried_part_3 ;
  XOR A                      ;
  LD (part_being_fitted),A   ;
build_ship_sprite_3:
  LD A,(addon_nine)       ; add-on 9: 11 more rows, up to row 50
  AND A                   ; (spare_after_ship); then the rows of the tail of
  JP Z,load_ore_3         ; load_ore
  LD A,(ship_height)      ;
  ADD A,$0B               ;
  LD (ship_height),A      ;
  JP load_ore_3           ;

; Draw an add-on into the ship's sprite
;
; Used by the routine at build_ship_sprite.
;
; Draws add-on n, from its graphic in addon_graphics at its place in the buffer
; from addon_places, and raises the sprite's height (ship_height) to the byte
; before the graphic if that is more. If the graphic has a mask -- anything but
; $FF after the picture -- it is ANDed onto the buffer first
; (addon_mask_and_draw); then the picture is ORed on. Rows in the buffer are 6
; bytes apart.
;
; The mask test has a catch: a mask whose first byte is $FF is taken as no mask
; at all.
;
; IX the add-on's slot in the player record, holding its number
fit_addon:
  LD HL,addon_graphics    ; DE = its graphic (addon_graphics)
  LD C,(IX+$00)           ;
  DEC C                   ;
  CALL table_word         ;
  EX DE,HL                ;
  PUSH DE                 ; HL = its place in the buffer (addon_places); keep
  LD A,(IX+$00)           ; both
  LD C,A                  ;
  DEC C                   ;
  LD HL,addon_places      ;
  CALL table_word         ;
  POP DE                  ;
  PUSH DE                 ;
  PUSH HL                 ;
  DEC DE                  ; the sprite has to be at least as tall as the byte
  LD A,(DE)               ; before the graphic says
  INC DE                  ;
  LD B,A                  ;
  LD A,(ship_height)      ;
  CP B                    ;
  JR NC,fit_addon_0       ;
  LD A,B                  ;
  LD (ship_height),A      ;
fit_addon_0:
  LD A,(DE)               ; the width (in A') and height (ground_cell); step
  LD B,A                  ; over the picture, width times height bytes
  EX AF,AF'               ;
  INC DE                  ;
  LD A,(DE)               ;
  LD (ground_cell),A      ;
  INC DE                  ;
  LD C,A                  ;
  XOR A                   ;
fit_addon_1:
  ADD A,C                 ;
  DJNZ fit_addon_1        ;
  LD C,A                  ;
  EX DE,HL                ;
  ADD HL,BC               ;
  EX DE,HL                ;
  LD A,(DE)                  ; $FF after it: no mask
  CP $FF                     ;
  JR Z,addon_mask_and_draw_2 ;
  EX AF,AF'                ; A' = the height, C = the width; into the mask loop
  LD B,A                   ; (addon_mask_and_draw)
  LD A,(ground_cell)       ;
  EX AF,AF'                ;
  LD C,B                   ;
  JR addon_mask_and_draw_0 ;

; Mask and draw an add-on into the ship's picture
;
; fit_addon runs into it, to its second instruction for the mask or to the draw
; if there is no mask. A row at a time for the height in A': the mask, C bytes
; of it, ANDed onto the buffer at HL, then HL 6 bytes on to the next row. Then
; the stack gives back the buffer address and the graphic, the width and height
; are read again, and the picture is ORed on the same way. Returns to
; build_ship_sprite's loop.
;
; A' the height in lines
; C the width in bytes
; DE the mask
; HL the place in the buffer
addon_mask_and_draw:
  EX AF,AF'               ; keep the row count
; This entry point is used by the routine at fit_addon.
addon_mask_and_draw_0:
  LD B,C                     ; AND a row of the mask onto the buffer
  PUSH HL                    ;
addon_mask_and_draw_1:
  LD A,(DE)                  ;
  AND (HL)                   ;
  LD (HL),A                  ;
  INC DE                     ;
  INC HL                     ;
  DJNZ addon_mask_and_draw_1 ;
  POP HL                    ; the next row, 6 bytes on
  LD A,$06                  ;
  CALL add_a_to_hl          ;
  EX AF,AF'                 ;
  DEC A                     ;
  JR NZ,addon_mask_and_draw ;
; This entry point is used by the routine at fit_addon.
addon_mask_and_draw_2:
  POP HL                  ; the buffer address and the graphic back; its width
  POP DE                  ; and height
  LD A,(DE)               ;
  LD B,A                  ;
  LD C,A                  ;
  INC DE                  ;
  LD A,(DE)               ;
  INC DE                  ;
addon_mask_and_draw_3:
  EX AF,AF'                  ; OR a row of the picture onto the buffer
  LD B,C                     ;
  PUSH HL                    ;
addon_mask_and_draw_4:
  LD A,(DE)                  ;
  OR (HL)                    ;
  LD (HL),A                  ;
  INC DE                     ;
  INC HL                     ;
  DJNZ addon_mask_and_draw_4 ;
  POP HL                      ; the next row
  LD A,$06                    ;
  CALL add_a_to_hl            ;
  EX AF,AF'                   ;
  DEC A                       ;
  JR NZ,addon_mask_and_draw_3 ;
  RET                     ; done

; Read the controls, and burn fuel for steering and thrust
;
; Used by the routine at move_ship.
;
; Reads the current player's control method (read_control_method) into
; controls_now, after three overrides: no thrust at the ceiling -- the view on
; y page 5 (page 0 with add-on 5) and view_y, the view's line within its page,
; below $60; only fire and the special key while the special beam is on
; (special_beam_on); and once the ship has docked with the finished rocket
; (launch_state set) nothing is read at all and move_ship flies the ship
; itself.
;
; Then the fuel: 8 a pass while steering or thrusting, 16 while doing both at
; once, halved with add-on 6 and halved again if fuel_saver is set (by
; ship_contact when something knocks the ship sideways; cleared every pass in
; update_objects). It is added to the fuel word through add_fuel, which counts
; up towards empty.
;
; IX the ship's record
read_controls:
  CALL read_control_method ; C = the controls: bit 0 right, 1 left, 2 down, 3
                           ; up (thrust), 4 fire, 5 the special key
  LD B,$00                ; the ceiling: page 5, or 0 with add-on 5
  LD A,(addon_five)       ;
  AND A                   ;
  JR NZ,read_controls_0   ;
  LD B,$05                ;
read_controls_0:
  LD A,(view_row)         ;
  CP B                    ;
  JR NZ,read_controls_1   ;
  LD A,(view_y)           ;
  CP $60                  ;
  JR NC,read_controls_1   ;
  LD A,C                  ;
  AND $F7                 ;
  LD C,A                  ;
read_controls_1:
  LD A,(special_beam_on)  ; the special beam on: only fire and the special key
  AND A                   ;
  JR Z,read_controls_2    ;
  LD A,C                  ;
  AND $30                 ;
  LD C,A                  ;
read_controls_2:
  LD A,(launch_state)     ; docked with the rocket: nothing
  AND A                   ;
  RET NZ                  ;
  LD A,C                  ; this pass's controls
  LD (controls_now),A     ;
  AND $0B                 ; not steering or thrusting: no fuel burned
  RET Z                   ;
  LD DE,$0008             ; 8, or 16 for thrust and steering together
  BIT 3,A                 ;
  JR Z,read_controls_3    ;
  AND $03                 ;
  JR Z,read_controls_3    ;
  LD DE,$0010             ;
read_controls_3:
  LD A,(addon_six)        ; add-on 6: half
  AND A                   ;
  JR Z,read_controls_4    ;
  SRA D                   ;
  RR E                    ;
read_controls_4:
  LD A,(fuel_saver)       ; knocked sideways this pass: half again; burn it
  AND A                   ;
  JP Z,add_fuel           ;
  SRA D                   ;
  RR E                    ;
  JP add_fuel             ;

; Read the current player's control method
;
; Used by the routine at read_controls.
;
; By the method in the current player's record (control_method): 0 keyboard, 1
; Kempston, 2 cursor keys, 3 Interface II. Every method gives C in the Kempston
; layout -- bit 0 right, 1 left, 2 down, 3 up (thrust), 4 fire -- plus bit 5,
; the special key, which fires the special beam (special_beam_key). On the
; keyboard the special key is the number row; with a joystick it is any letter
; key but CAPS SHIFT and SPACE (read_keyboard masks the five keys of each
; half-row).
;
; The keyboard: right is X, V, N or SYMBOL SHIFT, left Z, C, B or M --
; alternate keys along the bottom row -- thrust any key on the middle letter
; row, fire any on the top letter row. Down is never set from the keyboard.
; Every read goes through OUT ($FD),A first (read_keyboard).
;
; O:C the controls
read_control_method:
  LD A,(control_method)      ; by the method
  AND A                      ;
  JP Z,read_control_method_3 ;
  CP $01                     ;
  JR Z,read_control_method_0 ;
  CP $02                     ;
  JR Z,read_control_method_2 ;
  JR read_control_method_1   ;
read_control_method_0:
  IN A,($1F)               ; Kempston: the port is already in this layout
  AND $1F                  ;
  LD C,A                   ;
  JP read_control_method_9 ;
read_control_method_1:
  LD A,$EF                 ; Interface II, keys 6-0 on half-row $EF: 0 fire, 9
  OUT ($FD),A              ; up, 8 down, 7 right, 6 left, rotated into place a
  IN A,($FE)               ; bit at a time and inverted
  LD B,$FF                 ;
  RRA                      ;
  RL B                     ;
  RRA                      ;
  RL B                     ;
  RRA                      ;
  RL B                     ;
  RRA                      ;
  RRA                      ;
  RL B                     ;
  RLA                      ;
  RL B                     ;
  LD A,B                   ;
  CPL                      ;
  LD C,A                   ;
  JP read_control_method_9 ;
read_control_method_2:
  LD A,$EF                 ; cursor keys: 7 up, 8 right, 6 down and 0 fire from
  OUT ($FD),A              ; half-row $EF, 5 left from half-row $F7; bits 6 and
  IN A,($FE)               ; 7 come out set
  OR $E0                   ;
  LD B,A                   ;
  AND $08                  ;
  LD C,A                   ;
  LD A,B                   ;
  RRCA                     ;
  RRCA                     ;
  LD B,A                   ;
  AND $05                  ;
  OR C                     ;
  LD C,A                   ;
  LD A,B                   ;
  RRA                      ;
  RRA                      ;
  AND $10                  ;
  OR C                     ;
  LD C,A                   ;
  LD A,$F7                 ;
  OUT ($FD),A              ;
  IN A,($FE)               ;
  RRA                      ;
  RRA                      ;
  RRA                      ;
  AND $02                  ;
  OR C                     ;
  CPL                      ;
  LD C,A                   ;
  JP read_control_method_9 ;
read_control_method_3:
  XOR A                      ; keyboard: X or V (half-row $FE), N or SYMBOL
  LD C,A                     ; SHIFT ($7F): right
  LD A,$FE                   ;
  OUT ($FD),A                ;
  IN A,($FE)                 ;
  AND $14                    ;
  LD B,A                     ;
  LD A,$7F                   ;
  OUT ($FD),A                ;
  IN A,($FE)                 ;
  AND $0A                    ;
  OR B                       ;
  XOR $1E                    ;
  JR Z,read_control_method_4 ;
  LD A,C                     ;
  OR $01                     ;
  LD C,A                     ;
  JR read_control_method_5   ;
read_control_method_4:
  LD A,$FE                   ; Z or C, B or M: left
  OUT ($FD),A                ;
  IN A,($FE)                 ;
  AND $0A                    ;
  LD B,A                     ;
  LD A,$7F                   ;
  OUT ($FD),A                ;
  IN A,($FE)                 ;
  AND $14                    ;
  OR B                       ;
  XOR $1E                    ;
  JR Z,read_control_method_5 ;
  SET 1,C                    ;
read_control_method_5:
  LD A,$BD                   ; any key of A-G or H-ENTER: thrust
  CALL read_keyboard         ;
  JR Z,read_control_method_6 ;
  SET 3,C                    ;
read_control_method_6:
  LD A,$DB                   ; any of Q-T or Y-P: fire
  CALL read_keyboard         ;
  JR Z,read_control_method_7 ;
  SET 4,C                    ;
read_control_method_7:
  LD A,$E7                ; any of 1-5 or 6-0: the special key
  LD B,$00                ;
read_control_method_8:
  CALL read_keyboard      ;
  OR B                    ;
  RET Z                   ;
  SET 5,C                 ;
  RET                     ;
read_control_method_9:
  RES 5,C                  ; joysticks: Z-V or B-SYMBOL SHIFT (not CAPS SHIFT
  LD A,$7E                 ; or SPACE), or any of the other letter rows: the
  CALL read_keyboard       ; special key
  AND $1E                  ;
  LD B,A                   ;
  LD A,$99                 ;
  JR read_control_method_8 ;

; Steer the ship: its speed across and up, and this pass's scroll
;
; Used by the routine at ship_pass.
;
; Called first thing in a pass (ship_pass). The ship never moves on the screen
; across, and up and down only with its height: the landscape moves under it.
; This works out how far, in pixels, up and across (scroll_down and the byte
; after it), for the landscape (draw_landscape) and, negated, for the ship's
; position in the world (ship_speed_from_scroll).
;
; Across, accelerate_across does the work: this gives it the push in DE, in
; 256ths of a pixel a pass a pass: $40 left or -$40 right, plus a drag of 12
; against the way the ship was going (the step across last pass) -- but only
; when pushing against it; pushing the way the ship goes ignores the drag. With
; nothing pushed the drag alone slows the ship. Nothing is pushed while
; fuel_saver is set (knocked sideways). steer_push says which way it was pushed
; ($80 left, 3 right, 0 not), for accelerate_across and the ship's drawing.
;
; Up: gravity takes $40 a pass from the climb speed (the ship's record +$1B
; +$1C), or, while thrusting, thrust adds $50 instead -- one or the other is
; loaded, never both. The speed is clamped (clamp_climb): no faster than half
; the top climb (top_climb, 2 or 4) up, and -3 down. It is added to a fraction
; kept in climb_fraction, and the change in the whole part, doubled, is the
; step: the screen moves 2 lines a unit. A step one unit too big upwards, or
; down by 6, is cut to the top climb, or 4. At a step of 4 either way, every
; other fine-scroll position (origin_y bit 1) moves only 2.
;
; While the special beam is on (special_beam_on) the ship hangs still. Once it
; has docked with the finished rocket (launch_state set) it thrusts by itself
; at the fast climb until the view reaches y page 0, then stops.
;
; The second entry, at the adding of the speed to the fraction, works the step
; out from a speed in HL; bounce_up_down and hit_block (a bounce) use it.
;
; IX the ship's record
move_ship:
  CALL read_controls      ; the controls, and fuel (read_controls)
  XOR A                   ; not pushed yet; no drag
  LD (steer_push),A       ;
  LD DE,(no_push)         ;
  LD A,(launch_state)     ; docked with the rocket: thrust on and the fast
  AND A                   ; climb; at the top page, stop
  JR Z,move_ship_0        ;
  LD A,$08                ;
  LD (controls_now),A     ;
  LD A,$04                ;
  LD (top_climb),A        ;
  LD A,(view_row)         ;
  AND A                   ;
  JR NZ,move_ship_1       ;
  LD (scroll_down),A      ;
  RET                     ;
move_ship_0:
  LD A,(IX+$07)            ; the drag: 12 against last pass's way across, if it
  NEG                      ; moved
  AND A                    ;
  JR Z,move_ship_1         ;
  LD DE,(drag_going_right) ;
  BIT 7,A                  ;
  JR NZ,move_ship_1        ;
  LD DE,(drag_going_left)  ;
move_ship_1:
  LD A,(controls_now)     ; left: $40, with the drag if the ship was going
  BIT 1,A                 ; right
  JR Z,move_ship_2        ;
  EX DE,HL                ;
  LD DE,(push_left)       ;
  LD A,$80                ;
  LD (steer_push),A       ;
  LD A,H                  ;
  CP $FF                  ;
  JR Z,move_ship_3        ;
  ADD HL,DE               ;
  EX DE,HL                ;
  JR move_ship_3          ;
move_ship_2:
  BIT 0,A                 ; right: -$40, with the drag if the ship was going
  JR Z,move_ship_3        ; left
  EX DE,HL                ;
  LD DE,(push_right)      ;
  LD A,$03                ;
  LD (steer_push),A       ;
  LD A,H                  ;
  AND A                   ;
  JR Z,move_ship_3        ;
  ADD HL,DE               ;
  EX DE,HL                ;
move_ship_3:
  LD A,(fuel_saver)       ; knocked sideways this pass: no push
  AND A                   ;
  JR Z,move_ship_4        ;
  LD DE,$0000             ;
move_ship_4:
  CALL accelerate_across  ; the speed across, the position across and the step
                          ; across (accelerate_across)
  LD DE,(gravity)         ; gravity, or thrust
  LD A,(controls_now)     ;
  AND $08                 ;
  JR Z,move_ship_5        ;
  LD DE,(thrust)          ;
move_ship_5:
  LD H,(IX+$1B)           ; the climb speed
  LD L,(IX+$1C)           ;
  LD A,(special_beam_on)  ; the special beam on: no climb, no fall
  AND A                   ;
  JR Z,move_ship_6        ;
  LD HL,$0000             ;
  LD DE,$0000             ;
  LD (climb_fraction),HL  ;
move_ship_6:
  ADD HL,DE               ; add, clamp (clamp_climb) and keep
  CALL clamp_climb        ;
  LD (IX+$1B),H           ;
  LD (IX+$1C),L           ;
; This entry point is used by the routines at bounce_up_down and hit_block.
move_ship_7:
  LD DE,(climb_fraction)  ; the second entry: add the speed to the fraction in
  ADD HL,DE               ; climb_fraction
  LD (climb_fraction),HL  ;
  LD A,H                  ; the whole lines moved, doubled
  SUB D                   ;
  ADD A,A                 ;
  LD H,A                  ;
  BIT 7,A                 ; upwards: one unit too many (the top climb + 2)
  JR NZ,move_ship_8       ; becomes the top climb
  LD A,(top_climb)        ;
  ADD A,$02               ;
  CP H                    ;
  LD A,H                  ;
  JR NZ,move_ship_9       ;
  LD A,(top_climb)        ;
  JR move_ship_9          ;
move_ship_8:
  CP $FA                  ; downwards: -6 becomes -4
  JR NZ,move_ship_9       ;
  LD A,$FC                ;
move_ship_9:
  LD (scroll_down),A      ; the step up this pass
  CP $04                  ; 4 either way: halved when origin_y bit 1 is set
  JR Z,move_ship_10       ;
  CP $FC                  ;
  RET NZ                  ;
move_ship_10:
  LD A,(origin_y)         ;
  AND $02                 ;
  RET Z                   ;
  LD A,(scroll_down)      ;
  SRA A                   ;
  LD (scroll_down),A      ;
  RET                     ;

; Gravity
;
; The ship's accelerations are words in 256ths of a pixel a pass a pass, read
; by move_ship: this, gravity, -$40; then thrust thrust, push_right and
; push_left steering, no_push no push, drag_going_left and drag_going_right the
; drag across.
gravity:
  DEFW $FFC0

; Thrust
;
; +$50, added to the climb speed while thrusting, in place of gravity
; (move_ship).
thrust:
  DEFW $0050

; Steering right
;
; -$40, the push across while steering right (move_ship).
push_right:
  DEFW $FFC0

; Steering left
;
; +$40, the push across while steering left (move_ship).
push_left:
  DEFW $0040

; No push
;
; 0: the push across when the ship was not moving across (move_ship).
no_push:
  DEFW $0000

; The drag while going left
;
; -12, against the ship's way across when it was going left (move_ship).
drag_going_left:
  DEFW $FFF4

; The drag while going right
;
; +12, against the ship's way across when it was going right (move_ship).
drag_going_right:
  DEFW $000C

; The ship's vertical position, in 256ths
;
; move_ship adds the climb speed to it every pass; the change in its whole
; part, doubled, is the step up. Zeroed while the special beam holds the ship
; (move_ship) and by a bounce off a floor or ceiling (hit_block).
climb_fraction:
  DEFW $0000

; Clamp the ship's climb speed
;
; Used by the routine at move_ship.
;
; Down (negative) no faster than -3 and a fraction; up no faster than half the
; top climb (top_climb): 1, or 2 with add-on 5.
;
; H the whole part of the climb speed
clamp_climb:
  LD A,H                  ; falling: from -3.99 to -3.0 becomes -3 and the
  BIT 7,A                 ; fraction
  JR Z,clamp_climb_0      ;
  CP $FE                  ;
  RET NC                  ;
  LD H,$FD                ;
  RET                     ;
clamp_climb_0:
  LD A,(top_climb)        ; climbing: no more than top_climb / 2
  SRL A                   ;
  CP H                    ;
  RET NC                  ;
  LD H,A                  ;
  RET                     ;

; OR a beam onto the screen
;
; Used by the routine at draw_shot.
;
; Draws a shot's beam: B bytes across, for the lines in the last byte of
; old_sprite_x, from the screen address in HL upwards (line_up_hl), the pattern
; from DE with its rows 8 bytes apart. ORed onto what is there, so the erase
; (erase_laser) can take it off again exactly.
;
; B the width in bytes (0: nothing)
; DE the pattern
; HL the screen address of its bottom left
draw_beam:
  LD A,B                  ; nothing to draw; C = the width, A = the lines
  AND A                   ;
  RET Z                   ;
  LD C,B                  ;
  LD A,(new_sprite_rows)  ;
draw_beam_0:
  EX AF,AF'               ; one line: OR the pattern on
  LD B,C                  ;
  PUSH DE                 ;
  PUSH HL                 ;
draw_beam_1:
  LD A,(DE)               ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;
  DJNZ draw_beam_1        ;
  POP HL                  ; the line above
  CALL line_up_hl         ;
  POP DE                  ; the pattern's next row, 8 bytes on
  LD A,$08                ;
  ADD A,E                 ;
  LD E,A                  ;
  EX AF,AF'               ;
  DEC A                   ;
  JR NZ,draw_beam_0       ;
  RET                     ;

; Take a beam off the screen
;
; Used by the routine at draw_shot.
;
; The inverse of draw_beam, from the alternate registers, which hold last
; pass's width, pattern and screen address (the shot routines EXX them away
; before moving the shot): each pattern byte inverted and ANDed onto the
; screen, for the lines in the fifth byte of old_sprite_x. Leaves the alternate
; set as it was.
;
; B' the width in bytes (0: nothing)
; DE' the pattern
; HL' the screen address of its bottom left
erase_laser:
  EXX                     ; the alternate set; nothing to rub out if the width
  LD A,B                  ; is 0
  AND A                   ;
  JR Z,erase_laser_2      ;
  LD C,B                  ;
  LD A,(old_sprite_rows)  ;
erase_laser_0:
  EX AF,AF'               ; one line: clear the pattern's bits
  LD B,C                  ;
  PUSH DE                 ;
  PUSH HL                 ;
erase_laser_1:
  LD A,(DE)               ;
  CPL                     ;
  AND (HL)                ;
  LD (HL),A               ;
  INC L                   ;
  INC DE                  ;
  DJNZ erase_laser_1      ;
  POP HL                  ; the line above; the pattern's next row
  CALL line_up_hl         ;
  POP DE                  ;
  LD A,$08                ;
  ADD A,E                 ;
  LD E,A                  ;
  EX AF,AF'               ;
  DEC A                   ;
  JR NZ,erase_laser_0     ;
erase_laser_2:
  EXX                     ; back to the main set
  RET                     ;

; The four shot records
;
; Four records of $18 bytes, one for each shot in flight: a laser beam, the
; special beam, or a missile. The fields are listed at the top of this part.
; fire fills a free one when the ship fires, update_shots updates them every
; pass, and test_player_shots checks them against the attackers.
shot_records:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; Shot 1
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; Shot 2
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; Shot 3
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ; Shot 4
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;
  DEFB $00,$00,$00,$00,$00,$00,$00,$00 ;

; The pattern of a laser going right
;
; The laser is one line high; this and laser_pattern_left are its pattern, 8
; bytes each. A beam of n cells takes n bytes from offset +F (beam_pattern), so
; as the beam grows and +F falls the head stays solid and the tail ragged.
; Going right: broken at the left, the tail, solid at the right, the head.
; update_shot draws with it.
laser_pattern_right:
  DEFB $4D,$9E,$7C,$7F,$3F,$E7,$FF,$FF

; The pattern of a laser going left
;
; As laser_pattern_right, solid at the left, the head, and broken at the right.
; update_left_shot draws with it.
laser_pattern_left:
  DEFB $FF,$FF,$E7,$FC,$FE,$3E,$79,$B2

; Point DE at a shot's beam pattern
;
; Used by the routines at update_shot, draw_shot and update_left_shot.
;
; DE the pattern's start
; IX the shot's record
beam_pattern:
  LD L,(IX+$0F)           ; plus the record's +F
  LD H,$00                ;
  ADD HL,DE               ;
  EX DE,HL                ;
  RET                     ;

; Give a laser a random colour
;
; Used by the routine at update_shot.
;
; Bright cyan, yellow or white, or bright magenta in place of a fourth, from
; the random number at random_a.
random_beam_colour:
  LD A,(random_a)            ; $45-$47, or $43
  AND $03                    ;
  ADD A,$45                  ;
  CP $48                     ;
  JR NZ,random_beam_colour_0 ;
  LD A,$43                   ;
random_beam_colour_0:
  LD (IX+$0D),A           ; the record's +D
  RET                     ;

; A random -1, 0 or +1
;
; Used by the routine at update_shot.
;
; From the second byte of random_a: 0 half the time, -1 and +1 a quarter each.
; Used to shake a laser's line by one as it leaves the ship.
;
; O:B -1, 0 or +1
random_jitter:
  LD A,(random_b)         ; 0 or 1: 0
  AND $03                 ;
  LD B,$00                ;
  CP $02                  ;
  RET C                   ;
  DEC B                   ; 3: -1
  CP $03                  ;
  RET Z                   ;
  INC B                   ; 2: +1
  INC B                   ;
  RET                     ;

; Update the special beam
;
; Used by the routine at update_shot.
;
; The special beam (record +15 bit 7) is fired by the special key with add-on 7
; while the ship is still (special_beam_key), and the ship hangs still while it
; lasts. It is a band 5 to 8 lines high (beam_frames's frames, cycling 0, 1, 2,
; 3, 2, 1 a pass) that grows a cell every other pass up to 8 cells, its probe 8
; pixels further each time (+12), stops growing when the probe meets the
; landscape (probe_special_beam), and after 22 passes ($16 in +11), or when the
; key is let go (end_special_beam), shrinks a cell a pass until gone. Going
; left, its left end moves out as it grows. A sound every pass (sound_flutter).
;
; Drawing as for a laser (draw_shot), but the pattern for rubbing out is last
; pass's frame expanded into the sprite buffer at the end of
; build_shifted_pieces (old_beam_frame), and the new one into sprite_buffer_two
; (new_beam_frame); the beam is coloured bright magenta. When it has gone the
; ship is free again (special_beam_on).
;
; It ran in the build's every-add-on session, but only going right and never
; with the probe in the landscape: the left end's moves going left and the
; probe's return to the ship are read only. Fired with the ship on the ground,
; the probe is in the landscape at once: the cell the beam has just grown is
; taken off again, its length is back to 0, and draw_shot frees the record that
; same pass, so the beam shows only in the air (run in SkoolKit's simulator for
; the Animations page).
;
; IX the shot's record
update_special_beam:
  LD A,(IX+$0E)           ; a buzz while it is out
  AND A                   ;
  CALL NZ,sound_flutter   ;
  INC (IX+$11)            ; age it
  CALL old_beam_frame        ; last pass's frame for rubbing out; its screen
  CALL record_screen_address ; address and width into the alternate set
  LD B,(IX+$0E)              ;
  EXX                        ;
  BIT 7,(IX+$10)              ; stopped: shrink
  JR NZ,update_special_beam_1 ;
  BIT 0,(IX+$11)             ; grow only every other pass
  JR Z,update_special_beam_2 ;
  LD A,(IX+$12)           ; move the probe a step out
  ADD A,(IX+$07)          ;
  LD (IX+$07),A           ;
  LD A,(IX+$0E)              ; at most 8 cells
  CP $08                     ;
  JR Z,update_special_beam_0 ;
  INC (IX+$0E)               ; a cell longer; going left, the left end a cell
  BIT 7,(IX+$00)             ; further out
  JR Z,update_special_beam_0 ;
  LD A,$F8                   ;
  ADD A,(IX+$0B)             ;
  LD (IX+$0B),A              ;
update_special_beam_0:
  CALL probe_special_beam    ; the probe in the landscape (probe_special_beam)?
  JR Z,update_special_beam_2 ;
  LD A,(ship_dx)          ; yes: the probe moves with the ship again
  LD (IX+$07),A           ;
update_special_beam_1:
  DEC (IX+$0E)               ; a cell shorter; going left, the left end a cell
  BIT 7,(IX+$00)             ; back
  JR Z,update_special_beam_2 ;
  LD A,$08                   ;
  ADD A,(IX+$0B)             ;
  LD (IX+$0B),A              ;
update_special_beam_2:
  CALL new_beam_frame     ; this pass's frame (new_beam_frame)
  LD A,(IX+$11)           ; 22 passes old: start shrinking; draw it (draw_shot)
  CP $16                  ;
  JP NZ,draw_shot_1       ;
  SET 7,(IX+$10)          ;
  JP draw_shot_1          ;

; Update a laser shot
;
; Used by the routine at update_shots.
;
; Called for each laser (and special beam) in flight (update_shots). A laser is
; a line one pixel high, drawn on the screen and coloured a cell high, which
; grows out of the ship's nose: its head 16 pixels a pass and its tail 8, its
; length (+E, in cells) one more each pass. A probe moves through the world 16
; pixels a pass ahead of the ship (probe_laser, the record's +7 being the
; ship's speed plus 16); when it meets the landscape, or the head reaches the
; play area's edge (x $B8 for the left end of a beam going right), the head
; stops (+10 bit 7). The pattern's offset (+F) then counts down to 0, after
; which the tail catches up (bit 6) a cell a pass until the beam has gone.
; While stopped the beam is moved by the scroll, so it stays on what it hit.
; The attackers are checked against it in test_player_shots.
;
; On its first pass (+E 0) the line is set: the ship's shot line (ship_line)
; less this pass's climb, plus a random -1, 0 or +1, and a random colour
; (random_beam_colour), and the record's world position is moved by that. Going
; left is update_left_shot; both end in draw_shot, which draws the beam and
; frees the record when it has gone.
;
; piece_step_x is set to $80 for collide_with_landscape: a shot stops at the
; first box of landscape it touches.
;
; IX the shot's record
update_shot:
  LD A,(ship_dx)          ; the probe moves with the ship; piece_step_x = $80,
  LD (IX+$07),A           ; a shot
  LD A,$80                ;
  LD (piece_step_x),A     ;
  BIT 7,(IX+$15)            ; the special beam: update_special_beam
  JP NZ,update_special_beam ;
  LD A,$01                ; a laser is one line high, drawn and rubbed out
  LD (old_sprite_rows),A  ;
  LD (new_sprite_rows),A  ;
  LD A,(IX+$0E)           ; not its first pass
  AND A                   ;
  JR NZ,update_shot_0     ;
  LD (IX+$10),$00         ; its first: no flags, no speed; the line, shaken by
  LD (IX+$07),$00         ; one; a colour; move the record to it
  CALL random_jitter      ;
  LD A,(scroll_down)      ;
  NEG                     ;
  ADD A,B                 ;
  LD (IX+$09),B           ;
  LD B,A                  ;
  LD A,(ship_line)        ;
  ADD A,B                 ;
  LD (IX+$0C),A           ;
  CALL random_beam_colour ;
  CALL move_object        ;
  LD (IX+$09),$00         ;
update_shot_0:
  BIT 7,(IX+$00)          ; going left: update_left_shot
  JP NZ,update_left_shot  ;
  LD DE,laser_pattern_right  ; last pass's pattern, screen address and width
  CALL beam_pattern          ; into the alternate set
  CALL record_screen_address ;
  LD B,(IX+$0E)              ;
  EXX                        ;
  BIT 7,(IX+$10)          ; the head has stopped
  JR NZ,update_shot_4     ;
  LD A,(IX+$0E)           ; first pass: the pattern's offset 8; the left end at
  AND A                   ; x $88
  JR NZ,update_shot_1     ;
  LD (IX+$0F),$08         ;
  LD (IX+$0B),$88         ;
update_shot_1:
  LD A,$10                ; the probe 16 pixels ahead
  ADD A,(IX+$07)          ;
  LD (IX+$07),A           ;
  CALL probe_laser        ; in the landscape (probe_laser)?
  JR Z,update_shot_2      ;
  LD A,$F0                ; yes: the probe back; the head stops, a hit
  ADD A,(IX+$07)          ;
  LD (IX+$07),A           ;
  SET 7,(IX+$10)          ;
  SET 5,(IX+$10)          ;
  JP update_shot_9        ;
update_shot_2:
  LD A,$08                ; the left end 8 on; at $B8 the head is at the edge
  ADD A,(IX+$0B)          ;
  LD (IX+$0B),A           ;
  CP $B8                  ;
  JR NZ,update_shot_3     ;
  SET 7,(IX+$10)          ;
update_shot_3:
  LD A,(IX+$0E)           ; a cell longer, the pattern's offset one less; draw
  INC A                   ; it
  LD (IX+$0E),A           ;
  LD A,(IX+$0F)           ;
  DEC A                   ;
  LD (IX+$0F),A           ;
  JP update_shot_10       ;
update_shot_4:
  BIT 5,(IX+$10)          ; stopped: probe again until it hits
  JR NZ,update_shot_5     ;
  CALL probe_laser        ;
  JR Z,update_shot_8      ;
  SET 5,(IX+$10)          ;
update_shot_5:
  LD A,(IX+$07)           ; follow the scroll across; no more probing speed
  NEG                     ;
  ADD A,(IX+$0B)          ;
  LD (IX+$0B),A           ;
  LD (IX+$07),$00         ;
  BIT 6,(IX+$10)          ; the right end past the edge at $E4-$EF: a cell
  LD B,(IX+$0E)           ; shorter; past $F0 (off the screen): gone
  LD C,(IX+$0B)           ;
  JR Z,update_shot_6      ;
  DEC B                   ;
  LD A,C                  ;
  ADD A,$08               ;
  LD C,A                  ;
update_shot_6:
  SLA B                   ;
  SLA B                   ;
  SLA B                   ;
  LD A,B                  ;
  ADD A,C                 ;
  AND $F8                 ;
  CP $E4                  ;
  JR C,update_shot_8      ;
  CP $F0                  ;
  JR C,update_shot_7      ;
  LD (IX+$0E),$00         ;
update_shot_7:
  LD A,(IX+$0E)           ; a cell shorter at the right
  DEC A                   ;
  LD (IX+$0E),A           ;
  LD A,(IX+$0B)           ;
  SUB $08                 ;
  LD (IX+$0B),A           ;
update_shot_8:
  BIT 6,(IX+$10)          ; the tail catching up: a cell shorter, the left end
  JR Z,update_shot_9      ; 8 on
  LD A,(IX+$0E)           ;
  DEC A                   ;
  LD (IX+$0E),A           ;
  LD A,(IX+$0B)           ;
  ADD A,$08               ;
  LD (IX+$0B),A           ;
  JR update_shot_10       ;
update_shot_9:
  LD A,(IX+$0F)           ; the pattern's offset down to 0: then the tail
  DEC A                   ; starts
  LD (IX+$0F),A           ;
  JR NZ,update_shot_10    ;
  SET 6,(IX+$10)          ;
update_shot_10:
  LD DE,laser_pattern_right ; the right-going pattern; draw it
  JR draw_shot_0            ;

; Draw a beam, colour it, and free the record when it has gone
;
; Used by the routine at update_left_shot.
;
; The end of every beam's update: update_left_shot comes in at the top with the
; left-going pattern, update_shot at its second instruction with the
; right-going one, update_special_beam at its third with its own in DE. The
; beam follows the vertical scroll (scroll_down) on the screen; last pass's
; beam is rubbed out (erase_laser) and this pass's drawn (draw_beam), and the
; record moved by its speed (move_object).
;
; A laser is coloured with its own attribute, a strip of its length one cell
; high (shot_colour_patch, painted by fill_attributes); the special beam bright
; magenta over its width and height (colour_sprite's second entry). A beam of
; no length is finished: its record is freed (+B 0) and, for the special beam,
; the ship let go (special_beam_on) -- the jump to load_ore then returns at
; once, a shot's +13 being 0.
;
; IX the shot's record
draw_shot:
  LD DE,laser_pattern_left ; the left-going pattern
; This entry point is used by the routine at update_shot.
draw_shot_0:
  CALL beam_pattern       ; plus the record's offset
; This entry point is used by the routine at update_special_beam.
draw_shot_1:
  LD A,(scroll_down)      ; follow the vertical scroll
  ADD A,(IX+$0C)          ;
  LD (IX+$0C),A           ;
  CALL record_screen_address ; this pass's screen address and length (not below
  LD B,(IX+$0E)              ; 0)
  BIT 7,B                    ;
  JR Z,draw_shot_2           ;
  LD B,$00                   ;
  LD (IX+$0E),B              ;
draw_shot_2:
  CALL erase_laser        ; rub out, draw, and move the record through the
  CALL draw_beam          ; world
  CALL move_object        ;
  LD A,(IX+$0E)           ; gone: free the record
  AND A                   ;
  JR NZ,draw_shot_3       ;
  LD (IX+$0B),$00         ;
  BIT 7,(IX+$15)          ;
  RET Z                   ;
  XOR A                   ; the special beam gone: the ship is free
  LD (special_beam_on),A  ;
  JP load_ore             ;
draw_shot_3:
  BIT 7,(IX+$15)          ; the special beam: bright magenta, its height in
  JR Z,draw_shot_4        ; lines, its width in pixels
  LD A,$43                ;
  LD ($AF03),A            ;
  LD L,(IX+$0B)           ;
  LD H,(IX+$0C)           ;
  LD A,(new_sprite_rows)  ;
  LD D,A                  ;
  LD A,(IX+$0E)           ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  JP colour_sprite_3      ;
draw_shot_4:
  LD DE,shot_colour_patch ; a laser: a strip of its length, one cell high, in
  LD A,(IX+$0E)           ; its colour
  LD (DE),A               ;
  INC DE                  ;
  INC DE                  ;
  LD A,(IX+$0D)           ;
  LD (DE),A               ;
  LD L,(IX+$0B)           ;
  LD H,(IX+$0C)           ;
  LD DE,shot_colour_patch ;
  CALL fill_attributes    ;
  RET                     ;

; The colour patch for a laser
;
; For fill_attributes: the width in cells (the beam's length), the height (1)
; and the attribute, filled in by draw_shot.
shot_colour_patch:
  DEFB $00,$01,$00

; Update a laser shot going left
;
; Used by the routine at update_shot.
;
; update_shot for a beam going left, mirrored: the left end moves 16 pixels a
; pass as the beam grows a cell, the probe 16 pixels a pass the other way (+7
; the ship's speed less 16), the pattern taken from the left-going one at
; offset +F counting up. The head stops at the play area's left edge (x 8) or
; when the probe meets the landscape; the tail catches up when offset and
; length together reach 8.
;
; IX the shot's record
update_left_shot:
  LD DE,laser_pattern_left   ; last pass's pattern, screen address and width
  CALL beam_pattern          ; into the alternate set
  CALL record_screen_address ;
  LD B,(IX+$0E)              ;
  EXX                        ;
  BIT 7,(IX+$10)           ; the head has stopped
  JR NZ,update_left_shot_3 ;
  LD A,$F0                ; the left end 16 out; the probe 16 ahead
  ADD A,(IX+$0B)          ;
  LD (IX+$0B),A           ;
  LD A,$F0                ;
  ADD A,(IX+$07)          ;
  LD (IX+$07),A           ;
  LD A,(IX+$0E)            ; first pass: the left end at x $58
  AND A                    ;
  JR NZ,update_left_shot_0 ;
  LD (IX+$0B),$58          ;
update_left_shot_0:
  LD (IX+$0F),$00         ; the offset 0
  CALL probe_laser        ; in the landscape (probe_laser)?
  JR Z,update_left_shot_1 ;
  SET 7,(IX+$10)          ; yes: the head stops, a hit; both back 16
  SET 5,(IX+$10)          ;
  LD A,$10                ;
  ADD A,(IX+$07)          ;
  LD (IX+$07),A           ;
  LD A,$10                ;
  ADD A,(IX+$0B)          ;
  LD (IX+$0B),A           ;
  JR update_left_shot_2   ;
update_left_shot_1:
  LD A,(IX+$0E)           ; no: a cell longer
  INC A                   ;
  LD (IX+$0E),A           ;
update_left_shot_2:
  LD A,(IX+$0B)           ; at the left edge the head stops; draw it
  CP $08                  ;
  JP NZ,draw_shot         ;
  SET 7,(IX+$10)          ;
  JP draw_shot            ;
update_left_shot_3:
  LD A,(IX+$0F)           ; stopped: the offset one more
  INC A                   ;
  LD (IX+$0F),A           ;
  BIT 5,(IX+$10)           ; probe again until it hits
  JR NZ,update_left_shot_4 ;
  CALL probe_laser         ;
  JR Z,update_left_shot_5  ;
  SET 5,(IX+$10)           ;
update_left_shot_4:
  LD A,(IX+$07)            ; follow the scroll across
  NEG                      ;
  ADD A,(IX+$0B)           ;
  LD (IX+$0B),A            ;
  LD (IX+$07),$00          ;
  CP $08                   ;
  JR NC,update_left_shot_5 ;
  ADD A,$08               ; past the left edge: a cell shorter, from the left
  LD (IX+$0B),A           ;
  LD A,(IX+$0E)           ;
  DEC A                   ;
  LD (IX+$0E),A           ;
  LD A,(IX+$0F)           ;
  INC A                   ;
  LD (IX+$0F),A           ;
update_left_shot_5:
  BIT 6,(IX+$10)          ; the tail catching up: a cell shorter
  JR Z,update_left_shot_6 ;
  LD A,(IX+$0E)           ;
  DEC A                   ;
  LD (IX+$0E),A           ;
  JP draw_shot            ;
update_left_shot_6:
  LD A,(IX+$0F)           ; offset and length reach 8: the tail starts
  ADD A,(IX+$0E)          ;
  CP $08                  ;
  JP NZ,draw_shot         ;
  SET 6,(IX+$10)          ;
  JP draw_shot            ;

; Has a laser's probe met the landscape?
;
; Used by the routines at update_shot and update_left_shot.
;
; A box 24 pixels wide and 12 high.
probe_laser:
  LD B,$0C                ; 12 lines high
  JR probe_shot           ;

; Has the special beam's probe met the landscape?
;
; Used by the routine at update_special_beam.
;
; A box 24 pixels wide and 16 high. Runs into probe_shot.
probe_special_beam:
  LD B,$10                ; 16 lines high

; Test a shot's probe against the landscape
;
; Used by the routine at probe_laser.
;
; A box C wide and B high at the record's world position, tested with
; test_landscape_at (which moves it 4 pixels left and 4 down for the test, and
; back). A laser that hits makes a sound (sound_tick).
;
;   B the height in lines
;   IX the shot's record
; O:F NZ if it hit
probe_shot:
  LD C,$18                ; 24 wide: test it
  CALL test_landscape_at  ;
  LD A,(scratch_count)    ; not hit: Z
  AND A                   ;
  RET Z                   ;
  BIT 7,(IX+$15)          ; a laser: a tick; NZ
  CALL Z,sound_tick       ;
  OR $01                  ;
  RET                     ;

; Update the four shots
;
; Used by the routine at ship_pass.
;
; Called every pass from ship_pass. A missile (+16 bit 7) is updated by
; update_missile while its +0 is 0 (its type); any other record in use (+B not
; 0) is a beam, updated by update_shot.
update_shots:
  LD IX,shot_records      ; the four shot records
  LD B,$04                ;
update_shots_0:
  PUSH BC                 ; a missile: its own routine
  PUSH IX                 ;
  BIT 7,(IX+$16)          ;
  JR Z,update_shots_1     ;
  LD A,(IX+$00)           ;
  AND A                   ;
  CALL Z,update_missile   ;
  JR update_shots_2       ;
update_shots_1:
  LD A,(IX+$0B)           ; a beam in flight: update_shot
  AND A                   ;
  CALL NZ,update_shot     ;
update_shots_2:
  POP IX                  ; the next
  LD BC,$0018             ;
  ADD IX,BC               ;
  POP BC                  ;
  DJNZ update_shots_0     ;
  RET                     ;

; Which shot comes next
;
; Bit 0 flips with every shot fired (fire): with add-on 8 every other shot is a
; missile. special_beam_key sets bit 2 for the one call that launches the
; special beam.
shot_alternator:
  DEFB $00

; Fire, if the fire key is pressed
;
; Used by the routine at draw_trail.
;
; Called every pass from draw_ship. First the special beam's key
; (special_beam_key); then, if the fire key is held and the gun has cooled (6
; passes, gun_cooling), a shot in the first free record of four. Its record is
; cleared (free_object), takes the ship's facing and world position, 8 pixels
; ahead of it (24 facing right), at the ship's shot line (place_shot), and is
; moved there. Then one of three kinds:
;
; A laser (update_shot takes it on), with a zap (sound_crackle_b).
;
; A missile, every other shot with add-on 8 (shot_alternator bit 0): type 0,
; the small 8 by 3 sprite of missile_frames, steady bright cyan, flying 8
; pixels a pass from 32 ahead of the ship (8 facing left) and 3 lines higher,
; updated as an ordinary object by update_missile (+16 $80).
;
; The special beam (shot_alternator bit 2, from special_beam_key): +15 $80,
; bright magenta, at x $90 going right or $60 going left, stepping 8 a pass; it
; holds the ship still (special_beam_on).
;
; Both ran in the build's every-add-on session, though a missile was never
; fired facing left.
fire:
  CALL special_beam_key   ; the special beam's key
  LD A,(gun_cooling)      ; the gun cooling
  AND A                   ;
  JR Z,fire_0             ;
  DEC A                   ;
  LD (gun_cooling),A      ;
  RET                     ;
fire_0:
  LD A,(controls_now)     ; fire not pressed
  BIT 4,A                 ;
  RET Z                   ;
  LD A,$06                ; 6 passes to the next; flip the alternator
  LD (gun_cooling),A      ;
  LD A,(shot_alternator)  ;
  XOR $01                 ;
  LD (shot_alternator),A  ;
; This entry point is used by the routine at special_beam_key.
fire_1:
  LD IX,shot_records      ; (entry from special_beam_key) four records; a
  LD A,$04                ; missile's is free when its +0 is $FF
fire_2:
  EX AF,AF'               ;
  BIT 7,(IX+$16)          ;
  JR NZ,fire_3            ;
  LD A,(IX+$0B)           ; a beam's when +B is 0
  AND A                   ;
  JR Z,fire_4             ;
fire_3:
  LD A,(IX+$00)           ;
  INC A                   ;
  JR NZ,fire_6            ;
fire_4:
  CALL free_object        ; clear it; the ship's facing; in use; the ship's
  LD A,(ship_facing)      ; position (place_shot)
  LD (IX+$00),A           ;
  LD (IX+$0B),$99         ;
  CALL place_shot         ;
  LD A,(shot_alternator)  ; the special beam, or (every other shot) a missile
  BIT 2,A                 ;
  JP NZ,fire_10           ;
  DEC A                   ;
  JP Z,fire_7             ;
fire_5:
  CALL move_object        ; a laser: move it ahead of the ship; a zap
  LD (IX+$09),$00         ;
  BIT 7,(IX+$15)          ;
  RET NZ                  ;
  JP sound_crackle_b      ;
fire_6:
  LD BC,$0018             ; the next record; none free, no shot
  ADD IX,BC               ;
  EX AF,AF'               ;
  DEC A                   ;
  JR NZ,fire_2            ;
  RET                     ;
fire_7:
  LD A,(addon_eight)      ; no add-on 8: a laser after all
  AND A                   ;
  JP Z,fire_5             ;
  LD A,(IX+$07)           ; 32 ahead facing right, 3 lines up; put it there
  CP $08                  ;
  JR Z,fire_8             ;
  LD (IX+$07),$20         ;
fire_8:
  LD A,(IX+$09)           ;
  SUB $03                 ;
  LD (IX+$09),A           ;
  LD (IX+$08),$00         ;
  LD (IX+$0A),$00         ;
  CALL move_object        ;
  LD (IX+$09),$00         ;
  LD (IX+$10),$27         ; 8 a pass, the right way; type 0, bright cyan, a
  LD A,(IX+$07)           ; missile
  LD (IX+$07),$08         ;
  CP $20                  ;
  JR Z,fire_9             ;
  LD (IX+$07),$F8         ;
fire_9:
  LD (IX+$00),$00         ;
  LD (IX+$0D),$DD         ;
  LD (IX+$15),$7F         ;
  LD (IX+$16),$80         ;
  CALL clear_screen_fields ; nothing on the screen yet; a zap
  LD (IX+$17),$F0          ;
  JP sound_crackle_b       ;
fire_10:
  LD A,$80                ; the special beam: hold the ship; bright magenta
  LD (IX+$15),A           ;
  LD (special_beam_on),A  ;
  LD (IX+$0D),$43         ;
  LD A,(ship_line)        ; the shot line, and 9 down in the world
  ADD A,$09               ;
  LD (IX+$0C),A           ;
  LD A,(IX+$09)           ;
  ADD A,$09               ;
  LD (IX+$09),A           ;
  LD A,(IX+$07)           ; facing left: x $60, -8 a pass; facing right: x $90,
  ADD A,$08               ; +8 a pass
  AND $20                 ;
  LD (IX+$07),A           ;
  LD (IX+$12),$F8         ;
  LD (IX+$0B),$60         ;
  JP Z,fire_5             ;
  LD (IX+$12),$08         ;
  LD (IX+$0B),$90         ;
  JP fire_5               ;

; The special beam is out
;
; $80 while the special beam lasts (fire), 0 when it has gone (draw_shot).
; While it is set the ship takes only fire and the special key (read_controls)
; and hangs still (move_ship), and special_beam_key watches for the key being
; let go.
special_beam_on:
  DEFB $00

; Launch or end the special beam
;
; Used by the routine at fire.
;
; The special key (the number row, or a letter with a joystick:
; read_control_method) launches the special beam if add-on 7 is fitted and the
; ship is still: its speeds last pass (ship_across_speed across and
; ship_down_speed up and down, in the ship's record) both within dead_band's
; dead band. Letting the key go while the beam is out starts it shrinking
; (end_special_beam).
;
; It ran in the build's every-add-on session.
special_beam_key:
  LD A,(special_beam_on)  ; the beam is out: watch the key
  AND A                   ;
  JR NZ,end_special_beam  ;
  LD A,(controls_now)     ; the key, and add-on 7
  BIT 5,A                 ;
  RET Z                   ;
  LD A,(addon_seven)      ;
  AND A                   ;
  RET Z                   ;
  LD HL,(ship_across_speed) ; the ship still across and up and down
  LD A,H                    ;
  LD H,L                    ;
  LD L,A                    ;
  CALL dead_band            ;
  LD A,H                    ;
  OR L                      ;
  RET NZ                    ;
  LD HL,(ship_down_speed)   ;
  LD A,H                    ;
  LD H,L                    ;
  LD L,A                    ;
  CALL dead_band            ;
  LD A,H                    ;
  OR L                      ;
  RET NZ                    ;
  LD A,(shot_alternator)  ; launch it through fire's second entry, with
  XOR $04                 ; shot_alternator bit 2 set for the one call
  LD (shot_alternator),A  ;
  CALL fire_1             ;
  LD A,(shot_alternator)  ;
  AND $01                 ;
  LD (shot_alternator),A  ;
  RET                     ;

; Find the special beam's record
;
; Used by the routines at beam_hits_ground_object and end_special_beam.
;
; Looks through the first three shot records for one that is not a missile and
; has +15 bit 7 set. If none of the three is, IY is left at the fourth,
; unchecked. Also used by beam_hits_ground_object.
;
; O:IY the record
find_special_beam:
  LD IY,shot_records        ; the first three records
  LD DE,$0018               ;
  LD B,$03                  ;
find_special_beam_0:
  BIT 7,(IY+$16)            ;
  JR NZ,find_special_beam_1 ;
  BIT 7,(IY+$15)            ;
  RET NZ                    ;
find_special_beam_1:
  ADD IY,DE                 ;
  DJNZ find_special_beam_0  ;
  RET                       ;

; Start the special beam shrinking when its key is let go
;
; Used by the routine at special_beam_key.
;
; It ran in the build's every-add-on session.
end_special_beam:
  LD A,(controls_now)     ; the key still held
  BIT 5,A                 ;
  RET NZ                  ;
  CALL find_special_beam  ; let go: stop its head (+10 bit 7)
  SET 7,(IY+$10)          ;
  RET                     ;

; Expand the special beam's last frame, for rubbing out, and step to the next
;
; Used by the routine at update_special_beam.
;
; The frame (+15 bits 0-3, from 0 to 5 and round again) is expanded into the
; sprite buffer at the end of build_shifted_pieces (beam_frames), and its
; height goes into the fifth byte of old_sprite_x, the lines erase_laser rubs
; out.
;
;   IX the shot's record
; O:DE the expanded pattern
old_beam_frame:
  LD DE,$F800             ; the frame's graphic (beam_frame)
  CALL beam_frame         ;
  LD A,(IX+$15)           ; the next frame, 0 to 5, keeping bit 7
  AND $0F                 ;
  INC A                   ;
  CP $06                  ;
  JR NZ,old_beam_frame_0  ;
  XOR A                   ;
old_beam_frame_0:
  OR $80                  ;
  LD (IX+$15),A           ;
  EX DE,HL                ; the height for rubbing out; expand it
  LD A,(DE)               ; (expand_beam_frame)
  LD (old_sprite_rows),A  ;
  JP expand_beam_frame    ;

; Expand the special beam's frame for drawing
;
; Used by the routine at update_special_beam.
;
; As old_beam_frame for this pass's frame, into sprite_buffer_two; its height
; goes into the last byte of old_sprite_x, the lines draw_beam draws.
;
;   IX the shot's record
; O:DE the expanded pattern
new_beam_frame:
  LD DE,sprite_buffer_two ; the frame's graphic; its height; expand it
  CALL beam_frame         ; (beam_frames)
  EX DE,HL                ;
  LD A,(DE)               ;
  LD (new_sprite_rows),A  ;
  JP expand_beam_frame    ;

; The special beam's frame graphic
;
; Used by the routines at old_beam_frame and new_beam_frame.
;
;   IX the shot's record
; O:HL the graphic
beam_frame:
  LD A,(IX+$15)           ; the frame from beam_frames's table (table_word)
  AND $0F                 ;
  LD C,A                  ;
  LD HL,beam_frames       ;
  JP table_word           ;

; The special beam's frames
;
; Six words, the frames in the order they are shown: 0, 1, 2, 3, 2, 1, so the
; band swells and shrinks. Each frame is a byte of its height (5 to 8 lines),
; then two bytes a line, a 16-pixel pattern. beam_frame picks one;
; expand_beam_frame expands it.
beam_frames:
  DEFW ring_graphic       ; Frames 0, 1, 2, 3
  DEFW ring_b_graphic     ;
  DEFW ring_c_graphic     ;
  DEFW ring_d_graphic     ;
  DEFW ring_c_graphic     ; Frames 4 and 5
  DEFW ring_b_graphic     ;

; Expand a special beam frame into a pattern buffer
;
; Used by the routines at old_beam_frame and new_beam_frame.
;
; Reached by JP from old_beam_frame and new_beam_frame with DE the frame's
; graphic and HL the buffer (the one at the end of build_shifted_pieces for
; last pass's frame, sprite_buffer_two for this pass's). Each line's two bytes
; are repeated four times into an 8-byte row, for the frame's height, so
; draw_beam and erase_laser can take up to 8 cells of it. Then DE is the
; buffer, one byte on if the beam goes left and its length is odd, which keeps
; the pattern in step with the screen.
;
;   A the frame's height
;   DE the frame's graphic
;   HL the buffer
;   IX the shot's record
; O:DE the buffer, adjusted
expand_beam_frame:
  PUSH HL                 ; keep the buffer; A' = the lines; this line's two
  INC DE                  ; bytes in C and A; four copies
expand_beam_frame_0:
  EX AF,AF'               ;
  LD A,(DE)               ;
  LD C,A                  ;
  INC DE                  ;
  LD A,(DE)               ;
  INC DE                  ;
  LD B,$04                ;
expand_beam_frame_1:
  LD (HL),C                ; into the row, two bytes a time
  INC L                    ;
  LD (HL),A                ;
  INC L                    ;
  DJNZ expand_beam_frame_1 ;
  EX AF,AF'                 ; the next line
  DEC A                     ;
  JR NZ,expand_beam_frame_0 ;
  POP DE                  ; the buffer
  BIT 7,(IX+$00)          ; going left with an odd length: a byte on
  RET Z                   ;
  BIT 0,(IX+$0E)          ;
  RET Z                   ;
  INC E                   ;
  RET                     ;

; Clear a record's +B, +C, +E and +F
;
; Used by the routines at stop_object, free_object, fire, hit_block and
; leave_slope.
;
; For a shot these are its screen position, length and pattern offset;
; free_object starts the clearing of a whole object record here, and the
; landscape collisions (hit_block, leave_slope) use it on an object that hits
; something.
;
;   IX the record
; O:A 0
clear_screen_fields:
  XOR A                   ; zero them
  LD (IX+$0B),A           ;
  LD (IX+$0C),A           ;
  LD (IX+$0E),A           ;
  LD (IX+$0F),A           ;
  RET                     ;

; Put a shot at the ship
;
; Used by the routine at fire.
;
; The shot's world position is the ship's (view_page_column and view_page_row,
; its record's +1 to +5) and its first move, in +9, is down to the ship's shot
; line (ship_line) from the ship's bottom line (ship_screen_y). Its speed
; across is 8 pixels (facing left) or 24 (facing right), for fire to move it
; ahead of the ship's nose.
;
; IX the shot's record
place_shot:
  LD A,(ship_screen_y)    ; the line, from the ship's bottom line
  LD B,A                  ;
  LD A,(ship_line)        ;
  SUB B                   ;
  LD (IX+$09),A           ;
  LD HL,(view_page_column) ; the ship's place in the world
  LD DE,(view_page_row)    ;
  LD (IX+$01),L            ;
  LD (IX+$02),H            ;
  LD (IX+$04),E            ;
  LD (IX+$05),D            ;
  LD (IX+$07),$08         ; 8 ahead, or 24 facing right
  BIT 7,(IX+$00)          ;
  RET NZ                  ;
  LD (IX+$07),$18         ;
  RET                     ;

; The missile's frames
;
; Type 0's list of frames (object_graphic_lists): two words, missile_frame_a
; and missile_frame_b. A missile is drawn with these.
missile_frames:
  DEFW missile_frame_a    ; The two frames
  DEFW missile_frame_b    ;

; The missile, first frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; One byte wide and three lines high: a width, a height and three bytes, bottom
; row first.
missile_frame_a:
  DEFB $01,$03,$BC,$BF,$BC

; The missile, second frame
;
; As the compositor draw_sprite_pair draws it in play (three times the size;
; sprites have no colour of their own).
;
; As missile_frame_a.
missile_frame_b:
  DEFB $01,$03,$3D,$FD,$3D

; An empty list of landscape boxes
;
; A count of 0: find_page_boxes points IX here when a record's box is in a page
; that has no list (one off the screen), so collide_with_landscape finds no
; boxes. The byte after it is not used as far as read.
empty_box_list:
  DEFB $00,$FF

; Find the list of landscape boxes for a page
;
; Used by the routine at place_collision_box.
;
; The landscape's collision boxes are kept page by page: the world is a ring of
; $60 pages across, each 224 pixels wide (x 8 to $E7), and a stack of pages
; down, each 160 lines high (y $18 to $B7). The screen shows parts of four at
; once, and draw_landscape builds a list of boxes for each of them in the four
; work areas: loader_entry for the view's page (view_column across, view_row
; down), ground_cell_bottom_left for the one below it, ground_cell_top_right
; for the one to its right, ground_cell_bottom_right for the one below and
; right. This finds the list for page D across, B down; a page off the screen
; gets the empty list at empty_box_list.
;
; Each list is a count, then five bytes a box: +0 its left x, +1 its bottom
; line, +2 its width, +3 its height (or, for a slope, its other edge's line),
; +4 its kind (see collide_with_landscape).
;
;   B the page down
;   D the page across
; O:IX the list
; O:F Z if found
find_page_boxes:
  LD E,B                  ; E = the page down
  LD IX,loader_entry      ; the view's page
  LD BC,(view_column)     ;
  CALL page_matches       ;
  RET Z                   ;
  LD IX,ground_cell_bottom_left ; the one below
  INC B                         ;
  CALL page_matches             ;
  RET Z                         ;
  LD IX,ground_cell_bottom_right ; the one below and right (across wraps at
  INC C                          ; $60)
  LD A,C                         ;
  CP $60                         ;
  JR NZ,find_page_boxes_0        ;
  LD C,$00                       ;
find_page_boxes_0:
  CALL page_matches              ;
  RET Z                          ;
  DEC B                       ; the one to the right
  LD IX,ground_cell_top_right ;
  CALL page_matches           ;
  RET Z                       ;
  LD IX,empty_box_list    ; none: the empty list
  RET                     ;

; Is page (D, E) page (C, B)?
;
; Used by the routine at find_page_boxes.
;
; O:F Z if so
page_matches:
  LD A,D                  ; across, then down
  CP C                    ;
  RET NZ                  ;
  LD A,E                  ;
  CP B                    ;
  RET                     ;

; Move a record across the world by its speed
;
; Used by the routines at leave_slope, place_collision_box and move_object.
;
; Adds the speed (+7 +8) to the position (+2 +3), and steps the page (+1) when
; the position leaves 8 to $E7: up a page past $E7, wrapping from $5F to 0,
; down a page below 8, wrapping from 0 to $5F. Both a run past the end and an
; 8-bit overflow count, so speeds up to 32 pixels a pass are safe. The record
; is not changed: move_object stores the result, and place_collision_box uses
; it as the place to test.
;
;   IX the record
; O:H the page across
; O:L the x
; O:E the fraction
move_across:
  LD H,(IX+$02)           ; HL = x and fraction plus the speed; E the fraction,
  LD L,(IX+$03)           ; L the x, H the page, C the old x
  LD D,(IX+$07)           ;
  LD E,(IX+$08)           ;
  ADD HL,DE               ;
  LD E,L                  ;
  LD L,H                  ;
  LD H,(IX+$01)           ;
  LD C,(IX+$02)           ;
  LD A,L                  ; moving right: in the page still
  BIT 7,D                 ;
  JR NZ,move_across_1     ;
  CP $E8                  ;
  JR NC,move_across_0     ;
  CP C                    ;
  RET NC                  ;
move_across_0:
  SUB $E0                 ; into the next page, from $5F round to 0
  LD L,A                  ;
  INC H                   ;
  LD A,H                  ;
  SUB $60                 ;
  RET NZ                  ;
  LD H,A                  ;
  RET                     ;
move_across_1:
  CP $08                  ; moving left: in the page still
  JR C,move_across_2      ;
  CP C                    ;
  RET C                   ;
  RET Z                   ;
move_across_2:
  ADD A,$E0               ; into the page before, from 0 round to $5F
  LD L,A                  ;
  DEC H                   ;
  LD A,H                  ;
  INC A                   ;
  RET NZ                  ;
  LD H,$5F                ;
  RET                     ;

; Move a record up or down the world by its speed
;
; Used by the routines at build_ship_sprite, place_collision_box and
; move_object.
;
; As move_across for the y (+4 page, +5 line, +6 fraction, +9 +A speed, down
; positive): a page is 160 lines, $18 to $B7, and the pages do not wrap.
;
;   IX the record
; O:H the page down
; O:L the line
; O:E the fraction
move_down:
  LD H,(IX+$05)           ; HL = line and fraction plus the speed; moving down:
  LD L,(IX+$06)           ; in the page still
  LD D,(IX+$09)           ;
  LD E,(IX+$0A)           ;
  ADD HL,DE               ;
  LD E,L                  ;
  LD L,H                  ;
  LD H,(IX+$04)           ;
  LD C,(IX+$05)           ;
  LD A,L                  ;
  BIT 7,D                 ;
  JR NZ,move_down_1       ;
  CP $B8
  JR NC,move_down_0
  CP C
  RET NC
move_down_0:
  SUB $A0                 ; into the page below
  LD L,A                  ;
  INC H                   ;
  RET                     ;
move_down_1:
  CP $18                  ; moving up: in the page still
  JR C,move_down_2        ;
  CP C                    ;
  RET C                   ;
  RET Z                   ;
move_down_2:
  ADD A,$A0               ; into the page above
  LD L,A                  ;
  DEC H                   ;
  RET                     ;

; Set the ship's speed in the world from this pass's scroll
;
; Used by the routines at toggle_carried_part, build_ship_sprite and ship_pass.
;
; The ship's record, at ship_record, holds its position in the world (+1 to +6)
; like any object's; it moves exactly as the landscape scrolls, the other way:
; across and up by minus the steps in scroll_down, whole pixels.
;
; O:IX the ship's record
ship_speed_from_scroll:
  LD IX,ship_record       ; the speeds, no fractions
  LD A,(scroll_across)    ;
  NEG                     ;
  LD (IX+$07),A           ;
  LD A,(scroll_down)      ;
  NEG                     ;
  LD (IX+$09),A           ;
  LD (IX+$08),$00         ;
  LD (IX+$0A),$00         ;
  RET                     ;

; The record being tested against the landscape
;
; collide_with_landscape keeps IX here while IX walks the lists of boxes.
collision_object:
  DEFW $0000

; Zero a speed too small to matter
;
; Used by the routines at reverse_speed, special_beam_key, hit_block and
; leave_slope.
;
; A speed (8.8) from -0.65 to 0.39 (-$A8 to $63 in 256ths) becomes 0. The
; landscape bounces (hit_block, leave_slope) use it so that a bounce dies away;
; special_beam_key to tell whether the ship is still.
;
;   HL the speed
; O:HL the speed, or 0
dead_band:
  LD A,H                  ; 0 to $63: 0
  AND A                   ;
  JR NZ,dead_band_0       ;
  LD A,L                  ;
  CP $64                  ;
  RET NC                  ;
  LD HL,$0000             ;
  RET                     ;
dead_band_0:
  INC A                   ; -$A8 to -1: 0
  RET NZ                  ;
  LD A,L                  ;
  CP $58                  ;
  RET C                   ;
  LD HL,$0000             ;
  RET                     ;

; Test a record against the landscape
;
; Used by the routines at test_landscape_at and ship_pass.
;
; The engine's collision with the ground, for the ship (ship_pass, piece_step_x
; $FF), the shots' probes (probe_shot, piece_step_x $80) and the objects
; (test_landscape_at, piece_step_x 0). The record's box -- C pixels wide and B
; lines high, up from its bottom line -- is placed where its speed would take
; it (place_collision_box), cut into the parts in each page it overlaps (one,
; two or four), and each part tested against every box in that page's list
; (find_page_boxes). scratch_count is non-zero for a hit; for the ship and the
; objects the speed is also changed, a bounce or a slide.
;
; A box's kind, its +4: if any of bits 2-5 is set it is a solid block
; (hit_block): bits 4-5 clear make it a floor (bit 2: direction 0 in
; piece_classes) or a ceiling (bit 3: direction 7), a floor tested only while
; the record moves down onto it and a ceiling only while it moves up into it
; (either, too, while it does not move up or down); bit 4 or 5 set makes it a
; wall, tested by the way it is moving across. If bits 2-5 are all clear it is
; a slope (hit_slope), its shape tested by test_landscape_piece: bits 0-2 the
; slope's kind (2 a 45-degree one), bits 6-7 which way it faces.
;
; piece_ceiling keeps track of what has happened this pass: bit 7 a block hit
; (no more blocks tested), bit 6 a bounce (no more boxes at all), bit 4 a slope
; hit (no more slopes).
;
;   B the height in lines
;   C the width in pixels
;   IX the record
; O:IX the record
collide_with_landscape:
  XOR A                    ; no hit yet; keep IX; set up the first part
  LD (scratch_count),A     ;
  LD (collision_object),IX ;
  CALL place_collision_box ;
  JR next_part_boxes_0     ;

; Test the next part of a record's box against its page's boxes
;
; Used by the routine at next_box.
;
; The loop of collide_with_landscape: from the top it sets up the next part
; (next_part); collide_with_landscape joins after that for the first. For each
; box in the page's list, its kind goes into pieces_left and it is tested as a
; solid block (hit_block) or a slope (hit_slope); both come back to next_box.
next_part_boxes:
  CALL next_part          ; the next part's place and page (next_part)
; This entry point is used by the routine at collide_with_landscape.
next_part_boxes_0:
  LD A,(IX+$00)           ; the page's list: empty, the next part
  AND A                   ;
  JR Z,next_box_1         ;
  INC IX                  ;
; This entry point is used by the routine at next_box.
next_part_boxes_1:
  PUSH IX                 ; for each box: no hit yet; its kind in pieces_left;
  EX AF,AF'               ; a block or a slope
  XOR A                   ;
  LD (scratch_count),A    ;
  LD A,(IX+$04)           ;
  LD (pieces_left),A      ;
  AND $3C                 ;
  JR NZ,hit_block         ;
  JP hit_slope            ;

; The next box, the next part, or done
;
; Used by the routines at hit_block and hit_slope.
;
; The loop's tail: after a bounce (bit 6 of piece_ceiling) the test ends;
; otherwise the next box, then the next part (next_part_boxes), until
; ground_cell, the parts left, is 0. When the ship has gone through all of them
; without stopping, ship_on_slope is cleared. The last part is where a test
; stops early: a shot stops at its first hit; the ship and the objects stop at
; a bounce, a slope or a block -- except an object once the ship has docked
; with the rocket or is dying (death_count 10 or more), which goes on to the
; other boxes.
next_box:
  LD A,(piece_ceiling)    ; a bounce: stop
  BIT 6,A                 ;
  JR NZ,next_box_2        ;
next_box_0:
  POP IX                  ; the next box
  LD DE,$0005             ;
  ADD IX,DE               ;
  EX AF,AF'               ;
  DEC A                   ;
  JR NZ,next_part_boxes_1 ;
; This entry point is used by the routine at next_part_boxes.
next_box_1:
  LD A,(ground_cell)      ; the next part
  DEC A                   ;
  LD (ground_cell),A      ;
  JP NZ,next_part_boxes   ;
  LD IX,(collision_object) ; all done; IX back; the ship: not on a slope
  LD A,(piece_step_x)      ;
  INC A                    ;
  RET NZ                   ;
  LD (ship_on_slope),A     ;
  RET                      ;
; This entry point is used by the routines at hit_block, hit_slope and
; ship_on_slope_response.
next_box_2:
  LD A,(death_count)      ; stop, unless an object after docking or during a
  CP $0A                  ; death
  JR C,next_box_3         ;
  LD A,(piece_step_x)     ;
  AND A                   ;
  JR NZ,next_box_3        ;
  JP next_box_0           ;
next_box_3:
  POP IX                   ; IX back
  LD IX,(collision_object) ;
  RET                      ;

; A word kept for a moment
;
; leave_slope keeps an object's speed across here while it pushes the object
; out of a slope; the sprite code at erase_old_draw_new keeps a screen address
; here; landscape_end_pieces reads it too.
saved_speed:
  DEFW $0000

; Test a part against a solid block, and bounce
;
; Used by the routine at next_part_boxes.
;
; The block is tested only if the record is moving into it: a floor (kind bit 2
; set) when it moves down or not at all (last_speed_up, the climb speed it had
; last pass negated, so positive while it falls), a ceiling (bit 2 clear: bit 3
; is set) when it moves up or not at all, a wall by its speed across
; (last_speed_across; bit 4 of the kind says which way). Then the boxes are
; tested for overlap (boxes_overlap). A shot that hits stops there.
;
; Floors and ceilings: the climb speed is reversed and halved (negate_hl,
; halve_hl), clamped (clamp_object_speed) and dropped if small (dead_band). For
; the ship the new speed sets this pass's step at once (move_ship's second
; entry) with a bump (sound_crackle). An object hitting it has its screen
; fields cleared and, on a floor (bit 3 clear), its bottom line put 20 above
; the box's; one that has stopped under a ceiling is given a speed of 2
; downwards (+9 = $FE while the collision code has its speeds turned round), so
; that it drops away. A ship hitting a ceiling (bit 3) without add-on 9 sets
; trail_hold to 4 (counted down in draw_trail).
;
; Walls: the speed across reversed; for the ship halved, clamped and dropped if
; small, then this pass's step across worked out again (accelerate_across's
; second entry) with a sound (sound_buzz), and trail_collision set.
;
; Also, before any of this, for the ship: a floor (bit 2) whose bottom line is
; 0 to 40 below the ship's and which spans part of x+4 to x+40 sets
; lift_jet_skip, which stops lift_jet drawing the lift jet below the ship that
; add-on 5 brings -- unless add-on 2 is fitted.
;
; Every branch ran in the build's sessions, the bit-3 boxes, the walls and the
; hop included.
;
; IX the box
hit_block:
  LD A,(piece_ceiling)    ; a block already hit this pass
  BIT 7,A                 ;
  JP NZ,next_box          ;
  LD HL,(box_x)           ; HL = the part's x and bottom line; a shot: just
  LD A,(piece_step_x)     ; test
  CP $80                  ;
  JR Z,hit_block_6        ;
  LD A,(piece_step_x)     ; an object
  INC A                   ;
  JP NZ,hit_block_2       ;
  LD A,L                  ; the ship: the span under it, x+4 to x+40
  ADD A,$04               ;
  CP $E8                  ;
  JR C,hit_block_0        ;
  LD A,$E8                ;
hit_block_0:
  LD D,A                  ;
  ADD A,$24               ;
  JR NC,hit_block_1       ;
  LD A,$F8                ;
hit_block_1:
  LD E,A                  ;
  BIT 2,(IX+$04)          ; a floor (bit 2) under that span, 0 to 40 lines
  JR Z,hit_block_2        ; down, and no add-on 2: lift_jet_skip
  LD A,(addon_two)        ;
  AND A                   ;
  JR NZ,hit_block_2       ;
  LD A,(IX+$01)           ;
  SUB H                   ;
  JR C,hit_block_2        ;
  CP $29                  ;
  JR NC,hit_block_2       ;
  LD A,(IX+$00)           ;
  CP E                    ;
  JR NC,hit_block_2       ;
  ADD A,(IX+$02)          ;
  CP D                    ;
  JR C,hit_block_2        ;
  LD (lift_jet_skip),A    ;
hit_block_2:
  LD A,(IX+$04)           ; a wall?
  AND $30                 ;
  JR NZ,hit_block_4       ;
  LD A,(last_speed_up)    ; a floor or ceiling: tested only if the record moves
  AND A                   ; into it
  JR Z,hit_block_6        ;
  BIT 2,(IX+$04)          ;
  JR NZ,hit_block_3       ;
  NEG                     ;
hit_block_3:
  BIT 7,A                 ;
  JP NZ,next_box          ;
  JR hit_block_6          ;
hit_block_4:
  BIT 4,A                  ; a wall: likewise, across
  LD A,(last_speed_across) ;
  JR NZ,hit_block_5        ;
  NEG                      ;
hit_block_5:
  AND A                    ;
  JR Z,hit_block_6         ;
  BIT 7,A                  ;
  JP Z,next_box            ;
  JR hit_block_6           ;
hit_block_6:
  LD DE,(box_height)      ; do they overlap (boxes_overlap)?
  CALL boxes_overlap      ;
  LD A,(scratch_count)    ;
  AND A                   ;
  JP Z,next_box           ;
  LD A,(piece_step_x)     ; a shot: stop
  CP $80                  ;
  JP Z,next_box_2         ;
  LD A,$80                ; a block hit; the box's bottom line less 20, and its
  LD (piece_ceiling),A    ; kind
  LD A,(IX+$01)           ;
  SUB $14                 ;
  LD (current_piece),A    ;
  LD A,(IX+$04)            ; IX the record again; a wall?
  LD ($DEDD),A             ;
  LD B,A                   ;
  AND $30                  ;
  LD IX,(collision_object) ;
  JP NZ,hit_block_11       ;
  LD A,(piece_step_x)     ; the ship hitting a ceiling (bit 3) without add-on
  AND A                   ; 9: trail_hold = 4
  JR Z,hit_block_7        ;
  BIT 3,B                 ;
  JR Z,hit_block_8        ;
  LD A,(addon_nine)       ;
  AND A                   ;
  JR NZ,hit_block_8       ;
  LD A,$04                ;
  LD (trail_hold),A       ;
  JR hit_block_8          ;
hit_block_7:
  CALL clear_screen_fields ; an object: clear its screen fields; on a floor,
  BIT 3,B                  ; sit it 20 above the box's bottom
  JR NZ,hit_block_8        ;
  LD A,(current_piece)     ;
  LD (IX+$05),A            ;
hit_block_8:
  LD H,(IX+$09)           ; the climb speed reversed, halved, clamped, dropped
  LD L,(IX+$0A)           ; if small; keep it
  CALL negate_hl          ;
  CALL halve_hl           ;
  CALL clamp_object_speed ;
  CALL dead_band          ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  LD A,H                  ;
  LD (last_speed_up),A    ;
  OR L                    ; still moving: a bounce
  JR Z,hit_block_9        ;
  LD A,(piece_ceiling)    ;
  OR $40                  ;
  LD (piece_ceiling),A    ;
  JR hit_block_10         ;
hit_block_9:
  LD A,(piece_step_x)     ; stopped: an object under a ceiling drops away at 2
  INC A                   ;
  JR Z,hit_block_10       ;
  BIT 3,B                 ;
  JP Z,next_box           ;
  LD (IX+$09),$FE         ;
hit_block_10:
  LD A,(piece_step_x)     ; the ship: a bump; this pass's step from the new
  INC A                   ; speed
  JP NZ,next_box          ;
  LD A,L                  ;
  OR H                    ;
  CALL NZ,sound_crackle   ;
  LD DE,$0000             ;
  LD (climb_fraction),DE  ;
  CALL move_ship_7        ;
  LD A,($DEDD)            ; a box of kind exactly 4 (the flat floor), and the
  CP $04                  ; ship's line below the box's bottom less 20: this
  JP NZ,next_box          ; pass's step up is 2
  LD A,(view_page_y)      ;
  LD B,A                  ;
  LD A,(current_piece)    ;
  CP B                    ;
  JP NC,next_box          ;
  LD A,$02                ;
  LD (scroll_down),A      ;
  JP next_box             ;
hit_block_11:
  LD H,(IX+$07)            ; a wall: the speed across reversed; an object just
  LD L,(IX+$08)            ; has its screen fields cleared
  CALL negate_hl           ;
  LD A,(piece_step_x)      ;
  AND A                    ;
  JR NZ,hit_block_12       ;
  CALL clear_screen_fields ;
  JR hit_block_13          ;
hit_block_12:
  CALL halve_hl            ; the ship: halved, clamped, dropped if small; keep
  CALL clamp_object_speed  ; it
  CALL dead_band           ;
hit_block_13:
  LD (IX+$07),H            ;
  LD (IX+$08),L            ;
  LD A,H                   ;
  LD (last_speed_across),A ;
  LD A,(piece_step_x)          ; the ship: a sound; this pass's step across
  INC A                        ; again; trail_collision
  JP NZ,next_box               ;
  LD A,L                       ;
  OR H                         ;
  CALL NZ,sound_buzz           ;
  LD DE,$0000                  ;
  LD (ship_position_across),DE ;
  CALL speed_to_scroll         ;
  LD A,$01                     ;
  LD (trail_collision),A       ;
  JP next_box                  ;

; The ship was on a slope last pass
;
; $80 after the ship touches a slope (ship_on_slope_response), cleared when it
; goes through a whole test without stopping (next_box). A second pass on a
; slope slides the ship 2 pixels downhill.
ship_on_slope:
  DEFB $00

; Test a part against a slope, and bounce off it
;
; Used by the routine at next_part_boxes.
;
; One slope a pass (piece_ceiling bit 4). test_landscape_piece tests the part
; against the slope's shape and leaves in touch_depth how far into it the part
; is. A shot that hits stops; the ship sets trail_collision to $FE. Then the
; speed is reflected, by the slope's kind and facing: a 45-degree slope swaps
; the speeds across and up (bounce_45_negate, bounce_45_swap_only), any other
; mixes them with factors of 3/8 and 3/4 (bounce_shallow_negate,
; bounce_shallow).
;
; IX the box
hit_slope:
  LD A,(piece_ceiling)    ; a slope already hit this pass
  BIT 4,A                 ;
  JP NZ,next_box          ;
  CALL test_landscape_piece ; the shape test (test_landscape_piece)
  LD A,(scratch_count)      ;
  AND A                     ;
  JP Z,next_box             ;
  LD A,(piece_ceiling)    ; a slope hit; a shot: stop
  OR $10                  ;
  LD (piece_ceiling),A    ;
  LD A,(piece_step_x)     ;
  CP $80                  ;
  JP Z,next_box_2         ;
  AND A                   ; the ship: trail_collision = $FE
  JR Z,hit_slope_0        ;
  DEC A                   ;
  LD (trail_collision),A  ;
hit_slope_0:
  LD A,(IX+$04)           ; kind 2, facing bit 6 clear: swap and negate
  LD (pieces_left),A      ;
  AND $7F                 ;
  CP $02                  ;
  JP Z,bounce_45_negate   ;
  AND $07                  ; kind 2, facing bit 6 set: swap
  CP $02                   ;
  JP Z,bounce_45_swap_only ;
  LD A,(IX+$04)              ; other kinds, by bit 6
  BIT 6,A                    ;
  JP Z,bounce_shallow_negate ;
  JP bounce_shallow          ;

; Push an object out of a slope, and keep it sliding
;
; Used by the routines at bounce_45_negate and bounce_shallow_negate.
;
; Where every slope bounce ends. An object (not the ship) is moved across out
; of the slope by the depth test_landscape_piece found, through move_across
; with that as its speed for a moment; a push of 16 or more sets its +10 bit 3.
; Its screen fields are cleared. If its new speeds are both too small to
; matter, it is set sliding a pixel a pass downhill (downhill). Then
; ship_on_slope_response for the ship.
;
; O:IX the record
leave_slope:
  LD IX,(collision_object) ; the ship: no pushing
  LD A,(piece_step_x)      ;
  INC A                    ;
  JR Z,leave_slope_2       ;
  LD L,(IX+$07)           ; the depth as the speed across for a moment; a deep
  LD H,(IX+$08)           ; one sets +10 bit 3
  LD (saved_speed),HL     ;
  LD A,(touch_depth)      ;
  NEG                     ;
  LD (IX+$07),A           ;
  BIT 7,A                 ;
  JR Z,leave_slope_0      ;
  NEG                     ;
leave_slope_0:
  CP $10                  ;
  JR C,leave_slope_1      ;
  SET 3,(IX+$10)          ;
leave_slope_1:
  LD (IX+$08),$00         ; move across by it; the speed back
  CALL move_across        ;
  LD (IX+$01),H           ;
  LD (IX+$02),L           ;
  LD HL,(saved_speed)     ;
  LD (IX+$07),L           ;
  LD (IX+$08),H           ;
leave_slope_2:
  CALL clear_screen_fields ; clear the screen fields
  LD H,(IX+$09)                ; both speeds too small: slide downhill
  LD L,(IX+$0A)                ;
  CALL dead_band               ;
  LD A,L                       ;
  OR H                         ;
  JR NZ,ship_on_slope_response ;
  LD H,(IX+$07)                ;
  LD L,(IX+$08)                ;
  CALL dead_band               ;
  LD (IX+$08),L                ;
  LD A,H                       ;
  OR L                         ;
  JR NZ,ship_on_slope_response ;
  CALL downhill                ;
  JR ship_on_slope_response    ;

; Set a record sliding a pixel a pass downhill
;
; Used by the routines at leave_slope and ship_on_slope_response.
;
; By the slope's facing (pieces_left bits 6-7): both clear or both set, right;
; otherwise left.
;
; IX the record
downhill:
  LD A,(pieces_left)      ; +1, or -1
  LD (IX+$07),$01         ;
  AND $C0                 ;
  RET Z                   ;
  XOR $C0                 ;
  RET Z                   ;
  LD (IX+$07),$FF         ;
  RET                     ;

; The ship on a slope
;
; Used by the routine at leave_slope.
;
; An object is done (next_box's stop). The ship stops scrolling this pass, with
; a sound unless it is being knocked sideways (fuel_saver) or was on the slope
; last pass; on its second pass in a row it slides 2 pixels downhill. Marks
; ship_on_slope. All of it ran in the build's sessions.
ship_on_slope_response:
  LD A,(piece_step_x)     ; an object: stop
  AND A                   ;
  JP Z,next_box_2         ;
  XOR A                   ; the ship: no scroll this pass
  LD (scroll_across),A    ;
  LD (scroll_down),A      ;
  LD A,(fuel_saver)              ; a sound, if new to the slope and not knocked
  AND A                          ;
  JR NZ,ship_on_slope_response_0 ;
  LD A,(ship_on_slope)           ;
  AND A                          ;
  CALL Z,sound_buzz              ;
ship_on_slope_response_0:
  LD A,(ship_on_slope)          ; on the slope last pass too: 2 pixels downhill
  AND A                         ;
  JR Z,ship_on_slope_response_1 ;
  CALL downhill                 ;
  LD A,(IX+$07)                 ;
  ADD A,A                       ;
  LD (scroll_across),A          ;
ship_on_slope_response_1:
  LD A,$80                ; on a slope; stop
  LD (ship_on_slope),A    ;
  JP next_box_2           ;

; Bounce off a 45-degree slope, swapping and negating
;
; Used by the routine at hit_slope.
;
; The speeds across and up are swapped, each clamped (clamp_object_speed): a
; reflection off a line at 45 degrees. From here both are negated on the way;
; from bounce_45_swap_only they are not. Which is done is decided by two JRs in
; this routine whose displacements are patched: 0 runs the CALL to
; negate_high_byte after each, 3 jumps over it.
;
; IX the box
bounce_45_negate:
  XOR A                   ; the negations on
; This entry point is used by the routine at bounce_45_swap_only.
bounce_45_negate_0:
  LD ($BD16),A            ; (bounce_45_swap_only's entry) the two patched JRs'
  LD ($BD2B),A            ; displacements
  LD IX,(collision_object) ; the record; HL = its speed across
  LD H,(IX+$07)            ;
  LD L,(IX+$08)            ;
bounce_45_negate_1:
  JR bounce_45_negate_1   ; patched: run the CALL or jump over it
  CALL negate_high_byte   ; negate the speed across
  CALL clamp_object_speed ; clamp it; it becomes the speed up and down; HL the
  EX DE,HL                ; old one
  LD H,(IX+$09)           ;
  LD L,(IX+$0A)           ;
  LD (IX+$09),D           ;
  LD (IX+$0A),E           ;
bounce_45_negate_2:
  JR bounce_45_negate_2   ; patched: run the CALL or jump over it
  CALL negate_high_byte   ; negate it
  CALL clamp_object_speed ; clamp it; it becomes the speed across; push the
  LD (IX+$07),H           ; object out (leave_slope)
  LD (IX+$08),L           ;
  JP leave_slope          ;

; Bounce off a 45-degree slope, swapping
;
; Used by the routine at hit_slope.
;
; bounce_45_negate with the negations jumped over.
bounce_45_swap_only:
  LD A,$03                ; the JRs jump 3
  JR bounce_45_negate_0   ;

; Negate H
;
; Used by the routines at bounce_45_negate and slope_negate.
;
; Negates the whole part of an 8.8 speed, leaving the fraction: near enough a
; negation for a bounce.
;
; H the whole part
negate_high_byte:
  LD A,H                  ; 0 - H
  NEG                     ;
  LD H,A                  ;
  RET                     ;

; Bounce off a slope that is not 45 degrees
;
; Used by the routine at hit_slope.
;
; The new speeds are a mix of the old: across = 3/8 across + 3/8 up and down,
; up and down = -3/4 up and down + 3/4 across, the terms from the other speed
; negated or not (slope_negate) as two JRs in this routine decide: their
; displacements are patched to 0 to run the CALL after each (from here) or to 3
; to jump over it (from bounce_shallow). For kind 3 slopes the speeds are
; swapped before and after (swap_speeds), which mirrors the mix about the
; diagonal: the same rule serves the steep slopes and the shallow. Each result
; is clamped (clamp_object_speed). The factors shrink the speed, so a bounce
; loses energy.
;
; IX the box
bounce_shallow_negate:
  XOR A                   ; the negations on
  LD ($BD85),A            ;
  LD ($BD74),A            ;
; This entry point is used by the routine at bounce_shallow.
bounce_shallow_negate_0:
  LD A,(IX+$04)            ; (bounce_shallow's entry) the box's kind in C; kind
  LD C,A                   ; 3: swap the speeds first
  AND $07                  ;
  PUSH AF                  ;
  LD IX,(collision_object) ;
  CP $03                   ;
  CALL Z,swap_speeds       ;
  LD H,(IX+$07)           ; 3/8 of the speed across, kept for later
  LD L,(IX+$08)           ;
  CALL three_eighths      ;
  LD (saved_step_x),HL    ;
  LD H,(IX+$07)           ; 3/4 of it
  LD L,(IX+$08)           ;
  CALL double_hl          ;
  CALL three_eighths      ;
bounce_shallow_negate_1:
  JR bounce_shallow_negate_1 ; patched: run the CALL or jump over it
  CALL slope_negate       ; negate it
  LD (saved_step_y),HL    ; keep it; 3/8 of the speed up and down
  LD H,(IX+$09)           ;
  LD L,(IX+$0A)           ;
  CALL three_eighths      ;
bounce_shallow_negate_2:
  JR bounce_shallow_negate_2 ; patched: run the CALL or jump over it
  CALL slope_negate       ; negate it
  LD DE,(saved_step_x)    ; across: plus the 3/8 across, clamped
  ADD HL,DE               ;
  CALL clamp_object_speed ;
  LD (IX+$07),H           ;
  LD (IX+$08),L           ;
  LD H,(IX+$09)           ; up and down: 3/4 of it negated, plus the 3/4
  LD L,(IX+$0A)           ; across, clamped
  CALL double_hl          ;
  CALL three_eighths      ;
  CALL slope_negate       ;
  LD DE,(saved_step_y)    ;
  ADD HL,DE               ;
  CALL clamp_object_speed ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  POP AF                  ; kind 3: swap back; push the object out
  CP $03                  ; (leave_slope)
  CALL Z,swap_speeds      ;
  JP leave_slope          ;

; Bounce off a slope that is not 45 degrees, without the negations
;
; Used by the routine at hit_slope.
;
; bounce_shallow_negate with its two patched JRs jumping over the CALLs.
bounce_shallow:
  LD A,$03                   ; the JRs jump 3
  LD ($BD85),A               ;
  LD ($BD74),A               ;
  JP bounce_shallow_negate_0 ;

; Negate a term of a slope bounce
;
; Used by the routine at bounce_shallow_negate.
;
; For a box with bit 7 of its kind set, or of kind 3, the whole word is negated
; (negate_hl); otherwise only its whole part (negate_high_byte).
;
; C the box's kind
; HL the term
slope_negate:
  BIT 7,C                 ; bit 7 or kind 3: all of it; otherwise the whole
  JP NZ,negate_hl         ; part
  LD A,C                  ;
  AND $07                 ;
  CP $03                  ;
  JP Z,negate_hl          ;
  JP negate_high_byte     ;

; Swap a record's speeds across and up and down
;
; Used by the routine at bounce_shallow_negate.
;
; IX the record
swap_speeds:
  LD H,(IX+$07)           ; +7 +8 and +9 +A change places
  LD L,(IX+$08)           ;
  LD D,(IX+$09)           ;
  LD E,(IX+$0A)           ;
  LD (IX+$07),D           ;
  LD (IX+$08),E           ;
  LD (IX+$09),H           ;
  LD (IX+$0A),L           ;
  RET                     ;

; Three eighths of HL
;
; Used by the routine at bounce_shallow_negate.
;
;   HL the value
; O:HL 3/8 of it
three_eighths:
  CALL halve_hl           ; a quarter plus an eighth
  CALL halve_hl           ;
  PUSH HL                 ;
  POP DE                  ;
  CALL halve_hl           ;
  ADD HL,DE               ;
  RET                     ;

; Double HL
;
; Used by the routine at bounce_shallow_negate.
double_hl:
  SLA L                   ; shift left
  RL H                    ;
  RET                     ;

; Halve HL, keeping its sign
;
; Used by the routines at reverse_speed, hit_block and three_eighths.
halve_hl:
  SRA H                   ; arithmetic shift right
  RR L                    ;
  RET                     ;

; Negate HL
;
; Used by the routines at ship_contact, contact_push, negate_velocity,
; drop_part, reverse_speed, hit_block, slope_negate and draw_trail.
negate_hl:
  LD A,H                  ; invert and add one
  CPL                     ;
  LD H,A                  ;
  LD A,L                  ;
  CPL                     ;
  LD L,A                  ;
  INC HL                  ;
  RET                     ;

; Set the next part to the page above
;
; Used by the routine at next_part.
;
; For a record whose box reaches above the top of its page: the part in the
; page above, from its bottom line ($B7) up by what stuck out (stick_out_up),
; its full width.
part_above:
  LD A,(stick_out_up)     ; the height that stuck out
  LD (piece_y),A          ;
  LD HL,(tile_mirrored_left_right) ; the page above, at its bottom line
  DEC H                            ;
  LD L,$B7                         ;
  LD (moved_y),HL                  ;
  LD A,(piece_page_x)     ; the full width
  LD (piece_x),A          ;
  RET                     ;

; Set the next part to the page to the right
;
; Used by the routine at next_part.
;
; For a record whose box reaches past the right of its page: the part in the
; next page across (from $5F round to 0), from its left edge (x 8) across by
; what stuck out (stick_out_right), its full height.
part_right:
  LD A,(stick_out_right)  ; the width that stuck out
  LD (piece_x),A          ;
  LD HL,(move_across_kept) ; the next page across, at its left edge
  INC H                    ;
  LD A,H                   ;
  CP $60                   ;
  JR NZ,part_right_0       ;
  LD H,$00                 ;
part_right_0:
  LD L,$08                 ;
  LD (moved_x),HL          ;
  LD A,(piece_page_y)     ; the full height
  LD (piece_y),A          ;
  RET                     ;

; Set up the next part of a record's box
;
; Used by the routine at next_part_boxes.
;
; After the part in the record's own page: the part to the right, then the part
; above, and when the box reaches past both edges, last, the part above and
; right (both at once). Each goes on to work out the box in its page
; (place_collision_box's second half). direction_flag is set while the part to
; the right is still to do.
next_part:
  LD A,(ground_cell)       ; the last of four: above and right
  CP $01                   ;
  JR NZ,next_part_0        ;
  LD A,(stick_out_right)   ;
  AND A                    ;
  JR Z,next_part_0         ;
  LD A,(stick_out_up)      ;
  AND A                    ;
  JR Z,next_part_0         ;
  CALL part_above          ;
  CALL part_right          ;
  LD A,(stick_out_up)      ;
  LD (piece_y),A           ;
  JP place_collision_box_2 ;
next_part_0:
  LD A,(stick_out_right)           ; the part to the right, at the record's own
  AND A                            ; line
  JR Z,next_part_1                 ;
  LD A,(direction_flag)            ;
  AND A                            ;
  JR Z,next_part_1                 ;
  XOR A                            ;
  LD (direction_flag),A            ;
  CALL part_right                  ;
  LD HL,(tile_mirrored_left_right) ;
  LD (moved_y),HL                  ;
  JP place_collision_box_2         ;
next_part_1:
  CALL part_above          ; the part above, at the record's own x
  LD HL,(move_across_kept) ;
  LD (moved_x),HL          ;
  JP place_collision_box_2 ;

; The speed across of the record being tested
;
; Set by place_collision_box: the whole part of the record's speed across (for
; the ship, last pass's). hit_block tests a wall only if the record is moving
; into it, and updates this after a bounce.
last_speed_across:
  DEFB $00

; The climb speed of the record being tested, negated
;
; Set by place_collision_box: the whole part of the record's speed up and down
; (+9; for the ship, last pass's climb speed), negated. The collision code
; works with up positive -- the objects' speeds, kept the other way round, are
; turned round for it (negate_velocity) -- so this is positive while the record
; falls. hit_block tests a floor only while it is 0 or more and a ceiling only
; while it is 0 or less, and updates it after a bounce.
last_speed_up:
  DEFB $00

; Work out where a record's box is, and its part in the first page
;
; Used by the routine at collide_with_landscape.
;
; The box is the record's position after this pass's move (move_across,
; move_down; the record is not changed), C wide and B high from its bottom line
; up. The move is kept in moved_x and move_across_kept (across), and moved_y
; and tile_mirrored_left_right (down).
;
; Then, from the loading of moved_y (where next_part comes in for the other
; parts): the page's list of boxes (find_page_boxes), and the part of the box
; inside the page -- its x (box_x), bottom line (box_bottom), height
; (box_height) and width (box_width) -- with what sticks out past the right
; (stick_out_right) and above the top (stick_out_up). On the first part, the
; number of parts to test, 1, 2 or 4, goes into ground_cell.
;
; For the ship, its record's speeds are put back to last pass's for the bounce
; rules (ship_across_speed, ship_down_speed); for an object, they are negated
; (negate_velocity) -- the objects' speeds are kept the other way round.
;
;   B the box's height
;   C the box's width
;   IX the record
; O:IX the page's list of boxes
place_collision_box:
  LD (piece_page_x),BC    ; the size; nothing sticking out yet, no parts, no
  XOR A                   ; hits
  LD (stick_out_right),A  ;
  LD (stick_out_up),A     ;
  LD (ground_cell),A      ;
  LD (piece_ceiling),A    ;
  LD (direction_flag),A   ;
  CALL move_across                 ; the record's place after its move
  LD (moved_x),HL                  ;
  LD (move_across_kept),HL         ;
  CALL move_down                   ;
  LD (moved_y),HL                  ;
  LD (tile_mirrored_left_right),HL ;
  LD A,(piece_step_x)        ; an object: its speeds negated
  INC A                      ;
  JR Z,place_collision_box_0 ;
  DEC A                      ;
  CALL Z,negate_velocity     ;
  JR place_collision_box_1   ;
place_collision_box_0:
  LD HL,(ship_across_speed) ; the ship: last pass's speeds
  LD (ship_dx),HL           ;
  LD HL,(ship_down_speed)   ;
  LD (ship_dy),HL           ;
place_collision_box_1:
  LD A,(IX+$07)            ; the speeds for the bounce rules
  LD (last_speed_across),A ;
  LD A,(IX+$09)            ;
  NEG                      ;
  LD (last_speed_up),A     ;
  LD A,(piece_page_x)     ; the full size
  LD (piece_x),A          ;
  LD A,(piece_page_y)     ;
  LD (piece_y),A          ;
; This entry point is used by the routine at next_part.
place_collision_box_2:
  LD BC,(moved_y)         ; the page's list (find_page_boxes)
  LD DE,(moved_x)         ;
  CALL find_page_boxes    ;
  LD A,(moved_x)              ; past the right edge (x + width over $E8)?
  LD C,A                      ;
  LD A,(piece_x)              ;
  LD B,A                      ;
  LD A,C                      ;
  ADD A,$18                   ;
  ADD A,B                     ;
  LD E,B                      ;
  JR NC,place_collision_box_4 ;
  LD B,A                      ; yes: on the first part, what sticks out; this
  LD A,(ground_cell)          ; part is up to $E8
  AND A                       ;
  JR NZ,place_collision_box_3 ;
  LD A,B                      ;
  LD (stick_out_right),A      ;
  LD A,$01                    ;
  LD (direction_flag),A       ;
place_collision_box_3:
  LD A,$E8                    ;
  SUB C                       ;
  LD E,A                      ;
place_collision_box_4:
  LD A,E                  ; the part's width and x
  LD (box_width),A        ;
  LD A,C                  ;
  LD (box_x),A            ;
  LD A,(moved_y)              ; above the top (line - height under $18)?
  LD C,A                      ;
  LD A,(piece_y)              ;
  LD B,A                      ;
  LD A,C                      ;
  SUB $17                     ;
  SUB B                       ;
  LD E,B                      ;
  JR NC,place_collision_box_6 ;
  LD B,A                      ; yes: on the first part, what sticks out; this
  LD A,(ground_cell)          ; part is from line $18
  AND A                       ;
  JR NZ,place_collision_box_5 ;
  LD A,B                      ;
  NEG                         ;
  LD (stick_out_up),A         ;
place_collision_box_5:
  LD A,C                      ;
  SUB $17                     ;
  LD E,A                      ;
place_collision_box_6:
  LD A,E                  ; the part's height and line
  LD (box_height),A       ;
  LD A,C                  ;
  LD (box_bottom),A       ;
  LD A,(ground_cell)         ; on the first part: how many parts, 1, 2 or 4
  AND A                      ;
  RET NZ                     ;
  LD B,$01                   ;
  LD A,(stick_out_right)     ;
  AND A                      ;
  JR Z,place_collision_box_7 ;
  INC B                      ;
  LD A,(stick_out_up)        ;
  AND A                      ;
  JR Z,place_collision_box_7 ;
  INC B                      ;
place_collision_box_7:
  LD A,(stick_out_up)        ;
  AND A                      ;
  JR Z,place_collision_box_8 ;
  INC B                      ;
place_collision_box_8:
  LD A,B                     ;
  LD (ground_cell),A         ;
  RET                        ;

; Move a record through the world by its speed
;
; Used by the routines at fire_from, start_at_edge, place_at_ship,
; test_landscape_at, move_bouncing, move_or_explode, update_rocket_part,
; draw_rocket, toggle_carried_part, update_shot, draw_shot, fire, ship_pass and
; fly_in_step.
;
; Used for every moving thing: the ship, the shots, the objects. The world is a
; ring $60 pages round, 224 pixels to a page, and a stack of pages 160 lines
; high (see move_across and move_down).
;
; IX the record
move_object:
  CALL move_across        ; across: page, x and fraction into +1 +2 +3
  LD (IX+$01),H           ;
  LD (IX+$02),L           ;
  LD (IX+$03),E           ;
  CALL move_down          ; down: page, line and fraction into +4 +5 +6
  LD (IX+$04),H           ;
  LD (IX+$05),L           ;
  LD (IX+$06),E           ;
  RET                     ;

; A record's place across after its move
;
; The x and the page across of a record's box after this pass's move, or of the
; part being tested (place_collision_box, part_right, next_part).
moved_x:
  DEFB $00,$00

; A record's place down after its move
;
; The line and the page down of a record's box after this pass's move, or of
; the part being tested (place_collision_box, part_above, next_part).
moved_y:
  DEFB $00,$00

; Where the ship's picture is drawn
;
; The screen x and line the ship's picture (or a part drawn with it) is drawn
; at, set by draw_ship and draw_carried_piece; mirror_ship_sprite moves the x
; for a mirrored one.
sprite_screen_pos:
  DEFB $00,$00

; Draw the ship's sprite mirrored when it faces left
;
; Used by the routine at draw_ship_image.
;
; The ship's sprite is drawn the right way round facing right. Facing left
; (ship_facing bit 7) the drawing starts at its right-hand byte -- x moved by
; the width less one, in bytes (sprite_width), times 8 -- and runs right to
; left through a blitter that turns each byte round with the table at
; bit_reverse_table (B = $F9): direct_erase_row_mirrored for a width of 6
; bytes, or one of its later entries for 5, 4, or 3 or fewer.
; draw_carried_piece comes in at the second instruction with the facing of the
; thing it draws in A.
;
;   IY the blitter for drawing the right way round
; O:B $F9, the reversal table's page
; O:HL the screen position
; O:IY the blitter
mirror_ship_sprite:
  LD A,(ship_facing)      ; the ship's facing
; This entry point is used by the routine at draw_carried_piece.
mirror_ship_sprite_0:
  LD HL,(sprite_screen_pos) ; the position; facing right, as it is
  BIT 7,A                   ;
  RET Z                     ;
  LD B,$F9                 ; the reversal table; start at the right-hand byte
  LD A,(sprite_width)      ;
  DEC A                    ;
  ADD A,A                  ;
  ADD A,A                  ;
  ADD A,A                  ;
  ADD A,L                  ;
  LD (sprite_screen_pos),A ;
  LD A,(sprite_width)             ; the blitter by the width
  LD IY,direct_erase_row_mirrored ;
  CP $06                          ;
  RET Z                           ;
  LD IY,mirrored_erase_five       ;
  CP $05                          ;
  RET Z                           ;
  LD IY,mirrored_erase_four       ;
  CP $04                          ;
  RET Z                           ;
  LD IY,mirrored_erase_three      ;
  RET                             ;

; Choose the mirrored drawer for a sprite when the ship faces left
;
; Used by the routines at draw_carried_piece and draw_ship_image.
;
; The drawing half of mirror_ship_sprite: when the ship faces left (bit 7 of
; its facing byte, ship_facing) the image is drawn mirrored. The mirrored
; drawers (direct_draw_row_mirrored on, beside the plain ones that
; draw_direct_pair jumps into) work from right to left and look each byte up in
; the bit-reversal table that build_bit_reverse builds at the start (page $F9),
; through BC with B the table's page. So this moves L to the last byte of the
; row and picks the mirrored drawer for the width. Facing right, it returns
; with IX as the caller left it.
;
;   L The low byte of the screen address of the image's first row (its left
;     end)
;   IX The plain drawer the caller chose
; O:IX The drawer to use: the mirrored one for a width of 6, 5, 4, or anything
;      less
; O:B $F9 when mirrored: the bit-reversal table's page
set_mirrored_drawer:
  LD A,(ship_facing)      ; Facing right: keep the plain drawer.
  BIT 7,A                 ;
  RET Z                   ;
  LD B,$F9                ; B is the page of the bit-reversal table
                          ; (build_bit_reverse), for the mirrored drawer's LD
                          ; A,(BC).
  LD A,(sprite_width_drawn) ; Start at the right-hand byte of the row: L plus
  DEC A                     ; the new width (sprite_width_drawn) less one.
  ADD A,L                   ;
  LD L,A                    ;
  LD A,(sprite_width_drawn)      ; The mirrored drawer for the width: each is
  LD IX,direct_draw_row_mirrored ; entered further in for a narrower image, as
  CP $06                         ; the plain ones are.
  RET Z                          ;
  LD IX,mirrored_draw_five       ;
  CP $05                         ;
  RET Z                          ;
  LD IX,mirrored_draw_four       ;
  CP $04                         ;
  RET Z                          ;
  LD IX,mirrored_draw_three      ;
  RET                            ;

; Move and draw the three layers of parallax stars
;
; Used by the routine at scroll_landscape.
;
; Called each pass from scroll_landscape after the landscape. Only in the top
; four rows of the map (the view's map row view_row below 4: open sky) are
; there stars; lower down this returns at once. It runs in the build's tour at
; map row 3.
;
; There are three layers of six stars. Each layer keeps an x and a y offset (in
; star_offsets) that is moved each pass by the scroll steps (scroll_across
; across, scroll_down down) scaled for the layer by parallax_step: a quarter
; for the far layer, a half for the middle one, the whole step for the near
; one. A star's place on the screen is its fixed position in star_layer_pointer
; plus its layer's offset, wrapping at 256 both ways, so the layers slide past
; one another at three speeds: parallax for six additions a pass. Last pass's
; offsets are kept so each star can be erased where it was drawn.
draw_stars:
  LD A,(view_row)         ; No stars below map row 3.
  CP $04                  ;
  RET NC                  ;
  LD BC,$0006               ; Keep last pass's offsets (six bytes) for the
  LD HL,star_offsets        ; erasing.
  LD DE,star_offsets_before ;
  LDIR                      ;
  LD B,$03                ; Three layers, B=3 (the far one) first.
  LD IX,star_offsets      ;
draw_stars_0:
  LD HL,(scroll_down)     ; Across: the horizontal step (H, from scroll_across)
  CALL parallax_step      ; scaled for the layer and added to the layer's x
  LD A,H                  ; offset.
  ADD A,(IX+$00)          ;
  LD (IX+$00),A           ;
  LD H,L                  ; Down: the vertical step (L, from scroll_down)
  INC IX                  ; scaled the same way and added to its y offset.
  CALL parallax_step      ;
  LD A,H                  ;
  ADD A,(IX+$00)          ;
  LD (IX+$00),A           ;
  INC IX                  ;
  DJNZ draw_stars_0       ;
  LD HL,star_positions       ; Point at the first star in star_layer_pointer,
  LD (star_pointer),HL       ; the first layer's frame slot (star_frames) and
  LD IX,star_frames          ; its offsets.
  LD HL,star_offsets         ;
  LD (star_layer_pointer),HL ;
  LD B,$03                   ;
draw_stars_1:
  PUSH BC                 ; Draw the three layers.
  CALL draw_star_layer    ;
  POP BC                  ;
  DJNZ draw_stars_1       ;
  RET                     ;

; Erase and redraw the six stars of one layer
;
; Used by the routine at draw_stars.
;
; One layer of draw_stars. For each star: if it was drawn last pass (bit 0 of
; its x in the table is the 'drawn' flag) its old place is its position plus
; the layer's old offsets, and the erase is set up there. Its new place is
; tested by star_in_sight and, if it is inside the play area, the drawing is
; set up too. draw_sprite_pair, the sprite compositor, then erases and draws in
; one go, and the new attribute cell is made white.
;
; A star's image is a graphic number kept in the layer's frame slot: the
; layer's base number (7, 9 or 11, at +1 of the slot) plus bit 0 of the star's
; x, since x is held in 2-pixel steps and an odd x takes a copy of the image
; one pixel further right (see star_offsets).
;
; IX The layer's frame slot (two bytes: the frame drawn, then the layer's base
;    graphic number)
draw_star_layer:
  PUSH IX                 ; Keep the frame slot; six stars.
  LD B,$06                ;
draw_star_layer_0:
  CALL clear_sprite_pair  ; Reset the mover (clear_sprite_pair) and the
  LD (star_drawn),A       ; 'anything to do' flag at star_drawn.
  PUSH BC                 ; Not drawn last pass: nothing to erase.
  PUSH IX                 ;
  LD HL,(star_pointer)    ;
  BIT 0,(HL)              ;
  JR Z,draw_star_layer_1  ;
  LD BC,$0006                ; Its old place: the star's x (flag off) and y
  LD HL,(star_layer_pointer) ; plus the layer's old offsets (six bytes on from
  ADD HL,BC                  ; the new ones), into the mover's old position at
  LD DE,(star_pointer)       ; old_sprite_x.
  LD A,(DE)                  ;
  AND $FE                    ;
  ADD A,(HL)                 ;
  LD (old_sprite_x),A        ;
  INC HL                     ;
  INC DE                     ;
  LD A,(DE)                  ;
  ADD A,(HL)                 ;
  LD (old_sprite_y),A        ;
  LD A,(star_drawn)       ; There is something to do.
  OR $01                  ;
  LD (star_drawn),A       ;
  JR draw_star_layer_2    ;
draw_star_layer_1:
  JR draw_star_layer_3    ; A jump to a jump: the way round for 'not drawn'.
draw_star_layer_2:
  LD A,(old_sprite_x)     ; The old image: the layer's base number plus bit 0
  AND $01                 ; of x; then x made even for the screen address.
  ADD A,(IX+$01)          ;
  LD (IX+$00),A           ;
  LD A,(old_sprite_x)     ;
  AND $FE                 ;
  LD (old_sprite_x),A     ;
  CALL graphic_frame_list ; Set up the erase: the image's address
  CALL old_sprite_header  ; (graphic_frame_list), its height clipped to the
  CALL set_old_sprite     ; play area, and the eraser for its width.
draw_star_layer_3:
  EXX                     ; Work out the new place (the code at star_in_sight);
  CALL star_in_sight      ; out of sight, clear the 'drawn' flag.
  JR NZ,draw_star_layer_4 ;
  LD HL,(star_pointer)    ;
  RES 0,(HL)              ;
  JR draw_star_layer_5    ;
draw_star_layer_4:
  LD HL,(star_pointer)    ; In sight: set the flag, choose the image for the
  SET 0,(HL)              ; new x, and set up the drawing the same way.
  LD A,(new_sprite_x)     ;
  AND $01                 ;
  ADD A,(IX+$01)          ;
  LD (IX+$00),A           ;
  LD A,(new_sprite_x)     ;
  AND $FE                 ;
  LD (new_sprite_x),A     ;
  CALL graphic_frame_list ;
  CALL new_sprite_header  ;
  CALL set_new_sprite     ;
draw_star_layer_5:
  LD A,(star_drawn)       ; Anything to erase or draw: do it, and make the
  AND A                   ; star's new attribute cell white ($47).
  JR Z,draw_star_layer_6  ;
  CALL draw_sprite_pair   ;
  LD HL,(new_sprite_x)    ;
  CALL pixel_to_attribute ;
  LD (HL),$47             ;
draw_star_layer_6:
  LD DE,(star_pointer)    ; On to the next star's two bytes.
  INC DE                  ;
  INC DE                  ;
  LD (star_pointer),DE    ;
  POP IX                  ; Six stars.
  POP BC                  ;
  DEC B                   ;
  JP NZ,draw_star_layer_0 ;
  POP IX                     ; The next layer's frame slot and offsets are two
  INC IX                     ; bytes on.
  INC IX                     ;
  LD HL,(star_layer_pointer) ;
  INC HL                     ;
  INC HL                     ;
  LD (star_layer_pointer),HL ;
  RET                        ;

; The current star layer's offsets
;
; Used by draw_stars and draw_star_layer: the address of the current layer's
; two offsets in star_offsets, moved on by two for each layer.
star_layer_pointer:
  DEFW $0000

; The current star
;
; Used by draw_star_layer and star_in_sight: the address of the current star in
; star_positions, moved on by two for each star.
star_pointer:
  DEFW $0000

; The parallax stars' positions
;
; Eighteen stars of two bytes, six a layer, the far layer first (draw_stars): x
; in pixels -- bit 0 is the 'drawn last pass' flag, set and cleared by
; draw_star_layer, which is why every star's x is even -- and y. A star's place
; on the screen is this plus its layer's offsets (star_offsets).
star_positions:
  DEFB $50,$20,$A0,$40,$38,$88
  DEFB $D8,$90,$78,$C0,$10,$D0
  DEFB $28,$10,$E0,$28,$68,$68
  DEFB $B0,$A8,$70,$B4,$28,$E0
  DEFB $38,$18,$D0,$30,$90,$60
  DEFB $08,$6C,$60,$BD,$D8,$E6

; Decide whether a star is in sight, and where
;
; Used by the routine at draw_star_layer.
;
; Called by draw_star_layer for each star. The first code map typed these bytes
; as data, because no session then flew high enough for stars; the tour at map
; row 3 runs them.
;
; The new x is the star's x (flag off) plus the layer's x offset, and the star
; is out of sight unless that is between 8 and $E7 (inside the frame); the new
; y is the star's y plus the layer's y offset, out of sight unless between $18
; and $B8. In map row 3, the lowest with stars, it is also out of sight once it
; is level with or below the top of the landscape (y against the complement of
; the view's y less $31), so stars do not show through the hills.
;
; O:F NZ in sight, Z out of sight
star_in_sight:
  LD HL,(star_layer_pointer) ; The layer's offsets and the star.
  LD DE,(star_pointer)       ;
  LD A,(DE)                  ;
  AND $FE                 ; x: inside 8 to $E7, or out of sight; into the
  ADD A,(HL)              ; mover's new x (old_sprite_x's second pair).
  SUB $08                 ;
  CP $E0                  ;
  JR NC,star_in_sight_1   ;
  ADD A,$08               ;
  LD (new_sprite_x),A     ;
  INC DE                  ;
  INC HL                  ;
  LD A,(DE)               ; y: inside $18 to $B8, or out of sight.
  ADD A,(HL)              ;
  SUB $18                 ;
  CP $A1                  ;
  JR NC,star_in_sight_1   ;
  ADD A,$18               ; Into the mover's new y. In map row 3 ...
  LD (new_sprite_y),A     ;
  LD A,(view_row)         ;
  CP $03                  ; ... the star must be above the top of the
  JR NZ,star_in_sight_0   ; landscape.
  LD A,(view_y)           ;
  CPL                     ;
  SUB $31                 ; In sight: say there is something to draw
  LD B,A                  ; (star_drawn) and return NZ; out of sight return Z.
  LD A,(new_sprite_y)     ;
  CP B                    ;
  JR NC,star_in_sight_1   ;
star_in_sight_0:
  LD A,(star_drawn)       ;
  OR $01                  ;
  LD (star_drawn),A       ;
  RET                     ;
star_in_sight_1:
  XOR A                   ;
  RET                     ;

; Scale a scroll step for a parallax layer
;
; Used by the routine at draw_stars.
;
; Divides the step in H by 1, 2 or 4 for the layer in B. A plain arithmetic
; shift would turn a step of -2 into -1 but +2 into 0 for the far layer, so it
; would only ever drift one way; instead the far layer takes 0 and -1, or 1 and
; 0, on alternate passes (bit 0 of the pass counter pass_counter), and moves at
; a quarter of the speed either way.
;
;   B The layer: 1 near, 2 middle, 3 far
;   H The scroll step (signed pixels)
; O:H The layer's step
parallax_step:
  LD A,B                  ; Near layer: the whole step.
  CP $01                  ;
  RET Z                   ;
  CP $02                  ; Middle layer: just halve it.
  JR Z,parallax_step_0    ;
  LD A,H                  ; Far layer, step -2: no movement on odd passes.
  CP $FE                  ;
  JR NZ,parallax_step_0   ;
  LD A,(pass_counter)     ;
  AND $01                 ;
  JR Z,parallax_step_0    ;
  LD H,$00                ;
  RET                     ;
parallax_step_0:
  LD A,B                  ; Far layer, step +2: on odd passes make it 4, so
  CP $03                  ; that the quarter is 1.
  JR NZ,parallax_step_1   ;
  LD A,H                  ;
  CP $02                  ;
  JR NZ,parallax_step_1   ;
  LD A,(pass_counter)     ;
  AND $01                 ;
  JR Z,parallax_step_1    ;
  LD H,$04                ;
parallax_step_1:
  LD A,B                  ; Shift right, keeping the sign, B-1 times.
  DEC A                   ;
parallax_step_2:
  SRA H                   ;
  DEC A                   ;
  JR NZ,parallax_step_2   ;
  RET                     ;

; The star layers' offsets
;
; Each layer's x and y offsets (far, middle, near), moved each pass by
; draw_stars with the scroll scaled for the layer (parallax_step), wrapping at
; 256.
star_offsets:
  DEFB $00,$00
  DEFB $00,$00
  DEFB $00,$00

; Last pass's star layer offsets
;
; The copies of star_offsets made at the start of draw_stars, for erasing each
; star where it was drawn.
star_offsets_before:
  DEFB $00,$00
  DEFB $00,$00
  DEFB $00,$00

; The star images
;
; Graphics 7 to 11, as the compositor draw_sprite_pair draws the stars (three
; times the size).
;
; Graphics 7 to 11 in the sprite graphic table that graphic_frame_list reads,
; drawn without a mask (OR on, AND NOT off) by the small-image mover
; draw_sprite_pair. Each is a width in bytes, a height in rows, then the rows
; bottom first (everything here is drawn upwards). 7 and 8 are one pixel (at
; bits 7 and 6), 9 and 10 a two-by-two dot at two positions, 11 a three-row
; cross. Graphic 12, which an odd x in the near layer would ask for, is not a
; star; the near layer moves by the whole step, which is always even, so it
; never is.
star_images:
  DEFB $01,$01,$80,$01,$01
  DEFB $40,$01,$02,$C0,$C0
  DEFB $01,$02,$60,$60,$01
  DEFB $03,$40,$E0,$40

; The star layers' frame slots
;
; Two bytes a layer for draw_star_layer: the frame drawn, and the layer's base
; graphic number (7, 9, 11 -- see star_images).
star_frames:
  DEFB $07,$07
  DEFB $09,$09
  DEFB $0B,$0B

; The ship's height
;
; In rows: 14 for the bare ship, more with parts fitted. Set by
; build_ship_sprite when it builds the ship's image, and changed by 5 by
; toggle_carried_part when a rocket part is hung under the ship or let go (bit
; 6 of part_being_fitted); read by draw_ship_image, ship_pass and the collision
; code.
ship_height:
  DEFB $0E

; The ship's graphic
;
; The address of the ship's image, which build_ship_sprite builds up from the
; bare ship (bare_ship) and the parts fitted, in a buffer after the player
; records; toggle_carried_part moves it to take in or leave out a hanging
; rocket part (bit 6 of part_being_fitted, not the facing). Read by
; draw_ship_image.
ship_graphic:
  DEFW $0000

; Make an object of a dropped ship part
;
; Used by the routine at carried_part.
;
; Used by carried_part when the carried part is put down on the surface:
; initialises the object record at IX (free_object) and fills in its graphic
; number ($0C), its kind (+$13), $FF at +$0D, and its place in the world from
; four bytes at HL. Read only: none of it ran in the build's sessions.
;
; IX The object record
; HL Four bytes: map column, map row, x and y in the cell
; B The kind (9 or 10)
place_dropped_part:
  CALL free_object        ; Clear the record.
  LD (IX+$00),$0C         ; Graphic $0C, the kind, and $FF at +$0D.
  LD (IX+$13),B           ;
  LD (IX+$0D),$FF         ;
  LD A,(HL)               ; The map column to +1, the row to +4, x to +2, y to
  LD (IX+$01),A           ; +5.
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$04),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$02),A           ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (IX+$05),A           ;
  RET                     ;

; Draw the slab-and-spire load on top of the ship, or put it down on the
; surface
;
; Used by the routines at draw_rocket, draw_ship and play.
;
; The load the ship picks up as an object of kind 9 (collect_pickup sets +$10
; of the player's record, player_count_nine, to 9) is the slab and the spire,
; add-ons 10 and 11 (addon_slab_picture, addon_spire_picture), and rides on top
; of it in two pieces, 5 bytes wide by $23 rows and 3 wide by $2F rows, drawn
; by draw_carried_piece at fixed places above the ship: the slab's bottom line
; $0D above ship_line, which is five lines above the top of the ship's picture,
; and the spire's $23 higher, standing on the slab. When the ship comes down to
; the surface (the view's map row, +4 of the ship record, is $0B, the lowest
; row of the surface above the caverns, and its y in the page is $A7 or more)
; the load is let go: it is erased and becomes two objects, of kinds 9 and 10,
; the slab and the spire (place_dropped_part), at the ship's place in the
; world, the second $10 pixels right and $23 rows up.
;
; Called on even passes from draw_ship, every pass of the death sequence (play)
; and from draw_rocket. Nothing here ran in the build's sessions (+$10 of the
; player's record was always zero), so this is read only.
carried_part:
  LD A,(player_count_nine) ; Nothing carried: return.
  AND A                    ;
  RET Z                    ;
  LD A,(view_page_row)    ; Not yet down on the surface: just draw it.
  CP $0B                  ;
  JP NZ,carried_part_0    ;
  LD A,(view_page_y)      ;
  CP $A7                  ;
  JP C,carried_part_0     ;
  LD A,(carried_part_facing) ; Let go: bit 6 of carried_part_facing makes
  OR $40                     ; draw_carried_piece erase it and not draw it
  LD (carried_part_facing),A ; again; nothing is carried any more, and the ship
  XOR A                      ; record's +$1F is cleared.
  LD (player_count_nine),A   ;
  LD (part_being_fitted),A   ;
  LD A,(view_page_row)    ; The view's map cell (column in E, row in D) for
  LD D,A                  ; both new objects (at +$41 and +$45 of the player's
  LD A,(view_page_column) ; record, score).
  LD E,A                  ;
  LD (place_nine),DE      ;
  LD (place_ten),DE       ;
  LD A,(ship_line)        ; The first piece's y in the cell from the ship's
  SUB $0D                 ; screen row (ship_line) and its screen y
  LD B,A                  ; (ship_screen_y); its x the view's x plus 8, rounded
  LD A,(ship_screen_y)    ; down to 8.
  SUB B                   ;
  LD B,A                  ;
  LD A,(view_page_y)      ;
  SUB B                   ;
  LD H,A                  ;
  LD A,(view_page_x)      ;
  ADD A,$08               ;
  AND $F8                 ;
  LD L,A                  ;
  LD ($F62A),HL           ;
  LD A,L                  ; The second piece $10 to the right and $23 higher.
  ADD A,$10               ;
  LD L,A                  ;
  LD A,H                  ;
  SUB $23                 ;
  LD H,A                  ;
  LD ($F62E),HL           ;
  LD HL,place_nine           ; Make them objects of kinds 9 and 10, in the two
  LD IX,pickup_record        ; object records loaded into IX.
  LD B,$09                   ;
  CALL place_dropped_part    ;
  LD IX,second_pickup_record ;
  LD HL,place_ten            ;
  LD B,$0A                   ;
  CALL place_dropped_part    ;
carried_part_0:
  LD IX,carried_big_x      ; The 5-wide piece: its graphic, old and new widths
  LD DE,addon_slab_picture ; 5, no erase unless draw_carried_piece decides on
  LD HL,$0505              ; one, $23 rows; its place kept at carried_big_x.
  LD (sprite_width),HL     ;
  LD HL,$2300              ;
  LD (scratch_count),HL    ;
  CALL draw_carried_piece  ;
  LD IX,carried_small_x     ; The 3-wide piece: its graphic, widths 3, $2F
  LD DE,addon_spire_picture ; rows; its place kept at carried_small_x.
  LD HL,$0303               ;
  LD (sprite_width),HL      ;
  LD HL,$2F00               ;
  LD (scratch_count),HL     ;
  CALL draw_carried_piece   ;
  LD A,(ship_facing)         ; Remember the facing it was drawn with; the
  LD (carried_part_facing),A ; forced redraw is done.
  XOR A                      ;
  LD (carried_part_redraw),A ;
  JP colour_carried_parts ; The colour patches over the two pieces
                          ; (colour_carried_parts).

; The facing the carried load was drawn with
;
; Used by carried_part and draw_carried_piece: the ship's facing byte
; (ship_facing) when the carried load was last drawn, 0 if it has not been; bit
; 6 set when the load is let go. Cleared when the load is picked up
; (collect_pickup).
carried_part_facing:
  DEFB $00

; Force the carried load to be redrawn
;
; Non-zero to make the next pass erase and redraw the carried load
; (draw_carried_piece): set by draw_ship_image when a part is fitted, and by
; draw_rocket and reset_for_life; cleared by carried_part.
carried_part_redraw:
  DEFB $00

; The carried load's larger piece: screen x
;
; Where draw_carried_piece last drew the 5-byte-wide piece of the carried load:
; x here, y in carried_big_y. Read as a word by colour_carried_parts for its
; colour patch.
carried_big_x:
  DEFB $00

; The carried load's larger piece: screen y
;
; See carried_big_x.
carried_big_y:
  DEFB $00

; The carried load's smaller piece: screen x
;
; Where draw_carried_piece last drew the 3-byte-wide piece: x here, y in
; carried_small_y.
carried_small_x:
  DEFB $00

; The carried load's smaller piece: screen y
;
; See carried_small_x.
carried_small_y:
  DEFB $00

; Erase and redraw one piece of the carried load
;
; Used by the routine at carried_part.
;
; The old image is erased only when it has to be: when the load is let go, when
; a redraw is forced (carried_part_redraw), or when the ship has turned since
; it was drawn (its facing byte against the one kept at carried_part_facing).
; Otherwise the new image is drawn over the old one, which is at the same
; place: the ship does not move on the screen. The place is fixed relative to
; the ship: x $68 for the 5-wide piece, $78 and $23 rows higher for the 3-wide
; one, $60 when facing left, and y $0D above the ship's line ship_line, just
; above the ship's top, so the load rides on top of the ship. Facing left, the
; mirrored eraser and drawer are used (mirror_ship_sprite,
; set_mirrored_drawer). Read only: never ran.
;
; IX The piece's place (screen x, y)
; DE The piece's graphic
; #R$5C28 The piece's width in bytes (old and new)
draw_carried_piece:
  PUSH DE                 ; Keep the graphic.
  LD A,(carried_part_facing) ; Let go: erase it.
  BIT 6,A                    ;
  JR NZ,draw_carried_piece_0 ;
  AND A                     ; Never drawn: nothing to erase.
  JR Z,draw_carried_piece_2 ;
  LD B,A                     ; Erase only if forced, or if the ship faced the
  LD A,(carried_part_redraw) ; other way when it was drawn.
  AND A                      ;
  JR NZ,draw_carried_piece_0 ;
  LD A,(ship_facing)         ;
  XOR B                      ;
  BIT 7,A                    ;
  JR Z,draw_carried_piece_2  ;
draw_carried_piece_0:
  LD A,(ground_cell)         ; Set up the erase: the old height is the full
  LD (scratch_count),A       ; height, clipped at the top of the play area
  LD L,(IX+$00)              ; (clip_height_to_top); its old place; the eraser
  LD H,(IX+$01)              ; for width 5 or 3 (entry points in
  LD A,H                     ; direct_erase_row), mirrored if it was drawn
  LD (direct_old_line),A     ; facing left (mirror_ship_sprite); the screen
  LD A,(scratch_count)       ; address.
  CALL clip_height_to_top    ;
  LD A,B                     ;
  LD (scratch_count),A       ;
  LD (sprite_screen_pos),HL  ;
  LD IY,direct_erase_five    ;
  LD A,(sprite_width)        ;
  CP $05                     ;
  JR Z,draw_carried_piece_1  ;
  LD IY,direct_erase_three   ;
draw_carried_piece_1:
  LD A,(carried_part_facing) ;
  CALL mirror_ship_sprite_0  ;
  LD HL,(sprite_screen_pos)  ;
  CALL pixel_to_screen       ;
  EXX                        ;
draw_carried_piece_2:
  POP DE                     ; Let go: no new image; run the mover for the
  LD A,(carried_part_facing) ; erase alone.
  BIT 6,A                    ;
  JR Z,draw_carried_piece_3  ;
  XOR A                      ;
  LD (ground_cell),A         ;
  JP draw_direct_pair        ;
draw_carried_piece_3:
  LD (IX+$00),$68         ; The 5-wide piece: x $68, y the ship's screen row
  LD A,(ship_line)        ; less $0D.
  SUB $0D                 ;
  LD (IX+$01),A           ;
  LD HL,direct_draw_five    ; The 3-wide piece: x $78, $23 rows higher, and the
  LD A,(sprite_width_drawn) ; narrower drawer.
  CP $05                    ;
  JR Z,draw_carried_piece_4 ;
  LD (IX+$00),$78           ;
  LD A,(IX+$01)             ;
  SUB $23                   ;
  LD (IX+$01),A             ;
  LD HL,direct_draw_three   ;
draw_carried_piece_4:
  LD A,(ship_facing)        ; Facing left: x $60.
  BIT 7,A                   ;
  JR Z,draw_carried_piece_5 ;
  LD (IX+$00),$60           ;
draw_carried_piece_5:
  PUSH HL                  ; Draw it: its new place, the height clipped at the
  LD L,(IX+$00)            ; top, the screen address, the mirrored drawer if
  LD H,(IX+$01)            ; facing left, then erase and draw
  LD A,H                   ; (draw_direct_pair).
  LD (direct_new_line),A   ;
  LD A,(ground_cell)       ;
  CALL clip_height_to_top  ;
  LD A,B                   ;
  LD (ground_cell),A       ;
  CALL pixel_to_screen     ;
  POP IX                   ;
  CALL set_mirrored_drawer ;
  JP draw_direct_pair      ;

; Clip a sprite's height at the top of the play area
;
; Used by the routines at ship_contact, colour_carried_parts and
; draw_carried_piece.
;
; Images are drawn upwards from their bottom row, so one whose bottom row is
; near the top of the play area would run up into the panel. The play area
; starts at row $17: the height is cut to the rows between there and y.
;
;   A The height in rows
;   H The y of the image's bottom row
; O:B The height to draw
clip_height_to_top:
  LD B,A                  ; Rows available (y less $17) at least the height:
  LD A,H                  ; draw it all.
  SUB $17                 ;
  SUB B                   ;
  RET NC                  ;
  LD A,H                  ; Otherwise only the rows available.
  SUB $17                 ;
  LD B,A                  ;
  RET                     ;

; Remember where to start the next ship, if this is a safe place
;
; Used by the routine at ship_pass.
;
; Called every fourth pass by ship_pass. A place is safe if the bottom pixel
; row of the play area (from screen address $57C1, 28 bytes) is nothing but the
; flat ground's dither ($55: the ship is over flat ground filling the bottom of
; the screen), or if all four map cells on the screen (the four tile bytes kept
; by load_map_window) are empty sky (0) or cloud ($A6). Then the view's map
; cell, its x and y in the cell and the fine scroll are saved in the player's
; record, where start_landscape finds them when the next ship starts. Bit 0 of
; the saved x says which kind of place it was (0 over the ground, 1 in the
; sky): over the ground, start_landscape first paints the dither across the
; bottom of the play area.
save_restart_point:
  LD HL,$57C1                ; Is the bottom row of the play area all ground
  LD B,$1C                   ; dither?
save_restart_point_0:
  LD A,(HL)                  ;
  CP $55                     ;
  JR NZ,save_restart_point_2 ;
  INC L                      ;
  DJNZ save_restart_point_0  ;
  LD B,$00                ; Yes: an over-the-ground restart.
save_restart_point_1:
  LD HL,(view_column)     ; Save the view's map cell (view_column), its x and y
  LD (restart_scroll),HL  ; in the cell (view_x, with bit 0 of x the kind of
  LD HL,(view_x)          ; place) and the origins (origin_x) at +$13, +$15 and
  LD A,L                  ; +$17 of the player's record (score).
  OR B                    ;
  LD L,A                  ;
  LD (restart_view),HL    ;
  LD HL,(origin_x)        ;
  LD (restart_fine),HL    ;
  RET                     ;
save_restart_point_2:
  LD HL,map_window_tile_bytes ; Otherwise every cell on the screen must be sky
  LD B,$04                    ; or cloud.
save_restart_point_3:
  LD A,(HL)                   ;
  AND A                       ;
  JR Z,save_restart_point_4   ;
  CP $A6                      ;
  RET NZ                      ;
save_restart_point_4:
  INC HL                      ;
  DJNZ save_restart_point_3   ;
  LD B,$01                ; A sky restart.
  JR save_restart_point_1 ;

; Move the ship and the world for one pass of the game
;
; Used by the routine at play.
;
; The first call of each pass of the play loop (play), and of the death
; sequence. It counts the pass (pass_counter), counts the shield pick-up's
; timer shield_timer down on odd passes, and every fourth pass offers the place
; as a restart point (save_restart_point). Then, unless the ship is dying or
; the finished rocket is lifting off (launch_state), the controls are read and
; the ship's speeds worked out (move_ship, which leaves this pass's scroll
; steps in scroll_down and scroll_across), the ship record is given the
; opposite step (ship_speed_from_scroll) and tested against the landscape
; (collide_with_landscape) and the other objects (ship_hits_ground_objects),
; and the record is moved (move_object).
;
; It ends by jumping to scroll_landscape, which scrolls the view, redraws the
; landscape and the stars, and jumps on to draw_ship to draw the ship: IY,
; pushed here, is popped there.
ship_pass:
  LD HL,(pass_counter)    ; Count the pass.
  INC HL                  ;
  LD (pass_counter),HL    ;
  BIT 0,L                 ; On odd passes, run the shield timer down.
  JR Z,ship_pass_0        ;
  LD A,(shield_timer)     ;
  AND A                   ;
  JR Z,ship_pass_0        ;
  DEC A                   ;
  LD (shield_timer),A     ;
ship_pass_0:
  LD A,L                    ; Every fourth pass, perhaps a restart point.
  AND $03                   ;
  CALL Z,save_restart_point ;
  PUSH IY                 ; Kept for draw_ship.
  XOR A                   ; No scroll unless the ship moves.
  LD (scroll_across),A    ;
  LD (scroll_down),A      ;
  LD A,(death_count)      ; Dying (death byte $0B or more): no movement.
  CP $0B                  ;
  JR NC,ship_pass_2       ;
  LD IX,ship_record       ; Read the controls and work out the ship's speeds
  CALL move_ship          ; and this pass's scroll.
  LD A,$FF                ; piece_step_x = $FF: a marker for the landscape test
  LD (piece_step_x),A     ; (collide_with_landscape clears ship_on_slope if it
                          ; is still $FF afterwards).
  LD A,(launch_state)     ; The finished rocket is lifting off (launch_state):
  AND A                   ; no tests.
  JR NZ,ship_pass_2       ;
  CALL ship_speed_from_scroll ; The ship record's step is minus the scroll.
  LD C,$30                    ; Test it against the landscape: B the ship's
  LD A,(addon_nine)           ; height plus 4 (plus nothing with the add-on at
  AND A                       ; +$0F of the player's record).
  LD B,$04                    ;
  JR Z,ship_pass_1            ;
  LD B,$00                    ;
ship_pass_1:
  LD A,(ship_height)          ;
  ADD A,B                     ;
  LD B,A                      ;
  CALL collide_with_landscape ;
  CALL ship_hits_ground_objects ; Test it against the other objects (this is
                                ; what calls the damage routine).
  LD HL,(ship_dx)           ; Keep a copy of the ship record's two step words
  LD (ship_across_speed),HL ; (at ship_across_speed and ship_down_speed).
  LD HL,(ship_dy)           ;
  LD (ship_down_speed),HL   ;
  CALL ship_speed_from_scroll ; Set the step again and move the record by it.
  CALL move_object            ;
ship_pass_2:
  CALL update_shots       ; Then the landscape, and the ship.
  JP scroll_landscape     ;

; Draw the ship, its trail and its jets
;
; Used by the routine at scroll_landscape.
;
; Reached by a jump from scroll_landscape at the end of each pass's landscape
; drawing; returns to the play loop. IY was pushed by ship_pass.
;
; The ship stays where it is on the screen and the world moves under it, so its
; image is drawn over itself and only erased when it changes: when the ship
; turns (the steering direction steer_push against the facing byte, +$18 of the
; ship record), when a part is being fitted (+$1F of the record,
; part_being_fitted), or while docking with the finished rocket (death byte
; 10). It is redrawn on every odd pass, and on even passes only when one of
; those things happens; on even passes the carried part (carried_part) is drawn
; instead.
;
; The ship's image is draw_ship_image; then come the trail (draw_trail), the
; pause key (check_pause_in_play), the fire button (fire), and one of the two
; jets: the lift jet (lift_jet) on even passes, the side jet (side_jet) on odd
; ones.
;
; All of them are drawn by draw_direct_pair, which interleaves an erase of the
; old image (main registers: DE the graphic, HL the screen, IY the eraser for
; its width, scratch_count its height, direct_old_line its y) with the drawing
; of the new (alternate registers, IX the drawer, ground_cell the height, the
; byte after direct_old_line the y), a row of each in turn, so that nothing is
; ever missing from the screen for long. Images are drawn with OR and erased
; with AND NOT, without masks.
draw_ship:
  POP IY                  ; IY as it was before ship_pass.
  LD A,(launch_state)     ; Nothing to draw once the rocket is lifting off or
  AND A                   ; the ship is dying.
  RET NZ                  ;
  LD A,(death_count)      ;
  CP $0B                  ;
  RET NC                  ;
  CALL drop_part          ; First drop_part, which lets a carried rocket part
                          ; go over its slot.
  LD A,(pass_counter)     ; Odd pass: redraw the ship.
  AND $01                 ;
  JR NZ,draw_ship_image   ;
  CALL carried_part       ; Even pass: the carried part.
  LD A,(part_being_fitted) ; Redraw the ship anyway when a part is being
  AND A                    ; fitted, while docking with the finished rocket
  JR NZ,draw_ship_image    ; (death byte 10) or when ship_on_slope is set.
  LD A,(death_count)       ;
  CP $0A                   ;
  JR Z,draw_ship_image     ;
  LD A,(ship_on_slope)     ;
  AND A                    ;
  JR NZ,draw_ship_image    ;
  LD A,(steer_push)       ; Otherwise only when it is steered the other way
  AND A                   ; from the way it faces.
  JP Z,draw_trail_9       ;
  LD B,A                  ;
  LD A,(ship_facing)      ;
  XOR B                   ;
  BIT 7,A                 ;
  JP Z,draw_trail_9       ;

; Erase and redraw the ship's image
;
; Used by the routines at draw_rocket and draw_ship.
;
; Entered from draw_ship on odd passes or when the image must change, and
; called by draw_rocket. The old image is erased only on a turn, while a part
; is being fitted (part_being_fitted) or while docking with the finished rocket
; (death byte 10); the new one is drawn at the same place (ship_screen_x,
; ship_screen_y), mirrored if the ship now faces left. Then on into draw_trail.
draw_ship_image:
  LD HL,$0000             ; No erase so far (old height 0), y the ship's
  LD (scratch_count),HL   ; (ship_screen_y), widths 6, IX the ship record.
  LD A,(ship_screen_y)    ;
  LD (direct_old_line),A  ;
  LD HL,$0606             ;
  LD (sprite_width),HL    ;
  LD IX,ship_record       ;
  LD A,(death_count)      ; Docking with the rocket: erase it.
  CP $0A                  ;
  JR Z,draw_ship_image_0  ;
  LD C,(IX+$18)           ; Turning? ship_turned says so (the trail is then
  LD A,(steer_push)       ; redrawn too), and the fire delay is cleared.
  AND A                   ;
  JR Z,draw_ship_image_2  ;
  XOR C                   ;
  AND $80                 ;
  LD (ship_turned),A      ;
  JR Z,draw_ship_image_2  ;
  XOR A                   ;
  LD (gun_cooling),A      ;
draw_ship_image_0:
  LD HL,(ship_screen_x)     ; Set up the erase: the old graphic (ship_graphic),
  LD (sprite_screen_pos),HL ; height (ship_height) and place (ship_screen_x),
  LD DE,(ship_graphic)      ; the 6-wide eraser, mirrored if it faced left
  LD A,(ship_height)        ; (mirror_ship_sprite).
  LD (scratch_count),A      ;
  LD IY,direct_erase_row    ;
  CALL mirror_ship_sprite   ;
  LD HL,(sprite_screen_pos) ;
  CALL pixel_to_screen      ;
  EXX                       ;
  LD A,(IX+$1F)           ; A part being fitted?
  AND A                   ;
  JR Z,draw_ship_image_2  ;
  BIT 7,A                    ; One with bit 7 set is drawn by
  JR Z,draw_ship_image_1     ; toggle_carried_part.
  CALL toggle_carried_part   ;
  LD A,$80                   ;
  LD (carried_part_redraw),A ;
  JR draw_ship_image_3       ;
draw_ship_image_1:
  LD B,A                     ; Otherwise it goes into the player's list of
  DEC A                      ; parts (from +7 of score), the old image is
  LD HL,addon_slots          ; erased now, the ship's graphic rebuilt with it
  CALL add_a_to_hl           ; (build_ship_sprite), and the erase set up again
  LD (HL),B                  ; for nothing (read only).
  CALL draw_direct_pair      ;
  CALL build_ship_sprite     ;
  LD A,$80                   ;
  LD (carried_part_redraw),A ;
  LD IX,ship_record          ;
  LD HL,$0000                ;
  LD (scratch_count),HL      ;
  LD HL,$0606                ;
  LD (sprite_width),HL       ;
  JR draw_ship_image_3       ;
draw_ship_image_2:
  LD A,(IX+$1F)           ; Not turning: erase only if a part is being fitted.
  AND A                   ;
  JR NZ,draw_ship_image_0 ;
draw_ship_image_3:
  LD A,(steer_push)       ; Being steered: the facing byte follows.
  AND A                   ;
  JR Z,draw_ship_image_4  ;
  LD (IX+$18),A           ;
draw_ship_image_4:
  LD A,(death_count)      ; Docking: no new image.
  CP $0A                  ;
  JR Z,draw_ship_image_5  ;
  LD A,(ship_screen_y)      ; The new image at the same place, with the current
  LD (direct_new_line),A    ; graphic and height, mirrored if it now faces
  LD DE,(ship_graphic)      ; left.
  LD HL,(ship_screen_x)     ;
  LD (sprite_screen_pos),HL ;
  CALL pixel_to_screen      ;
  LD A,(ship_height)        ;
  LD (ground_cell),A        ;
  LD IX,direct_draw_row     ;
  CALL set_mirrored_drawer  ;
draw_ship_image_5:
  CALL draw_direct_pair   ; Erase and draw.
  CALL colour_ship        ; Colour the ship (colour_ship).
  LD A,(death_count)      ; Docking or dying: stop here.
  CP $0A                  ;
  RET NC                  ;

; Erase and redraw the ship's trail
;
; Used by the routines at draw_rocket and play.
;
; The trail is a one-byte-wide line behind the ship whose bend follows the
; speed across (ship_across_speed): the frame is 1 + twice the whole pixels +
; the half, no more than one past the top speed, mirrored with the direction
; (record trail_frame-trail_hold, images trail_images). It is erased only when
; the frame changes, the ship turns (ship_turned) or the collision code asks
; (trail_collision). Also called at the start of each life and of the death
; sequence (play; dying, it is only erased) and by draw_rocket. Then on to the
; pause key, the fire button and the jets.
draw_trail:
  LD HL,$0000               ; The trail: its old frame and place (trail_frame)
  LD (scratch_count),HL     ; for the erase.
  LD IX,trail_frame         ;
  CALL graphic_frame_list   ;
  LD HL,(trail_x)           ;
  LD (sprite_screen_pos),HL ;
  CALL read_graphic_size    ;
  CALL set_direct_old       ;
  LD HL,(sprite_screen_pos) ; The 1-byte eraser, or the mirrored one
  CALL pixel_to_screen      ; (trail_mirror).
  LD IY,direct_erase_one    ;
  CALL trail_mirror         ;
  JR NZ,draw_trail_0        ;
  LD IY,mirrored_erase_one  ;
draw_trail_0:
  EXX                     ; Keep the old frame in E.
  LD E,(IX+$00)           ;
  LD A,(part_being_fitted) ; A part being fitted: the trail hangs lower
  AND A                    ; (ship_line less 9) and is redrawn.
  JR Z,draw_trail_1        ;
  LD A,(ship_line)         ;
  SUB $09                  ;
  LD (trail_y),A           ;
  LD ($BF84),A             ;
  LD E,$FF                 ;
  XOR A                    ;
  LD (part_being_fitted),A ;
draw_trail_1:
  LD HL,(ship_across_speed) ; The ship's speed across (the step word kept at
  LD A,H                    ; ship_across_speed, the whole pixels in the high
  LD H,L                    ; byte): under half a pixel, flagged as moving
  LD L,A                    ; right (trail_direction bit 7).
  LD A,H                    ;
  AND A                     ;
  JR NZ,draw_trail_2        ;
  LD A,L                    ;
  CP $80                    ;
  LD A,H                    ;
  JR NC,draw_trail_2        ;
  LD A,$80                  ;
  LD (trail_direction),A    ;
  JR draw_trail_3           ;
draw_trail_2:
  AND $80                 ; Otherwise its sign; moving the other way, take its
  LD (trail_direction),A  ; size.
  CALL NZ,negate_hl       ;
draw_trail_3:
  LD A,L                  ; Frame = 1 + twice the whole pixels + the half: the
  AND $80                 ; faster, the further bent; no more than one past the
  RLCA                    ; top speed (top_speed).
  SLA H                   ;
  ADD A,H                 ;
  INC A                   ;
  LD (IX+$00),A           ;
  LD A,(top_speed)        ;
  LD B,A                  ;
  LD A,H                  ;
  CP B                    ;
  JR C,draw_trail_4       ;
  INC B                   ;
  LD (IX+$00),B           ;
draw_trail_4:
  LD A,(death_count)      ; A countdown at trail_hold (alive only) holds the
  AND A                   ; frame at 6.
  JR NZ,draw_trail_5      ;
  LD A,(trail_hold)       ;
  AND A                   ;
  JR Z,draw_trail_5       ;
  DEC A                   ;
  LD (trail_hold),A       ;
  LD (IX+$00),$06         ;
  JR draw_trail_6         ;
draw_trail_5:
  LD A,(death_count)      ; Same frame, no turn and no collision
  CP $0A                  ; (trail_collision): no erase needed.
  JR NC,draw_trail_8      ;
  LD A,(trail_collision)  ;
  AND A                   ;
  JR NZ,draw_trail_6      ;
  LD A,(IX+$00)           ;
  CP E                    ;
  JR NZ,draw_trail_6      ;
  LD A,(ship_turned)      ;
  AND A                   ;
  JR NZ,draw_trail_6      ;
  LD (scratch_count),A    ;
draw_trail_6:
  CALL graphic_frame_list  ; The new image: x $68, or $78 facing left, at the
  LD B,$68                 ; trail's y.
  LD A,(ship_facing)       ;
  BIT 7,A                  ;
  JR Z,draw_trail_7        ;
  LD B,$78                 ;
draw_trail_7:
  LD A,B                   ;
  LD (trail_x),A           ;
  LD (sprite_screen_pos),A ;
  CALL graphic_frame_list  ;
  CALL read_graphic_size   ;
  CALL set_direct_new      ;
  LD IX,direct_draw_one    ; The 1-byte drawer, or the mirrored one 8 pixels
  CALL trail_mirror        ; further right.
  JR NZ,draw_trail_8       ;
  LD IX,mirrored_draw_one  ;
  LD A,(sprite_screen_pos) ;
  ADD A,$08                ;
  LD (sprite_screen_pos),A ;
draw_trail_8:
  CALL draw_direct_pair   ; Erase and draw.
  XOR A                   ; The collision flag is dealt with.
  LD (trail_collision),A  ;
  LD A,(trail_frame)        ; Unless it is frame 6, the attribute cell 7 rows
  CP $06                    ; above its bottom made white.
  JP Z,draw_trail_9         ;
  LD HL,(sprite_screen_pos) ;
  LD A,H                    ;
  SUB $07                   ;
  LD H,A                    ;
  CALL pixel_to_attribute   ;
  LD (HL),$47               ;
; This entry point is used by the routine at draw_ship.
draw_trail_9:
  CALL check_pause_in_play ; The pause key and the fire button.
  CALL fire                ;
  LD A,(death_count)      ; Alive, on even passes: the lift jet.
  CP $0A                  ;
  JR NC,side_jet          ;
  LD A,(pass_counter)     ;
  AND $01                 ;
  JP Z,lift_jet           ;

; Erase and redraw the side jet behind the ship
;
; Used by the routines at check_pause_in_play and draw_trail.
;
; Reached from draw_trail on odd passes, and called by check_pause_in_play
; during the pause. The add-on fitted when +$0A of the player's record (score)
; is set (which also raises the top speed across, top_speed, to 4:
; build_ship_sprite) flickers behind the ship while it is steered left or
; right: graphics $0E-$10 through the sequence side_jet_frames, with the record
; side_jet_record and a 2x2 colour patch whose colour cycles through
; jet_colours. It ran in the build's every-add-on session, but never mirrored:
; the erase and the draw for steering left are read only.
;
; The end, from the colour patch on, is also used by lift_jet.
side_jet:
  LD A,(addon_four)       ; Not fitted: return.
  AND A                   ;
  RET Z                   ;
  LD IX,side_jet_record   ; Its record (side_jet_record). Not drawn last pass:
  LD HL,$0000             ; nothing to erase, and nothing to do unless steering
  LD (scratch_count),HL   ; left or right.
  BIT 7,(IX+$03)          ;
  JR NZ,side_jet_0        ;
  LD A,(controls_now)     ;
  AND $03                 ;
  RET Z                   ;
  JR side_jet_1           ;
side_jet_0:
  CALL sprite_graphic_width ; Set up the erase from its last frame and place,
  LD (scratch_count),A      ; mirrored if it was.
  LD HL,($C6C3)             ;
  LD IY,direct_erase_two    ;
  BIT 6,(IX+$03)            ;
  JR Z,side_jet_1           ;
  LD IY,mirrored_erase_two  ;
  LD B,$F9                  ;
side_jet_1:
  EXX                     ; Not drawn now; not steering: just erase.
  LD (IX+$03),$00         ;
  LD A,(controls_now)     ;
  AND $03                 ;
  JP Z,side_jet_3         ;
  LD A,(death_count)      ; Dying or docking: just erase.
  CP $0A                  ;
  JR NC,side_jet_3        ;
  LD A,(IX+$06)           ; Next frame of the flicker: a count of 0-7 through
  INC A                   ; side_jet_frames gives graphic $0E, $0F or $10.
  AND $07                 ;
  LD (IX+$06),A           ;
  LD HL,side_jet_frames   ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  ADD A,$0E               ;
  LD (IX+$00),A           ;
  CALL sprite_graphic_width ; Its height; drawn now.
  LD (ground_cell),A        ;
  LD (IX+$03),$80           ;
  LD IX,direct_draw_two   ; Behind the ship, 8 rows below its screen row: x $50
  LD A,(ship_line)        ; steering right; steering left x $98, mirrored (bit
  ADD A,$08               ; 6), with the mirrored drawer.
  LD H,A                  ;
  LD L,$50                ;
  LD A,(controls_now)     ;
  BIT 1,A                 ;
  JR Z,side_jet_2         ;
  LD L,$98                ;
  LD B,$F9                ;
  LD A,$C0                ;
  LD ($C6C5),A            ;
  LD IX,mirrored_draw_two ;
side_jet_2:
  LD ($C6C6),HL           ;
  CALL pixel_to_screen    ; Its screen address, kept for the next erase.
  LD ($C6C3),HL           ;
side_jet_3:
  CALL draw_direct_pair   ; Erase and draw.
  LD A,($C6C5)            ; Drawn: a colour patch over it chosen by the flicker
  AND $80                 ; count.
  RET Z                   ;
  LD HL,($C6C6)           ;
  LD A,L                  ;
  AND $F0                 ;
  LD L,A                  ;
  LD A,H                  ;
  SUB $04                 ;
  LD H,A                  ;
  LD A,($C6C8)            ;
; This entry point is used by the routine at lift_jet.
side_jet_4:
  EX DE,HL                ; The patch for fill_attributes, two cells by two,
  LD HL,$0202             ; its colour from jet_colours (lift_jet comes in
  LD (colour_patch),HL    ; here).
  LD HL,jet_colours       ;
  AND $07                 ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  EX DE,HL                ;
  LD ($AF03),A            ;
  LD DE,colour_patch      ;
  JP fill_attributes      ;

; The side jet's sprite record
;
; Used by side_jet. +0 the graphic number ($0E-$10); +1 +2 the screen address
; it was drawn at; +3 bit 7 drawn, bit 6 mirrored (steering left); +4 +5 its
; screen x and y; +6 the flicker count (0-7).
side_jet_record:
  DEFB $00,$00,$00,$00,$00,$00,$00

; The side jet's record at the start of a life
;
; Copied over side_jet_record by reset_for_life.
side_jet_start:
  DEFB $00,$00,$00,$00,$00,$00,$00

; Skip the lift jet for a pass
;
; Set by hit_block; makes lift_jet erase the lift jet without drawing it again,
; for one pass. The byte after it is not used.
lift_jet_skip:
  DEFB $00
  DEFB $00

; Erase and redraw the lift jet under the ship
;
; Used by the routines at draw_rocket, draw_trail and play.
;
; The add-on fitted when +$0B of the player's record (score) is set (which also
; raises the top vertical speed top_climb to 4, build_ship_sprite, and lets the
; ship climb above map row 5, read_controls) is drawn under the ship on even
; passes: graphic $12, or $11 while thrusting (control bit 3). It is drawn
; mirrored and not in turn (bit 6 of +3 flips each time), so it seems to spin.
; While the finished rocket lifts off it is drawn at the rocket's height. With
; the ship dying (death byte 10 or more) it is only erased, which is why the
; death sequence (play) calls this once at its start. Its 2x2 attribute patch
; is white. It ran in the build's every-add-on session, all but the lift-off
; branch: the launch session had no lift jet.
;
; Stage 0 took this for the death sequence; it is not: the death sequence calls
; it to take the jet off the screen.
lift_jet:
  LD A,(addon_five)       ; Not fitted: return.
  AND A                   ;
  RET Z                   ;
  LD IX,lift_jet_record   ; Its record (lift_jet_record); nothing to erase yet.
  LD HL,$0000             ;
  LD (scratch_count),HL   ;
  LD (direct_old_line),HL ;
  BIT 7,(IX+$03)            ; Drawn last pass: set up the erase, mirrored if it
  JR Z,lift_jet_0           ; was.
  CALL sprite_graphic_width ;
  LD (scratch_count),A      ;
  LD HL,($C790)             ;
  LD IY,direct_erase_four   ;
  BIT 6,(IX+$03)            ;
  JR Z,lift_jet_0           ;
  LD IY,mirrored_erase_four ;
  LD B,$F9                  ;
lift_jet_0:
  RES 7,(IX+$03)          ; Not drawn now.
  EXX                     ;
  LD A,(lift_jet_skip)    ; Skipped this pass (lift_jet_skip).
  AND A                   ;
  JR NZ,lift_jet_5        ;
  LD (IX+$00),$12         ; Graphic $12; dying or docking, just erase.
  LD A,(death_count)      ;
  CP $0A                  ;
  JR NC,lift_jet_5        ;
  LD A,(controls_now)     ; Thrusting: graphic $11.
  AND $08                 ;
  JR Z,lift_jet_1         ;
  LD (IX+$00),$11         ;
lift_jet_1:
  CALL sprite_graphic_width ; Its height; drawn, and mirrored if it was not
  LD (ground_cell),A        ; last time.
  LD A,(IX+$03)             ;
  XOR $40                   ;
  OR $80                    ;
  LD (IX+$03),A             ;
  LD A,(launch_state)     ; The rocket lifting off: at the rocket's height,
  AND A                   ; graphic $11.
  JR Z,lift_jet_2         ;
  LD A,($96B2)            ;
  ADD A,$13               ;
  LD H,A                  ;
  LD (IX+$00),$11         ;
  JR lift_jet_3           ;
lift_jet_2:
  LD A,(ship_line)        ; Otherwise $1D rows below the ship's screen row
  ADD A,$1D               ; (ship_line), 8 less for graphic $12.
  LD H,A                  ;
  LD A,(IX+$00)           ;
  CP $11                  ;
  JR Z,lift_jet_3         ;
  LD A,H                  ;
  SUB $08                 ;
  LD H,A                  ;
lift_jet_3:
  LD L,$68                 ; x $68, or $80 with the mirrored drawer.
  BIT 6,(IX+$03)           ;
  LD IX,direct_draw_four   ;
  JR Z,lift_jet_4          ;
  LD IX,mirrored_draw_four ;
  LD B,$F9                 ;
  LD L,$80                 ;
lift_jet_4:
  LD ($C793),HL           ; Its place and screen address, kept for the next
  CALL pixel_to_screen    ; erase.
  LD ($C790),HL           ;
lift_jet_5:
  CALL draw_direct_pair   ; Erase and draw; the skip flag is used up.
  XOR A                   ;
  LD (lift_jet_skip),A    ;
  LD A,($C792)            ; Drawn: a white 2x2 patch 7 rows above it at x $70.
  AND $80                 ;
  RET Z                   ;
  LD HL,($C793)           ;
  LD A,H                  ;
  SUB $07                 ;
  LD H,A                  ;
  LD L,$70                ;
  LD A,$07                ;
  JP side_jet_4           ;

; The lift jet's sprite record
;
; Used by lift_jet. +0 the graphic ($12 or $11); +1 +2 the screen address it
; was drawn at; +3 bit 7 drawn, bit 6 mirrored; +4 +5 its screen x and y.
lift_jet_record:
  DEFB $12,$00,$00,$00,$00,$00

; The lift jet's record at the start of a life
;
; Copied over lift_jet_record by reset_for_life.
lift_jet_start:
  DEFB $12,$00,$00,$00,$00,$00

; Side jet colours
;
; Eight attributes for the jets' colour patches, chosen by the side jet's
; flicker count (draw_ship): red, yellow, magenta, blue, yellow, magenta, red,
; white, all bright. The lift jet uses the last.
jet_colours:
  DEFB $42,$46,$43,$41,$46,$43,$42,$47

; Get a jet's graphic and its height
;
; Used by the routines at side_jet and lift_jet.
;
; The graphic's address for the sprite record at IX (graphic_frame_list), and
; its first byte. Only the jets use this (side_jet, graphics $0E-$10; lift_jet,
; $11 and $12), whose pictures are in the height-first form (puff_graphic), so
; the byte is the height in lines, not a width.
;
;   IX The sprite record (+0 the graphic number)
; O:A The height
; O:DE The graphic's address plus one
sprite_graphic_width:
  CALL graphic_frame_list ; Look it up and read the height.
  LD A,(DE)               ;
  INC DE                  ;
  RET                     ;

; The side jet's flicker sequence
;
; Eight steps of the side jet's animation (draw_ship), each added to graphic
; $0E. Not a plain cycle, so it flickers.
side_jet_frames:
  DEFB $00,$01,$02,$01,$00,$02,$01,$02

; Use the mirrored trail image, depending on the speed's sign
;
; Used by the routine at draw_trail.
;
; For the trail (draw_ship): when bit 7 of trail_direction is clear, returns Z
; with B the bit-reversal page and L one byte further right, for a mirrored
; eraser or drawer; otherwise returns NZ.
trail_mirror:
  LD A,(trail_direction)  ; Bit 7 set: the plain image.
  BIT 7,A                 ;
  RET NZ                  ;
  LD B,$F9                ; Mirrored, one byte to the right.
  INC L                   ;
  XOR A                   ;
  RET                     ;

; The trail's frame
;
; The graphic number (1-6, trail_images) of the ship's trail as last drawn by
; draw_trail. With the next five bytes it is the trail's record, reset each
; life from trail_start.
trail_frame:
  DEFB $01

; The trail's screen x
;
; $68, or $78 facing left (draw_trail).
trail_x:
  DEFB $68

; The trail's screen y
;
; Its bottom row (draw_trail); lower while a part is being fitted.
trail_y:
  DEFB $61

; The trail's direction
;
; Bit 7 set while the ship's speed across is positive, or under half a pixel;
; otherwise clear and the trail is drawn mirrored (draw_trail, trail_mirror).
trail_direction:
  DEFB $00

; The ship turned this pass
;
; Bit 7 set by draw_ship_image on a pass the ship turns round; it makes
; draw_trail erase and redraw the trail.
ship_turned:
  DEFB $00

; Hold the trail straight
;
; A countdown: set to 4 by the collision code when the ship hits a ceiling
; (hit_block: a box of class bit 3, piece_classes) without add-on 9, and
; counted down by draw_trail, which holds the trail at frame 6 while it runs.
trail_hold:
  DEFB $00

; The trail's record at the start of a life
;
; Copied over trail_frame-trail_hold by reset_for_life.
trail_start:
  DEFB $01,$68,$61,$00,$00,$00

; The trail's six images
;
; Graphics 1 to 6, as the direct drawer draw_direct_pair draws the trail (three
; times the size).
;
; Graphics 1 to 6 (in the sprite graphic table that graphic_frame_list reads),
; each one byte wide and 8, 8, 7, 6, 5 and 4 rows high: a width, a height and
; the rows, bottom first. A line rising from its bottom row and leaning further
; over at each step, frame 6 a short curve. One byte is 8 pixels: the bend is
; in the images, not made by shifting. What the trail is in the game has not
; been seen: drawn behind the ship and bent by its speed across, it looks like
; a plume.
trail_images:
  DEFB $01,$08,$01,$01,$01,$01,$01,$01,$00,$01 ; Graphic 1: 8 rows
  DEFB $01,$08,$01,$01,$01,$01,$02,$02,$02,$04 ; Graphic 2: 8 rows
  DEFB $01,$07,$01,$01,$01,$02,$02,$04,$10 ; Graphic 3: 7 rows
  DEFB $01,$06,$01,$01,$02,$04,$18,$40 ; Graphic 4: 6 rows
  DEFB $01,$05,$01,$01,$06,$38,$80 ; Graphic 5: 5 rows
  DEFB $01,$04,$01,$81,$42,$3C ; Graphic 6: 4 rows

; The ship's running position across
;
; A word moved by the speed across each pass (speed_to_scroll): twice the
; change in its high byte is the pass's horizontal scroll. Zeroed when coasting
; brings the ship to a stop (accelerate_across).
ship_position_across:
  DEFW $0000

; Accelerate the ship across, and work out this pass's horizontal scroll
;
; Used by the routine at move_ship.
;
; Called by move_ship with the thrust (or the drag) for this pass. The ship's
; speed across is a word at +$19/+$1A of its record, whole pixels in the high
; byte (stored high byte first). The thrust is added and the speed clamped to
; the top speed (clamp_speed_across). With the controls off the drag must not
; turn the ship round, so a speed that changes sign is made zero instead.
;
; Then speed_to_scroll turns the speed into this pass's horizontal scroll.
;
; IX The ship record
; DE The thrust or drag this pass (8.8 fixed point)
accelerate_across:
  LD H,(IX+$19)           ; Add the thrust and clamp.
  LD L,(IX+$1A)           ;
  EX DE,HL                ;
  ADD HL,DE               ;
  CALL clamp_speed_across ;
  LD A,(steer_push)            ; Not steering, and the speed changed sign: stop
  AND A                        ; dead, the position word too.
  JR NZ,accelerate_across_0    ;
  LD A,H                       ;
  XOR D                        ;
  AND $80                      ;
  JR Z,accelerate_across_0     ;
  LD HL,$0000                  ;
  LD (ship_position_across),HL ;
accelerate_across_0:
  LD (IX+$19),H           ; The new speed.
  LD (IX+$1A),L           ;

; Turn the speed across into this pass's horizontal step
;
; Used by the routine at hit_block.
;
; The second half of accelerate_across, also used by hit_block. The speed is
; added to the running position word ship_position_across, and twice the change
; in its high byte is the step in pixels (the landscape moves in 2-pixel
; steps). A step of the top speed (top_speed) plus 2 is cut to the top speed.
; The step goes to scroll_across. One last rule: a step of 4 taken when the
; fine scroll (origin_x) is at 2 or 6 is halved, so 4-pixel steps start only
; from 0 or 4.
;
; HL The speed across (8.8)
speed_to_scroll:
  LD DE,(ship_position_across) ; The position word moves by the speed; the step
  ADD HL,DE                    ; is twice the change in its whole part.
  LD (ship_position_across),HL ;
  LD A,H                       ;
  SUB D                        ;
  ADD A,A                      ;
  LD H,A                       ;
  LD A,(top_speed)        ; The limit: the top speed (top_speed) plus 2, with
  BIT 7,H                 ; the step's sign.
  JR NZ,speed_to_scroll_0 ;
  ADD A,$02               ;
  LD B,A                  ;
  JR speed_to_scroll_1    ;
speed_to_scroll_0:
  NEG                     ;
  SUB $02                 ;
  LD B,A                  ;
speed_to_scroll_1:
  LD A,H                  ; A step at the limit becomes the top speed.
  BIT 7,A                 ;
  JR NZ,speed_to_scroll_2 ;
  CP B                    ;
  JR NZ,speed_to_scroll_3 ;
  LD A,(top_speed)        ;
speed_to_scroll_2:
  CP B                    ;
  JR NZ,speed_to_scroll_3 ;
  LD A,(top_speed)        ;
  NEG                     ;
speed_to_scroll_3:
  LD (scroll_across),A    ; This pass's horizontal scroll; anything but a
  CP $04                  ; 4-pixel step is done.
  JR Z,speed_to_scroll_4  ;
  CP $FC                  ;
  RET NZ                  ;
speed_to_scroll_4:
  LD A,(origin_x)         ; A 4-pixel step from 2 or 6 becomes 2.
  AND $02                 ;
  RET Z                   ;
  LD A,(scroll_across)    ;
  SRA A                   ;
  LD (scroll_across),A    ;
  RET                     ;

; Keep the speed across within the top speed
;
; Used by the routine at accelerate_across.
;
; The whole part of the speed may be no more than half the top speed
; (top_speed) one way, or one more than that the other (the fraction is kept).
;
; H The whole part of the speed
clamp_speed_across:
  LD A,(top_speed)          ; Negative: no less than minus half the top speed,
  SRL A                     ; less one.
  BIT 7,H                   ;
  JR Z,clamp_speed_across_0 ;
  NEG                       ;
  LD B,A                    ;
  LD A,H                    ;
  CP B                      ;
  RET NC                    ;
  DEC B                     ;
  LD H,B                    ;
  RET                       ;
clamp_speed_across_0:
  LD B,A                  ; Positive: no more than half the top speed.
  LD A,H                  ;
  CP B                    ;
  RET C                   ;
  LD H,B                  ;
  RET                     ;

; Keep a speed between -3 and 2
;
; Used by the routines at hit_block, bounce_45_negate and
; bounce_shallow_negate.
;
; Used by the object routines (hit_block, bounce_45_negate and
; bounce_shallow_negate) on a speed's whole part.
;
; H The whole part of the speed
clamp_object_speed:
  LD A,H                    ; Negative: -1 and -2 stand, anything less becomes
  BIT 7,A                   ; -3.
  JR Z,clamp_object_speed_0 ;
  CP $FE                    ;
  RET NC                    ;
  LD H,$FD                  ;
  RET                       ;
clamp_object_speed_0:
  CP $02                  ; Positive: 0 and 1 stand, anything more becomes 2.
  RET C                   ;
  LD H,$02                ;
  RET                     ;

; The bare ship
;
; The ship with nothing fitted: build_ship_sprite builds its picture from these
; rows and the direct drawer draw_direct_pair draws it, as in play (three times
; the size).
;
; The ship with no parts fitted, 6 bytes by 14 rows, bottom row first and no
; width or height bytes. build_ship_sprite copies it to the bottom of the
; buffer where the ship's image is built up with the parts the player has
; fitted.
bare_ship:
  DEFB $00,$00,$07,$FC,$00,$00
  DEFB $00,$0F,$F8,$06,$00,$00
  DEFB $00,$3F,$FF,$F8,$00,$00
  DEFB $00,$FF,$FF,$FF,$F8,$00
  DEFB $03,$FF,$FF,$FF,$FF,$F8
  DEFB $02,$00,$00,$00,$00,$1E
  DEFB $01,$FF,$FF,$FF,$FF,$E0
  DEFB $00,$83,$FF,$E0,$0E,$00
  DEFB $00,$43,$FF,$EF,$E0,$00
  DEFB $00,$23,$FF,$EE,$00,$00
  DEFB $00,$13,$FF,$E0,$00,$00
  DEFB $00,$0B,$FE,$00,$00,$00
  DEFB $00,$07,$E0,$00,$00,$00
  DEFB $00,$02,$00,$00,$00,$00

; The ship record
;
; 32 bytes, ship_record-part_being_fitted, laid out like the other objects'
; records, many of them used by name and given entries of their own: +1
; view_page_column the map column and +2 +3 view_page_x the x in the page and
; its fraction, +4 view_page_row the map row and +5 +6 view_page_y the y and
; fraction (this is the view's place in the world: the screen moves with the
; ship); +7 +8 ship_dx and +9 +A ship_dy the speeds (or this pass's step)
; across and down; +$0B-$17 ship_record_middle; +$18 ship_facing the facing;
; +$19 +$1A ship_across_speed and +$1B +$1C ship_down_speed the speeds across
; and up; +$1D ship_screen_x and +$1E ship_screen_y the ship's place on the
; screen; +$1F part_being_fitted a part being fitted. Reset each life from
; ship_record_start (reset_for_life).
;
; +0 here is 0 (an object's graphic number in the other records).
ship_record:
  DEFB $00

; The ship record: map column
;
; +1 of ship_record: the column of the page the view is in.
view_page_column:
  DEFB $00

; The ship record: x in the page
;
; +2 +3 of ship_record: x and its fraction.
view_page_x:
  DEFB $60,$00

; The ship record: map row
;
; +4 of ship_record: the row of the page the view is in. Map row $0B, the
; lowest of the surface, is where carried_part puts the carried part down;
; draw_rocket looks for the rocket's place.
view_page_row:
  DEFB $08

; The ship record: y in the page
;
; +5 +6 of ship_record: y and its fraction.
view_page_y:
  DEFB $6F,$00

; The ship record: the speed across, or the step
;
; +7 +8 of ship_record, high byte first. ship_speed_from_scroll sets it to
; minus this pass's horizontal scroll so that move_object moves the record; the
; collision code (place_collision_box) copies the speed across
; (ship_across_speed) into it to test the move, may reverse and halve it
; (reverse_speed), and ship_pass copies it back.
ship_dx:
  DEFW $0000

; The ship record: the speed down, or the step
;
; +9 +A of ship_record, as ship_dx for the vertical: ship_down_speed is its
; speed.
ship_dy:
  DEFW $0000

; The ship record: +$0B to +$17
;
; Nothing in this range uses these bytes by name; +$0D holds $47 at the start,
; the ship's colour it seems.
ship_record_middle:
  DEFB $00,$00,$47,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00

; The ship's facing
;
; +$18 of ship_record: bit 7 set facing left. Set from the steering
; (draw_ship_image); read wherever the ship and its add-ons are drawn mirrored
; (mirror_ship_sprite, set_mirrored_drawer, draw_carried_piece).
ship_facing:
  DEFB $00

; The ship's speed across
;
; +$19 +$1A of ship_record, 8.8 fixed point stored high byte first: accelerated
; and clamped by accelerate_across, copied back from ship_dx after the
; collision tests (ship_pass); read by draw_trail for the trail's bend.
ship_across_speed:
  DEFW $0000

; The ship's climb speed
;
; +$1B +$1C of ship_record, the same for the vertical, up positive: gravity
; takes from it and thrust adds to it (move_ship).
ship_down_speed:
  DEFW $0000

; The ship's screen x
;
; +$1D of ship_record: $60. The ship does not move on the screen; the world
; does.
ship_screen_x:
  DEFB $60

; The ship's screen y
;
; +$1E of ship_record: $6F at the start, the row the ship is drawn up from;
; changed by build_ship_sprite when parts make the ship taller.
ship_screen_y:
  DEFB $6F

; A part being fitted to the ship
;
; +$1F of ship_record. Set when the ship picks up a part (collect_pickup: the
; kind plus one, or $C0 plus the rocket part's number for a rocket part); while
; it is non-zero draw_ship_image erases and redraws the ship, fitting the part
; (build_ship_sprite) or hanging a rocket part under it (toggle_carried_part).
part_being_fitted:
  DEFB $00

; The ore collected
;
; Part 6 of the rocket needs 18. Kept in the player's record between lives
; (play, lose_life).
ore:
  DEFB $00

; The ship record at the start of a life
;
; Copied over ship_record by reset_for_life (32 bytes); the byte after it is
; not used.
ship_record_start:
  DEFB $00,$00,$60,$00,$08,$6F,$00,$00
  DEFB $00,$00,$00,$00,$00,$47,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $03,$00,$00,$00,$00,$60,$6F,$00
  DEFB $00

; The bitmap under the scores
;
; As draw_bitmap draws it (three times the size).
;
; 4 bytes by 9 rows (a width, a height, the rows bottom first), drawn under
; each player's score by draw_panel.
score_bitmap:
  DEFB $04,$09,$00,$03,$E0,$00,$00,$7C
  DEFB $10,$00,$01,$FF,$E0,$00,$07,$FF
  DEFB $FF,$00,$08,$00,$00,$F0,$07,$7F
  DEFB $7F,$00,$03,$7F,$70,$00,$01,$7F
  DEFB $00,$00,$00,$F0,$00,$00

; The frame's ornament for the top and bottom edges
;
; As draw_bitmap draws it (three times the size).
;
; 1 byte by 6 rows (draw_frame).
edge_ornament:
  DEFB $01,$06,$81,$E7,$1E,$78,$E7,$81

; The frame's ornament for the sides
;
; As draw_bitmap draws it (three times the size).
;
; 1 byte by 6 rows (draw_frame).
side_ornament:
  DEFB $01,$06,$34,$34,$18,$18,$2C,$2C

; Unreferenced bytes
;
; Nothing refers to these 18 bytes (searched); they look like the remains of
; text records.
panel_leftovers:
  DEFB $00,$00,$20,$77,$47,$02
  DEFB $00,$00,$00,$00,$08,$68
  DEFB $6F,$47,$FF,$00,$00,$00

; Copy a bitmap to the screen
;
; Used by the routines at move_method_arrow, draw_panel, draw_frame,
; draw_fuel_gauge and menu.
;
; Draws a byte-aligned bitmap with no mask, overwriting what is there: a width
; in bytes, a height in rows, then the rows, bottom row first, drawn upwards
; from the given pixel position (line_up_hl steps up a row). Used for the panel
; and the play area's frame.
;
;   H The y of the bottom row
;   L The x (a multiple of 8)
;   DE The bitmap
; O:DE The byte after it
draw_bitmap:
  PUSH HL                 ; Read the width and height; the screen address
  LD A,(DE)               ; (pixel_to_screen).
  LD B,A                  ;
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  INC DE                  ;
  CALL pixel_to_screen    ;
  LD A,B                  ;
  LD (scratch_count),A    ;
draw_bitmap_0:
  PUSH HL                 ; Each row: copy the bytes, then up one pixel row.
  LD A,(scratch_count)    ;
  LD B,A                  ;
draw_bitmap_1:
  LD A,(DE)               ;
  LD (HL),A               ;
  INC DE                  ;
  INC HL                  ;
  DJNZ draw_bitmap_1      ;
  POP HL                  ;
  CALL line_up_hl         ;
  DEC C                   ;
  JR NZ,draw_bitmap_0     ;
  POP HL                  ;
  RET

; Apply a colour patch
;
; Used by the routines at colour_player_line, contact_damage, draw_beams,
; colour_sprite, draw_shot, side_jet, draw_panel, flash_fuel_gauge, draw_frame,
; colour_ship and colour_play_area.
;
; Fills a rectangle of attribute cells with one attribute, from a three-byte
; record: width in cells, height in cells, attribute. The rectangle is given by
; its bottom-left pixel and filled upwards. Every sprite's colour comes from
; such a patch: the patch is all the colour a sprite has, so a sprite takes the
; colour of the cells it is in (colour_sprite works the patches out for the
; objects).
;
;   H The y of the bottom row
;   L The x
;   DE The record
; O:DE The byte after it
fill_attributes:
  LD A,(DE)               ; Width in B, height in C, and the attribute address
  LD B,A                  ; (pixel_to_attribute).
  INC DE                  ;
  LD A,(DE)               ;
  LD C,A                  ;
  CALL pixel_to_attribute ;
  INC DE                  ; The attribute in D.
  LD A,(DE)               ;
  INC DE                  ;
  PUSH DE                 ;
  LD D,A                  ;
  LD E,B                  ;
fill_attributes_0:
  PUSH HL                 ; Each row of cells: fill it, then up a row (32 bytes
  LD B,E                  ; back).
fill_attributes_1:
  LD (HL),D               ;
  INC HL                  ;
  DJNZ fill_attributes_1  ;
  POP HL                  ;
  PUSH BC                 ;
  LD BC,$0020             ;
  AND A                   ;
  SBC HL,BC               ;
  POP BC                  ;
  DEC C                   ;
  JR NZ,fill_attributes_0 ;
  POP DE                  ;
  RET                     ;

; An unused bitmap-to-attributes routine
;
; Code nothing calls (searched): draw_bitmap written for the attributes (a
; width, a height and the attribute bytes, each row filled upwards). A
; leftover, kept as data.
unused_attribute_bitmap:
  DEFB $E5,$1A,$47,$13,$1A,$4F,$CD,$70
  DEFB $A1,$78,$32,$1C,$5C,$13,$E5,$3A
  DEFB $1C,$5C,$47,$1A,$77,$13,$23,$10
  DEFB $FA,$E1,$D5,$11,$20,$00,$A7,$ED
  DEFB $52,$D1,$0D,$20,$E9,$E1,$C9

; The panel's text records: 'SCORE' twice
;
; The first of the panel's eight text records, printed by draw_panel through
; print_strings and print_string. Each is x, y (pixels), an attribute (bit 7
; for double height), a length, and the characters in the game's own font
; (font, 7 rows a character; codes $1A-$23 are the digits). Here: 'SCORE' for
; each player, white. The others follow: p1_score_text, p1_lives_text,
; p2_score_text, p2_lives_text, hi_label, hiscore_text.
score_labels:
  DEFB $00,$07,$47,$05,$12,$02,$0E,$11,$04
  DEFB $D8,$07,$47,$05,$12,$02,$0E,$11,$04

; The panel's text records: player 1's score
;
; x, y, the attribute (set by draw_panel: yellow for the player in play, red
; for the other) and the length; the six digits follow at p1_score_digits.
p1_score_text:
  DEFB $00,$0F,$46,$06

; Player 1's score
;
; Six digit codes ($1A-$23) of the text record p1_score_text: the score as it
; is kept and printed.
p1_score_digits:
  DEFB $1A,$1A,$1A,$1A,$1A,$1A

; The panel's text records: player 1's lives
;
; x, y, attribute, length 2, then a '0' and the digit at p1_lives_digit. Its x
; is tested by new_game to see which player's record is in front.
p1_lives_text:
  DEFB $50,$0A,$45,$02,$1A

; Player 1's lives digit
;
; The lives less one plus $19 (print_lives): the ship in play is not counted.
p1_lives_digit:
  DEFB $1D

; The panel's text records: player 2's score
;
; As p1_score_text; the digits at p2_score_digits.
p2_score_text:
  DEFB $D0,$0F,$46,$06

; Player 2's score
;
; Six digit codes of the text record p2_score_text.
p2_score_digits:
  DEFB $1A,$1A,$1A,$1A,$1A,$1A

; The panel's text records: player 2's lives
;
; As p1_lives_text; the digit at p2_lives_digit.
p2_lives_text:
  DEFB $A0,$0A,$45,$02,$1A

; Player 2's lives digit
;
; All the waiting player's lives plus $1A (draw_panel).
p2_lives_digit:
  DEFB $1D

; The panel's text records: 'HI'
;
; The heading over the high score: one of the panel's text records, laid out as
; score_labels says (x, y, attribute, length, then the characters in the game's
; font).
hi_label:
  DEFB $78,$07,$44,$02,$07,$08

; The panel's text records: the high score
;
; x, y, attribute, length; the six digits at hiscore_digits.
hiscore_text:
  DEFB $68,$0F,$46,$06

; The high score
;
; Six digit codes of the text record hiscore_text.
hiscore_digits:
  DEFB $1A,$1B,$1A,$1A,$1A,$1A

; The player in play
;
; 0 for player 1 or 1 for player 2 (new_game, swap_players, show_player); also
; $99 for a moment while new_game puts the score lines back. Not part of the
; text.
player_in_play:
  DEFB $00

; The play area's frame
;
; The 17-byte description draw_frame draws the frame from (see there for the
; layout): the two sides, the two horizontal edges, the corner list
; (frame_corners), the ornament list (ornament_places) and the ornaments'
; places.
frame_description:
  DEFB $C0,$57,$DD,$57,$A0,$E1,$56,$41,$46
  DEFB $00,$00,$06,$00,$00,$16,$E8,$1C

; The colour patch behind the top of the panel
;
; 20 cells by 2, white (draw_panel, through fill_attributes).
panel_patch:
  DEFB $14,$02,$47

; Print the lives of the player in play
;
; Used by the routines at contact_extra_life and draw_panel.
;
; The number shown is the lives less one: the ship in play is not counted. The
; digit codes start at $1A, so lives plus $19.
print_lives:
  LD A,(lives)            ; Make the digit and print the record at
  ADD A,$19               ; p1_lives_text (print_string).
  LD (p1_lives_digit),A   ;
  LD DE,p1_lives_text     ;
  JP print_string         ;

; Draw the panel and the play area's frame
;
; Used by the routines at game and play.
;
; What it draws on a clear screen, run in the simulator after the game's
; start-up (twice the size).
;
; Called by game before a game and by play at the start of each life. Player
; 1's score in yellow and player 2's in red, the colour patch behind the
; panel's top, both players' lives, the eight text records from score_labels, a
; bitmap under each score, the frame (draw_frame), the fuel gauge (the entry at
; the end of flash_fuel_gauge, draw_fuel_gauge) and the word 'FUEL' beside it.
draw_panel:
  LD A,$46                ; Score colours: player 1 yellow, player 2 red.
  LD ($C9EE),A            ;
  LD A,$42                ;
  LD ($C9FE),A            ;
  LD HL,$0F30             ; The white patch behind the top of the panel.
  LD DE,panel_patch       ;
  CALL fill_attributes    ;
  CALL print_lives        ; Player 1's lives; player 2's digit is all its lives
  LD A,($F656)            ; (+6 of other_player_record, plus $1A).
  ADD A,$1A               ;
  LD (p2_lives_digit),A   ;
  LD DE,score_labels      ; The eight text records.
  LD B,$08                ;
  CALL print_strings      ;
  LD HL,$0B30             ; The bitmap under each score.
  LD DE,score_bitmap      ;
  CALL draw_bitmap        ;
  LD HL,$0BB0             ;
  LD DE,score_bitmap      ;
  CALL draw_bitmap        ;
  LD IX,frame_description ; The frame: its corners at frame_corners, its
  LD DE,frame_corners     ; ornaments' places at ornament_places.
  LD (IX+$09),E           ;
  LD (IX+$0A),D           ;
  LD DE,ornament_places   ;
  LD (IX+$0C),E           ;
  LD (IX+$0D),D           ;
  CALL draw_frame         ;
  CALL flash_fuel_gauge_0 ; The gauge: its colour and its outline.
  CALL draw_fuel_gauge    ;
  LD HL,$37F0             ; The cyan patch behind the label.
  PUSH HL                 ;
  LD DE,fuel_label_patch  ;
  CALL fill_attributes    ;
  POP HL                  ; The label, 2 bytes by 32 rows, at x $F0
  LD DE,fuel_label        ; (fuel_label).
  JP draw_bitmap          ;

; Colour the fuel gauge, flashing red when fuel is low
;
; Used by the routine at update_fuel_gauge.
;
; On odd passes only. With fuel low (the high byte of fuel, $98 or more: about
; seven tenths used) the gauge's colour patch (2 cells by 16, at x $F0) is red
; on two passes in four and yellow on the others; otherwise yellow. From the
; entry point near its end (used by draw_panel): always yellow.
flash_fuel_gauge:
  LD A,(pass_counter)     ; Even passes: nothing.
  LD B,A                  ;
  AND $01                 ;
  RET Z                   ;
  LD A,($CC4C)            ; Low and bit 1 of the pass counter set: red.
  CP $98                  ;
  JR C,flash_fuel_gauge_0 ;
  BIT 1,B                 ;
  JR Z,flash_fuel_gauge_0 ;
  LD A,$42                ;
  JR flash_fuel_gauge_1   ;
; This entry point is used by the routine at draw_panel.
flash_fuel_gauge_0:
  LD A,$46                ; Yellow.
flash_fuel_gauge_1:
  LD (gauge_colour),A     ; Set the patch's attribute and apply it.
  LD HL,$BFF0             ;
  LD DE,gauge_patch       ;
  JP fill_attributes      ;

; Draw the play area's frame
;
; Used by the routines at draw_panel and menu.
;
; From a 17-byte description at IX (frame_description for the game; the menu,
; menu, has its own). It draws the two sides, the two edges top and bottom, the
; four corner pieces with their colour patches, and the ornaments along the
; edges.
;
; The description: +0 +1 and +2 +3 the screen addresses of the bottom of the
; left and right sides; +4 their height in rows (each row a byte of $66); +5 +6
; and +7 +8 the screen addresses of the two horizontal edges (each two double
; lines of $FF bytes, with a two-row gap); +9 +A the corner list (four entries
; of seven bytes: the bitmap's place, the colour patch's place, the patch's
; width, height and attribute; the bitmaps follow one another from
; corner_bitmaps, frame_corners); +B the number of ornaments; +C +D their list
; (pairs: an x for the top and bottom edges, a y for the sides); +E the y of
; the bottom edge's ornaments (the top's is $BE); +F the x of the right side's
; (the left's is 0); +$10 the edges' width in bytes.
;
; IX The frame's description
draw_frame:
  LD L,(IX+$00)           ; The two sides' addresses and height.
  LD H,(IX+$01)           ;
  LD E,(IX+$02)           ;
  LD D,(IX+$03)           ;
  LD B,(IX+$04)           ;
draw_frame_0:
  LD (HL),$66             ; The sides, upwards, a $66 byte each row.
  LD A,$66                ;
  LD (DE),A               ;
  CALL line_up_hl         ;
  CALL line_up_de         ;
  DJNZ draw_frame_0       ;
  LD L,(IX+$05)           ; The two horizontal edges: two rows, a gap of two
  LD H,(IX+$06)           ; rows, two rows.
  LD E,(IX+$07)           ;
  LD D,(IX+$08)           ;
  CALL fill_edge_rows     ;
  CALL fill_edge_rows     ;
  DEC H                   ;
  DEC H                   ;
  DEC D                   ;
  DEC D                   ;
  CALL fill_edge_rows     ;
  CALL fill_edge_rows     ;
  LD DE,corner_bitmaps    ; The corner bitmaps start at corner_bitmaps.
  LD (box_x),DE           ;
  LD E,(IX+$09)           ; Four corners: a bitmap, then its colour patch.
  LD D,(IX+$0A)           ;
  LD B,$04                ;
draw_frame_1:
  PUSH BC                 ;
  CALL read_word          ;
  PUSH DE                 ;
  LD DE,(box_x)           ;
  CALL draw_bitmap        ;
  LD (box_x),DE           ;
  POP DE                  ;
  CALL read_word          ;
  CALL fill_attributes    ;
  POP BC                  ;
  DJNZ draw_frame_1       ;
  LD B,(IX+$0B)           ; Each ornament: one at its x on the bottom edge and
  LD E,(IX+$0C)           ; one on the top.
  LD D,(IX+$0D)           ;
draw_frame_2:
  PUSH BC                 ;
  LD A,(DE)               ;
  LD L,A                  ;
  LD H,(IX+$0E)           ;
  PUSH DE                 ;
  LD DE,edge_ornament     ;
  CALL draw_bitmap        ;
  LD H,$BE                ;
  LD DE,edge_ornament     ;
  CALL draw_bitmap        ;
  POP DE                  ;
  INC DE                  ; And one at its y on the left side and one on the
  LD A,(DE)               ; right.
  LD H,A                  ;
  LD L,$00                ;
  PUSH DE                 ;
  LD DE,side_ornament     ;
  CALL draw_bitmap        ;
  LD L,(IX+$0F)           ;
  LD DE,side_ornament     ;
  CALL draw_bitmap        ;
  POP DE                  ;
  INC DE                  ;
  POP BC                  ;
  DJNZ draw_frame_2       ;
  RET

; The fuel gauge's label
;
; As draw_bitmap draws it (three times the size).
;
; The word 'FUEL' set vertically: a bitmap 2 bytes by 32 rows (a width, a
; height, the rows bottom first), drawn beside the gauge by draw_panel.
fuel_label:
  DEFB $02,$20,$0F,$F0,$0F,$00,$0F,$00
  DEFB $0F,$00,$0F,$00,$0F,$00,$0F,$00
  DEFB $0F,$00,$00,$00,$0F,$F0,$0F,$00
  DEFB $0F,$00,$0F,$C0,$0F,$00,$0F,$00
  DEFB $0F,$F0,$00,$00,$07,$E0,$0F,$30
  DEFB $0F,$30,$0F,$30,$0F,$30,$0F,$30
  DEFB $0F,$30,$00,$00,$0F,$00,$0F,$00
  DEFB $0F,$00,$0F,$C0,$0F,$00,$0F,$00
  DEFB $0F,$F0

; The fuel label's colour patch
;
; Cyan, 2 cells by 4 (draw_panel).
fuel_label_patch:
  DEFB $02,$04,$45

; The fuel gauge's colour patch
;
; 2 cells by 16; its attribute follows at gauge_colour (flash_fuel_gauge).
gauge_patch:
  DEFB $02,$10

; The fuel gauge's colour
;
; Yellow, or red when fuel is low (flash_fuel_gauge).
gauge_colour:
  DEFB $46

; Draw the fuel gauge's outline
;
; Used by the routine at draw_panel.
;
; The gauge is a tube at x $F0-$FF: a cap at the foot (line $BF, gauge_top_cap)
; and one at the top (line $43, gauge_bottom_cap), each 2 bytes by 4 rows, and
; between them 120 rows of two single-pixel walls ($10 in each byte). The fuel
; inside is drawn by update_fuel_gauge.
draw_fuel_gauge:
  LD HL,$BFF0             ; The two caps.
  LD DE,gauge_top_cap     ;
  CALL draw_bitmap        ;
  LD HL,$43F0             ;
  LD DE,gauge_bottom_cap  ;
  CALL draw_bitmap        ;
  LD HL,$53FE             ; The walls, upwards.
  LD B,$78                ;
draw_fuel_gauge_0:
  LD (HL),$10             ;
  INC HL                  ;
  LD (HL),$10             ;
  DEC HL                  ;
  CALL line_up_hl         ;
  DJNZ draw_fuel_gauge_0  ;
  RET                     ;

; The fuel gauge's top cap
;
; As draw_bitmap draws it (three times the size).
;
; 2 bytes by 4 rows, closed at the top: drawn at line $43, the top of the tube
; (draw_fuel_gauge). The label says bottom; the picture and its place say top.
gauge_bottom_cap:
  DEFB $02,$04,$10,$10,$18,$30,$0C,$60,$07,$C0

; The fuel gauge's bottom cap
;
; As draw_bitmap draws it (three times the size).
;
; 2 bytes by 4 rows: drawn at line $BF, the foot of the tube (draw_fuel_gauge).
; The label says top; its place says bottom.
gauge_top_cap:
  DEFB $02,$04,$07,$C0,$0C,$60,$18,$30,$10,$10

; The frame's ornament places
;
; Six pairs for draw_frame: an x on the top and bottom edges, a y on the sides.
ornament_places:
  DEFB $20,$36
  DEFB $40,$56
  DEFB $60,$7E
  DEFB $88,$9E
  DEFB $A8,$9E
  DEFB $C8,$9E

; Read a word
;
; Used by the routine at draw_frame.
;
;   DE The word's address
; O:HL The word
; O:DE The address after it
read_word:
  LD A,(DE)               ; Low byte, then high byte.
  INC DE                  ;
  LD L,A                  ;
  LD A,(DE)               ;
  INC DE                  ;
  LD H,A                  ;
  RET                     ;

; The frame's corners
;
; Four entries of seven bytes for draw_frame: the corner bitmap's place (x, y),
; then a colour patch's place (x, y), width, height and attribute ($4D: bright
; cyan on blue). The patches are the frame's colour: the top and bottom edges
; (30 cells by 1) and the two sides (1 by 20). The bitmaps are at
; corner_bitmaps.
frame_corners:
  DEFB $00,$BE,$00,$BF,$1E,$01,$4D
  DEFB $E8,$BE,$00,$17,$1E,$01,$4D
  DEFB $00,$17,$00,$B7,$01,$14,$4D
  DEFB $E8,$17,$E8,$B7,$01,$14,$4D

; The frame's corner bitmaps
;
; The four corners in turn, as draw_bitmap draws them (three times the size).
;
; Four bitmaps, each 1 byte by 7 rows (a width, a height, the rows bottom
; first), drawn in turn by draw_frame for both frames, the panel's
; (frame_corners) and the menu's (menu_frame_corners).
corner_bitmaps:
  DEFB $01,$07,$07,$07,$06,$06,$76,$76,$66
  DEFB $01,$07,$E0,$E0,$60,$60,$6E,$6E,$66
  DEFB $01,$07,$66,$76,$76,$06,$06,$07,$07
  DEFB $01,$07,$66,$6E,$6E,$60,$60,$E0,$E0

; Draw a row of the frame's two horizontal edges
;
; Used by the routine at draw_frame.
;
; Fills a row of $FF bytes at HL and the same at DE, then moves both up one
; pixel row (within a character row only: the edges never cross one).
;
; IX The frame's description (+$10 the width in bytes)
; HL The bottom edge's row
; DE The top edge's row
fill_edge_rows:
  LD B,(IX+$10)           ; Fill both rows; up one row.
  PUSH HL                 ;
  PUSH DE                 ;
fill_edge_rows_0:
  LD (HL),$FF             ;
  LD A,$FF                ;
  LD (DE),A               ;
  INC DE                  ;
  INC HL                  ;
  DJNZ fill_edge_rows_0   ;
  POP DE                  ;
  POP HL                  ;
  DEC D                   ;
  DEC H                   ;
  RET                     ;

; The fuel
;
; A word, its high byte the gauge's level as a screen row: $42 full, $BE empty,
; so burning fuel makes it larger. Changed only through add_fuel: the burn at
; read_controls adds 8 or 16 a pass, contact_refuel takes away $1C00 (a
; refuel), contact_drain_fuel adds $1000. Filled (high byte $42) at the start
; of each life by start_landscape; set to empty (high byte $BE) as the death
; sequence starts (play).
fuel:
  DEFW $0000

; Change the fuel, keeping the level between full and empty
;
; Used by the routines at contact_refuel, contact_drain_fuel and read_controls.
;
; DE The amount to add to the fuel word (positive burns, negative fills)
add_fuel:
  LD HL,(fuel)            ; Add it.
  ADD HL,DE               ;
  LD A,H                  ; No emptier than $BE.
  CP $BE                  ;
  JR C,add_fuel_0         ;
  LD H,$BE                ;
add_fuel_0:
  CP $42                  ; No fuller than $42.
  JR NC,add_fuel_1        ;
  LD H,$42                ;
add_fuel_1:
  LD (fuel),HL            ; Store it.
  RET                     ;

; The fuel level shown
;
; The row the gauge's fuel column reaches on the screen. update_fuel_gauge
; moves it one row a pass towards the fuel's level, so the gauge fills up at
; the start of a life from $BE (set by start_landscape) and drains one row at a
; time.
fuel_shown:
  DEFB $00

; Start the death sequence when the gauge shows empty
;
; Used by the routine at update_fuel_gauge.
;
; Reached from update_fuel_gauge when the gauge is up to date. With the ship
; alive and the gauge showing empty ($BE), the death byte is set to 1 the next
; time the low seven bits of the pass counter are zero: up to 128 passes (about
; six seconds) of grace after the fuel runs out.
;
; B The level shown
check_fuel_empty:
  LD A,(death_count)      ; Already dying: nothing.
  AND A                   ;
  RET NZ                  ;
  LD A,B                  ; Not empty: nothing.
  CP $BE                  ;
  RET NZ                  ;
  LD A,(pass_counter)     ; Wait for the pass counter.
  AND $7F                 ;
  RET NZ                  ;
  INC A                   ; Death byte 1.
  LD (death_count),A      ;
  RET                     ;

; Colour the fuel gauge, and move its fuel column a row towards the fuel level
;
; Used by the routines at start_landscape and play.
;
; Called each pass by the play loop (play) and twice a pass in the death
; sequence. The fuel inside the gauge is a column three to five pixels wide
; that runs from the bottom of the gauge up to the level shown. It is never
; redrawn: each step towards the fuel's level toggles (XOR) two rows: $03,$80
; (three pixels in the middle, x $F6-$F8) at one row and $04,$40 (the two
; pixels either side, $F5 and $F9) at the row below. Two neighbouring steps
; overlap, so every row inside the column ends up with all five pixels and only
; the top row has the narrow three: a rounded top, kept up to date by touching
; two rows a pass.
;
; Going up (filling), the toggled rows are the new level and the one below it;
; going down (burning), the old level and the one below, which takes the top
; row's three pixels away and leaves the next row's narrow three as the new
; top.
update_fuel_gauge:
  CALL flash_fuel_gauge   ; First the gauge's colour (flash_fuel_gauge).
  LD A,(fuel_shown)       ; Level shown against the fuel's level: the same,
  LD B,A                  ; then see whether it is empty.
  LD A,($CC4C)            ;
  SUB B                   ;
  JR Z,check_fuel_empty   ;
  DEC B                     ; Filling (level going up the screen): the rows
  LD H,B                    ; from the new level; burning: from the old level,
  BIT 7,A                   ; and the new level is one row lower.
  JR NZ,update_fuel_gauge_0 ;
  INC B                     ;
  LD H,B                    ;
  INC B                     ;
update_fuel_gauge_0:
  LD A,B                  ; The new level shown.
  LD (fuel_shown),A       ;
  LD A,H                  ; The first row: x $F0, the middle three pixels
  LD (scratch_count),A    ; toggled.
  LD L,$F0                ;
  CALL pixel_to_screen    ;
  LD A,$03                ;
  XOR (HL)                ;
  LD (HL),A               ;
  INC HL                  ; (Second byte of that row.)
  LD A,$80                ;
  XOR (HL)                ;
  LD (HL),A               ;
  LD A,(scratch_count)    ; Not below the bottom of the tube.
  CP $BC                  ;
  RET NC                  ;
  INC A                   ; The row below: the two outer pixels toggled.
  LD H,A                  ;
  LD L,$F0                ;
  CALL pixel_to_screen    ;
  LD A,$04                ;
  XOR (HL)                ;
  LD (HL),A               ;
  INC HL                  ;
  LD A,$40                ;
  XOR (HL)                ;
  LD (HL),A               ;
  RET                     ;

; This pass's vertical scroll
;
; Signed, in pixels: the ship's step up this pass, worked out from its climb
; speed (move_ship), positive climbing; 0 while dying. With scroll_across it is
; read as a word by draw_stars.
scroll_down:
  DEFB $00

; This pass's horizontal scroll
;
; Signed, in pixels, a multiple of 2 (speed_to_scroll).
scroll_across:
  DEFB $00

; Something to do for this star
;
; Bit 0 set by draw_star_layer and star_in_sight when the star has an old image
; to erase or a new one to draw.
star_drawn:
  DEFB $00

; This pass's shift table
;
; Chosen by draw_landscape from the fine scroll: one of the four tables from
; piece_blitters_shift2 (shifts 2, 4, 6, then 0) (sixteen entries of four
; bytes, one per piece direction: a blitter entry point and the address of the
; piece's graphic at that shift).
shift_table:
  DEFW $0000

; The landscape's graphic-offset table
;
; Six bytes for each piece direction 0 to 8 (directions 7 to 13 use 0 to 6's
; rows, 14 and 15 rows 7 and 8: the code in landscape_step_loop that finds the
; row). The byte at the row plus twice the column's variety (0-2, from
; column_varieties), plus one if the fine scroll is not 0 (draw_landscape then
; starts the table one byte on), is the offset into the piece's graphic at
; which its POP blitter starts: so each column of a flat run can show one of
; three stretches of the ground's graphic.
graphic_offsets:
  DEFB $00,$00,$0C,$0C,$18,$18
  DEFB $00,$00,$12,$1B,$24,$36
  DEFB $00,$00,$09,$12,$12,$24
  DEFB $00,$00,$11,$22,$22,$44
  DEFB $00,$00,$12,$1B,$24,$36
  DEFB $00,$00,$09,$12,$12,$24
  DEFB $00,$00,$11,$22,$22,$44
  DEFB $00,$00,$11,$22,$22,$44
  DEFB $00,$00,$11,$22,$22,$44

; Move the column address on by the piece's column step, and stop the piece
; stepping
;
; Used by the routines at set_right_clip_columns, use_mirrored_right_clip and
; slope_step_at_left_edge.
;
; Used by the clipping code at use_mirrored_right_clip and
; slope_step_at_left_edge: the current column's screen address (its low byte,
; column_address) takes the piece's column step at once, and the piece's x and
; column steps (piece_dx, piece_dcolumn) become zero.
fold_column_step:
  LD A,(column_address)   ; Add the step; clear both steps.
  LD B,A                  ;
  LD A,(piece_dcolumn)    ;
  ADD A,B                 ;
  LD (column_address),A   ;
  XOR A                   ;
  LD (piece_dcolumn),A    ;
  LD (piece_dx),A         ;
  RET                     ;

; Set up a piece's steps from its direction
;
; Used by the routine at draw_landscape.
;
; Called for each piece by draw_landscape with the piece's direction in +3 of
; the piece state at IX (current_piece). A tile turned upside down
; (draw_landscape's $99) first remaps the direction through upside_down_table:
; 0-6 to 7-13 and back, which turns each piece over top to bottom. Then
; set_piece_shape loads the direction's three steps from piece_shapes: the x
; step in pixels (piece_dx), the column step in screen bytes (piece_dcolumn)
; and the rise in rows (piece_rise), each per step of the piece, and sets
; direction_flag to $FF for a piece that steps right or 1 for one that steps
; left.
;
; The directions, by their steps (x pixels, bytes, rows): 0 flat (24, 3, 0); 1
; a gentle slope (16, 2, 8); 2 a steep one (8, 1, 8); 3 steeper (8, 1, 16); 4-6
; the same three going left (-16, -2, 8; -8, -1, 8; -8, -1, 16); 7-13 the same
; seven upside down (the rise is still taken from 0-6, and piece_ceiling bit 7
; marks them); 14 and 15 a wall, going right or left (0, +1 or -1, 16).
;
; From those: piece_rows, the rows to work on per step (piece_rise, or 4 for a
; flat piece); piece_step_y, the size of the column step; piece_dx_copy, the x
; step (0 for a wall); piece_step_x, the size of the x step, or 8 for a wall. A
; wall in an upside-down tile is also moved 8 pixels.
;
; IX The piece state (current_piece)
load_piece_steps:
  LD A,(tile_upside_down) ; An upside-down tile: the direction through
  AND A                   ; upside_down_table.
  JR Z,load_piece_steps_0 ;
  LD E,(IX+$03)           ;
  LD D,$00                ;
  LD HL,upside_down_table ;
  ADD HL,DE               ;
  LD A,(HL)               ;
  LD (IX+$03),A           ;
load_piece_steps_0:
  CALL set_piece_shape    ; The direction's steps.
  LD A,(piece_rise)        ; Rows per step: the rise, or 4 for a flat piece.
  LD (piece_rows),A        ;
  AND A                    ;
  JR NZ,load_piece_steps_1 ;
  OR $04                   ;
  LD (piece_rows),A        ;
load_piece_steps_1:
  LD A,(piece_dcolumn)    ; The column step, and the x step kept in
  LD (piece_step_y),A     ; piece_dx_copy.
  LD A,(piece_dx)         ;
  LD (piece_dx_copy),A    ;
  AND A                    ; A wall (no x step): 8 pixels, with the sign of its
  JR NZ,load_piece_steps_2 ; direction.
  LD A,(direction_flag)    ;
  NEG                      ;
  ADD A,A                  ;
  ADD A,A                  ;
  ADD A,A                  ;
  LD B,A                   ;
load_piece_steps_2:
  LD (piece_step_x),A     ; piece_step_x the size of the x step; going left,
  BIT 7,A                 ; piece_step_y the size of the column step.
  JR Z,load_piece_steps_3 ;
  NEG                     ;
  LD (piece_step_x),A     ;
  LD A,(piece_step_y)     ;
  NEG                     ;
  LD (piece_step_y),A     ;
load_piece_steps_3:
  LD A,(tile_upside_down) ; A wall upside down: 8 pixels over.
  CP $99                  ;
  RET NZ                  ;
  LD A,(piece_dx_copy)    ;
  AND A                   ;
  RET NZ                  ;
  LD A,(piece_page_x)     ;
  ADD A,B                 ;
  LD (piece_page_x),A     ;
  RET                     ;

; Read a flat piece's end pieces, and mirror a piece for a mirrored tile
;
; Used by the routine at draw_landscape.
;
; Called for each piece by draw_landscape after load_piece_steps, with HL at
; the byte after the piece's direction-and-steps byte and C its number of
; steps. Only a flat piece (no rise) has two more bytes: the codes of the
; pieces at its left and right ends, kept in piece_extra_a and piece_extra_b
; ($16 means none, and codes from $10 have special handling in flat_step_clip).
; For a tile mirrored left-right ($FF) they are read the other way round with
; bit 1 changed (left for right); for one turned upside down ($99) bit 0 is
; changed as well (top for bottom), and the piece's y is moved (XOR $0C).
;
; The address of the next piece is kept in next_piece. Then, for a mirrored
; tile, the piece's x is reflected across the page (piece_page_x becomes the
; complement of x plus its x step plus $0F), its direction is remapped through
; mirror_table (right for left), set_piece_shape reloads the steps, and a flat
; run is moved back along by its length less one step (24 pixels a step), so it
; starts at its other end. A flat piece whose right end is $16 is not mirrored
; at all.
;
; HL The next byte of the tile record
; C The piece's number of steps
; IX The piece state (current_piece)
piece_ends:
  LD A,(piece_rise)       ; A piece with a rise has no end bytes.
  AND A                   ;
  JR NZ,piece_ends_4      ;
  LD A,(tile_upside_down) ; An upside-down tile moves a flat piece's y.
  AND A                   ;
  JR Z,piece_ends_0       ;
  LD A,(piece_page_y)     ;
  XOR $0C                 ;
  LD (piece_page_y),A     ;
piece_ends_0:
  LD A,(tile_mirrored_left_right) ; A mirrored tile reads the ends the other
  AND A                           ; way round.
  JR Z,piece_ends_2               ;
  LD A,(HL)               ; Mirrored: the first byte, changed left for right,
  XOR $02                 ; is the right end; the second (unless $16) the left
  LD (piece_extra_b),A    ; end.
  INC HL                  ;
  LD A,(HL)               ;
  CP $16                  ;
  JR Z,piece_ends_1       ;
  XOR $02                 ;
  LD (piece_extra_a),A    ;
  INC HL                  ;
  JR piece_ends_3         ;
piece_ends_1:
  LD (piece_extra_b),A    ; Mirrored, second byte $16: the right end is none.
  INC HL                  ;
  JR piece_ends_3         ;
piece_ends_2:
  LD A,(HL)               ; Not mirrored: left end, then right end.
  LD (piece_extra_a),A    ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (piece_extra_b),A    ;
  INC HL                  ;
piece_ends_3:
  LD A,(tile_upside_down) ; Upside down: both ends top for bottom, except $16.
  AND A                   ;
  JR Z,piece_ends_4       ;
  LD A,(piece_extra_a)    ;
  XOR $01                 ;
  LD (piece_extra_a),A    ;
  LD A,(piece_extra_b)    ;
  CP $16                  ;
  JR Z,piece_ends_4       ;
  XOR $01                 ;
  LD (piece_extra_b),A    ;
piece_ends_4:
  LD (next_piece),HL      ; Where the next piece starts.
  LD A,(tile_mirrored_left_right) ; Not mirrored, or mirrored with no right
  AND A                           ; end: done.
  RET Z                           ;
  LD A,(piece_extra_b)            ;
  CP $16                          ;
  RET Z                           ;
  LD A,(piece_page_x)     ; Reflect the x across the page.
  LD B,A                  ;
  LD A,(piece_step_x)     ;
  ADD A,B                 ;
  ADD A,$0F               ;
  CPL                     ;
  LD (piece_page_x),A     ;
  EXX                     ; Remap the direction through mirror_table and reload
  LD E,(IX+$03)           ; the steps.
  LD D,$00                ;
  LD HL,mirror_table      ;
  ADD HL,DE               ;
  LD A,(HL)               ;
  LD (IX+$03),A           ;
  CALL set_piece_shape    ;
  EXX                     ;
  LD A,(piece_rise)       ; A slope: done.
  AND A                   ;
  RET NZ                  ;
  LD B,C                  ; A flat run: back along by its length less one step.
  LD A,(piece_page_x)     ;
  DEC B                   ;
  RET Z                   ;
piece_ends_5:
  SUB $18                 ;
  DJNZ piece_ends_5       ;
  LD (piece_page_x),A     ;
  RET                     ;

; The left-right mirror table
;
; For a tile mirrored left-right ($FF, piece_ends): a piece direction to the
; direction of the same piece left for right (1 and 4, 2 and 5, 3 and 6 swap,
; 8-10 and 11-13 swap, the walls 14 and 15 swap; 0 and 7 stay).
mirror_table:
  DEFB $00,$04,$05,$06,$01,$02,$03,$07
  DEFB $0B,$0C,$0D,$08,$09,$0A,$0F,$0E

; The upside-down table
;
; For a tile turned upside down ($99, load_piece_steps): a direction to the
; same piece upside down (0-6 and 7-13 swap; the walls swap too), so floors
; become ceilings.
upside_down_table:
  DEFB $07,$08,$09,$0A,$0B,$0C,$0D,$00
  DEFB $01,$02,$03,$04,$05,$06,$0F,$0E

; The column-variety sequence
;
; Seventeen bytes, each 0, 1 or 2, walked one a column by the piece drawing
; (draw_landscape leaves HL here for each piece): which of three stretches of a
; piece's graphic each column uses (graphic_offsets), so that a long flat run
; is not one pattern repeated.
column_varieties:
  DEFB $00,$01,$00,$02,$02,$01,$00,$02,$01,$02,$00,$01,$02,$00,$01,$00,$02

; The piece's x step
;
; In pixels per step, signed (set_piece_shape from piece_shapes; cleared by
; fold_column_step). Also a scratch byte of the sprite code (draw_sprite_pair).
; Like the rest of the landscape's variables here, zero in the snapshot.
piece_dx:
  DEFB $00

; The piece's column step
;
; In screen bytes per step, signed (set_piece_shape; cleared by
; fold_column_step and for walls by colour_piece).
piece_dcolumn:
  DEFB $00

; The piece's rise
;
; In rows per step, 0 for a flat piece (set_piece_shape). Also written by the
; sprite code (draw_sprite_pair).
piece_rise:
  DEFB $00

; Not used
;
; Nothing refers to these two bytes (searched).
landscape_spare:
  DEFB $00,$00

; An object's box: x; or a list pointer
;
; The object's collision box made by place_collision_box is here and in the
; next three bytes: x, bottom line (box_bottom), height (box_height) and width
; (box_width). As a word it is also a list pointer: draw_frame walks the corner
; bitmaps with it.
box_x:
  DEFB $00

; An object's box: bottom line
;
; See box_x.
box_bottom:
  DEFB $00

; An object's box: height; the piece's y as read
;
; The box's height (place_collision_box); and, while the landscape is drawn,
; the current piece's y as read from its tile record before any change
; (draw_landscape), which colour_piece compares with $67.
box_height:
  DEFB $00

; An object's box: width
;
; See box_x.
box_width:
  DEFB $00

; Attribute rows to colour per step
;
; Worked out by colour_piece for the piece being coloured.
attribute_rows:
  DEFB $00

; The size of the vertical scroll
;
; scroll_down without its sign (scroll_landscape), used to uncover rows behind
; pieces (fill_behind_moving_piece).
vertical_step_size:
  DEFB $00

; Half the rows per step
;
; piece_rows halved (the code in landscape_step_loop), for the blitter.
half_rows:
  DEFB $00

; The horizontal origin
;
; Added to a page position to give the screen x; AND 7 is the fine scroll. With
; view_x it always sums to 8 (scroll_landscape). draw_landscape moves it on by
; $E0 for the right-hand pages and puts it back.
origin_x:
  DEFB $00

; The vertical origin
;
; Added to a page position to give the screen y. With view_y it sums to $B7:
; the scroll moves one up as the other comes down, and the wraps set both
; afresh. The exception is a new game's start, whose pair from start_position
; sums to $F7 until the fly-in's first vertical wrap. draw_landscape moves it
; on by $A0 for the lower pages and back.
origin_y:
  DEFB $00

; The piece's x
;
; The current piece's x in its page (draw_landscape), moved as it is drawn;
; also the start of a word used by place_collision_box.
piece_page_x:
  DEFB $00

; The piece's y
;
; The current piece's y in its page (draw_landscape), moved as it is drawn.
piece_page_y:
  DEFB $00

; The view's x in its page
;
; Moved by the horizontal scroll and wrapped into the next page by
; scroll_landscape; saved with the restart point (save_restart_point).
view_x:
  DEFB $00

; The view's y in its page
;
; Moved by the vertical scroll (scroll_landscape).
view_y:
  DEFB $00

; The piece's graphic
;
; Its address at this pass's shift, from shift_table's table (the code in
; landscape_step_loop).
piece_graphic:
  DEFW $0000

; The column being drawn
;
; The screen address of the piece's current column (landscape_step_loop,
; fold_column_step).
column_address:
  DEFW $0000

; Where to start in the graphic-offset table
;
; graphic_offsets, or one byte on when the scroll is shifted (draw_landscape).
offsets_start:
  DEFW $0000

; The next piece
;
; Its address in the tile record (piece_ends), picked up by draw_landscape's
; loop.
next_piece:
  DEFW $0000

; The piece's first step
;
; Its screen address, kept by step_screen_address.
first_step_address:
  DEFW $0000

; The piece's row of graphic offsets
;
; In graphic_offsets (the code in landscape_step_loop).
piece_offsets:
  DEFW $0000

; The piece's first step: y
;
; Kept by step_screen_address; read by after_step_drawn.
first_step_y:
  DEFB $00

; Columns at the view's edges
;
; Counted for each piece by column_clipped and after_step_drawn; cleared by
; draw_landscape.
clipped_columns:
  DEFB $00

; The first drawn step's place: x
;
; Kept by step_screen_address with first_step_y_drawn; the two are read as a
; word by fill_behind_moving_piece.
first_step_x:
  DEFB $00

; The first drawn step's place: y
;
; See first_step_x.
first_step_y_drawn:
  DEFB $00

; The view's page: column
;
; 0-$5F, wrapping (scroll_landscape, load_map_window); with view_row it is the
; word the restart point saves (save_restart_point).
view_column:
  DEFB $00

; The view's page: row
;
; 0-$1B: rows 0-3 are open sky (stars, draw_stars; no landscape below row 3),
; $0B is the surface's lowest, $0C on are the caverns.
view_row:
  DEFB $00

; The piece's x step again
;
; piece_dx kept by load_piece_steps: 0 for a wall.
piece_dx_copy:
  DEFB $00

; Rows per step
;
; piece_rise, or 4 for a flat piece (load_piece_steps); reused by colour_piece
; and after_step_drawn.
piece_rows:
  DEFB $00

; A column of a piece is cut off at the top or bottom of the view
;
; Used by the routine at landscape_step_loop.
;
; Jumped to from the column loop (landscape_step_loop) when a column lies
; across the view's top or bottom edge (edge_flags, the edge flags, bit 0 or 1,
; or any of bits 0-3 with others). In the upper tiles, a slope ends after this
; column (C=1). The column is counted in clipped_columns, and the first one so
; counted calls the code in step_screen_address that keeps the first step's
; place; then on to the next column.
column_clipped:
  LD A,(ground_cell)      ; Upper tile and a slope: stop after this column.
  BIT 0,A                 ;
  JR NZ,column_clipped_0  ;
  LD A,(piece_rise)       ;
  AND A                   ;
  JR Z,column_clipped_0   ;
  LD C,$01                ;
column_clipped_0:
  LD A,(clipped_columns)       ; Count it; the first one keeps the step's place
  INC A                        ; (step_screen_address).
  LD (clipped_columns),A       ;
  CP $01                       ;
  CALL Z,step_screen_address_2 ;
  JP after_step_drawn_2   ; Next column.

; A lower tile's column reaches the view's bottom edge
;
; Used by the routine at landscape_step_loop.
;
; From landscape_step_loop when, in one of the two lower tiles, the column's
; top is level with the view's edge (view_y). Bit 7 of the edge flags
; edge_flags is set; unless the vertical step scroll_down is negative, bit 0 is
; set instead and copied to piece_clip. On to the horizontal tests.
column_at_view_bottom:
  LD A,(edge_flags)           ; Flag it; a negative step: no more.
  OR $80                      ;
  LD (edge_flags),A           ;
  LD A,(scroll_down)          ;
  BIT 7,A                     ;
  JP NZ,landscape_step_loop_2 ;
  LD A,(edge_flags)        ; Otherwise bit 0, kept in piece_clip too.
  XOR $81                  ;
  LD (edge_flags),A        ;
  LD (piece_clip),A        ;
  JP landscape_step_loop_2 ;

; An upper tile's column reaches the view's top edge
;
; Used by the routine at landscape_step_loop.
;
; From landscape_step_loop when, in one of the two upper tiles, the column's y
; is the view's y (view_y). As column_at_view_bottom but bit 1, when the
; vertical step scroll_down is negative.
column_at_view_top:
  LD A,(edge_flags)          ; Flag it; a positive step: no more.
  OR $80                     ;
  LD (edge_flags),A          ;
  LD A,(scroll_down)         ;
  BIT 7,A                    ;
  JP Z,landscape_step_loop_2 ;
  LD A,(edge_flags)        ; Otherwise bit 1, kept in piece_clip too.
  XOR $82                  ;
  LD (edge_flags),A        ;
  LD (piece_clip),A        ;
  JP landscape_step_loop_2 ;

; A column reaches the view's left edge
;
; Used by the routine at landscape_step_loop.
;
; From the horizontal test in landscape_step_loop when, in one of the left-hand
; tiles, the column's x is the view's left edge (view_x). Moving left
; (scroll_across negative): bit 3 of the edge flags; otherwise the column is
; not drawn.
column_at_view_left:
  LD A,(scroll_across)    ; Not moving left: next column.
  BIT 7,A                 ;
  JP Z,after_step_drawn_2 ;
  LD A,(edge_flags)        ; Flag it and go on.
  OR $08                   ;
  LD (edge_flags),A        ;
  JP landscape_step_loop_5 ;

; A column reaches the view's right edge
;
; Used by the routine at landscape_step_loop.
;
; As column_at_view_left for the right-hand tiles: moving right (scroll_across
; positive or zero), bit 2 of the edge flags; otherwise the column is not
; drawn.
column_at_view_right:
  LD A,(scroll_across)     ; Moving left: next column.
  BIT 7,A                  ;
  JP NZ,after_step_drawn_2 ;
  LD A,(edge_flags)        ; Flag it and go on.
  OR $04                   ;
  LD (edge_flags),A        ;
  JP landscape_step_loop_5 ;

; A column on the view's left or right edge
;
; Used by the routine at landscape_step_loop.
;
; From landscape_step_loop when the edge flags hold just bit 2 or 3. For a
; slope going the way that matters for its tile (right in the upper tiles, left
; in the lower), the piece is cut to at most two more steps (C). The first such
; column of the piece takes its screen position (step_screen_address) and
; clears +1 of the piece state; every one is counted in piece_steps_done and
; clipped_columns by after_step_drawn and is not drawn here.
edge_column:
  LD A,(piece_dx_copy)    ; A slope going the right way: no more than two more
  AND A                   ; steps.
  JR Z,edge_column_2      ;
  LD A,(ground_cell)      ;
  BIT 1,A                 ;
  JR NZ,edge_column_1     ;
  LD A,(direction_flag)   ;
  CP $01                  ;
  JR NZ,edge_column_2     ;
edge_column_0:
  LD A,C                  ;
  CP $02                  ;
  JR C,edge_column_2      ;
  LD C,$02                ;
  JR edge_column_2        ;
edge_column_1:
  LD A,(direction_flag)
  CP $FF
  JR NZ,edge_column_2
  JR edge_column_0
edge_column_2:
  LD A,(piece_steps_done)  ; Not the first: count it.
  AND A                    ;
  JP NZ,after_step_drawn_1 ;
  EXX                      ; The first: its screen position; then count it.
  CALL step_screen_address ;
  XOR A                    ;
  LD (IX+$01),A            ;
  EXX                      ;
  JP after_step_drawn_1    ;

; Is the next attribute cell inside the play area?
;
; Used by the routines at colour_one_cell, colour_two_cells and
; colour_three_cells.
;
; Tests the attribute address one on from HL: inside rows 3 to 22 of the screen
; it returns NZ (A=$FF); in the panel's rows 0-2 or the bottom row 23, Z. HL is
; left as it was.
;
; HL An attribute address
in_play_area:
  INC HL                  ; In the top third: rows 3 and below are inside.
  LD A,H                  ;
  CP $58                  ;
  JR NZ,in_play_area_2    ;
  LD A,L                  ;
  AND $E0                 ;
  CP $60                  ;
  JR C,in_play_area_1     ;
in_play_area_0:
  DEC HL                  ; Inside.
  OR $FF                  ;
  RET                     ;
in_play_area_1:
  DEC HL                  ; Outside.
  XOR A                   ;
  RET                     ;
in_play_area_2:
  LD A,H                  ; The middle third is inside; in the bottom third,
  CP $5A                  ; row 23 is outside.
  JR NZ,in_play_area_0    ;
  LD A,L                  ;
  AND $E0                 ;
  CP $E0                  ;
  JR NC,in_play_area_1    ;
  JR in_play_area_0       ;

; Jump to IY
;
; Used by the routine at colour_piece.
;
; Lets colour_piece CALL the colouring routine whose address it put in IY.
jump_iy:
  JP (IY)                 ; Off to it.

; Colour the attribute cells under a landscape piece
;
; Used by the routine at after_step_drawn.
;
; Called by after_step_drawn after a piece is drawn. The landscape takes its
; colour from its attribute cells, a colour for the upper tiles
; (map_row_colour) and another for the lower ones (map_row_colour_next), both
; set from the level's pair of colours (start_landscape) by
; set_landscape_colours (the deeper colour from map row $0C down: the caverns).
; Where the surface meets the caverns (map row $0B in a lower tile, or $0C in
; an upper one) the deeper colour is used, or the upper one for a piece
; starting above y $67.
;
; For each step of the piece it colours a column of cells (the rows per step,
; attribute_rows) one, two or three cells wide (the column step plus one:
; colour_one_cell, colour_two_cells, colour_three_cells), then moves up by the
; rise and across by the column step; a flat piece colours three cells wide and
; as deep as its graphic (about two cells). Only cells inside the play area
; (in_play_area) and its columns 1-28 are coloured. A piece ending in the
; special end piece $13 on the right gets one more column when the scroll is
; not on a byte boundary.
;
; Nothing is coloured while the landscape is being built after a restart
; (map_rebuild), nor for a piece with no columns drawn.
;
; IX The piece state (current_piece): +0 +1 the screen x and y of its first
;    column, +2 the number of steps drawn, +3 the direction
colour_piece:
  LD A,(map_rebuild)      ; Building after a restart, or nothing drawn: no
  AND A                   ; colour.
  RET NZ                  ;
  LD A,(IX+$01)           ;
  AND A                   ;
  RET Z                   ;
  LD A,(IX+$02)           ;
  AND A                   ;
  RET Z                   ;
  LD A,(piece_dx_copy)    ; A wall: no column step, and one byte (two cells)
  AND A                   ; wide.
  JR NZ,colour_piece_0    ;
  LD (piece_dcolumn),A    ;
  INC A                   ;
  LD (piece_step_y),A     ;
colour_piece_0:
  LD A,(piece_step_y)     ; The width in cells.
  INC A                   ;
  LD C,A                  ;
  LD A,(direction_flag)   ; A piece going left starts a cell further left.
  CP $FF                  ;
  JR Z,colour_piece_1     ;
  LD A,(IX+$00)           ;
  SUB $02                 ;
  AND $F8                 ;
  LD (IX+$00),A           ;
colour_piece_1:
  LD A,C                   ; The colouring routine for the width.
  LD IY,colour_one_cell    ;
  CP $01                   ;
  JR Z,colour_piece_2      ;
  LD IY,colour_two_cells   ;
  CP $02                   ;
  JR Z,colour_piece_2      ;
  LD IY,colour_three_cells ;
colour_piece_2:
  LD A,(piece_rise)       ; The rise in cells per step; the screen position.
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD (piece_rows),A       ;
  LD L,(IX+$00)           ;
  LD H,(IX+$01)           ;
  LD A,(piece_rows)       ; Rows to colour: the rise in cells, one more if the
  LD (attribute_rows),A   ; first row does not end on a cell boundary.
  LD A,H                  ;
  AND $07                 ;
  XOR $07                 ;
  JR Z,colour_piece_3     ;
  LD A,(attribute_rows)   ;
  INC A                   ;
  LD (attribute_rows),A   ;
colour_piece_3:
  LD A,(IX+$03)           ; A flat piece (direction 0 or 7):
  AND A                   ;
  JR Z,colour_piece_4     ;
  CP $07                  ;
  JR NZ,colour_piece_5    ;
colour_piece_4:
  LD IY,colour_three_cells ; three cells wide, no rise, and the cells from its
  XOR A                    ; y down to $0B rows below.
  LD (piece_rows),A        ;
  LD A,H                   ;
  SUB $0B                  ;
  LD B,A                   ;
  LD A,H                   ;
  OR $07                   ;
  SUB B                    ;
  SRL A                    ;
  SRL A                    ;
  SRL A                    ;
  LD (attribute_rows),A    ;
colour_piece_5:
  LD B,L                  ; The attribute address (an x of $F0 or more, off the
  LD A,L                  ; right, wraps to the start of the row).
  CP $F0                  ;
  JR C,colour_piece_6     ;
  LD L,$00                ;
colour_piece_6:
  CALL pixel_to_attribute ;
  LD A,B                  ;
  CP $F0                  ;
  JR C,colour_piece_7     ;
  DEC HL                  ;
colour_piece_7:
  LD A,(map_row_colour)      ; The upper or lower colour.
  LD C,A                     ;
  LD A,(ground_cell)         ;
  BIT 0,A                    ;
  JR Z,colour_piece_8        ;
  LD A,(map_row_colour_next) ;
  LD C,A                     ;
colour_piece_8:
  LD A,(view_row)          ; Where the surface meets the caverns, the deeper
  CP $0B                   ; colour, or the upper one above y $67.
  JR Z,colour_piece_9      ;
  CP $0C                   ;
  JR NZ,colour_piece_11    ;
  LD A,(ground_cell)       ;
  BIT 0,A                  ;
  JR NZ,colour_piece_11    ;
  JR colour_piece_10       ;
colour_piece_9:
  LD A,(ground_cell)       ;
  BIT 0,A                  ;
  JR Z,colour_piece_11     ;
colour_piece_10:
  LD A,(ground_attr_below) ;
  LD C,A                   ;
  LD A,(box_height)        ;
  CP $67                   ;
  JR NC,colour_piece_11    ;
  LD A,(ground_attr_above) ;
  LD C,A                   ;
colour_piece_11:
  LD DE,$0020             ; Each step of the piece:
  LD B,(IX+$02)           ;
colour_piece_12:
  PUSH BC                 ; a column of cells,
  LD A,(attribute_rows)   ;
  LD B,A                  ;
  PUSH HL                 ;
  CALL jump_iy            ;
  POP HL                  ;
  LD A,(piece_rows)       ; up by the rise,
  AND A                   ;
  JR Z,colour_piece_14    ;
  LD B,A                  ;
colour_piece_13:
  AND A                   ;
  SBC HL,DE               ;
  DJNZ colour_piece_13    ;
colour_piece_14:
  LD A,(piece_dcolumn)    ; and across by the column step (signed).
  LD C,A                  ;
  LD B,$00                ;
  BIT 7,A                 ;
  JR Z,colour_piece_15    ;
  DEC B                   ;
colour_piece_15:
  ADD HL,BC               ;
  POP BC                  ;
  DJNZ colour_piece_12    ;
  LD A,(piece_extra_b)    ; The end piece $13 on the right, scrolled off a byte
  AND $7F                 ; boundary: one more column.
  CP $13                  ;
  RET NZ                  ;
  LD A,(origin_x)         ;
  AND $07                 ;
  RET Z                   ;
  LD A,(attribute_rows)   ;
  LD B,A                  ;

; Colour a column of attribute cells one wide
;
; colour_piece runs on into it for the extra column after an end piece $13, and
; enters it through IY for the narrowest pieces. Each row upwards: the cell at
; HL, if it is inside the play area (in_play_area) and its columns 1-28.
;
; B The number of rows
; C The attribute
; DE $0020
; HL The attribute address of the bottom row
colour_one_cell:
  CALL in_play_area       ; One cell a row, upwards.
  JR Z,colour_one_cell_0  ;
  LD A,L                  ;
  AND $1F                 ;
  DEC A                   ;
  CP $1C                  ;
  JR NC,colour_one_cell_0 ;
  LD (HL),C               ;
colour_one_cell_0:
  AND A                   ;
  SBC HL,DE               ;
  DJNZ colour_one_cell    ;
  RET                     ;

; Colour a column of attribute cells two wide
;
; Entered through IY from colour_piece for pieces with a column step of one
; byte (and for walls). Each row upwards: the two cells at HL, wherever they
; are inside the play area and its columns 1-28.
;
; B The number of rows
; C The attribute
; DE $0020
; HL The attribute address of the bottom row
colour_two_cells:
  CALL in_play_area        ; The first cell.
  JR Z,colour_two_cells_2  ;
  LD A,L                   ;
  AND $1F                  ;
  DEC A                    ;
  CP $1C                   ;
  JR NC,colour_two_cells_0 ;
  LD (HL),C                ;
colour_two_cells_0:
  INC HL                   ; The second.
  LD A,L                   ;
  AND $1F                  ;
  DEC A                    ;
  CP $1C                   ;
  JR NC,colour_two_cells_1 ;
  LD (HL),C                ;
colour_two_cells_1:
  DEC HL                   ;
colour_two_cells_2:
  AND A                   ; Up a row.
  SBC HL,DE               ;
  DJNZ colour_two_cells   ;
  RET                     ;

; Colour a column of attribute cells three wide
;
; As colour_two_cells for three cells: pieces with a column step of two bytes
; or more, and flat pieces.
;
; B The number of rows
; C The attribute
; DE $0020
; HL The attribute address of the bottom row
colour_three_cells:
  CALL in_play_area          ; The first cell.
  JR Z,colour_three_cells_3  ;
  LD A,L                     ;
  AND $1F                    ;
  DEC A                      ;
  CP $1C                     ;
  JR NC,colour_three_cells_0 ;
  LD (HL),C                  ;
colour_three_cells_0:
  INC HL                     ; The second.
  LD A,L                     ;
  AND $1F                    ;
  DEC A                      ;
  CP $1C                     ;
  JR NC,colour_three_cells_1 ;
  LD (HL),C                  ;
colour_three_cells_1:
  INC HL                     ; The third.
  LD A,L                     ;
  AND $1F                    ;
  DEC A                      ;
  CP $1C                     ;
  JR NC,colour_three_cells_2 ;
  LD (HL),C                  ;
colour_three_cells_2:
  DEC HL                     ;
  DEC HL                     ;
colour_three_cells_3:
  AND A                   ; Up a row.
  SBC HL,DE               ;
  DJNZ colour_three_cells ;
  RET                     ;

; Landscape pieces' collision classes
;
; Sixteen bytes, one per piece direction, put in +4 of each piece's collision
; box (draw_landscape) for the collision code (collide_with_landscape): 4 flat;
; 1, 2, 3 the three slopes rising to the right; $41-$43 the same rising to the
; left (bit 6); 8 flat upside down; $81-$83 and $C1-$C3 the slopes upside down
; (bit 7); $10 and $20 the two walls.
piece_classes:
  DEFB $04,$01,$02,$03,$41,$42,$43,$08
  DEFB $81,$82,$83,$C1,$C2,$C3,$10,$20

; Redraw the trail
;
; Set by the collision code (ship_contact, ship_hits_ground_objects,
; hit_slope); makes draw_trail erase and redraw the ship's trail even if its
; frame has not changed; cleared there.
trail_collision:
  DEFB $00

; Draw the landscape
;
; Used by the routine at scroll_landscape.
;
; Called each pass by scroll_landscape, when the view is below the sky (map row
; 3 or more). The screen shows at most four map tiles, each the size of the
; play area: the view's page, the one below, the one to the right and the one
; below that. load_map_window has listed their tile records' addresses after
; map_window_pointer, which points into the list, and their collision-box lists
; (37 bytes each) are at loader_entry, ground_cell_bottom_left,
; ground_cell_top_right and ground_cell_bottom_right; ground_cell says which of
; the four is being drawn (0 the view's page, 1 below, 2 right, 3 below right).
; Nothing is scrolled: every piece that can be seen is drawn again, straight
; onto the screen, from a copy of its graphic pre-shifted for this pass's fine
; scroll.
;
; First the shift table: the fine scroll (origin_x AND 7) picks one of four
; tables (at 0, 2, 4 or 6 pixels; shift_table), and the graphic-offset table's
; start (graphic_offsets, or one byte on when shifted). Then, for each tile:
; the page-to-screen offsets origin_x and origin_y are moved on to that tile
; ($E0 to the right, $A0 down, as 8-bit sums) and put back at the end (in
; after_step_drawn).
;
; A tile record (the tables from tile_records): first byte 0, an empty tile.
; Otherwise, optionally $99 (the tile is turned upside down: floors become
; ceilings), optionally $FF (mirrored left-right), optionally a byte of $95 or
; more (skipped: padding, so a mirrored or upside-down tile can point a few
; bytes before a plain record and share it), then the number of pieces. Each
; piece: x and y in the page (pixels; y counts down from the top of the page,
; and the piece's line rises from there), then a byte whose high nibble is its
; direction (load_piece_steps) and whose low nibble its number of steps (0
; meaning 16), and, only for a flat direction (0 or 7), the codes of its two
; end pieces (piece_ends). So a tile's ground is a chain of pieces, each a line
; of steps of one shape: a flat run of 24-pixel steps, a slope up or down, a
; wall.
;
; Each collision-box list: +0 the number of pieces, then up to seven 5-byte
; boxes, one per piece: +0 +1 x and y, +2 +3 for a flat piece its width (24 a
; step) and $14, for a wall $18 and its height (16 a step), for a slope its far
; end's x and y; +4 its class (piece_classes). These are refilled only while
; record_ground is set: when the view has moved into a new page this pass
; (scroll_landscape) or the screen is being built after a restart
; (map_rebuild); otherwise only the piece count is rewritten.
;
; Each piece is then tested against the view: a piece wholly outside it across
; (view_x, piece_off_left_edge for the left-hand tiles, piece_off_right_edge
; for the right) or up and down (view_y, piece_off_top_edge for the upper
; tiles, piece_off_bottom_edge for the lower) is not drawn. One that may be
; seen is drawn by the column loop at landscape_step_loop, its attributes
; coloured by colour_piece, and the next piece taken from next_piece. The
; renderer uses SP for its POP blitter and reloads it from the fixed address
; stack_top between pieces.
draw_landscape:
  LD A,(map_rebuild)      ; Building after a restart: refill the collision
  AND A                   ; records too.
  JR Z,draw_landscape_0   ;
  LD (record_ground),A    ;
draw_landscape_0:
  LD A,(origin_y)         ; The vertical offset for the first tile.
  ADD A,$60               ;
  LD (origin_y),A         ;
  LD DE,graphic_offsets   ; Start of the graphic-offset table for an unshifted
  LD (offsets_start),DE   ; scroll.
  EXX                         ; The fine scroll picks the shift table (and the
  LD HL,piece_blitters_shift0 ; shifted half of the offsets) for 0, 2, 4 or 6
  LD A,(origin_x)             ; pixels.
  AND $07                     ;
  JR Z,draw_landscape_1       ;
  EXX                         ;
  INC DE                      ;
  LD (offsets_start),DE       ;
  EXX                         ;
  LD HL,piece_blitters_shift2 ;
  CP $02                      ;
  JR Z,draw_landscape_1       ;
  LD HL,piece_blitters_shift4 ;
  CP $04                      ;
  JR Z,draw_landscape_1       ;
  LD HL,piece_blitters_shift6 ;
draw_landscape_1:
  LD (shift_table),HL         ;
  XOR A                   ; The first tile.
  LD (ground_cell),A      ;
; This entry point is used by the routine at after_step_drawn.
draw_landscape_2:
  LD IX,loader_entry             ; Each tile: its box list.
  LD A,(ground_cell)             ;
  AND A                          ;
  JR Z,draw_landscape_3          ;
  LD IX,ground_cell_bottom_left  ;
  CP $01                         ;
  JR Z,draw_landscape_3          ;
  LD IX,ground_cell_top_right    ;
  CP $02                         ;
  JR Z,draw_landscape_3          ;
  LD IX,ground_cell_bottom_right ;
draw_landscape_3:
  LD HL,(map_window_pointer) ; The next tile record's address from the list.
  LD E,(HL)                  ;
  INC HL                     ;
  LD D,(HL)                  ;
  INC HL                     ;
  LD (map_window_pointer),HL ;
  EX DE,HL                   ;
  XOR A                           ; Not mirrored, not upside down, no pieces
  LD (tile_mirrored_left_right),A ; yet.
  LD (tile_upside_down),A         ;
  LD (IX+$00),A                   ;
  LD A,(HL)                ; An empty tile: on to the next.
  AND A                    ;
  JP Z,after_step_drawn_17 ;
  CP $99                  ; $99: upside down.
  JR NZ,draw_landscape_4  ;
  LD (tile_upside_down),A ;
  INC HL                  ;
draw_landscape_4:
  LD A,(HL)                       ; $FF: mirrored left-right.
  CP $FF                          ;
  JR NZ,draw_landscape_5          ;
  LD (tile_mirrored_left_right),A ;
  INC HL                          ;
draw_landscape_5:
  LD A,(HL)               ; A byte from $95 up is skipped.
  CP $95                  ;
  JR C,draw_landscape_6   ;
  INC HL                  ;
  LD A,(HL)               ;
draw_landscape_6:
  LD (pieces_left),A       ; The number of pieces, to pieces_left and the box
  LD (IX+$00),A            ; list; its first box kept at collision_object.
  INC IX                   ;
  INC HL                   ;
  LD (collision_object),IX ;
; This entry point is used by the routine at after_step_drawn.
draw_landscape_7:
  LD IX,current_piece        ; Each piece: its state at current_piece, and the
  XOR A                      ; per-piece flags cleared.
  LD (piece_steps_done),A    ;
  LD (clipped_columns),A     ;
  LD (piece_clip),A          ;
  LD (piece_kept),A          ;
  LD (piece_extra_b),A       ;
  LD (piece_extra_a),A       ;
  LD (piece_ceiling),A       ;
  LD (piece_rows),A          ;
  LD (last_step_in_border),A ;
  LD (IX+$01),A              ;
  LD A,(HL)               ; Its x and y in the page (the y kept unchanged in
  LD (piece_page_x),A     ; box_height too).
  INC HL                  ;
  LD A,(HL)               ;
  LD (piece_page_y),A     ;
  LD (box_height),A       ;
  INC HL                  ;
  LD A,(HL)               ; Its direction (high nibble) and its steps
  RRCA                    ; (load_piece_steps).
  RRCA                    ;
  RRCA                    ;
  RRCA                    ;
  AND $0F                 ;
  LD (IX+$03),A           ;
  EXX                     ;
  CALL load_piece_steps   ;
  EXX                     ;
  LD A,(HL)               ; The number of steps; 0 means 16.
  AND $0F                 ;
  JR NZ,draw_landscape_8  ;
  LD A,$10                ;
draw_landscape_8:
  LD (piece_steps),A      ;
  LD C,A                  ;
  INC HL                  ; The end pieces, and a mirrored tile's changes
  CALL piece_ends         ; (piece_ends).
  LD HL,column_varieties  ; The column-variety sequence in HL; the direction
  LD B,(IX+$03)           ; kept in B.
  EXX                      ; The box, filled only while building
  LD IX,(collision_object) ; (record_ground).
  LD A,(record_ground)     ;
  AND A                    ;
  JP Z,draw_landscape_15   ;
  LD A,($DEDF)            ; Its class, steps, x and y.
  LD E,A                  ;
  LD D,$00                ;
  LD HL,piece_classes     ;
  ADD HL,DE               ;
  LD A,(HL)               ;
  LD (IX+$04),A           ;
  LD A,(piece_steps)      ;
  LD (IX+$02),A           ;
  LD A,(piece_page_x)     ;
  LD (IX+$00),A           ;
  LD A,(piece_page_y)     ;
  LD (IX+$01),A           ;
  LD A,(piece_rise)       ; A flat piece:
  AND A                   ;
  JR NZ,draw_landscape_10 ;
  LD B,(IX+$02)           ; width 24 pixels a step, height $14,
  XOR A                   ;
draw_landscape_9:
  ADD A,$18               ;
  DJNZ draw_landscape_9   ;
  LD (IX+$02),A           ;
  LD A,($DEDF)            ;
  CP $07                  ;
  LD (IX+$03),$14         ;
  JP Z,draw_landscape_15  ;
  LD A,(IX+$01)           ; and its y $10 lower unless it is upside down.
  ADD A,$10               ;
  LD (IX+$01),A           ;
  JP draw_landscape_15    ;
draw_landscape_10:
  LD A,(piece_dx_copy)    ; A wall:
  AND A                   ;
  JR NZ,draw_landscape_12 ;
  LD B,(IX+$02)           ; width $18, height 16 a step, and the left-going
  XOR A                   ; wall $10 to the left.
draw_landscape_11:
  ADD A,$10               ;
  DJNZ draw_landscape_11  ;
  LD B,A                  ;
  LD (IX+$02),$18         ;
  LD A,($DEDF)            ;
  LD (IX+$03),B           ;
  CP $0F                  ;
  JP NZ,draw_landscape_15 ;
  LD A,(IX+$00)           ;
  SUB $10                 ;
  LD (IX+$00),A           ;
  JP draw_landscape_15    ;
draw_landscape_12:
  LD A,(direction_flag)   ; A slope going left starts one x step further right.
  CP $FF                  ;
  JR Z,draw_landscape_13  ;
  LD A,(piece_step_x)     ;
  ADD A,(IX+$00)          ;
  LD (IX+$00),A           ;
draw_landscape_13:
  LD B,(IX+$02)           ; Its far end: the x and y after all its steps.
  LD D,(IX+$00)           ;
  LD E,(IX+$01)           ;
draw_landscape_14:
  LD A,(piece_dx)         ;
  ADD A,D                 ;
  LD D,A                  ;
  LD A,(piece_rise)       ;
  NEG                     ;
  ADD A,E                 ;
  LD E,A                  ;
  DJNZ draw_landscape_14  ;
  LD (IX+$03),E           ;
  LD (IX+$02),D           ;
draw_landscape_15:
  LD A,(view_x)             ; Wholly to the left or right of the view?
  LD B,A                    ;
  LD A,(ground_cell)        ;
  BIT 1,A                   ;
  JR Z,draw_landscape_16    ;
  CALL piece_off_right_edge ;
  JR draw_landscape_17      ;
draw_landscape_16:
  CALL piece_off_left_edge  ;
draw_landscape_17:
  JP NZ,draw_landscape_20   ;
  LD A,(view_y)              ; Wholly above or below it?
  LD B,A                     ;
  LD A,(ground_cell)         ;
  BIT 0,A                    ;
  JR Z,draw_landscape_18     ;
  CALL piece_off_bottom_edge ;
  JR draw_landscape_19       ;
draw_landscape_18:
  CALL piece_off_top_edge    ;
draw_landscape_19:
  JP NZ,draw_landscape_20    ;
  CALL next_piece_box      ; It can be seen: on to the next collision record,
  JP landscape_step_loop_0 ; and draw it.
draw_landscape_20:
  CALL next_piece_box     ; It cannot: on to the next collision record and the
  JP after_step_drawn_16  ; next piece.

; Test whether a piece in a left-hand map cell ends before the play area's left
; edge
;
; Used by the routine at draw_landscape.
;
; Used by draw_landscape for each piece of the two left-hand map cells of the
; 2x2 window (bit 1 of the cell counter ground_cell clear). It finds the
; piece's rightmost x within the cell from the piece's collision box and
; compares it with view_x, the in-cell x that sits at the play area's left
; edge. A piece that ends left of it cannot be seen and is skipped without
; walking its steps.
;
; The box (IX) is five bytes of a map cell's collision-box list (loader_entry,
; ground_cell_bottom_left, ground_cell_top_right or ground_cell_bottom_right:
; the view's page, the one below, right, below-right). It holds the piece's
; start x at +0 and, for a flat piece, its length in pixels at +2; for a sloped
; piece +2 holds the x of its far end, and +0 its start x (moved on by a step's
; width for a piece running left, direction_flag = 1). +4 is a class byte from
; piece_classes for the collision code (collide_with_landscape).
;
;   IX Piece's collision box
;   B In-cell x at the play area's left edge (view_x)
; O:F Z clear (NZ) if the piece is wholly left of the edge
piece_off_left_edge:
  LD A,(piece_rise)           ; Flat piece (no rise, piece_rise zero): right
  AND A                       ; end = start x + length.
  JR NZ,piece_off_left_edge_0 ;
  LD A,(IX+$00)               ;
  ADD A,(IX+$02)              ;
  JR piece_off_left_edge_3    ;
piece_off_left_edge_0:
  LD A,(piece_dx_copy)        ; A vertical wall (no run, piece_dx_copy zero) is
  AND A                       ; 8 pixels wide.
  JR NZ,piece_off_left_edge_1 ;
  LD A,(piece_page_x)         ;
  ADD A,$08                   ;
  JR piece_off_left_edge_3    ;
piece_off_left_edge_1:
  LD A,(direction_flag)      ; A sloped piece: its right end is +0 if it runs
  CP $01                     ; left (direction_flag = 1), else +2.
  JR Z,piece_off_left_edge_2 ;
  LD A,(IX+$02)              ;
  JR piece_off_left_edge_3   ;
piece_off_left_edge_2:
  LD A,(IX+$00)              ;
piece_off_left_edge_3:
  CP B                    ; NZ (skip) if the right end is left of the edge; Z
  RET C                   ; otherwise.
  XOR A                   ;
  RET                     ;

; Test whether a piece in a right-hand map cell starts beyond the play area's
; right edge
;
; Used by the routine at draw_landscape.
;
; The counterpart of piece_off_left_edge for the two right-hand map cells of
; the window. In the right-hand cell the play area's right edge falls at the
; same in-cell x as the left edge does in the left-hand one (view_x), so a
; piece whose leftmost x is beyond it is out of sight.
;
;   IX Piece's collision box
;   B In-cell x of the play area's right edge (view_x)
; O:F Z clear (NZ) if the piece is wholly right of the edge
piece_off_right_edge:
  LD A,(piece_dx_copy)        ; A sloped piece running left: its leftmost x is
  AND A                       ; its far end, +2, less one.
  JR Z,piece_off_right_edge_0 ;
  LD A,(direction_flag)       ;
  CP $FF                      ;
  JR Z,piece_off_right_edge_0 ;
  LD A,(IX+$02)               ;
  DEC A                       ;
  JR piece_off_right_edge_1   ;
piece_off_right_edge_0:
  LD A,(piece_page_x)     ; Otherwise the piece's start x.
piece_off_right_edge_1:
  CP B                    ; NZ if beyond the edge. (A piece starting exactly on
  RET NC                  ; the edge gives Z and is walked anyway.)
  XOR A                   ;
  RET                     ;

; Test whether a piece in an upper map cell lies wholly above the play area
;
; Used by the routine at draw_landscape.
;
; Pieces are walked upwards from their start, so in the upper map cells of the
; window a piece whose start (lowest) y is above the in-cell y of the play
; area's top edge cannot be seen.
;
;   B In-cell y of the play area's bottom edge in the lower cell, which is also
;     the top edge's in-cell y in the upper one (view_y)
; O:F Z clear (NZ) if the piece is wholly above the play area
piece_off_top_edge:
  LD A,(piece_page_y)
  CP B
  RET C
  XOR A
  RET

; Test whether a piece in a lower map cell lies wholly below the play area
;
; Used by the routine at draw_landscape.
;
; The counterpart of piece_off_top_edge for the lower map cells: the piece's
; topmost y is compared with view_y, the in-cell y of the play area's bottom
; row. A flat piece's top is its start y less 4 (a flat step is four rows
; tall); a vertical wall's is +1 less +3 of its collision box; a slope's is +3.
;
;   IX Piece's collision box
;   B In-cell y of the play area's bottom edge (view_y)
; O:F Z clear (NZ) if the piece is wholly below the play area
piece_off_bottom_edge:
  LD A,(piece_rise)             ; Flat piece.
  AND A                         ;
  JR NZ,piece_off_bottom_edge_0 ;
  LD A,(piece_page_y)           ;
  SUB $04                       ;
  JR piece_off_bottom_edge_2    ;
piece_off_bottom_edge_0:
  LD A,(piece_dx_copy)          ; Vertical wall.
  AND A                         ;
  JR NZ,piece_off_bottom_edge_1 ;
  LD A,(IX+$01)                 ;
  SUB (IX+$03)                  ;
  JR piece_off_bottom_edge_2    ;
piece_off_bottom_edge_1:
  LD A,(IX+$03)           ; Slope.
piece_off_bottom_edge_2:
  CP B                    ; NZ if below the bottom edge.
  RET NC                  ;
  XOR A                   ;
  RET                     ;

; Move to the next piece's collision box and back to the current-piece record
;
; Used by the routine at draw_landscape.
;
; Called by draw_landscape once a piece's visibility has been decided. Moves
; the pointer collision_object on by the five bytes of a box, points IX back at
; the current-piece record current_piece, clears its "first step set up" byte
; (+1) and stores the piece's direction at +3.
;
; IX Box just used
; B' Direction (in the other register set)
next_piece_box:
  LD DE,$0005              ; Advance the box pointer.
  ADD IX,DE                ;
  LD (collision_object),IX ;
  LD IX,current_piece     ; IX = current-piece record; switch register sets;
  EXX                     ; clear +1; keep the direction at +3.
  LD (IX+$01),$00         ;
  LD (IX+$03),B           ;
  RET                     ;

; Move to the next step of a piece, cull it, and set up its graphic and blitter
;
; Used by the routine at after_step_drawn.
;
; The per-step loop of the landscape renderer. A piece is a straight run of
; identical steps; each step moves the drawing position on by its direction's
; advance (piece_dx pixels across, piece_dcolumn bytes across, piece_rise
; pixels up, from the table at piece_shapes by set_piece_shape). Directions 0-6
; are floors -- flat, three slopes rising to the right, three rising to the
; left -- and 7-13 the same shapes as ceilings (rock above, sky below;
; set_piece_shape sets piece_ceiling to $80 for them); 14 and 15 are vertical
; walls. For each step this routine:
;
; 1. Advances the in-cell position (piece_page_x, piece_page_y) and the screen
; position (piece_x, piece_y). A piece's first step is entered further down,
; without moving. 2. Culls it against the play area. Vertically: in an upper
; map cell a step whose base is above the top edge is skipped; in a lower one,
; a step whose top is below the bottom edge; a step exactly on the edge sets a
; flag in edge_flags (column_at_view_top, column_at_view_bottom). Horizontally
; the same against view_x, with flags set by column_at_view_left and
; column_at_view_right. Skipped steps go to the step counter in
; after_step_drawn without drawing. 3. With an edge flag set, goes to
; edge_column or column_clipped (the edge-exact cases). 4. Otherwise reads the
; step's variant (0-2, from the repeating sequence at column_varieties) and,
; for the piece's first drawn step, looks its direction up in the fine-scroll
; table at shift_table (16 entries of four bytes: a blitter address into IY, a
; graphic address into piece_graphic), works out the screen address
; (step_screen_address) and finds this direction's row of variant offsets
; (piece_offsets). 5. Chooses how to draw: a flat step goes to flat_step_clip;
; a sloped step near the right edge (screen x from $D1) through the patched JR
; at the end of this routine to slope_step_at_right_edge; others to
; slope_step_at_left_edge, which clips at the left edge or draws whole. All of
; them end at draw_step, which draws the step.
;
; Interrupts are off for the whole game, which is what makes it safe for the
; blitters to borrow SP.
;
; HL' Address of this step's variant byte (in column_varieties)
; C' Steps left in the piece
; D' Steps drawn so far
landscape_step_loop:
  LD A,(piece_rise)       ; Move to this step: up by the direction's rise,
  LD B,A                  ; across by its run.
  LD A,(piece_page_y)     ;
  SUB B                   ;
  LD (piece_page_y),A     ;
  LD A,(piece_y)          ;
  SUB B                   ;
  LD (piece_y),A          ;
  LD A,(piece_dx)         ;
  LD B,A                  ;
  LD A,(piece_x)          ;
  ADD A,B                 ;
  LD (piece_x),A          ;
  LD A,(piece_page_x)     ;
  ADD A,B                 ;
  LD (piece_page_x),A     ;
; This entry point is used by the routine at draw_landscape.
landscape_step_loop_0:
  XOR A                   ; A piece's first step enters here (from
  LD (edge_flags),A       ; draw_landscape): no edge flags yet.
  LD A,(ground_cell)          ; Upper map cell: skip if the step's base is
  BIT 0,A                     ; above the top edge; exactly on it, flag it
  JR NZ,landscape_step_loop_1 ; (column_at_view_top).
  LD A,(piece_page_y)         ;
  LD B,A                      ;
  LD A,(view_y)               ;
  CP B                        ;
  JP Z,column_at_view_top     ;
  JP NC,after_step_drawn_2    ;
  JP landscape_step_loop_2    ;
landscape_step_loop_1:
  LD A,(piece_rows)          ; Lower map cell: skip if the step's top is below
  LD B,A                     ; the bottom edge; exactly on it, flag it
  LD A,(piece_page_y)        ; (column_at_view_bottom).
  SUB B                      ;
  LD B,A                     ;
  LD A,(view_y)              ;
  CP B                       ;
  JP C,after_step_drawn_2    ;
  JP Z,column_at_view_bottom ;
; This entry point is used by the routines at column_at_view_bottom and
; column_at_view_top.
landscape_step_loop_2:
  LD A,(piece_page_x)         ; Horizontal cull; the right-hand map cells are
  LD B,A                      ; tested further on.
  LD A,(ground_cell)          ;
  BIT 1,A                     ;
  JR NZ,landscape_step_loop_4 ;
  LD A,(piece_rise)           ; A flat step of a left-hand cell whose screen x
  AND A                       ; falls in $F1-$F6 (hanging off the left edge
  JR NZ,landscape_step_loop_3 ; into the border) is drawn only if it is the
  LD A,(origin_x)             ; piece's last, and then the flag
  ADD A,B                     ; last_step_in_border is set to $FF. Exactly $F0
  CP $F0                      ; is an edge case (column_at_view_left).
  JR C,landscape_step_loop_3  ;
  JP Z,column_at_view_left    ;
  CP $F7                      ;
  JR NC,landscape_step_loop_3 ;
  LD A,C                      ;
  DEC A                       ;
  JP NZ,after_step_drawn_2    ;
  DEC A                       ;
  LD (last_step_in_border),A  ;
landscape_step_loop_3:
  LD A,(piece_step_x)      ; Skip the step if it ends left of the left edge;
  ADD A,B                  ; exactly on it, column_at_view_left.
  LD B,A                   ;
  LD A,(view_x)            ;
  CP B                     ;
  JP Z,column_at_view_left ;
  JP NC,after_step_drawn_2 ;
  JR landscape_step_loop_5 ;
landscape_step_loop_4:
  LD A,(view_x)             ; Right-hand map cell: skip the step if it starts
  CP B                      ; right of the right edge; exactly on it,
  JP Z,column_at_view_right ; column_at_view_right.
  JP C,after_step_drawn_2   ;
; This entry point is used by the routines at column_at_view_left and
; column_at_view_right.
landscape_step_loop_5:
  LD A,(edge_flags)          ; An edge flag set: off to the edge-case handlers.
  AND A                      ;
  JR Z,landscape_step_loop_6 ;
  CP $08                     ;
  JP Z,edge_column           ;
  CP $04                     ;
  JP Z,edge_column           ;
  CP $01                     ;
  JP Z,column_clipped        ;
  CP $02                     ;
  JP Z,column_clipped        ;
  AND $0F                    ;
  JP NZ,column_clipped       ;
  JP after_step_drawn_2      ;
landscape_step_loop_6:
  LD E,(HL)               ; E = this step's variant (0, 1 or 2).
  LD A,(IX+$01)               ; Steps after the piece's first drawn one carry
  AND A                       ; on from where the last blitter left HL.
  JP NZ,landscape_step_loop_8 ;
  EXX                     ; First drawn step: from the fine-scroll table
  LD HL,(shift_table)     ; (shift_table + 4 * direction) the blitter address
  LD E,(IX+$03)           ; into IY and the graphic address into piece_graphic.
  LD D,$00                ;
  SLA E                   ;
  SLA E                   ;
  ADD HL,DE               ;
  LD C,(HL)               ;
  INC HL                  ;
  LD B,(HL)               ;
  INC HL                  ;
  PUSH BC                 ;
  POP IY                  ;
  LD C,(HL)               ;
  INC HL                  ;
  LD B,(HL)               ;
  LD (piece_graphic),BC   ;
  LD A,(piece_rows)       ; Row pairs to draw = the step's height (piece_rows)
  SRL A                   ; / 2.
  LD (half_rows),A        ;
  CALL step_screen_address ; Screen address of the step (step_screen_address),
  LD A,(piece_dcolumn)     ; less the byte advance, into column_address.
  LD B,A                   ;
  LD A,L                   ;
  SUB B                    ;
  LD L,A                   ;
  LD (column_address),HL   ;
  LD DE,(offsets_start)      ; piece_offsets = this direction's row of variant
  LD A,(IX+$03)              ; offsets: offsets_start (graphic_offsets on a
  CP $07                     ; byte boundary, one byte on off it) + 6 *
  JR C,landscape_step_loop_7 ; (direction mod 7).
  SUB $07                    ;
landscape_step_loop_7:
  ADD A,A                    ;
  LD H,A                     ;
  ADD A,A                    ;
  ADD A,H                    ;
  LD L,A                     ;
  LD H,$00                   ;
  ADD HL,DE                  ;
  LD (piece_offsets),HL      ;
  EXX                         ; D' = 0 steps drawn; the displacement of the JR
  XOR A                       ; at the end of slope_step_at_right_edge = 1 if
  LD D,A                      ; the step is on a byte boundary, else 0
  LD ($D480),A                ; (mirrored_slope_far_right copies it too).
  LD A,(IX+$00)               ;
  AND $07                     ;
  JR NZ,landscape_step_loop_8 ;
  LD A,$01                    ;
  LD ($D480),A                ;
landscape_step_loop_8:
  LD A,(piece_dx_copy)        ; A vertical wall: once a step is drawn the rest
  AND A                       ; go straight to the draw; before that piece_dx =
  JR NZ,landscape_step_loop_9 ; -8 * direction.
  LD A,D                      ;
  AND A                       ;
  JP NZ,draw_step             ;
  LD A,(direction_flag)       ;
  NEG                         ;
  ADD A,A                     ;
  ADD A,A                     ;
  ADD A,A                     ;
  LD (piece_dx),A             ;
landscape_step_loop_9:
  XOR A                   ; Default dispatch (the dispatch JR in draw_step):
  LD ($D702),A            ; the step's own blitter. Flat steps: flat_step_clip.
  LD A,(piece_rise)       ;
  AND A                   ;
  JP Z,flat_step_clip     ;
  LD A,(piece_x)               ; A sloped step: near the right edge (screen x
  CP $D1                       ; $D1 or more)?
  JP C,slope_step_at_left_edge ;
  LD B,A                  ; B = screen x, A = run in pixels.
  LD A,(piece_dx)         ;
landscape_step_loop_10:
  JR landscape_step_loop_10 ; Patched by set_piece_shape: displacement 0 for a
                            ; piece running left, into slope_step_at_right_edge
                            ; to negate the run first; 2 for one running right,
                            ; past the NEG.

; Decide how a sloped step near the right edge is clipped
;
; Entered from the patched JR at the end of landscape_step_loop with A = the
; step's run in pixels (negated here for a piece running left) and B its screen
; x. If the step's far side stays left of $E9 it goes on to
; slope_step_at_left_edge. Otherwise it crosses the play area's right edge and
; one of the right-clipping blitters is chosen: for a step starting left of $E0
; only its rightmost column is lost (slope_lose_last_column); for one starting
; at $E0 or later only its first column or two are drawn, the count patched
; into the blitter's JR (blit_right_clip by set_right_clip_columns, or
; blit_mirrored_right_clip by mirrored_slope_far_right).
;
; A Run of the step in pixels
; B Screen x of the step
slope_step_at_right_edge:
  NEG                          ; Far side left of $E9: no right clipping.
  ADD A,B                      ;
  CP $E9                       ;
  JP C,slope_step_at_left_edge ;
  LD A,B                      ; Starts left of $E0: slope_lose_last_column.
  CP $E0                      ;
  JR C,slope_lose_last_column ;
  LD A,(direction_flag)         ; A piece running left uses the mirrored
  CP $01                        ; blitter (mirrored_slope_far_right).
  JR Z,mirrored_slope_far_right ;
  LD A,(piece_dcolumn)    ; Displacement for the JR in blit_right_clip: $21
  LD B,A                  ; less the byte advance.
  LD A,$21                ;
  SUB B                   ;
slope_step_at_right_edge_0:
  JR slope_step_at_right_edge_0 ; Patched by landscape_step_loop (displacement
                                ; 1 on a byte boundary): into
                                ; set_right_clip_columns (one less) or past its
                                ; DEC A.

; Patch the right-clipping blitter's column count
;
; The target of the patched JR at the end of slope_step_at_right_edge, entered
; at its DEC A when the step is off a byte boundary and just after it when it
; is on one. Stores A as the displacement of the JR after the first write in
; blit_right_clip, selects that blitter through the dispatch JR in draw_step,
; and draws.
;
; A Displacement, plus one unless the DEC A is skipped
set_right_clip_columns:
  DEC A                   ; Off a byte boundary: one less.
  LD ($DB92),A            ; Patch the JR in blit_right_clip.
  LD A,$02                ; Dispatch 2: blit_right_clip (through
  LD ($D702),A            ; step_dispatch).
  LD A,(piece_dx_copy)    ; A vertical wall moves its address once and stops
  AND A                   ; stepping across (fold_column_step).
  CALL Z,fold_column_step ;
  JP draw_step            ; Draw.

; Clip a sloped step that loses only its rightmost column at the right edge
;
; Used by the routine at slope_step_at_right_edge.
;
; For a step starting left of $E0 that crosses the right edge. A piece running
; right uses blit_right_clip with its patched JR at displacement 0 (all but the
; last column); one running left goes to use_mirrored_right_clip.
slope_lose_last_column:
  LD A,(direction_flag)        ; Running left: the mirrored blitter.
  CP $01                       ;
  JR Z,use_mirrored_right_clip ;
  XOR A                   ; Displacement 0 into the JR in blit_right_clip;
  LD ($DB92),A            ; dispatch 2; draw.
  LD A,$02                ;
  LD ($D702),A            ;
  JP draw_step            ;

; Work out the column count for a mirrored step far into the right edge
;
; Used by the routine at slope_step_at_right_edge.
;
; A step of a piece running left that starts at screen x $E0 or more. The
; displacement for the patched JR in blit_mirrored_right_clip is $29 less the
; byte advance's size, one less again unless the step is on a byte boundary:
; the JR at the end of this routine (whose displacement is copied here from the
; byte-boundary flag landscape_step_loop left in the JR at the end of
; slope_step_at_right_edge) skips the DEC A at use_mirrored_right_clip or not.
mirrored_slope_far_right:
  LD A,($D480)            ; Copy the byte-boundary flag into the JR's
  LD ($D4B7),A            ; displacement.
  LD A,(piece_dcolumn)    ; A = $29 - size of the byte advance (negative for
  NEG                     ; these directions).
  LD B,A                  ;
  LD A,$29                ;
  SUB B                   ;
mirrored_slope_far_right_0:
  JR mirrored_slope_far_right_0 ; Patched: into use_mirrored_right_clip (DEC A)
                                ; or past it.

; Select the mirrored right-clipping blitter
;
; Used by the routine at slope_lose_last_column.
;
; Entered at its DEC A or just after it by the patched JR at the end of
; mirrored_slope_far_right, and from slope_lose_last_column with A = 1
; (displacement 0: all but the last column). Stores the displacement of the
; patched JR in blit_mirrored_right_clip and selects that blitter with dispatch
; displacement 5.
;
; A Column-count displacement, plus one
use_mirrored_right_clip:
  DEC A                   ; Patch the JR in blit_mirrored_right_clip.
  LD ($DC5A),A            ;
  LD A,$05                ; Dispatch 5: blit_mirrored_right_clip (through
  LD ($D702),A            ; step_dispatch).
  LD A,(piece_dx_copy)    ; A vertical wall moves its address once and stops
  AND A                   ; stepping across (fold_column_step); draw.
  CALL Z,fold_column_step ;
  JP draw_step            ;

; Draw a sloped step whole, or decide how it is clipped at the left edge
;
; Used by the routines at landscape_step_loop and slope_step_at_right_edge.
;
; Steps whose screen x is from -8 to 6 hang over the play area's left edge
; (column 0 is the border, and x from $F0 is "negative": columns 30 and 31 of
; the same character row). Anything else is drawn whole (with fold_column_step
; for a vertical wall). A clipped step uses blit_left_clip (dispatch 8) for a
; piece running right, or blit_mirrored_left_clip (dispatch 11) for one running
; left; the bytes to skip and the columns to draw are patched into those
; blitters' JRs, and for blit_left_clip the next step's column into
; move_across_kept, according to the column the step starts in.
;
; All the clipped cases ran in the build's sessions.
slope_step_at_left_edge:
  LD A,(piece_x)                 ; Not near the left edge: draw whole.
  ADD A,$08                      ;
  CP $0F                         ;
  JR C,slope_step_at_left_edge_0 ;
  LD A,(piece_dx_copy)           ;
  AND A                          ;
  JP NZ,draw_step                ;
  CALL fold_column_step          ;
  JP draw_step                   ;
slope_step_at_left_edge_0:
  LD A,(direction_flag)          ; A piece running left: further on.
  CP $01                         ;
  JR Z,slope_step_at_left_edge_6 ;
  LD A,(column_address)          ; Running right: by the column in
  AND $1F                        ; column_address ($1D, $1E or another).
  CP $1D                         ;
  JR Z,slope_step_at_left_edge_4 ;
  CP $1E                         ;
  JR Z,slope_step_at_left_edge_5 ;
slope_step_at_left_edge_1:
  LD A,(piece_dx_copy)    ; Another column: skip one graphic byte (first JR in
  AND A                   ; blit_left_clip at 1), draw the last column of a
  CALL Z,fold_column_step ; two-byte step (second JR at $2C), the next step's
  LD A,(piece_dcolumn)    ; column from the byte advance (move_across_kept);
  LD (move_across_kept),A ; dispatch 8.
  LD A,$2C                ;
  LD ($DBC6),A            ;
slope_step_at_left_edge_2:
  LD A,$01                ;
  LD ($DBC1),A            ;
slope_step_at_left_edge_3:
  LD A,$08                ;
  LD ($D702),A            ;
  JP draw_step            ;
slope_step_at_left_edge_4:
  LD A,$01                     ; $1D: skip two graphic bytes (first JR at 0),
  LD (move_across_kept),A      ; draw the last column of a three-byte step
  XOR A                        ; (second JR at $30); the next step's column
  LD ($DBC1),A                 ; from 1.
  LD A,$30                     ;
  LD ($DBC6),A                 ;
  JP slope_step_at_left_edge_3 ;
slope_step_at_left_edge_5:
  LD A,($D480)                   ; $1E off a byte boundary: skip one byte, draw
  CP $01                         ; the last two columns (second JR at 0).
  JR Z,slope_step_at_left_edge_1 ;
  LD A,(piece_dcolumn)           ;
  LD (move_across_kept),A        ;
  XOR A                          ;
  LD ($DBC6),A                   ;
  JP slope_step_at_left_edge_2   ;
slope_step_at_left_edge_6:
  LD A,(piece_dcolumn)           ; A piece running left: by the column the step
  LD B,A                         ; starts in.
  LD A,(column_address)          ;
  SUB B                          ;
  AND $1F                        ;
  CP $02                         ;
  JR Z,slope_step_at_left_edge_8 ;
  CP $03                         ; Another column off a byte boundary: skip one
  JR Z,slope_step_at_left_edge_9 ; byte, two columns (first JR in
  LD A,($D480)                   ; blit_mirrored_left_clip at 1, second at 0);
  CP $01                         ; dispatch 11.
  JR Z,slope_step_at_left_edge_8 ;
  XOR A                          ;
  LD ($DC14),A                   ;
  INC A                          ;
slope_step_at_left_edge_7:
  LD ($DC0C),A                   ;
  LD A,$0B                       ;
  LD ($D702),A                   ;
  JP draw_step                   ;
slope_step_at_left_edge_8:
  LD A,(piece_dx_copy)         ; Column 2, or on a byte boundary: skip one
  AND A                        ; byte, one column (second JR at $26).
  CALL Z,fold_column_step      ;
  LD A,$26                     ;
  LD ($DC14),A                 ;
  LD A,$01                     ;
  JP slope_step_at_left_edge_7 ;
slope_step_at_left_edge_9:
  LD A,D                           ; Column 3: skip two bytes, one column
  AND A                            ; (first JR at 0, second at $2A); with no
  JR NZ,slope_step_at_left_edge_10 ; step drawn yet, column_address goes a
  LD A,(column_address)            ; character row back.
  SUB $20                          ;
  LD (column_address),A            ;
slope_step_at_left_edge_10:
  LD A,$2A                         ;
  LD ($DC14),A                     ;
  XOR A                            ;
  JP slope_step_at_left_edge_7     ;

; Patch the flat-step blitter for the play area's edges and the piece's end
; caps
;
; Used by the routine at landscape_step_loop.
;
; Flat steps (directions 0 and 7) are three bytes wide and four rows tall, all
; drawn by blit_flat_step, which has three patchable JRs in its loop and two on
; its cut path (flat_blit_left_edge, flat_blit_left_rows). This routine resets
; the loop's three to draw all three columns, then patches them by the column
; the step starts in (the low five bits of column_address are three less than
; that column):
;
; * $1D (start in column 0, the left border): the first JR to
;   flat_blit_left_edge, which skips the first byte and draws columns 1 and 2
;   (flat_blit_left_two_rows).
; * $1C (start in column 31, one column left of the screen): the same, drawing
;   only the third byte, in column 1 (flat_blit_left_one_row).
; * $18 (columns 27-29): the third JR to flat_blit_left_two, two columns.
; * $19 (columns 28-30): the second JR to flat_blit_one_column, one column.
;
; Then the piece's end caps. A flat piece carries two cap bytes (piece_extra_a
; for its first step, piece_extra_b for its last, read by piece_ends); bit 4
; marks a cap to act on, and bit 7 is set here when it is acted on
; (landscape_end_pieces looks for $91 and $93). A cap's low two bits pick one
; of four extra graphics stored after the three variants (offset $24 + 12 * n),
; drawn in place of the variant. Cap $16 ($96 once seen) draws just the first
; column, and skips the step if that column is outside. An odd cap marks a drop
; at that end of the run: if the step is on a byte boundary and the horizontal
; scroll is moving towards that end, its address and x are kept
; (first_step_address, piece_kept, scratch_count) for the column fill after the
; piece (fill_flat_end_column).
;
; The first step of a piece whose start cap is exactly $10 loses its first
; column (the first JR to flat_blit_lose_first) when the ground is off a byte
; boundary.
;
; C' Steps left (equal to piece_steps on the piece's first step, 1 on its last)
flat_step_clip:
  XOR A                   ; All three loop JRs to displacement 0: draw every
  LD ($E25A),A            ; column.
  LD ($E25E),A            ;
  LD ($E262),A            ;
  LD A,(column_address)   ; The step's start column.
  AND $1F                 ;
  CP $1D                  ;
  JR Z,flat_step_clip_0   ;
  CP $1C                  ;
  JR Z,flat_step_clip_2   ;
  CP $18                  ;
  JR Z,flat_step_clip_3   ;
  CP $19                  ;
  JR Z,flat_step_clip_4   ;
  LD A,(last_step_in_border) ; A last step hanging into the border
  AND A                      ; (last_step_in_border set) is not drawn.
  JP NZ,after_step_drawn_1   ;
  LD A,(piece_steps)      ; First step, start cap $10, off a byte boundary:
  CP C                    ; lose the first column.
  JR NZ,flat_step_clip_5  ;
  LD A,(piece_extra_a)    ;
  CP $10                  ;
  JR NZ,flat_step_clip_5  ;
  LD A,(origin_x)         ;
  AND $07                 ;
  JR Z,flat_step_clip_5   ;
  LD A,$EE                ;
  JR flat_step_clip_1     ;
flat_step_clip_0:
  LD A,$33                ; Start column 0: from the second byte on.
flat_step_clip_1:
  LD ($E25A),A            ;
  XOR A                   ;
  LD ($E2AA),A            ;
  INC A                   ;
  LD ($E2A5),A            ;
  JR flat_step_clip_5     ;
flat_step_clip_2:
  LD A,$33                ; Start column 31: only the third byte.
  LD ($E25A),A            ;
  LD A,$0B                ;
  LD ($E2AA),A            ;
  XOR A                   ;
  LD ($E2A5),A            ;
  JR flat_step_clip_5     ;
flat_step_clip_3:
  LD A,$26                ; Start column 27: two columns.
  LD ($E262),A            ;
  JR flat_step_clip_5     ;
flat_step_clip_4:
  LD A,$24                ; Start column 28: one column.
  LD ($E25E),A            ;
flat_step_clip_5:
  LD A,(piece_steps)      ; First step with a start cap to act on: mark it.
  CP C                    ;
  JP NZ,flat_step_clip_10 ;
  LD A,(piece_extra_a)    ;
  BIT 4,A                 ;
  JP Z,draw_step          ;
  OR $80                  ;
  LD (piece_extra_a),A    ;
flat_step_clip_6:
  LD B,A                     ; An odd cap (a drop) facing the scroll's
  CP $96                     ; direction, on a byte boundary: keep the step's
  JR Z,flat_step_clip_8      ; address and screen x for fill_flat_end_column.
  BIT 0,A                    ;
  JR Z,flat_step_clip_9      ;
  CP $91                     ;
  LD A,(scroll_across)       ;
  JR Z,flat_step_clip_7      ;
  NEG                        ;
flat_step_clip_7:
  BIT 7,A                    ;
  JR NZ,flat_step_clip_9     ;
  LD A,(IX+$00)              ;
  AND $07                    ;
  JR NZ,flat_step_clip_9     ;
  LD A,B                     ;
  LD (piece_kept),A          ;
  EXX                        ;
  LD HL,(column_address)     ;
  LD (first_step_address),HL ;
  LD A,(piece_x)             ;
  LD (scratch_count),A       ;
  EXX                        ;
  JR flat_step_clip_9        ;
flat_step_clip_8:
  LD A,(column_address)    ; Cap $16: one column only, or nothing if that
  INC A                    ; column is outside.
  INC A                    ;
  AND $1F                  ;
  CP $1C                   ;
  JP NC,after_step_drawn_2 ;
  XOR A                    ;
  LD ($E25A),A             ;
  LD ($E262),A             ;
  LD A,$24                 ;
  LD ($E25E),A             ;
flat_step_clip_9:
  LD A,B                  ; Graphic offset of the cap: $24 + 12 * (cap AND 3),
  AND $03                 ; and on to the draw.
  ADD A,A                 ;
  ADD A,A                 ;
  LD B,A                  ;
  ADD A,A                 ;
  ADD A,B                 ;
  ADD A,$24               ;
  EXX                     ;
  LD E,A                  ;
  LD D,$00                ;
  JR draw_step_0          ;
flat_step_clip_10:
  LD A,C                  ; Last step with an end cap to act on: mark it and
  CP $01                  ; treat it the same way.
  JR NZ,draw_step         ;
  LD A,(piece_extra_b)    ;
  BIT 4,A                 ;
  JR Z,draw_step          ;
  OR $80                  ;
  LD (piece_extra_b),A    ;
  JP flat_step_clip_6     ;

; Draw a landscape step
;
; Used by the routines at landscape_step_loop, set_right_clip_columns,
; slope_lose_last_column, use_mirrored_right_clip, slope_step_at_left_edge and
; flat_step_clip.
;
; The draw shared by every direction, reached from landscape_step_loop,
; set_right_clip_columns, slope_lose_last_column, use_mirrored_right_clip,
; slope_step_at_left_edge and flat_step_clip (which jumps into it past the
; first lines with DE already holding a cap's graphic offset). It turns the
; variant into a graphic address and loads SP with it. If the step hangs below
; the play area (piece_y from $B8) it pops the rows below the edge away and
; starts at the bottom row; if it reaches above the top it shortens the row
; count (and, for a slope, cuts the piece short, since pieces only climb). Then
; the JR at the end, whose displacement was set to 0, 2, 5, 8 or 11 (0 by
; landscape_step_loop, the others by the clipping set-ups), lands on one of the
; five jumps at step_dispatch: the step's own blitter (IY) or one of the four
; edge-clipping blitters.
;
; Every blitter is entered with A = row pairs to draw, HL = the screen address
; less the step's byte advance, SP = the first graphic byte, and ends by
; jumping to after_step_drawn with HL where the next step will start.
;
; E Variant (0-2)
; HL' Screen address of the step, less the byte advance (column_address)
draw_step:
  LD A,E                  ; Normal graphic: the offset from this direction's
  EXX                     ; variant row (piece_offsets + 2 * variant).
  LD HL,(piece_offsets)   ;
  LD E,A                  ;
  SLA E                   ;
  LD D,$00                ;
  ADD HL,DE               ;
  LD E,(HL)               ;
; This entry point is used by the routine at flat_step_clip.
draw_step_0:
  LD HL,(piece_graphic)   ; SP = the step's graphic. (A cap's offset joins
  ADD HL,DE               ; here.)
  LD SP,HL                ;
  LD A,(half_rows)        ; E = row pairs. Does the step hang below the play
  LD E,A                  ; area (piece_y from $B8)?
  LD A,(piece_y)          ;
  CP $B8                  ;
  JP C,draw_step_3        ;
  SUB $B7                 ; It does: lose half the overhang in row pairs, and
  LD B,A                  ; pop those rows of the graphic away (D POPs a row
  SRL B                   ; pair: the byte advance, plus one for a sloped step
  LD A,E                  ; off a byte boundary).
  SUB B                   ;
  LD E,A                  ;
  LD A,(piece_step_y)     ;
  LD D,A                  ;
  LD A,(piece_rise)       ;
  AND A                   ;
  JR Z,draw_step_1        ;
  LD A,(IX+$00)           ;
  AND $07                 ;
  JR Z,draw_step_1        ;
  INC D                   ;
draw_step_1:
  LD A,D                  ;
draw_step_2:
  POP HL                  ;
  DEC A                   ;
  JR NZ,draw_step_2       ;
  DJNZ draw_step_1        ;
  LD H,$57                ; Start at the play area's bottom row (pixel row $B7)
  LD A,(column_address)   ; in the step's column.
  LD L,A                  ;
  LD A,(piece_dcolumn)    ;
  ADD A,L                 ;
  AND $1F                 ;
  OR $C0                  ;
  LD L,A                  ;
  LD A,(piece_dcolumn)    ;
  NEG                     ;
  ADD A,L                 ;
  LD L,A                  ;
  LD A,($D702)            ; For the mirrored left-clipping blitter a column
  CP $0B                  ; past 28 belongs to the character row above.
  JP NZ,draw_step_6       ;
  LD A,L                  ;
  CP $DD                  ;
  JP C,draw_step_6        ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JP draw_step_6          ;
draw_step_3:
  LD B,A                  ; Not below: does the step reach above the top row
  LD A,(piece_rows)       ; ($18)?
  NEG                     ;
  ADD A,B                 ;
  SUB $18                 ;
  JP NC,draw_step_5       ;
  CPL                     ; It does: shorten it; a slope's piece is cut to at
  SRL A                   ; most one more step (C' = 2 unless already 1).
  NEG                     ;
  ADD A,E                 ;
  LD E,A                  ;
  LD A,(piece_rise)       ;
  AND A                   ;
  JR Z,draw_step_5        ;
  EXX                     ;
  LD A,C                  ;
  CP $01                  ;
  JR Z,draw_step_4        ;
  LD C,$02                ;
draw_step_4:
  EXX                     ;
draw_step_5:
  LD HL,(column_address)  ; HL = the screen address kept for this step.
draw_step_6:
  LD A,E                  ; A = row pairs to draw.
draw_step_7:
  JR draw_step_7          ; Patched dispatch: displacement 0, 2, 5, 8 or 11
                          ; lands on one of the five jumps that follow.

; The five jumps of the step dispatch
;
; Reached through the patched JR at the end of draw_step: displacement 0 lands
; on the first, 2 on the second, and so on.
step_dispatch:
  JP (IY)                 ; 0: the step's own blitter, from the fine-scroll
                          ; table.
  JP blit_right_clip      ; 2: blit_right_clip, right edge, piece running
                          ; right.
  JP blit_mirrored_right_clip ; 5: blit_mirrored_right_clip, right edge, piece
                              ; running left.
  JP blit_left_clip       ; 8: blit_left_clip, left edge, piece running right.
  JP blit_mirrored_left_clip ; 11: blit_mirrored_left_clip, left edge, piece
                             ; running left.

; Finish a step; at a piece's end repair behind it and colour it; then the next
; piece and map cell
;
; Used by the routines at blit_right_clip, blit_left_clip,
; blit_mirrored_left_clip, blit_mirrored_right_clip, blit_one, blit_three,
; blit_two, blit_mirror_three, blit_mirror_two and blit_mirror_one.
;
; Every step blitter ends with JP after_step_drawn (HL = where the next step
; starts); the flat-step blitter blit_flat_step stores column_address itself
; and comes back to the second instruction. edge_column and column_clipped come
; in further down, at the step counters, for steps handled at the edges.
;
; When a piece's last step is done the stack is put back at stack_top. That
; address is hard-wired: the landscape is always reached by the play loop's
; CALL ship_pass (2 bytes), its PUSH IY (2) and the CALL draw_landscape in
; scroll_landscape (2), below the game's stack top; the fly-in in
; start_landscape reaches it at the same depth (its CALL, a PUSH BC, the CALL).
;
; Then what lies behind the moving landscape is repaired. The screen is never
; cleared: the sky is black and the inside of the ground is a $55/$AA checker,
; and both look the same after a move of two pixels either way, which is why
; the landscape scrolls in steps of two. Only the strips a moving edge uncovers
; need painting. Across: when the fine horizontal position comes back to a byte
; boundary each step has just moved into the next byte column, leaving its old
; one behind; a sloped piece gets that column filled step by step by
; fill_under_slope, with the checker ($55AA) or black (0) chosen by the
; scroll's direction, the piece's direction and whether it is a ceiling
; (piece_ceiling); a flat piece's drop ends are done by fill_flat_end_column.
; Up and down: if the landscape moved vertically (scroll_down),
; fill_behind_moving_piece fills the rows behind every drawn step. Then
; colour_piece colours the piece.
;
; Then the next piece of this map cell begins (back in draw_landscape); when
; the cell's pieces are done, the next of the four map cells (ground_cell 0 to
; 3: upper-left, lower-left, upper-right, lower-right) follows, with the
; drawing origin moved down a cell (origin_y + $A0) or back up (origin_y + $60,
; i.e. - $A0), and right a cell (origin_x - $20, i.e. + $E0) for the right-hand
; pair. After the fourth, origin_x is put back and the landscape is done.
; draw_landscape jumps to both of those places too.
;
; HL Screen address where the next step starts
after_step_drawn:
  LD (column_address),HL  ; Keep it.
; This entry point is used by the routine at flat_blit_upper_row.
after_step_drawn_0:
  EXX                     ; Count the step as drawn (D'). The flat-step blitter
  INC D                   ; returns here.
; This entry point is used by the routines at edge_column and flat_step_clip.
after_step_drawn_1:
  LD A,(piece_steps_done) ; Count it in piece_steps_done and clipped_columns
  INC A                   ; too.
  LD (piece_steps_done),A ;
  LD A,(clipped_columns)  ;
  INC A                   ;
  LD (clipped_columns),A  ;
; This entry point is used by the routines at column_clipped,
; column_at_view_left, column_at_view_right, landscape_step_loop and
; flat_step_clip.
after_step_drawn_2:
  INC HL                    ; Next step: its variant byte, one fewer to go.
  DEC C                     ;
  JP NZ,landscape_step_loop ;
  LD (IX+$02),D           ; Steps drawn, into the current-piece record.
  LD SP,stack_top         ; Take the stack back from the blitters: always
                          ; stack_top, the depth the landscape is called at.
  CALL draw_flat_end_lip  ; A flat piece's end lip (draw_flat_end_lip).
  LD A,(piece_steps_done)   ; Column fill only if steps were drawn and the fine
  AND A                     ; horizontal position is on a byte boundary.
  JP Z,after_step_drawn_14  ;
  LD A,(origin_x)           ;
  AND $07                   ;
  JP NZ,after_step_drawn_14 ;
  LD (touch_depth),A       ; No horizontal scroll: a flat piece needs nothing;
  LD A,(scroll_across)     ; a sloped one is filled anyway, with a scroll
  AND A                    ; direction made up from its own direction
  JP NZ,after_step_drawn_4 ; (direction_flag, flipped by the ceiling flag
  LD A,(piece_rise)        ; piece_ceiling for a slope) and taken away
  AND A                    ; afterwards (touch_depth says to).
  JP Z,after_step_drawn_14 ;
  LD (touch_depth),A       ;
  LD A,(direction_flag)    ;
  LD B,A                   ;
  LD A,(piece_dx_copy)     ;
  AND A                    ;
  JR Z,after_step_drawn_3  ;
  LD A,(piece_ceiling)     ;
after_step_drawn_3:
  XOR B                    ;
  LD (scroll_across),A     ;
after_step_drawn_4:
  LD A,(piece_rise)         ; Flat piece: fill_flat_end_column.
  AND A                     ;
  JP Z,fill_flat_end_column ;
  EXX                        ; Start from the piece's first step
  LD HL,(first_step_address) ; (first_step_address, its y from first_step_y). A
  LD A,(first_step_y)        ; vertical wall is not a ceiling here, and moves a
  LD (piece_page_y),A        ; byte against its direction.
  LD A,(piece_dx_copy)       ;
  AND A                      ;
  JR NZ,after_step_drawn_5   ;
  LD (piece_ceiling),A       ;
  LD A,(direction_flag)      ;
  NEG                        ;
  LD (piece_dcolumn),A       ;
after_step_drawn_5:
  LD DE,$55AA             ; DE = the checker, or black if scroll direction,
  LD A,(scroll_across)    ; ceiling flag and piece direction say the uncovered
  LD B,A                  ; side is sky.
  LD A,(piece_ceiling)    ;
  XOR B                   ;
  LD B,A                  ;
  LD A,(direction_flag)   ;
  XOR B                   ;
  BIT 7,A                 ;
  JR Z,after_step_drawn_6 ;
  LD DE,$0000             ;
after_step_drawn_6:
  LD A,(direction_flag)     ; The column to fill: just left of the piece, or
  CP $FF                    ; past its steps, by direction.
  JR Z,after_step_drawn_8   ;
  LD A,(scroll_across)      ;
  BIT 7,A                   ;
  JR NZ,after_step_drawn_7  ;
  DEC L                     ;
  JP after_step_drawn_11    ;
after_step_drawn_7:
  LD A,(piece_step_y)       ;
  ADD A,L                   ;
  LD L,A                    ;
  JP after_step_drawn_9     ;
after_step_drawn_8:
  LD A,(scroll_across)      ;
  BIT 7,A                   ;
  JR NZ,after_step_drawn_10 ;
  DEC L                     ;
after_step_drawn_9:
  JP after_step_drawn_11    ;
after_step_drawn_10:
  LD A,(piece_dcolumn)      ;
  ADD A,L                   ;
  LD L,A                    ;
after_step_drawn_11:
  LD A,(piece_dx_copy)      ; A wall is filled as one column, 16 rows a step
  AND A                     ; tall.
  JR NZ,after_step_drawn_13 ;
  LD A,(piece_steps_done)   ;
  LD B,A                    ;
  XOR A                     ;
after_step_drawn_12:
  ADD A,$10                 ;
  DJNZ after_step_drawn_12  ;
  LD (piece_rows),A         ;
  LD A,$01                  ;
  LD (piece_steps_done),A   ;
after_step_drawn_13:
  LD A,(piece_rows)       ; C = row pairs a step, B = steps: fill
  SRL A                   ; (fill_under_slope).
  LD C,A                  ;
  EXX                     ;
  LD A,(piece_steps_done) ;
  LD B,A                  ;
  CALL fill_under_slope   ;
  LD A,(touch_depth)       ; Take away a made-up scroll direction.
  AND A                    ;
  JR Z,after_step_drawn_14 ;
  XOR A                    ;
  LD (scroll_across),A     ;
; This entry point is used by the routine at fill_flat_end_column.
after_step_drawn_14:
  EXX                            ; Vertical scroll this pass and steps drawn:
  LD A,(scroll_down)             ; fill the rows behind
  AND A                          ; (fill_behind_moving_piece).
  JR Z,after_step_drawn_15       ;
  LD A,(clipped_columns)         ;
  AND A                          ;
  JP NZ,fill_behind_moving_piece ;
; This entry point is used by the routine at fill_behind_moving_piece.
after_step_drawn_15:
  CALL colour_piece       ; Colour the piece (colour_piece).
  EXX                     ;
; This entry point is used by the routine at draw_landscape.
after_step_drawn_16:
  LD SP,stack_top         ; Stack back to stack_top; the next piece, if any are
  LD HL,(next_piece)      ; left in this map cell (pieces_left). draw_landscape
  LD A,(pieces_left)      ; jumps here for a piece it skips.
  DEC A                   ;
  LD (pieces_left),A      ;
  JP NZ,draw_landscape_7  ;
; This entry point is used by the routine at draw_landscape.
after_step_drawn_17:
  LD A,(ground_cell)       ; Next map cell of the window. draw_landscape jumps
  INC A                    ; here for an empty cell.
  LD (ground_cell),A       ;
  CP $04                   ;
  JR Z,after_step_drawn_19 ;
  BIT 0,A                  ; Origin down a cell for a lower one, back up for an
  LD B,$60                 ; upper one.
  JR Z,after_step_drawn_18 ;
  LD B,$A0                 ;
after_step_drawn_18:
  LD A,(origin_y)          ;
  ADD A,B                  ;
  LD (origin_y),A          ;
  LD A,(ground_cell)      ; Origin right a cell for the right-hand pair.
  CP $02                  ;
  JP NZ,draw_landscape_2  ;
  LD B,$20                ;
  LD A,(origin_x)         ;
  SUB B                   ;
  LD (origin_x),A         ;
  JP draw_landscape_2     ;
after_step_drawn_19:
  LD A,(origin_x)         ; Done: horizontal origin put back.
  ADD A,$20               ;
  LD (origin_x),A         ;
  RET                     ;

; End-lip graphics for flat floor pieces
;
; Used by draw_flat_end_lip. Three lips, four rows tall and one byte wide, one
; for each fine horizontal position 2, 4 and 6 (none is needed on a byte
; boundary), each listed from the bottom row up. The ceiling pieces' lips
; follow at ceiling_lip_bytes.
floor_lip_bytes:
  DEFB $40,$C0,$00,$00    ; Fine positions 2, 4, 6
  DEFB $50,$B0,$C0,$80    ;
  DEFB $54,$AC,$F0,$E0    ;

; Last flat step hangs into the border
;
; $FF when a flat piece's last step hangs off the left edge into the border
; (set by landscape_step_loop, cleared for each piece by draw_landscape); 0
; otherwise. It makes flat_step_clip skip that step, draw_flat_end_lip draw the
; piece's lip a character row up, and fill_behind_moving_piece treat the
; piece's fill differently.
last_step_in_border:
  DEFB $00

; End-lip graphics for flat ceiling pieces
;
; As floor_lip_bytes, for direction 7.
ceiling_lip_bytes:
  DEFB $00,$00,$C0,$C0    ; Fine positions 2, 4, 6
  DEFB $00,$C0,$70,$B0    ;
  DEFB $00,$F0,$5C,$AC    ;

; Draw the lip at the end of a flat piece whose end cap is $13
;
; Used by the routine at after_step_drawn.
;
; A flat run ending in cap $13 ($93 once seen) gets a lip one byte wide and
; four rows tall in the byte column after its last step, from floor_lip_bytes
; (floor) or ceiling_lip_bytes (ceiling) for the current fine horizontal
; position. Nothing is drawn on a byte boundary or outside the play area's
; columns, and the lip is cut to two rows at the play area's bottom (piece_y =
; $B9) or top (piece_y = $19).
draw_flat_end_lip:
  LD A,(piece_rise)       ; Only for a flat piece with end cap $13 that drew
  AND A                   ; something.
  RET NZ                  ;
  LD A,(piece_extra_b)    ;
  AND $7F                 ;
  CP $13                  ;
  RET NZ                  ;
  LD A,(piece_steps_done) ;
  AND A                   ;
  RET Z                   ;
  LD HL,floor_lip_bytes    ; DE = the lip for this direction and fine position.
  LD A,(IX+$03)            ;
  AND A                    ;
  JR Z,draw_flat_end_lip_0 ;
  LD HL,ceiling_lip_bytes  ;
draw_flat_end_lip_0:
  LD A,(origin_x)          ;
  AND $07                  ;
  RET Z                    ;
  DEC A                    ;
  DEC A                    ;
  ADD A,A                  ;
  LD E,A                   ;
  LD D,$00                 ;
  ADD HL,DE                ;
  EX DE,HL
  LD A,(last_step_in_border) ; The column after the last step; a last step
  AND A                      ; hanging into the border puts it a character row
  LD HL,(column_address)     ; up.
  JR Z,draw_flat_end_lip_1   ;
  LD (IX+$01),$00            ;
  LD A,L                     ;
  SUB $20                    ;
  LD L,A                     ;
  INC L                      ;
  INC L                      ;
  INC L                      ;
draw_flat_end_lip_1:
  INC L                      ;
  INC L                      ;
  INC L                      ;
  LD A,L                  ; Outside columns 1-28: nothing.
  AND $1F                 ;
  DEC A                   ;
  CP $1C                  ;
  RET NC                  ;
  LD B,$04                  ; At the bottom edge: two rows only, from the play
  LD A,(piece_y)            ; area's bottom row (the cut never came up in the
  CP $B9                    ; build's sessions).
  JR NZ,draw_flat_end_lip_2 ;
  INC DE                    ;
  INC DE                    ;
  LD H,$57                  ;
  LD A,L                    ;
  AND $1F                   ;
  OR $C0                    ;
  LD L,A                    ;
  LD B,$02                  ;
  JR draw_flat_end_lip_3    ;
draw_flat_end_lip_2:
  CP $19                    ; At the top edge: two rows only.
  JR NZ,draw_flat_end_lip_3 ;
  LD B,$02                  ;
draw_flat_end_lip_3:
  LD A,(DE)                ; Write the lip upwards.
  LD (HL),A                ;
  INC DE                   ;
  CALL line_up_hl          ;
  DJNZ draw_flat_end_lip_3 ;
  RET                      ;

; Blank the byte column a flat piece's drop leaves behind when scrolling across
;
; Used by the routine at after_step_drawn.
;
; Called at the end of a flat piece when the fine horizontal position is on a
; byte boundary and the landscape is scrolling across (scroll_across). A byte
; column four rows tall beside a drop at one end of the run is cleared to black
; by fill_under_slope. Which step gives the place depends on the current-piece
; record's +1: if zero, the piece's first step (first_step_address), provided
; its cap on the side the scroll moves towards (piece_extra_b when
; scroll_across is negative, else piece_extra_a) is odd; otherwise the step
; kept by flat_step_clip (piece_kept set), two columns on.
fill_flat_end_column:
  LD A,(scroll_across)     ; No horizontal scroll: nothing to do.
  AND A                    ;
  JP Z,after_step_drawn_14 ;
  LD A,(IX+$01)                ; +1 zero: is that cap odd? Then the column left
  AND A                        ; of the first step.
  JR NZ,fill_flat_end_column_1 ;
  LD A,(scroll_across)         ;
  BIT 7,A                      ;
  LD A,(piece_extra_b)         ;
  JR NZ,fill_flat_end_column_0 ;
  LD A,(piece_extra_a)         ;
fill_flat_end_column_0:
  BIT 0,A                      ;
  JP Z,after_step_drawn_14     ;
  EXX                          ;
  LD HL,(first_step_address)   ;
  DEC L                        ;
  JR fill_flat_end_column_2    ;
fill_flat_end_column_1:
  LD A,(piece_kept)           ; Otherwise the step kept by flat_step_clip, two
  AND A                       ; columns on (a character row up if it hung off
  JP Z,after_step_drawn_14    ; the left edge).
  EXX                         ;
  LD HL,(first_step_address)  ;
  INC L                       ;
  INC L                       ;
  LD A,(scratch_count)        ;
  CP $F0                      ;
  JR C,fill_flat_end_column_2 ;
  LD A,L                      ;
  SUB $20                     ;
  LD L,A                      ;
fill_flat_end_column_2:
  LD A,(first_step_y)         ; Its y; scrolling the other way, four columns
  LD (piece_page_y),A         ; further.
  LD A,(scroll_across)        ;
  BIT 7,A                     ;
  JR Z,fill_flat_end_column_3 ;
  LD A,L                      ;
  ADD A,$04                   ;
  LD L,A                      ;
fill_flat_end_column_3:
  LD DE,$0000             ; Black, two row pairs, one step: fill_under_slope.
  LD C,$02                ;
  EXX                     ;
  LD B,$01                ;
  CALL fill_under_slope   ;
  JP after_step_drawn_14  ;

; Work out a step's screen position and address
;
; Used by the routines at edge_column and landscape_step_loop.
;
; Screen x = in-cell x (piece_page_x) + horizontal origin (origin_x); screen y
; = in-cell y (piece_page_y) + vertical origin (origin_y). Both go into the
; current-piece record (+0, +1) and piece_x/piece_y, and HL is the screen
; address (pixel_to_screen). For the piece's first step the address is also
; kept in first_step_address (a character row back when the step hangs off the
; left edge, x from $F0) and its y in first_step_y; for the piece's first drawn
; step the screen position is kept in first_step_x/first_step_y_drawn
; (column_clipped calls the routine's last part for that alone).
;
;   IX Current-piece record (current_piece)
; O:HL Screen address of the step
step_screen_address:
  LD A,(piece_page_y)     ; Screen y.
  LD H,A                  ;
  LD A,(origin_y)         ;
  ADD A,H                 ;
  LD H,A                  ;
  LD (IX+$01),A           ;
  LD (piece_y),A          ;
  LD A,(piece_page_x)     ; Screen x.
  LD L,A                  ;
  LD A,(origin_x)         ;
  ADD A,L                 ;
  LD L,A                  ;
  LD (piece_x),A          ;
  LD (IX+$00),A           ;
  LD D,$00                   ; D = $20 if the step hangs off the left edge; the
  CP $F0                     ; address.
  JR C,step_screen_address_0 ;
  LD D,$20                   ;
step_screen_address_0:
  CALL pixel_to_screen       ;
  LD A,(piece_steps_done)     ; First step of the piece: keep its address and
  AND A                       ; y.
  JR NZ,step_screen_address_1 ;
  LD A,L                      ;
  SUB D                       ;
  LD L,A                      ;
  LD (first_step_address),HL  ;
  ADD A,D                     ;
  LD L,A                      ;
  LD A,(piece_y)              ;
  LD (first_step_y),A         ;
step_screen_address_1:
  LD A,(clipped_columns)  ; Not the first drawn step: done.
  AND A                   ;
  RET NZ                  ;
; This entry point is used by the routine at column_clipped.
step_screen_address_2:
  LD A,(piece_page_x)       ; Keep the first drawn step's screen position (also
  LD B,A                    ; entered here).
  LD A,(origin_x)           ;
  ADD A,B                   ;
  LD (first_step_x),A       ;
  LD A,(piece_page_y)       ;
  LD B,A                    ;
  LD A,(origin_y)           ;
  ADD A,B                   ;
  LD (first_step_y_drawn),A ;
  RET                       ;

; Adjust a vertical fill's start column and width for a horizontal scroll
;
; Used by the routine at fill_behind_moving_piece.
;
; Used by fill_behind_moving_piece for a flat piece with no step set up. With
; scroll_across negative the fill starts a column further left. The rest (a
; column back right and one narrower) can only run for scroll_across = -128,
; never ran, and looks dead.
;
; L Low byte of the fill's screen address
; C Fill width in bytes
adjust_fill_for_scroll:
  LD A,(scroll_across)
  BIT 7,A
  RET Z
  DEC L
  LD A,(piece_page_y)
  CP $5F
  RET NC
  LD A,(scroll_across)
  NEG
  BIT 7,A
  RET Z
  INC L
  DEC C
  RET

; Fill the rows a vertically scrolled piece has uncovered
;
; Used by the routine at after_step_drawn.
;
; When the landscape has moved up or down by vertical_step_size pixels (2 or 4)
; this pass, each drawn step of the piece leaves that many rows of its old
; place uncovered: below it when the landscape moved up, above when it moved
; down. Starting from the first drawn step (first_step_x), this paints them
; step by step (fill_next_step with IY = fill_two_rows for 2 rows,
; fill_four_rows for 4) with the checker ($55AA) for ground or black for sky --
; the other way round for a ceiling piece (piece_ceiling). The width per step
; is its byte advance (piece_step_y), adjusted for the shift, a reversing piece
; and drop caps (landscape_end_pieces, adjust_fill_for_scroll).
fill_behind_moving_piece:
  LD IY,fill_four_rows            ; Two rows or four.
  LD A,(vertical_step_size)       ;
  CP $04                          ;
  JR Z,fill_behind_moving_piece_0 ;
  LD IY,fill_two_rows             ;
fill_behind_moving_piece_0:
  LD DE,$0000                     ; Black or checker by the direction and the
  LD A,(scroll_down)              ; ceiling flag.
  LD B,A                          ;
  LD A,(piece_ceiling)            ;
  XOR B                           ;
  BIT 7,A                         ;
  JR Z,fill_behind_moving_piece_1 ;
  LD DE,$55AA                     ;
fill_behind_moving_piece_1:
  LD HL,(first_step_x)             ; Start at the first drawn step.
  LD A,(piece_dx_copy)             ;
  AND A                            ;
  JR NZ,fill_behind_moving_piece_4 ;
  LD A,(direction_flag)           ; A vertical wall: filled as one tall step;
  CP $01                          ; nothing if it is moving the wrong way.
  JR Z,fill_behind_moving_piece_2 ;
  LD A,L                          ;
  ADD A,$07                       ;
  LD L,A                          ;
fill_behind_moving_piece_2:
  LD A,(clipped_columns)          ;
  LD B,A                          ;
  XOR A                           ;
fill_behind_moving_piece_3:
  ADD A,$10                       ;
  DJNZ fill_behind_moving_piece_3 ;
  LD (piece_rows),A               ;
  LD A,$01                        ;
  LD (clipped_columns),A          ;
  LD DE,$55AA                     ;
  LD A,(direction_flag)           ;
  LD B,A                          ;
  LD A,(scroll_across)            ;
  AND A                           ;
  JR Z,fill_behind_moving_piece_4 ;
  XOR B                           ;
  BIT 7,A                         ;
  JP NZ,after_step_drawn_15       ;
fill_behind_moving_piece_4:
  LD A,(scroll_down)               ; Start above the step (moving down) or
  BIT 7,A                          ; below it (moving up).
  LD A,(vertical_step_size)        ;
  JR NZ,fill_behind_moving_piece_5 ;
  LD A,(piece_rows)                ;
  NEG                              ;
fill_behind_moving_piece_5:
  ADD A,H                          ;
  LD H,A                           ;
  LD (piece_page_y),A              ;
  LD (saved_speed),HL     ; Keep the start for landscape_end_pieces.
  LD A,(piece_rise)                ; Flat piece: moved by the horizontal
  AND A                            ; scroll, and filled as a single strip as
  JR NZ,fill_behind_moving_piece_9 ; wide as all its steps (1 + 3 a step, in
  LD A,(scroll_across)             ; piece_step_y, and less one into the
  NEG                              ; operand of the LD A,n near the end of
  ADD A,L                          ; landscape_end_pieces).
  LD L,A                           ;
  LD A,(clipped_columns)           ;
  LD B,A                           ;
  LD A,$01                         ;
  LD (clipped_columns),A           ;
  LD A,(last_step_in_border)       ;
  AND A                            ;
  JR Z,fill_behind_moving_piece_6  ;
  LD A,(piece_extra_b)             ;
  AND $7F                          ;
  CP $13                           ;
  JR NZ,fill_behind_moving_piece_6 ;
  LD A,E                           ;
  AND A                            ;
  JR Z,fill_behind_moving_piece_6  ;
  LD A,$01                         ;
  JR fill_behind_moving_piece_8    ;
fill_behind_moving_piece_6:
  LD A,$01                         ;
fill_behind_moving_piece_7:
  ADD A,$03                        ;
  DJNZ fill_behind_moving_piece_7  ;
fill_behind_moving_piece_8:
  LD (piece_step_y),A              ;
  DEC A                            ;
  LD ($8384),A                     ;
fill_behind_moving_piece_9:
  LD A,(piece_dx_copy)              ; No step set up and not a wall: moved by
  AND A                             ; the horizontal scroll; a piece that
  JP Z,fill_behind_moving_piece_15  ; reverses (piece_clip) turns
  LD A,(IX+$01)                     ; direction_flag round; off a byte boundary
  AND A                             ; the strip is a byte wider.
  JR NZ,fill_behind_moving_piece_15 ;
  LD A,E                            ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_10  ;
  LD A,(piece_clip)                 ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_10  ;
  LD A,H                            ;
  CP $1F                            ;
  JR NC,fill_behind_moving_piece_11 ;
fill_behind_moving_piece_10:
  LD A,(scroll_across)              ;
  NEG                               ;
  ADD A,L                           ;
  LD L,A                            ;
fill_behind_moving_piece_11:
  LD A,(piece_clip)                 ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_15  ;
  CP $01                            ;
  JR Z,fill_behind_moving_piece_13  ;
  LD A,(direction_flag)             ;
  CP $01                            ;
  JR Z,fill_behind_moving_piece_14  ;
fill_behind_moving_piece_12:
  NEG                               ;
  LD (direction_flag),A             ;
  JR fill_behind_moving_piece_14    ;
fill_behind_moving_piece_13:
  LD A,(direction_flag)             ;
  CP $01                            ;
  JR Z,fill_behind_moving_piece_12  ;
fill_behind_moving_piece_14:
  LD A,L                            ;
  AND $07                           ;
  JR Z,fill_behind_moving_piece_15  ;
  LD A,(piece_step_y)               ;
  INC A                             ;
  LD (piece_step_y),A               ;
fill_behind_moving_piece_15:
  LD A,(piece_rise)           ; Flat piece: width adjusted for drop caps
  AND A                       ; (landscape_end_pieces).
  CALL Z,landscape_end_pieces ;
  LD B,$00                         ; Screen address of the start; x from $F0 is
  LD A,L                           ; the character row before.
  LD (scratch_count),A             ;
  CP $F0                           ;
  JR C,fill_behind_moving_piece_16 ;
  LD L,B                           ;
  INC B                            ;
fill_behind_moving_piece_16:
  CALL pixel_to_screen             ;
  LD A,L                           ;
  SUB B                            ;
  LD L,A                           ;
  LD A,(piece_step_y)               ; C = width; nudge the start column by the
  LD C,A                            ; piece's and the scroll's directions.
  LD A,(piece_dx_copy)              ;
  AND A                             ;
  JP Z,fill_behind_moving_piece_21  ;
  LD A,(piece_rise)                 ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_18  ;
  LD A,(IX+$01)                     ;
  AND A                             ;
  JP Z,fill_behind_moving_piece_19  ;
  LD A,(scratch_count)              ;
  AND $07                           ;
  JP NZ,fill_behind_moving_piece_19 ;
  LD A,(scroll_across)              ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_17  ;
  LD B,A                            ;
  LD A,(scroll_down)                ;
  XOR B                             ;
  LD B,A                            ;
  LD A,(direction_flag)             ;
  XOR B                             ;
  BIT 7,A                           ;
  JR Z,fill_behind_moving_piece_17  ;
  DEC L                             ;
  LD A,(scroll_across)              ;
  BIT 7,A                           ;
  JR Z,fill_behind_moving_piece_17  ;
  INC L                             ;
  INC L                             ;
fill_behind_moving_piece_17:
  JP fill_behind_moving_piece_21    ;
fill_behind_moving_piece_18:
  LD A,(IX+$01)                     ;
  AND A                             ;
  CALL Z,adjust_fill_for_scroll     ;
  JR fill_behind_moving_piece_21    ;
fill_behind_moving_piece_19:
  LD A,(piece_rise)                 ;
  AND A                             ;
  JR Z,fill_behind_moving_piece_21  ;
  LD A,(direction_flag)             ;
  CP $01                            ;
  LD A,(scroll_down)                ;
  JR NZ,fill_behind_moving_piece_20 ;
  NEG                               ;
fill_behind_moving_piece_20:
  BIT 7,A                           ;
  JR Z,fill_behind_moving_piece_21  ;
  INC L                             ;
fill_behind_moving_piece_21:
  EXX                     ; B = steps: fill behind them, then on to colouring.
  LD A,(clipped_columns)  ;
  LD B,A                  ;
  CALL fill_next_step     ;
  EXX                     ;
  JP after_step_drawn_15  ;

; Fill behind one step (loop head)
;
; Used by the routines at fill_behind_moving_piece and fill_up_past_step.
;
; Switches to the fill's register set and jumps to the routine in IY
; (fill_two_rows or fill_four_rows), which comes back through fill_up_past_step
; for the next step.
;
; B' Steps left
fill_next_step:
  EXX
  JP (IY)

; Move a fill up past the rest of a step, and loop
;
; Used by the routines at fill_two_rows and fill_four_rows.
;
; Moves HL up the rest of the step's height (piece_rows less the B rows already
; filled), keeps piece_page_y in step, and loops to fill_next_step for the next
; step.
;
; B Rows already filled
fill_up_past_step:
  LD A,(piece_rows)
  SUB B
  LD B,A
  LD A,(piece_page_y)
  SUB B
  LD (piece_page_y),A
  SRL B
fill_up_past_step_0:
  DEC H
  CALL line_up_hl
  DJNZ fill_up_past_step_0
  EXX
  DJNZ fill_next_step
  RET

; Fill two rows behind a step
;
; One row pair (fill_row_pair), then up the step.
fill_two_rows:
  CALL fill_row_pair
  LD B,$02
  JP fill_up_past_step

; Fill four rows behind a step
;
; Two row pairs, the second a byte advance back to the left, then up the step.
fill_four_rows:
  CALL fill_row_pair
  LD A,(piece_dcolumn)
  NEG
  ADD A,L
  LD L,A
  CALL fill_row_pair
  LD B,$04
  JP fill_up_past_step

; Fill one row pair across a step's width
;
; Used by the routines at fill_two_rows and fill_four_rows.
;
; Writes D on the row at HL and E on the row above, for C bytes rightwards,
; skipping any byte outside columns 1-28, and only when the row (piece_page_y)
; is inside the play area. Then moves piece_page_y and HL two rows up and a
; byte advance on.
;
; HL Screen address
; C Width in bytes
; DE Fill pattern: $55AA (checker) or 0
fill_row_pair:
  LD A,(piece_page_y)     ; Row outside the play area: nothing.
  CP $B8                  ;
  JR NC,fill_row_pair_2   ;
  CP $18                  ;
  JR C,fill_row_pair_2    ;
  LD B,C                  ; Across the width, inside columns 1-28 only.
  PUSH HL                 ;
fill_row_pair_0:
  LD A,L                  ;
  AND $1F                 ;
  DEC A                   ;
  CP $1C                  ;
  JR NC,fill_row_pair_1   ;
  LD (HL),D               ;
  DEC H                   ;
  LD (HL),E               ;
  INC H                   ;
fill_row_pair_1:
  INC L                   ;
  DJNZ fill_row_pair_0    ;
  POP HL                  ;
fill_row_pair_2:
  LD A,(piece_page_y)     ; Two rows up, a byte advance across.
  DEC A                   ;
  DEC A                   ;
  LD (piece_page_y),A     ;
  LD A,(piece_dcolumn)    ;
  ADD A,L                 ;
  LD L,A                  ;
  DEC H                   ;
  JP line_up_hl           ;

; Draw a sloped step cut off by the right edge
;
; Used by the routine at step_dispatch.
;
; Reached through step_dispatch (dispatch 2) for a sloped step of a piece
; running right (directions 1-3 and their ceilings) that crosses the play
; area's right edge: only its left one or two byte columns are written. Like
; the full blitters it zig-zags up the screen two rows at a time, popping the
; graphic from SP. After the first column's byte, the JR whose displacement
; set_right_clip_columns or slope_lose_last_column set decides the rest:
;
; * 0: the second column, then the upper row's second and first columns,
;   passing the third column's pair -- a three-byte step cut to two.
; * $1E: one column -- a three-byte step cut to one.
; * $1F: one column -- a two-byte step cut to one.
; * $20: one column -- a one-byte step, uncut.
;
; This was code typed as text and data in the first code map.
;
; A Row pairs
; HL Screen address, less the byte advance
; SP The step's graphic
blit_right_clip:
  LD B,A                  ; B = row pairs.
  LD A,(piece_dcolumn)    ; To the step's first column.
  ADD A,L                 ;
  LD L,A                  ;
blit_right_clip_0:
  POP DE                  ; Lower row, first column.
  LD (HL),E               ;
blit_right_clip_1:
  JR blit_right_clip_1    ; Patched: how much more of the row pair is drawn.
  INC L                   ; Second column; up a row.
  LD (HL),D               ;
  DEC H                   ;
  POP DE                  ; Pass the third column's pair; take the upper row's.
  POP DE                  ;
  LD (HL),E               ; Upper row: second column, first column.
  DEC L                   ;
blit_right_clip_2:
  LD (HL),D               ;
  DEC H                   ; Up a pixel row, crossing a character row if need
  LD A,H                  ; be.
  INC A                   ;
  AND $07                 ;
  JR NZ,blit_right_clip_3 ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_right_clip_3  ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_right_clip_3:
  DJNZ blit_right_clip_0  ; Next row pair.
  JP after_step_drawn     ; Done.
  POP DE                  ; One-column cuts: pass two pairs, one, or none; up a
  POP DE                  ; row; the upper row's first column.
  DEC H                   ;
  JP blit_right_clip_2    ;

; Draw a sloped step cut off by the left edge
;
; Used by the routine at step_dispatch.
;
; Reached through step_dispatch (dispatch 8) for a sloped step of a piece
; running right that hangs over the play area's left edge
; (slope_step_at_left_edge). HL moves to column 1 of the same character row;
; the first patched JR decides whether one or two graphic bytes are skipped
; (INC SP), so that the first byte popped belongs in column 1; the second
; decides how much of each row pair is written: displacement 0 the last two
; columns, $2C the last column of a two-byte step, $30 the last column of a
; three-byte step. A POP at the end of each row pair keeps the graphic in step.
; Afterwards the next step's column comes from move_across_kept.
;
; This was code typed as text and data in the first code map, before the staged
; tours ran it.
;
; A Row pairs
; HL Screen address
; SP The step's graphic
blit_left_clip:
  LD B,A                  ; B = row pairs.
  LD A,L                  ; Column 1 of this character row.
  INC A                   ;
  INC A                   ;
  AND $E0                 ;
  INC A                   ;
  LD L,A                  ;
  INC SP                  ; Skip a graphic byte...
blit_left_clip_0:
  JR blit_left_clip_0     ; ...patched: and another (displacement 0) or not
                          ; (1).
  INC SP                  ; The second byte skipped.
blit_left_clip_1:
  POP DE                  ; Lower row, column 1.
  LD (HL),E               ;
blit_left_clip_2:
  JR blit_left_clip_2     ; Patched: how much more of the row pair is drawn.
  INC L                   ; Column 2; up a row; upper row column 2, column 1.
  LD (HL),D               ;
  DEC H                   ;
  POP DE                  ;
  LD (HL),E               ;
  DEC L                   ;
blit_left_clip_3:
  LD (HL),D               ;
blit_left_clip_4:
  POP DE                  ; Pass the rest of the row pair.
  DEC H                   ; Up a pixel row, crossing a character row if need
  LD A,H                  ; be.
  INC A                   ;
  AND $07                 ;
  JR NZ,blit_left_clip_5  ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_left_clip_5   ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_left_clip_5:
  DJNZ blit_left_clip_1   ; Next row pair.
  LD A,(move_across_kept) ; The next step's column, from move_across_kept and
  LD B,A                  ; the byte advance.
  LD A,(piece_dcolumn)    ;
  LD C,A                  ;
  LD A,L                  ;
  AND $E0                 ;
  OR B                    ;
  SUB C                   ;
  LD L,A                  ;
  JP after_step_drawn     ; Done.
  DEC H                   ; Last column of a two-byte step: up a row, the upper
  JP blit_left_clip_3     ; row's byte.
  DEC H                   ; Last column of a three-byte step: up a row, the
  LD (HL),D               ; upper row's byte, pass a pair.
  POP DE                  ;
  JP blit_left_clip_4     ;

; Draw a mirrored sloped step cut off by the left edge
;
; Used by the routine at step_dispatch.
;
; Reached through step_dispatch (dispatch 11) for a sloped step of a piece
; running left (directions 4-6 and their ceilings) that hangs over the left
; edge. Those directions are drawn as mirror images: each byte is looked up in
; the bit-reversing table bit_reverse_table with B = its page and C = the byte,
; so the row count lives in B' and the loop switches register sets. HL goes to
; column 1; INC SP and the first patched JR skip one or two graphic bytes; the
; second patched JR (displacement 0, $26 or $2A, set by
; slope_step_at_left_edge) picks how many columns of each row pair are written.
;
; This was code partly typed as data in the first code map.
;
; A Row pairs
; HL Screen address
; SP The step's graphic
blit_mirrored_left_clip:
  EXX                     ; Row count into B'.
  LD B,A                  ;
  EXX                     ;
  LD A,L                  ; Column 1 of this character row.
  INC A                   ;
  INC A                   ;
  AND $E0                 ;
  INC A                   ;
  LD L,A                  ;
  LD B,$F9                ; B = the page of the bit-reversing table.
  EXX                     ; Skip one graphic byte...
  INC SP                  ;
blit_mirrored_left_clip_0:
  JR blit_mirrored_left_clip_0 ; ...patched: and another (displacement 0) or
                               ; not (1).
  INC SP                  ; The second byte skipped.
blit_mirrored_left_clip_1:
  EXX                     ; Lower row, column 1, mirrored.
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
blit_mirrored_left_clip_2:
  JR blit_mirrored_left_clip_2 ; Patched: how much more of the row pair is
                               ; drawn.
  INC L                   ; Column 2; up a row; upper row column 2.
  LD C,D                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ;
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC L                   ;
blit_mirrored_left_clip_3:
  LD C,D                  ; Upper row, column 1.
  LD A,(BC)               ;
  LD (HL),A               ;
blit_mirrored_left_clip_4:
  POP DE                  ; Pass the rest of the row pair.
  DEC H                           ; Up a pixel row, crossing a character row if
  LD A,H                          ; need be.
  INC A                           ;
  AND $07                         ;
  JR NZ,blit_mirrored_left_clip_5 ;
  LD A,L                          ;
  SUB $20                         ;
  LD L,A                          ;
  JR C,blit_mirrored_left_clip_5  ;
  LD A,H                          ;
  ADD A,$08                       ;
  LD H,A                          ;
blit_mirrored_left_clip_5:
  EXX                            ; Next row pair (counted in B'); done.
  DJNZ blit_mirrored_left_clip_1 ;
  EXX                            ;
  JP after_step_drawn            ;
  DEC H                        ; One column of a two-byte step: up a row, the
  JP blit_mirrored_left_clip_3 ; upper row's byte.
  DEC H                        ; One column of a three-byte step: up a row, the
  LD C,D                       ; upper row's byte, pass a pair.
  LD A,(BC)                    ;
  LD (HL),A                    ;
  POP DE                       ;
  JP blit_mirrored_left_clip_4 ;

; Draw a mirrored sloped step cut off by the right edge
;
; Used by the routine at step_dispatch.
;
; Reached through step_dispatch (dispatch 5) for a sloped step of a piece
; running left that crosses the right edge. Like blit_mirrored_left_clip it
; mirrors each byte through the table bit_reverse_table and keeps the row count
; in B'. The patched JR (displacement set by use_mirrored_right_clip: 0 for two
; columns, $26, $27 or $28 for one, by how many pairs must be passed) decides
; how many of the step's columns are written.
;
; This was code partly typed as data in the first code map.
;
; A Row pairs
; HL Screen address less the byte advance
; SP The step's graphic
blit_mirrored_right_clip:
  EXX                     ; Row count into B'.
  LD B,A                  ;
  EXX                     ;
  LD A,(piece_dcolumn)    ; To the step's first column.
  ADD A,L                 ;
  LD L,A                  ;
  LD B,$F9                      ; B = the page of the bit-reversing table; into
  JP blit_mirrored_right_clip_1 ; the loop.
blit_mirrored_right_clip_0:
  EXX                     ; Loop: the drawing registers.
blit_mirrored_right_clip_1:
  POP DE                  ; Lower row, first column, mirrored.
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
blit_mirrored_right_clip_2:
  JR blit_mirrored_right_clip_2 ; Patched: how much more of the row pair is
                                ; drawn.
  INC L                   ; Second column; up a row; pass the third column's
  LD C,D                  ; pair; upper row second column.
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ;
  POP DE                  ;
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC L                   ;
blit_mirrored_right_clip_3:
  LD C,D                  ; Upper row, first column.
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                            ; Up a pixel row, crossing a character row
  LD A,H                           ; if need be.
  INC A                            ;
  AND $07                          ;
  JR NZ,blit_mirrored_right_clip_4 ;
  LD A,L                           ;
  SUB $20                          ;
  LD L,A                           ;
  JR C,blit_mirrored_right_clip_4  ;
  LD A,H                           ;
  ADD A,$08                        ;
  LD H,A                           ;
blit_mirrored_right_clip_4:
  EXX                             ; Next row pair (counted in B'); done.
  DJNZ blit_mirrored_right_clip_0 ;
  EXX                             ;
  JP after_step_drawn             ;
  POP DE                        ; One-column cuts: pass two pairs, one, or
  POP DE                        ; none; up a row; the upper row's first column.
  DEC H                         ;
  JP blit_mirrored_right_clip_3 ;

; The next tile record to draw
;
; A pointer into map_window_tiles: reset to its start by scroll_landscape and
; load_map_window, and moved on a word by draw_landscape as it takes each map
; cell's tile record (and by load_map_window as it fills the list).
map_window_pointer:
  DEFW $0000

; The tile records of the four map cells on screen
;
; Filled by load_map_window whenever the view crosses into a new map cell: the
; addresses of the tile records (from tile_table) of the view's page, the one
; below, the one to the right and the one below-right -- the same order as
; their collision-box lists loader_entry, ground_cell_bottom_left,
; ground_cell_top_right and ground_cell_bottom_right. Walked through
; map_window_pointer by draw_landscape once a pass.
map_window_tiles:
  DEFW $0000
  DEFW $0000
  DEFW $0000
  DEFW $0000

; The tile bytes of the four map cells on screen
;
; Written by load_map_window: the four cells' tile bytes from the map
; landscape_map, in the same order as map_window_tiles. The collision and
; object code reads them too.
map_window_tile_bytes:
  DEFB $02                ; The view's cell

; The tile byte of the cell below the view's
;
; The second of map_window_tile_bytes's four, read on its own by the collision
; and object code.
map_window_tile_below:
  DEFB $02

; The tile byte of the cell right of the view's
;
; The third of map_window_tile_bytes's four.
map_window_tile_right:
  DEFB $02

; The tile byte of the cell below and right of the view's
;
; The fourth of map_window_tile_bytes's four.
map_window_tile_below_right:
  DEFB $02

; The landscape's attribute above ground
;
; Copied by start_landscape from the current planet's pair in
; planet_ground_colours. set_landscape_colours uses it for map rows above row
; 12; draw_landscape's colouring reads it too.
ground_attr_above:
  DEFB $00

; The landscape's attribute below ground
;
; Copied by start_landscape from the current planet's pair in
; planet_ground_colours. set_landscape_colours uses it for map rows 12 on, the
; caverns.
ground_attr_below:
  DEFB $00

; Landscape attributes for each planet
;
; Five pairs, one per planet (indexed by the planet number at +$12 of the
; player record score, in start_landscape): the attribute above ground, then
; below. Planet 0's is bright magenta, then yellow (seen as olive) on black.
planet_ground_colours:
  DEFB $43,$06
  DEFB $44,$46
  DEFB $47,$43
  DEFB $46,$47
  DEFB $45,$44

; Set up the landscape for a life and scroll it into view
;
; Used by the routine at play.
;
; Called by play when a player's life starts. Fills the fuel, blacks out the
; play area's colour, takes the planet's landscape colours from
; planet_ground_colours, and restores the view from the player's record score:
; the map column and row (+$13, and +$14 kept one less), the in-cell x and y at
; the play area's left and bottom edges (+$15, +$16; bit 0 of the x set means
; the play area does not need filling with the checker first), and the scroll
; origins (+$17, +$18).
;
; Then, with map_rebuild = $FF (the fly-in), the landscape is scrolled
; vertically 4 pixels a pass (2 on the first if that brings the origin to a
; multiple of 4) through scroll_landscape, with fly_in_step deciding when to
; stop; with map_rebuild = 3 one more pass is drawn; and with map_rebuild = 0
; the window is rebuilt with its objects, the ship's record (in bare_ship) gets
; the map position, and play goes on by way of sound_rising_whine.
start_landscape:
  LD A,$BE                ; Fuel full.
  LD (fuel_shown),A       ;
  LD HL,$4200             ;
  LD (fuel),HL            ;
  XOR A                   ; Play area's attributes black (the colour patch at
  LD ($F111),A            ; play_area_colour, 28 by 20 cells).
  CALL colour_play_area   ;
  LD A,(level)                ; This planet's two landscape attributes.
  ADD A,A                     ;
  LD HL,planet_ground_colours ;
  CALL add_a_to_hl            ;
  LD A,(HL)                   ;
  LD (ground_attr_above),A    ;
  INC HL                      ;
  LD A,(HL)                   ;
  LD (ground_attr_below),A    ;
  LD A,(ground_attr_above) ; Starting below ground (map row 12 or more): the
  LD B,A                   ; below-ground colour, and items 9 and 10 of the
  LD A,($F5FB)             ; player's item records (bit 7 of +$41 and +$45 of
  CP $0C                   ; score) and +$10 reset.
  JR C,start_landscape_0   ;
  LD A,(ground_attr_below) ;
  LD B,A                   ;
  LD HL,place_nine         ;
  RES 7,(HL)               ;
  LD HL,place_ten          ;
  RES 7,(HL)               ;
  XOR A                    ;
  LD (player_count_nine),A ;
start_landscape_0:
  LD A,B                  ; The play area's colour, applied later.
  LD ($F111),A            ;
  LD A,(restart_view)     ; Fill the play area (28 columns by 160 rows, from
  AND $01                 ; the bottom) with the checker unless bit 0 of the
  XOR $01                 ; saved x says not to.
  LD (start_filled),A     ;
  JR Z,start_landscape_3  ;
  LD HL,$57C1             ;
  LD C,$50                ;
start_landscape_1:
  LD B,$1C                ;
  PUSH HL                 ;
start_landscape_2:
  LD (HL),$55             ;
  DEC H                   ;
  LD (HL),$AA             ;
  INC H                   ;
  INC HL                  ;
  DJNZ start_landscape_2  ;
  POP HL                  ;
  DEC H                   ;
  CALL line_up_hl         ;
  DEC C                   ;
  JR NZ,start_landscape_1 ;
start_landscape_3:
  LD HL,(restart_scroll)  ; Map position, edge positions and scroll origins
  INC H                   ; from the player's record.
  LD (view_column),HL     ;
  LD HL,(restart_view)    ;
  LD A,L                  ;
  AND $FE                 ;
  LD L,A                  ;
  LD (view_x),HL          ;
  LD HL,(restart_fine)    ;
  LD (origin_x),HL        ;
  LD A,$02                ; Vertical scroll step: 2 if bit 1 of the vertical
  BIT 1,H                 ; origin is set, else 4.
  JR NZ,start_landscape_4 ;
  ADD A,A                 ;
start_landscape_4:
  LD (scroll_down),A      ;
  LD A,$FF                ; Fly-in phase; build the map window
  LD (map_rebuild),A      ; (load_map_window).
  LD HL,(view_column)     ;
  CALL load_map_window    ;
  XOR A                   ; No horizontal scroll; the ship's screen x
  LD (scroll_across),A    ; (ship_screen_x) = $60; clear 300 bytes from
  LD A,$60                ; ship_picture.
  LD (ship_screen_x),A    ;
  LD BC,$012C             ;
  LD HL,ship_picture      ;
start_landscape_5:
  LD (HL),$00             ;
  INC HL                  ;
  DEC BC                  ;
  LD A,B                  ;
  OR C                    ;
  JR NZ,start_landscape_5 ;
  CALL build_ship_sprite     ; Ship and panel set-up (build_ship_sprite,
  LD A,(rocket_part_carried) ; toggle_carried_part, the end of load_ore); at
  INC A                      ; most 40 passes of fly-in.
  JR Z,start_landscape_6     ;
  DEC A                      ;
  OR $C0                     ;
  LD (part_being_fitted),A   ;
  CALL toggle_carried_part   ;
start_landscape_6:
  XOR A                      ;
  LD (part_being_fitted),A   ;
  CALL load_ore_3            ;
  LD A,(ship_line)           ;
  SUB $09                    ;
  LD (trail_y),A             ;
  LD B,$28                   ;
; This entry point is used by the routine at fly_in_step.
start_landscape_7:
  PUSH BC                 ; One pass: fuel gauge, then the scroll and landscape
  CALL update_fuel_gauge  ; (scroll_landscape). fly_in_step comes back here for
  JP scroll_landscape     ; the next.

; Decide whether the landscape's fly-in has finished
;
; Used by the routine at scroll_landscape.
;
; scroll_landscape comes here instead of going on with the play pass while
; map_rebuild is not zero. In the fly-in ($FF) the landscape keeps scrolling 4
; pixels a pass: 40 passes (160 rows, one map cell) when the play area was left
; unfilled, or, when it was filled with the checker, until the six bytes in two
; rows at the ship's screen position (kept in bare_ship) are all clear -- the
; sky has been swept down to the ship's place. Then the colour goes on, one
; pass is drawn with map_rebuild = 3, and finally the window is rebuilt with
; objects (map_rebuild = 0), the ship's record (in bare_ship) gets the map
; column and row (+1, +4) and the in-cell position (+2, +5), and play begins
; through sound_rising_whine, which returns to the caller of start_landscape.
fly_in_step:
  LD A,(map_rebuild)      ; The last pass done: on to play.
  CP $03                  ;
  JP Z,fly_in_step_4      ;
  LD A,$04                ; Scroll 4 pixels a pass.
  LD (scroll_down),A      ;
  LD A,(start_filled)     ; Filled with the checker: go on until the ship's
  AND A                   ; place is clear.
  JR Z,fly_in_step_2      ;
  LD B,$06                ;
  LD HL,(ship_screen_x)   ;
  CALL pixel_to_screen    ;
fly_in_step_0:
  LD A,(HL)               ;
  AND A                   ;
  JR NZ,fly_in_step_1     ;
  DEC H                   ;
  LD A,(HL)               ;
  AND A                   ;
  JR NZ,fly_in_step_1     ;
  INC H                   ;
  INC L                   ;
  DJNZ fly_in_step_0      ;
  POP BC                  ;
  JR fly_in_step_3        ;
fly_in_step_1:
  POP BC                  ;
  JR start_landscape_7    ;
fly_in_step_2:
  POP BC                  ; Not filled: 40 passes.
  DJNZ start_landscape_7  ;
fly_in_step_3:
  CALL colour_play_area   ; Colour the play area; one more pass with the window
  XOR A                   ; rebuilt.
  LD (scroll_down),A      ;
  LD A,$03                ;
  LD (map_rebuild),A      ;
  LD HL,(view_column)     ;
  CALL load_map_window    ;
  JP start_landscape_7    ;
fly_in_step_4:
  POP BC                  ; Play: rebuild the window with its objects.
  XOR A                   ;
  LD (map_rebuild),A      ;
  LD HL,(view_column)     ;
  CALL load_map_window    ;
  LD IX,ship_record       ; The ship's map position and in-cell position.
  LD HL,(view_column)     ;
  LD (IX+$01),L           ;
  LD (IX+$04),H           ;
  LD HL,(view_x)          ;
  LD (IX+$02),L           ;
  LD (IX+$05),H           ;
  LD (IX+$07),$58         ;
  LD A,(IX+$1E)           ;
  SUB $17                 ;
  LD (IX+$09),A           ;
  CALL move_object        ;
  JP sound_rising_whine   ; Into play.

; Play area filled at the start of the life
;
; 1 if start_landscape filled the play area with the checker at the start of
; the life, which changes how the fly-in ends (fly_in_step); 0 if it left it.
start_filled:
  DEFB $01

; Unused
;
; Two bytes nothing reads or writes.
unused_landscape_bytes:
  DEFB $00,$00

; Objects in the window's left map column
;
; How many objects the map holds in map column view_column; written and read by
; spawn_column_objects.
left_column_objects:
  DEFB $00

; Objects in the window's right map column
;
; How many objects the map holds in the column after view_column; written and
; read by spawn_column_objects.
right_column_objects:
  DEFB $00

; The window's right-hand map column
;
; The map column after view_column, wrapped to 0-95, written by read_map_cell
; as it reads the map. load_map_window, drop_far_objects, spawn_column_objects
; and the object code read it.
right_map_column:
  DEFB $00

; Scroll the view, step through the map, and draw the landscape
;
; Used by the routines at ship_pass and start_landscape.
;
; Jumped to at the end of ship_pass (once a play pass) and from start_landscape
; (the fly-in). scroll_down and scroll_across are this pass's vertical and
; horizontal scroll in pixels (signed, from the ship's movement). The scroll
; origins origin_y and origin_x move by them, and the in-cell positions at the
; play area's bottom and left edges (view_y = $B7 - origin_y, view_x = 8 -
; origin_x) move the other way. When an edge position leaves its map cell it
; wraps by a cell -- 160 rows or 224 columns -- and the map row or column
; (view_row, view_column) moves by one; C counts the wraps into record_ground.
;
; If any happened (or in the fly-in) the window of four map cells is rebuilt
; (load_map_window) and its colours chosen (set_landscape_colours). The
; landscape is drawn (draw_landscape) unless the view is in the top three map
; rows, which hold no landscape (the four collision-box lists are just
; emptied). The sprites are handled by update_ground_objects before or after
; the landscape by the scroll's direction, draw_beams and draw_stars run, four
; rows of the frame below the play area's bottom-left corner are made solid
; again, and the pass goes on at draw_ship -- or at fly_in_step during the
; fly-in.
scroll_landscape:
  LD A,(scroll_down)        ; vertical_step_size = size of the vertical scroll.
  BIT 7,A                   ;
  JR Z,scroll_landscape_0   ;
  NEG                       ;
scroll_landscape_0:
  LD (vertical_step_size),A ;
  LD C,$00                ; No wraps yet; HL = map column and row.
  LD HL,(view_column)     ;
scroll_landscape_1:
  LD A,(origin_y)          ; Move vertically.
  LD B,A                   ;
  LD A,(scroll_down)       ;
  ADD A,B                  ;
  LD (origin_y),A          ;
  LD A,(scroll_down)       ;
  NEG                      ;
  LD B,A                   ;
  LD A,(view_y)            ;
  ADD A,B                  ;
  LD (view_y),A            ;
  CP $B8                   ;
  JR NC,scroll_landscape_2 ;
  CP $16                   ; The bottom edge has passed the cell's top: wrap to
  JR NC,scroll_landscape_3 ; the cell above (row - 1).
  LD A,$B7                 ;
  LD (view_y),A            ;
  XOR A                    ;
  LD (origin_y),A          ;
  DEC H                    ;
  INC C                    ;
  JP scroll_landscape_1    ;
scroll_landscape_2:
  LD A,$17                ; Past its bottom: wrap to the cell below (row + 1).
  LD (view_y),A           ;
  LD A,$A0                ;
  LD (origin_y),A         ;
  INC H                   ;
  INC C                   ;
  JP scroll_landscape_1   ;
scroll_landscape_3:
  LD A,(scroll_across)     ; Move horizontally.
  LD B,A                   ;
  LD A,(origin_x)          ;
  ADD A,B                  ;
  LD (origin_x),A          ;
  LD A,(scroll_across)     ;
  NEG                      ;
  LD B,A                   ;
  LD A,(view_x)            ;
  ADD A,B                  ;
  LD (view_x),A            ;
  CP $E9                   ;
  JR NC,scroll_landscape_4 ;
  CP $07                   ; The left edge has passed the cell's left: wrap
  JR NC,scroll_landscape_5 ; (column - 1).
  LD A,$E8                 ;
  LD (view_x),A            ;
  LD A,$20                 ;
  LD (origin_x),A          ;
  DEC L                    ;
  INC C                    ;
  JP scroll_landscape_3    ;
scroll_landscape_4:
  LD A,$08                ; Past its right: wrap (column + 1).
  LD (view_x),A           ;
  XOR A                   ;
  LD (origin_x),A         ;
  INC L                   ;
  INC C                   ;
  JP scroll_landscape_3   ;
scroll_landscape_5:
  LD A,C                     ; Rebuild the window if a cell was crossed;
  LD (record_ground),A       ; colours.
  AND A                      ;
  CALL NZ,load_map_window    ;
  CALL set_landscape_colours ;
  LD DE,map_window_tiles     ; Walk the window's four cells from the first.
  LD (map_window_pointer),DE ;
  LD A,(scroll_down)            ; scroll_down negative (the landscape moving
  BIT 7,A                       ; up): sprites first.
  CALL NZ,update_ground_objects ;
  LD A,(view_row)          ; Map rows 0-2 hold no landscape:
  CP $03                   ;
  JR NC,scroll_landscape_6 ;
  XOR A                    ;
  LD (loader_entry),A             ; empty the four collision-box lists instead.
  LD (ground_cell_bottom_left),A  ;
  LD (ground_cell_top_right),A    ;
  LD (ground_cell_bottom_right),A ;
  JR scroll_landscape_7           ;
scroll_landscape_6:
  CALL draw_landscape     ; Draw the landscape.
scroll_landscape_7:
  LD A,(scroll_down)           ; Otherwise sprites after.
  BIT 7,A                      ;
  CALL Z,update_ground_objects ;
  CALL draw_beams         ; draw_beams, draw_stars.
  CALL draw_stars         ;
  LD A,$FF                ; Solid frame bytes at column 1, pixel rows 185, 186,
  LD ($56E1),A            ; 189 and 190.
  LD ($55E1),A            ;
  LD ($52E1),A            ;
  LD ($51E1),A            ;
  LD A,(map_rebuild)      ; On with the play pass, or the fly-in.
  AND A                   ;
  JP Z,draw_ship          ;
  JP fly_in_step          ;

; Current-piece record
;
; Five bytes, addressed through IX while a piece is drawn: +0, +1 the screen x
; and y of the latest step set up (+1 is cleared at the start of each piece, so
; zero means none yet), +2 the number of steps drawn, +3 the direction (0-15).
; +4 is not used here. test_landscape_at and hit_block use the same bytes as
; scratch.
current_piece:
  DEFS $05

; Choose the landscape attributes for the upper and lower map cells
;
; Used by the routine at scroll_landscape.
;
; When the window has moved (record_ground) or during the fly-in:
; map_row_colour gets the attribute for the upper map row (view_row) and
; map_row_colour_next the one for the row below -- ground_attr_above for rows
; below 12 (above ground), ground_attr_below from row 12 (caverns). The
; routine's tail is called as a subroutine for the first and fallen into for
; the second.
set_landscape_colours:
  LD A,(record_ground)    ; Nothing changed: done.
  LD B,A                  ;
  LD A,(map_rebuild)      ;
  OR B                    ;
  RET Z                   ;
  LD A,(view_row)              ; The upper row's attribute.
  CALL set_landscape_colours_0 ;
  LD (map_row_colour),A        ;
  LD A,(view_row)              ; The lower row's.
  INC A                        ;
set_landscape_colours_0:
  CP $0C                       ;
  LD A,(ground_attr_above)     ;
  LD B,A                       ;
  JR C,set_landscape_colours_1 ;
  LD A,(ground_attr_below)     ;
  LD B,A                       ;
set_landscape_colours_1:
  LD A,B                       ;
  LD (map_row_colour_next),A   ;
  RET                          ;

; Build the 2x2 window of map cells, and bring in the objects they hold
;
; Used by the routines at start_landscape, fly_in_step and scroll_landscape.
;
; HL is the map column (0-95, wrapping at both ends) and row. The four tile
; bytes -- (column, row), (column, row+1), (column+1, row), (column+1, row+1)
; -- are read from the map landscape_map (96 bytes a row, read_map_cell) into
; map_window_tile_bytes, and their tile records' addresses from tile_table into
; map_window_tiles. At row 28 the lower two are tile $20, at row 29 all four.
; In the fly-in, tile $3E is drawn as $AA.
;
; In play (map_rebuild zero) it then refreshes the objects: the last two slots
; of ground_objects, kept for the player's items, are freed if their objects
; have left the window (drop_far_objects); each of the current player's 19 item
; records (from +$1D of score; four bytes: map column, map row, x, y; bit 7 of
; the first set when gone) that lies in the window is put into a free special
; slot as kind $0C or $1E, unless it is in one already (same_item_in_slot); the
; four ground-object slots at ground_objects are freed likewise; and
; spawn_column_objects brings in the map's objects for the window's two
; columns.
;
; HL Map column (L) and row (H)
load_map_window:
  LD DE,$0060                ; Wrap the column; reset the window's pointer.
  LD A,L                     ;
  CP $FF                     ;
  JR NZ,load_map_window_0    ;
  LD L,$5F                   ;
load_map_window_0:
  CP $60                     ;
  JR NZ,load_map_window_1    ;
  LD L,$00                   ;
load_map_window_1:
  LD (view_column),HL        ;
  LD BC,map_window_tiles     ;
  LD (map_window_pointer),BC ;
  PUSH HL                            ; The four tile bytes.
  CALL read_map_cell                 ;
  LD (map_window_tile_bytes),A       ;
  ADD HL,DE                          ;
  LD A,(HL)                          ;
  LD (map_window_tile_below),A       ;
  POP HL                             ;
  INC L                              ;
  CALL read_map_cell                 ;
  LD (map_window_tile_right),A       ;
  ADD HL,DE                          ;
  LD A,(HL)                          ;
  LD (map_window_tile_below_right),A ;
  LD A,(view_row)                    ; Below the map (rows $1C and $1D): never
  CP $1C                             ; reached in the build's sessions.
  JR NZ,load_map_window_2            ;
  LD A,$20                           ;
  LD (map_window_tile_below),A       ;
  LD (map_window_tile_below_right),A ;
load_map_window_2:
  CP $1D                             ;
  JR NZ,load_map_window_3            ;
  LD BC,$2020                        ;
  LD (map_window_tile_bytes),BC      ;
  LD (map_window_tile_right),BC      ;
load_map_window_3:
  LD A,(map_rebuild)          ; Fly-in: tile $3E becomes $AA. (No tile $3E came
  BIT 7,A                     ; into a fly-in in the build's sessions.)
  JR Z,load_map_window_6      ;
  LD B,$04                    ;
  LD HL,map_window_tile_bytes ;
load_map_window_4:
  LD A,(HL)                   ;
  CP $3E                      ;
  JR NZ,load_map_window_5     ;
  LD (HL),$AA                 ;
load_map_window_5:
  INC HL                      ;
  DJNZ load_map_window_4      ;
load_map_window_6:
  LD BC,map_window_tile_bytes ; The four tile-record addresses.
  LD A,$04                    ;
load_map_window_7:
  EX AF,AF'                   ;
  LD A,(BC)                   ;
  LD L,A                      ;
  LD H,$00                    ;
  LD DE,tile_table            ;
  ADD HL,HL                   ;
  ADD HL,DE                   ;
  LD E,(HL)                   ;
  INC HL                      ;
  LD D,(HL)                   ;
  LD HL,(map_window_pointer)  ;
  LD (HL),E                   ;
  INC HL                      ;
  LD (HL),D                   ;
  INC HL                      ;
  LD (map_window_pointer),HL  ;
  INC BC                      ;
  EX AF,AF'                   ;
  DEC A                       ;
  JR NZ,load_map_window_7     ;
  LD A,(map_rebuild)      ; Fly-in: no objects.
  AND A                   ;
  RET NZ                  ;
  LD A,$0C                ; Free the special slots whose objects have left.
  LD (scratch_count),A    ;
  LD IX,pickup_record     ;
  LD B,$02                ;
  CALL drop_far_objects   ;
  LD IX,player_places     ; The player's item records: the first 11 are
  LD HL,(view_column)     ; numbered 0-10 with kind $0C, the last 8 numbered
  LD A,(right_map_column) ; 0-7 with kind $1E.
  LD D,A                  ;
  LD BC,$1300             ;
load_map_window_8:
  LD A,C                  ;
  SUB $0B                 ;
  JR NZ,load_map_window_9 ;
  LD C,A                  ;
  LD A,$1E                ;
  LD (scratch_count),A    ;
load_map_window_9:
  BIT 7,(IX+$00)           ; Skip items gone or outside the window.
  JR NZ,load_map_window_13 ;
  LD A,(IX+$00)            ;
  CP L                     ;
  JR Z,load_map_window_10  ;
  CP D                     ;
  JR NZ,load_map_window_13 ;
load_map_window_10:
  LD A,H                   ;
  INC A                    ;
  CP (IX+$01)              ;
  JR Z,load_map_window_11  ;
  LD A,H                   ;
  CP (IX+$01)              ;
  JR NZ,load_map_window_13 ;
load_map_window_11:
  LD IY,pickup_record        ; Skip items already in a special slot.
  CALL same_item_in_slot     ;
  JR Z,load_map_window_13    ;
  LD IY,second_pickup_record ;
  CALL same_item_in_slot     ;
  JR Z,load_map_window_13    ;
  LD IY,pickup_record        ; Into the first free special slot: map column,
  LD A,(IY+$00)              ; row, x, y, item number, kind.
  INC A                      ;
  JR Z,load_map_window_12    ;
  LD IY,second_pickup_record ;
load_map_window_12:
  LD A,(IX+$00)              ;
  LD (IY+$01),A              ;
  LD A,(IX+$01)              ;
  LD (IY+$04),A              ;
  LD A,(IX+$02)              ;
  LD (IY+$02),A              ;
  LD A,(IX+$03)              ;
  LD (IY+$05),A              ;
  LD (IY+$13),C              ;
  LD (IY+$0D),$FF            ;
  LD A,(scratch_count)       ;
  LD (IY+$00),A              ;
load_map_window_13:
  INC IX                  ; Next item.
  INC IX                  ;
  INC IX                  ;
  INC IX                  ;
  INC C                   ;
  DJNZ load_map_window_8  ;
  LD IX,pickup_record         ; Tidy the special slots; free the ground-object
  LD B,$02                    ; slots that have left; bring in the map's.
  CALL spawn_column_objects_3 ;
  LD IX,ground_objects        ;
  LD B,$04                    ;
  CALL drop_far_objects       ;
  JP spawn_column_objects     ;

; Compare an item record with a special object slot
;
; Used by the routine at load_map_window.
;
;   IX Item record (map column, row, x, y)
;   IY Object slot
; O:F Z if the slot is in use and holds the same item; NZ if not, or if the
;     slot is free ($FF)
same_item_in_slot:
  LD A,(IY+$00)
  INC A
  JR Z,same_item_in_slot_0
  LD A,(IX+$00)
  CP (IY+$01)
  RET NZ
  LD A,(IX+$01)
  CP (IY+$04)
  RET NZ
  LD A,(IX+$02)
  CP (IY+$02)
  RET NZ
  LD A,(IX+$03)
  CP (IY+$05)
  RET
same_item_in_slot_0:
  OR $01
  RET

; Free the object slots whose objects are outside the window
;
; Used by the routine at load_map_window.
;
; A slot ($1A bytes: +0 state, $FF when free; +1 map column; +4 map row) is
; freed if its column is neither the window's left (view_column) nor its right
; (right_map_column) column, or its row is neither of the window's two.
;
; IX First slot
; B Number of slots
drop_far_objects:
  LD A,(right_map_column)
  LD C,A
  LD DE,$001A
  LD HL,(view_column)
  INC H
drop_far_objects_0:
  LD A,(IX+$00)
  INC A
  JR Z,drop_far_objects_3
  LD A,(IX+$01)
  CP L
  JR Z,drop_far_objects_1
  CP C
  JR NZ,drop_far_objects_2
drop_far_objects_1:
  LD A,(IX+$04)
  CP H
  JR Z,drop_far_objects_3
  INC A
  CP H
  JR Z,drop_far_objects_3
drop_far_objects_2:
  LD (IX+$00),$FF
drop_far_objects_3:
  ADD IX,DE
  DJNZ drop_far_objects_0
  RET

; Bring in the map's objects for the window's two map columns
;
; Used by the routine at load_map_window.
;
; The things lying in the world are listed column by column:
; map_objects_per_column holds a count for each map column, map_object_kinds
; one byte per object (bits 0-5 its kind, bit 7 set once it is gone), and
; map_object_places two position bytes per object. The index of column
; view_column's first object is the sum of the counts before it;
; spawn_objects_from_list brings in that column's objects, then the next
; column's (right_map_column), restarting at object 0 when the column wraps to
; 0. Last, slots still free get +$10 cleared.
spawn_column_objects:
  LD A,(view_column)           ; Index of the column's first object, into
  LD B,A                       ; box_x.
  LD HL,$0000                  ;
  LD DE,map_objects_per_column ;
  AND A                        ;
  JR Z,spawn_column_objects_1  ;
spawn_column_objects_0:
  LD A,(DE)                    ;
  CALL add_a_to_hl             ;
  INC DE                       ;
  DJNZ spawn_column_objects_0  ;
spawn_column_objects_1:
  LD (box_x),HL                ;
  ADD HL,HL                   ; Object counts of the two columns.
  EX DE,HL                    ;
  LD A,(HL)                   ;
  LD (left_column_objects),A  ;
  INC HL                      ;
  LD A,(HL)                   ;
  LD (right_column_objects),A ;
  LD IX,map_object_places      ; Pointers to its positions and kinds; bring
  ADD IX,DE                    ; them in.
  LD DE,map_object_kinds       ;
  LD HL,(box_x)                ;
  ADD HL,DE                    ;
  LD A,(view_column)           ;
  LD C,A                       ;
  LD A,(left_column_objects)   ;
  LD B,A                       ;
  CALL spawn_objects_from_list ;
  LD A,(right_map_column)      ; The next column (from object 0 if it wrapped;
  AND A                        ; that path never ran).
  JR NZ,spawn_column_objects_2 ;
  LD HL,$0000                  ;
  LD (box_x),HL                ;
  LD HL,map_object_kinds       ;
  LD IX,map_object_places      ;
spawn_column_objects_2:
  LD C,A                       ;
  LD A,(right_column_objects)  ;
  LD B,A                       ;
  CALL spawn_objects_from_list ;
  LD B,$04                ; Free slots: +$10 cleared.
  LD IX,ground_objects    ;
; This entry point is used by the routine at load_map_window.
spawn_column_objects_3:
  LD DE,$001A
spawn_column_objects_4:
  LD A,(IX+$00)
  CP $FF
  JR NZ,spawn_column_objects_5
  LD (IX+$10),$00
spawn_column_objects_5:
  ADD IX,DE
  DJNZ spawn_column_objects_4
  RET                     ; }

; Object kinds: slot flags
;
; One byte per kind (0-16), read by spawn_objects_from_list. Bits 7-5 go to
; bits 3-1 of the slot's +$18; bit 4 set makes the object an animated one,
; graphic $1B instead of $0D, with its frame byte and frame range from
; object_part_pairs (at the offset in the low four bits) and +$15 set to $80 so
; that animate_object animates it: kinds 4, 5, $0C, $0E and $10 (whose byte is
; $10: bit 4 set and offset 0, frames 0 and 1 like kind 4 -- the lamp).
object_kind_flags:
  DEFB $40,$01,$02,$23,$D0,$92,$06,$07 ; One byte per kind
  DEFB $08,$09,$0A,$0B,$B2,$6D,$14,$0F ;
  DEFB $10                             ;

; Object kinds: the animated objects' frames
;
; Read by spawn_objects_from_list for a kind whose flag byte in
; object_kind_flags has bit 4 set: the two bytes at the low four bits' offset
; go to the slot's +$13, the frame byte (the first frame in bits 0-3, a count
; and its reload above it), and +$14, the frame range (the first frame in the
; high nibble, the last in the low), which animate_object steps through.
; Offsets 0, 2 and 4 are used: kinds 4 and $10 show frames 0 and 1, kinds 5 and
; $0C frames 2 and 3, and kind $0E, the fuel station, frames 4 and 5 -- the two
; ship_hits_ground_objects takes for a refuel pad. Read in the build's sessions
; (spawn_objects_from_list).
object_part_pairs:
  DEFB $50,$01
  DEFB $52,$23
  DEFB $54,$45

; Bring the objects of one map column into free ground-object slots
;
; Used by the routine at spawn_column_objects.
;
; For each of B objects: skip it if gone (bit 7 of its kind byte, or bit 6 for
; player 2: play patches the BIT), if its map row is not one of the window's
; two, or if a slot at ground_objects already holds it; otherwise take the
; first free of the first three slots (the fourth if none is), set it up
; (free_object) and fill it in from the object's two position bytes -- bits 7-2
; of the first are the y (less one), bits 1-0 of it and bits 7-5 of the second
; the x, bits 4-0 of the second the map row -- with graphic $0D, kind (+$13),
; its colours from object_kind_bytes (+$0D), the flags from object_kind_flags
; (+$18) and the object's index (+$18 low bits, +$19) so it can be marked gone
; later.
;
; A Number of objects (none if 0)
; B Same
; C Map column
; HL Object's kind byte (in map_object_kinds)
; IX Object's position bytes (in map_object_places)
spawn_objects_from_list:
  AND A                           ; Gone, or in neither row of the window:
  RET Z                           ; next.
spawn_objects_from_list_0:
  PUSH BC                         ;
  BIT 7,(HL)                      ;
  JP NZ,spawn_objects_from_list_7 ;
  LD A,(IX+$01)                   ;
  AND $1F                         ;
  LD E,A                          ;
  LD A,(view_row)                 ;
  CP E                            ;
  JR Z,spawn_objects_from_list_1  ;
  INC A                           ;
  CP E                            ;
  JP NZ,spawn_objects_from_list_7 ;
spawn_objects_from_list_1:
  LD IY,ground_objects            ; Already in a slot: next.
  LD B,$04                        ;
spawn_objects_from_list_2:
  LD A,(IY+$00)                   ;
  INC A                           ;
  JR Z,spawn_objects_from_list_3  ;
  LD A,(IY+$01)                   ;
  CP C                            ;
  JR NZ,spawn_objects_from_list_3 ;
  LD A,(IY+$04)                   ;
  CP E                            ;
  JP Z,spawn_objects_from_list_7  ;
spawn_objects_from_list_3:
  LD A,E                          ;
  LD DE,$001A                     ;
  ADD IY,DE                       ;
  LD E,A                          ;
  DJNZ spawn_objects_from_list_2  ;
  LD IY,ground_objects           ; The first free slot.
  LD B,$03                       ;
spawn_objects_from_list_4:
  LD A,(IY+$00)                  ;
  INC A                          ;
  JR Z,spawn_objects_from_list_5 ;
  LD A,E                         ;
  LD DE,$001A                    ;
  ADD IY,DE                      ;
  LD E,A                         ;
  DJNZ spawn_objects_from_list_4 ;
spawn_objects_from_list_5:
  PUSH IX                 ; Set it up.
  PUSH IY                 ;
  POP IX                  ;
  POP IY                  ;
  CALL free_object        ;
  PUSH IX                 ;
  PUSH IY                 ;
  POP IX                  ;
  POP IY                  ;
  LD (IY+$04),E           ; Row, graphic $0D, kind, the colours from
  LD (IY+$00),$0D         ; object_kind_bytes.
  LD A,(HL)               ;
  AND $3F                 ;
  LD (IY+$13),A           ;
  PUSH HL                 ;
  LD HL,object_kind_bytes ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  LD (IY+$0D),A           ;
  LD HL,object_kind_flags ; Flags from object_kind_flags.
  LD A,(IY+$13)           ;
  CALL add_a_to_hl        ;
  LD A,(HL)               ;
  AND $E0                 ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD (IY+$18),A           ;
  BIT 4,(HL)                     ; An animated object: graphic $1B, its frame
  JR Z,spawn_objects_from_list_6 ; byte and range (object_part_pairs).
  LD A,(HL)                      ;
  AND $0F                        ;
  LD HL,object_part_pairs        ;
  CALL add_a_to_hl               ;
  LD A,(HL)                      ;
  LD (IY+$13),A                  ;
  INC HL                         ;
  LD A,(HL)                      ;
  LD (IY+$14),A                  ;
  LD (IY+$15),$80                ;
  LD (IY+$00),$1B                ;
spawn_objects_from_list_6:
  POP HL                  ; The object's index.
  LD DE,(box_x)           ;
  LD A,D                  ;
  OR (IY+$18)             ;
  LD (IY+$18),A           ;
  LD (IY+$19),E           ;
  LD (IY+$01),C           ; Column, y, x.
  LD A,(IX+$00)           ;
  AND $FC                 ;
  DEC A                   ;
  LD (IY+$05),A           ;
  LD A,(IX+$00)           ;
  AND $03                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  ADD A,A                 ;
  LD E,A                  ;
  LD A,(IX+$01)           ;
  AND $E0                 ;
  OR E                    ;
  LD (IY+$02),A           ;
spawn_objects_from_list_7:
  INC IX                          ; Next object.
  INC IX                          ;
  INC HL                          ;
  LD DE,(box_x)                   ;
  INC DE                          ;
  LD (box_x),DE                   ;
  POP BC                          ;
  DEC B                           ;
  JP NZ,spawn_objects_from_list_0 ;
  RET                             ;

; Object kinds: their colours
;
; One byte per kind (0-16), copied by the kind into the slot's +$0D by
; spawn_objects_from_list: the object's two colours, one in each nibble (BRIGHT
; in bit 3 and the ink in bits 0-2, and again in bits 7 and 4-6), which
; prepare_draw shows on alternate passes, so an object whose nibbles differ
; flashes between the two.
object_kind_bytes:
  DEFB $EE,$DD,$FF,$BB,$CD,$DF,$DD,$DD
  DEFB $DD,$DD,$DD,$FF,$AA,$CC,$EE,$FF
  DEFB $DD

; Address of a map cell
;
; Used by the routine at read_map_cell.
;
; Map rows are numbered from 1: row H is at landscape_map + (H - 1) * $60 (rows
; 0 and 1 give the same address; read_map_cell treats row 0 as empty). The
; column wraps: $FF becomes 95 and 96 becomes 0. C is left holding the wrapped
; column.
;
;   HL Column (L), row (H)
;   DE $60
; O:HL Address in the map
; O:C Column, wrapped
map_cell_address:
  LD C,L                  ; Row.
  LD A,H                  ;
  AND A                   ;
  LD B,H                  ;
  LD HL,landscape_map     ;
  JR Z,map_cell_address_1 ;
  DEC B                   ;
  JR Z,map_cell_address_1 ;
map_cell_address_0:
  ADD HL,DE               ;
  DJNZ map_cell_address_0 ;
map_cell_address_1:
  LD A,C                   ; Column, wrapped. (Never needed in the sessions.)
  CP $FF                   ;
  JR NZ,map_cell_address_2 ;
  LD C,$5F                 ;
map_cell_address_2:
  LD A,C                   ;
  CP $60                   ;
  JR NZ,map_cell_address_3 ;
  LD C,$00                 ;
map_cell_address_3:
  ADD HL,BC                ;
  RET

; Read a tile byte from the map
;
; Used by the routine at load_map_window.
;
; Returns the tile byte at (column, row), keeping the wrapped column in
; right_map_column. For the upper row 0 (above the map) it returns 0 (empty)
; and moves HL a row back so that HL + $60 is still the lower cell's.
;
;   HL Column (L), row (H)
; O:A Tile byte
read_map_cell:
  CALL map_cell_address
  LD A,C
  LD (right_map_column),A
  LD A,(view_row)
  AND A
  JR Z,read_map_cell_0
  LD A,(HL)
  RET
read_map_cell_0:
  AND A                   ; Row 0.
  SBC HL,DE               ;
  XOR A                   ;
  RET                     ;

; Flat-step blitter: lose the first column
;
; Where the first patched JR in blit_flat_step goes when flat_step_clip sets
; its displacement to $EE: the first step of a piece with start cap $10, off a
; byte boundary, is drawn from its second column (by way of the end of
; flat_blit_left_edge, which skips one graphic byte and repatches the JR for
; the following row pairs).
flat_blit_lose_first:
  INC L
  JP flat_blit_left_edge_1

; Draw a flat landscape step
;
; The blitter for directions 0 and 7 (flat floor and flat ceiling) at every
; fine position: all four entries in the fine-scroll tables point here. The
; step is three bytes wide and four rows tall (two row pairs); its graphic is
; twelve bytes in the order the zig-zag writes them -- the bottom row left to
; right, the row above right to left, and so on up. SP points at the graphic
; and each POP DE fetches two bytes.
;
; column_address is moved on three bytes for the next step. Then each row pair
; passes three JRs whose displacements flat_step_clip patches: all 0 draws
; every column; the first (here) can go to flat_blit_left_edge (start column 0
; or 31: from the second or third byte, at column 1), to flat_blit_lose_first,
; or, on later row pairs, straight into flat_blit_left_rows; the second
; (flat_blit_first_byte) to flat_blit_one_column (one column); the third
; (flat_blit_second_byte) to flat_blit_left_two (two columns). The cut paths
; POP the bytes they do not write, so SP stays in step with the graphic.
;
; Each row pair ends with the step up a pixel row, including the crossing of a
; character row, and DJNZ back to this JR (in flat_blit_upper_row); at the end
; it returns to the second instruction of after_step_drawn with column_address
; already set. An uncut row pair costs about 170 T-states and a whole step
; about 400 -- less than the deciding done for each step in landscape_step_loop
; and flat_step_clip.
;
; A Row pairs to draw
; HL Screen address, three bytes to the left
; SP The step's graphic
blit_flat_step:
  LD B,A                  ; The next step starts three bytes on.
  LD A,(column_address)   ;
  ADD A,$03               ;
  LD (column_address),A   ;
  INC L                   ; To the step's first column.
  INC L                   ;
  INC L                   ;
; This entry point is used by the routine at flat_blit_upper_row.
blit_flat_step_0:
  JR blit_flat_step_0     ; Patched: 0 to draw; $33, $EE or $4C for the cut
                          ; paths.

; Flat-step blitter: first column of the lower row
;
; Part of blit_flat_step.
flat_blit_first_byte:
  POP DE
  LD (HL),E
flat_blit_first_byte_0:
  JR flat_blit_first_byte_0 ; Patched: 0, or $24 to flat_blit_one_column.

; Flat-step blitter: second column of the lower row
;
; Part of blit_flat_step.
flat_blit_second_byte:
  INC L
  LD (HL),D
flat_blit_second_byte_0:
  JR flat_blit_second_byte_0 ; Patched: 0, or $26 to flat_blit_left_two.

; Flat-step blitter: third column, the upper row, and the step up
;
; Part of blit_flat_step: the third column of the lower row, one row up, then
; the upper row right to left; then up another pixel row with the fix-up for
; leaving a character row (H back up by 8 within the third, unless the
; character row was the third's top one), and the loop.
flat_blit_upper_row:
  INC L                   ; Third column, lower row.
  POP DE                  ;
  LD (HL),E               ;
  DEC H                   ; Row up; third, second, first columns.
  LD (HL),D               ; flat_blit_left_two joins at the second,
  DEC L                   ; flat_blit_one_column at the first.
; This entry point is used by the routine at flat_blit_left_two.
flat_blit_upper_row_0:
  POP DE
  LD (HL),E
  DEC L
; This entry point is used by the routine at flat_blit_one_column.
flat_blit_upper_row_1:
  LD (HL),D               ; }
; This entry point is used by the routines at flat_blit_left_two_rows and
; flat_blit_left_one_row.
flat_blit_upper_row_2:
  DEC H                       ; Up a pixel row, crossing a character row if
  LD A,H                      ; need be. flat_blit_left_two_rows and
  INC A                       ; flat_blit_left_one_row join here.
  AND $07                     ;
  JR NZ,flat_blit_upper_row_3 ;
  LD A,L                      ;
  SUB $20                     ;
  LD L,A                      ;
  JR C,flat_blit_upper_row_3  ;
  LD A,H                      ;
  ADD A,$08                   ;
  LD H,A                      ;
flat_blit_upper_row_3:
  DJNZ blit_flat_step_0   ; Next row pair (back to the patched JR in
                          ; blit_flat_step).
  JP after_step_drawn_0   ; Done: count the step and carry on.

; Flat-step blitter: one column only
;
; Reached by the JR in flat_blit_first_byte (displacement $24) after the first
; byte has been written: row up, two POPs to pass the rest of the row pair's
; graphic, and the upper row's first-column byte (D of the last pop), written
; by the last part of flat_blit_upper_row.
flat_blit_one_column:
  DEC H
  POP DE
  POP DE
  JP flat_blit_upper_row_1

; Draw only a flat piece's left two bytes
;
; One of the clipped ways through the flat-piece blitter blit_flat_step, for a
; piece that runs off the right-hand side of the play area with two of its
; three bytes still on it. flat_step_clip sends the loop here by patching the
; JR in flat_blit_second_byte (offset $26) when the piece's column (the low
; five bits of column_address) is $18. Each pair of rows is then drawn two
; bytes wide: the first row's third byte and the second row's right-hand byte
; are popped and dropped, so SP stays in step with the graphic.
flat_blit_left_two:
  DEC H                   ; Up a pixel row: the first row's third byte is not
                          ; drawn
  POP DE                  ; Drop it, and the second row's right-hand byte with
                          ; it
  JP flat_blit_upper_row_0 ; Draw the second row's other two bytes, right to
                           ; left, and go round the loop

; Start a flat piece that crosses the left-hand side of the play area
;
; The first pass of the flat-piece blitter blit_flat_step comes here when
; flat_step_clip has patched the JR at the top of its loop to offset $33 (the
; piece's column is $1C or $1D, that is, it begins just left of the play area,
; whose columns are 1 to 28). It keeps the column saved for the next piece
; (column_address) in the right character row, moves L to column 1 of the row,
; and patches the loop's JR again (offset $4C) so that every later pair of rows
; goes straight to flat_blit_left_rows's second instruction. Then it skips the
; graphic's first byte, and through the JR flat_step_clip also patched (0 or 1)
; maybe a second one, so that only the bytes that lie on the play area are
; drawn.
;
; Column $1D leaves two bytes of each row to draw (flat_blit_left_two_rows),
; column $1C one (flat_blit_left_one_row). The POPs that follow are one byte
; out of step with the graphic's pairs, which is why each row's last write and
; the drops in those two routines look lopsided.
;
; A the column kept for the next piece, low byte (column_address after the
;   blitter added 3)
; HL the screen address of the piece's first byte
flat_blit_left_edge:
  INC A                       ; Has the next piece's column come round to the
  AND $1F                     ; start of a character row?
  JR NZ,flat_blit_left_edge_0 ;
  LD A,L                  ; If so, take it back into this one
  SUB $20                 ;
  LD (column_address),A   ;
flat_blit_left_edge_0:
  LD A,L                  ; L to column 1 of this pixel row
  AND $E0                 ;
  INC A                   ;
  LD L,A                  ;
; This entry point is used by the routine at flat_blit_lose_first.
flat_blit_left_edge_1:
  LD A,$4C                ; From now on the loop's JR goes to
  LD ($E25A),A            ; flat_blit_left_rows + 1 (offset $4C)
  INC SP                  ; Skip the graphic's first byte
flat_blit_left_edge_2:
  JR flat_blit_left_edge_2 ; JR patched by flat_step_clip: 0 skips a second
                           ; byte at flat_blit_left_rows, 1 does not

; Draw a flat piece's rows that lie right of the play area's left-hand side
;
; Part of flat_blit_left_edge's clipped path. The INC SP here is executed only
; on the first pass, and only when flat_step_clip left the JR before it at 0
; (the piece starts two bytes left of the play area); every later pass of the
; blitter's loop enters at the POP. The JR at the end is patched by
; flat_step_clip too: 0 goes on to draw two bytes (flat_blit_left_two_rows),
; $0B one (flat_blit_left_one_row).
flat_blit_left_rows:
  INC SP                  ; Skip another byte of the graphic
  POP DE                  ; The first visible byte of the row, at column 1
  LD (HL),E               ;
flat_blit_left_rows_0:
  JR flat_blit_left_rows_0 ; JR patched by flat_step_clip: 0 for two bytes, $0B
                           ; for one

; Draw a pair of rows two bytes wide at the play area's left-hand side
;
; For a flat piece that begins one byte left of the play area: draws the second
; byte of the first row next to the one flat_blit_left_rows drew, the second
; row's two right-hand bytes back the other way, and drops the second row's
; left-hand byte together with the first byte of the next pair (the POPs run
; one byte out of step with the graphic since flat_blit_left_edge's INC SP).
flat_blit_left_two_rows:
  INC L                   ; The first row's other byte, at column 2
  LD (HL),D               ;
  POP DE                  ; Up a row; the second row's two bytes, right to left
  DEC H                   ;
  LD (HL),E               ;
  DEC L                   ;
  LD (HL),D               ;
  POP DE                  ; Drop the second row's left-hand byte and the next
                          ; row's first
  JP flat_blit_upper_row_2 ; Up a row, the character-row fix-up and the loop

; Draw a pair of rows one byte wide at the play area's left-hand side
;
; For a flat piece that begins two bytes left of the play area: one byte of
; each row is drawn, at column 1, and the rest popped and dropped.
flat_blit_left_one_row:
  DEC H                   ; Up a row: the second row's right-hand byte, at
  LD (HL),D               ; column 1
  POP DE                  ; Drop the second row's other two bytes and the next
  POP DE                  ; row's first two
  JP flat_blit_upper_row_2 ; Up a row, the character-row fix-up and the loop

; Landscape pieces: blitters and graphics at a shift of 2 pixels
;
; The first of four tables of 16 entries, one per piece direction (the high
; nibble of the piece's direction-and-steps byte in its tile record, kept in
; (IX+3)), chosen by draw_landscape from the fine scroll position and kept in
; shift_table: this one for a shift of 2 pixels, piece_blitters_shift4 for 4,
; piece_blitters_shift6 for 6 and piece_blitters_shift0 for 0. Each entry is
; two words: the blitter that draws a step of the piece, which
; landscape_step_loop loads into IY and step_dispatch jumps to, and the step's
; graphic at that shift, which it keeps in piece_graphic and starts SP from.
;
; The directions: 0 is flat, three bytes wide; 1, 2 and 3 are slopes that climb
; to the right (8 rows over 16 pixels, 8 rows over 8, 16 rows over 8), 4, 5 and
; 6 the same three climbing to the left; 7 to 13 are 0 to 6 upside down
; (ceilings: rock above, sky below), and 14 and 15 are a 16-row wall facing
; either way. Each graphic holds three variants of the step one above the other
; (9 or 17 rows each; 28 rows for a flat step); landscape_step_loop picks one
; and the count of row pairs comes in A.
;
; Only nine graphics are stored: a step climbing to the left is its partner's
; graphic started one row later and drawn by a blitter that mirrors each byte
; through the bit-reversed table at bit_reverse_table (blit_mirror_two,
; blit_mirror_one). That works because the graphics zig-zag: each pair of rows
; is stored left to right and then right to left, so read one row later every
; row's bytes come in the other order. A mirrored shifted step uses the
; partner's graphic at the opposite shift: a slope climbing left at a shift of
; 2 is the right-climbing one at 6, mirrored.
;
; The blitters: blit_flat_step for the flat steps at every shift; at shift 0
; blit_two (two bytes wide), blit_one (one byte), and their mirrored twins
; blit_mirror_two (entered one DEC L early, at the end of blit_mirror_three)
; and blit_mirror_one; at shifts 2 to 6, one byte wider, blit_three (entered at
; its second INC L), blit_two (at its second INC L) and the mirrored
; blit_mirror_three (at its second DEC L) and blit_mirror_two. The walls
; (directions 14 and 15) enter one instruction later than the slopes, skipping
; the INC L or DEC L that places a slope in its column.
piece_blitters_shift2:
  DEFW blit_flat_step     ; Shift 2: directions 0 (flat) and 1 (climbs right,
  DEFW flat_piece_shifted ; 16 pixels)
  DEFW $E4A8              ;
  DEFW slope_1_shifted    ;
  DEFW $E4D0              ; Directions 2 and 3 (climb right, 8 pixels: 8 and 16
  DEFW slope_2_shifted    ; rows)
  DEFW $E4D0              ;
  DEFW slope_3_shifted    ;
  DEFW $E4F2              ; Directions 4 and 5 (climb left: mirrors of 1 and 2)
  DEFW $E8C1              ;
  DEFW blit_mirror_two    ;
  DEFW $E7C8              ;
  DEFW blit_mirror_two        ; Directions 6 (mirror of 3) and 7 (flat, upside
  DEFW $E9FC                  ; down)
  DEFW blit_flat_step         ;
  DEFW flat_piece_two_shifted ;
  DEFW $E4A8              ; Directions 8 and 9 (1 and 2 upside down)
  DEFW slope_8_shifted    ;
  DEFW $E4D0              ;
  DEFW slope_9_shifted    ;
  DEFW $E4D0              ; Directions 10 and 11 (3 and 4 upside down)
  DEFW slope_10_shifted   ;
  DEFW $E4F2              ;
  DEFW $EF57              ;
  DEFW blit_mirror_two    ; Directions 12 and 13 (5 and 6 upside down)
  DEFW $EE5E              ;
  DEFW blit_mirror_two    ;
  DEFW $F08F              ;
  DEFW $E4D1              ; Directions 14 and 15 (wall, and its mirror)
  DEFW wall_shifted       ;
  DEFW $E52D              ;
  DEFW $ED74              ;

; Landscape pieces: blitters and graphics at a shift of 4 pixels
;
; Laid out as piece_blitters_shift2: 16 entries, a blitter and a graphic for
; each piece direction.
piece_blitters_shift4:
  DEFW blit_flat_step     ; Shift 4: directions 0 and 1
  DEFW $EABC              ;
  DEFW $E4A8              ;
  DEFW $E861              ;
  DEFW $E4D0              ; Directions 2 and 3
  DEFW $E788              ;
  DEFW $E4D0              ;
  DEFW $E98C              ;
  DEFW $E4F2              ; Directions 4 and 5
  DEFW $E864              ;
  DEFW blit_mirror_two    ;
  DEFW $E78A              ;
  DEFW blit_mirror_two    ; Directions 6 and 7
  DEFW $E98E              ;
  DEFW blit_flat_step     ;
  DEFW $EBB8              ;
  DEFW $E4A8              ; Directions 8 and 9
  DEFW $EEF7              ;
  DEFW $E4D0              ;
  DEFW $EE1E              ;
  DEFW $E4D0              ; Directions 10 and 11
  DEFW $F01F              ;
  DEFW $E4F2              ;
  DEFW $EEFA              ;
  DEFW blit_mirror_two    ; Directions 12 and 13
  DEFW $EE20              ;
  DEFW blit_mirror_two    ;
  DEFW $F021              ;
  DEFW $E4D1              ; Directions 14 and 15
  DEFW $ED04              ;
  DEFW $E52D              ;
  DEFW $ED06              ;

; Landscape pieces: blitters and graphics at a shift of 6 pixels
;
; Laid out as piece_blitters_shift2: 16 entries, a blitter and a graphic for
; each piece direction.
piece_blitters_shift6:
  DEFW blit_flat_step     ; Shift 6: directions 0 and 1
  DEFW $EB10              ;
  DEFW $E4A8              ;
  DEFW $E8BE              ;
  DEFW $E4D0              ; Directions 2 and 3
  DEFW $E7C6              ;
  DEFW $E4D0              ;
  DEFW $E9FA              ;
  DEFW $E4F2              ; Directions 4 and 5
  DEFW $E807              ;
  DEFW blit_mirror_two    ;
  DEFW $E74C              ;
  DEFW blit_mirror_two    ; Directions 6 and 7
  DEFW $E920              ;
  DEFW blit_flat_step     ;
  DEFW $EC0C              ;
  DEFW $E4A8              ; Directions 8 and 9
  DEFW $EF54              ;
  DEFW $E4D0              ;
  DEFW $EE5C              ;
  DEFW $E4D0              ; Directions 10 and 11
  DEFW $F08D              ;
  DEFW $E4F2              ;
  DEFW $EE9D              ;
  DEFW blit_mirror_two    ; Directions 12 and 13
  DEFW $EDE2              ;
  DEFW blit_mirror_two    ;
  DEFW $EFB3              ;
  DEFW $E4D1              ; Directions 14 and 15
  DEFW $ED72              ;
  DEFW $E52D              ;
  DEFW $EC98              ;

; Landscape pieces: blitters and graphics at a shift of 0 pixels
;
; Laid out as piece_blitters_shift2: 16 entries, a blitter and a graphic for
; each piece direction.
piece_blitters_shift0:
  DEFW blit_flat_step     ; Shift 0: directions 0 and 1
  DEFW $E583              ;
  DEFW blit_two           ;
  DEFW $E6A5              ;
  DEFW blit_one           ; Directions 2 and 3
  DEFW $E64E              ;
  DEFW blit_one           ;
  DEFW $E716              ;
  DEFW $E52B              ; Directions 4 and 5
  DEFW $E6A7              ;
  DEFW blit_mirror_one    ;
  DEFW $E64F              ;
  DEFW blit_mirror_one    ; Directions 6 and 7
  DEFW $E717              ;
  DEFW blit_flat_step     ;
  DEFW $E5D9              ;
  DEFW blit_two           ; Directions 8 and 9
  DEFW $E66C              ;
  DEFW blit_one           ;
  DEFW $E630              ;
  DEFW blit_one           ; Directions 10 and 11
  DEFW $E6E0              ;
  DEFW $E52B              ;
  DEFW $E66E              ;
  DEFW blit_mirror_one    ; Directions 12 and 13
  DEFW $E631              ;
  DEFW blit_mirror_one    ;
  DEFW $E6E1              ;
  DEFW $E48C              ; Directions 14 and 15
  DEFW $EC63              ;
  DEFW $E55C              ;
  DEFW $EC64              ;

; Terrain piece shapes
;
; Nine records of three bytes, read by set_piece_shape, which copies one to
; piece_dx, piece_dcolumn and piece_rise for the piece being drawn: its run in
; pixels (negative for a piece that climbs to the left; 0 for a wall), its
; width in bytes signed the same way (also the step from one column of the
; ground fill to the next, fill_under_slope), and its rise in pixel rows (0 for
; a flat piece). Directions 0 to 6 (the high nibble of the map byte) use
; records 0 to 6, directions 7 to 15 records 0 to 8.
piece_shapes:
  DEFB $18,$03,$00        ; 0: flat, 24 pixels, 3 bytes
  DEFB $10,$02,$08        ; 1: climbs right, 16 pixels, 2 bytes, 8 rows
  DEFB $08,$01,$08        ; 2: climbs right, 8 pixels, 1 byte, 8 rows
  DEFB $08,$01,$10        ; 3: climbs right, 8 pixels, 1 byte, 16 rows
  DEFB $F0,$FE,$08        ; 4: climbs left, mirror of 1
  DEFB $F8,$FF,$08        ; 5: climbs left, mirror of 2
  DEFB $F8,$FF,$10        ; 6: climbs left, mirror of 3
  DEFB $00,$01,$10        ; 7 (direction 14): a wall, 16 rows
  DEFB $00,$FF,$10        ; 8 (direction 15): the wall, mirrored

; Look up a terrain piece's shape
;
; Used by the routines at load_piece_steps and piece_ends.
;
; Called by load_piece_steps and piece_ends with the piece in hand. Copies its
; direction's three bytes from piece_shapes to piece_dx (run in pixels),
; piece_dcolumn (width in bytes) and piece_rise (rise in rows); directions 7
; and up (the ceilings, and the walls) use the records seven lower and set
; piece_ceiling to $80. Then sets direction_flag and the displacement of the JR
; at the end of landscape_step_loop to match: for a piece that climbs to the
; right (width positive) direction_flag is $FF and the JR skips the NEG at
; slope_step_at_right_edge; for one that climbs to the left it is 1 and the NEG
; is done.
;
; IX the collision-box list of the map cell being drawn (see draw_landscape);
;    (IX+3) the piece's direction
set_piece_shape:
  LD HL,piece_shapes      ; The shape records
  LD A,(IX+$03)           ; Directions 0 to 6 go straight to their record
  CP $07                  ;
  JR C,set_piece_shape_0  ;
  LD A,$80                ; Directions 7 to 15: say so, and use records 0 to 8
  LD (piece_ceiling),A    ;
  LD A,(IX+$03)           ;
  SUB $07                 ;
set_piece_shape_0:
  LD E,A                  ; Three bytes a record
  ADD A,A                 ;
  ADD A,E                 ;
  LD E,A                  ;
  LD D,$00                ;
  ADD HL,DE               ;
  LD A,(HL)               ; Run, width and rise
  LD (piece_dx),A         ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (piece_dcolumn),A    ;
  INC HL                  ;
  LD A,(HL)               ;
  LD (piece_rise),A       ;
  LD A,$01                ; Say it climbs to the left: direction 1, the NEG
  LD (direction_flag),A   ; done
  XOR A                   ;
  LD ($D463),A            ;
  LD A,(piece_dcolumn)
  BIT 7,A                 ; It does if the width is negative
  RET NZ                  ;
  LD A,$FF                ; Climbs to the right: $FF, and the JR jumps over the
  LD (direction_flag),A   ; NEG
  LD A,$02                ;
  LD ($D463),A            ;
  RET                     ;

; The current terrain piece's direction, and a flag of the collision-box
; builder's
;
; For the landscape: $FF for a piece that climbs to the right (directions 0 to
; 3, 7 to 10, 14), 1 for one that climbs to the left. Set by set_piece_shape,
; turned round by fill_behind_moving_piece when a piece reverses, and read
; throughout the landscape drawing (landscape_step_loop,
; slope_step_at_right_edge, after_step_drawn and others).
;
; place_collision_box borrows it as well: set to 1 there, it says the part to
; the right is still to test, and next_part clears it when it does so. The code
; map took it for an unused byte.
direction_flag:
  DEFB $00

; Fill the ground under a terrain piece, column by column
;
; Used by the routines at after_step_drawn and fill_flat_end_column.
;
; Called by after_step_drawn once a piece is drawn, to fill what lies beneath
; it with the ground's checkerboard (DE = $55AA) or to clear it (DE = 0), and
; by fill_flat_end_column to clear a column four rows deep. Each column is one
; byte wide and climbs from HL for C pairs of rows: D is written on one pixel
; row and E on the row above. After each column L moves on by the piece's width
; in bytes (piece_dcolumn), and piece_page_y, the screen row the fill has
; reached, goes up by the column's height. B, the number of columns, is the
; number of steps of the piece drawn (piece_steps_done), so this is one column
; a step, each the step's width in bytes across and as tall as the fill needs,
; which is how the ground under a slope comes out solid.
;
; A column is clipped to the play area: one in screen column 0 or 29 to 31 is
; not drawn at all (the pointer is only moved on); if the fill starts below row
; 183 ($B7, the bottom of the play area) the first rows are dropped and it
; starts at the bottom row of the play area; and near the top it is cut short
; by what the sum with piece_rows (the piece's height) and 24 says. The
; arithmetic of the top clip is read but not traced through.
;
; B the number of columns
; C' pairs of rows in each column
; DE' the two pattern bytes ($55AA for the ground's checkerboard, 0 to clear)
; HL' the screen address of the first column's bottom byte
fill_under_slope:
  EXX                     ; Into the other set: B counts this column's pairs of
  LD B,C                  ; rows
  LD A,L                   ; Off the play area (column 0, or 29 and up)? Only
  AND $1F                  ; move on
  DEC A                    ;
  CP $1C                   ;
  JR NC,fill_under_slope_3 ;
  LD A,(piece_page_y)     ; Does the fill start below the play area (row 184 or
  CP $B8                  ; more)?
  JR C,fill_under_slope_0 ;
  JR Z,fill_under_slope_0 ;
  SUB $B7                 ; Then drop the rows that would lie below it
  SRL A                   ;
  NEG                     ;
  ADD A,B                 ;
  LD B,A                  ;
  LD H,$57                ; And start on the play area's bottom row, $57C0 +
  LD A,L                  ; the column
  AND $1F                 ;
  OR $C0                  ;
  LD L,A                  ;
  JP fill_under_slope_1   ;
fill_under_slope_0:
  LD B,A                   ; Will the column reach above the top of the play
  LD A,(piece_rows)        ; area?
  NEG                      ;
  ADD A,B                  ;
  SUB $18                  ;
  LD B,C                   ;
  JR NC,fill_under_slope_1 ;
  CPL                     ; Then shorten it
  SRL A                   ;
  NEG                     ;
  ADD A,B                 ;
  LD B,A                  ;
fill_under_slope_1:
  LD A,(piece_page_y)     ; piece_page_y goes up by the column's height (two
  SLA C                   ; rows a pair)
  SUB C                   ;
  SRL C                   ;
  LD (piece_page_y),A     ;
fill_under_slope_2:
  LD (HL),D               ; Fill the column: D, up a row, E, up a row
  DEC H                   ;
  LD (HL),E               ;
  CALL line_up_hl         ;
  DJNZ fill_under_slope_2 ;
  LD A,(piece_dcolumn)    ; On to the next column, by the piece's width in
  ADD A,L                 ; bytes
  LD L,A                  ;
  EXX                     ; The next column
  DJNZ fill_under_slope   ;
  RET                     ;
fill_under_slope_3:
  LD A,(piece_page_y)     ; A column off the play area: account for its height
  SLA B                   ; and step across
  SUB B                   ;
  SRL B                   ;
  LD (piece_page_y),A     ;
  LD A,(piece_dcolumn)    ;
  ADD A,L                 ;
  LD L,A                  ;
fill_under_slope_4:
  DEC H                   ; Move HL up by its height without drawing
  CALL line_up_hl         ;
  DJNZ fill_under_slope_4 ;
  EXX                     ; The next column
  DJNZ fill_under_slope   ;
  RET                     ;

; Draw a one-byte-wide terrain step
;
; The blitter for the one-byte-wide slopes at shift 0: directions 2, 3, 9 and
; 10 here, the wall (direction 14) one instruction on, skipping the INC L. Two
; rows a POP, one byte a row, climbing the screen with DEC H; about 75 T-states
; a pair of rows. The right-climbing twin of blit_mirror_one.
;
; A the number of pairs of rows
; HL the screen address of the bottom row, less one
; SP the graphic (at the variant to draw)
blit_one:
  INC L                   ; Step right into the column; B counts the pairs of
  LD B,A                  ; rows; pop two bytes
blit_one_0:
  POP DE                  ;
  LD (HL),E               ; One on this row, up a row, the other on the row
  DEC H                   ; above, up a row
  LD (HL),D               ;
  DEC H                   ;
  LD A,H                  ; Into the next character row up if this one is done;
  INC A                   ; the next pair; leave HL as the column for the next
  AND $07                 ; step (after_step_drawn)
  JR NZ,blit_one_1        ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_one_1         ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_one_1:
  DJNZ blit_one_0         ;
  JP after_step_drawn     ;

; Draw a three-byte-wide terrain step
;
; The blitter for the two-byte-wide slopes shifted by 2, 4 or 6 pixels, which
; are three bytes wide: directions 1 and 8, entered at the second INC L (the
; first is not used by the tables). Each pair of rows is three POPs: a row left
; to right, up a row, a row right to left; about 125 T-states a pair.
;
; A the number of pairs of rows
; HL the screen address of the bottom row's first byte, less two
; SP the graphic
blit_three:
  INC L                   ; Step right into the column; B counts the pairs of
  INC L                   ; rows
  INC L                   ;
  LD B,A                  ;
blit_three_0:
  POP DE                  ; Pop two bytes: the row's first two, left to right
  LD (HL),E               ;
  INC L                   ;
  LD (HL),D               ;
  INC L                   ;
  POP DE                  ; Pop two more: the row's third byte; up a row; the
  LD (HL),E               ; next row's right-hand byte
  DEC H                   ;
  LD (HL),D               ;
  DEC L                   ;
  POP DE                  ; Pop two more: that row's other two bytes, right to
  LD (HL),E               ; left; up a row
  DEC L                   ;
  LD (HL),D               ;
  DEC H                   ;
  LD A,H                  ; Into the next character row up if this one is done;
  INC A                   ; the next pair; leave HL as the column for the next
  AND $07                 ; step
  JR NZ,blit_three_1      ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_three_1       ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_three_1:
  DJNZ blit_three_0       ;
  JP after_step_drawn     ;

; Draw a two-byte-wide terrain step
;
; Directions 1 and 8 at shift 0 (here), the one-byte slopes shifted by 2, 4 or
; 6 (directions 2, 3, 9 and 10, at the second INC L), and the wall shifted
; (direction 14, at the LD B,A). Two POPs a pair of rows: a row left to right,
; up, a row right to left, up; about 105 T-states a pair.
;
; A the number of pairs of rows
; HL the screen address of the bottom row's first byte, less one or two
; SP the graphic
blit_two:
  INC L                   ; Step right into the column; B counts the pairs of
  INC L                   ; rows
  LD B,A                  ;
blit_two_0:
  POP DE                  ; Pop two bytes: a row left to right; up a row
  LD (HL),E               ;
  INC L                   ;
  LD (HL),D               ;
  DEC H                   ;
  POP DE                  ; Pop two more: the row above, right to left; up a
  LD (HL),E               ; row
  DEC L                   ;
  LD (HL),D               ;
  DEC H                   ;
  LD A,H                  ; Into the next character row up if this one is done;
  INC A                   ; the next pair; leave HL as the column for the next
  AND $07                 ; step
  JR NZ,blit_two_1        ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_two_1         ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_two_1:
  DJNZ blit_two_0         ;
  JP after_step_drawn     ;

; Draw a three-byte-wide terrain step, mirrored
;
; Directions 4 and 11 shifted by 2, 4 or 6 pixels, entered at the second DEC L
; (the first is not used by the tables). As blit_three, but each byte goes
; through the bit-reversed table at bit_reverse_table, and the graphic is the
; partner's at the opposite shift, started one row on. The last DEC L is the
; entry for directions 4 and 11 at shift 0 into blit_mirror_two.
;
; A the number of pairs of rows
; HL the screen address, plus two
; SP the graphic
blit_mirror_three:
  DEC L                   ; Step left into the column
  DEC L                   ;
  DEC L                   ;
  LD B,$F9                ; B' holds the table's page, B the count; (the loop)
  EXX                     ; pop two bytes and mirror the first
  LD B,A                  ;
blit_mirror_three_0:
  EXX                     ;
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ; It goes on the left; the second
  INC L                   ;
  LD C,D                  ;
  LD A,(BC)               ; Mirrored on its right; pop two more and mirror the
  LD (HL),A               ; first
  INC L                   ;
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ; The row's third byte; up a row; the next
  DEC H                   ;
  LD C,D                  ;
  LD A,(BC)               ; Mirrored, right to left; pop two more and mirror
  LD (HL),A               ; the first
  DEC L                   ;
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ; Leftwards; the last of the row
  DEC L                   ;
  LD C,D                  ;
  LD A,(BC)                 ; Mirrored; up a row; into the next character row
  LD (HL),A                 ; up if this one is done; the next pair; leave HL
  DEC H                     ; as the column for the next step. The last DEC L
  LD A,H                    ; is the entry for directions 4 and 11 at shift 0
  INC A                     ; into blit_mirror_two
  AND $07                   ;
  JR NZ,blit_mirror_three_1 ;
  LD A,L                    ;
  SUB $20                   ;
  LD L,A                    ;
  JR C,blit_mirror_three_1  ;
  LD A,H                    ;
  ADD A,$08                 ;
  LD H,A                    ;
blit_mirror_three_1:
  EXX                       ;
  DJNZ blit_mirror_three_0  ;
  EXX                       ;
  JP after_step_drawn       ;
  DEC L                     ;

; Draw a two-byte-wide terrain piece, mirrored
;
; The blitter for the pieces that climb to the left and are two bytes wide:
; directions 4 and 11 at shift 0 (entered one DEC L earlier, at the end of
; blit_mirror_three), directions 5, 6, 12 and 13 at shifts 2 to 6 (here), and
; the wall direction 15 at those shifts (at blit_mirror_two + 1, skipping the
; DEC L). It draws like the two-byte blitter at blit_two -- POP DE, two bytes
; left to right, up a row, two bytes right to left, up a row -- but each byte
; goes through the bit-reversed table at bit_reverse_table (built by
; build_bit_reverse), so the graphic comes out mirrored. With the graphic
; started one row later than its right-climbing partner's (see
; piece_blitters_shift2), each row's bytes arrive in the mirrored order too.
;
; The table lookups make it about 40 T-states a pair of rows slower than the
; plain blitter: about 150 T-states for two rows of two bytes, against about
; 105.
;
; A the number of pairs of rows
; HL the screen address of the bottom row's right-hand byte, plus one
; SP the piece's graphic (at the variant to draw)
blit_mirror_two:
  DEC L                   ; Step left into the piece's column
  LD B,$F9                ; B' holds the table's page; B the count
  EXX                     ;
  LD B,A                  ;
blit_mirror_two_0:
  EXX                     ; Pop two bytes; the first, mirrored, on the left
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  INC L                   ; The second, mirrored, on its right
  LD C,D                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ; Up a row
  POP DE                  ; Pop two more: the first on the right
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC L                   ; The second on the left
  LD C,D                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ; Up a row
  LD A,H                  ; Into the next character row up if this one is done
  INC A                   ;
  AND $07                 ;
  JR NZ,blit_mirror_two_1 ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_mirror_two_1  ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_mirror_two_1:
  EXX                     ; The next pair of rows
  DJNZ blit_mirror_two_0  ;
  EXX                     ; Leave HL as the column for the next piece
  JP after_step_drawn     ; (after_step_drawn)

; Draw a one-byte-wide terrain piece, mirrored
;
; The blitter for the one-byte-wide pieces that climb to the left at shift 0:
; directions 5, 6, 12 and 13 here, and the wall direction 15 at blit_mirror_one
; + 1 (skipping the DEC L). Each popped byte is mirrored through the
; bit-reversed table at bit_reverse_table on its way to the screen; one byte a
; row, two rows a POP. The right-climbing twin is the blitter at blit_one,
; which the code map left as data.
;
; A the number of pairs of rows
; HL the screen address of the bottom row's byte, plus one
; SP the piece's graphic (at the variant to draw)
blit_mirror_one:
  DEC L                   ; Step left into the piece's column
  LD B,$F9                ; B' holds the table's page; B the count
  EXX                     ;
  LD B,A                  ;
blit_mirror_one_0:
  EXX                     ; Pop two bytes: the first, mirrored, then up a row
  POP DE                  ;
  LD C,E                  ;
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ;
  LD C,D                  ; The second, mirrored, then up a row
  LD A,(BC)               ;
  LD (HL),A               ;
  DEC H                   ;
  LD A,H                  ; Into the next character row up if this one is done
  INC A                   ;
  AND $07                 ;
  JR NZ,blit_mirror_one_1 ;
  LD A,L                  ;
  SUB $20                 ;
  LD L,A                  ;
  JR C,blit_mirror_one_1  ;
  LD A,H                  ;
  ADD A,$08               ;
  LD H,A                  ;
blit_mirror_one_1:
  EXX                     ; The next pair of rows
  DJNZ blit_mirror_one_0  ;
  EXX                     ; Leave HL as the column for the next piece
  JP after_step_drawn     ; (after_step_drawn)

; Landscape graphics: the flat piece (direction 0)
;
; Its three variants and its four end caps, each drawn as one step of direction
; 0 at shift 0 by the blitter piece_blitters_shift0 gives it (its clipping
; jumps set to draw every column, as flat_step_clip sets them for a step clear
; of the edges), in planet 0's surface colour (four times the size).
;
; The terrain pieces' graphics as they came on the tape: the nine stored shapes
; unshifted (to the end of slope_3_gfx), each after a header, and the room
; after them for their copies shifted by 2, 4 and 6 pixels, which
; build_shifted_pieces makes at the start (from slope_2_shifted). The tables at
; piece_blitters_shift2 point into them. All are drawn bottom row first by the
; POP blitters, and stored as the blitters read them: each pair of rows left to
; right, then right to left. Every graphic holds three variants of its piece
; one above the other.
;
; This one: a two-byte header for shift_flat_piece, then the flat piece, three
; bytes a row, 28 rows (84 bytes). The ground is the planet's checkerboard ($55
; and $AA on alternate rows) with the surface's bumps at the top.
flat_piece_gfx:
  DEFB $06,$02            ; Header: pairs of rows to make shifted (6), width in
                          ; bytes less one (2)
  DEFB $55,$55,$55        ; The flat piece, 28 rows of 3 bytes
  DEFB $AA,$AA,$AA        ;
  DEFB $7E,$FD,$77        ;
  DEFB $C1,$63,$B5        ;
  DEFB $55,$55,$55        ;
  DEFB $AA,$AE,$EB        ;
  DEFB $F7,$FF,$77        ;
  DEFB $C1,$E1,$8C        ;
  DEFB $55,$55,$55        ;
  DEFB $AA,$BE,$EA        ;
  DEFB $FD,$7F,$77        ;
  DEFB $C1,$C3,$87        ;
  DEFB $00,$F5,$55        ;
  DEFB $AA,$7A,$00        ;
  DEFB $00,$0F,$77        ;
  DEFB $C0,$01,$00        ;
  DEFB $D5,$55,$55        ;
  DEFB $AA,$BA,$EA        ;
  DEFB $7F,$7F,$77        ;
  DEFB $C1,$E3,$1D        ;
  DEFB $55,$57,$00        ;
  DEFB $00,$FC,$AA        ;
  DEFB $77,$E0,$00        ;
  DEFB $00,$C0,$C1        ;
  DEFB $55,$55,$55        ;
  DEFB $AB,$EA,$AA        ;
  DEFB $77,$FF,$FC        ;
  DEFB $78,$DC,$C1        ;

; Landscape graphics: the flat ceiling (direction 7)
;
; Its three variants and its four end caps, each drawn as one step of direction
; 7 at shift 0 by the blitter piece_blitters_shift0 gives it (its clipping
; jumps set to draw every column, as flat_step_clip sets them for a step clear
; of the edges), in planet 0's surface colour (four times the size).
;
; A two-byte header for shift_flat_piece as at flat_piece_gfx, then 28 rows of
; 3 bytes.
flat_piece_two_gfx:
  DEFB $06,$02            ; Header: pairs of rows to make shifted (6), width
                          ; less one (2)
  DEFB $F8,$00,$0F        ; The piece, 28 rows of 3 bytes
  DEFB $FA,$01,$BE        ;
  DEFB $55,$87,$55        ;
  DEFB $AA,$FE,$AA        ;
  DEFB $07,$FE,$0F        ;
  DEFB $FA,$AB,$FF        ;
  DEFB $55,$55,$55        ;
  DEFB $AA,$AA,$AA        ;
  DEFB $C0,$00,$0F        ;
  DEFB $FA,$F9,$F0        ;
  DEFB $5F,$DF,$55        ;
  DEFB $AA,$AA,$AA        ;
  DEFB $00,$00,$0F        ;
  DEFB $FA,$0F,$00        ;
  DEFB $00,$7D,$55        ;
  DEFB $AA,$EA,$00        ;
  DEFB $3F,$00,$0F        ;
  DEFB $FA,$F9,$6B        ;
  DEFB $D5,$5F,$55        ;
  DEFB $AA,$AA,$AA        ;
  DEFB $0F,$80,$00        ;
  DEFB $00,$F0,$FA        ;
  DEFB $55,$5C,$00        ;
  DEFB $00,$AF,$AA        ;
  DEFB $0F,$87,$C0        ;
  DEFB $FC,$FE,$FA        ;
  DEFB $55,$55,$57        ;
  DEFB $AB,$AA,$AA        ;

; Landscape graphics: direction 9, direction 2 upside down, one byte wide
;
; Its three variants, each drawn as one step of direction 9 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; A three-byte header for read_piece_header (rows in each variant, halved and
; rounded up: 5; pairs of rows to make: 17; width in bytes: 1), then three
; variants of 9 rows, one byte a row (27 bytes). Direction 12 is this graphic
; one byte (one row) on, mirrored. The shifting at the start reads on past the
; end into the next graphic, for a fourth, partial variant that is never drawn.
slope_9_gfx:
  DEFB $05,$11,$01        ; Header
  DEFB $80,$E0,$78,$BC,$56,$AA,$57,$AB,$55 ; Three variants of 9 rows
  DEFB $80,$80,$C0,$C0,$60,$B0,$5B,$AF,$57 ;
  DEFB $80,$80,$D8,$FC,$54,$AE,$57,$AB,$57 ;

; Landscape graphics: direction 2, a slope one byte wide, 9 rows
;
; Its three variants, each drawn as one step of direction 2 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (5, 17, 1: see slope_9_gfx), then three variants of 9 rows of one
; byte. Direction 5 is this graphic one byte on, mirrored.
slope_2_gfx:
  DEFB $05,$11,$01        ; Header
  DEFB $D5,$EA,$55,$6A,$3D,$1E,$07,$01,$01 ; Three variants of 9 rows
  DEFB $F5,$DA,$0D,$06,$03,$03,$01,$01,$01 ;
  DEFB $D5,$EA,$75,$2A,$3F,$1B,$01,$01,$01 ;

; Landscape graphics: direction 8, direction 1 upside down, two bytes wide
;
; Its three variants, each drawn as one step of direction 8 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (5, 17, 2), then three variants of 9 rows of two bytes (54 bytes).
; Direction 11 is this graphic one row (two bytes) on, mirrored.
slope_8_gfx:
  DEFB $05,$11,$02        ; Header
  DEFB $C0,$00,$00,$F0,$5F,$00 ; Three variants of 9 rows (a pair of rows and a
  DEFB $C0,$AB,$55,$70,$B8,$AA ; half a line)
  DEFB $55,$5E,$AB,$AA,$55,$53 ;
  DEFB $C0,$00,$00,$FC,$5E,$00 ;
  DEFB $00,$AB,$55,$80,$C0,$AA ;
  DEFB $55,$78,$AF,$AA,$55,$57 ;
  DEFB $80,$00,$00,$E0,$71,$00 ;
  DEFB $88,$BB,$5F,$D8,$FC,$AE ;
  DEFB $55,$F6,$AB,$AA,$55,$55 ;

; Landscape graphics: direction 1, a slope two bytes wide, 9 rows
;
; Its three variants, each drawn as one step of direction 1 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (5, 17, 2), then three variants of 9 rows of two bytes, and the extra
; row the mirrored direction 4 reads (it starts one row on).
slope_1_gfx:
  DEFB $05,$11,$02        ; Header
  DEFB $D5,$55,$AA,$7A,$1D,$55 ; Three variants of 9 rows, and a row more
  DEFB $AA,$0E,$03,$D5,$FA,$00 ;
  DEFB $00,$0F,$03,$00,$00,$01 ;
  DEFB $F5,$55,$AA,$DE,$03,$55 ;
  DEFB $AA,$01,$00,$D5,$7A,$00 ;
  DEFB $00,$3F,$03,$00,$00,$01 ;
  DEFB $D5,$55,$AA,$6F,$3F,$75 ;
  DEFB $FA,$1B,$11,$DD,$1E,$00 ;
  DEFB $00,$07,$01,$00,$00,$01 ;
  DEFB $00,$00                 ;

; Landscape graphics: direction 10, direction 3 upside down
;
; Its three variants, each drawn as one step of direction 10 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (9, 29, 1: variants of 17 rows), then three variants of 17 rows of one
; byte (51 bytes). Direction 13 is this graphic one byte on, mirrored.
slope_10_gfx:
  DEFB $09,$1D,$01        ; Header
  DEFB $80,$80,$80,$C0,$40,$C0,$40,$E0,$70,$B0,$50,$B8,$58 ; Three variants of
  DEFB $AE,$57,$AB,$55,$80,$80,$80,$E0,$70,$B4,$5C,$AE,$56 ; 17 rows
  DEFB $AA,$57,$AB,$55,$AB,$55,$AB,$55,$80,$80,$C0,$E0,$60 ;
  DEFB $A0,$50,$B8,$58,$A8,$58,$AA,$56,$AB,$55,$AB,$55     ;

; Landscape graphics: direction 3, a steep slope, one byte wide, 17 rows
;
; Its three variants, each drawn as one step of direction 3 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (9, 29, 1), then three variants of 17 rows of one byte, and one byte
; more for the mirrored direction 6.
slope_3_gfx:
  DEFB $09,$1D,$01        ; Header
  DEFB $D5,$EA,$75,$1A,$1D,$0A,$0D,$0E,$07,$02,$03,$02,$03 ; Three variants of
  DEFB $01,$01,$01,$01,$D5,$AA,$D5,$AA,$D5,$EA,$55,$6A,$75 ; 17 rows and a byte
  DEFB $3A,$2D,$0E,$07,$01,$01,$01,$01,$D5,$AA,$D5,$6A,$55 ; more
  DEFB $1A,$15,$1A,$1D,$0A,$05,$06,$03,$03,$01,$01,$01,$00 ;

; Landscape graphics: direction 2 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 2 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_2_gfx (zeros on the tape):
; three copies of 62 bytes, one after another from here, each two bytes wide
; and 31 rows (three variants of 9 and a partial fourth). The mirrored
; direction 5 uses them two bytes on, at the opposite shift.
slope_2_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Landscape graphics: direction 1 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 1 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_1_gfx: three copies of 93
; bytes (three bytes wide, 31 rows) one after another from here, and three
; spare bytes the mirrored direction 4 reads.
slope_1_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Landscape graphics: direction 3 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 3 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_3_gfx: three copies of 110
; bytes (two bytes wide, 55 rows: three variants of 17 and a partial fourth)
; one after another from here.
slope_3_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Landscape graphics: the flat piece (direction 0) shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants and
; its four end caps at shifts 2, 4 and 6, a row each, each drawn as one step of
; direction 0 by the blitter piece_blitters_shift2, piece_blitters_shift4 or
; piece_blitters_shift6 gives it (its clipping jumps set to draw every column,
; as flat_step_clip sets them for a step clear of the edges), in planet 0's
; surface colour (four times the size).
;
; Three copies of 84 bytes (three bytes wide, 28 rows) one after another from
; here. Only the first 12 rows of each are made at the start (shift_flat_piece,
; by rotating the rows of flat_piece_gfx); the other 16 rows of each came on
; the tape already shifted.
flat_piece_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$C0,$3D,$55,$AA
  DEFB $1E,$80,$00,$03,$DD,$70,$00,$00
  DEFB $35,$55,$55,$AA,$AE,$3A,$1F,$DF
  DEFB $DD,$F0,$78,$07,$55,$55,$C0,$00
  DEFB $AF,$AA,$DD,$F8,$00,$00,$70,$70
  DEFB $55,$55,$55,$AA,$BA,$AA,$DD,$FF
  DEFB $FF,$1E,$77,$70,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $70,$0F,$55,$AA,$07,$E0,$C0,$00
  DEFB $F7,$1C,$00,$00,$0D,$55,$55,$AA
  DEFB $AB,$0E,$07,$F7,$F7,$3C,$DE,$01
  DEFB $55,$55,$70,$C0,$AB,$AA,$77,$7E
  DEFB $00,$00,$1C,$1C,$55,$55,$55,$AA
  DEFB $AE,$AA,$77,$7F,$FF,$C7,$1D,$1C
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$5C,$03,$D5,$EA
  DEFB $01,$B8,$70,$00,$3D,$07,$00,$C0
  DEFB $03,$55,$55,$EA,$AA,$03,$01,$FD
  DEFB $FD,$8F,$77,$00,$55,$55,$5C,$F0
  DEFB $AA,$AA,$DD,$DF,$80,$00,$07,$07
  DEFB $55,$55,$55,$AA,$AB,$AA,$DD,$DF
  DEFB $FF,$71,$07,$07

; Landscape graphics: the flat ceiling (direction 7) shifted by 2, 4 and 6
; pixels
;
; As the game's start-up makes them (run in the simulator): the variants and
; its four end caps at shifts 2, 4 and 6, a row each, each drawn as one step of
; direction 7 by the blitter piece_blitters_shift2, piece_blitters_shift4 or
; piece_blitters_shift6 gives it (its clipping jumps set to draw every column,
; as flat_step_clip sets them for a step clear of the edges), in planet 0's
; surface colour (four times the size).
;
; As flat_piece_shifted, from flat_piece_two_gfx: three copies one after
; another, their first 12 rows made at the start.
flat_piece_two_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$03,$FE
  DEFB $03,$00,$80,$1F,$55,$AA,$3A,$C0
  DEFB $0F,$C0,$03,$7E,$FE,$1A,$35,$57
  DEFB $D5,$AA,$AA,$2A,$C3,$E0,$00,$00
  DEFB $BC,$BE,$55,$57,$00,$C0,$AB,$AA
  DEFB $C3,$E1,$F0,$BF,$BF,$BE,$55,$55
  DEFB $55,$AA,$AA,$AA,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$FF,$00,$80,$60,$07
  DEFB $D5,$AA,$0E,$B0,$03,$F0,$00,$9F
  DEFB $BF,$06,$0D,$55,$F5,$AA,$AA,$0A
  DEFB $F0,$F8,$00,$00,$AF,$AF,$55,$55
  DEFB $C0,$F0,$AA,$AA,$F0,$F8,$7C,$EF
  DEFB $AF,$AF,$55,$55,$55,$AA,$AA,$AA
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$40,$00,$00,$3F
  DEFB $00,$A0,$58,$01,$FA,$AA,$03,$AC
  DEFB $00,$FC,$00,$E7,$AF,$01,$03,$55
  DEFB $7D,$AA,$AA,$02,$3C,$3E,$00,$C0
  DEFB $EB,$EB,$55,$55,$70,$BC,$AA,$AA
  DEFB $3C,$3E,$1F,$FB,$EB,$EB,$55,$55
  DEFB $55,$AA,$AA,$AA

; Landscape graphics: the wall (direction 14)
;
; Its three variants, each drawn as one step of direction 14 at shift 0 by the
; blitter piece_blitters_shift0 gives it, in planet 0's surface colour (four
; times the size).
;
; Header (9, 29, 1), then three variants of 17 rows of one byte; the mirrored
; direction 15 reads it one byte on. Its last bytes are overwritten by the
; first shifted copy (wall_shifted) before the shifting reads them, so the
; partial fourth variant it makes is garbage -- harmless, as it is never drawn.
wall_gfx:
  DEFB $09,$1D,$01        ; Header
  DEFB $D5,$AA,$D5,$6A,$75,$3A,$0D,$06,$0D,$3A,$75,$6A,$55 ; Three variants of
  DEFB $EA,$D5,$AA,$D5,$D5,$EA,$55,$6A,$35,$6A,$55,$EA,$D5 ; 17 rows
  DEFB $AA,$D5,$6A,$55,$6A,$D5,$AA,$D5,$D5,$EA,$55,$6A,$35 ;
  DEFB $1A,$1D,$1A,$1D,$1A,$35,$2A,$75,$6A,$D5,$AA,$D5     ;

; Landscape graphics: the wall (direction 14) shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 14 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from wall_gfx: three copies of 110
; bytes one after another from here.
wall_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Landscape graphics: direction 9 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 9 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_9_gfx, with the
; checkerboard on the left: three copies of 62 bytes one after another from
; here.
slope_9_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Landscape graphics: direction 8 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 8 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_8_gfx: three copies of 93
; bytes one after another from here.
slope_8_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00

; Landscape graphics: direction 10 shifted by 2, 4 and 6 pixels
;
; As the game's start-up makes them (run in the simulator): the variants at
; shifts 2, 4 and 6, a row each, each drawn as one step of direction 10 by the
; blitter piece_blitters_shift2, piece_blitters_shift4 or piece_blitters_shift6
; gives it, in planet 0's surface colour (four times the size).
;
; Made at the start by shift_slope_piece from slope_10_gfx: three copies of 110
; bytes one after another from here. The mirrored direction 13 reads two bytes
; past the end.
slope_10_shifted:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; Spare
;
; Twenty zero bytes; the first two are read by the mirrored direction 13 at a
; shift of 2 (slope_10_shifted).
spare_after_pieces:
  DEFS $14

; The play area's colour patch
;
; For fill_attributes, painted by colour_play_area: 28 cells wide, 20 deep, and
; the attribute -- the third byte, which start_landscape sets for each level (0
; at first, then the level's colour from its table).
play_area_colour:
  DEFB $1C,$14,$43        ; Width, depth, attribute

; The menu's text
;
; Twelve messages for print_string, printed by menu through print_strings: each
; a pixel x, a pixel y, an attribute (bit 7 set for the title), a count of
; characters, and the characters in the game's font codes ($00 to $19 the
; letters, $1A to $23 the digits, $26 a space, $28 a dash; $25 and $27 two
; special glyphs, and $2A to $2C the pieces of the rule under the title). In
; order: the title, the one- and two-player lines, the start line, the four
; control methods (the two joystick lines in two parts each), the copyright
; line and the rule. The code map split them into many small entries; their
; bytes run on to colour_ship.
menu_text:
  DEFB $48,$17,$C5,$07,$02,$18,$01,$04
  DEFB $11,$14,$0D,$50,$2F,$43,$0C,$00
  DEFB $26,$28,$26,$1B,$26,$0F,$0B,$00
  DEFB $18,$04,$11,$50,$3F,$43,$0D,$01
  DEFB $26,$28,$26,$1C,$26,$0F,$0B,$00
  DEFB $18,$04,$11,$12,$50,$4F,$46,$0E
  DEFB $1A,$26,$28,$26,$12,$13,$00,$11
  DEFB $13,$26,$06,$00,$0C,$04,$60,$67
  DEFB $45,$08,$0A,$04,$18,$01,$0E,$00
  DEFB $11,$03,$60,$77,$44,$08,$0A,$04
  DEFB $0C,$0F,$12,$13,$0E,$0D,$60,$7F
  DEFB $46,$08,$09,$0E,$18,$12,$13,$08
  DEFB $02,$0A,$68,$8F,$44,$06,$02,$14
  DEFB $11,$12,$0E,$11,$60,$97,$46,$08
  DEFB $09,$0E,$18,$12,$13,$08,$02,$0A
  DEFB $58,$A7,$44,$0B,$08,$0D,$13,$04
  DEFB $11,$05,$00,$02,$04,$26,$25,$50
  DEFB $BF,$72,$0C,$26,$26,$00,$27,$02
  DEFB $27,$06,$27,$1B,$23,$22,$1F,$40
  DEFB $21,$47,$10,$2A,$2B,$2B,$2B,$2B
  DEFB $2B,$2B,$2B,$2B,$2B,$2B,$2B,$2B
  DEFB $2B,$2B,$2C

; Colour the player's ship
;
; Used by the routine at draw_ship_image.
;
; Called once a pass by draw_ship. Works out the ship's attribute and paints it
; over the ship with the colour patch at ship_colour_patch (6 cells wide, as
; many deep as the ship's height in ship_height needs): white ($47) as a rule,
; yellow ($46) when the ore count at ore has reached 18, and while the
; hit-immunity timer shield_timer runs, cyan ($45) -- flashing between cyan and
; the usual colour every 8 passes (bit 3 of the pass counter pass_counter) in
; its last 32. A change of colour from the last one used (kept in the patch's
; third byte) makes a short sound through sound_ship_colour.
;
; The ore count ore goes up (to 18 at most) at load_ore while the rocket part
; carried (rocket_part_carried) is part 6, the one that needs 18 ore.
colour_ship:
  LD B,$47                ; White, or yellow with 18 ore aboard
  LD A,(ore)              ;
  CP $12                  ;
  JR NZ,colour_ship_0     ;
  LD B,$46                ;
colour_ship_0:
  LD C,B                  ; C keeps it
  LD A,(shield_timer)     ; Is the hit-immunity timer running?
  AND A                   ;
  JR Z,colour_ship_1      ;
  LD B,$45                ; Cyan; steady while the timer is 32 or more
  CP $20                  ;
  JR NC,colour_ship_1     ;
  LD A,(pass_counter)     ; In its last 32, the usual colour every other 8
  AND $08                 ; passes
  JR Z,colour_ship_1      ;
  LD B,C                  ;
colour_ship_1:
  LD A,($F201)              ; A sound if the colour has changed
  CP B                      ;
  PUSH BC                   ;
  CALL NZ,sound_ship_colour ;
  POP BC                    ;
  LD HL,(ship_screen_x)   ; Where the ship is on the screen
  LD A,(ship_height)      ; Its height in character rows, rounded up
  ADD A,$07               ;
  SRL A                   ;
  SRL A                   ;
  SRL A                   ;
  LD C,A                  ;
  LD ($F200),BC           ; Into the patch: depth and attribute
  LD DE,ship_colour_patch ; Paint it
  JP fill_attributes      ;

; The ship's colour patch
;
; Three bytes for fill_attributes: the width in cells (6), the depth in cells
; and the attribute, the last two written by colour_ship every pass.
ship_colour_patch:
  DEFB $06,$02,$47

; The menu frame's corners
;
; Four corners of seven bytes, read by draw_frame through +9 of the frame
; record (menu_frame): a pixel position word for the corner's graphic (only its
; place: draw_frame always draws the four bitmaps from corner_bitmaps, the
; panel frame's own, with draw_bitmap), a position word for its colour, and a
; fill_attributes colour patch (width, depth, attribute).
menu_frame_corners:
  DEFB $00,$BE,$00,$BF,$20,$01,$72 ; Top left
  DEFB $F8,$BE,$00,$07,$20,$01,$72 ; Top right
  DEFB $00,$07,$00,$B7,$01,$16,$72 ; Bottom left
  DEFB $F8,$07,$F8,$B7,$01,$16,$72 ; Bottom right

; The menu frame's tick marks
;
; Six pairs, read by draw_frame through +12 of the frame record: an x used for
; a tick on the top and bottom edges, and a y used for one on the left and
; right edges.
menu_frame_ticks:
  DEFB $20,$26            ; x, y
  DEFB $40,$4E            ;
  DEFB $68,$76            ;
  DEFB $90,$9E            ;
  DEFB $B8,$9E            ;
  DEFB $D8,$9E            ;

; The menu frame record
;
; The 17-byte record draw_frame draws the menu's frame from, given to it in IX
; by menu. The play area's frame is drawn from a record of the same shape by
; draw_panel.
menu_frame:
  DEFB $C0,$57,$DF,$57    ; The screen addresses of the tops of the left and
                          ; right edges
  DEFB $B0                ; Their height in rows
  DEFB $E1,$56,$01,$46    ; The screen addresses of the top and bottom edges
  DEFW $0000              ; The corners (menu_frame_corners, set by menu)
  DEFB $06                ; The number of tick-mark pairs
  DEFW $0000              ; The tick marks (menu_frame_ticks, set by menu)
  DEFB $06                ; The y of the top tick marks
  DEFB $F8                ; The x of the right-hand ones
  DEFB $1F                ; The length of the top and bottom edges, in bytes

; Stir the random numbers
;
; Used by the routines at deal_places, menu_loop and play.
;
; Called once a pass of the play loop (play), of the menu's loop (menu_loop)
; and from deal_places. The two random bytes of random_a are stirred with a
; byte read from a pointer that walks through memory (kept big-endian after
; them), and with the player's controls as they were last read (controls_now).
; Bits 4 and 6 of the pointer's high byte are cleared and it never reaches the
; top 20K, so the bytes come from the ROM and the game's own code.
random:
  LD IX,random_a          ; Step the pointer on, kept within the ROM and the
  LD H,(IX+$02)           ; code
  LD L,(IX+$03)           ;
  INC HL                  ;
  LD A,H                  ;
  AND $AF                 ;
  LD (IX+$02),A           ;
  LD (IX+$03),L           ;
  LD B,(HL)               ; The byte there
  LD A,(controls_now)     ; First random byte: add the byte, the controls and 5
  ADD A,B                 ;
  ADD A,$05               ;
  ADD A,(IX+$00)          ;
  LD (IX+$00),A           ;
  LD A,B                  ; Second: add the byte's complement, the controls and
  CPL                     ; 9
  LD B,A                  ;
  LD A,(controls_now)     ;
  ADD A,$09               ;
  ADD A,B                 ;
  ADD A,(IX+$01)          ;
  LD (IX+$01),A           ;
  RET

; Show the menu and wait for a game to be started
;
; Used by the routine at game.
;
; Called by game before each game. Clears the screen, draws the frame from
; menu_frame_ticks, prints the twelve lines of text at menu_text with
; print_strings, puts the two small glyphs of the copyright line (player_count)
; and colours them, draws a rule under the title, shows the two players'
; control methods (move_method_arrow), plays the title tune at title_tune until
; a key is pressed (play_tune_until_key), runs the menu's loop menu_loop until
; 0 starts a game, and plays the start tune at start_tune to the end.
menu:
  CALL clear_pixels       ; Clear the pixels, attributes to white on black
  CALL clear_attributes   ;
  LD IX,menu_frame         ; The frame, with its corners and tick marks
  LD DE,menu_frame_corners ;
  LD (IX+$09),E            ;
  LD (IX+$0A),D            ;
  LD DE,menu_frame_ticks   ;
  LD (IX+$0C),E            ;
  LD (IX+$0D),D            ;
  CALL draw_frame          ;
  LD DE,menu_text         ; The twelve lines of text
  LD B,$0C                ;
  CALL print_strings      ;
  LD HL,$BF40             ; The two glyphs on the copyright line
  LD DE,copyright_glyphs  ;
  CALL draw_bitmap        ;
  LD HL,$BEB0             ;
  CALL draw_bitmap        ;
  LD A,$46                ; Their colour
  LD ($5AE9),A            ;
  LD ($5AF6),A            ;
  LD HL,$486C             ; A rule under the title
  LD (HL),$0F             ;
  INC L                   ;
  LD B,$06                ;
menu_0:
  LD (HL),$FF             ;
  INC L                   ;
  DJNZ menu_0             ;
  LD (HL),$F0             ;
  LD IX,player1_pointer    ; Show each player's control method
  LD DE,pointer_p1_graphic ;
  CALL move_method_arrow   ;
  LD IX,player2_pointer    ;
  LD DE,pointer_p2_graphic ;
  CALL move_method_arrow   ;
  CALL menu_show_players   ; The title tune, until a key is pressed
  LD DE,title_tune         ;
  CALL play_tune_until_key ;
  CALL menu_loop          ; The menu's keys, until 0
  CALL menu_show_players  ; The start tune, played to the end
  LD DE,start_tune        ;
  JP play_tune            ;

; Number of players
;
; The first byte is the number of players less one: 0 one, 1 two (set by the
; menu's keys, menu_keys). Read by new_game, lose_life, game_over and the menu.
player_count:
  DEFB $00

; Two glyphs for the menu's copyright line
;
; The two, as draw_bitmap draws them (three times the size).
;
; Drawn by menu with draw_bitmap, one after the other: a width in bytes, a
; height in rows, and the rows. A copyright sign (3 bytes by 8 rows) and two
; letters (2 bytes by 6 rows).
copyright_glyphs:
  DEFB $03,$08            ; 3 bytes, 8 rows
  DEFB $00,$01,$3C
  DEFB $FC,$83,$42
  DEFB $FE,$C7,$99
  DEFB $03,$C7,$A1
  DEFB $03,$C7,$A1
  DEFB $FE,$C7,$99
  DEFB $FC,$83,$42
  DEFB $00,$01,$3C
  DEFB $02,$06            ; 2 bytes, 6 rows
  DEFB $81,$3F
  DEFB $C3,$7F
  DEFB $C3,$C0
  DEFB $C3,$C0
  DEFB $C3,$7F
  DEFB $81,$3F

; Clear B bytes from HL
;
; Used by the routines at reset_for_life, clear_player_record and
; build_shifted_pieces.
;
;   HL the first byte
;   B how many
; O:HL the byte after the last
clear_bytes:
  LD (HL),$00
  INC HL
  DJNZ clear_bytes
  RET

; Reset the play for a new life
;
; Used by the routine at play.
;
; Called by play at the start of every life (and of every level). Sets the
; ship's colour back to white, clears the death byte death_count, the
; hit-immunity timer shield_timer, the end-of-level byte launch_state, the hit
; count and two more flags, and empties or resets the game's tables: bit 0 of
; the first byte of 18 two-byte entries of star_layer_pointer, 96 bytes at
; shot_records, the first byte of the eight 5-byte entries of
; rocket_part_table, 16 bytes of beam_grace's table; then copies four records
; back from the starting copies that follow them (trail_frame, lift_jet_record,
; side_jet_record, and the 32 bytes of the ship's state in bare_ship), and
; resets with free_object the nine 24-byte records at object_records and the
; six 26-byte records at ground_objects.
;
; Which of these tables is which is for the ranges that hold them; here they
; are named by address only.
reset_for_life:
  LD A,$47                ; The ship's colour back to white
  LD ($F201),A            ;
  XOR A                      ; Clear the death byte, the immunity timer, the
  LD (death_count),A         ; end of the level, the hits and two more flags
  LD (shield_timer),A        ;
  LD (launch_state),A        ;
  LD (hit_count),A           ;
  LD (carried_part_redraw),A ;
  LD (meteor_shower),A       ;
  LD A,$18                ; beam_grace to 24
  LD (beam_grace),A       ;
  LD HL,star_positions    ; Bit 0 of the first byte of 18 entries of
  LD B,$12                ; star_layer_pointer
reset_for_life_0:
  LD A,(HL)               ;
  AND $FE                 ;
  LD (HL),A               ;
  INC HL                  ;
  INC HL                  ;
  DJNZ reset_for_life_0   ;
  LD HL,shot_records      ; 96 bytes at shot_records
  LD B,$60                ;
  CALL clear_bytes        ;
  LD HL,rocket_part_table ; The first byte of the eight 5-byte entries of
  LD B,$08                ; rocket_part_table
reset_for_life_1:
  LD (HL),$00             ;
  LD A,$05                ;
  CALL add_a_to_hl        ;
  DJNZ reset_for_life_1   ;
  LD HL,flicker_beam_records ; 16 bytes of beam_grace's table
  LD B,$10                   ;
  CALL clear_bytes           ;
  LD DE,trail_frame       ; Four records back to their starting values
  LD HL,trail_start       ;
  LD BC,$0006             ;
  LDIR                    ;
  LD DE,lift_jet_record   ;
  LD HL,lift_jet_start    ;
  LD C,$06                ;
  LDIR                    ;
  LD DE,side_jet_record   ;
  LD HL,side_jet_start    ;
  LD C,$07                ;
  LDIR                    ;
  LD C,$20                ;
  LD DE,ship_record       ;
  LD HL,ship_record_start ;
  LDIR                    ;
  LD IX,object_records    ; Reset the nine records at object_records
  LD B,$09                ;
  LD DE,$0018             ;
  CALL reset_for_life_2   ;
  LD IX,ground_objects    ; And the six at ground_objects
  LD E,$1A                ;
  LD B,$06                ;
reset_for_life_2:
  CALL free_object        ; The loop: B records of DE bytes from IX
  ADD IX,DE               ;
  DJNZ reset_for_life_2   ;
  RET                     ;

; Clear a player record for a new game
;
; Used by the routine at new_game.
;
; Zeroes the first 28 bytes of the record (score, flags, lives, add-ons,
; level), sets the rocket part carried (+26) to $FF (none), and copies the
; starting restart position from start_position into +19 to +24. The rest of
; the record (the control method at +28, and the table of the level's objects
; from +29) is set by new_game.
;
; HL the record (messages_and_players + 25 for player 1, + 130 for player 2)
clear_player_record:
  LD B,$1C                ; Zero +0 to +27
  PUSH HL                 ;
  CALL clear_bytes        ;
  DEC HL                  ; +26: no rocket part carried
  DEC HL                  ;
  LD (HL),$FF             ;
  POP HL                  ; +19 to +24: the restart position at the start of
  LD A,$13                ; the game
  CALL add_a_to_hl        ;
  LD DE,start_position    ;
  LD C,$06                ;
  EX DE,HL                ;
  LDIR                    ;
  RET                     ;

; The restart position at the start of a game
;
; Three words copied by clear_player_record into a player record's +19 to +24,
; the place in the landscape a life starts from (see messages_and_players): the
; values of view_column, view_x and origin_x to restore. Taken from those
; variables by save_restart_point when the restart position moves on, and put
; back by start_landscape.
;
; The origins here are $78 and $40 against a view x and y of $90 and $B7: the x
; pair sums to 8 as origin_x says it always does, but the y pair sums to $F7,
; not $B7 (origin_y). A vertical move keeps the sum (scroll_landscape adds the
; step to the origin and takes it from the view's y), and only a wrap into the
; next cell sets the pair afresh, to $B7 and 0 or $17 and $A0; so from a new
; game's start the sum is $F7 until the fly-in's first vertical wrap. Why, and
; what the first passes look like, is not known (read only).
start_position:
  DEFW $070D              ; view_column, view_x, origin_x
  DEFW $B790              ;
  DEFW $4078              ;

; Set up a new game
;
; Used by the routine at game.
;
; Called by game after the menu. Clears bits 6 and 7 of the 290 bytes from
; map_object_kinds (mask_world_objects with E = $3F): each player's mark on the
; level's objects, bit 7 for player 1 and bit 6 for player 2 (see play). Clears
; both player records (clear_player_record) and gives each five lives; if the
; score lines were left the other way round by the last game (the byte at
; p1_lives_text is $A0) they are put back with swap_players, player_in_play
; being set to $99 meanwhile so that it does not also change the current
; player. Player 1 is current; with one player, player 2 has no lives. Each
; player's control method is copied from the menu, the two scores on the panel
; are set to six noughts ($1A is the font's 0), and each player's table of the
; level's objects is filled in by deal_places.
new_game:
  LD E,$3F                ; Clear both players' marks on the level's objects
  CALL mask_world_objects ;
  LD HL,score               ; Clear both records
  CALL clear_player_record  ;
  LD HL,other_player_record ;
  CALL clear_player_record  ;
  LD A,$05                ; Five lives each
  LD (lives),A            ;
  LD ($F656),A            ;
  LD A,$99                ; Put the score lines back in order if they were
  LD (player_in_play),A   ; swapped
  LD A,(p1_lives_text)    ;
  CP $A0                  ;
  CALL Z,swap_players     ;
  XOR A                   ; Player 1 is current
  LD (player_in_play),A   ;
  LD A,(player_count)     ; One player: player 2 has no lives
  AND A                   ;
  JR NZ,new_game_0        ;
  LD ($F656),A            ;
new_game_0:
  LD A,($870A)            ; Each player's control method, as chosen on the menu
  LD (control_method),A   ;
  LD A,($870D)            ;
  LD ($F66C),A            ;
  LD B,$06                ; Both scores to 000000
  LD DE,p1_score_digits   ;
  LD HL,p2_score_digits   ;
  LD A,$1A                ;
new_game_1:
  LD (DE),A               ;
  LD (HL),A               ;
  INC HL                  ;
  INC DE                  ;
  DJNZ new_game_1         ;
  LD DE,player_places     ; Each player's table of the level's objects
  CALL deal_places        ;
  LD DE,$F66D             ;
  CALL deal_places        ;
  RET                     ; Done

; Colour the play area
;
; Used by the routines at start_landscape and fly_in_step.
;
; Paints the play area's attributes with the colour patch play_area_colour (28
; cells by 20, in the attribute start_landscape sets in it for each level).
; Called by start_landscape and fly_in_step.
colour_play_area:
  LD HL,$B708
  LD DE,play_area_colour
  JP fill_attributes

; The game: menu, a new game, and into play
;
; Used by the routines at start and play.
;
; Jumped to by the start (start), and by play when the current player has no
; lives left. The menu (menu), a new game (new_game), a clear screen
; (clear_attributes, clear_pixels), the panel (draw_panel), the PLAYER message
; (show_player), a pause of six delays (delay), the play area cleared
; (clear_play_area), and into the play (play).
game:
  CALL menu               ; Menu, and a new game
  CALL new_game           ;
  CALL clear_attributes   ; Clear the screen and draw the panel
  CALL clear_pixels       ;
  CALL draw_panel         ;
  CALL show_player        ; Say which player is to play
  LD B,$06                ; A pause
game_0:
  CALL delay              ;
  DJNZ game_0             ;
  CALL clear_play_area    ; Clear the play area and play
  JR play_0               ;

; The play: a life, from its start to its end
;
; Used by the routine at level_done.
;
; Entered at the start of every life after the first: from its own death
; sequence once lose_life has taken a life, and from level_done at the end of a
; level. A new game enters further on, past the reset and the panel, from game.
; Resets the play (reset_for_life), goes back to the menu if the current player
; has no lives, redraws the panel, and then: puts back the ore (ore) the player
; had aboard, patches the BIT in spawn_objects_from_list and the SETs in
; ship_hits_ground_objects and beam_hits_ground_object that test and mark an
; object gone in map_object_kinds (one byte per object lying in the world) so
; that they use bit 7 for player 1 and bit 6 for player 2, and starts the level
; (start_landscape).
;
; The play loop, once a pass (about 20 a second): ship_pass, the fuel gauge
; (update_fuel_gauge), spawn_tick, update_objects (counts the death byte up,
; and much else), the random numbers (random). It ends when the death byte
; death_count reaches 12, or when launch_state is $FF: the rocket, all eight
; parts built and the ship docked, has risen (level_done).
;
; The death sequence: the landscape is stopped (scroll_down), the fuel is set
; to empty (fuel), part of draw_ship runs once and lift_jet erases the lift
; jet; then a shorter loop -- ship_pass, the pause key (check_pause), the fuel
; gauge, update_objects, carried_part, the gauge again -- runs until
; update_objects has counted the death byte up to $50. Then the death tune
; (death_tune), the play area dissolved (dissolve_and_clear), and a life is
; taken (lose_life) before going round again.
play:
  CALL reset_for_life     ; Reset the play
  LD A,(lives)            ; No lives left: back to the menu
  AND A                   ;
  JR Z,game               ;
  CALL draw_panel         ; Redraw the panel
; This entry point is used by the routine at game.
play_0:
  LD A,(ore_kept)         ; Put back the player's ore
  LD (ore),A              ;
  LD A,(player_in_play)   ; Player 1 tests bit 7 of an object's state (BIT
  LD B,A                  ; 7,(HL)), player 2 bit 6 (BIT 6,(HL))
  LD A,$7E                ;
  BIT 0,B                 ;
  JR Z,play_1             ;
  LD A,$76                ;
play_1:
  LD ($E118),A            ; Patch the test, and (with the bits for SET) the two
  OR $F0                  ; instructions that set it
  LD ($A9B8),A            ;
  LD ($ABDE),A            ;
  CALL start_landscape    ; Start the level
play_2:
  CALL ship_pass          ; A pass of the play loop
  CALL update_fuel_gauge  ;
  CALL spawn_tick         ;
  CALL update_objects     ;
  CALL random             ;
  LD A,(death_count)      ; Has the death byte reached 12?
  CP $0C                  ;
  JR NC,play_3            ;
  LD A,(launch_state)     ; Go round again unless launch_state is $FF
  INC A                   ;
  JR NZ,play_2            ;
  JP level_done           ; The level is done
play_3:
  XOR A                   ; Death: stop the landscape, the fuel gauge to empty
  LD (scroll_across),A    ;
  LD (scroll_down),A      ;
  LD HL,$BE00             ;
  LD (fuel),HL            ;
  CALL draw_trail         ; Once at the start of the death sequence
  CALL lift_jet           ;
play_4:
  CALL ship_pass          ; A pass of the death sequence
  CALL check_pause        ;
  CALL update_fuel_gauge  ;
  CALL update_objects     ;
  CALL carried_part       ;
  CALL update_fuel_gauge  ;
  LD A,(death_count)      ; Until the death byte is $50
  CP $50                  ;
  JR NZ,play_4            ;
  LD DE,death_tune        ; The death tune
  CALL play_tune          ;
  CALL dissolve_and_clear ; Dissolve the play area
  CALL lose_life          ; Take a life, and start the next
  JP play                 ;

; Take a life from the current player
;
; Used by the routine at play.
;
; Keeps the ore aboard (ore) in the player's record (+25, ore_kept), takes a
; life (+6), and if that was the last ends the player's game (game_over).
; Otherwise, with two players, hands over to the other one (swap_players).
;
; The JR Z tests the flags the DEC A leaves; LD does not touch them. With the
; DEC A taken out (a NOP poked over it, as stage 0's infinite-lives poke did)
; the JR Z picks up the zero flag left by the last DEC C of clear_play_area,
; which the death sequence has just run, and every death goes to game_over with
; the lives kept. An OR A in its place keeps the lives and tests them (run for
; the reference pages).
lose_life:
  LD A,(ore)              ; Keep the ore aboard in the record
  LD (ore_kept),A         ;
  LD A,(lives)            ; A life less; none left: game over
  DEC A                   ;
  LD (lives),A            ;
  JR Z,game_over          ;
  LD A,(player_count)     ; Two players: the other one's turn
  AND A                   ;
  CALL NZ,swap_players    ;
  RET                     ;

; The tune played when the ship has been destroyed
;
; For play_tune: one byte a note, bits 0 to 5 the note (an index into the table
; at note_table; 0 a rest) and bits 6 and 7 its length less one; $FF ends it.
death_tune:
  DEFB $1E,$1C,$1B,$19,$58,$D9,$FF

; The tune played when a game is started
;
; For play_tune, in the format of death_tune; played by menu as the menu ends.
; It ends with $FF.
start_tune:
  DEFB $2A,$2C,$2A,$27,$29,$29,$29,$25
  DEFB $E7,$FF

; Say which player is to play
;
; Used by the routines at game, game_over and swap_players.
;
; Puts the player's digit ($1B for 1, $1C for 2, from player_in_play) into the
; message at messages_and_players + 13 and prints it with print_string.
show_player:
  LD A,(player_in_play)
  ADD A,$1B
  LD (player_digit),A
  LD DE,player_message
  JP print_string

; The current player's game is over
;
; Used by the routine at lose_life.
;
; Called by lose_life when the last life has gone. Prints the player's name and
; GAME OVER (both messages at messages_and_players), puts the score in the high
; score if it beats it (check_high_score), plays the game-over tune
; (game_over_tune), waits twelve delays or until 0 is pressed, clears the play
; area and, with two players, hands over to the other one (swap_players does
; nothing if that one has no lives either). play then finds the current player
; with no lives and goes back to the menu.
game_over:
  CALL show_player           ; PLAYER n, GAME OVER
  LD DE,messages_and_players ;
  CALL print_string          ;
  CALL check_high_score   ; A new high score?
  LD DE,game_over_tune    ; The game-over tune
  CALL play_tune          ;
  LD B,$0C                ; Wait twelve delays, or until 0 is pressed
game_over_0:
  CALL delay              ;
  LD A,$EF                ;
  OUT ($FD),A             ;
  IN A,($FE)              ;
  AND $01                 ;
  JR Z,game_over_1        ;
  DJNZ game_over_0        ;
game_over_1:
  CALL clear_play_area    ; Clear the play area
  LD A,(player_count)     ; Two players: the other one's turn, if they have
  AND A                   ; lives
  CALL NZ,swap_players    ;
  RET                     ;

; The end of a level: the rocket has risen
;
; Used by the routine at play.
;
; Jumped to from the play loop (play) when launch_state is $FF, after the
; rocket has been built and has risen. Clears the play area, shows the end of
; the level (planet_secured, which also counts the level on in the record,
; +18), plays its tune (level_tune), waits fourteen delays, clears the play
; area again and the player's three flag bytes (+3 to +5, see
; messages_and_players), and starts the next life's play at play.
;
; The rocket's part of this is from the agent who read launch_state's setting;
; here, planet_secured is read counting the level on.
level_done:
  CALL clear_play_area    ; Clear the play area; the end-of-level display
  CALL planet_secured     ;
  LD DE,level_tune        ; Its tune
  CALL play_tune          ;
  LD B,$0E                ; A pause
level_done_0:
  CALL delay              ;
  DJNZ level_done_0       ;
  CALL clear_play_area    ; Clear the play area
  XOR A                   ; Clear the record's three flag bytes, and play on
  LD (player_flag_bits),A ;
  LD ($F5EB),A            ;
  LD ($F5EC),A            ;
  JP play                 ;

; Make the score the high score if it beats it
;
; Used by the routine at game_over.
;
; Compares the current player's score as printed on the panel (six characters
; from p1_score_digits) with the high score (hiscore_digits), digit by digit
; from the left, and copies it over if it is greater.
check_high_score:
  LD HL,hiscore_digits    ; The score's six digits against the high score's
  LD DE,p1_score_digits   ;
  LD B,$06                ;
check_high_score_0:
  LD A,(DE)               ; A digit less: not a high score
  CP (HL)                 ;
  RET C                   ;
  JR NZ,check_high_score_1 ; Equal: the next digit
  INC HL                   ;
  INC DE                   ;
  DJNZ check_high_score_0  ;
check_high_score_1:
  LD DE,hiscore_digits    ; Greater: the new high score
  LD HL,p1_score_digits   ;
  LD BC,$0006             ;
  LDIR                    ;
  RET                     ;

; The tune played at the end of a level
;
; For play_tune, in the format of death_tune; played by level_done. Twenty-one
; notes and the $FF that ends them.
level_tune:
  DEFB $A5,$25,$25,$25,$A9,$25,$25,$25
  DEFB $AC,$A4,$A5,$6C,$2C,$AE,$AC,$AA
  DEFB $AE,$AC,$A4,$A5,$99,$FF

; The tune played when a player's game is over
;
; For play_tune, in the format of death_tune; played by game_over. Sixteen
; notes and the $FF that ends them.
game_over_tune:
  DEFB $2E,$2C,$2A,$2C,$2A,$29,$2A,$29
  DEFB $27,$22,$1E,$22,$27,$27,$27,$27
  DEFB $FF

; Hand over to the other player
;
; Used by the routines at new_game, lose_life and game_over.
;
; Does nothing if the other player has no lives (+6 of the second record).
; Otherwise swaps the two player records, all 105 bytes, so that the current
; player is always the one at messages_and_players + 25 and the rest of the
; game never needs to know which; swaps the two score lines of the panel
; (p1_score_text and p2_score_text, 16 bytes each); and unless player_in_play
; is $99 (as new_game sets it while it puts the score lines in order) changes
; player_in_play to the other player, says PLAYER n (show_player), pauses eight
; delays and clears the play area.
swap_players:
  LD A,($F656)            ; The other player has no lives: carry on
  AND A                   ;
  RET Z                   ;
  LD DE,score               ; Swap the records
  LD HL,other_player_record ;
  LD B,$69                  ;
  CALL swap_bytes           ;
  LD DE,p1_score_text     ; Swap the score lines
  LD HL,p2_score_text     ;
  LD B,$10                ;
  CALL swap_bytes         ;
  LD A,(player_in_play)   ; Setting up: leave the current player alone
  CP $99                  ;
  RET Z                   ;
  XOR $01                 ; The other player, and say so
  LD (player_in_play),A   ;
  CALL show_player        ;
  LD B,$08                ; A pause, and clear the play area
swap_players_0:
  CALL delay              ;
  DJNZ swap_players_0     ;
  JP clear_play_area      ;

; Swap B bytes between (HL) and (DE)
;
; Used by the routine at swap_players.
;
; HL one block
; DE the other
; B how many
swap_bytes:
  LD C,(HL)
  LD A,(DE)
  LD (HL),A
  LD A,C
  LD (DE),A
  INC HL
  INC DE
  DJNZ swap_bytes
  RET

; The GAME OVER message
;
; For print_string, printed by game_over: a pixel x and y, an attribute (bit 7
; set here), a count of characters (9) and the characters (font codes: $00 to
; $19 the letters, $1A to $23 the digits, $26 a space). The PLAYER message
; follows (player_message).
messages_and_players:
  DEFB $30,$4F,$C6,$09,$06,$00,$0C,$04
  DEFB $26,$0E,$15,$04,$11

; The PLAYER message
;
; For print_string, printed by show_player and game_over: x, y, attribute, a
; count of 8 and the characters; the last of them, the player's digit, is
; player_digit.
player_message:
  DEFB $38,$67,$C5,$08    ; x, y, attribute, count
  DEFB $0F,$0B,$00,$18,$04,$11,$26 ; The word and a space

; The PLAYER message's digit
;
; The last character of player_message: $1B (the font's 1) or $1C (2), written
; by show_player from player_in_play, the current player.
player_digit:
  DEFB $00

; The current player's record: the score
;
; The current player's record is 105 ($69) bytes from here, to
; other_player_record; swap_players swaps it with the other player's at a
; change of player, so the game only ever reads and writes this one.
; clear_player_record clears it for a new game and new_game fills in the rest.
; Each field is an entry of its own; +n below is the offset from here.
;
; +0 to +2: the score, three BCD bytes, least significant first.
; test_player_shots adds BC to it.
score:
  DEFB $00,$00,$00        ; Score

; The current player's destroyed beam generators (+3 to +5)
;
; 24 bits, one for each map square (map_square_bit picks the byte and the bit):
; set when the special beam destroys a beam generator there (object kinds 6 and
; 7, beam_hits_ground_object), and tested by draw_beams, which then draws no
; beam in that square. Cleared at the end of a level (level_done).
player_flag_bits:
  DEFB $00,$00,$00

; The current player's lives (+6)
;
; 5 at a new game (new_game); one taken by lose_life, one given by
; contact_extra_life (at most 9); none left sends play back to the menu. Also
; read by print_lives for the panel and by ground_object_fires for the ground
; objects' firing rate.
lives:
  DEFB $03

; The current player's add-ons (+7 to +15): the first slot
;
; Nine bytes, one per add-on the ship can pick up, each 0 or the add-on's own
; number (1 to 9), set by draw_ship when one is fitted. build_ship_sprite
; builds the ship's picture (ship_picture) from them. The ones the code reads
; by name are entries of their own: addon_two, addon_four to addon_nine. This
; one is read by collect_pickup, build_ship_sprite and draw_ship.
addon_slots:
  DEFB $00

; Add-on 2 (+8)
;
; Set by build_ship_sprite to twice the first slot as it builds the ship, and
; read by hit_block. The byte after it, add-on 3, is not read by name.
addon_two:
  DEFB $00,$00

; Add-on 4 (+10)
;
; Read by start_attacker, build_ship_sprite (top speed across) and draw_ship
; (the side jet is drawn if it is fitted).
addon_four:
  DEFB $00

; Add-on 5 (+11)
;
; Read by start_attacker, build_ship_sprite, read_controls and lift_jet (the
; lift jet).
addon_five:
  DEFB $00

; Add-on 6 (+12)
;
; When fitted, the fuel burnt by read_controls is halved.
addon_six:
  DEFB $00

; Add-on 7 (+13)
;
; Read by special_beam_key when bit 5 of the controls (the number row) is set.
addon_seven:
  DEFB $00

; Add-on 8 (+14)
;
; Read by fire (firing).
addon_eight:
  DEFB $00

; Add-on 9 (+15)
;
; Read by collect_pickup, build_ship_sprite, hit_block and ship_pass.
addon_nine:
  DEFB $00

; The slab-and-spire load is carried (+16)
;
; 9 while the ship carries the slab and the spire (object kinds 9 and $0A,
; add-ons 10 and 11): set by collect_pickup when the slab is taken, cleared by
; carried_part when the load is put down and by start_landscape; tested by
; carried_part, which draws the load, and ship_contact (a cloud tests itself
; against the load only while it is carried). The byte after it (+17) is not
; referred to.
player_count_nine:
  DEFB $00,$00

; The current player's level (+18)
;
; From 0; counted on by planet_secured when the planet is secured.
; start_landscape picks the level's colour and data by it.
level:
  DEFB $00

; The current player's restart position (+19 to +24): the first word
;
; Three words, saved by save_restart_point as the restart point moves on and
; put back by start_landscape at the start of a life: this one for view_column,
; restart_view for view_x, restart_fine for origin_x. A new game starts from
; start_position.
restart_scroll:
  DEFW $070D

; The restart position's second word (+21)
;
; For view_x; see restart_scroll. start_landscape also reads its low byte.
restart_view:
  DEFW $B790

; The restart position's third word (+23)
;
; For origin_x, the scroll position; see restart_scroll.
restart_fine:
  DEFW $4078

; The ore the current player had aboard (+25)
;
; Kept here from ore by lose_life when a life is lost and put back by play;
; cleared by drop_part when part 6 is dropped in place.
ore_kept:
  DEFB $00

; The rocket part the current player carries (+26)
;
; $FF for none (clear_player_record, drop_part); set by collect_pickup when a
; part is picked up and to 6 by planet_secured as a planet is secured. Read by
; drop_part, load_ore (ore is collected only with part 6 aboard),
; build_ship_sprite and others.
rocket_part_carried:
  DEFB $FF

; The rocket parts delivered (+27)
;
; How many parts of the rocket have been delivered, which is also the number of
; the part due next: counted up by update_rocket_part as each one falls into
; place; read by draw_rocket, which draws that many and launches at eight, and
; by collect_pickup, which lets the ship take only the part due next. Set to 6
; by planet_secured, with rocket_part_carried, at the end of a planet.
parts_built:
  DEFB $00

; The current player's control method (+28)
;
; 0 keyboard, 1 Kempston, 2 cursor, 3 Interface II; copied from the menu by
; new_game and read by read_control_method.
control_method:
  DEFB $00

; The current player's places (+29 to +104)
;
; 19 entries of 4 bytes, filled in by deal_places at a new game and walked by
; load_map_window; bit 7 of an entry's first byte marks it done
; (collect_pickup). The entries the code names directly are entries of their
; own: place_nine, place_ten, place_eleven, place_last.
player_places:
  DEFB $00,$00,$00,$00
  DEFB $FF,$FF,$FF,$FF
  DEFB $FF,$FF,$FF,$FF
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00

; Place entry 9 (+65)
;
; Given a position by carried_part; bit 7 cleared by start_landscape.
place_nine:
  DEFB $00,$00,$00,$00

; Place entry 10 (+69)
;
; Given a position by carried_part; bit 7 set by collect_pickup and cleared by
; start_landscape.
place_ten:
  DEFB $00,$00,$00,$00

; Place entry 11 (+73), and the ones after it
;
; collect_pickup indexes the entries from here by the part's number.
place_eleven:
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00
  DEFB $00,$00,$00,$00

; The last place entry (+101)
;
; Filled in again by planet_secured (through deal_places's second entry point)
; when a planet is secured.
place_last:
  DEFB $00,$00,$00,$00

; The other player's record
;
; Laid out as score. With one player its lives (+6) are 0, so swap_players
; never swaps it in. new_game writes its lives, control method (+28) and object
; table (+29) directly.
other_player_record:
  DEFB $00,$00,$00,$00,$00,$00,$03,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$0D,$07,$90,$B7,$78
  DEFB $40,$00,$FF,$00,$00,$00,$00,$00
  DEFB $00,$FF,$FF,$FF,$FF,$FF,$FF,$FF
  DEFB $FF,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00

; The ship's picture, built in RAM
;
; 300 bytes ($12C), cleared by start_landscape and built by build_ship_sprite
; from the add-ons the player has picked up (it starts from 84 bytes copied
; from bare_ship to 156 bytes in, and adds each part); six bytes a row, bottom
; row first like the game's other graphics. ship_graphic points at ship_picture
; while a rocket part hangs from the ship (bit 6 of part_being_fitted), the
; picture then five rows taller, and otherwise 30 bytes (five rows) on
; (toggle_carried_part); and load_ore adds rows to it from rocket_tank_graphic
; as the ore aboard (ore) grows.
ship_picture:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00

; Row 50 of the ship's picture
;
; Six bytes after ship_picture's 300, and not spare: add-on 9
; (build_ship_sprite) grows the picture up to row 50 (51 rows with every add-on
; fitted, simulated), so its top row and its mask's are drawn here, and the
; ship is drawn from here up to its top: the figure's top row puts $30 in the
; third byte, spare_after_ship + 2, and leaves the rest nought (watched for the
; reference pages). Zero in the snapshot; nothing clears it, and nothing else
; uses it.
spare_after_ship:
  DEFS $06

; Build the landscape pieces' shifted copies
;
; Used by the routine at sprite_buffer_two.
;
; Called once, at the start (build_shift_tables), with the shift tables from
; shift_tables in place. Clears the work bytes from scratch_count, 38 of them,
; and makes, for each of the nine stored terrain-piece graphics, its copies
; shifted right by 2, 4 and 6 pixels, one after another at the address given:
; with shift_slope_piece for the slopes and the wall, with shift_flat_piece for
; the two flat pieces. The three ceiling slopes are made with piece_step_x set,
; which pads them with the checkerboard on the left instead of the right.
;
; The tape holds the copies' places as zeros (the generator ORs into them),
; except that the flat pieces' copies are only partly made here; the rest of
; them came on the tape (see shift_flat_piece). Each graphic starts with a
; header (read_piece_header), which is why the source addresses here are two or
; three bytes before those in the tables at piece_blitters_shift2.
;
; This routine and the ones after it to shift_slope_piece's end run once.
; Afterwards their bytes are reused: a sprite-compositing buffer of up to 256
; bytes starts 21 bytes into this routine, inside the operand of a CALL, so it
; cannot have a label of its own (copy_box_in copies into it with LDI and
; dissolve_sprite reads it back; old_beam_frame builds a sprite there and
; new_beam_frame another from its 128th byte, sprite_buffer_two), over the end
; of this routine and the ones up to bit_reverse_table; and from
; bit_reverse_table the bit-reversed table build_bit_reverse makes, over the
; rest.
build_shifted_pieces:
  LD HL,scratch_count     ; Clear the work bytes scratch_count to
  LD B,$26                ; basic_leftovers
  CALL clear_bytes        ;
  XOR A                   ; Floors: no padding on the left
  LD (piece_step_x),A     ;
  LD HL,slope_2_shifted   ; Direction 2 (one byte wide, 9 rows), shifted to
  EXX                     ; slope_2_shifted
  LD HL,slope_2_gfx       ;
  CALL shift_slope_piece  ;
  LD HL,slope_1_shifted   ; Direction 1 (two bytes wide, 9 rows), to
  EXX                     ; slope_1_shifted
  LD HL,slope_1_gfx       ;
  CALL shift_slope_piece  ;
  LD HL,slope_3_shifted   ; Direction 3 (one byte wide, 17 rows), to
  EXX                     ; slope_3_shifted
  LD HL,slope_3_gfx       ;
  CALL shift_slope_piece  ;
  LD HL,wall_shifted      ; Direction 14 (the wall), to wall_shifted
  EXX                     ;
  LD HL,wall_gfx          ;
  CALL shift_slope_piece  ;
  LD HL,flat_piece_shifted ; Direction 0 (flat), to flat_piece_shifted
  EXX                      ;
  LD HL,flat_piece_gfx     ;
  CALL shift_flat_piece    ;
  LD HL,flat_piece_two_shifted ; Direction 7 (the flat ceiling), to
  EXX                          ; flat_piece_two_shifted
  LD HL,flat_piece_two_gfx     ;
  CALL shift_flat_piece        ;
  OR $FF                  ; Ceilings: checkerboard on the left
  LD (piece_step_x),A     ;
  LD HL,slope_9_shifted   ; Direction 9, to slope_9_shifted
  EXX                     ;
  LD HL,slope_9_gfx       ;
  CALL shift_slope_piece  ;
  LD HL,slope_8_shifted   ; Direction 8, to slope_8_shifted
  EXX                     ;
  LD HL,slope_8_gfx       ;
  CALL shift_slope_piece  ;
  LD HL,slope_10_shifted  ; Direction 10, to slope_10_shifted
  EXX                     ;
  LD HL,slope_10_gfx      ;
  JP shift_slope_piece    ;

; Build the bit-reversed table at bit_reverse_table
;
; Used by the routine at sprite_buffer_two.
;
; Jumped to at the end of build_shift_tables, after build_shifted_pieces has
; run: bit_reverse_table + n gets n with its bits in the opposite order, for
; mirroring graphics: the ship when it faces left, the landscape's
; left-climbing pieces (blit_mirror_two, blit_mirror_one), and the sprite
; routines mirror_ship_sprite, draw_ship, blit_mirrored_left_clip and others.
; It overwrites the start-up routines from bit_reverse_table to
; shift_slope_piece's end, which are done with by then; the code map shows them
; because the snapshot was taken before the game ran.
build_bit_reverse:
  LD C,$00                ; From bit_reverse_table, for n = 0 to 255
  LD HL,bit_reverse_table ;
build_bit_reverse_0:
  LD B,$08                 ; Shift n's bits out of the top of D and into the
  LD D,C                   ; top of A
  XOR A                    ;
build_bit_reverse_1:
  SLA D                    ;
  RR A                     ;
  DJNZ build_bit_reverse_1 ;
  LD (HL),A                 ; Store, and the next
  INC HL                    ;
  INC C                     ;
  JR NZ,build_bit_reverse_0 ;
  RET

; Build the shift tables
;
; Used by the routine at start.
;
; Called once by the start (start). Makes three pairs of pages: for a shift of
; 2 pixels, shift_tables + n is n shifted right two bits and the next page's
; byte n the two bits that fall off the right, at the top of a byte; for 4,
; loader_copy and the page after it; for 6, loader_leftover_charset and the
; page after it. A byte drawn shifted is then two lookups: the left-hand screen
; byte gets the high page, the right-hand one the low page. These pages
; overwrite the end of the loaded block, the loader's copy routine at
; loader_copy and the loader's stack below loader_leftover_charset among it.
; Then the landscape pieces are shifted with them (build_shifted_pieces) and
; the bit-reversed table made (build_bit_reverse).
;
; The tables are used afterwards too: toggle_carried_part reads the loader_copy
; page, clamp_climb and clamp_object_speed the page after it.
build_shift_tables:
  LD HL,shift_tables      ; From shift_tables; two bits first
  LD B,$02                ;
; This entry point is used by the routine at sprite_buffer_two.
build_shift_tables_0:
  LD C,$00                ; DE = n shifted right B bits, across two bytes
; This entry point is used by the routine at sprite_buffer_two.
build_shift_tables_1:
  LD D,C
  LD E,$00
  LD A,B
build_shift_tables_2:
  SRL D
  RR E
  DEC A
  JR NZ,build_shift_tables_2 ; }
  LD (HL),D               ; The high part in this page, the low part in the
  INC H                   ; next
  LD (HL),E               ;

; Build the shift tables (continued); later, the second sprite buffer
;
; The rest of build_shift_tables's loop. After the start-up these bytes are the
; second half of the sprite-compositing buffer that begins inside
; build_shifted_pieces: new_beam_frame builds a sprite here (and old_beam_frame
; one 128 bytes lower).
sprite_buffer_two:
  DEC H                      ; Back to the high page; all 256 values
  INC HL                     ;
  INC C                      ;
  LD A,C                     ;
  AND A                      ;
  JR NZ,build_shift_tables_1 ;
  INC H                      ; The next pair of pages, two bits more, up to six
  INC B                      ;
  INC B                      ;
  LD A,B                     ;
  CP $08                     ;
  JR NZ,build_shift_tables_0 ;
  CALL build_shifted_pieces ; Shift the landscape pieces, and the bit-reversed
  JP build_bit_reverse      ; table

; Read a slope piece's header
;
; Used by the routine at shift_slope_piece.
;
; For shift_slope_piece: a slope's header is three bytes -- the number of rows
; in each variant, halved and rounded up (to row_count, and row_count_spare to
; reload it), the number of pairs of rows (row pairs to scratch_count and
; ground_cell), and the width in bytes (C). A flat piece's header is the last
; two of these (entered at its fourth instruction by shift_flat_piece). D is
; set to $FA, the first high page of the shift tables.
;
;   HL the header
; O:HL the graphic
; O:C its width in bytes
; O:D $FA
read_piece_header:
  LD A,(HL)               ; Rows in each variant, halved
  LD (row_count),A        ;
  LD (row_count_spare),A  ;
  INC HL                  ;
; This entry point is used by the routine at shift_flat_piece.
read_piece_header_0:
  LD A,(HL)               ; Pairs of rows
  LD (scratch_count),A    ;
  LD (ground_cell),A      ;
  INC HL                  ;
  LD D,$FA                ; The shift tables' first high page
  LD C,(HL)               ; The width in bytes
  INC HL                  ;
  RET                     ;

; Shift a graphic byte into a row being made left to right
;
; Used by the routines at shift_flat_piece and shift_slope_piece.
;
; ORs the high part of the byte at HL into the byte at HL' and moves on to the
; next, then ORs the low part into that one: the byte that spills over is left
; there for the next byte's high part to join.
;
; HL the graphic byte
; D the high page of the shift tables
; HL' the copy being made
shift_byte_forward:
  LD E,(HL)               ; High part, into this byte; on to the next
  LD A,(DE)               ;
  EXX                     ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC HL                  ;
  EXX                     ;
  INC D                   ; Low part, into the next byte
  LD A,(DE)               ;
  DEC D                   ;
  EXX                     ;
  OR (HL)                 ;
  LD (HL),A               ;
  EXX                     ;
  RET                     ;

; OR the checkerboard's high part into the copy, and move on
;
; Used by the routine at shift_slope_piece.
;
; Ends a row made left to right with the checkerboard's ($55) bits shifted in
; on the right. Entered at its third instruction with A ready, and at its
; fourth from the alternate set, by shift_flat_piece and shift_slope_piece.
pad_right_checker:
  LD E,$55                ; The high part of $55
  LD A,(DE)               ;
; This entry point is used by the routines at bit_reverse_table and
; shift_slope_piece.
pad_right_checker_0:
  EXX                     ; OR it in, and move on
; This entry point is used by the routine at bit_reverse_table.
pad_right_checker_1:
  OR (HL)
  LD (HL),A
  INC HL
  EXX
  RET                     ; }

; An INC HL never reached
;
; Nothing jumps here; pad_right_checker returns just before it.
unused_inc_hl:
  DEFB $23

; Fetch the low part of a graphic byte
;
; Used by the routine at shift_flat_piece.
;
; B = C first (the width), when entered here; at its second instruction just A
; = the low part of the byte at HL, its high part being left for the next call
; (shift_byte_high) to fetch.
;
; HL the graphic byte
; D the high page of the shift tables
shift_byte_low:
  LD B,C                  ; B is the width
; This entry point is used by the routines at bit_reverse_table and
; shift_slope_piece.
shift_byte_low_0:
  LD E,(HL)               ; A = the byte's low part
  INC D                   ;
  LD A,(DE)               ;
  DEC D                   ;
  RET                     ;

; OR a graphic byte's high part into the copy, and step to the next graphic
; byte
;
; Used by the routines at bit_reverse_table and shift_slope_piece.
;
; For a row being made right to left: the high part of the byte last fetched
; (E) goes into the copy's current byte, which is not moved on; HL steps to the
; next graphic byte. Entered at its third instruction with A ready.
shift_byte_high:
  INC HL                  ; Next graphic byte; A = the last one's high part
  LD A,(DE)               ;
; This entry point is used by the routine at shift_slope_piece.
shift_byte_high_0:
  EXX                     ; OR it into the copy
  OR (HL)                 ;
  LD (HL),A               ;
  EXX                     ;
  RET                     ;

; Start a graphic's next shifted copy
;
; Used by the routine at shift_slope_piece.
;
; Reloads the row counts from their spares (row_count_spare to row_count when
; entered here, ground_cell to scratch_count from its third instruction) and
; moves D on to the next pair of shift tables, two pages up. Returns with zero
; set when D has gone past $FF, that is, after the copy shifted by 6.
next_shift:
  LD A,(row_count_spare)  ; The rows in each variant
  LD (row_count),A        ;
; This entry point is used by the routine at bit_reverse_table.
next_shift_0:
  LD A,(ground_cell)      ; The pairs of rows
  LD (scratch_count),A    ;
  LD A,D                  ; The next shift; zero after the last
  ADD A,$02               ;
  LD D,A                  ;
  AND A                   ;
  RET                     ;

; Make a flat piece's shifted copies
;
; Used by the routine at build_shifted_pieces.
;
; For build_shifted_pieces, the two flat pieces (directions 0 and 7). A flat
; piece is three bytes wide and tiles: the ground under it repeats every 24
; pixels. So its rows are rotated rather than shifted: the bits that would fall
; off the right-hand end come back in at the left (the byte that would hold
; them is cleared and its bits ORed into the row's first byte), and nothing is
; added to the width. The row pairs alternate as in the blitter blit_flat_step:
; left to right, then right to left. After each copy, HL' skips 48 bytes: only
; the first 12 rows of each 28-row copy are made here, the other 16 rows of
; each having come on the tape already shifted (read from the simulator). The
; 12 made are the three 4-row variants of the step, the ground that repeats and
; so can be rotated; the 16 on the tape are the four end caps, already shifted
; (checked in the simulator for the Graphics page) -- presumably because a cap
; does not tile, so rotating it would wrap its edge round to the wrong side
; (assumed).
;
; At the end it patches a byte in the last row of each flat piece's copy
; shifted by 6 (flat_piece_shifted and flat_piece_two_shifted; to $5C and $40),
; perhaps to mend a pixel the rotation got wrong (not checked).
;
; HL the piece's header (two bytes: pairs of rows, width)
; HL' where the copy shifted by 2 goes; the copies shifted by 4 and 6 follow,
;     84 bytes apart
shift_flat_piece:
  CALL read_piece_header_0 ; Read the header
; This entry point is used by the routine at bit_reverse_table.
shift_flat_piece_0:
  PUSH HL                 ; Back here for each shift
; This entry point is used by the routine at bit_reverse_table.
shift_flat_piece_1:
  LD B,C                  ; A row left to right: width + 1 bytes shifted in
  INC B                   ;
shift_flat_piece_2:
  CALL shift_byte_forward ;
  INC HL                  ;
  DJNZ shift_flat_piece_2 ;
  EXX                     ; Rotate: the spill clears and ORs into the row's
  LD (HL),$00             ; first byte
  DEC HL                  ;
  DEC HL                  ;
  DEC HL                  ;
  OR (HL)                 ;
  LD (HL),A               ;
  INC HL                  ;
  INC HL                  ;
  INC HL                  ;
  EXX                     ;
  CALL shift_byte_low     ; A row right to left: keep the first byte's low part
  LD (pieces_left),A      ;

; Make a flat piece's shifted copies (continued); later, the bit-reversed table
;
; The rest of shift_flat_piece. Once the start-up is over this is the
; bit-reversed table build_bit_reverse builds: bit_reverse_table + n holds n
; with its bits reversed, 256 bytes over the end of shift_flat_piece and all of
; shift_slope_piece, which have done their work by then. The game reads it with
; LD B,$F9 and the byte in C, to mirror the ship (mirror_ship_sprite), the
; left-climbing landscape steps (blit_mirror_two, blit_mirror_one) and other
; sprites (draw_ship, blit_mirrored_left_clip).
bit_reverse_table:
  INC B                   ; One byte more than the width
bit_reverse_table_0:
  CALL shift_byte_high     ; Shift the bytes in
  CALL shift_byte_low_0    ;
  CALL pad_right_checker_0 ;
  DJNZ bit_reverse_table_0 ;
  INC D                   ; Take out the spill of the next row's byte
  LD A,(DE)               ;
  DEC D                   ;
  EXX                     ;
  DEC HL                  ;
  XOR (HL)                ;
  LD (HL),A               ;
  LD A,(pieces_left)       ; And put the first byte's low part in the last
  CALL pad_right_checker_1 ; byte, rotating
  LD A,(scratch_count)     ; The next pair of rows
  DEC A                    ;
  LD (scratch_count),A     ;
  JR NZ,shift_flat_piece_1 ;
  CALL next_shift_0        ; The next shift, 48 bytes further on
  EXX                      ;
  LD DE,$0030              ;
  ADD HL,DE                ;
  EXX                      ;
  POP HL                   ;
  JR NZ,shift_flat_piece_0 ;
  LD A,$5C                ; Two bytes of the copies mended
  LD ($EB34),A            ;
  LD A,$40                ;
  LD ($EC30),A            ;
  RET                     ;

; Make a slope piece's shifted copies
;
; Used by the routine at build_shifted_pieces.
;
; For build_shifted_pieces, the slopes and the wall. A slope is made one byte
; wider than its graphic, row by row, the rows alternating left to right and
; right to left like the blitters that draw them; the open side of each row is
; filled with the checkerboard's bits ($55 on rows made left to right, $AA on
; the others), on the right normally and on the left for the ceilings
; (piece_step_x set). The header's first count (row_count) cuts the zig-zag
; after each variant: a variant of 9 rows is 5 rows left to right and 4 back,
; so every variant begins with a row made left to right, as the blitter
; starting there expects.
;
; The pieces' graphics are packed so tightly that each copy reads a few bytes
; past its graphic's end (into the next graphic's header, or into the first
; copy made), giving a fourth, partial variant that the game never draws.
;
; HL the piece's header (three bytes; see read_piece_header)
; HL' where the copy shifted by 2 goes; the copies shifted by 4 and 6 follow it
;     directly
shift_slope_piece:
  CALL read_piece_header  ; Read the header
shift_slope_piece_0:
  PUSH HL                 ; Back here for each shift
shift_slope_piece_1:
  LD B,C                   ; A row left to right; ceilings: the checkerboard on
  LD A,(piece_step_x)      ; the left first
  AND A                    ;
  JR Z,shift_slope_piece_2 ;
  LD E,$55                 ;
  INC D                    ;
  LD A,(DE)                ;
  DEC D                    ;
  EXX                      ;
  OR (HL)                  ;
  LD (HL),A                ;
  EXX                      ;
shift_slope_piece_2:
  CALL shift_byte_forward  ; The row's bytes shifted in
  INC HL                   ;
  DJNZ shift_slope_piece_2 ;
  LD A,(piece_step_x)      ; Ceilings: the right-hand byte is left with the
  AND A                    ; spill only
  JR Z,shift_slope_piece_3 ;
  EXX                      ;
  INC HL                   ;
  EXX                      ;
  JR shift_slope_piece_4   ;
shift_slope_piece_3:
  CALL pad_right_checker  ; Floors: the checkerboard's bits on the right
shift_slope_piece_4:
  LD A,(row_count)         ; The variant's last row? Then no row back
  DEC A                    ;
  LD (row_count),A         ;
  JR Z,shift_slope_piece_8 ;
  LD A,(piece_step_x)       ; A row right to left; first style: the
  AND A                     ; checkerboard on the right first
  JR NZ,shift_slope_piece_5 ;
  LD E,$AA                  ;
  LD A,(DE)                 ;
  CALL shift_byte_high_0    ;
shift_slope_piece_5:
  LD B,C                   ; The row's bytes shifted in, right to left
shift_slope_piece_6:
  CALL shift_byte_low_0    ;
  CALL pad_right_checker_0 ;
  CALL shift_byte_high     ;
  DJNZ shift_slope_piece_6 ;
  LD A,(piece_step_x)      ; Ceilings: the checkerboard on the left
  AND A                    ;
  EXX                      ;
  JR Z,shift_slope_piece_7 ;
  EXX                      ;
  LD E,$AA                 ;
  INC D                    ;
  LD A,(DE)                ;
  DEC D                    ;
  EXX                      ;
  OR (HL)                  ;
  LD (HL),A                ;
shift_slope_piece_7:
  INC HL                  ; On past the row's last byte
  EXX                     ;
  JR shift_slope_piece_9  ;
shift_slope_piece_8:
  LD A,(row_count_spare)  ; Start the next variant
  LD (row_count),A        ;
shift_slope_piece_9:
  LD A,(scratch_count)      ; The next pair of rows
  DEC A                     ;
  LD (scratch_count),A      ;
  JR NZ,shift_slope_piece_1 ;
  POP HL                    ; The next shift
  CALL next_shift           ;
  JR NZ,shift_slope_piece_0 ;
  RET                       ;

; The rest of the bit-reversed table
;
; The bit-reversed table build_bit_reverse makes runs from bit_reverse_table to
; the end of this block, over the start-up routines shift_flat_piece and
; shift_slope_piece, which are finished with by then; the snapshot, taken
; before the game ran, holds zeros here. The shift tables follow
; (shift_tables).
bit_reverse_table_end:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$30

; The shift tables
;
; Six pages made by build_shift_tables at the start: shift_tables + n is n
; shifted right 2 bits, the next page's byte n the bits that fall off (at the
; top of the byte); loader_copy and the page after it the same for 4 bits,
; loader_leftover_charset and the page after it for 6. Before the start they
; hold what the loader left: undecoded bytes to the end of the game block, then
; the copy routine loader_copy, the loader's stack, and a character set.
shift_tables:
  DEFB $09,$1F,$42,$80,$9E,$1D,$22,$6A
  DEFB $DE,$9C,$93,$1F,$E2,$91,$9E,$1D
  DEFB $32,$71,$DE,$9C,$93,$1F,$62,$C9
  DEFB $9E,$1D,$02,$C6,$DE,$9C,$93,$1F
  DEFB $82,$A6,$9E,$1D,$12,$58,$DE,$2C
  DEFB $8E,$1F,$42,$B6,$9E,$1D,$72,$5D
  DEFB $DE,$2C,$8E,$6F,$FF,$2F,$53,$C2
  DEFB $15,$02,$DE,$9E,$1D,$D2,$62,$DE
  DEFB $9C,$93,$1F,$A2,$E9,$9E,$1D,$92
  DEFB $66,$DE,$9C,$93,$1F,$12,$FB,$9E
  DEFB $1D,$D2,$6D,$3E,$9C,$93,$EF,$00
  DEFB $10,$02,$90,$6F,$80,$10,$F5,$BA
  DEFB $2C,$B2,$FC,$01,$A1,$7F,$37,$C2
  DEFB $00,$12,$9F,$1C,$02,$A0,$6F,$20
  DEFB $E0,$00,$10,$E5,$01,$80,$B7,$AC
  DEFB $B3,$BC,$D1,$03,$92,$2F,$47,$32
  DEFB $57,$32,$C2,$90,$77,$0A,$B2,$4E
  DEFB $42,$40,$80,$E7,$8F,$00,$12,$DE
  DEFB $BC,$7E,$3F,$6C,$85,$EF,$27,$E3
  DEFB $C1,$25,$F3,$C1,$35,$E2,$27,$C3
  DEFB $C1,$25,$D3,$C1,$35,$62,$A1,$EF
  DEFB $34,$92,$EC,$A5,$91,$6D,$7B,$37
  DEFB $92,$4D,$A1,$51,$91,$6D,$7B,$97
  DEFB $9D,$EC,$51,$A5,$91,$6D,$7B,$37
  DEFB $92,$9D,$3C,$12,$E4,$45,$A1,$51
  DEFB $91,$3C,$A2,$91,$6D,$7B,$97,$9D
  DEFB $AC,$F3,$C1,$25,$E3,$C1,$A5,$D3
  DEFB $C1,$25,$C3,$C1,$A5,$67,$2C,$70
  DEFB $75,$9A,$DC,$DC,$89,$5F,$1E,$44
  DEFB $D0,$AC,$8A,$3F,$02,$A1,$9F,$6D
  DEFB $03,$B0,$B2,$B2,$62,$7B,$37,$32
  DEFB $32,$92,$DD,$3C,$8C,$2F,$03,$C2
  DEFB $45,$D0,$9C,$8C,$DF,$4C,$8C,$DF
  DEFB $CC,$8B,$0F,$51,$4F,$A1,$51,$91
  DEFB $BD,$E2,$7A,$A7,$03,$C2,$D5,$DC
  DEFB $8B,$AF,$C3,$C1,$D5,$23,$C3,$C1
  DEFB $05,$42,$DC,$6C,$8D,$9F,$1D,$01
  DEFB $03,$90,$91,$1D,$0E,$72,$EB,$C3
  DEFB $25,$43,$B3,$EE,$03,$24,$03,$C3
  DEFB $9E,$DC,$5C,$89,$5F,$1E,$A4,$53
  DEFB $C2,$75,$8A,$92,$E0,$51,$45,$A1
  DEFB $51,$91,$6D,$7B,$97,$DD,$AC,$8A
  DEFB $3F,$02,$A1,$AF,$53,$C2,$75,$8A
  DEFB $52,$90,$3D,$92,$8D,$31,$D0,$9C
  DEFB $8B,$AF,$E3,$C1,$D5,$23,$E3,$C1
  DEFB $85,$C2,$A2,$53,$C2,$75,$0A,$62
  DEFB $E0,$A1,$AA,$D1,$BC,$8C,$1F,$D4
  DEFB $4C,$8C,$DF,$CC,$8B,$DF,$9C,$8C
  DEFB $0F,$51,$AF,$53,$C2,$75,$9A,$8D
  DEFB $92,$90,$ED,$A1,$4A,$A1,$51,$91
  DEFB $6D,$7B,$37,$92,$8D,$61,$A0,$F3
  DEFB $C1,$25,$E3,$C1,$A5,$C3,$C1,$D5
  DEFB $23,$C3,$C1,$05,$82,$19,$DE,$0C
  DEFB $8D,$0F,$12,$99,$0C,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00

; The loader's copy routine
;
; Used by the routine at loader_entry.
;
; Put here by the PIC block (its first LDIR copies 16 bytes into place) and
; called by the loader's entry at loader_entry once the game block has loaded,
; 512 bytes higher than it runs: it moves the block down 512 bytes to
; game_entry. The entry then decodes it with RRD and jumps to game_entry.
; Overwritten at the start by the shift tables (build_shift_tables), so it ran
; only during loading.
loader_copy:
  LD HL,$5E80             ; From where the block loaded
  LD DE,game_entry        ; To where it runs (game_entry)
  LD BC,$9D80             ; The whole block, all of it moved
  LDIR                    ;
  RET                     ; Back to the loader

; The rest of the 16 bytes the PIC block copied
;
; LD SP,loader_leftover_charset and DI, the same as the start of the loader's
; entry at loader_entry; not run here. Then what lies below the loader's stack.
loader_leftover:
  DEFB $31,$00,$FE,$F3,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00

; The loader's stack
;
; The return address into loader_entry pushed when the entry at loader_entry
; called loader_copy, left below the loader's stack pointer
; loader_leftover_charset. The game's own stack is below loader_entry.
loader_stack:
  DEFW $5B87              ; Return address into the loader's entry

; What the loader left above its stack
;
; Zeros, then a character set from the loading; overwritten by the shift tables
; for 6 bits at the start (build_shift_tables).
loader_leftover_charset:
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $00,$00,$00,$00,$00,$00,$00,$00
  DEFB $F3,$0D,$CE,$0B,$E3,$50,$CE,$0B
  DEFB $E4,$50,$1D,$17,$DC,$0A,$CE,$0B
  DEFB $E7,$50,$1A,$17,$DC,$0A,$D7,$18
  DEFB $38,$00,$38,$00,$0D,$19,$DB,$02
  DEFB $DB,$02,$4D,$00,$EB,$53,$15,$00
  DEFB $EA,$53,$0C,$02,$5C,$0E,$C0,$57
  DEFB $71,$0E,$F3,$0D,$21,$17,$C6,$1E
  DEFB $00,$5E,$76,$1B,$03,$13,$00,$3E
  DEFB $00,$3C,$42,$42,$7E,$42,$42,$00
  DEFB $00,$7C,$42,$7C,$42,$42,$7C,$00
  DEFB $00,$3C,$42,$40,$40,$42,$3C,$00
  DEFB $00,$78,$44,$42,$42,$44,$78,$00
  DEFB $00,$7E,$40,$7C,$40,$40,$7E,$00
  DEFB $00,$7E,$40,$7C,$40,$40,$40,$00
  DEFB $00,$3C,$42,$40,$4E,$42,$3C,$00
  DEFB $00,$42,$42,$7E,$42,$42,$42,$00
  DEFB $00,$3E,$08,$08,$08,$08,$3E,$00
  DEFB $00,$02,$02,$02,$42,$42,$3C,$00
  DEFB $00,$44,$48,$70,$48,$44,$42,$00
  DEFB $00,$40,$40,$40,$40,$40,$7E,$00
  DEFB $00,$42,$66,$5A,$42,$42,$42,$00
  DEFB $00,$42,$62,$52,$4A,$46,$42,$00
  DEFB $00,$3C,$42,$42,$42,$42,$3C,$00
  DEFB $00,$7C,$42,$42,$7C,$40,$40,$00
  DEFB $00,$3C,$42,$42,$52,$4A,$3C,$00
  DEFB $00,$7C,$42,$42,$7C,$44,$42,$00
  DEFB $00,$3C,$40,$3C,$02,$42,$3C,$00
  DEFB $00,$FE,$10,$10,$10,$10,$10,$00
  DEFB $00,$42,$42,$42,$42,$42,$3C,$00

