; level_x_increment_score - increment the player score by 1
; level_x_new_high_score - is the current score greater than high score? copy if so
; level_x_hit_a_bonus - did the player get a bonus item?

;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------
; Scoring
;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------

;------------------------------------
; level_x_increment_score
;
level_x_increment_score:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;

   LD hl, RAM_SCORE_0          ; Set RAM address
   LD b, 4                     ;

lxis_loop_0:   
   INC (hl)                    ; Add a point
   LD a, (hl)                  ;
   CP 10                       ;
   JP NZ, lxis_end             ;

   LD a, 0                     ; Carry to 10s
   LD (hl), a                  ;
   DEC hl                      ;
   
   DEC b                       ; Loop for all 5 digits
   JP NZ, lxis_loop_0          ;
   
lxis_end:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine 
   
;------------------------------------
; level_x_new_high_score
; 
level_x_new_high_score:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD de, RAM_SCORE_4          ; Compare score
   LD hl, RAM_TOP_SCORE_4      ; to top score
   LD c, 5                     ;

lxnhs_loop_0:   
   LD a, (hl)                  ; digit 4
   LD b, a                     ;
   LD a, (de)                  ;
   CP b                        ; a - b = ?
   
   JP M, lxnhs_end             ;
   CP b                        ; a - b = ?
   JP NZ, lxnhs_copy           ;

   INC hl                      ;
   INC de                      ;
   DEC c                       ;
   JP NZ, lxnhs_loop_0         ;    
   
   
lxnhs_copy:
   LD de, RAM_SCORE_4          ; Prepare to copy
   LD hl, RAM_TOP_SCORE_4      ; 
   LD b, 5                     ;

lxnhs_loop_1:   
   LD a, (de)                  ; Copy current score 
   LD (hl), a                  ; to high score
   INC de                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lxnhs_loop_1         ;  
   
lxnhs_end:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine 
 
;------------------------------------ 
; level_x_hit_a_bonus
;
level_x_hit_a_bonus:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD a, (RAM_VPOS + 52)       ; Player y
   LD (RAM_COLL_Y1), a         ;
   LD a, (RAM_VPOS + 56)       ; Egg y (left half)
   LD (RAM_COLL_Y2), a         ;   
   LD a, (RAM_HPOS + 104)      ; Player x
   LD (RAM_COLL_X1), a         ;
   LD a, (RAM_HPOS + 112)      ; Egg x
   LD (RAM_COLL_X2), a         ;   
   CALL xy_collision_detect16  ;
   
   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP Z, lxhab_hit             ;

   LD a, (RAM_VPOS + 57)       ; Egg y (right half)
   LD (RAM_COLL_Y2), a         ;   
   LD a, (RAM_HPOS + 114)      ; Egg x
   LD (RAM_COLL_X2), a         ;   
   CALL xy_collision_detect16  ;
   
   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP NZ, lxhab_miss           ;
   
lxhab_hit:
   CALL level_x_increment_score ; 5 points per egg
   CALL level_x_increment_score ;
   CALL level_x_increment_score ;
   CALL level_x_increment_score ;
   CALL level_x_increment_score ;
   CALL sound_pow_sfx          ;
   
   LD de, level_x_sp_vpos + 54 ; Reload egg
   LD hl, RAM_VPOS + 54        ;
   LD b, 4                     ;

lxmbd_respawn_loop2:   
   LD a, (de)                  ;
   LD (hl), a                  ;
   INC hl                      ;
   INC de                      ;
   DEC b                       ;   
   JP NZ, lxmbd_respawn_loop2  ;

   LD de, level_x_sp_hpos + 108 ;
   LD hl, RAM_HPOS + 108       ;
   LD b, 8                     ;

lxmbd_respawn_loop3:   
   LD a, (de)                  ;
   LD (hl), a                  ;
   INC hl                      ;
   INC de                      ;
   DEC b                       ;   
   JP NZ, lxmbd_respawn_loop3  ;
   
lxhab_miss:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine 