;****************************
;*** Initialize end level ***
;****************************
.END_LEVEL_Init
	ld A,1
	ld (no_hud),A

	;save vars , especially weapons !
	call GAME_SaveVars

	;set up end level music
	ld A,#FF
	ld (LVL_NextMusik),A
	xor A
	ld (LVL_FadeVolume),A

	;set end level
	ld A,1
	ld (LVL_End),A
	ld (ForcePlayerMove),A	;force player centering
	
	;fill score to 0
	ld HL,str_LVL_SCORE
	ld B,6
	ld A,16
	
.END_LEVEL_InitScore
	ld (HL),A
	inc HL
	djnz END_LEVEL_InitScore

	ld A,6
	ld (LVL_EndDigit),A
	
	xor A
	ld (LVL_DigitCount),A
	
	ld A,20
	ld (LVL_EndCount),A

	ret

;**********************
;*** SHOW END LEVEL ***
;**********************
.END_LEVEL
	ld A,(LVL_End)
	or A
	ret z
	
	;wait a bit !
	inc A
	ld (LVL_End),A
	cp 16
	ret c
	jr nz,END_LEVEL_MusicStarted
	
	call END_LVL_PlayMusic
	call END_LVL_SetFlag

.END_LEVEL_MusicStarted

	SetIntTable int_endlevel_table	
	
	ld DE,#C100+5 ;1
	ld HL,str_LVL_COMPLETED
	ld BC,color_table_options_normal
	call FONT_DrawString
	
	ld A,(LVL_End)
	cp 16
	;jr c,END_LEVEL_NoScoreUpdate
	ret c
	
	ld A,17
	ld (LVL_End),A
	
	ld A,(LVL_DigitCount)
	inc A
	cp 9
	jr nz,END_LEVEL_NotTen
	xor A
.END_LEVEL_NotTen	
	ld (LVL_DigitCount),A
	
	ld C,A
	
	ld A,(LVL_EndDigit)
	or A
	
	jr z,END_LEVEL_NoScoreUpdate
	
	ld B,A
	
	ld A,sfx_points
	call SND_SfxPlay
	
	ld A,16
	add C
	ld HL,str_LVL_SCORE
	
.END_LEVEL_FillScore
	ld (HL),A
	inc HL
	djnz END_LEVEL_FillScore
	
	ld A,(LVL_EndCount)
	dec A
	or A
	jr nz,END_LEVEL_ContinueCount
	
	ld A,(LVL_EndDigit)
	dec A
	ld (LVL_EndDigit),A
	
	or A
	call z,END_LEVEL_Final
	
	ld E,A
	ld D,0
	
;chek if score is NULL
	ld HL,LVL_Score
	ld B,6
	xor A
.END_LEVEL_CheckIfNullScore
	or (HL)
	inc HL
	djnz END_LEVEL_CheckIfNullScore
	
	jr nz,END_LEVEL_NotNullScore
	
	ld HL,str_LVL_DOTEATER
	add HL,DE
	ld A,(HL)
	jr END_LEVEL_NullScore

.END_LEVEL_NotNullScore
;here copy score digit	
	ld HL,LVL_Score ;Current_Score
	add HL,DE
	ld A,(HL)
	add 16
	
.END_LEVEL_NullScore	
	ld HL,str_LVL_SCORE
	add HL,DE
	ld (HL),A
	
	ld A,20
	
.END_LEVEL_ContinueCount
	ld (LVL_EndCount),A
	
.END_LEVEL_NoScoreUpdate

	ld DE,#C1C0+14
	ld HL,str_LVL_STAGE_SCORE
	ld BC,color_table_options_normal
	call FONT_DrawString
	
	ret
;finishes level !
.END_LEVEL_Final
END_LEVEL_Jump	EQU $+1
	ld A,0
	bit 7,A
	jr z,END_LEVEL_NotJump
	push AF
	xor A
	ld (END_LEVEL_Jump),A
	pop AF
	and 127
	jr END_LEVEL_DoJump
	
.END_LEVEL_NotJump	
	ld A,(current_lvl)
	inc A
.END_LEVEL_DoJump	
;no more cycling
	;cp 10	;we have 8 levels + end
	;jr c,END_LEVEL_Final_nocycle
	;ld A,1
.END_LEVEL_Final_nocycle	
	ld (current_lvl),A		
	
	ld A,20
	ld (END_COUNT),A
	ld HL,GAME_StartLevel
	ld (END_JUMP),HL	
	
	xor A	;A is supposed to be null
	ret
	
.str_LVL_COMPLETED
	db "S T A G E  "
.str_LVL_COMPLETED_CURRENT
	str "1  C L E A R E D"
.str_LVL_STAGE_SCORE
	db "STAGE SCORE "
.str_LVL_SCORE
	str "000000 "
.str_LVL_DOTEATER
	db "DOTEAT" 
.LVL_Score	;level score !
	ds 6,0

;******************************************	
;*** SOFT END LEVEL - JUST CLEAN SCREEN ***
;******************************************
.SOFT_END_Start

	ld A,(BIGEXPLO_State)	;all is exploding ?
	or A
	ret nz
	
	ld A,(LVL_End)			;is already end level ?
	or A
	ret nz

	ld A,(SHIP)	;be sure player is alive !
	or A
	ret z
	
	ld A,-1
	ld (SOFT_END_Pos),A

	
	call BIG_EXPLO_StartStopAll
	;make player immortal
	;ld A,#C9
	;ld (PLAYER_Die),A
	;no enemies
	;ld A,1
	;ld (SYNCOFF),A
	;start level clean
	ld (SOFT_END_State),A
	ret
	
.SOFT_END_Run
	ld A,(SOFT_END_State)
	or A
	ret z
;run 1/4	
	;ld A,(FRAME)
	;and 4
	;ret nz
	
	ld A,(SOFT_END_Pos)
	inc A
	ld (SOFT_END_Pos),A
	cp 10
	jr nc,SOFT_END_Stop
	
	;ld C,A
	;ld B,0
	;ld DE,#0114
	;call REND_EraseZone
	ld C,0
	ld B,A
	push BC
	ld DE,#2001
	push DE
	ld A,9
	sub B
	ld B,A
	call REND_EraseZone
	pop DE
	pop BC
	ld A,10
	add B
	ld B,A
	call REND_EraseZone
	
	ret
.SOFT_END_Stop
	xor A
	ld (SOFT_END_State),A
	call END_LEVEL_Init
	ret

;**************************************
;*** BIG FINAL EXPLOSION MANAGEMENT ***
;**************************************

.BIG_EXPLO_Start
	
	ld A,(SOFT_END_State)	;be sure to not use other end
	or A
	ret nz

	ld A,(SHIP)	;be sure player is alive !
	or A
	ret z

	ld HL,BIGEXPLO_State
	ld (HL),1
	inc HL
	ld (HL),-1	;x
	inc HL
	ld (HL),19	;y	start at bottom of the screen
.BIG_EXPLO_StartStopAll	
	;make player immortal
	ld A,#C9
	ld (PLAYER_Die),A
	;no enemies
	ld A,1
	ld (SYNCOFF),A
	;suppress jewels
	xor A
	ld (JEW1),A
	ld (JEW2),A

	call GETSYNC91	;stop scroll !
	ret
	
.BIG_EXPLO_Run
	;ld A,(FRAME)
	;and 1
	;ret nz

	ld A,(BIGEXPLO_State)
	or A
	ret z
	
;cycle 1-5 = random explosions
	cp 5
	jr z,BIG_EXPLO_Run_Phase2
;generate explosions only every 8 frames	
	ld A,(FRAME)
	and 7
	ret nz
	
	ld HL,BIGEXPLO_State
	inc (HL)
	
;loop all explosion slots and generate explosion
	ld HL,EXPLOBUFF	
	
	ld B,20	;all slots
	


.BIG_EXPLO_Run_Phase1_loop
	ld A,10	;5 tries per explosion
	ld (BIG_ExploRunRetryCount),A
	push BC

.BIG_ExploRunRetry
	ld A,(int_counter)
	add A
	add A
	;add A
	ld C,A
	
	ld A,R
	and 31
	add C
	cp 27
	jr c,BIG_ExploRun_Phase1_OnScreen
	sub 4
.BIG_ExploRun_Phase1_OnScreen
	ld C,A
	
	
	ld A,(int_counter)
	neg
	add A
	;add A
	ld B,A
	
	ld A,R
	add B
	and 15
	ld B,A
	
	;jr BIG_ExploRun_ValidPos
	
	call COLL_CheckPosition
	jr nc,BIG_ExploCheckRetry
	or A
	jr z,BIG_ExploRun_ValidPos

.BIG_ExploCheckRetry	
BIG_ExploRunRetryCount equ $+1
	ld A,5
	dec A
	ld (BIG_ExploRunRetryCount),A
	jr nz,BIG_ExploRunRetry
	
; no chance !
	inc L
	inc L
	inc L
	jr BIG_ExploRun_Cancel


.BIG_ExploRun_ValidPos	
	call BIG_ExploGenOne
.BIG_ExploRun_Cancel	
	pop BC
	djnz BIG_EXPLO_Run_Phase1_loop

	ret
	
;gen en explosion at DE if position not empty
.BIG_ExploGenOne
	push BC

;add explosion	
	ld (HL),72
	inc L
	ld (HL),C
	inc L
	ld (HL),B
	inc L
	push HL
	
	ld DE,#0404
	call REND_EraseZone
	
	ld A,sfx_explodeC
	call SND_SfxPlay
	
	pop HL
	pop BC
	ret

.BIG_EXPLO_Run_Phase2

	ld A,(FRAME)
	and 1
	ret nz
	
	ld A,7
	ld (BIG_ExploRunLoopCounter),A
	
;stop only on occupied positions
.BIG_ExploRunLoop
	ld BC,(BIGEXPLO_X)	
	call BIG_EXPLO_NextPos		
	ld (BIGEXPLO_X),BC	
	call COLL_CheckPosition ;check if empty , if not create exposion !

	jr nc,BIG_ExploRunLoop
	;or A
	;jr nz,BIG_ExploRunLoop
	
	;ld DE,#0404
	;ld A,R
	;and 7
	;or 1
	;ld D,A

	
	dec B
	dec B
	dec B

	
	;push BC
	;call REND_EraseZone
	
	;ld A,sfx_explodeC
	;call SND_SfxPlay
	push BC
	CALL ADDEXPLO0
	pop BC
	call BIG_ExploGenOne

	;ld (HL),72 ;type 4 , count 8
	;pop BC
	;inc L
	;ld (HL),C
	;inc L
	;ld (HL),B
	
BIG_ExploRunLoopCounter EQU $+1
	ld A,0
	dec A
	ret z
	
	ld (BIG_ExploRunLoopCounter),A
	
	jr BIG_ExploRunLoop
	
.BIG_EXPLO_NextPos
	;ld A,C
	;add 1
	;ld C,A
	inc C
	ld A,C
	cp 32
	ret c
	
	ld C,0
	ld A,B
	sub 1
	ld B,A
	ret nc
;this is the end !
	pop HL	;skip ret address and ret to main loop
	xor A
	ld (BIGEXPLO_State),A
	call END_LEVEL_Init
	ret

read "end_lvl_low.asm"	