
;Uncomment one of the lines below to select your compilation target

;BuildCPC equ 1	; Build for Amstrad CPC
;BuildMSX equ 1 ; Build for MSX
;BuildTI8 equ 1 ; Build for TI-83+
;BuildZXS equ 1 ; Build for ZX Spectrum
;BuildENT equ 1 ; Build for Enterprise
;BuildSAM equ 1 ; Build for SamCoupe
;BuildSMS equ 1 ; Build for Sega Mastersystem
;BuildSGG equ 1 ; Build for Sega GameGear
;BuildGMB equ 1 ; Build for GameBoy Regular
;BuildGBC equ 1 ; Build for GameBoyColor 
ScrWid256 equ 1
;BuildMSX_MSX1VDP equ 1

;RedefineKeys equ 1

	ifdef BuildSMS
BuildCONSOLE 	equ 1
BuildCONSOLEjoy	equ 1
BuildSxx 		equ 1

	endif
	ifdef BuildSGG
BuildCONSOLE 	equ 1
BuildCONSOLEjoy	equ 1
BuildSxx 		equ 1
ScreenSmall 	equ 1
	endif
	ifdef BuildGMB
PaletteGB 		equ 1
BuildCONSOLE 	equ 1
BuildCONSOLEjoy	equ 1
BuildGBx 		equ 1
ScreenSmall 	equ 1
	endif
	ifdef BuildGBC
PaletteGB 		equ 1
BuildCONSOLE 	equ 1
BuildCONSOLEjoy	equ 1
BuildGBx 		equ 1
	endif
	ifdef BuildCPC
Palette4 		equ 1
	endif
	ifdef BuildENT
Palette4 		equ 1
	endif
	ifdef BuildTI8
ScreenSmall 	equ 1
	endif
	ifdef BuildMSX_MSX1
BuildMSX_MSX1VDP equ 1
BuildCONSOLE 	equ 1
	endif
	
	ifndef Palette4
		ifndef PaletteGB
Palette16 equ 1
		endif
	endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ifdef vasm
		include "..\SrcALL\VasmBuildCompat.asm"
	else
		read "..\SrcALL\WinApeBuildCompat.asm"
	endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	read "..\SrcALL\V1_Header.asm"
	ifdef BuildCONSOLE
		read "..\SrcALL\V1_VdpMemory_Header.asm"
	else
		read "..\SrcALL\V1_BitmapMemory_Header.asm"
	endif
	read "..\SrcALL\CPU_Compatability.asm"
	read "..\SrcALL\Multiplatform_ReadJoystick_Header.asm"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ifdef BuildTI8
ScreenWidthG equ ScreenWidth*2
ScreenHeightG equ ScreenHeight*2
	else
ScreenWidthG equ ScreenWidth
ScreenHeightG equ ScreenHeight
	endif

	ifdef BuildCONSOLE
	ifdef BuildMSX
GameRam equ &C000
	else
GameRam equ &D800
		endif
	else
		ifdef BuildMSX
GameRam equ &C000
		else
		ifdef BuildTI8
GameRam equ &F000
			else
GameRam equ &7000
			endif
		endif
	endif
MonitorRam 						equ GameRam
Monitor_TMP1 					equ MonitorRam; &C011
Monitor_NextMonitorPos_2Byte	equ MonitorRam+2;&C012;-13
Monitor_EI_Reenable_2byte 		equ MonitorRam+4;&C014;-5
Monitor_Special					equ MonitorRam+6;&C016

KeyboardScanner_KeyPresses		equ GameRam+48
PlayerX1	equ GameRam+32
RandomSeed 	equ PlayerX1+1
PlayerY1	equ PlayerX1+2
FastGrowth	equ PlayerX1+3
PlayerDir	equ PlayerX1+4
BulletX1	equ PlayerX1+5
NothingShot	equ PlayerX1+6
BulletY1	equ PlayerX1+7
AllowedFastGrowth	equ PlayerX1+9
BulletDir	equ PlayerX1+10
PlayingSFX	equ PlayerX1+11
ControlMode	equ PlayerX1+12
GameTick	equ PlayerX1+13
Lives	equ PlayerX1+14
Score		equ PlayerX1+16		;8 BCD digits (4 bytes)
TileMap 						equ GameRam+64 ;768  (32x24)
TileMap2 						equ GameRam+64+768  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	Call DOINIT	; Get ready
	call ScreenINIT
	
	ld a,&C9
	ld (&0038),a
	
	ifdef BuildCONSOLE
		ifdef BuildGBx
			ld	de, &8800	;Tile 128
		endif
		ifdef BuildSxx
			ld	hl, &1000	;Tile 128
			call prepareVram
		endif
		ifdef BuildMSX_MSX1
			ld	hl, 128*8	;Tile 128
			call prepareVram
		endif
		ld hl,RawBitmap
		ld bc,RawBitmap_End-RawBitmap
		call DefineTiles
		
		ifdef BuildMSX_MSX1
			
			ld	hl, 128*8+&2000	;Tile 128
			call prepareVram
			
			ld hl,RawPalettes
			ld bc,RawPalettes_End-RawPalettes
			call DefineTiles
		endif
		
		
	endif
	ld hl,GameRam
	ld de,GameRam+1
	ld bc,64
	ld (hl),0
	z_ldir
	ifndef BuildCONSOLEjoy
		call KeyboardScanner_Init
		call KeyboardScanner_AllowJoysticks
	endif
	
	;call Monitor
	;push af
	;call Monitor_PushedRegister
	
	

	ifdef BuildCPC
		ifdef CpcPlus
			call CPCPLUS_Init
		endif
	endif

	;      
	ld hl,&0FFF

	ld hl,Palette
	ld b,16
PaletteAgain:
	ld e,(hl)
	inc hl
	ld d,(hl)
	inc hl
	push hl
		z_ex_dehl
		push bc

			ld a,16
			sub b
			call SetPalette
		pop bc
	pop hl
	dec b
	jp nz,PaletteAgain
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
       	di
       	
       	z_ld_a_r				;LD a,r
       		LD a,r
       	ld (RandomSeed),a
       	
       	call ClearScreen	
       	jp PlayerDead
       	
       	ld de,TileMap
       	ifndef ScreenSmall
       		ld hl,2*32+4
       		add hl,de
       		z_ex_dehl
       	endif
       	ld a,16
       	z_ld_ixl_a
       		ld ixl,a
       	
       	
       	ifdef ScreenSmall
       		ifdef BuildTI8
       			ld hl,TitlePic
       		else
       			ld hl,TitlePic+2
       		endif
       	else
       		ld hl,TitlePic
       	endif
TitlePicNextLine:
       	ld bc,24
       	z_ldir
       		ldir
       	push hl
       		ld hl,8
       		add hl,de
       		z_ex_dehl
       		ex de,hl
       	pop hl
       	z_dec_ixl
       		dec ixl
       	jr nz,TitlePicNextLine
       	
       	
       	push bc
       		call RepaintScreen
       	pop bc
       	
       	ld h,screenwidth/2-8
       	ld l,screenheight-3
       	
       	call Locate
       	ld hl,HighScoreMessage
       	call PrintString
       	ld de,Score
       	ld b,4
       	call BCD_Show
       	
TitleTextReset:
       	ld c,0
TitleAgain:
       	push bc
       		call RepaintScreen
       	pop bc
       	ld hl,ScrollingMessage
       	ld e,0
       	ld b,c
       	inc b
       	ld a,ScreenWidth-1
       	sub c
       	jr nc,TextNotTooLong
       	ld b,ScreenWidth
       	ld a,c
       	sub ScreenWidth-1
       	ld e,a
       	ld a,0
       	
TextNotTooLong:
       	ld h,a
       	ld l,ScreenHeight-1
       	call locate
       	ld a,(hl)
       	ld hl,ScrollingMessage
       	ld d,0
       	add hl,de
       	
       	dec hl
       	ld a,(hl)
       	cp 255
       	jr z,TitleTextReset
       	inc hl
       	
TextNextChar:
       	ld a,(hl)
       	cp 255
       	inc hl
       	jr nz,TextNextCharValidChar
       	dec hl
       	ld a,32
       TextNextCharValidChar:
       	call PrintChar
       	z_djnz TextNextChar
       			djnz TextNextChar
       	
       	inc c
       	push bc
       		ld bc,&8000
       		call Pause
       		call GetRandom
       		call Player_ReadControlsDual
       		ld a,h
       		and l
       		ld h,a
       		
       	pop bc
       	or Keymap_AnyFire
       	inc a
       	jr nz,NewGame
       	
       	jp TitleAgain
       	
       	
       	
       	
NewGame:
	di
	
	z_ld_a_r
	ld (RandomSeed),a
	
	
	call ClearScreen	
	ifdef BuildZXS
		xor a
	else
		ifdef BuildSAM
			xor a
		else	
			ld a,1
		endif
	endif
	ld (ControlMode),a
		
	ld a,5
	ld (PlayerX1),a
	ld (PlayerY1),a
	ld (BulletX1),a
	ld (BulletY1),a
	ld b,a
	ld c,a
	ld a,1
	ld (PlayerDir),a
	ld (BulletDir),a
	
	ld a,2
	ld (AllowedFastGrowth),a
	
	
	call CreateInvader
	call CreateInvader
	call CreateDescender
	;call DrawPlayer
	ld a,255
	ld (GameTick),a
	
	call RepaintScreen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GameLoop:

	
	 ld hl,&0000
	 push hl
		 call Locate
		 ld de,Score
		 ld b,4
		 call BCD_Show
	 pop hl
	 ld h,ScreenWidth-2
	 call Locate
	 ld a,"L"
	 call PrintChar
	 ld a,(Lives)
	 add "0"
	 call PrintChar
	
	call GetRandom
	di
	and %11111110
	cp 192
	call z,CreateInvader
	cp 128
	call z,CreateDescender
	
	
	ld a,(FastGrowth)
	or a
	jr z,NoFastGrowth
	dec a
	ld (FastGrowth),a
	jr DoGameEvolve
NoFastGrowth:	
	ld a,(GameTick)
	inc a
	ld (GameTick),a
	and %00111111
	jp nz,DoGameMove
DoGameEvolve:
	call MouldEvolve
	call RepaintScreen
	jr ProcessPlayer	
	
DoGameMove:	
	and %00000001
	jr nz,DoGamePause
	call MouldMove
	call RepaintScreen
	jr ProcessPlayer	
DoGamePause:
	call RepaintScreen
	ld bc,10000
GamePause:
	dec bc
	ld a,b
	or c
	jp nz,GamePause

ProcessPlayer:	
	ld a,(NothingShot)
	inc a
	cp 64
	jr c,PlayerActive
	call CreateInvader				;player is slacking!
	call CreateInvader
	call CreateDescender
	
	ld a,2
	ld (AllowedFastGrowth),a
	
	xor a
	
PlayerActive:	
	ld (NothingShot),a
	
	
	ld a,(PlayerX1)
	ld b,a
	ld a,(PlayerY1)
	ld c,a
	
	
	call findtile
	
	ld a,b
	cp 0
	jr z,PlayerFarLeft
	dec hl
	ld a,(hl)
	cp 8
	jr c,PlayerStillAlive
	inc hl
PlayerFarLeft:
	inc hl
	
	ld a,b
	cp ScreenWidthG-1
	jr z,PlayerFarRight
	
	ld a,(hl)
	cp 8
	jr c,PlayerStillAlive
PlayerFarRight:
	ld de,-33
	add hl,de
	ld a,C
	cp 0
	jr z,PlayerFarTop
	ld a,(hl)
	cp 8
	jr c,PlayerStillAlive
PlayerFarTop:
	ld de,64
	add hl,de
	
	ld a,C
	cp ScreenHeightG-1
	jr z,PlayerFarBottom
	
	ld a,(hl)
	cp 8
	jr c,PlayerStillAlive
PlayerFarBottom:
	jp PlayerDead
PlayerStillAlive:	
	push bc
		call Player_ReadControlsDual
		ld a,h
		and l
		ld h,a
	pop bc
	
	ld a,(PlayerDir)
	ld l,a

	xor a
	ld (PlayingSFX),a

	ld a,(ControlMode)
	bit 0,a
	jr nz,ControlMode2Fire
F1_ControlMode1Fire:	
	bit Keymap_U,h
	jr nz,F1_PlayerNotUp
	bit Keymap_F1,h
	jr z,FireFaceUp
	ld a,c
	cp 0
	jr z,F1_PlayerNotUp
	dec c
	jr F1_PlayerNotUp
FireFaceUp:
	ld a,1
	ld l,a
F1_PlayerNotUp:
	bit Keymap_D,h
	jr nz,F1_PlayerNotDown
	bit Keymap_F1,h
	jr z,FireFaceDown
	ld a,c
	cp ScreenHeightG-1
	jr z,F1_PlayerNotDown
	inc c
	jr F1_PlayerNotDown
	FireFaceDown:
	ld a,3
	ld l,a
F1_PlayerNotDown:
	bit Keymap_L,h
	jr nz,F1_PlayerNotLeft
	bit Keymap_F1,h
	jr z,FireFaceLeft
	ld a,b
	cp 0
	jr z,F1_PlayerNotLeft
	dec b
	jr F1_PlayerNotLeft
FireFaceLeft:
	ld a,4
	ld l,a
F1_PlayerNotLeft:
	bit Keymap_R,h
	jr nz,F1_PlayerNotRight
	bit Keymap_F1,h
	jr z,FireFaceRight
	ld a,b
	cp ScreenWidthG-1
	jr z,F1_PlayerNotRight
	inc b
	jr F1_PlayerNotRight
FireFaceRight:
	ld a,2
	ld l,a
F1_PlayerNotRight:
	;bit Keymap_F1,h
	jp PlayerNotFire2		
	
	
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ControlMode2Fire:		
	
	bit Keymap_U,h
	jr nz,PlayerNotUp
	ld a,c
	cp 0
	jr z,PlayerNotUp
	dec c
PlayerNotUp:
	bit Keymap_D,h
	jr nz,PlayerNotDown
	ld a,c
	cp ScreenHeightG-1
	jr z,PlayerNotDown
	inc c
PlayerNotDown:
	bit Keymap_L,h
	jr nz,PlayerNotLeft
	ld a,b
	cp 0
	jr z,PlayerNotLeft
	dec b
PlayerNotLeft:
	bit Keymap_R,h
	jr nz,PlayerNotRight
	ld a,b
	cp ScreenWidthG-1
	jr z,PlayerNotRight
	inc b
PlayerNotRight:
	bit Keymap_F1,h
	jr nz,PlayerNotFire
	
	ld a,l
	dec a
	jr nz,PlayerDirReset
	ld a,4
PlayerDirReset:	
	ld l,a
	
	;ld a,(PlayingSFX)
	;inc a
	;ld (PlayingSFX),a
PlayerNotFire:
	bit Keymap_F2,h
	jr nz,PlayerNotFire2
	
	ld a,l
	inc a
	cp 5
	jr nz,PlayerDirReset2
	ld a,1
PlayerDirReset2:	
	ld l,a
PlayerNotFire2:
	bit Keymap_F3,h
	jr nz,PlayerNotFire3
	ld a,(ControlMode)
	cpl
	ld (ControlMode),a
PlayerNotFire3:

	ld a,(PlayerDir)
	cp L
	jr nz,PlayerMoved
	
	ld a,(PlayerX1)
	cp B
	jr nz,PlayerMoved
	
	ld a,(PlayerY1)
	cp C
	jr nz,PlayerMoved
	jr PlayerUnmoved


PlayerMoved:
	ld a,l
	ld (PlayerDir),a
	call FindTile
	ld a,(hl)
	or a
	jr nz,PlayerUnmoved
	
	push bc
		ld a,(PlayerX1)
		ld b,a
		ld a,(PlayerY1)
		ld c,a
		
		call FindTile	
		ld a,0
		ld (hl),a
		call RenderTile
	pop bc
	ld a,b
	ld (PlayerX1),a
	ld a,c
	ld (PlayerY1),a
	
	
PlayerUnmoved:
	ld a,(BulletX1)
	ld b,a
	ld a,(BulletY1)
	ld c,a	
	push bc
		call FindTile	
		ld a,0
		ld (hl),a
		call RenderTile
	pop bc
	ld a,4		;Bullet moves per tick
	z_ld_ixl_a
BulletRecalc:
	ld a,(BulletDir)
	cp 1
	jr z,BulletUp
	cp 2
	jr z,BulletRight
	cp 3
	jr z,BulletDown
	cp 4
	jr z,BulletLeft
BulletUp:
	dec c
	jr BulletCbeck
BulletDown:
	inc c
	jr BulletCbeck
BulletLeft:
	dec b
	jr BulletCbeck
BulletRight:
	inc b
	jr BulletCbeck
BulletCbeck:
	ld a,b
	cp ScreenWidthG
	jr nc,BulletRestart
	ld a,c
	cp ScreenHeightG
	jr nc,BulletRestart

	call FindTile	
	ld a,(hl)
	or a
	jp nz,BulletStrike
	
	z_dec_ixl
	jr nz,BulletRecalc
	
	jr BulletDraw
BulletRestart:
	ld a,(PlayerDir)
	ld (BulletDir),a
	ld a,(PlayerX1)
	ld b,a
	ld a,(PlayerY1)
	ld c,a
	call FindTile
BulletDraw:
	ld a,b
	ld (BulletX1),a
	ld a,c
	ld (BulletY1),a
	
	ld a,5
	ld (hl),a
	call RenderTile
	
	ld a,(PlayerX1)
	ld b,a
	ld a,(PlayerY1)
	ld c,a

	call FindTile
	ld a,(PlayerDir)
	ld (hl),a
	call RenderTile

	ld a,(PlayingSFX)
	call ChibiSound

	jp GameLoop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;	call GetRandom	;Add some randomization
;	ld a,12;
	;ld (TileMap+10),a
CreateDescender:
	call GetRandom	;Add some randomization
	and %00011111
	cp ScreenWidthG
	jr nc,CreateDescender	;Get a number 0-screenheight
	ld hl,tilemap
	add l
	ld l,a
	ld a,12
	ld (hl),a
	xor a	;Clear A
	ret
CreateInvader:
	call GetRandom	;Add some randomization
	and %00011111
	cp ScreenHeightG
	jr nc,CreateInvader	;Get a number 0-screenheight
	ld hl,tilemap
	ld b,0
	ld c,a
	or a
	rl c
	rl b
	rl c
	rl b
	rl c
	rl b
	rl c
	rl b
	rl c
	rl b
	ld a,ScreenWidthG-1
	or c
	ld c,a
	add hl,bc
	ld a,11
	ld (hl),a
	xor a
	xor a	;Clear A
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PlayerDead
	ld h,(ScreenWidth/2)-7+2
	ld l,(ScreenHeight/2)-2
	call Locate
	ld hl,GameOver
	call PrintString
	ld h,(ScreenWidth/2)-7
	ld l,(ScreenHeight/2)+2
	call Locate
	ld hl,GameOver2
	call PrintString
	di
	halt	
GameOver:  db "You're dead!",255
GameOver2: db "('cos you suck!)",255
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BulletStrike:				;Players bullet has hit something
	push bc
		push hl
			ld hl,BCD1
			cp 10
			jr z,ApplyScore
			ld hl,BCD3
			cp 9
			jr z,ApplyScore
			ld hl,BCD5
ApplyScore:
			ld de,Score
			ld b,4
			call BCD_Add
		pop hl

		ld (hl),0
		call RenderTile
		ld a,%10000111
		ld (PlayingSFX),a
		
		xor a
		ld (NothingShot),a
		ld (AllowedFastGrowth),a	;Player is busy, so stop fast growth
		
		
		call GetRandom	;Add some randomization

	pop bc
	
	jp BulletRestart
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetRandom:
	push hl
		ld hl,RandomSeed		;Update the random seed 
		z_ld_a_r
		;ld a,(GameTick)
		xor (hl)
		rlca
		ld (hl),a
	pop hl
	ret


ClearScreen:
	push bc
	push de
	push hl
		ld hl,tilemap
		ld d,h
		ld e,l
		inc de
		ld (hl),0
		ld bc,768-1
		z_ldir
		
		ld hl,tilemap2
		ld d,h
		ld e,l
		inc de
		ld (hl),255
		ld bc,768-1
		z_ldir
		
		call RepaintScreen
		
	pop hl
	pop de
	pop bc
	ret
RepaintScreen:

	ld hl,&0000
	call DoFill
	
	ld hl,&0101
	call DoFill
	
	ld hl,&0001
	call DoFill
	
	ld hl,&0100
	call DoFill
	
	ret
RepaintTile:
		call FindTile
		ld a,(hl)
		call RenderTile
	ret

DoFill:	
	ld c,l
FillCAgain:
	ld b,h
FillBAgain:
	push bc
	push hl
		call FindTile
		ld a,(hl)
		ld de,768
		add hl,de
		cp (hl)
		ld (hl),a
		;call RenderTile
		ifndef BuildTI8
			call nz,RenderTile
		else
			bit 0,b
			call z,RenderTile
TileSkip:
		endif
		
	pop hl
	pop bc
	inc b
	inc b
	ld a,b
	cp ScreenWidthG
	jr c,FillBAgain
	inc c
	inc c
	ld a,c
	cp ScreenHeightG
	jr c,FillCAgain
	ret


	CALL SHUTDOWN ; return to basic or whatever
	ret
MouldEvolve:
	
	;ld hl,TileMap
	;ld de,TileMap2
	;ld bc,768
	;push de
		;z_ldir
	;pop hl
	ld hl,TileMap2
	xor a
	ld c,a
MouldEvolve_NextY:
	xor a
	ld b,a
MouldEvolve_NextX:
	or a
	ld a,(hl)
	push hl
		push af
			ld de,768
			z_sbc_hl_de
		pop af
		
		cp 8
		jp z,MouldEvolve_Green
		cp 9
		jp z,MouldEvolve_Blue
		cp 12
		call z,DoDescender
		cp 11
		call z,DoInvader
		cp 10
		call z,DoMouldSeek
MouldEvolve_Next:
	pop hl
	inc hl
	inc b
	ld a,b
	cp 32
	jr nz,MouldEvolve_NextX
	inc c
	ld a,c
	cp 24
	jr nz,MouldEvolve_NextY
	ret
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MouldMove:
	
	;ld hl,TileMap
	;ld de,TileMap2
	;ld bc,768
	;push de
		;z_ldir
	;pop hl
	ld hl,TileMap2
	xor a
	ld c,a
MouldMove_NextY:
	xor a
	ld b,a
MouldMove_NextX:
	or a
	ld a,(hl)
	push hl
		push af
			ld de,768
			z_sbc_hl_de
		pop af
		cp 12
		call z,DoDescender
		cp 11
		call z,DoInvader
		cp 10
		call z,DoMouldSeek
MouldMove_Next:
	pop hl
	inc hl
	inc b
	ld a,b
	cp 32
	jr nz,MouldMove_NextX
	inc c
	ld a,c
	cp 24
	jr nz,MouldMove_NextY
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DoDescender:
	ld a,c
	cp ScreenHeightG-1
	jr z,ZeroCurrent
	ld d,(hl)
	push bc
	push hl	
		ld bc,32
		add hl,bc
		ld a,(hl)
		ld (hl),d
	pop hl
	pop bc
	
	ld (hl),a
	xor a
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DoInvader:
	ld a,l
	and %00011111
	jr z,ZeroCurrent
	ld d,(hl)
	dec hl
	ld a,(hl)
	ld (hl),d
	inc hl
	ld d,a
	cp 8
	jr nc,DoInvader_NoBlob

	call GetRandom
	ifdef ScreenSmall
		and %00001111
	else
		and %00011111
	endif
	jr nz,DoInvader_NoBlob
	
	ld a,(AllowedFastGrowth)				;We're making a blob, so speed up the growth for a few ticks
	ld (FastGrowth),a
	
	ld a,8
	jr DoInvader_NewBlob
	
DoInvader_NoBlob:	
	ld a,d
DoInvader_NewBlob:	
	ld (hl),a
	xor a
	ret
ZeroCurrent:
	ld (hl),0
	xor a
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
MouldEvolve_Green:
	ld a,9
	z_ld_ixl_a
	ld a,8
	z_ld_ixh_a
	jp DoMouldEvolve
	
MouldEvolve_Blue:
	ld a,10
	z_ld_ixl_a
	ld a,9
	z_ld_ixh_a
	jp DoMouldEvolve


DoMouldSeek:
	ld d,h
	ld e,l
	ld a,(PlayerX1)
	cp b
	jr z,DoMouldMove_SkipX
	jr nc,DoMouldMove_XSmaller
		dec de
		jr DoMouldMove_SkipX
DoMouldMove_XSmaller:
		inc de
DoMouldMove_SkipX:	
	ld a,(PlayerY1)
	cp c
	jr z,DoMouldMove_SkipY
	jr nc,DoMouldMove_YSmaller
		push hl
		ld hl,-32
		add hl,de
		z_ex_dehl
		pop hl
		jr DoMouldMove_SkipY
DoMouldMove_YSmaller:
		push hl
		ld hl,32
		add hl,de
		z_ex_dehl
		pop hl
DoMouldMove_SkipY:	
	;push hl
	;push de
		;ld hl,768
		;add hl,de
		;ld a,(hl)
		;or a
	;pop de
	;pop hl
	ld a,(de)
	or a
	jr z,SeekOK
	cp 8	;Bullet
	jr z,SeekOK
	ret
SeekOK:
	
	ld a,(hl)
	ld (de),a
	ld (hl),0
	ret
	
DoMouldEvolve:
		z_ld_a_ixl ;ld a,9
		ld (hl),a
		inc hl
		ld a,l
		and %00011111
		jr z,MouldEvolve_GreenSkip1b
		ld a,(hl)
		or a
		jr nz,MouldEvolve_GreenSkip1
		z_ld_a_ixh ;ld a,8
		ld (hl),a
MouldEvolve_GreenSkip1:
		dec hl
		ld a,l
		and %00011111
		jr z,MouldEvolve_GreenSkip2b
MouldEvolve_GreenSkip1b:
		dec hl
		ld a,(hl)
		or a
		jr nz,MouldEvolve_GreenSkip2
		z_ld_a_ixh ;ld a,8
		ld (hl),a
MouldEvolve_GreenSkip2:
		inc hl
MouldEvolve_GreenSkip2b:
		ld de,32
		ld a,c
		cp 24
		jr z,MouldEvolve_GreenSkip3b
		add hl,de
		ld a,(hl)
		or a
		jr nz,MouldEvolve_GreenSkip3
		z_ld_a_ixh ;ld a,8
		ld (hl),a
MouldEvolve_GreenSkip3:
		ld a,c
		or a
		jr z,MouldEvolve_GreenSkip4
		ld de,64
MouldEvolve_GreenSkip3b:
		or a
		z_sbc_hl_de
		ld a,(hl)
		or a
		jr nz,MouldEvolve_GreenSkip4
		
		z_ld_a_ixh ;ld a,8
		ld (hl),a
MouldEvolve_GreenSkip4:
	jp MouldEvolve_Next


FindTile:
	ld hl,TileMap
FindTileAlt:
		ld d,0
		ld e,c
		or a
		rl e
		rl d
		rl e
		rl d
		rl e
		rl d
		rl e
		rl d
		rl e
		rl d
		ld a,e
		add b
		ld e,a
		add hl,de
	ret		
RenderTile:
	ifdef BuildTI8
	di
		ld a,b
		and %11111110
		ld b,a
		
		call FindTile
		ld a,(hl)
		ld ixh,a
		inc hl
		ld a,(hl)
		ld ixl,a
		call ShowTile
	else
		call FindTile
		ld a,(hl)
		ScreenStartDrawing
		call ShowTile
		ScreenStopDrawing
	endif
	ret
	
	
PaletteText:
	db "-GRB",255

TestString: defs 16

Palette:
	ifdef Palette16 
		defw &0000;0  -GRB
		defw &0555;1  -GRB
		defw &0AAA;2  -GRB
		defw &0FFF;3  -GRB
		defw &0900;4  -GRB
		defw &0F27;5  -GRB
		defw &0518;6  -GRB
		defw &061F;7  -GRB
		defw &08B0;8  -GRB
		defw &0DF2;9  -GRB
		defw &038E;10  -GRB
		defw &00FF;11  -GRB
		defw &083A;12  -GRB
		defw &0F0F;13  -GRB
		defw &088F;14  -GRB
		defw &088F;15  -GRB

	endif
	ifdef Palette4
		defw &0000;0  -GRB
		defw &044F;1  -GRB
		defw &0F44;2  -GRB
		defw &0FF8;3  -GRB
		defw &0900;4  -GRB
		defw &0F27;5  -GRB
		defw &0518;6  -GRB
		defw &061F;7  -GRB
		defw &08B0;8  -GRB
		defw &0DF2;9  -GRB
		defw &0AAA;10  -GRB
		defw &0BBB;11  -GRB
		defw &0CCC;12  -GRB
		defw &0DDD;13  -GRB
		defw &0EEE;14  -GRB
		defw &0FFF;15  -GRB
	endif
		ifdef PaletteGB
		defw &0000;0  -GRB
		defw &044F;1  -GRB
		defw &0F44;2  -GRB
		defw &0FF8;3  -GRB
		
		defw &0000;4  -GRB
		defw &0800;5  -GRB
		defw &0C00;6  -GRB
		defw &0F00;7  -GRB
		
		defw &0000;8  -GRB
		defw &0208;9  -GRB
		defw &040F;10  -GRB
		defw &086F;11  -GRB
		
		defw &0000;8  -GRB
		defw &0A80;9  -GRB
		defw &0FF0;10  -GRB
		defw &0FF4;11  -GRB
	endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ifdef BuildTI8
		read "..\SrcTI\V1_SimpleTileChibi.asm"
	else
		read "..\SrcAll\V1_SimpleTile.asm"
	endif


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BitmapFont:
	ifdef BMP_UppercaseOnlyFont
		incbin "..\ResALL\Font64.FNT"			;Font bitmap, this is common to all systems
	else
		incbin "..\ResALL\Font96.FNT"			;Font bitmap, this is common to all systems
	endif
	
	
RawBitmap:
	ifdef BuildCPC
		incbin "..\ResALL\Sprites\GrimeCPC.RAW"
	endif
	ifdef BuildENT
		incbin "..\ResALL\Sprites\GrimeCPC.RAW"
	endif
	ifdef BuildZXS
		incbin "..\ResALL\Sprites\GrimeZX.Raw"
	endif
	ifdef BuildTI8
		incbin "..\ResALL\Sprites\GrimeTI.Raw"
	endif
	ifdef BuildGBx
		incbin "..\ResALL\Sprites\GrimeGB.Raw"
	endif
	ifdef BuildSxx
		incbin "..\ResALL\Sprites\GrimeSMS.Raw"
	endif
	ifdef BuildMSX
		ifdef BuildMSX_MSX1
			incbin "..\ResALL\Sprites\GrimeZX.Raw"
		else
			incbin "..\ResALL\Sprites\GrimeMSX.RAW"
		endif
	endif
	ifdef BuildSAM
		incbin "..\ResALL\Sprites\GrimeMSX.RAW"
	endif
RawBitmap_End:	
RawPalettes:
	ifdef BuildZXS
		db	64+ 7;0
		db	64+ 7;1
		db	64+ 7;2
		db	64+ 7;3
		db	64+ 7;4
		db	64+ 7;5
		db	64+ 7;6
		db	64+ 7;7
		db	64+ 4;8
		db	64+ 5;9
		db	64+ 6;10
		db	64+ 6;11
		db	64+ 6;12
		db	64+ 6;13
		db	64+ 6;14
		
	endif
	ifdef BuildMSX_MSX1
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;0
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;1
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;2
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;3
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;4
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;5
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;6
		db &F0,&F0,&F0,&F0,&F0,&F0,&F0,&F0;7
		db &C0,&20,&30,&30,&30,&30,&20,&C0;8
		db &40,&50,&70,&70,&70,&70,&50,&40;9
		db &A0,&A0,&B0,&B0,&B0,&B0,&A0,&A0;10
		db &C0,&20,&30,&30,&30,&30,&20,&C0;11
		db &C0,&20,&30,&30,&30,&30,&20,&C0;12
		db &C0,&20,&30,&30,&30,&30,&20,&C0;13
		db &C0,&20,&30,&30,&30,&30,&20,&C0;14
	endif
	ifdef BuildGBC
		db	0;0
		db	0;1
		db	0;2
		db	0;3
		db	0;4
		db	0;5
		db	0;6
		db	0;7
		db	1;8
		db	2;9
		db	3;10
		db	1;11
		db	1;12
		db	1;13
		db	1;14
	endif
RawPalettes_End:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	align32	
KeyMap equ KeyMap2+16			;wsad bnm p
KeyMap2:
	ifdef BuildCPC
	db &F7,&03,&7f,&05,&ef,&09,&df,&09,&f7,&09,&fB,&09,&fd,&09,&fe,&09 ;p2-pause,f3,f2,f1,r,l,d,u
	db &f7,&03,&bf,&04,&bf,&05,&bf,&06,&df,&07,&df,&08,&ef,&07,&f7,&07 ;p1-pause,f3,f2,f1,r,l,d,u
	endif
	ifdef BuildENT
	db &ef,&09,&bf,&08,&df,&0a,&bf,&0a,&fe,&0a,&fd,&0a,&fb,&0a,&f7,&0a
	db &ef,&09,&fe,&08,&fe,&00,&fb,&00,&f7,&01,&bf,&01,&df,&01,&bf,&02
	endif
	ifdef BuildZXS
	db &fe,&05,&fe,&07,&fe,&06,&ef,&08,&fe,&08,&fd,&08,&fb,&08,&f7,&08
	db &fe,&0f,&fb,&07,&f7,&07,&ef,&07,&fb,&01,&fe,&01,&fd,&01,&fd,&02
	endif
	ifdef BuildMSX
	db &df,&04,&fe,&08,&ef,&0c,&df,&0c,&f7,&0c,&fb,&0c,&fd,&0c,&fe,&0c
	db &df,&04,&fb,&04,&f7,&04,&7f,&02,&fd,&03,&bf,&02,&fe,&05,&ef,&05
	endif
	ifdef BuildTI8
	db &f7,&05,&fb,&05,&fd,&05,&fe,&05,&fb,&04,&fb,&02,&fd,&03,&f7,&03
	db &bf,&05,&7f,&00,&bf,&00,&df,&00,&fb,&06,&fd,&06,&fe,&06,&f7,&06
	endif
	ifdef BuildSAM
	db &FE,&05,&Fe,&07,&FE,&06,&FE,&04,&F7,&04,&EF,&04,&FB,&04,&FD,&04
	db &FE,&05,&FB,&07,&F7,&07,&EF,&07,&FB,&01,&FE,&01,&FD,&01,&FD,&02
	endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BCD1: db &01,&00,&00,&00
BCD3: db &03,&00,&00,&00
BCD5: db &05,&00,&00,&00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TitlePic:
       	db 0,0,0,0,9,0,9,9,9,0,0,0,0,0,0,9,9,9,0,0,0,0,0,0
       	db 0,0,0,0,0,9,9,0,0,9,9,0,0,0,0,9,0,9,9,0,0,0,9,0
       	db 9,9,0,8,8,0,0,8,8,0,0,8,0,0,8,0,8,0,9,0,8,8,0,0
       	db 9,0,8,0,0,0,0,8,0,8,0,8,0,8,0,8,0,8,0,8,0,0,9,0
       	db 9,0,8,0,8,8,0,8,8,0,0,8,0,8,0,0,0,8,0,8,8,0,9,0
       	db 9,0,8,0,0,8,0,8,0,8,0,8,0,8,0,9,0,8,0,8,0,0,9,0
       	db 0,9,0,8,8,0,0,8,0,8,0,8,0,8,0,9,0,8,0,0,8,8,0,0
       	db 9,0,9,0,0,9,0,0,0,0,9,0,9,0,9,0,9,0,9,9,0,0,9,0
       	db 0,9,9,9,9,0,8,8,8,0,0,8,0,9,0,8,0,9,9,9,9,9,9,9
       	db 0,9,9,9,9,9,0,0,8,0,8,0,8,0,8,0,8,0,9,9,9,9,9,0
       	db 0,0,9,9,9,9,0,8,0,9,0,8,0,0,8,0,8,0,9,9,9,9,9,0
       	db 9,9,9,9,9,0,8,0,9,0,8,0,8,0,8,0,8,0,9,9,9,0,9,9
       	db 0,9,0,0,9,0,8,8,8,0,0,8,0,9,0,8,0,9,9,9,9,0,0,0
       	db 0,0,0,0,9,9,0,0,0,0,9,0,9,9,9,0,9,9,9,0,9,9,9,0
       	db 0,0,0,9,0,9,9,9,9,0,0,0,0,9,9,9,9,9,9,0,0,0,9,0
       	db 0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,9,0,0,9,0,0,0,0
ScrollingMessage:
       	db "Hello World, How are you today?!?! I am doing well, but how are you?",255
HighScoreMessage:
       	db "HiScore:",255
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Monitor_Pause equ 1 				;*** Pause after showing debugging info



	read "..\SrcALL\Multiplatform_Monitor_RomVer.asm"
	read "..\SrcALL\Multiplatform_MonitorSimple_RomVer.asm"
	read "..\SrcALL\Multiplatform_ShowHex.asm"
	read "..\SrcALL\Multiplatform_BCD.asm"
	
	read "..\SrcALL\Multiplatform_ChibiSound.asm"


	;read "..\SrcAll\MultiPlatform_Stringreader.asm"		;*** read a line in from the keyboard
	;read "..\SrcAll\Multiplatform_StringFunctions.asm"	;*** convert Lower>upper and decode hex ascii


;read "..\SrcAll\Multiplatform_ReadJoystick.asm"



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;			End of Your program
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ifndef BuildCONSOLEjoy
UseHardwareKeyMap equ 1 ; Need the Keyboard map
	endif
	
	ifdef BuildCONSOLE
		read "..\SrcALL\V1_VdpMemory.asm"
		read "..\SrcALL\V1_HardwareTileArray.asm"
	else

		;read "..\SrcALL\Multiplatform_ScanKeys.asm"
		;read "..\SrcALL\Multiplatform_KeyboardDriver.asm"
		read "..\SrcALL\Multiplatform_BitmapFonts.asm"
		read "..\SrcALL\V1_BitmapMemory.asm"
	endif
	align16

	read "..\SrcAll\Multiplatform_ReadJoystick.asm"
	
	read "..\SrcAll\V1_Palette.asm"
	read "..\SrcALL\V2_Functions.asm"
	read "..\SrcALL\V1_Footer.asm"
	