; ********************************************************************************************************
; * moveentity
; ********************************************************************************************************
FUNCTION	MOVEENTITY	SPRITE,TABLESZ,TABLE,MAP
REM	; moveentity( sprite,tablesize,table,map) table,map=word
REM	; returns to table
DEF	SPRITE	BYTE
DEF	TABLESZ	BYTE
DEF	TABLE	WORD
DEF	MAP	WORD
ANGLE	FCB	0
XCNT	FCB	0
YCNT	FCB	0
XPOS	FCB	0
YPOS	FCB	0
SPANX	FCB	0
SPANY	FCB	0
TABLESZ	FCB	0
SPRITE	FCB	0
MAP	FCB	0,0
TABLE	FCB	0,0
; 0=reserved 1xcnt,2ycnt,3angle,4posx,5posy,6scrx,7scry other 8frame 9&10addr 11onscreen 12&13 span 14&15 keys
MOVEENTITY	LDU	TABLE
	LDA	SPRITE
	LDB	TABLESZ
	MUL
	LEAU	D,U	;ready on table
	INC	1,U	;XCNT
	INC	2,U	;YCNT
; check span angles 16 of em
	LDA	3,U	;ANGLE
	CMPA	#128	;******************************128
	BNE	NOTRA1
	LDA	#2
	STA	13,U	;SPANY
	CLR	1,U	;XCNT	;no x
	JSR	CHECKAY2	;y-
	RTS
NOTRA1	CMPA	#0	;******************************0
	BNE	NOTRA2
	LDA	#2
	STA	13,U	;SPANY
	CLR	1,U	;XCNT	;no x
	JSR	CHECKAY1	;y+
	RTS
NOTRA2	CMPA	#64	;******************************64
	BNE	NOTRA3
	LDA	#2
	STA	12,U	;SPANX
	CLR	2,U	;YCNT	;no y
	JSR	CHECKAX1	;x+
	RTS
NOTRA3	CMPA	#192	;******************************192
	BNE	NOTRA33
	LDA	#2
	STA	12,U	;SPANX
	CLR	2,U	;YCNT	;no y
	JSR	CHECKAX2	;x-
	RTS
; even angles
NOTRA33	CMPA	#160	;******************************160
	BNE	NOTRA4
	LDA	#3
	STA	12,U	;SPANX
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY2	;y-
	RTS
NOTRA4	CMPA	#96	;******************************96
	BNE	NOTRA5
	LDA	#3
	STA	12,U	;SPANX
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY2	;y-
	RTS
NOTRA5	CMPA	#32	;******************************32
	BNE	NOTRA6
	LDA	#3
	STA	12,U	;SPANX
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY1	;y+
	RTS
NOTRA6	CMPA	#224	;******************************224
	BNE	NOTRA7
	LDA	#3
	STA	12,U	;SPANX
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY1	;y+
	RTS
; more complex
NOTRA7	CMPA	#144	;******************************144
	BNE	NOTRA8
	LDA	#4
	STA	12,U	;SPANX
	LDA	#2
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY2	;y-
	RTS
NOTRA8	CMPA	#112	;******************************112
	BNE	NOTRA9
	LDA	#4
	STA	12,U	;SPANX
	LDA	#2
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY2	;y-
	RTS
NOTRA9	CMPA	#240	;******************************240
	BNE	NOTRA10
	LDA	#4
	STA	12,U	;SPANX
	LDA	#2
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY1	;y+
	RTS
NOTRA10	CMPA	#16	;******************************16
	BNE	NOTRA11
	LDA	#4
	STA	12,U	;SPANX
	LDA	#2
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY1	;y+
	RTS
NOTRA11	CMPA	#176	;******************************176
	BNE	NOTRA12
	LDA	#2
	STA	12,U	;SPANX
	LDA	#4
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY2	;y-
	RTS
NOTRA12	CMPA	#208	;******************************208
	BNE	NOTRA13
	LDA	#2
	STA	12,U	;SPANX
	LDA	#4
	STA	13,U	;SPANY
	JSR	CHECKAX2	;x-
	JSR	CHECKAY1	;y+
	RTS
NOTRA13	CMPA	#80	;******************************80
	BNE	NOTRA14
	LDA	#2
	STA	12,U	;SPANX
	LDA	#4
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY2	;y-
	RTS
NOTRA14	CMPA	#48	;******************************48
	BNE	NOTRA15
	LDA	#2
	STA	12,U	;SPANX
	LDA	#4
	STA	13,U	;SPANY
	JSR	CHECKAX1	;x+
	JSR	CHECKAY1	;y+
NOTRA15	RTS	
CHECKAY1	LDA	2,U	;YCNT
	CMPA	13,U	;SPANY
	BLO	FAILAY1
	CLR	2,U	;YCNT
	LDA	5,U	;YPOS
	ADDA	#1
	STA	TPOSY
	JSR	TESTPOSY
	BNE	FAILAY1
	LDA	TPOSY
	STA	5,U	;YPOS
FAILAY1	RTS
CHECKAY2	LDA	2,U	;YCNT
	CMPA	13,U	;SPANY
	BLO	FAILAY2
	CLR	2,U	;YCNT
	LDA	5,U	;YPOS
	SUBA	#1
	STA	TPOSY
	JSR	TESTPOSY
	BNE	FAILAY2
	LDA	TPOSY
	STA	5,U	;YPOS
FAILAY2	RTS
CHECKAX1	LDA	1,U	;XCNT
	CMPA	12,U	;SPANX
	BLO	FAILAX1
	CLR	1,U	;XCNT
	LDA	4,U	;XPOS
	ADDA	#1
	STA	TPOSX
	JSR	TESTPOSX
	BNE	FAILAX1
	LDA	TPOSX
	STA	4,U	;XPOS
FAILAX1	RTS
CHECKAX2	LDA	1,U	;XCNT
	CMPA	12,U	;SPANX
	BLO	FAILAX2
	CLR	1,U	;XCNT
	LDA	4,U	;XPOS
	SUBA	#1
	STA	TPOSX
	JSR	TESTPOSX
	BNE	FAILAX2
	LDA	TPOSX
	STA	4,U	;XPOS
FAILAX2	RTS
; *****************************************
; * testposx
; *****************************************
TESTPOSX	LDA	TPOSX
	CMPA	#1
	BLO	FAILX1
	CMPA	#31
	BHI	FAILX1	;map restrictions
	LDX	MAP
	LDA	5,U	;YPOS
	STA	PLOTY
	LDA	TPOSX
	STA	PLOTX
	LDA	PLOTY
	LDB	#32
	MUL
	LEAX	D,X
	LDB	PLOTX
	ABX
	LDA	,X
	BEQ	PASSX1
	CMPA	#5
	BEQ	PASSX1
	CMPA	#6
	BEQ	PASSX1
	CMPA	#8
	BEQ	PASSX1
	CMPA	#10
	BEQ	PASSX1
	LDB	14,U	;key1
	BEQ	NOTKEYX1
	CMPA	#4
	BEQ	PASSX1	;cause i have the key
NOTKEYX1	LDB	15,U
	BEQ	NOTKEYX2
	CMPA	#7
	BEQ	PASSX1
	BRA	FAILX1	;end of the line
NOTKEYX2	BRA	FAILX1
PASSX1	LDA	#0
	RTS
FAILX1	LDA	#1
	RTS
;
; *****************************************
; * testposy
; *****************************************
TESTPOSY	LDA	TPOSY
	CMPA	#1
	BLO	FAILY1
	CMPA	#31
	BHI	FAILY1	;map restrictions
	LDX	MAP
	LDA	TPOSY
	STA	PLOTY
	LDA	4,U	;XPOS
	STA	PLOTX
	LDA	PLOTY
	LDB	#32
	MUL
	LEAX	D,X
	LDB	PLOTX
	ABX
	LDA	,X
	BEQ	PASSY1
	CMPA	#5
	BEQ	PASSY1
	CMPA	#6
	BEQ	PASSY1
	CMPA	#8
	BEQ	PASSY1
	CMPA	#10
	BEQ	PASSY1
	LDB	14,U	;key1
	BEQ	NOTKEYY1
	CMPA	#4
	BEQ	PASSY1	;cause i have the key
NOTKEYY1	LDB	15,U
	BEQ	NOTKEYY2
	CMPA	#7
	BEQ	PASSY1
	BRA	FAILY1	;end of the line
NOTKEYY2	BRA	FAILY1
PASSY1	LDA	#0
	RTS
FAILY1	LDA	#1
	RTS
ENDFUNCTION
;
