;THE ELECTROIDS
;PROGRAMMED BY JAMES HAGUE
;(C) 1984
;CREATED WITH MAC/65 BY OSS
;
;Filename- #D:ELECTRO.M65
;
	.OPT OBJ
	.OPT NO LIST
;
;HARDWARE REGISTERS
;------------------
RANDOM	= $D20A 	;Random #'s
GRACTL	= $D01D 	;Graphic control
AUDC1	= $D201 	;Audio controls
AUDC2	= $D203
AUDC3	= $D205
AUDC4	= $D207
AUDF1	= $D200 	;Audio frequency
AUDF2	= $D202
AUDF3	= $D204
AUDF4	= $D206
HPOSP0	= $D000 	;Play. 0 horiz.
HPOSP1	= $D001 	;Play. 1 horiz.
P0PF	= $D004 	;P0 to PF coll.
P0PL	= $D002 	;P0 to PL coll.
P1PF	= $D005 	;P1 to PF coll.
P1PL	= $D00D 	;P1 to PL coll.
HITCLR	= $D01E 	;Collision clear
CONSOL	= $D01F 	;Console buttons
NMIEN	= $D40E 	;Interrupt enab.
WSYNC	= $D40A 	;Wait for sync.
COLP1	= $D013 	;P1 color
;
;SHADOW REGISTERS
;----------------
SDMACTL	= $022F 	;DMA control
STICK	= $0278 	;Joystick 0
TRIG	= $0284 	;Trigger 0
COLBK	= $02C8 	;Back. color
COL0	= $02C4 	;Color regs.
COL1	= $02C5
COL2	= $02C6
COL3	= $02C7
PCOL0	= $02C0 	;Player colors
PCOL1	= $02C1
CHBAS	= $02F4 	;Character base
DLISTL	= $0230 	;DL address
VDSLST	= $0200 	;DLI address
CLOCK	= 20 	;Jiffy clock
;
;SYSTEM ROUTINES
;---------------
SETVBV	= $E45C 	;Set VBI
XITVBI	= $E462 	;Exit VBI
;
;GAME LOCATIONS (PAGE 0)
;--------------
	*= $80
SCORE	*= *+3 	;Score
SCOADD	*= *+1 	;Point values
BLEVEL	*= *+1 	;Binary level #
LEVEL	*= *+1 	;Level #
ENERGY	*= *+1 	;Energy
ENSUB	*= *+1 	;Energy subtract
JANIM	*= *+1 	;Jewel     "
JTIME	*= *+1 	;Animation timer
GAMCTL	*= *+1 	;Game control
P0YPOS	*= *+1 	;Pl0 y position
P0XPOS	*= *+1 	;Pl0 x    "
DIE	*= *+1 	;Death flag
TEMP	*= *+2 	;Work area
ENXPOS	*= *+3 	;Enemy x pos.
ENYPOS	*= *+3 	;  "   y  "
ENXADD	*= *+3 	;Enemy x add
ENYADD	*= *+3 	;  "   y  "
ENSTAT	*= *+3 	;Enemy status
ENDIE	*= *+3 	;Enemy die flags
ENDOWN	*= *+3 	;Enem Down flags
TOTAL	*= *+1 	;# of enemy
DOW	*= *+1 	;Down flag
UP	*= *+1 	;Up flag
WAIT	*= *+4 	;Delay
PLAYER	*= *+12 	;Player image
JEWELS	*= *+1 	;# of jewels
DSND	*= *+1 	;Drain sound
DSND1	*= *+1 	;More drain snd
BSND	*= *+1 	;Bounce sound
PANIM	*= *+1 	;Player pointer
PTIME	*= *+1 	;Animation timer
LO	*= *+1 	;Lo byte storage
HI	*= *+1 	;Hi byte storage
;
;RESERVED MEMORY
;---------------
CHSET	= $2000 	;Character set
PM	= $2000 	;PM graphics
PL0	= PM+1024 	;Players
PL1	= PM+1280
PL2	= PM+1536
PL3	= PM+1792
DISP	= $2200 	;Display
;
	*= $3400 	;Start
;
	JSR $E465 	;Init sounds
;
;MOVE CHARACTER SET
;------------------
NEWGAM	LDX #0
MOV	LDA $E000,X
	STA CHSET,X
	LDA $E0FF,X
	STA CHSET+$FF,X
	INX 
	BNE MOV
;
	LDA # >CHSET 	;Install set
	STA CHBAS
;
REDEF	LDA CHARS,X 	;Alter set
	STA CHSET,X
	INX 
	CPX #48
	BNE REDEF
	JMP INIT
;
CHARS	;  	Character data
	.BYTE 0,0,0,0,0,0,0,0
	.BYTE 24,36,66,129,129,66,36
	.BYTE 24,255,187,187,238,238
	.BYTE 255,0,0,124,56,124,56
	.BYTE 124,56,124,56,0,24,24
	.BYTE 24,24,60,126,255,0,0,0
	.BYTE 255,255,255,255,255
;
;DLI
;---
DLI	PHA  	;Save A register
	LDA #0
	STA WSYNC 	;Wait for sync.
	STA COLP1 	;Zero pm colors
	STA COLP1+1
	STA COLP1+2
	PLA  	;Restore A
	RTI 
;
;INITIALIZE
;----------
INIT	JSR PMCLR 	;Clear players
	LDA #0 	;Turn off
	STA GAMCTL 	;Game control
	JSR SNDOFF 	;No sound
	JSR TSCRN 	;Title screen
PRESS	LDA RANDOM 	;Flash title
	STA $D016
	LDA CONSOL 	;Start key
	CMP #6 	;pressed?
	BEQ START 	;No, wait
	LDA TRIG 	;Trigger press?
	BNE PRESS 	;No
;
START	LDA #$50 	;Initialize
	STA ENERGY 	;Game variables
	LDA #0 	;That are only
	STA LEVEL 	;Set once per
	STA BLEVEL 	;Game
	STA GAMCTL
	STA SCORE
	STA SCORE+1
	STA SCORE+2
	STA HITCLR 	;Exceptions
	STA DSND
	STA DSND1
	STA BSND
	JSR SNDOFF 	;Sound off
;
	LDY #132 	;Set player pos
	STY P0YPOS
	LDA #115
	STA P0XPOS
	STA HPOSP0
	LDX #0 	;Draw player
LOAD	LDA PL0DAT,X
	STA PL0,Y
	STA PLAYER,X
	INY 
	INX 
	CPX #12
	BNE LOAD
;
	LDX # >VBI 	;Turn on VBI
	LDY # <VBI
	LDA #7
	JSR SETVBV 	;Set VBI
;
;NEW LEVEL
;---------
NEWLEV	LDA #0 	;Initialize new
	STA SCOADD 	;Level
	STA JANIM
	STA JTIME
	STA GAMCTL
	STA DOW
	STA ENSUB
	STA DSND
	STA DSND1
	STA BSND
	STA HITCLR
	STA PTIME
	STA PANIM
	DEC P0YPOS
;
;INCREMENT LEVEL
;---------------
	SED 
	LDA LEVEL 	;Get level #
	CLC 
	ADC #1 	;Raise it
	STA LEVEL
	CLD 
	LDX BLEVEL 	;Binary level
	CPX #12 	;Too high?
	BEQ GO 	;Yes, leave it
	INX  	;Raise blevel
	STX BLEVEL 	;Level is index
	LDA ENEM,X 	;# of enemy
	STA TOTAL
	LDA DEL,X 	;Time delay
	STA WAIT
	LDA #20 	;Time delay
	STA WAIT+2
	LDA JWS,X 	;# of jewels
	STA JEWELS
	LDA PTS,X 	;Point values
	STA SCOADD
	JMP GO 	;Continue
;
;DISPLAY LIST
;------------
DL	.BYTE 112,112,112,70
	.WORD DISP
	.BYTE 6,6,6,6,6,6,6,6,6,6,6
	.BYTE 6,6,6,6,6,6,6,6,6,6
	.BYTE 6,130,65
	.WORD DL
;
GO	LDA # <DL 	;Install dlist
	STA DLISTL
	LDA # >DL
	STA DLISTL+1
;
	LDA # <DLI 	;Set up DLI
	STA VDSLST
	LDA # >DLI
	STA VDSLST+1
	LDA #192
	STA NMIEN
;
	LDA #26 	;Color 0 is
	STA COL0 	;Gold
	LDA #202 	;Color 1 is
	STA COL1 	;Green
	LDA #36 	;Color 2 is
	STA COL2 	;Red
	LDA #56
	STA COL3 	;So is color 3
;
;CLEAR SCREEN
;------------
	LDX #0
	TXA 
CLR	STA DISP,X
	STA DISP+249,X
	INX 
	CPX #250
	BNE CLR
;
;DRAW SCREEN
;-----------
	LDX #0
B	LDA BOARD,X 	;Get lo byte
	STA LO
	INX 
	LDA BOARD,X 	;Get hi byte
	STA HI
	INX 
	LDA BOARD,X 	;Get length
	STA TEMP
	INX 
	LDA BOARD,X 	;Get color
	LDY #0
C	STA (LO),Y 	;Show byte
	INY 
	CPY TEMP 	;Done?
	BNE C 	;No.
	INX 
	CPX #44 	;Board done?
	BNE B 	;No.
	JMP MORE
;
;SCREEN DATA
;-----------
BOARD	.BYTE 51,$22,6,2
	.BYTE 62,$22,3,2
	.BYTE 126,$22,5,2
	.BYTE 174,$22,4,2
	.BYTE 203,$22,5,2
	.BYTE 18,$23,3,2
	.BYTE 31,$23,4,2
	.BYTE 85,$23,5,2
	.BYTE 117,$23,5,2
	.BYTE 164,$23,20,2
	.BYTE 184,$23,20,133
;
JWLDAT	;  	Jewel data
	.BYTE 153,155,151,147,97,159
	.BYTE 101,145,161,146,160
	.BYTE 152,154,133
;
INFO	;  	Info line
	.BYTE 0,0,0,0,51,35,47,50,37
	.BYTE 26,0,0,0,0,0,0,0,37,46
	.BYTE 37,50,39,57,26,0,0,0
	.BYTE 44,37,54,37,44,26,0,0
	.BYTE 0,0,0,0,0
;
;PLACE JEWELS
;------------
MORE	LDA #0
	STA TEMP
PUT	LDX TEMP
	LDA JWLDAT,X 	;Get j pos.
	TAX 
	LDA #65 	;Jewel color
	STA $2300,X 	;Show the jewel
	INC TEMP
	LDA TEMP
	CMP JEWELS
	BNE PUT
;
;SHOW INFO LINE
;--------------
	LDX #39
SHO	LDA INFO,X 	;Get info byte
	STA DISP+460,X 	;And show it.
	DEX 
	BPL SHO 	;Finish showing.
	JSR SHOEN 	;Show energy
	JSR SHOLEV 	;Show level
	JSR SHOSCO 	;Show score
;
;P/M SET-UP
;----------
	LDA # >PM 	;Tell where p/m
	STA 54279 	;Area is.
	LDA #2 	;Turn on p/m
	STA GRACTL 	;Graphics.
	LDA #62 	;Give single
	STA SDMACTL 	;Line res.
	JSR PMCLR 	;Clear players
;
	LDA #200 	;Pl0 is
	STA PCOL0 	;Green
;
	LDX #2 	;Get random
RAN	JSR RANPOS 	;Enemy positions
	DEX 
	BPL RAN 	;Finish them
;
	LDA #0 	;Zero sounds
	STA BSND
	STA DSND
	STA DSND1
	STA HITCLR
;
	JSR DRAWEN 	;Draw enemy
	LDA #1 	;Turn on
	STA GAMCTL 	;Game control.
;
;START LEVEL
;-----------
	LDX #8 	;Show pre-level
SL	LDA INTRO,X 	;Message
	STA DISP+225,X
	DEX 
	BPL SL
	LDY #120
	JSR PAUSE 	;Pause
	LDX #8 	;Erase message
	LDA #0
SL1	STA DISP+225,X
	DEX 
	BPL SL1
;
	JSR ELEC 	;Draw electrodes
;
;MAIN LOOP
;---------
MAIN	JSR ENMOVE 	;Move enemy
	JSR DRAIN 	;Drain energy
	JSR KILLEN 	;Kill enemy
	LDA CONSOL 	;Start pressed?
	CMP #6
	BNE M 	;No
	JMP START 	;Yes, start game
M	LDA TOTAL 	;Level over?
	BNE M1 	;No
	JMP ENDLEV 	;Yes, end level
M1	LDA ENERGY 	;Any energy?
	BNE MAIN 	;Yes
	JMP GAMOVR 	;No, the end
;
PL0DAT	;  	Player 0 data
	.BYTE 124,254,146,146,218
	.BYTE 254,124,40,40,40,108
	.BYTE 108,0,124,254,146,146
	.BYTE 218,254,124,40,40,108
	.BYTE 108,0,0,124,254,146
	.BYTE 146,218,254,124,40,108
	.BYTE 108,0,124,254,146,146
	.BYTE 218,254,124,40,40,108
	.BYTE 108
;
ENEMY	;  	Enemy data
	.BYTE 0,16,56,124,84,214
	.BYTE 254,198,124,0
;
JIMAGE	;  	Jewel images
	.BYTE 24,36,66,129,129,66,36
	.BYTE 24
	.BYTE 0,24,36,66,66,36,24,0
	.BYTE 0,0,24,36,36,24,0,0
	.BYTE 0,24,36,66,66,36,24,0
INTRO	;  	Intro message
	.SBYTE +64,"GET READY"
;
;CLEAR PLAYERS
;-------------
PMCLR	LDA #0
	TAX 
CLE	STA PL0,X
	STA PL1,X
	STA PL2,X
	STA PL3,X
	INX 
	BNE CLE
	RTS 
;
;RANDOM ENEMY STATUS
;-------------------
RANPOS	LDA RANDOM 	;Get random #
	AND #127 	;Mask high bit,
	CLC  	;And add 65
	ADC #65 	;to x position
	STA ENXPOS,X 	;And store.
	STA HPOSP1,X
	LDA RANDOM 	;Now do y pos.
	AND #15 	;< 16
	STA ENYPOS,X 	;And store.
	LDA #216 	;Set color to
	STA PCOL1,X 	;Green.
	LDA #1 	;Set down-flag
	STA ENDOWN,X 	;And init. #'S
	STA ENYADD,X 	;To add.
	LDA #0
	STA ENXADD,X
	RTS 
;
;DRAW ENEMY
;----------
DRAWEN	LDY ENYPOS 	;Draw 1st.
	LDX #0
DR1	LDA ENEMY,X
	STA PL1,Y
	INY 
	INX 
	CPX #10
	BNE DR1
;
	LDY ENYPOS+1 	;Draw 2nd.
	LDX #0
DR2	LDA ENEMY,X
	STA PL2,Y
	INY 
	INX 
	CPX #10
	BNE DR2
;
	LDY ENYPOS+2 	;Draw 3rd.
	LDX #0
DR3	LDA ENEMY,X
	STA PL3,Y
	INY 
	INX 
	CPX #10
	BNE DR3
	RTS 
;
;SOUND OFF
;---------
SNDOFF	LDA #0
	STA AUDF1
	STA AUDF2
	STA AUDF3
	STA AUDF4
	STA AUDC1
	STA AUDC2
	STA AUDC3
	STA AUDC4
	RTS 
;
;VBI
;---
VBI	LDA GAMCTL 	;Do this VBI?
	BNE DOVBI 	;Yes, do the VBI
	JMP EXIT 	;No, leave now.
;
DOVBI	LDA #0 	;No attract mode
	STA 77
;
	INC JTIME 	;Time to animate
	LDX JTIME
	CPX #3
	BNE NOAN 	;No
	LDX #0
	STX JTIME
	JSR ANIM 	;Animate jewels
;
	LDX COL3 	;Flash electrode
	DEX 
	CPX #48
	BNE COLCH
	LDX #60
COLCH	STX COL3
;
NOAN	LDX #0 	;Jewels hit?
JWLHIT	LDA P1PF,X 	;Get colls
	AND #2 	;With jewels
	BEQ NOHIT 	;No coll leave
	LDA #0 	;Yes, set all
	STA HPOSP1,X 	;Enemy status
	STA ENXADD,X 	;to 0
	STA ENXPOS,X
	LDA ENSUB 	;Set energy
	SED  	;Drain
	CLC 
	ADC #5 	;To 5
	CLD 
	STA ENSUB
	LDA #138 	;Set drain
	STA AUDC1 	;Sound
	LDA #50
	STA DSND
NOHIT	INX 
	CPX #3 	;Done with 3?
	BNE JWLHIT 	;No
;
	LDX #2 	;Check for dead
CHKIL	LDA P1PL,X 	;Enemy
	AND #1
	BEQ NOTDED 	;Still alive
	LDA ENYADD,X
	CMP #$FF
	BEQ NOTDED
	LDA ENDOWN,X 	;Enemy falling?
	BNE KILLED 	;Yes, kill it
	SED  	;No,drain energy
	LDA ENSUB
	CLC 
	ADC #1
	STA ENSUB
	CLD 
	LDA #1
	STA DSND1 	;Set drain sound
	JMP NOTDED
KILLED	LDA #1 	;Kill enemy by
	STA ENDIE,X 	;Death flag=1
	LDA #16 	;Set bounce
	STA BSND 	;Sound
NOTDED	DEX 
	BPL CHKIL
;
	LDA P0PF 	;Player hit any
	AND #8 	;Obstacles?
	BEQ NOWAY 	;No
	SED  	;Yes
	LDA ENSUB
	CLC 
	ADC #1
	CLD 
	STA ENSUB
	LDA #1
	STA DSND1
NOWAY
;
;SOUND
;-----
	LDX DSND 	;Do drain sound?
	BEQ MORSND 	;No
	DEX 
	BEQ DROFF
	STX DSND
	STX AUDF1
	LDA RANDOM
	STA COL1
	JMP MORSND
DROFF	LDX #0
	STX DSND
	STX AUDF1
	STX AUDC1
	LDA #202
	STA COL1
;
MORSND	LDA DSND1 	;Do 2nd drain
	BEQ OFF 	;Sound? No
	LDA #200 	;Yes
	STA AUDC2
	LDA RANDOM
	AND #3
	CLC 
	ADC #43
	STA AUDF2
	LDA #0
	STA DSND1
	JMP MOR1
OFF	STA AUDC2
	STA AUDF2
;
MOR1	LDA BSND 	;Do bounce sound
	BEQ MOR2 	;No
	SEC 
	SBC #1
	BEQ BOFF
	STA BSND
	CLC 
	ADC #32
	STA AUDC3
	LDA RANDOM
	AND #15
	CLC 
	ADC #60
	STA AUDF3
	JMP MOR2
BOFF	STA BSND
	STA AUDF3
	STA AUDC3
;
;ERASE PLAYER
;------------
MOR2	LDY P0YPOS 	;1st: erase pl0
	LDX #11
	LDA #0
ER	STA PL0,Y 	;Put 0 in pl0
	INY 
	DEX 
	BPL ER 	;Finish erasing
;
;HORIZ. MOVEMENT
;---------------
	LDA STICK 	;Get stick
	CMP #11 	;Left?
	BEQ LEF 	;Yes, go left.
	CMP #7 	;Right?
	BEQ RIG 	;Yes, go right.
	JMP VERT 	;Go do vertical
;
LEF	LDX P0XPOS 	;Get x pos.
	DEX  	;Subtract 1
	CPX #43 	;On screen?
	BNE L 	;Yes, move.
	LDX #202 	;No, change it.
L	STX P0XPOS 	;Store new pos.
	STX HPOSP0
	JMP VERT 	;Do vertical.
;
RIG	LDX P0XPOS 	;Get x pos.
	INX  	;Add 1
	CPX #203 	;On screen?
	BNE R 	;Yes, move.
	LDX #44 	;No, change it.
R	STX P0XPOS 	;Store new pos.
	STX HPOSP0
;
;VERTICAL MOVEMENT
;-----------------
VERT	LDA DOW 	;Get downflag
	BNE CHKMOR 	;Go check more.
	LDA TRIG 	;Get trigger
	BEQ GOUP 	;Go up.
	LDA #0 	;Clear 'up' flag
	STA UP
CHKMOR	JSR HITLED 	;Check hits.
	LDA DOW 	;Get downflag
	BNE DOWN 	;If set, go down
	JMP CONT 	;Continue.
;
DOWN	INC P0YPOS 	;Move pl0 down.
	JMP CONT 	;Redraw pl0
;
GOUP	LDA #1 	;Set 'up' flag
	STA UP
	LDA P0YPOS
	CMP #30 	;Does y=30?
	BNE GUP 	;No, go up.
	LDA #1 	;Yes, set the
	STA DOW 	;Down flag.
	JMP DOWN 	;Move down
GUP	DEC P0YPOS 	;Move pl0 up.
;
CONT	JSR PLAN 	;Animate player
;
;REDRAW PLAYER
;-------------
	LDY P0YPOS 	;Redraw player
	LDX #0 	;In its new pos.
RED	LDA PLAYER,X 	;Get byte
	STA PL0,Y 	;Show it
	INY 
	INX 
	CPX #12 	;Done?
	BNE RED 	;No, go back.
	JMP OUT
;
EXIT	JSR SNDOFF
OUT	LDA #0 	;Clear all
	STA HITCLR 	;Collisions
	JMP XITVBI 	;Leave.
;
;CHECK PLAYER STATUS
;-------------------
HITLED	LDX #0
	LDA P0PF 	;Any collisions
	AND #1 	;With ledges?
	BNE YESCOL 	;Yes, check it.
LD1	LDA #1 	;Down flag=1 for
	STA DOW 	;No collision.
	RTS  	;Leave.
	; 
YESCOL	LDA P0XPOS 	;Where is pl0?
	CMP #137 	;<137?
	BCC LHALF 	;Yes, do left.
;
RHALF	LDA RSIDE,X 	;No, do right.
	CMP P0YPOS 	;Is pl0 there?
	BNE RMOR 	;No, check more.
LD0	LDA #0 	;Yes, downflag=0
	STA DOW 	;For a collsion.
	RTS  	;Return.
RMOR	INX 
	CPX #5 	;All done?
	BNE RHALF 	;No, finish it.
	JMP LD1 	;Downflag =1
;
LHALF	LDA LSIDE,X 	;Repeat for
	CMP P0YPOS 	;Left side.
	BNE LMOR
	JMP LD0 	;Downflag=0
;
LMOR	INX 
	CPX #6 	;Done?
	BNE LHALF 	;No, finish it.
	JMP LD1 	;Yes
;
RSIDE	;  	Right side data
	.BYTE 37,85,125,165,189
LSIDE	;  	Left side data
	.BYTE 45,69,101,133,157,189
;
;ENEMY HANDLER
;-------------
ENMOVE
;
	LDX #0 	;Set enemy color
CHKCOL	LDA ENDOWN,X 	;Falling?
	BNE GRN 	;Yes, green
	LDA #54 	;No, red
	STA PCOL1,X
	JMP MORCOL
GRN	LDA #216 	;Green
	STA PCOL1,X
MORCOL	INX 
	CPX #3
	BNE CHKCOL
;
	LDA WAIT+1 	;Get 1st timer
	BEQ TWO 	;If 0, move.
	DEC WAIT+1 	;Decrement time
	RTS  	;And return.
TWO	LDA WAIT 	;Reset timer
	STA WAIT+1
	LDA WAIT+3 	;Get 2nd timer
	BEQ MOVE 	;If 0, move
	DEC WAIT+3 	;Decrement time
	RTS  	;And return.
MOVE	LDA WAIT+2 	;Reset timer
	STA WAIT+3
;
	LDX #0 	;Check enemy
CHKEN	LDA ENDOWN,X 	;Falling?
	BNE FALL 	;Yes, fall
	JMP HORIZ 	;No, don't fall
FALL	JSR ENHIT 	;Update status
	LDA ENDOWN,X 	;Still falling?
	BNE NEXT 	;Yes, keep going
	LDA RANDOM 	;No, get ran #
	AND #1 	;Mask LSB
	BEQ MOVL 	;=0? Yes left
	LDA #0
	STA ENYADD,X
	LDA #1
	STA ENXADD,X
	JMP NEXT
MOVL	STA ENYADD,X
	LDA #$FF
	STA ENXADD,X
HORIZ	JSR ENHIT 	;Update status
	LDA ENDOWN,X 	;On ledge?
	BEQ NEXT 	;Yes, stay
NOT0	LDA RANDOM 	;Get random #
	AND #3 	;Only #'s 1-3
	BEQ NOT0 	;Can't be 0
	CMP #1 	;=1
	BEQ DIR1 	;Yes, dir=1
	CMP #2 	;=2
	BEQ DIR2 	;Yes, dir=2
	LDA #1 	;Dir=3
	STA ENYADD,X
	STA ENXADD,X
	JMP NEXT
DIR1	LDA #1
	STA ENYADD,X
	LDA #0
	STA ENXADD,X
	JMP NEXT
DIR2	LDA #1
	STA ENYADD,X
	LDA #$FF
	STA ENXADD,X
NEXT
	INX 
	CPX #3 	;Done?
	BNE CHKEN 	;No, go back
;
	LDX #0 	;Get new pos.
ADD	LDA ENXPOS,X 	;Get x pos.
	CLC  	;And add number
	ADC ENXADD,X 	;To position
	CMP #40 	;Too far left?
	BNE NOTL 	;No
	LDA #215 	;Yes change pos
	JMP STORE 	;And store
NOTL	CMP #216 	;Too far right?
	BNE STORE 	;No
	LDA #41 	;Yes, change
STORE	STA ENXPOS,X 	;And store it
	STA HPOSP1,X
	LDA ENYPOS,X 	;Now repeat for
	CLC  	;Y position.
	ADC ENYADD,X
	CMP #240 	;Too far down?
	BNE NOPE 	;Nope.
	JSR RANPOS 	;Yup, new pos.
	DEC TOTAL
NOPE	STA ENYPOS,X
	INX 
	CPX #3 	;All 3 done?
	BNE ADD 	;No, finsih.
	JMP DRAWEN 	;Redraw enemy
;
ENHIT	LDY #0 	;Zero index
CHK	LDA LEV,Y 	;Get a level
	CMP ENYPOS,X 	;Enemy here?
	BNE NEX 	;No, do next
	LDA ENXPOS,X 	;Yes, get x pos
	CMP LOW,Y
	BCC NEX 	;Less than?
	CMP HIG,Y
	BCS NEX 	;Greater than?
	LDA #0
	STA ENDOWN,X
	RTS  	;Return
NEX	INY 
	CPY #10
	BNE CHK
	LDA #1
	STA ENDOWN,X
	RTS  	;Return
;
LEV	.BYTE 47,71,103,135,159
	.BYTE 39,87,127,167,191
LOW	.BYTE 57,89,65,97,49,129,153
	.BYTE 153,145,41
HIG	.BYTE 89,137,113,137,97,185
	.BYTE 193,185,193,209
;
;SHOW 1 BCD
;----------
SHOBCD	STA TEMP
	LSR A
	LSR A
	LSR A
	LSR A
	ORA #16
	STA DISP+460,Y
	INY 
	LDA TEMP
	AND #15
	ORA #16
	STA DISP+460,Y
	RTS 
;
;SHOW SCORE
;----------
SHOSCO	LDX #2
	LDY #10
SHOW	LDA SCORE,X
	JSR SHOBCD
	INY 
	DEX 
	BPL SHOW
	RTS 
;
;SHOW ENERGY
;-----------
SHOEN	LDA ENERGY
	LDY #24
	JMP SHOBCD
;
;SHOW LEVEL
;----------
SHOLEV	LDA LEVEL
	LDY #33
	JMP SHOBCD
;
;DRAIN ENERGY
;------------
DRAIN	LDA ENSUB 	;Drain energy
	BNE DRA 	;No drain
	RTS 
DRA	LDA ENERGY
	SED  	;Decimal mode
	SEC  	;Subtract #
	SBC ENSUB 	;From energy
	CLD 
	STA ENERGY
	CMP #$80 	;No energy?
	BCC SOME 	;Some energy
	LDA #0 	;None
	STA ENERGY
SOME	LDA #0
	STA ENSUB
	JMP SHOEN 	;Show energy
;
;KILL ENEMY
;----------
KILLEN	LDX #2 	;Kill any dead
KILL	LDA ENDIE,X 	;Enemy
	BEQ NOKILL 	;Enemy is alive
	LDA #0 	;Enemy is dead
	STA ENDIE,X
	LDA #$FF 	;Move enemy
	STA ENYADD,X 	;Backwards
	JSR NEWSCO 	;Update score
NOKILL	DEX 
	BPL KILL
	RTS 
;
NEWSCO	LDA SCOADD
	SED 
	CLC 
	ADC SCORE
	STA SCORE
	LDA #0
	ADC SCORE+1
	STA SCORE+1
	LDA #0
	ADC SCORE+2
	STA SCORE+2
	CLD 
	JMP SHOSCO
;
;JEWEL ANIMATOR
;--------------
ANIM	LDA JANIM 	;Get timer
	CLC 
	ADC #8
	CMP #32
	BNE AN
	LDA #0
AN	STA JANIM
	TAY 
	LDX #0
AN1	LDA JIMAGE,Y
	STA CHSET+8,X
	INY 
	INX 
	CPX #8
	BNE AN1
	RTS 
;
;ANIMATE PLAYER
;--------------
PLAN	LDA DOW 	;Falling?
	BNE GONE 	;Yes, be gone
	LDA UP 	;Jumping?
	BNE GONE 	;Yes, be gone
	INC PTIME
	LDA PTIME 	;Get timer
	CMP #7 	;Is it time?
	BNE GONE 	;No, be gone
	LDA #0 	;Yes, reset
	STA PTIME
	LDA PANIM 	;Get pointer
	CLC  	;Point to next
	ADC #12 	;image
	CMP #48 	;Last one?
	BNE PA 	;Nope.
	LDA #0 	;Yup, reset
PA	STA PANIM
	TAY  	;Copy new image
	LDX #0
PA1	LDA PL0DAT,Y
	STA PLAYER,X
	INY 
	INX 
	CPX #12
	BNE PA1
GONE	RTS  	;Get outta here!
;
;TIME DELAY
;----------
PAUSE	LDA #0 	;Zero the clock
	STA CLOCK
	STY TEMP 	;Save time
P	LDA CLOCK 	;Get time
	CMP TEMP 	;Time up?
	BEQ P1
	LDA CONSOL 	;Start pressed?
	CMP #6
	BNE P
	JMP START
P1	RTS 
;
;END LEVEL
;---------
ENDLEV	JSR SNDOFF 	;No sounds
	LDA #0 	;Remove enemy
	STA HPOSP0+1 	;From screen
	STA HPOSP0+2
	STA HPOSP0+3
	LDX #13 	;Show "LEVEL
EL	LDA LCOM,X 	;COMPLETE"
	STA DISP+223,X 	;Message
	DEX 
	BPL EL
	LDY #10 	;Now do end of
	LDA #168 	;Level effects
	STA AUDC1
FLASH	LDA #105 	;1st flash
	STA AUDF1
	LDA #134
	STA COL0
	JSR PAUSE 	;And wait
	LDA #115 	;2nd flash
	STA AUDF1
	LDA #26
	STA COL0
	JSR PAUSE 	;And wait
	LDA ENERGY 	;Any energy?
	BEQ GAMOVR
	DEY 
	BNE FLASH
	JSR SNDOFF 	;No sound
;
	LDX #13 	;Print "BONUS
SHOB	LDA BMSG,X 	;ENERGY"
	STA DISP+223,X
	DEX 
	BPL SHOB
	LDX BLEVEL 	;Get bonus value
	LDA BONUS,X
	TAX 
BON	LDA ENERGY 	;Get energy
	CMP #$75 	;At maximum?
	BEQ BON1 	;Yes, no more
	SED  	;No, dec. mode
	CLC 
	ADC #1 	;Add 1 to energy
	CLD 
	STA ENERGY
	JSR SHOEN 	;Show energy
BON1	LDY #8 	;Now do fancy
	LDA #40 	;sound effects!
	STA AUDC1
BON2	STY AUDF1
	STY TEMP+1
	LDY #1
	JSR PAUSE 	;A quick pause
	LDY TEMP+1
	DEY 
	BPL BON2
	DEX 
	BNE BON
	JSR SNDOFF 	;Kill sounds
	JMP NEWLEV 	;Next level
;
;END GAME
;--------
GAMOVR	LDA #0 	;Stop player
	STA GAMCTL 	;Movement
	STA HPOSP0 	;Remove Green
	STA PCOL0 	;Thing (GT)
	JSR SNDOFF 	;No sound
	LDA #202
	STA COL1
	LDA #24 	;Reset jewels
	STA JANIM
	JSR ANIM
	LDX #0 	;Print "THE END"
OVR	LDA THEND,X
	STA DISP+226,X
	LDY #25
	JSR PAUSE 	;A short pa)!!	;!!!!!!!! INVERSE CHARS SPECIAL CHARS !!!!!!!!
	.WORD )!!0!  !v!@!"!=!@!!!=!@!!!hPqNK!NK!NK!  !~!!!nK!nK!nK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! INVERSE CHARS SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
ERROR -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
