;****************************
;*** ORGY LEVEL 11 UNLOCK ***
;****************************
;HL = entry address
.hi_score_orgy_check
	ld BC,6
	add HL,BC
	ld DE,hi_score_orgy_str
;break	
	ld B,4
.hi_score_orgy_check_loop
	ld A,(DE)
	cp (HL)
	ret nz
	inc HL
	inc DE
	djnz hi_score_orgy_check_loop
	
	ld A,sfx_1up
	call SND_SfxPlay
	
	ld E,50
	call DSK_wait
;.hi_score_orgy_wait_loop
;	push BC
;	call GFX_WaitVbl
;	call GFX_WaitVblEnd
;	pop BC
;	djnz hi_score_orgy_wait_loop
;set cybernoid level bit	
	ld HL,level_list+1
	set 7,(HL)
	
	
	ret
	
	
.hi_score_orgy_str
	db "ORGY"

;*******************
;*** ENTER SCORE ***
;*******************
; in B = rank , HL = entry address
.hi_score_enter

	push BC
	push HL

	;init cursor pos and current char
	xor A
	ld (hi_score_cursor_pos),A
	ld HL,hi_score_allowed
	ld (hi_score_str_ptr),HL
	
	call hi_score_draw_table
	
	pop HL
	pop BC

	ld DE,6
	add HL,DE	;skip score itself
	ld (hi_score_current_slot),HL	;save it
;compute adress on screen using rank
	ld A,10
	sub B
	
;compute screen address = HL+(128*line)
	ld L,A
	ld H,0
	add HL,HL	;2
	add HL,HL	;4
	add HL,HL	;8
	add HL,HL	;16
	add HL,HL	;32
	add HL,HL	;64
	add HL,HL	;128
	
	ld DE,#C000+192+5+64+40
	add HL,DE
	ld (hi_score_cursor_address),HL
	
	;ld HL,#C000

.hi_score_enter_main_loop
	
	call GFX_WaitVbl
	call GFX_WaitVblEnd
	
	di
	call KEY_Update
	ei
	call KEY_UpdateState

;*** UP ***	
	ld A,(keys)
	cp 1
	call z,hi_score_enter_main_loop_up_pressed

;** DOWN **
	ld A,(keys+1)
	cp 1
	call z,hi_score_enter_main_loop_down_pressed

;** FIRE **
	ld A,(keys+4)
	cp 1
	call z,hi_score_enter_main_loop_fire_pressed

	call hi_score_enter_main_loop_draw_cursor
	
	jr hi_score_enter_main_loop


	ret
	
;draw cursor
.hi_score_enter_main_loop_draw_cursor
	ld DE,(hi_score_cursor_address)
	
	ld HL,(hi_score_str_ptr)
	ld A,(HL)
	or 128	
	ld HL,hi_score_current_char
	ld (HL),A
	
	
	call FONT_DrawString
	
	ret

	
;*** FIRE ***
.hi_score_enter_main_loop_fire_pressed
	ld HL,(hi_score_str_ptr)
	ld A,(HL)
	cp 28
	jr z,hi_score_enter_main_loop_fire_pressed_backspace
	cp 30
	jr z,hi_score_enter_main_loop_fire_pressed_end
;add normal char
	ld A,(hi_score_cursor_pos)
	inc A
	cp 8
	ret nc
	
.hi_score_enter_main_loop_notlastchar
	ld (hi_score_cursor_pos),A
	
	ld HL,(hi_score_str_ptr)
	ld A,(HL)
	ld HL,(hi_score_current_slot)
	ld (HL),A
	inc HL
	ld (hi_score_current_slot),HL
;move cursor
	ld HL,hi_score_cursor_address
	inc (HL)
	inc (HL)
	
	ld A,(hi_score_cursor_pos)
	cp 7
	ret nz
	ld HL,hi_score_allowed_end
	ld (hi_score_str_ptr),HL
	
	ret

;end (validate)	
.hi_score_enter_main_loop_fire_pressed_end
;erase char on screen
	ld HL,hi_score_space_pos
	ld (hi_score_str_ptr),HL
	call hi_score_enter_main_loop_draw_cursor
	pop de
	ret

.hi_score_enter_main_loop_fire_pressed_backspace
	ld A,(hi_score_cursor_pos)
	or A
	ret z
	dec A
	ld (hi_score_cursor_pos),A
	
;erase char on string
	xor A
	ld HL,(hi_score_current_slot)
	dec HL
	ld (HL),A
	ld (hi_score_current_slot),HL
	
;erase char on screen
	ld HL,hi_score_space_pos
	ld (hi_score_str_ptr),HL
	call hi_score_enter_main_loop_draw_cursor

;move cursor	
	ld HL,hi_score_cursor_address
	dec (HL)
	dec (HL)
;restore cursor to backspace
	ld HL,hi_score_allowed_end-1	
	ld (hi_score_str_ptr),HL
	
	ret
	
	
	
;*** UP ***
.hi_score_enter_main_loop_up_pressed
	ld HL,(hi_score_str_ptr)
	inc HL
	ld A,(HL)
	cp #FF
	jr nz,hi_score_enter_main_loop_up_validate
	ld HL,hi_score_allowed	
.hi_score_enter_main_loop_up_validate
	ld (hi_score_str_ptr),HL
	ret
	
;*** DOWN ***
.hi_score_enter_main_loop_down_pressed
	ld HL,(hi_score_str_ptr)
	dec HL
	ld A,(HL)
	cp #FF
	jr nz,hi_score_enter_main_loop_down_validate
	ld HL,hi_score_allowed_end
.hi_score_enter_main_loop_down_validate
	ld (hi_score_str_ptr),HL
	ret

.hi_score_cursor_address
	dw 0
.hi_score_cursor_pos
	db 0
.hi_score_current_char
	db 0
.hi_score_current_slot
	dw 0
.hi_score_str_ptr
	dw hi_score_allowed
	
	db #FF
.hi_score_allowed

	db "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
	db "0123456789 " 
	db "!?.=<"
.hi_score_allowed_end
	db ">"
	db #FF
.hi_score_space_pos
	db " "


;********************
;*** INSERT SCORE ***
;********************
; in B = rank , HL = entry address , DE = score address
.hi_score_insert
	push HL
	push DE

	ld A,B
	cp 1
	jr z,hi_score_insert_skip_shift
	
	
	;push BC
	;push HL
	
	;ld BC,14
	;add HL,BC	;next !
	
	;pop DE
	;ex DE,HL
	

	;pop BC
	ld DE,hi_score_table+126 ;14*9
	ld HL,hi_score_table+112 ;14*8
	
.hi_score_shift_loop
	push BC
	push HL
	push DE

	ld BC,13
	ldir
	
	ld BC,-14
	pop HL
	add HL,BC
	ex DE,HL
	pop HL
	add HL,BC
	
	pop BC
	djnz hi_score_shift_loop
	
	
.hi_score_insert_skip_shift
	pop DE
	pop HL
	
;copies score first
	ex DE,HL
	ld BC,6
	ldir
;clean up name
	xor A
	ld B,7
.hi_score_insert_cleanup_name
	ld (DE),A
	inc DE
	djnz hi_score_insert_cleanup_name
	
	;ld A,128
	;ld (DE),A
	
	ret

;****************************
;*** CHECK FOR HIGH SCORE ***
;****************************
;in  HL = score address
;out HL = entry , A = B = rank if NZ , Z = no entry
.hi_score_check
	ld DE,hi_score_table
	ld B,10
	ex DE,HL
	
.hi_score_check_loop1
	push HL
	push DE
	push BC
	
	call hi_score_compare
	
	pop BC
	pop DE
	pop HL
	
	jr c,hi_score_no_validate
	jr z,hi_score_no_validate
	
	ld A,B
	or A
	ret

.hi_score_no_validate
	push BC
	ld BC,14
	add HL,BC
	pop BC
	djnz hi_score_check_loop1
	xor A	;djnz does not update Z flag !
	ret

;************************
;*** COMPARE 2 SCORES ***
;************************
; HL = score 1 - DE = score 2
; C  if score1>score2
; NC if score1<=score2
; Z if score1=score2
.hi_score_compare
	ld B,6
.hi_score_compare_loop
	ld A,(DE)
	cp (HL)
	ret c
	ret nz
	inc HL
	inc DE	
	djnz hi_score_compare_loop	
	or A
	ret


;***************************
;*** HI SCORE TABLE CODE ***
;***************************
.hi_score_menu_enter
	call hi_score_draw_table

	ld HL,500
.hi_score_menu_enter_loop
	push HL
	di
	call KEY_Update
	ei
	call MMI
	pop HL
	ld A,(MMIVAR)
	or A
	ret nz
	call GFX_WaitVbl
	call GFX_WaitVblEnd
	dec HL
	ld A,L
	or H
	jr nz,hi_score_menu_enter_loop
	ret

;.hi_score_enter_music
	
;	ld A,12
;	call MUSIC_Play
	
;	call hi_score_draw_table
	
	;force keystate update
;	di
;	call KEY_Update
;	ei
	
;.hi_score_enter_music_waitFire
;	call MMI
;	bit 4,A
;	jr z,hi_score_enter_music_waitFire
	
;	call MUSIC_Stop
;	ret
	



.hi_score_draw_table

	call GFX_Cls
	
	ld DE,#C000+64+19
	ld HL,hi_score_title
	call FONT_DrawString

	xor A
	
	ld HL,#C000+192+5
	
.hi_score_Drawloop	
	push AF
	push HL

	push AF	
	ld E,10
	
.hi_score_DrawLoop_synchro_line
	call GFX_WaitVbl
	call GFX_WaitVblEnd
	dec E
	jr nz,hi_score_DrawLoop_synchro_line
	
	pop AF
	
	call hi_score_draw_line
	

	
	pop HL
	pop AF
	inc A
	cp 10
	jr nz,hi_score_Drawloop
	
	ret
	
;*** draw a line ***
; HL = base address , A = line
.hi_score_draw_line
	push HL
	push AF
	
	ld L,A
	ld H,0
;multiply by (6+8) to get effective address
	
	add HL,HL	;2	(6*8)=8+4+2
	push HL
	add HL,HL	;4
	push HL
	add HL,HL	;8
	pop BC
	add HL,BC	;8+4
	pop BC
	add HL,BC	;8+4+2
	
	;index
	ld DE,hi_score_table
	add HL,DE
	ld (hi_score_current),HL	;save it !
	
	;number
	pop AF
	push AF
	add A
	ld L,A
	ld H,0
	ld DE,hi_noList
	add HL,DE	
	ld (hi_score_number),HL
	
	;screen address
	
	pop AF	;compute screen address = HL+(128*line)
	ld L,A
	ld H,0
	add HL,HL	;2
	add HL,HL	;4
	add HL,HL	;8
	add HL,HL	;16
	add HL,HL	;32
	add HL,HL	;64
	add HL,HL	;128
	pop DE
	add HL,DE	;+HL
	ld (hi_score_current_addr),HL	;save address
	
	;compute score
	ld HL,(hi_score_current)
	ld DE,hi_score_str
	ld B,6
	
.hi_score_str_loop
	ld A,(HL)
	or 16
	ld (DE),A
	inc HL
	inc DE
	djnz hi_score_str_loop
	
;write bit 7 terminator	
	dec DE
	or 128
	ld (DE),A
	
;score
	ld HL,(hi_score_current_addr)
	ld BC,64+20
	add HL,BC
	ex DE,HL	
	ld HL,hi_score_str
	call FONT_DrawString
	
;NO
	ld DE,(hi_score_current_addr)
	ld HL,hi_score_NoLine1
	call FONT_DrawString
	ld HL,(hi_score_current_addr)
	ld BC,64
	add HL,BC
	ex DE,HL	
	ld HL,hi_score_NoLine2
	call FONT_DrawString
;number itself
	ld HL,(hi_score_current_addr)
	ld BC,64+8
	add HL,BC
	ex DE,HL	
hi_score_number	EQU $+1
	ld HL,0
	call FONT_DrawString
;name
	ld HL,(hi_score_current_addr)
	ld BC,64+40
	add HL,BC
	ex DE,HL
	
	ld HL,(hi_score_current)
	ld BC,6
	add HL,BC
	call FONT_DrawString
	
	
	ret

.hi_score_title
	str "R A N K I N G"
	
.hi_score_str
	ds 6,0
	
.hi_score_current
	dw 0
.hi_score_current_addr
	dw 0
	
.hi_score_NoLine1
;No  ( $ % & ' ( ) )
str "$&("
;str "( ) $ % & '"
.hi_score_NoLine2
str "%')."

.hi_noList
	str " 1"
	str " 2"
	str " 3"
	str " 4"
	str " 5"
	str " 6"
	str " 7"
	str " 8"
	str " 9"
	str "10"
	
.Current_hi_score
	db 0,0,0,0,0,0

;score table - score (6) , name (7) , end mark (1)
;.hi_score_table
;	db 1,7,4,5,0,0:db "ABIKO..",128
;	db 1,6,8,6,0,0:db "SUMITA ",128
;	db 1,5,9,7,0,0:db "AKIO.O ",128
;	db 1,1,7,9,0,0:db "SHINJI.",128
;	db 1,0,0,5,0,0:db "MISAKO!",128
;	db 0,9,8,9,0,0:db "MASATO ",128
;	db 0,9,2,0,0,0:db "HAMA...",128
;	db 0,8,0,0,0,0:db "KENT.K ",128
;	db 0,7,6,0,0,0:db "KIJEE  ",128
;	db 0,7,5,0,0,0:db "IREM . ",128