


;----------------------------------------------------------
; level_2_load (27 lines)
level_2_load_002:
   PUSH AF                     ;

   CALL VDP_off                ; Stop VDP

   CALL load_tile_data_002     ; Load tiles
   CALL load_bg_data_002       ; Load background
   CALL load_palette_data_002  ; Load palette
   CALL initialize_score_bg2   ; Set up background in RAM
   CALL set_initial_sprites_level_2 ; Set up sprites
   CALL sprites_to_VRAM        ;

   LD a, STATE_LEVEL_2_RUN     ; Go to next state
   LD (RAM_GAME_STATE), a      ;

   LD a, LEVEL_TIMER_START     ; 99 second timer
   LD (RAM_TIME_1), a          ; Time 1
   LD (RAM_TIME_2), a          ; Time 2
   LD a, LEVEL_2_HEALTH        ;
   LD (RAM_HEALTH_1), a        ; Health

   LD a, 240                   ; Foes start 240 frames after clock starts.
   LD (RAM_FOE_1_COUNT), a     ;
   LD (RAM_FOE_2_COUNT), a     ;
   LD (RAM_FOE_3_COUNT), a     ;
   LD (RAM_FOE_4_COUNT), a     ;
   LD a, 120                   ; Foes start 120 frames after clock starts.
   LD (RAM_FOE_5_COUNT), a     ;
   LD (RAM_FOE_6_COUNT), a     ;
   LD (RAM_FOE_7_COUNT), a     ;
   LD (RAM_FOE_8_COUNT), a     ;

   LD a, $7F                   ; Enable scrolling
   LD (RAM_ENABLE_SCROLLING), a ;

   CALL copy_song_to_RAM003    ; Start a level

   CALL VDP_on                 ; Start VDP

   POP AF                      ;
   RET                         ; End subroutine 
;----------------------------------------------------------

;----------------------------------------------------------
; level_2_run_002 (9 lines)
;
; Run level 1.
; * Copy score from RAM to VRAM 
; * Make pig fly
; * Make foes and points move
; * Collision detection
; * Check for no health (game over)
; * Check for no time (level complete)
;----------------------------------------------------------
level_2_run_002:

; Copy score from RAM to VRAM
   CALL copy_score_bg          ; Copy score from RAM to VRAM

; Copy sprites from RAM to VRAM
   CALL sprites_to_VRAM        ; Copy sprites to VRAM

; Run everything that does not use VRAM
   CALL set_score_level_2      ; Set the score in RAM
   CALL run_timer_level_2      ; Count down to 00. At 00, go to next level.
   CALL pig_flight_level_2     ; Flap pig wings
   CALL move_foes_level_2      ; Move the foes
   CALL lv2_collision_detect   ; Shot hit foe?
   CALL pig_got_hit_lv2        ; Pig got hit?

   RET                         ; End subroutine 
;----------------------------------------------------------

;----------------------------------------------------------
; load_tile_data_002 (12 lines)
;
; Load tile data to $0000.
;----------------------------------------------------------
load_tile_data_002: 
   LD a, $00                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $00                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, tiles_002 + 1        ; tile_data is defined below
   LD c, VDP_DATA              ; C <- $BE;   LD b, 128  

   LD e, 156                   ; Set up counter (310 tiles, 156 sets of 2 tiles)

tile_load_loopt2:
   LD b, 64                    ; Write 2 tiles (64 characters)               
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   DEC e                       ;
   JR NZ, tile_load_loopt2     ; Loop until de is empty.

   RET                         ; End subroutine 
;---------------------------------------

;----------------------------------------------------------
; load_bg_data_002 (12 lines)
;
; Load background data
;----------------------------------------------------------
load_bg_data_002: 
   LD a, $FF                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $37                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, background_002       ; background_000 is defined below
   LD c, VDP_DATA              ; C <- $BE;   LD b, 128  

   LD e, 24                    ; Set up counter (24 rows)

tile_load_loopbg2:
   LD b, 64                    ; Write 1 row (64 characters)               
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   DEC e                       ;
   JR NZ, tile_load_loopbg2    ; Loop until de is empty.

   RET                         ; End subroutine 
;---------------------------------------

;----------------------------------------------------------
; load_palette_data_001 (12 lines)
;
; Load palette data.
;----------------------------------------------------------
load_palette_data_002:
   LD a, $00                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $C0                   ; High address byte
   OUT (VDP_ADDR),a            ;

   LD hl, palette_001          ; 
   LD b, 32                    ;
   CALL copy_to_vdp_loop       ; HL -> VDP, until B goes to 0

   LD hl, palette_001          ; Copy restore color
   LD de, RAM_RESTORE_COLOR    ;
   LD bc, 32                   ;
   LDIR                        ;

   RET                         ; End subroutine  
;---------------------------------------

;---------------------------------------
; initialize_score_bg2 (10 lines)
;
;---------------------------------------
initialize_score_bg2:
   LD hl, background_002 + 64  ; Initial background
   LD de, RAM_SCORE_BG         ; RAM location
   LD b, 64*2                  ; Set B

isbg_loop_002:
   LD a, (hl)                  ; Copy
   LD (de), a                  ;
   INC hl                      ; Increment
   INC de                      ; Increment
   DEC b                       ; 

   JP NZ, isbg_loop_002        ;
   RET                         ; End subroutine 
;---------------------------------------

;---------------------------------------
; set_initial_sprites_level_2 (17 lines)
;
; Copy initial sprite values to RAM.
;---------------------------------------
set_initial_sprites_level_2:
   LD hl, vpos_sprites_level_2 ; VPOS
   LD de, RAM_VPOS            ; 
   LD b, 45                   ;
   CALL sisl1_copy_loop       ;

   LD hl, hpos_sprites_level_2 ; HPOS, TILE
   LD de, RAM_HPOS            ; 
   LD b, 89                   ;
   CALL sisl2_copy_loop       ;

   RET                        ; End subroutine 

sisl2_copy_loop:
   LD a, (hl)                 ; Read tile data
   LD (de), a                 ; Write to VRAM
   INC hl                     ; Increment hl
   INC de                     ; Increment de

   DEC b                      ; Loop until all data is copied.
   JP NZ, sisl2_copy_loop     ; 

   RET                        ; End subroutine 
;---------------------------------------

vpos_sprites_level_2:
.db $70 ; Pig 00
.db $70 ;
.db $70 ;
.db $70 ;
.db $78 ; 
.db $78 ;
.db $78 ;
.db $78 ;
.db $00 ; Shot 1 8
.db $00 ;
.db $08 ;
.db $08 ;
.db $20 ; Foe 1 12
.db $20 ;
.db $28 ;
.db $28 ;
.db $20 ; Foe 2 16
.db $20 ;
.db $28 ;
.db $28 ;
.db $20 ; Foe 3 20
.db $20 ;
.db $28 ;
.db $28 ;
.db $20 ; Foe 4 24
.db $20 ;
.db $28 ;
.db $28 ;
.db $60 ; Foe 5 28
.db $60 ;
.db $68 ;
.db $68 ;
.db $70 ; Foe 6 32
.db $70 ;
.db $78 ;
.db $78 ;
.db $80 ; Foe 7 36
.db $80 ;
.db $88 ;
.db $88 ;
.db $90 ; Foe 8 40
.db $90 ;
.db $98 ;
.db $98 ;
.db $D0 ; Delimiter

hpos_sprites_level_2:
.db $70, $01 ; Pig 00
.db $78, $02 ;
.db $80, $03 ;
.db $88, $04 ;
.db $70, $1D ; 
.db $78, $1E ;
.db $80, $1F ;
.db $88, $00 ;
.db $00, $39 ; Shot 1 16
.db $08, $3A ;
.db $00, $3F ;
.db $08, $40 ;
.db $30, $00 ; Foe 1 24
.db $38, $00 ;
.db $30, $00 ;
.db $38, $00 ;
.db $60, $00 ; Foe 2 32
.db $68, $00 ;
.db $60, $00 ;
.db $68, $00 ;
.db $90, $00 ; Foe 3 40
.db $98, $00 ;
.db $90, $00 ;
.db $98, $00 ;
.db $C0, $00 ; Foe 4 48
.db $C8, $00 ;
.db $C0, $00 ;
.db $C8, $00 ;
.db $00, $00 ; Foe 5 56
.db $08, $00 ;
.db $00, $00 ;
.db $08, $00 ;
.db $00, $00 ; Foe 6 64
.db $08, $00 ;
.db $00, $00 ;
.db $08, $00 ;
.db $00, $00 ; Foe 7 72
.db $08, $00 ;
.db $00, $00 ;
.db $08, $00 ;
.db $00, $00 ; Foe 8 80
.db $08, $00 ;
.db $00, $00 ;
.db $08, $00 ;
.db $D0 ; Delimiter

;---------------------------------------
; copy_score_bg2 (23 lines)
;
; Copy score data from RAM to background
;---------------------------------------
copy_score_bg2:
; Copy row 1 or row 2?
   LD a, (RAM_FRAME_CTR)       ; Frame counter
   AND $01                     ; Mask all but last bit
   JP NZ, csbg2_row2            ;

; Row 1
   LD a, $3F                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $38                   ; High address byte
   OUT (VDP_ADDR),a            ;
   LD hl, RAM_SCORE_BG         ; RAM source
   JP csbg2_start_copy          ;
 
; Row 2
csbg2_row2:
   LD a, $7F                   ; Low address byte
   OUT (VDP_ADDR),a            ;
   LD a, $38                   ; High address byte
   OUT (VDP_ADDR),a            ;
   LD hl, RAM_SCORE_BG + 64    ; RAM source

csbg2_start_copy:
   LD b, 64                    ; Write 1 row (64 characters)               
   CALL tool_copy_loop2        ; HL -> VDP, until B goes to 0

   RET                         ; End subroutine 

tool_copy_loop2:
   LD a, (hl)                 ; Read tile data
   OUT (VDP_DATA), a          ; Write to VRAM
   INC hl                     ; Increment hl

   DEC b                      ; Loop until all data is copied.
   JP NZ, tool_copy_loop2     ; 

   RET                        ; End subroutine
;---------------------------------------

;---------------------------------------
; set_score_level_2 (55 lines)
;
; Translate score to background info.
;
; 7 digits for score
; 1 digit for health
; 4 digits for time
;---------------------------------------
set_score_level_2:
   PUSH AF                    ;
   PUSH BC                    ;

   LD a, (RAM_SCORE_1)        ; Digit 1
   LD c, a                    ;
   LD de, $C306               ;
   LD hl, $C346               ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_2)        ; Digit 2
   LD c, a                    ;
   LD de, $C306 + 4           ;
   LD hl, $C346 + 4           ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_3)        ; Digit 3
   LD c, a                    ;
   LD de, $C306 + 8           ;
   LD hl, $C346 + 8           ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_4)        ; Digit 4
   LD c, a                    ;
   LD de, $C306 + 12          ;
   LD hl, $C346 + 12          ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_5)        ; Digit 5
   LD c, a                    ;
   LD de, $C306 + 16          ;
   LD hl, $C346 + 16          ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_6)        ; Digit 6
   LD c, a                    ;
   LD de, $C306 + 20          ;
   LD hl, $C346 + 20          ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_SCORE_7)        ; Digit 7
   LD c, a                    ;
   LD de, $C306 + 24          ;
   LD hl, $C346 + 24          ;
   CALL number_to_score_level_2 ;

   LD a, (RAM_HEALTH_1)       ; Health
   LD c, a                    ;
   LD de, $C326               ;
   LD hl, $C366               ;
   CALL number_to_score_level_2 ;   

   LD a, (RAM_TIME_1)         ; Time 1
   LD c, a                    ;
   LD de, $C336               ;
   LD hl, $C376               ;
   CALL number_to_score_level_2 ; 

   LD a, (RAM_TIME_2)         ; Time 2
   LD c, a                    ;
   LD de, $C336 + 4           ;
   LD hl, $C376 + 4           ;
   CALL number_to_score_level_2 ; 

   POP BC                     ;
   POP AF                     ;
   RET                        ; End subroutine
;---------------------------------------

;---------------------------------------
; number_to_score_level_2 (240 lines)
;
; C = number
; DE = location to write to
; HL = location to write to
;---------------------------------------
number_to_score_level_2:
   LD a, c                    ; Translate
   CP 0                       ;
   JP Z, ntsl2_0              ;
   LD a, c                    ; Translate
   CP 1                       ;
   JP Z, ntsl2_1              ;
   LD a, c                    ; Translate
   CP 2                       ;
   JP Z, ntsl2_2              ;
   LD a, c                    ; Translate
   CP 3                       ;
   JP Z, ntsl2_3              ;
   LD a, c                    ; Translate
   CP 4                       ;
   JP Z, ntsl2_4              ;
   LD a, c                    ; Translate
   CP 5                       ;
   JP Z, ntsl2_5              ;
   LD a, c                    ; Translate
   CP 6                       ;
   JP Z, ntsl2_6              ;
   LD a, c                    ; Translate
   CP 7                       ;
   JP Z, ntsl2_7              ;
   LD a, c                    ; Translate
   CP 8                       ;
   JP Z, ntsl2_8              ;
   LD a, c                    ; Translate
   CP 9                       ;
   JP Z, ntsl2_9              ;

ntsl2_0:
   LD a, $41                  ; 0
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $41                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $02                  ;
   LD (de), a                 ;

   LD a, $5B                  ; 0
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $5C                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_1:
   LD a, $42                  ; 1
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $43                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $42                  ; 1
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $04                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $43                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $04                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_2:
   LD a, $44                  ; 2
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $45                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $5D                  ; 2
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $5E                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_3:
   LD a, $46                  ; 3
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $47                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $5F                  ; 3
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $60                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_4:
   LD a, $48                  ; 4
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $49                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $61                  ; 4
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $62                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_5:
   LD a, $4A                  ; 5
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $4B                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $63                  ; 5
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $64                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_6:
   LD a, $4C                  ; 6
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $4D                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $65                  ; 6
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $66                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_7:
   LD a, $4E                  ; 7
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $4F                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $67                  ; 7
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $68                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_8:
   LD a, $50                  ; 8
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $51                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $69                  ; 8
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $6A                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   JP ntsl2_copy_end          ;

ntsl2_9:
   LD a, $52                  ; 9
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $53                  ;
   LD (de), a                 ;
   INC de                     ;
   LD a, $00                  ;
   LD (de), a                 ;

   LD a, $6B                  ; 9
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $6C                  ;
   LD (hl), a                 ;
   INC hl                     ;
   LD a, $00                  ;
   LD (hl), a                 ;

ntsl2_copy_end:
   RET                        ; End subroutine   
;---------------------------------------


;---------------------------------------
; run_timer_level_2 (18 lines)
;
; Count down the timer digits.
;---------------------------------------
run_timer_level_2:
   LD a, (RAM_FRAME_CTR)      ; Frame counter
   CP 59                      ; 60 frames per second
   JP NZ, rtlv2_skip_000      ;

   LD a, (RAM_TIME_2)         ; Time 2
   DEC a                      ;
   LD (RAM_TIME_2), a         ;

   CP $FF                     ; at -1? 
   JP NZ, rtlv2_skip_000      ;

   LD a, $09                  ;
   LD (RAM_TIME_2), a         ;

   LD a, (RAM_TIME_1)         ; Time 2
   DEC a                      ;
   LD (RAM_TIME_1), a         ;

   CP $FF                     ; at -1? 
   JP NZ, rtlv2_skip_000      ;

   LD a, STATE_LEVEL_3_LOAD   ; Go to next level
   LD (RAM_GAME_STATE), a     ;

rtlv2_skip_000:
   RET                        ; End subroutine 
;---------------------------------------

;---------------------------------------
; pig_flight_level_2 (83 lines)
;
; Make the pig fly.
;---------------------------------------
pig_flight_level_2:
   LD a, (RAM_PIG_BLINK)      ;
   CP $00                     ;
   JP NZ, pflv2_end2          ;

   LD a, (RAM_FRAME_CTR)      ; Frame counter
   CP 0                       ; flap
   JP Z, pflv2_000            ;
   CP 15                      ; flap
   JP Z, pflv2_001            ;
   CP 30                      ; flap
   JP Z, pflv2_002            ;
   CP 45                      ; flap
   JP Z, pflv2_001            ;
   JP pflv2_end               ;

; High wing
pflv2_000:
   LD a, $01                  ;
   LD (RAM_HPOS + 1), a       ;
   LD a, $02                  ;
   LD (RAM_HPOS + 3), a       ;
   LD a, $03                  ;
   LD (RAM_HPOS + 5), a       ;
   LD a, $04                  ;
   LD (RAM_HPOS + 7), a       ;
   LD a, $1D                  ;
   LD (RAM_HPOS + 9), a       ;
   LD a, $1E                  ;
   LD (RAM_HPOS + 11), a      ;
   LD a, $1F                  ;
   LD (RAM_HPOS + 13), a      ;
   LD a, $00                  ;
   LD (RAM_HPOS + 15), a      ;
   JP pflv2_end               ;

; Middle wing
pflv2_001:
   LD a, $05                  ;
   LD (RAM_HPOS + 1), a       ;
   LD a, $06                  ;
   LD (RAM_HPOS + 3), a       ;
   LD a, $03                  ;
   LD (RAM_HPOS + 5), a       ;
   LD a, $07                  ;
   LD (RAM_HPOS + 7), a       ;
   LD a, $20                  ;
   LD (RAM_HPOS + 9), a       ;
   LD a, $21                  ;
   LD (RAM_HPOS + 11), a      ;
   LD a, $1F                  ;
   LD (RAM_HPOS + 13), a      ;
   LD a, $00                  ;
   LD (RAM_HPOS + 15), a      ;
   JP pflv2_end               ;

; Low wing
pflv2_002:
   LD a, $08                  ;
   LD (RAM_HPOS + 1), a       ;
   LD a, $09                  ;
   LD (RAM_HPOS + 3), a       ;
   LD a, $03                  ;
   LD (RAM_HPOS + 5), a       ;
   LD a, $04                  ;
   LD (RAM_HPOS + 7), a       ;
   LD a, $22                  ;
   LD (RAM_HPOS + 9), a       ;
   LD a, $23                  ;
   LD (RAM_HPOS + 11), a      ;
   LD a, $1F                  ;
   LD (RAM_HPOS + 13), a      ;
   LD a, $00                  ;
   LD (RAM_HPOS + 15), a      ;
   JP pflv2_end               ;

pflv2_end2:
   LD a, (RAM_PIG_BLINK)      ; Make the pig blink
   DEC a                      ;
   LD (RAM_PIG_BLINK), a      ;

   LD a, $0A                  ;
   LD (RAM_HPOS + 1), a       ;
   LD a, $0B                  ;
   LD (RAM_HPOS + 3), a       ;
   LD a, $0C                  ;
   LD (RAM_HPOS + 5), a       ;
   LD a, $0D                  ;
   LD (RAM_HPOS + 7), a       ;
   LD a, $1F                  ;
   LD (RAM_HPOS + 9), a       ;
   LD a, $24                  ;
   LD (RAM_HPOS + 11), a      ;
   LD a, $25                  ;
   LD (RAM_HPOS + 13), a      ;
   LD a, $00                  ;
   LD (RAM_HPOS + 15), a      ;
pflv2_end:
   RET                        ; End subroutine 
;---------------------------------------


;----------------------------------------
; move_foes_level_2 (413 lines) 
;
; Move all 8 foes.
; 
; Each foe has two bytes in RAM that
; define it's behavior.
; byte 1 - Terminate counter
;          If zero, move as normal.
;          else, count down to zero, 
;          then respawn. 
; byte 2 - Algorithm temp byte.
;          Some movement algorithms may
;          require a state variable.
;
; RAM_FOE_1_COUNT
; RAM_FOE_1_STATE
; RAM_FOE_2_COUNT
; RAM_FOE_2_STATE
; RAM_FOE_3_COUNT
; RAM_FOE_3_STATE
; RAM_FOE_4_COUNT
; RAM_FOE_4_STATE
; RAM_FOE_5_COUNT
; RAM_FOE_5_STATE
; RAM_FOE_6_COUNT
; RAM_FOE_6_STATE
; RAM_FOE_7_COUNT
; RAM_FOE_7_STATE
; RAM_FOE_8_COUNT
; RAM_FOE_8_STATE
;
; Foe 1 - Icecicle
; Foe 2 - Icecicle
; Foe 3 - Icecicle
; Foe 4 - Icecicle
; Foe 5 - Snowman hat
; Foe 6 - Snowman head
; Foe 7 - Snowman body
; Foe 8 - Snowman feet
;----------------------------------------
move_foes_level_2:

; Foe 1 - Icecicle
  LD hl, RAM_FOE_1_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_001    ;

; Go down until hit end of screen.
; Then terminate.
  LD hl, RAM_VPOS + 12          ; Go down
  CALL lv2_down_subroutine      ;

  LD hl, RAM_VPOS + 12          ; Go down
  LD a, (hl)                    ; At end of screen?
  CP $C0                        ;
  JP Z, mflv2_skip_000          ;
  LD hl, RAM_HPOS + 24          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, $18                     ;; 18 19 31 32
  LD (RAM_HPOS + 25), a         ;
  LD a, $19                     ;; 18 19 31 32
  LD (RAM_HPOS + 27), a         ;
  LD a, $31                     ;; 18 19 31 32
  LD (RAM_HPOS + 29), a         ;
  LD a, $32                     ;; 18 19 31 32
  LD (RAM_HPOS + 31), a         ;
  JP mflv2_next_002             ; Go to next foe

mflv2_skip_000:
  LD a, 60                      ; Destroy icecicle 
  LD (RAM_FOE_1_COUNT), a       ;
  LD hl, RAM_FOE_1_COUNT        ;

mflv2_countdown_001:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_002         ;
  LD hl, vpos_sprites_level_2 + 12 ; Y respawn
  LD de, RAM_VPOS + 12          ;
  CALL copy_4_vpos_lv2          ;

;------------------------------------------
; Foe 2 - Icecicle
mflv2_next_002:
  LD hl, RAM_FOE_2_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_002    ;

; Go down until hit end of screen.
; Then terminate.
  LD hl, RAM_VPOS + 16          ; Go down
  CALL lv2_down_subroutine      ;

  LD hl, RAM_VPOS + 16          ; Go down
  LD a, (hl)                    ; At end of screen?
  CP $C0                        ;
  JP Z, mflv2_skip_001          ;
  LD hl, RAM_HPOS + 32          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, $18                     ;; 18 19 31 32
  LD (RAM_HPOS + 33), a         ;
  LD a, $19                     ;; 18 19 31 32
  LD (RAM_HPOS + 35), a         ;
  LD a, $31                     ;; 18 19 31 32
  LD (RAM_HPOS + 37), a         ;
  LD a, $32                     ;; 18 19 31 32
  LD (RAM_HPOS + 39), a         ;
  JP mflv2_next_003             ; Go to next foe

mflv2_skip_001:
  LD a, 60                      ; Destroy icecicle 
  LD (RAM_FOE_2_COUNT), a       ;
  LD hl, RAM_FOE_2_COUNT        ;

mflv2_countdown_002:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_003         ;
  LD hl, vpos_sprites_level_2 + 16 ; Y respawn
  LD de, RAM_VPOS + 16          ;
  CALL copy_4_vpos_lv2          ;

;------------------------------------------
; Foe 3 - Icecicle
mflv2_next_003:
  LD hl, RAM_FOE_3_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_003    ;

; Go down until hit end of screen.
; Then terminate.
  LD hl, RAM_VPOS + 20          ; Go down
  CALL lv2_down_subroutine      ;

  LD hl, RAM_VPOS + 20          ; Go down
  LD a, (hl)                    ; At end of screen?
  CP $C0                        ;
  JP Z, mflv2_skip_003          ;
  LD hl, RAM_HPOS + 40          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, $18                     ;; 18 19 31 32
  LD (RAM_HPOS + 41), a         ;
  LD a, $19                     ;; 18 19 31 32
  LD (RAM_HPOS + 43), a         ;
  LD a, $31                     ;; 18 19 31 32
  LD (RAM_HPOS + 45), a         ;
  LD a, $32                     ;; 18 19 31 32
  LD (RAM_HPOS + 47), a         ;
  JP mflv2_next_004             ; Go to next foe

mflv2_skip_003:
  LD a, 60                      ; Destroy icecicle 
  LD (RAM_FOE_2_COUNT), a       ;
  LD hl, RAM_FOE_3_COUNT        ;

mflv2_countdown_003:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_004         ;
  LD hl, vpos_sprites_level_2 + 20 ; Y respawn
  LD de, RAM_VPOS + 20          ;
  CALL copy_4_vpos_lv2          ;


;------------------------------------------
; Foe 4 - Icecicle
mflv2_next_004:
  LD hl, RAM_FOE_4_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_004    ;

; Go down until hit end of screen.
; Then terminate.
  LD hl, RAM_VPOS + 24          ; Go down
  CALL lv2_down_subroutine      ;

  LD hl, RAM_VPOS + 24          ; Go down
  LD a, (hl)                    ; At end of screen?
  CP $C0                        ;
  JP Z, mflv2_skip_004          ;
  LD hl, RAM_HPOS + 48          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, $18                     ;; 18 19 31 32
  LD (RAM_HPOS + 49), a         ;
  LD a, $19                     ;; 18 19 31 32
  LD (RAM_HPOS + 51), a         ;
  LD a, $31                     ;; 18 19 31 32
  LD (RAM_HPOS + 53), a         ;
  LD a, $32                     ;; 18 19 31 32
  LD (RAM_HPOS + 55), a         ;
  JP mflv2_next_005             ; Go to next foe

mflv2_skip_004:
  LD a, 60                      ; Destroy icecicle 
  LD (RAM_FOE_2_COUNT), a       ;
  LD hl, RAM_FOE_4_COUNT        ;

mflv2_countdown_004:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_005         ;
  LD hl, vpos_sprites_level_2 + 24 ; Y respawn
  LD de, RAM_VPOS + 24          ;
  CALL copy_4_vpos_lv2          ;

;------------------------------------------
; Foe 5 - Snowman hat
mflv2_next_005:
  LD hl, RAM_FOE_5_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_005    ;

  LD a, $12                     ;; 12 13 2B 2C
  LD (RAM_HPOS + 57), a         ;
  LD a, $13                     ;; 12 13 2B 2C
  LD (RAM_HPOS + 59), a         ;
  LD a, $2B                     ;; 12 13 2B 2C
  LD (RAM_HPOS + 61), a         ;
  LD a, $2C                     ;; 12 13 2B 2C
  LD (RAM_HPOS + 63), a         ;

  LD hl, RAM_HPOS + 56          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ;
  LD hl, RAM_VPOS + 28          ;
  CP $00                        ;
  CALL Z, lv2_up_subroutine     ; Go up 
  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ; 
  CP $01                        ;
  CALL Z, lv2_down_subroutine   ; Go down
  JP mflv2_next_006             ;

mflv2_countdown_005:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_006         ;
  LD hl, vpos_sprites_level_2 + 28 ; Y respawn
  LD de, RAM_VPOS + 28          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 56 ; X respawn
  LD de, RAM_HPOS + 56          ;
  CALL copy_4_hpos_lv2          ;

;------------------------------------------
; Foe 6 - Snowman head
mflv2_next_006:
  LD hl, RAM_FOE_6_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_006    ;

  LD a, $10                     ;; 10 11 29 2A
  LD (RAM_HPOS + 65), a         ;
  LD a, $11                     ;; 10 11 29 2A
  LD (RAM_HPOS + 67), a         ;
  LD a, $29                     ;; 10 11 29 2A
  LD (RAM_HPOS + 69), a         ;
  LD a, $2A                     ;; 10 11 29 2A
  LD (RAM_HPOS + 71), a         ;

  LD hl, RAM_HPOS + 64          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ;
  LD hl, RAM_VPOS + 32          ;
  CP $00                        ;
  CALL Z, lv2_up_subroutine     ; Go up  
  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ;
  CP $01                        ;
  CALL Z, lv2_down_subroutine   ; Go down
  JP mflv2_next_007             ;

mflv2_countdown_006:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_007         ;
  LD hl, vpos_sprites_level_2 + 32 ; Y respawn
  LD de, RAM_VPOS + 32          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 64 ; X respawn
  LD de, RAM_HPOS + 64          ;
  CALL copy_4_hpos_lv2          ;

;------------------------------------------
; Foe 7 - Snowman body
mflv2_next_007:
  LD hl, RAM_FOE_7_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_007    ;

  LD a, $0E                     ;; 0E 0F 27 28
  LD (RAM_HPOS + 73), a         ;
  LD a, $0F                     ;; 10 11 29 2A
  LD (RAM_HPOS + 75), a         ;
  LD a, $27                     ;; 10 11 29 2A
  LD (RAM_HPOS + 77), a         ;
  LD a, $28                     ;; 10 11 29 2A
  LD (RAM_HPOS + 79), a         ;

  LD hl, RAM_HPOS + 72          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ;
  LD hl, RAM_VPOS + 36          ;
  CP $00                        ;
  CALL Z, lv2_up_subroutine     ; Go up 
  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ; 
  CP $01                        ;
  CALL Z, lv2_down_subroutine   ; Go down
  JP mflv2_next_008             ;

mflv2_countdown_007:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_008         ;
  LD hl, vpos_sprites_level_2 + 36 ; Y respawn
  LD de, RAM_VPOS + 36          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 72 ; X respawn
  LD de, RAM_HPOS + 72          ;
  CALL copy_4_hpos_lv2          ;

;------------------------------------------
; Foe 7 - Snowman body
mflv2_next_008:
  LD hl, RAM_FOE_8_COUNT        ;
  LD a, (hl)                    ; Termination counter expired? 
  CP $00                        ;
  JP NZ, mflv2_countdown_008    ;

  LD a, $14                     ;; 14 15 2D 2E
  LD (RAM_HPOS + 81), a         ;
  LD a, $15                     ;; 14 15 2D 2E
  LD (RAM_HPOS + 83), a         ;
  LD a, $2D                     ;; 14 15 2D 2E
  LD (RAM_HPOS + 85), a         ;
  LD a, $2E                     ;; 14 15 2D 2E
  LD (RAM_HPOS + 87), a         ;

  LD hl, RAM_HPOS + 80          ; Go right, slowly
  LD a, (RAM_FRAME_CTR)         ;
  AND $03                       ;
  CP $00                        ;
  CALL Z, lv2_right_subroutine  ;

  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ;
  LD hl, RAM_VPOS + 40          ;
  CP $00                        ;
  CALL Z, lv2_up_subroutine     ; Go up 
  LD a, (RAM_SECOND_CTR)        ; Bounce up and down.
  AND $01                       ; 
  CP $01                        ;
  CALL Z, lv2_down_subroutine   ; Go down
  JP mflv2_next_009             ;

mflv2_countdown_008:
  DEC (hl)                      ; Decrement counter

  LD a, (hl)                    ; Respawn?
  CP $00                        ;
  JP NZ, mflv2_next_009         ;
  LD hl, vpos_sprites_level_2 + 40 ; Y respawn
  LD de, RAM_VPOS + 40          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 80 ; X respawn
  LD de, RAM_HPOS + 80          ;
  CALL copy_4_hpos_lv2          ;

mflv2_next_009:
   RET                        ; End subroutine 
;----------------------------------------

;----------------------------------------
; lv2_up_subroutine (8 lines)
; HL = VPOS
;----------------------------------------
lv2_up_subroutine:
  DEC (hl)                      ;
  INC hl                        ;
  DEC (hl)                      ;
  INC hl                        ;
  DEC (hl)                      ;
  INC hl                        ;
  DEC (hl)                      ;
  
  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; lv2_down_subroutine (8 lines)
; HL = VPOS
;----------------------------------------
lv2_down_subroutine:
  INC (hl)                      ;
  INC hl                        ;
  INC (hl)                      ;
  INC hl                        ;
  INC (hl)                      ;
  INC hl                        ;
  INC (hl)                      ;
  
  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; lv2_right_subroutine (11 lines)
; HL = HPOS
;----------------------------------------
lv2_right_subroutine:
  INC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  INC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  INC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  INC (hl)                      ;
  
  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; lv2_left_subroutine (11 lines)
; HL = HPOS
;----------------------------------------
lv2_left_subroutine:
  DEC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  DEC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  DEC (hl)                      ;
  INC hl                        ;
  INC hl                        ;
  DEC (hl)                      ;
  
  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; copy_4_vpos_lv2 (8 lines)
;
; HL = source
; DE = destination
;----------------------------------------
copy_4_vpos_lv2:
  LD b, 4            ;
copy_4_vpos_lv2_loop:
  LD a, (hl)         ; 
  LD (de), a         ; 
  INC hl             ;
  INC de             ;
  DEC b              ;
  JP NZ, copy_4_vpos_lv2_loop ;

  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; copy_4_hpos_lv2 (10 lines)
;
; HL = source
; DE = destination
;----------------------------------------
copy_4_hpos_lv2:
  LD b, 4            ;
copy_4_hpos_lv2_loop:
  LD a, (hl)         ; 
  LD (de), a         ; 
  INC hl             ;
  INC de             ;
  INC hl             ;
  INC de             ;
  DEC b              ;
  JP NZ, copy_4_hpos_lv2_loop ;

  RET                           ; End subroutine 
;----------------------------------------


;-----------------------------------------
; lv2_collision_detect (135 lines)
;
; Did the light gun hit a target?
;
; RAM_COLL_X1
; RAM_COLL_X2
; RAM_COLL_Y1
; RAM_COLL_Y2
; RAM_COLL_RESULT
; CALL xy_collision_detect
;-----------------------------------------
lv2_collision_detect:
  LD a, (RAM_LIGHT_GUN_READ)    ; Only detect when 
  CP $7F                        ; trigger is pulled.
  JP NZ, lv2_cd_skip_009        ;

  LD a, (RAM_VPOS + 8)          ;
  LD (RAM_COLL_Y1), a           ; Shot Y
  LD a, (RAM_HPOS + 16)         ;
  LD (RAM_COLL_X1), a           ; Shot X

; Foe 1
  LD a, (RAM_FOE_1_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_002        ;

  LD a, (RAM_VPOS + 12)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 24)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_002        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_1_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 24          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_002:
; Foe 2
  LD a, (RAM_FOE_2_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_003        ;

  LD a, (RAM_VPOS + 16)         ;
  LD (RAM_COLL_Y2), a           ; Foe 2 Y
  LD a, (RAM_HPOS + 32)         ;
  LD (RAM_COLL_X2), a           ; Foe 2 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_003        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_2_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 32          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_003:
; Foe 3
  LD a, (RAM_FOE_3_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_004        ;

  LD a, (RAM_VPOS + 20)         ;
  LD (RAM_COLL_Y2), a           ; Foe 3 Y
  LD a, (RAM_HPOS + 40)         ;
  LD (RAM_COLL_X2), a           ; Foe 3 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_004        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_3_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 40          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_004:
; Foe 4
  LD a, (RAM_FOE_4_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_005        ;

  LD a, (RAM_VPOS + 24)         ;
  LD (RAM_COLL_Y2), a           ; Foe 4 Y
  LD a, (RAM_HPOS + 48)         ;
  LD (RAM_COLL_X2), a           ; Foe 4 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_005        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_4_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 48          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_005:
; Foe 5
  LD a, (RAM_FOE_5_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_006        ;

  LD a, (RAM_VPOS + 28)         ;
  LD (RAM_COLL_Y2), a           ; Foe 5 Y
  LD a, (RAM_HPOS + 56)         ;
  LD (RAM_COLL_X2), a           ; Foe 5 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_006        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_5_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 56          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_006:
; Foe 6
  LD a, (RAM_FOE_6_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_007        ;

  LD a, (RAM_VPOS + 32)         ;
  LD (RAM_COLL_Y2), a           ; Foe 6 Y
  LD a, (RAM_HPOS + 64)         ;
  LD (RAM_COLL_X2), a           ; Foe 6 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_007        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_6_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 64          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_007:
; Foe 7
  LD a, (RAM_FOE_7_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_008        ;

  LD a, (RAM_VPOS + 36)         ;
  LD (RAM_COLL_Y2), a           ; Foe 7 Y
  LD a, (RAM_HPOS + 72)         ;
  LD (RAM_COLL_X2), a           ; Foe 7 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_008        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_7_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 72          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_008:
; Foe 8
  LD a, (RAM_FOE_8_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, lv2_cd_skip_009        ;

  LD a, (RAM_VPOS + 40)         ;
  LD (RAM_COLL_Y2), a           ; Foe 8 Y
  LD a, (RAM_HPOS + 80)         ;
  LD (RAM_COLL_X2), a           ; Foe 8 X
  CALL xy_collision_detect      ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, lv2_cd_skip_009        ;
  LD a, 240                     ; If hit, set respawn counter
  LD (RAM_FOE_8_COUNT), a       ;
  CALL  ds_lv2_add_to_score     ;
  LD hl, RAM_HPOS + 80          ;
  CALL ds_lv2_set_sprite        ;

lv2_cd_skip_009:
  RET                           ; End subroutine 
;----------------------------------------


;----------------------------------------
; ds_lv2_set_sprite (17 lines)
;
; Set sprite to "1".
;----------------------------------------
; HL = target address
ds_lv2_set_sprite:
  INC hl                        ;
  LD a, $33                     ;; 33 34 3B 3C
  LD (hl), a                    ;
  INC hl                        ;
  INC hl                        ;
  LD a, $34                     ;; 33 34 3B 3C
  LD (hl), a                    ;
  INC hl                        ;
  INC hl                        ;
  LD a, $3B                     ;; 33 34 3B 3C
  LD (hl), a                    ;
  INC hl                        ;
  INC hl                        ;
  LD a, $3C                     ;; 33 34 3B 3C
  LD (hl), a                    ;

  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; ds_lv2_add_to_score (16  lines)
;
; Increment the score.
;----------------------------------------
ds_lv2_add_to_score:
  LD b, 7                       ;
  LD hl, RAM_SCORE_7            ; LSD
  INC (hl)                      ;

  LD a, (hl)                    ;
  CP 10                         ; 10 = 1, 0
  JP NZ, ds_lv2_add_to_score_end ;

ds_lv2_add_to_score_loop:
  LD a, 0                       ; 
  LD (hl), a                    ;

  DEC hl                        ; Next digit
  INC (hl)                      ;

  LD a, (hl)                    ;
  CP 10                         ; 10 = 1, 0
  JP NZ, ds_lv2_add_to_score_end ;

  DEC b                         ;
  JP NZ, ds_lv2_add_to_score_loop ;
 
ds_lv2_add_to_score_end:
  RET                           ; End subroutine 
;----------------------------------------


;----------------------------------------
; pig_got_hit_lv2 (149 lines) 
;
; Did pig get hit?
;    Decrement health
;    Set pig invincibility counter
;    Decrement counter
;    If counter is 0, can get hit again
;----------------------------------------
pig_got_hit_lv2:
  LD a, (RAM_VPOS + 1)          ;
  LD (RAM_COLL_Y1), a           ; Pig Y
  LD a, (RAM_HPOS + 2)          ;
  LD (RAM_COLL_X1), a           ; Pig X

;-----------------------------------
; Foe 1
  LD a, (RAM_FOE_1_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_002        ;

  LD a, (RAM_VPOS + 12)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 24)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_002        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 12 ; Y respawn
  LD de, RAM_VPOS + 12          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 24 ; X respawn
  LD de, RAM_HPOS + 24          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_002:

;-----------------------------------
; Foe 2
  LD a, (RAM_FOE_2_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_003        ;

  LD a, (RAM_VPOS + 16)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 32)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_003        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 16 ; Y respawn
  LD de, RAM_VPOS + 16          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 32 ; X respawn
  LD de, RAM_HPOS + 32          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_003:

;-----------------------------------
; Foe 3
  LD a, (RAM_FOE_3_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_004        ;

  LD a, (RAM_VPOS + 20)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 40)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_004        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 20 ; Y respawn
  LD de, RAM_VPOS + 20          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 40 ; X respawn
  LD de, RAM_HPOS + 40          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_004:

;-----------------------------------
; Foe 4
  LD a, (RAM_FOE_4_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_005        ;

  LD a, (RAM_VPOS + 24)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 48)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_005        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 24 ; Y respawn
  LD de, RAM_VPOS + 24          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 48 ; X respawn
  LD de, RAM_HPOS + 48          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_005:

;-----------------------------------
; Foe 5
  LD a, (RAM_FOE_5_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_006        ;

  LD a, (RAM_VPOS + 28)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 56)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_006        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 28 ; Y respawn
  LD de, RAM_VPOS + 28          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 56 ; X respawn
  LD de, RAM_HPOS + 56          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_006:

;-----------------------------------
; Foe 6
  LD a, (RAM_FOE_6_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_007        ;

  LD a, (RAM_VPOS + 32)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 64)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_007        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 32 ; Y respawn
  LD de, RAM_VPOS + 32          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 64 ; X respawn
  LD de, RAM_HPOS + 64          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_007:

;-----------------------------------
; Foe 7
  LD a, (RAM_FOE_7_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_008        ;

  LD a, (RAM_VPOS + 36)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 72)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_008        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 36 ; Y respawn
  LD de, RAM_VPOS + 36          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 72 ; X respawn
  LD de, RAM_HPOS + 72          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_008:

;-----------------------------------
; Foe 8
  LD a, (RAM_FOE_8_COUNT)       ; Hit occured in this frame 
  CP 0                          ;
  JP NZ, pghlv2_skip_009        ;

  LD a, (RAM_VPOS + 40)         ;
  LD (RAM_COLL_Y2), a           ; Foe 1 Y
  LD a, (RAM_HPOS + 80)         ;
  LD (RAM_COLL_X2), a           ; Foe 1 X
  CALL xy_collision_detect16    ;

  LD a, (RAM_COLL_RESULT)       ; Hit? 
  CP $7F                        ;
  JP NZ, pghlv2_skip_009        ;

  CALL decrement_health_lv2     ;
  LD hl, vpos_sprites_level_2 + 40 ; Y respawn
  LD de, RAM_VPOS + 40          ;
  CALL copy_4_vpos_lv2          ;
  LD hl, hpos_sprites_level_2 + 80 ; X respawn
  LD de, RAM_HPOS + 80          ;
  CALL copy_4_hpos_lv2          ;
pghlv2_skip_009:

  RET                           ; End subroutine 
;----------------------------------------

;----------------------------------------
; decrement_health_lv2 (15 lines)
;
; Health goes down by 1.
;----------------------------------------
decrement_health_lv2:
  LD a, (RAM_PIG_BLINK)      ;
  CP $00                     ;
  JP NZ, dhlv2_skip_000      ;

  CALL copy_song_to_RAM004   ; Ouch

  LD a, (RAM_HEALTH_1)       ; Health 1
  DEC a                      ;
  CP $00                     ; At 0 health?
  JP Z, dhlv2_game_over      ;
  LD (RAM_HEALTH_1), a       ;
  LD a, 60                   ; 2 second pig blink
  LD (RAM_PIG_BLINK), a      ;
  JP  dhlv2_skip_000         ;

dhlv2_game_over
  LD a, STATE_GAME_OVER      ;
  LD (RAM_GAME_STATE), a     ;
dhlv2_skip_000:
  RET                        ; End subroutine 
;----------------------------------------

background_002:
.include "ppolis_level2_BG.inc"

tiles_002:
.include "ppolis_level2_T.inc"