;**********************
;*** LEVEL A - DEMO ***
;**********************

ORG level_start

IF output_files
write "bin/levelA.lvl"
ENDIF

START	DEFB 0
SYNCHRO	DEFW 0000

START1	DEFB 0
START2	DEFB 0
START3	DEFB 0

SYNCHRO1	DEFW SYNC1
SYNCHRO2	DEFW 0
SYNCHRO3	DEFW 0

TSPRADD		DEFW 0
TADD		DEFW 0
SEQTABLE	DEFW SQTABLE
SPEVENT1	DEFW SPEV1
SPEVENT2	DEFW SPEV2
;DEFB 0,0,0

;palette stuff 16 colors+border
.lvl_pal
	db 0,26,11,6,3,20,10,2,1,25,16,18,9,15,13,0
	db 0
	db 0,20,2,3,0,10,1,1,0,12,3,9,0,3,1,0
	db 0
	db 0,11,1,3,0,1,0,1,0,3,0,9,0,3,0,0
	db 0
	db 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	db 0

.lvl_floating_color
	db 14
.lvl_music
	db #FF	;no music 
.lvl_skip
	dw 0	;music to skip on music restart
.lvl_init
	dw	lA_init	;called after loading
.lvl_reinit
	dw	lA_reinit	;called each time player dies
	
.lA_init
	;no hud
	ld A,1
	ld (no_hud),A
	;force to return directly to casual menu
	ld A,1
	ld (menu_force),A

	ret
.lA_reinit
	xor A
	ld (fading_count),A
	
	;no weapon !
	xor A
	ld (BALL1),A
	ld (BALL2),A
	ld (BALL3),A
	ld (PILL2),A
	ld (PILL1),A
	ld (MISSILE),A
	ld (BALLTYPE),A

IF MMI_IsRecordMode
	ld HL,#4002
	ld A,#C5
	call MMI_EnterRecordMode
ELSE
	ld HL,level_demo
	ld A,#C0
	call MMI_EnterPlayMode
ENDIF

;registers BG effects
	ld HL,STARS_Init
	ld (BG_LevelEffect0_Init),HL
	
	ld HL,STARS_Run
	ld (BG_LevelEffect0_Run),HL

	ld HL,STARS_Stop
	ld (BG_LevelEffect0_Stop),HL 
	
;SFX only !

	ld A,2
	call SND_SetMode

	ret
	
;	LEVEL	A - ATTACK WAVES
;
;	1ST BYTE = TILENO
;	2ND BYTE
;		BITS 0-2 = TILESCROLL
;		BITS 3-4 = TILEPOS
;		BITS 5-7 =
;	3RD BYTE = SEQUENCE NUMBER
;	4TH BYTE = INITIAL X POSITION
;	5TH BYTE = INITIAL Y POSITION
;	(TILESCROLL = FRAME OR BITPOS)
;
SYNC1

	START_EFFECT 0,1,1
	FORCE_Slot 1,0,1,12,0,0	;ESC/fire control

	db 0,1,2,0,15
	db 0,1,99,4,0
	SCROLL_Stop	0,1

;move
	db 1,0,11,0,0
	db 1,1,11,0,0
;shoot
	db 2,22,11,0,0
	db 2,23,11,0,0
	
;demonstrate simple shoot and move
;enemy wave	
	db 3,0,1,31,5
	db 3,4,1,31,8
	db 3,8,1,31,10	
	db 3,12,1,31,7
	db 3,16,1,31,12
	db 3,20,1,31,4
	db 3,24,1,31,10	
	
;demonstrate BEAM

;green alien	
	db 5,0,7,28,255
;beam shoot load
	db 5,24,11,0,0
	db 5,25,11,0,0
	
;beam shoot release
	db 7,0,11,0,0
	db 7,1,11,0,0
	

;force bonus
	db 8,0,3,31,8
	
;get force txt
	db 8,26,11,0,0	
	db 8,27,11,0,0	
	
;bit bonus
	db 10,0,4,31,8
	
;get bit txt
	db 10,28,11,0,0	
	db 10,29,11,0,0	
	
;red bonus
	db 12,0,3,31,8
;increase force power
	db 12,24,11,0,0
	db 12,25,11,0,0

;force release
	db 16,0,11,0,0
	
;released force on enemy wave
;enemy wave	
	db 16,0,1,31,5
;erase txt here	
	db 16,1,11,0,0
	
	db 16,4,1,31,2
	db 16,8,1,31,10	
	db 16,12,1,31,7
	db 16,16,1,31,12
	db 16,20,1,31,4
	db 16,24,1,31,10
	
;red bonus
	db 18,0,3,31,8	
	
;force return
	db 18,0,11,0,0
	db 18,1,11,0,0

	
;back attack
;txt
	db 19,30,11,0,0
	db 19,31,11,0,0
;yellow butterflies
	db 20,0,9,4,255
	db 20,0,10,-2,9
	
	db 20,4,9,4,255
	db 20,4,10,-2,9
	
	db 20,8,9,4,255
	db 20,8,10,-2,9
	
	db 20,12,9,4,255
	db 20,12,10,-2,9
	
	db 20,16,9,4,255
	db 20,16,10,-2,9
	
	db 20,20,9,4,255
	db 20,20,10,-2,9
	
	db 20,24,9,4,255
	db 20,24,10,-2,9

	
;end level
	FORCE_Slot 1,24,0,6,0,0
	
;bonuses
	;db 1,0,3,31,10
	;db 1,0,4,31,5
	db 255,255,255,255,255
	
.SQTABLE	
	dw SQ1,SQ2,SQ3,SQ4,SQ5,SQ6,SQ7,SQ8,SQ9,SQ10,SQ11_writter,SQ12_Esc
	

	
 ;
;	1ST BYTE
 ;		BITS 0-3 = SPECIAL EVENT 1
;		BITS 0-4 = SPECIAL EVENT 2
;	2ND BYTE = HIT POINTS
;	3RD BYTE
 ;		BITS 0-3 = POINT VALUE
;		BITS 4-7 = EXPLOSION TYPE
 ;	BYTES ARE NOW PAIRED TO MAKE UP
;	ADDRESS OF SPRITE PATTERNS
 ;	WORD 0 = END OF SEQUENCE
;

;RED TWISTERS
.SQ1	DEFB 0,65,18 ;0,1+64,2+16*1
		DEFW SP1,SP1,SP1,SP1,0
		
;controller
.SQ2	db 2,63,0
		dw SP2
		dw #FFFF
		
;podder wave
.SQ3	db 3,1,34
		dw SP3A,SP3A,SP3A,SP3A
		dw SP3A,SP3A,SP3A,SP3A
		dw 0
;podder bit
.SQ4
		db 4,1,34
		dw #0303
		dw 0
		
;reversed twisters
.SQ5	db 0,65,18
		dw SP1A,SP1A,SP1A,SP1A,0
		
;exit level
.SQ6	db 5,63,0
		dw SP6,#FFFF
.SP6	db 1,128

;BLUE GOBOT
SQ7	DEFB 6,28,69 ;10,28,5+4*16
 	DEFW SP7,SP7,SP7,SP7,SP7,SP7A,65535
 ;BLUE GOBOT PROJECTILE
SQ8	DEFB 7,63,28 ;11,63,12+16*1
	DEFW SP8,SP8,0
	
;yellow butterflies
SQ9	db 0,1,#20
	dw SP9_down
	dw SP9_turn
	dw SP9_right
	dw 0
.SP9_down
repeat 3:db 17,8:rend
	db 17,8+128
.SP9_turn
	db 18,8+32,18,8+32+128
.SP9_right
repeat 34:db 19,32:rend
	db 19,32+128
	
;yellow bufferflies back
SQ10	db 0,1,#20
		dw SP9_right
		dw 0
		
;writter
SQ11_writter
		db 8,1,0
		dw SP11
		dw 0	;stay only one frame
.SP11
		db 1,128
		
;check for ESC
SQ12_Esc
		db 9,1,0
		dw SP11
		dw #FFFF
	
	
;	1ST BYTE = SPRITE NUMBER
 ;
;	2ND BYTE =
 ;		BITS 0-1 = ORIENTATION
;		BIT 2 = UP
;		BIT 3 = DOWN
;		BIT 4 = LEFT
;		BIT 5 = RIGHT
 ;		BIT 6 =
;		BIT 7 = END OF PATTERN
;
 ;RED TWISTERS
.SP1	DEFB 49,16,50,24,51,16,50,20,49,20,50,16,51,24,50,16+128
.SP1A	DEFB 49,32+1,50,24+16+1,51,16+16+1,50,20+16+1,49,20+16+1,50,16+16+1,51,24+16+1,50,1+32+128
;controller
.SP2	db 22,128
;green alien
SP7		DEFB 41,0,41,0,41,0
SP7A	DEFB 41,0+128
;BLUE GOBOT PROJECTILE
SP8		DEFB 12,16,12,16+2,12,16,12,16+2
		DEFB 12,16,12,16+2,12,18+128
		

;podder
.SP3A
		db 1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16+128
.SP3B
		db 1,20,1,20,1,20,1,20,1,20,1,20,1,20,1,20+128

.SPEV1
	dw FX0,FX1,FX2,FX3,FX4,FX5
	dw FX10,FX11	;in fact 6 and 7
	dw FX8_writter
	dw FX9_esc
.SPEV2
	dw FXA0,FXA1

;NO ACTION
FX0		JP C,TV5
		JP TV1
 
;HALF SPEED
FX1	
	JP C,TV5
FX1A	
	LD A,(FRAME)
	AND 1
 	JP NZ,TV1
 	JP TV2
	
;controller
.FX2
	push IY
	push DE
	push BC
	
	SetMaster NeutralMaster

	
	ld L,32
	ld A,(MMIVAR)
	bit 4,A
	jr z,FX2_NotFireA
	inc L
.FX2_NotFireA
	ld A,5
	add C
	ld C,A
	ld A,2
	add B
	ld B,A
	ld A,L
	CALL TSPGADD
	CALL SPNS
	
	pop BC
	push BC
	
	ld L,32
	ld A,(MMIVAR)
	bit 5,A
	jr z,FX2_NotFireB
	inc L
.FX2_NotFireB
	ld A,9
	add C
	ld C,A
	ld A,2
	add B
	ld B,A
	ld A,L
	CALL TSPGADD
	CALL SPNS	
	
	pop BC
	pop DE
	pop IY
	


	ex AF,AF'
	ld HL,MMIVAR
	ld A,26		;neutral
	
	bit 0,(HL)	;is up ?
	jr z,FX2_notup
	ld A,29
.FX2_notup

	bit 1,(HL)
	jr z,FX2_nodown
	ld A,23
.FX2_nodown

	bit 2,(HL)
	jr z,FX2_noleft

	dec A
.FX2_noleft
	bit 3,(HL)
	jr z,FX2_noright
	inc A
.FX2_noright
	ex AF,AF'
	jp TV2
;red bonus	
.FX3
	;jr c,FX3_bonus
	jp nc,FX1A
.FX3_Bonus
	ld L,2
.FX_GenBonus
	LD A,(JEW1)
 	AND A
 	JR NZ,FXA15B
;
	LD (JEW1X),BC
 	LD A,L
	LD (JEW1),A
 	LD A,(FRAME)
	DEC A
 	AND 7
 	LD (JEW1P),A
	JP TV3
;
FXA15B	
	LD (JEW2X),BC
 	LD A,L
	LD (JEW2),A
 	LD A,(FRAME)
	DEC A
 	AND 7
 	LD (JEW2P),A
	JP TV3
;bit bonus
.FX4
	jp nc,FX1A
	ld L,5
	jr FX_GenBonus
	
;fade and exit
.FX5
.fading_action
	ld A,(FRAME)
	and 7
	jp nz,TV5
	
	ld A,(fading_count)
	inc A
	ld (fading_count),A
	
	cp 4
	jr nc,fading_action_end
	
	ld HL,lvl_pal	
	call GFX_SetupLevelPalette
	
	jp TV5
.fading_action_end
	call GFX_HideScreen
	jp GAME_FullRestart
	
.fading_count
	db 0
	
;*******************
;*** BLUE GOBOT ***
;*******************

FX10

	JP C,TV5
	
	jr z,FX10_nodamage
	
	xor A
	call SND_SfxPlay
	
.FX10_nodamage
	
	ld A,(FX10R)
	or A
	jr nz,FX10_DoRecoil

	ld A,C
	cp 22
	jr c,FX10_NoForward

	dec C

.FX10_NoForward

	LD A,(IY+8)
 	CP 6
	JR Z,FX10D
	AND 1
 	JP Z,TV1
	LD L,B
	INC L
 	INC L
 	LD A,(SHIPY)
	CP L
	JP Z,TV1
	JR C,FX10A
	INC B
 	JP TV1
FX10A	DEC B
	JP TV1
;
FX10D	LD HL,FX10E+2
	LD A,B
	INC A
 	LD (HL),A

	dec HL
	ld A,C
	ld (HL),A

 	LD HL,FX10E
 	CALL FORCESYNC
 ;LD HL,9*256+HALF+1
 ;CALL BEEP
	ld A,sfx_L1_GreenAlien
	call SND_SfxPlay 
 
	ld A,3
	ld (FX10R),A
.FX10_Recoil
	ld A,42
	ex AF,AF'
 	JP TV1
.FX10_DoRecoil
	ld A,(FX10R)
	dec A
	ld (FX10R),A

	ld A,C
	inc C
	cp 28
	jr c,FX10_Recoil

	ld C,28
	jr FX10_Recoil

FX10E	DEFB 8,24,0
FX10R	db 0

;*******************************
;*** BLUE GOBOT FLAME WEAPON ***
;*******************************
.FX11	
	DEC C
 	JP TV1
	
;NO ACTION
.FXA0	JP TV3
;HALF SPEED
.FXA1	
	LD A,(FRAME)
	AND 1
 	JP NZ,TV3
 	JP TV4
	
;writter	
.FX8_writter
;break
;wait end of screen to avoid artifacts
	ld A,(int_counter)
	cp 5
	jr nz,FX8_writter

	push DE
	push BC
	ld A,20
	call GFX_SendR6
;erase text first	
	ld HL,writter_empty_str
	ld DE,writter_base	
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	
	ld HL,writter_empty_str
	ld DE,writter_base+128	
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	
	
.FX8_writter_loop	
	ld HL,(writter_ptr)
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld A,E	;is address string empty?
	or D
	jr z,FX8_writter_done
	
	ld C,(HL)
	inc HL
	ld B,(HL)
	inc HL
	
	push BC	;save text address
	
	ld C,(HL)
	inc HL
	ld B,(HL)
	inc HL
	ld A,(HL)
	inc HL
	ld (writter_ptr),HL ;save pointer to next string
	
	pop HL	;get text address
	push AF
	
	call FONT_DrawStringEX
	
	pop AF
	
	cp 2
	jr z,FX8_writter_done_erase_only
	
	or A
	jr nz,FX8_writter_loop		
	
.FX8_writter_done

	ld A,24
	call GFX_SendR6
	
	ld E,150
	call DSK_wait	
	
	pop BC
	pop DE
	jp TV5
.FX8_writter_done_erase_only
	ld A,24
	call GFX_SendR6
	
	pop BC
	pop DE
	jp TV5
	
writter_base	EQU #C540
.writter_ptr
	dw writter_table
.writter_table
;move
	dw writter_base+8,writter_text0,color_table_options_normal:db 1
	dw writter_base+8+8,writter_text0_red,color_table_options_red:db 1
	dw writter_base+128+8,writter_text0B,color_table_options_normal:db 0
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
;fire
	dw writter_base+2,writter_text1,color_table_options_normal:db 1
	dw writter_base+2+34,FIRE1_txt,color_table_options_red:db 1
	dw writter_base+2+128,writter_text1B,color_table_options_normal:db 0
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
;beam load
	dw writter_base+14,writter_text2,color_table_options_normal:db 1
	dw writter_base+14+10,FIRE1_txt,color_table_options_red:db 1
	dw writter_base+14+128,writter_text2B,color_table_options_normal:db 0
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
;beam release	
	dw writter_base+11,writter_text3,color_table_options_normal:db 1
	dw writter_base+11+16,FIRE1_txt,color_table_options_red:db 1
	dw writter_base+11+128,writter_text3B,color_table_options_normal:db 0
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
;get force
	dw writter_base+13,writter_text4,color_table_options_normal:db 1
	dw writter_base+128+13,writter_text5,color_table_options_normal:db 0
;erase
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2	
;items
	dw writter_base+6,writter_text6,color_table_options_normal:db 1
	dw writter_base+128+6,writter_text7,color_table_options_normal:db 0	
;erase
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2	
;force power
	dw writter_base+5,writter_text8,color_table_options_normal:db 1
	dw writter_base+128+5,writter_text9,color_table_options_normal:db 0
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
	
;force release
	dw writter_base+13,writter_text10,color_table_options_normal:db 1
	dw writter_base+13+12,FIRE2_txt,color_table_options_red:db 1
	dw writter_base+13+128,writter_text10B1,color_table_options_normal:db 0
	
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
;force return	
	dw writter_base+7,writter_text10B,color_table_options_normal:db 1
	dw writter_base+7+12,FIRE2_txt,color_table_options_red:db 1
	dw writter_base+7+128,writter_text10B2,color_table_options_normal:db 0
	
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2
	
;force rear
	dw writter_base+5,writter_text11,color_table_options_normal:db 1
	dw writter_base+128+5,writter_text11B,color_table_options_normal:db 0	
;erase	
	dw writter_base,writter_empty_str,color_table_options_normal:db 1
	dw writter_base+128,writter_empty_str,color_table_options_normal:db 2		
	
	dw 0
	
;move
.writter_text0
	str "USE DIRECTIONAL CONTROLS"
.writter_text0_red
	str "DIRECTIONAL"
.writter_text0B
	str "TO MOVE YOUR SHIP"
	
;fire	
.writter_text1
	str "PRESS REPEATEDLY FIRE 1 BUTTON"
.writter_text1B
	str "TO SHOOT ENEMIES"
	

.FIRE1_txt
	str "FIRE 1"
.FIRE2_txt
	str "FIRE 2"
	
;load BEAM	
.writter_text2
	str "HOLD FIRE 1 BUTTON"
.writter_text2B
	str "TO CONCENTRATE ENERGY"
	
;release BEAM
.writter_text3
	str "RELEASE FIRE 1 BUTTON"
.writter_text3B
	str "TO SHOOT A POWERFULL BEAM"
	
;collect gem	
.writter_text4
	str "COLLECT A FIRST GEM"
.writter_text5
	str "TO GET THE FORCE"
	
;collect items
.writter_text6
	str "COLLECT ITEMS"
.writter_text7
	str "TO INCREASE YOUR EQUIPMENT"
	
;increase power	
.writter_text8
	str "COLLECT GEMS"
.writter_text9
	str "TO INCREASE YOUR FIRE POWER"
	
;release force	
.writter_text10
	str "PRESS FIRE 2 BUTTON"
.writter_text10B1
	str "TO RELEASE THE FORCE"	

;return force	
.writter_text10B
	str "PRESS FIRE 2 BUTTON AGAIN"	
.writter_text10B2	
	str "TO GET THE FORCE BACK"
	
.writter_text11
	str "DOCK THE FORCE ON REAR SIDE"
.writter_text11B
	str "TO SHOOT REVERSE"
.writter_empty_str
	ds 31,0
	db 128
	
.FX9_esc
	ld IX,keymap
	bit 7,(IX+8)	;crtl
	jr z,FX9_Done
	bit 7,(IX+2)	;A
	jr z,FX9_Done	
	bit 2,(IX+8)	;ESC
	jr z,FX9_Done
	bit 4,(IX+9)	;joy fire A
	jr z,FX9_Done	

	jp TV5
.FX9_Done
	ld (IY+0),0		;disable it !
	ld A,25
	ld (TILENO),A	;prevent other events
	ld HL,FX9_DoneSpawn
	call FORCESYNC
	jp TV5
	
.FX9_DoneSpawn
	db 6,0,0

read "starfield.asm"
	
.level_demo
	incbin "demo.bin"
	db 0,0,0
