;draw lives
.LIVES_Show

	ld A,(no_hud)
	or A
	ret nz


	ld A,(Current_Lives)	
	ld C,A
	ld B,7
	ld DE,LIFE_POS+12
	
.LIVES_ShowLoop
	ld HL,font+992
	ld A,C
	cp B
	jr z,LIVES_ShowDraw
	jr nc,LIVES_ShowDraw
	ld HL,font
.LIVES_ShowDraw
	push BC
	push DE
	call GAUGE_DrawChar
	pop DE
	dec E
	dec E
	pop BC
	djnz LIVES_ShowLoop 
	ret

;add points to score
;A = score to add from SCOREBYTES
.SCORE_Add

	;ex AF,AF'
	push AF
 	ld A,2
	ld (Supdate),A
	pop AF
	;ex AF,AF'
	
	ADD A
 	LD D,0
	LD E,A
	LD HL,SCOREBYTES
	ADD HL,DE
 	LD E,(HL)	;get offset
 	INC HL
	LD B,(HL)	;get count to add
 	ld D,0
	ld HL,Current_Score
	add HL,DE
	
	push DE
	push BC
	
	call SCORE_AddLoop	;regular score
	
	pop BC
	pop DE
	
	ld HL,LVL_Score	;do same for level score
	add HL,DE

	
.SCORE_AddLoop
	ld A,(HL)
	add B
	ld (HL),A
	sub 10
	ret c
	ld (HL),A
	dec HL
	ld B,1
	ld A,6
	cp E
	jr nz,SCORE_AddLoop	
	ret
;.ATS
	;ret ;debug before please
;E = DIGIT  / D = NUMBER TO ADD
	;LD A,E
	;or A
 	;ret z
	
	;ld A,6
	;sub E
	;ld E,A
	
	;ld B,D
	;ld D,0
 	;ld HL,Current_Score
	;jr SCORE_AddLoop

.SCOREBYTES	
	DEFB 3,1	;1	100
 	DEFB 3,2	;2	
 	DEFB 3,3	;3
 	DEFB 3,4	;4
 	DEFB 3,5	;5
 	DEFB 3,6	;6
 	DEFB 3,7	;7
 	DEFB 3,8	;8
 	DEFB 3,9	;9
 	DEFB 2,1	;10	1000
	DEFB 2,2	;11	2000
	DEFB 2,5	;12	5000
	DEFB 2,8	;13	8000
	DEFB 1,1	;14 10K
	DEFB 0,0	;15
	DEFB 0,0	;16
	
;draw current score if needed
.SCORE_Update
	ld A,(Supdate)
	or A
	ret z
	
	;cp 1
	;jr nz,SCORE_Update2
	xor A
	ld (Supdate),A
	;ld BC,color_table_options_normal
	;jr SCORE_Draw

;.SCORE_Update2
	;ld A,1
	;ld (Supdate),A
	
;check if we won extra life
	call XLIFE_Check
	
;check if this is a new hi score	
	ld HL,Current_Score
	ld DE,Current_hi_score
	call hi_score_compare
	
	jr nc,SCORE_UpdateSkipNewHi
;copy hi score	
	ld HL,Current_Score
	ld DE,Current_hi_score
	ld BC,6
	ldir
;update hi score string	
	ld DE,Current_hi_score
	ld HL,HISCORE_String
	call SCORE_UpdateString
;and draw it
	call SCORE_DrawHiScore	
	
.SCORE_UpdateSkipNewHi	

	ld A,(no_hud)
	or A
	ret nz

	ld DE,Current_Score
	ld HL,SCORE_String
	call SCORE_UpdateString
	

	ld BC,color_table_options_normal
	ld DE,SCORE_POS
	ld HL,SCORE_String
	call FONT_DrawStringEX
	ret
	
;update a score string
;DE = score - HL = score string
.SCORE_UpdateString
	ld B,6		
.SCORE_UpdateLoop
	ld A,(DE)
	add 16
	ld (HL),A
	inc HL
	inc DE
	djnz SCORE_UpdateLoop	
	dec HL
	set 7,(HL)	
	ret	

;draw hi score	
.SCORE_DrawHiScore

	ld A,(no_hud)
	or A
	ret nz

	ld DE,HI_POS+6
	ld HL,InitHi+3
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	ret
	
.SCORE_String
	str "000000"

.InitHi
	str "HI-"	
.HISCORE_String
	str "000000"

;init gauge on run
;.GAUGE_Init

;interlace tiles
;	ld B,48
;	ld IX,GAUGE_Left
;	ld DE,4
;	
;.GAUGE_Init_Loop
;	ld A,(IX+2)
;	ld C,(IX+3)
;	ld (IX+2),C
;	ld (IX+3),A
;	add IX,DE
;	djnz GAUGE_Init_Loop
;	ret
	
;draw empty 64 units gauge
;HL = address
.GAUGE_Draw

	ld A,(no_hud)
	or A
	ret nz
	
	ex DE,HL
	
	ld HL,GAUGE_Left
	call GAUGE_DrawChar
	inc E
	inc E
	
	ld B,8

.GAUGE_Draw_loop

	ld HL,GAUGE_0
	call GAUGE_DrawChar
	
	inc E
	inc E
	djnz GAUGE_Draw_loop
	
	ld HL,GAUGE_Right
	call GAUGE_DrawChar

	ret
	
;display 64 units gauge
;HL = address
;A = count
.GAUGE_Display

	ld E,A
	
	ld A,(no_hud)
	or A
	ret nz
	
	ld A,E

;break
	ex DE,HL
	inc E	;skip first char
	inc E
	
	ld B,8
	ld C,A


	ld A,(FRAME)
	and 1
	jr z,GAUGE_Display_loop
	
	ld A,C
	cp 64
	jr nc,GAUGE_Display_Full
	
	
	
.GAUGE_Display_loop

	ld A,C
	cp 8 ;if  >8 draw full char
	jr nc,GAUGE_DisplayFullChar

	push DE
	
	add A
	ld E,A
	ld D,0
	ld HL,GAUGE_Table
	add HL,DE
	ld A,(HL)
	inc HL
	ld H,(HL)
	ld L,A
	xor A
	
	pop DE
	
.GAUGE_Display_Draw
	ld C,A
	call GAUGE_DrawChar
	inc E
	inc E
	djnz GAUGE_Display_loop 
	
	ret
	
.GAUGE_DisplayFullChar
	sub 8
	ld C,A
	ld HL,GAUGE_8
	jr GAUGE_Display_Draw
	
.GAUGE_Display_Full
	ld HL,GAUGE_FULL
	call GAUGE_DrawChar
	inc E
	inc E
	djnz GAUGE_Display_Full
	ret
	
	
.GAUGE_DrawChar
	draw_char
	ret
	
.GAUGE_Table
	dw GAUGE_0,GAUGE_1,GAUGE_2,GAUGE_3,GAUGE_4,GAUGE_5,GAUGE_6,GAUGE_7,GAUGE_8
	
;align 16

;.GAUGE_Left
;	incbin "gfx/beam_left.pm1"
;.GAUGE_Right
;	incbin "gfx/beam_right.pm1"
;.GAUGE_0
;	incbin "gfx/beam0.pm1"	
;.GAUGE_1
;	incbin "gfx/beam1.pm1"		
;.GAUGE_2
;	incbin "gfx/beam2.pm1"
;.GAUGE_3
;	incbin "gfx/beam3.pm1"
;.GAUGE_4
;	incbin "gfx/beam4.pm1"
;.GAUGE_5
;	incbin "gfx/beam5.pm1"
;.GAUGE_6
;	incbin "gfx/beam6.pm1"
;.GAUGE_7
;	incbin "gfx/beam7.pm1"
;.GAUGE_8
;	incbin "gfx/beam8.pm1"
;.GAUGE_FULL
;	incbin "gfx/beam_full.pm1"