;***********************
;*** Force Game Over ***
;***********************
.GAME_ForceOverResetScore
	call GAME_ScoreInitStart
.GAME_ForceOver
	xor A
	ld (Current_Lives),A
	inc A
	ld (Current_Tries),A
	
	ld A,1
	ld (END_COUNT),A
	ld HL,GAME_Restart
	ld (END_JUMP),HL
	ret

;**********************
;*** Show Game Over ***
;**********************
.GAME_IsOver


	;jr GAME_CheckContinue

	ld A,(Current_Lives)
	or A
	ret nz
	
	;palette fix for stopping on fading
	xor A
	ld HL,lvl_pal	
	call GFX_SetupLevelPalette
	
	ld A,10
	call MUSIC_Play
	
	ld B,255
	
.GAME_OverLoop
	push BC
	
	call GAME_OverShow
	call REND_Udpate

.GAME_Over_Sync1
	ld A,(int_counter)
	or A
	jr nz,GAME_Over_Sync1
.GAME_Over_Sync2
	ld A,(int_counter)
	or A
	jr z,GAME_Over_Sync2
	
	pop BC
	djnz GAME_OverLoop
	
	call GFX_Cls
	jr GAME_CheckContinue
	
.GAME_OverShow
	ld A,108
	ld C,10
	ld B,10
	call USPGADD
	call SPNS
	ret

;*********************
;*** CONTINUE CODE ***
;*********************
	
GAME_ContinueSpeed	Equ 56	
	
.GAME_CheckContinue

	;force keystate update
	di
	call KEY_Update
	ei

	SetIntTable int_gameover_table
	change_mode 1
	ld HL,continue_pal
	call GFX_SetColors
	
	;call hi_score_enter_music
	
	call GFX_Cls
	
;check for hi score

	ld HL,Current_Score
	call hi_score_check
	
	jr z,GAME_CheckContinueNoHiScore
	
	push BC
	push HL
	
	ld A,12
	call MUSIC_Play
	
	pop HL
	pop BC
	
	push BC
	push HL
	
	ld DE,Current_Score
	call hi_score_insert
	
	pop HL
	pop BC
	
	push HL
	
	call hi_score_enter
	
	pop HL
	call hi_score_orgy_check
	
	call GFX_Cls
	
	;force keystate update
	di
	call KEY_Update
	ei
	
.GAME_CheckContinueNoHiScore	
	
	ld A,(Current_Tries)
	cp 7
	jr z,GAME_CheckContinue_FreePlay
	
.GAME_CheckContinue_NotFreePlay
	
	dec A
	
	jp z,GAME_CheckContinue_EndGame

	ld (Current_Tries),A

	add 128+16
	ld (continue_credit_pos),A
	
.GAME_CheckContinue_FreePlay
	
	ld DE,#C040+18
	ld HL,continue_title
	;ld BC,color_table_options_normal
	;call FONT_DrawStringEX
	call FONT_DrawString

	ld A,(Current_Tries)
	cp 7
	jr z,GAME_CheckContinue_NoCreditShow
	
	ld DE,#C580+19
	ld HL,continue_credit
	call FONT_DrawString

.GAME_CheckContinue_NoCreditShow
	
	ld DE,#C480+16
	ld HL,continue_press_fire
	ld BC,color_table_options_normal
	call FONT_DrawStringEX	
	
	
	ld A,9
	call MUSIC_Play
	
	ld A,9
	ld (continue_counter),A
	ld A,GAME_ContinueSpeed
	ld (continue_vbl_counter),A
	
.GAME_CheckContinue_loop

	call GFX_WaitVbl
	
	;call MMI
	;bit 4,A
	call KEY_Update
	call KEY_UpdateState
	ld A,(keys+4)
	cp 1
	jr z,GAME_CheckContinueDone
	
continue_counter	EQU $+1
	ld A,0
	ld DE,#C180+25
	call GAME_ContinueDrawDigit
	
continue_vbl_counter	EQU $+1
	ld A,0
	dec A
	jr nz,continue_notNextDigit
	
	ld A,(continue_counter)
	dec A
	jp m,GAME_CheckContinue_EndGame	;no more count , return menu
	ld (continue_counter),A
	
	ld A,GAME_ContinueSpeed
.continue_notNextDigit
	ld (continue_vbl_counter),A
	
	
	call GFX_WaitVblEnd
	
	jr GAME_CheckContinue_loop 

.GAME_CheckContinueDone
	;restore lives and restart game
	ld SP,(Stack_Save)

	call GAME_ScoreInitStart
	
	call MUSIC_Stop
	
	jp GAME_Restart
	
.GAME_CheckContinue_EndGame
	;call hi_score_enter_music
	jp GAME_FullRestart
	
;A = digit - DE = start address 
.GAME_ContinueDrawDigit
	ld B,9
	
	ld IX,continue_digits
	push DE
	ld E,A
	add A	;*2
	add A	;4
	add A	;8
	add	E	;9
	ld E,A
	ld D,0
	add IX,DE
	pop DE	

.GAME_ContinueDrawDigit_loop
	push BC
	push DE

	ld C,(IX)
	inc IX
	
	ld  HL,continue_digits_line
	call continue_digits_make_line
	
	ld HL,continue_digits_line
	call FONT_DrawString
	
	pop HL
	ld DE,64
	add HL,DE
	ex DE,HL
	pop BC
	djnz GAME_ContinueDrawDigit_loop
	ret
	
.continue_digits_make_line
	ld 	B,8
.continue_digits_make_line_loop
	xor A
	rl 	C	;in carry
	jr	nc,continue_digits_make_line_null
	ld 	A,47
	
.continue_digits_make_line_null
	ld	(HL),A
	inc HL
	djnz continue_digits_make_line_loop
	dec HL
	set 7,(HL)
	ret
	
.int_gameover_table
dw int_done
dw int_game_over
dw int_done
dw int_done
dw int_done
dw int_done
dw int_done
dw int_done
	
.int_game_over
	push DE
	call KEY_Update
	pop DE
	jp int_SND_Player

.continue_title
	str "C O N T I N U E"
.continue_credit
	str "C R E D I T  0"
continue_credit_pos EQU $-1

.continue_press_fire
	str "PRESS FIRE BUTTON"

.continue_digits_line	
	ds 9,128
.continue_digits ;(from 0 to 9)
;9 lines per char
;*** 0 ***
	db %00111100	
repeat 7
	db %01000010
rend
	db %00111100
;*** 1 ***
	db %00001000
	db %00011000
repeat 6
	db %00001000
rend
	db %00011100
;*** 2 ***
	db %00111100
	db %01000010
	db %01000010
	db %00000010
	db %00011100
	db %00100000
	db %01000000
	db %01000000
	db %01111110
;*** 3 ***
	db %00111100
	db %01000010
	db %01000010
	db %00000010	
	db %00011100
	db %00000010
	db %01000010
	db %01000010
	db %00111100
;*** 4 ***
	db %00001100
	db %00010100
	db %00100100
	db %01000100
	db %01000100
	db %01000100
	db %01111110
	db %00000100	
	db %00000100
;*** 5 ***
	db %00111110
	db %01000000
	db %01000000
	db %01000000
	db %00111100
	db %00000010
	db %00000010
	db %01000010
	db %00111100
;*** 6 ***
	db %00111100
	db %01000010
	db %01000000
	db %01000000
	db %00111100
	db %01000010
	db %01000010
	db %01000010
	db %00111100
;*** 7 ***
	db %00111100
	db %00000010
	db %00000010
	db %00000010
	db %00000100
	db %00001000
	db %00010000
	db %00010000
	db %00010000
;*** 8 ***
	db %00111100
	db %01000010
	db %01000010
	db %01000010
	db %00111100
	db %01000010
	db %01000010
	db %01000010
	db %00111100
;*** 9 ***
	db %00111100
	db %01000010
	db %01000010
	db %01000010
	db %00111100
	db %00000010
	db %00000010
	db %00000010
	db %00111100

	
	
	
	