;***************************************
;*** Show ready state and wait a bit ***
;***************************************
.GAME_WaitReady

	ld A,(LVL_Run)
	or A
	ret z

	ld A,#FF
	ld (level_fade),A	;not first run, no fade !

	ld B,50 ;100

.GAME_WaitReady_loop
	push BC
	call GAME_ShowReady
	call REND_Udpate

;wait B frames	
.GAME_WaitReady_Sync1
	ld A,(int_counter)
	or A
	jr nz,GAME_WaitReady_Sync1
.GAME_WaitReady_Sync2
	ld A,(int_counter)
	or A
	jr z,GAME_WaitReady_Sync2
	
	pop BC
	djnz GAME_WaitReady_loop

	;CLS
	call REND_Udpate
	
	ret
.GAME_ShowReady
	ld A,107
	ld C,13
	ld B,10
	call USPGADD
	call SPNS
	ret
	
;*************************
;*** Start level music ***
;*************************
.GAME_StartMusic

	;full volume !
	xor A
	;no music change !
	ld (LVL_NextMusik),A
	call MUSIC_SetVolume
	



	ld A,(LVL_Run)
	or A
	jr z,GAME_StartMusic_FirstRun
	
	ld BC,(lvl_skip)
	ld A,C
	or B
	jr z,GAME_StartMusic_FirstRun	;if lvl_skip is null , consider as first run

	ld A,(lvl_music)
	
	call MUSIC_Skip
	
	ret

.GAME_StartMusic_FirstRun
	inc A
	ld (LVL_Run),A
	
	ld A,(lvl_music)	
	call MUSIC_Play
	
	ret

;**********************
;*** Load Current level ***
;**********************
.GAME_InitLoadLevel

;reset level score
	ld HL,LVL_Score		;stage score
	ld B,6
	xor A
	;ld A,1
;init score
.GAME_InitLoadLevelResetScore
	ld (HL),A
	inc HL
	djnz GAME_InitLoadLevelResetScore

;setup files names
	ld A,(current_lvl)

IF PNL_Preview	
	cp 5
	jr c,GAME_InitLoadLevel_Ok
	
	;force game over
	xor A
	ld (Current_Lives),A
	inc A
	ld (Current_Tries),A
	
	ret
.GAME_InitLoadLevel_Ok
ENDIF	
	ld E,A
	ld D,0
	ld HL,lvl_digit
	add HL,DE
	ld A,(HL)
	
	ld (lvl_C4_digit),A
	ld (lvl_C5_digit),A
	ld (spr_C6_digit),A
	ld (lvl_C0_digit),A
	
	sub 32
	ld (str_LVL_COMPLETED_CURRENT),A
	
;do loading

	call FDCON
	
	ld BC,#7FC4
	out (C),C

;load level code first	
	IF output_files
	ld HL,lvl_C0
	ld DE,level_start
	call Quick_load
	ENDIF
	
	ld A,(option_menu_game_mode	)
	cp 3
	jr nz,GAME_InitLoadLevel_NotMaster
	
	;break
;setup patcher name
IF FDC_3P5
	ld A,(current_head)
ELSE
	ld A,(current_side)
ENDIF
	add 65				;MASTER.FAA for face A , MASTER.FAB for face B
	ld (master_file_termination),A
;load it	
	ld HL,master_file
	ld DE,#4000
	call Quick_load
;run it
	ld A,(current_lvl)
	call #4000

.GAME_InitLoadLevel_NotMaster	

;IF debug
;	ld BC,#7F10	;gris
;	ld A,#40
;	out (C),C
;	out (C),A
;ENDIF

;map
	
	ld HL,lvl_C4
	ld DE,#4000
	call Quick_load
	
	ld A,#C4
	ld (#4000),A
	
;IF debug
;	ld BC,#7F10 ;vert
;	ld A,#42
;	out (C),C
;	out (C),A
;ENDIF
	
	ld BC,#7FC5
	out (C),C
	ld HL,lvl_C5
	ld DE,#4000
	call Quick_load
	
	ld A,#C5
	ld (#4000),A

;IF debug	
;	ld BC,#7F10 ;rose
;	ld A,#45
;	out (C),C
;	out (C),A
;ENDIF

;sprites
	
	ld BC,#7FC6
	out (C),C
	ld HL,spr_C6
	ld DE,#4000
	call Quick_load
	
	ld A,#C6
	ld (#4000),A
	
	
	ld BC,#7FC0
	out (C),C
	
	ld A,#C0
	ld (#4000),A

;IF debug
;	ld BC,#7F10 ;jaune
;	ld A,#4A
;	out (C),C
;	out (C),A
;ENDIF

;scripts
	

	
	call FDCOFF

;IF debug	
;	ld BC,#7F10 ;noir
;	ld A,#54
;	out (C),C
;	out (C),A
;ENDIF

;setup level	
	LD HL,(SYNCHRO1)
 	LD (SYNCHRO),HL
	ld (save_SYNCHOLD),HL
	ld A,L
 	LD (DEF_SYNCHOLD1),A
	ld A,H
	LD (DEF_SYNCHOLD2),A
	
	LD A,(START1)
  	LD (START),A
 	LD (DEF_TILENO),A
	ld (save_TILENO),A
	
;setup colors
	;ld A,(lvl_pal)
	
	;call GFX_ConvertColor
	
	;ld (save_BG_COLOR),A
	
	ld HL,lvl_pal
	xor A	
	;call GFX_SetColors
	call GFX_SetupLevelPalette
	ld (save_BG_COLOR),A
	;save BG & FL colors
	ld HL,lvl_pal
	ld A,(HL)	
	ld A,(lvl_floating_color)
	ld E,A
	ld D,0
	add HL,DE
	ld A,(HL)
	call GFX_ConvertColor 
	ld (save_FL_COLOR),A
	
;setup as first run
	xor A
	ld (LVL_Run),A
	
	ld A,3
	ld (level_fade),A	;fade !

	ret
	
charset_off

.current_lvl
	db 1

.lvl_digit
	db "0123456789AB" ;CDEF"

.lvl_C4
	db "MAP"
.lvl_C4_digit
	db "1    C4 "
.lvl_C5
	db "MAP"
.lvl_C5_digit
	db "1    C5 "
.spr_C6
	db "GFX_L"
.spr_C6_digit
	db "1  SPR"
.lvl_C0
	db "LEVEL"
.lvl_C0_digit
	db "1  LVL"
	
charset_on
	
;**************************************************
;*** SETUP FULL WEAPONRY FROM RESTORE DATA ***
;**************************************************

.GAME_InitWeaponry
	LD A,(BALLPOW)
	ld E,A
	
	ld A,(BALL1)
	or A
	jr z,GAME_InitWeaponryBall2
	
	ld A,E
	call BONUS_Get
	jr GAME_InitWeaponryBallDone
	
.GAME_InitWeaponryBall2	
	ld A,(BALL2)
	or A
	jr z,GAME_InitWeaponryBall3
	
	ld A,E
	call BONUS_Get
	ld A,E
	call BONUS_Get
	jr GAME_InitWeaponryBallDone
	
.GAME_InitWeaponryBall3
	ld A,(BALL3)
	or A
	jr z,GAME_InitWeaponryBallDone
	
	ld A,E
	call BONUS_Get
	ld A,E
	call BONUS_Get
	ld A,E
	call BONUS_Get
	
.GAME_InitWeaponryBallDone
	ld E,5
	
	ld A,(PILL1)
	or A
	jr z,GAME_InitWeaponryPillDone
	
	xor A
	ld (PILL1),A
	ld A,E
	call BONUS_Get
	
	ld A,(PILL2)
	or A
	jr z,GAME_InitWeaponryPillDone
	
	xor A
	ld (PILL2),A
	
	ld A,E
	call BONUS_Get
	
.GAME_InitWeaponryPillDone

	call MISSLE_Init

	ret

;save game vars

.GAME_SaveVars
	ld IX,GAME_SaveTable
	ld BC,5
	
.GAME_SaveVarsLoop
	ld L,(IX+2)
	ld H,(IX+3)
	ld A,L
	or H
	ret z
	ld A,(HL)
	ld L,(IX+0)
	ld H,(IX+1)
	ld (HL),A
	add IX,BC
	jr GAME_SaveVarsLoop
	
;restore game vars

.GAME_RestoreVars
	ld IX,GAME_SaveTable
	ld BC,5
	
.GAME_RestoreVarsLoop
	ld L,(IX+0)
	ld H,(IX+1)
	ld A,L
	or H
	ret z
	ld A,(HL)
	ld L,(IX+2)
	ld H,(IX+3)
	ld (HL),A
	add IX,BC
	jr GAME_RestoreVarsLoop
	

.GAME_SaveTable
	dw save_TILENO,TILENO
	DEF_TILENO EQU $
	db 0
	dw save_SYNCHOLD,SYNCHOLD
	DEF_SYNCHOLD1 EQU $
	db 0
	dw save_SYNCHOLD+1,SYNCHOLD+1
	DEF_SYNCHOLD2 EQU $
	db 0
	dw save_BG_COLOR,BG_COLOR:db #54
	dw save_FL_COLOR,FL_COLOR:db #58
	dw save_PILL1,PILL1:db 0
	dw save_PILL2,PILL2:db 0
	dw save_MISSILE,MISSILE:db 0
	dw save_BALL1,BALL1:db 0
	dw save_BALL2,BALL2:db 0
	dw save_BALL3,BALL3:db 0
	dw save_BALLPOW,BALLPOW:db 0
	dw save_BG_CurrentEffect,BG_CurrentEffect:db 0
	dw save_BG_StopCommand,BG_StopCommand:db 0
	dw save_BG_Counter,BG_Counter:db 0
	dw save_BG_Effect_Addr,BG_Effect_Addr:db BG_EffectNull AND 255
	dw save_BG_Effect_Addr+1,BG_Effect_Addr+1:db BG_EffectNull / 256
	dw 0,0

.GAME_ScoreInitStart

	;init lives
	ld A,(option_menu_lives)	;get options
	add A	;*2
	inc A	;+1
	ld (Current_Lives),A
	
.GAME_ScoreInitStart_ResetScoreEntry	
	ld HL,Current_Score
	ld DE,LVL_Score		;stage score
	ld B,6
	xor A
	;ld A,1
;init score
.GAME_InitStartInitScore
	ld (HL),A
	ld (DE),A
	inc HL
	inc DE
	djnz GAME_InitStartInitScore
	
	ret
	
;enter game here !
.GAME_InitStart

;init continues
	ld A,(option_menu_continue)
	inc A
	ld (Current_Tries),A	

	call GAME_ScoreInitStart
	
;reset all datas in table
	ld IX,GAME_SaveTable
	ld BC,5
	
.GAME_InitStart_Table
	ld L,(IX+0)
	ld H,(IX+1)
	ld A,L
	or H
	jr z,GAME_InitStart_Table_Done
	ld A,(IX+4)
	ld (HL),A
	add IX,BC
	jr GAME_InitStart_Table

.GAME_InitStart_Table_Done	
	;set current level
IF debug 
	ld A,test_level
	ld (current_lvl),A
ENDIF

;is first ?	
	ld A,(current_lvl)
	dec A

IF debug	
	ld A,1
ENDIF

	ld (BINGBONG),A		;the first run = intro !
	
;show hud
	xor A
	ld (no_hud),A

	ret
;***************************
;*** INIT WEAPON SPRITES ***
;***************************
.GAME_InitWeapons

	;init bullets	
	
	ld A,141
	ld IY,BULLETS_Table_Dir0
	
	;group A
	ld IX,#C600
	call BULLET_InitSprites
	
	;group B
	ld IX,#CE00
	call BULLET_InitSprites	

	;init fire
	ld A,62-4
	ld B,5
	ld IY,Fire_lopow_sprite+8
	ld IX,#D600
	ld DE,-2
	call BULLET_InitSprites_loop
	
	;init laser - IX is already defined in init fire
	
	;set 52
	ld (laser_move0+2),IX
	ld (laser_move4+2),IX
	ld (laser_move3+2),IX
	ld (laser_move7+2),IX
	ld A,52
	call GAME_InitWeaponsCompileSprite
	;set 53
	ld (laser_move1+2),IX
	ld (laser_move5+2),IX
	ld A,53
	call GAME_InitWeaponsCompileSprite
	;set 54
	ld (laser_move2+2),IX
	ld (laser_move6+2),IX
	ld A,54
	call GAME_InitWeaponsCompileSprite
	
	;init enemies default bullet
	ld IX,#DE00
	ld A,1
	call GAME_InitWeaponsCompileSprite
	
	ret
; A = sprite , IY = address
.GAME_InitWeaponsCompileSprite
	;get pixels
	call USPGADD
	
	;get first tile
	inc HL	;global flag
	inc HL	;tile flags
	ld E,(HL)	;low addr
	inc HL	
	ld D,(HL)	;high addr
	
	;compile sprite
	call AUTO_Generate_Sprite
	
	ret



;.LaserSprite_Table
	;db 1,53,54,52
	;db 1,53,54,52
	
;*** was INIT0***

.GAME_Init0

	;ld A,#54
	;ld (BG_COLOR),A
	
	ld HL,HUD_DefaultColors
	ld (int_hud_color_setup),HL

	SetIntTable int_game_table
	
	ld A,(no_hud)
	or A
	ret nz

	;LD HL,(SYNCHRO1)
 	;LD (SYNCHRO),HL
 	;LD (SYNCHOLD),HL
	;LD A,(START1)
  	;LD (START),A
 	;LD (TILENO),A
	
	ld HL,GAUGE_POS
	;call GAUGE_Display
	call GAUGE_Draw
	
	ld DE,SCORE_POS-6
	ld HL,InitScore
	ld BC,color_table_game_text
	call FONT_DrawStringEX
	
	ld DE,BEAM_POS
	ld HL,InitBeam
	ld BC,color_table_game_text
	call FONT_DrawStringEX

	ld A,2
	ld (Supdate),A
	call SCORE_Update
	call SCORE_Update
	
	call LIVES_Show
	
	ld DE,HI_POS
	ld HL,InitHi
	ld BC,color_table_game_text
	call FONT_DrawStringEX

;update hi score string	
	ld DE,Current_hi_score
	ld HL,HISCORE_String
	call SCORE_UpdateString
	call SCORE_DrawHiScore
	
	ret
	
.InitScore
	str "1P-"
.InitBeam
	str "BEAM"


	
;*** was INIT1 ***
.GAME_Init1
	LD IX,INIT1DATA
INIT1A	
	LD L,(IX+0)
 	LD H,(IX+1)
 	LD C,(IX+2)
 	LD B,(IX+3)
 	;CALL FILL0

.Init1B
	ld (HL),0
	inc HL
	dec BC
	ld A,B
	or C
	jr nz,Init1B
	
	LD BC,4
 	ADD IX,BC
 	LD A,(IX+1)
 	AND A
 	JR NZ,INIT1A

	XOR A
 	LD (EXBYTE),A
	LD (URSHIP1E),A
 	LD (ALIENACT),A
 	LD (MISSCOUNT),A
	;LD (NODIE),A		;unquote later
 	LD (BONUS),A
;
	CALL GETSYNC100
	
	call GAME_RestoreVars
	
 	ret
	
;*** was INIT2 ***
;39DB
.GAME_Init2

;reset all the table	
	ld IX,GAME_Init2_table
	ld B,0
.GAME_Init2_loop
	ld L,(IX+0)
	ld H,(IX+1)
	ld A,L
	or H
	cp 0
	jr z,GAME_Init2_done
	ld (HL),B
	inc IX
	inc IX
	jr GAME_Init2_loop
	
.GAME_Init2_done

	ld HL,GAME_Restart
	ld (END_JUMP),HL	
	
	ld A,4
	ld (ALIENBULL),A
	
	;setup some correct default values
	ld BC,#0A00
	ld (BALL1X),BC
	ld (BALL2X),BC
	ld (BALL3X),BC
	ld (SHIPX),BC
	
	call GAME_InitWeaponry
	
	RET
.GAME_Init2_table
	dw WAVE_COUNTER	
	dw TILEPOS
	dw TILESCROLL
	dw MISSCOUNT
	dw MISS1BUFF
	dw MISS2BUFF
	dw NOSCROLL
 	dw PLASPRINT
	dw PLASCOUNT
	dw FRAME
	dw current_laser
	dw CIRC_State	
	dw SCR_SPEC1
	dw SCR_SPEC2
	dw BALL1P
	dw BALL2P
	dw BALL3P
	dw BALLTYPE
	dw ATTATCH
	dw BALLTIME	
	dw PILL2P
	dw PILL1P	
	dw JEW1
	dw JEW2	
	dw BIGEXPLO_State
	dw SOFT_END_State
	dw ForcePlayerMove
	dw LVL_End
	dw END_COUNT
	
	dw 0	;end

.INIT1DATA	
	DEFW SEQBUFF,28*9
 	DEFW BULLMTABLE,48
 	DEFW ALIENFIRE,84 ;109
	DEFW EXPLOBUFF,60
 	DEFW FXBYTE1,7
	DEFW LaserBuffer,160+32
	DEFW MISS1BUFF,6
	DEFW MISS2BUFF,6
	dw WaveBufferB1,4
	dw WaveBufferB2,4
	dw WaveBufferR1,4
	dw WaveBufferR2,4
	DEFW 0000

.int_game_table	
	dw int_HUD2	
	dw int_done
	dw int_done
	dw int_done	
	dw int_HUD
	dw int_done	
	dw int_done
	dw int_done
	
.int_endlevel_table	
	dw int_HUD3	
	dw int_done
	dw int_HUD4
	dw int_done	
	dw int_HUD
	dw int_done	
	dw int_done
	dw int_done

.HUD_DefaultColors
	db #54,#4B,#57,#4C
.HUD_EndColors
	db #54,#57,#53,#4C
	
;set reserved colors 
; HL = colors , B = #7F
.HUD_SetReservedColors
	xor A	
.HUD_SetReservedColorsLoop
	ld C,(HL)
	out (C),A
	out (C),C
	inc HL
	inc A
	cp 4
	jr nz,HUD_SetReservedColorsLoop
	ret

.int_HUD
	push DE
	call KEY_Update
	pop DE
;synchro , loose 1 lines
	ld B,8 ;8 - 1
.int_HUD_sync
	ds 4,0
	djnz int_HUD_sync	;4
	nop	;missing this one
;break	

	ld B,#7F

int_hud_color_setup EQU $+1
	ld HL,HUD_DefaultColors
	call HUD_SetReservedColors
	
	ld C,#8C+1	;mode1
	out (C),C
	
	jp int_done
	
.int_HUD2	
	ld BC,#7F8C		;mode0
	out (C),C		

	ld HL,BG_COLOR
	call HUD_SetReservedColors
	
	ld A,(lvl_floating_color)
	ld C,A
	ld A,(FL_COLOR)
	out (C),C
	out (C),A
	
	jp int_SND_Player
	
.int_HUD3
	;ld BC,#7F8D		;mode1
	;out (C),C	
	;set text colors
	ld BC,#7F02
	ld A,#53
	out (C),C
	out (C),A
	ld A,#57
	inc C
	out (C),C
	out (C),A
	
	ld A,(BG_COLOR)	;bg color
	db #ED,#71 ;out (C),0
	out (C),A
	
	ld A,(lvl_floating_color)
	ld C,A
	ld A,(FL_COLOR)
	out (C),C
	out (C),A
	jp int_SND_Player
	
.int_HUD4

	
	ld B,60
	
.int_HUD4_Wait
	ds 16,0
	djnz int_HUD4_Wait
		
	ld BC,#7F8C		;mode0
	out (C),C	
	
	;restore colors
	ld C,2
	ld A,#57
	out (C),C
	out (C),A
	ld A,#4C
	inc C
	out (C),C
	out (C),A
	
	jp int_done

;******************
;*** ADD CHEATS ***
;******************
	
.GAME_Init_CheckCheats	

	ld A,(cheat_active)
	or A
	ret z

	ld A,(cheat_pill1)
	ld (save_PILL1),A
	
	ld A,(cheat_pill2)
	ld (save_PILL2),A
	
	ld A,(cheat_missiles)
	ld (save_MISSILE),A
	
	ld A,(cheat_mod1)
	ld (save_BALL1),A
	
	ld A,(cheat_mod2)	
	ld (save_BALL2),A
	
	ld A,(cheat_mod3)
	ld (save_BALL3),A
	
	ld A,(cheat_weapon)
	inc A
	ld (save_BALLPOW),A

	ret

.GAME_Init_Inv

	ld A,(cheat_active)
	or A
	ret z
	
	ld HL,PLAYER_Die
	ld (HL),0
	
	ld A,(cheat_inv)
	or A
	ret z
	
	ld (HL),#C9
	ret

;casual mode init	
.GAME_Init_CheckCasual

;casual mode ?

	ld A,(option_menu_game)
	or A
	ret z


	ld A,(current_lvl)
;casual only for level 0-8
	cp 9
	ret nc

;select a weaponry using level select	
	ld A,(casual_menu_try)
	and 7	;0-7
	add A	;2
	add A	;4
	add A	;8
	ld L,A
	ld H,0
	ld DE,casual_table
	add HL,DE
	;now HL points on level table
	
	;weapon
	ld A,(HL)
	inc HL
	ld (save_BALLPOW),A
	
	;module A
	ld A,(HL)
	inc HL
	ld (save_BALL1),A
	
	;module B
	ld A,(HL)
	inc HL	
	ld (save_BALL2),A
	
	;module C
	ld A,(HL)
	inc HL
	ld (save_BALL3),A
	
	;pill A
	ld A,(HL)
	inc HL
	ld (save_PILL1),A
	
	;pill B
	ld A,(HL)
	inc HL
	ld (save_PILL2),A
	
	;missile
	ld A,(HL)
	inc HL
	ld (save_MISSILE),A
	
	ret
.casual_table
	;level 1
	db 1		;weapon (1 laser , 2 circular , 3 fire)
	db 0,0,0	;module state A/B/C
	db 0,0		;pill A/B
	db 0		;missile
	db 0		;padding
	;level 2
	db 1		;weapon
	db 1,0,0	;module state A/B/C
	db 0,0		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 3
	db 2		;weapon
	db 0,1,0	;module state A/B/C
	db 0,0		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 4
	db 3		;weapon
	db 0,0,1	;module state A/B/C
	db 0,0		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 5
	db 1		;weapon
	db 0,0,1	;module state A/B/C
	db 1,0		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 6
	db 1		;weapon
	db 0,0,1	;module state A/B/C
	db 1,1		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 7
	db 1		;weapon
	db 0,0,1	;module state A/B/C
	db 1,1		;pill A/B
	db 1		;missile
	db 0		;padding
	;level 8
	db 1		;weapon
	db 0,0,1	;module state A/B/C
	db 1,1		;pill A/B
	db 1		;missile
	db 0		;padding
