;; MIT License
;; 
;; Copyright (c) 2021 capAI / Aitana Devis  Ávila (@AitanaDevis) / Carlos Peñarrubia Morcillo  (@carlospm12) / Pablo Ruiz Ponce (@pabloruizp)
;; 
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; 
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; 
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

.globl _puntuacion
.globl _print_score
.globl cpct_drawCharM0_asm
.globl cpct_setDrawCharM0_asm



;;///////////////////////////////////////////////////////////////////////
;; ADDS 50 POINTS TO THE TOTAL SCORE WHEN PASSING TO NEXT LEVEL
;;
;; PARAMETROS: 
;;
;; REGISTROS MODIFICADOS: 
;;
;; RETURN: 
;;///////////////////////////////////////////////////////////////////////
_add_50_score::
   ld hl, (_puntuacion)

   ld a, l
   add #0x50
   daa
   ld l, a
   ld a, h
   adc #0x0
   daa
   ld h, a

   ld (_puntuacion), hl

   call _print_score

   ret

;;///////////////////////////////////////////////////////////////////////
;; REMOVES 5 POINTS TO THE TOTAL SCORE WHEN THE PLAYER DIES
;;
;; PARAMETROS: 
;;
;; REGISTROS MODIFICADOS: 
;;
;; RETURN: 
;;///////////////////////////////////////////////////////////////////////
_sub_25_score::
   ld hl, (_puntuacion)

   ld a, l
   sub #0x25
   daa
   ld l, a

   ld a, h
   sbc a, #0x0
   daa
   ld h, a

   jr nc, continuar_sub_05
   ld hl, #0

   continuar_sub_05:
   ld (_puntuacion), hl

   call _print_score

   ret

;;///////////////////////////////////////////////////////////////////////
;; ADDS 20 POINTS TO THE TOTAL SCORE BY TAKING A BONUS
;;
;; PARAMETROS: 
;;
;; REGISTROS MODIFICADOS: 
;;
;; RETURN: 
;;///////////////////////////////////////////////////////////////////////
_add_20_score::
   ld hl, (_puntuacion)

   ld a, l
   add #0x20
   daa
   ld l, a
   ld a, h
   adc #0x0
   daa
   ld h, a

   ld (_puntuacion), hl

   call _print_score

   ret

;;///////////////////////////////////////////////////////////////////////
;; ADDS 10 POINTS TO THE TOTAL SCORE BY TAKING A BONUS
;;
;; PARAMETROS: 
;;
;; REGISTROS MODIFICADOS: 
;;
;; RETURN: 
;;///////////////////////////////////////////////////////////////////////
_add_10_score::
   ld hl, (_puntuacion)

   ld a, l
   add #0x10
   daa
   ld l, a
   ld a, h
   adc #0x0
   daa
   ld h, a

   ld (_puntuacion), hl

   call _print_score

   ret

;;///////////////////////////////////////////////////////////////////////
;; PRINTS THE SCORE DURING THE GAME
;;
;; PARAMETROS: 
;;
;; REGISTROS MODIFICADOS: 
;;
;; RETURN: 
;;///////////////////////////////////////////////////////////////////////
_print_score:
   ld l, #10
   ld h, #0
   call cpct_setDrawCharM0_asm


   ld hl, (_puntuacion)
   ;Printeo el primero caracter empezando por la derecha
   ld a, l
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC84C
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x884C
   call cpct_drawCharM0_asm
   pop hl

   ;Printeo el segundo caracter empezando por la derecha
   ld a, l
   rra
   rra
   rra
   rra
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC848
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x8848
   call cpct_drawCharM0_asm
   pop hl
   

   ld a, h
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC844
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x8844
   call cpct_drawCharM0_asm
   pop hl

   ld a, h
   rra
   rra
   rra
   rra
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC840
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x8840
   call cpct_drawCharM0_asm
   pop hl

   ret


_print_final_score:
   ;ld l, #10
   ;ld h, #0
   ;call cpct_setDrawCharM0_asm


   ld hl, (_puntuacion)
   ;Printeo el primero caracter empezando por la derecha
   ld a, l
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC536
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x8536
   call cpct_drawCharM0_asm
   pop hl

   ;Printeo el segundo caracter empezando por la derecha
   ld a, l
   rra
   rra
   rra
   rra
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC532
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x8532
   call cpct_drawCharM0_asm
   pop hl
   

   ld a, h
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC52E
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x852E
   call cpct_drawCharM0_asm
   pop hl

   ld a, h
   rra
   rra
   rra
   rra
   and #0x0F
   add #48
   ld e, a
   push hl
   push de
   ld hl, #0xC52A
   call cpct_drawCharM0_asm
   pop de
   ld hl, #0x852A
   call cpct_drawCharM0_asm
   pop hl

   ret