lorom

;The famous havoc beam! by Black_Falcon
;this makes ice beam projectiles ricochet from solid surfaces and even slopes (to a degree)!
;please give credit because it was a hassle to figure this stuff out!


;94A34B  JSR $90AE06 diagonal
;94A2C3  JSR $90AE06 horizontal


!solid = $AEF3	;code to run
!wave = $B0E4

!vert = $1F60	 ;vertical flag
!check = $1F62,x ;counter
!yes = #$1111	;unique value for the flag
!no = #$0000

!sp = $0500	;positive speed
!sn = $FB00	;negative speed (opposite direction)
!s0 = $0000	;no speed

!howmanybounces = #$000A  ;as the name says - use hex values here

;=================--------------=====================================

org $90B96E

DW !solid	;handles trails, movement and deletion
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

;=================--------------=====================================

org $90BA3E

DW !solid	;handles trails, movement and deletion
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

DW !solid
DW !wave

;=================--------------=====================================

org $94A1A5

	DW VERT

org $94A185
	DW HOR

org $94DC04
VERT:
	LDA !yes
	STA !vert  ;free RAM
	SEC
	RTS

HOR:
	LDA !no
	STA !vert
	SEC
	RTS

org $A0A875	;disable freezing
	AND #$0000	

;=================--------------=====================================

org $A0958E	;same
	LDA $09A6
	AND #$0000
	BEQ later
	LDA #$0001
	BRA $0F
later:

org $90ADC6
	STZ $1F62,x	;STZ $0C04,x part of the deleting routine
			;it's not neccessary to delete the direction here
			;because it get's overwritten by newly fired beams
			;so I thought this is a good spot for zeroing
			;the counter instead

;=================--------------=====================================


org $94A2C3
	JSL $93F625	;vertical/diagonal
	PLB
	NOP
	RTL

;=================--------------=====================================

org $94A34B
	JSL $93F625	;horizontal
	PLB
	NOP
	RTL

;=================--------------=====================================

org $93F625
	PHA
	LDA $0C18,x	;beam check
	BIT #$0002
	BEQ NOICE

	LDA !check
	CMP !howmanybounces
	BCS NOICE
	INC !check
	TXY		;y = beam index
	LDA $0C04,y	;direction
	AND #$000F
	ASL a
	STA $12		;index

	LDA $0DC4	;block index
	ASL a
	TAX
	LDA $7F0002,x	;Block type (check if slope)
	AND #$1000
	CMP #$1000
	BEQ slope

NOSLOPE:
	LDX $12
	LDA !vert
	CMP #$1111
	BNE NOFLOOR
	JSR FLOOR	;vertical
	BRA QUIT

NOFLOOR:
	JSR WALL	;horizontal
	BRA QUIT

slope:
	TXA
	LSR a
	TAX
	LDA $7F6402,x
	BIT #$0010
	BEQ NOSLOPE	;skipping the first 16 slopes because there are
	AND #$00C0
	CMP #$00C0
	BEQ horverslope

	CMP #$0080
	BEQ verslope

	CMP #$0040
	BEQ horslope
	LDX $12
	JSR SLOPE	;mostly squares and sloppy slopes
	BRA QUIT

horverslope:
	LDX $12
	JSR HORVERSLOPE
	BRA QUIT

verslope:
	LDX $12	
	JSR VERSLOPE
	BRA QUIT

horslope:
	LDX $12
	JSR HORSLOPE
	BRA QUIT

NOICE:
	STZ !check
	PLA
	JSR $90AE06
	SEC	;set carry flag
	RTL
QUIT:
	LDA #$000C
	JSR $8090C1	;just sound
	PLA
	CLC
	RTL


;=================---------- !!!DONE!! ---------=====================================

;   /|
;  / |
; /  |
;/___|


SLOPE:
	LDA $0C04,y	;direction
	AND #$FF00
	ORA SNDIRECTIONS,x
	STA $0C04,y	;new direction
	LDA SNSPEEDX,x	;new speed
	STA $0BDC,y
	LDA SNSPEEDY,x
	STA $0BF0,y
	RTS

SNDIRECTIONS:

	DW $0010	; U	0	1
	DW $0010	; UR	1	2
	DW $0000	; R	2	3
	DW $0008	; DR	3	2
	DW $0007	; D	4	1
	
	DW $0007	; D	5	4
	DW $0007	; DL	6	5
	DW $0010	; L	7	3
	DW $0010	; UL	8	5
	DW $0005	; U	9	4

SNSPEEDX:

	DW !s0
	DW !s0
	DW !s0
	DW !sn
	DW !sn
	
	DW !sn
	DW !sn
	DW !sp
	DW !s0
	DW !s0
 

SNSPEEDY:

	DW !s0
	DW !s0
	DW !sn
	DW !sn
	DW !s0

	DW !s0
	DW !s0
	DW !s0
	DW !s0
	DW !s0


;=================--!! DONE !!---=====================================


;|\
;| \
;|  \
;|___\

HORSLOPE:
	LDA $0C04,y	;direction
	AND #$FF00
	ORA SHDIRECTIONS,x
	STA $0C04,y	;new direction
	LDA SHSPEEDX,x	;new speed
	STA $0BDC,y
	LDA SHSPEEDY,x
	STA $0BF0,y
	RTS

SHDIRECTIONS:

	DW $0000	; U	0	1
	DW $0010	; UR	1	2
	DW $0010	; R	2	3
	DW $0002	; DR	3	2
	DW $0002	; D	4	1
	
	DW $0002	; D	5	4
	DW $0001	; DL	6	5
	DW $0000	; L	7	3
	DW $0000	; UL	8	5
	DW $0004	; U	9	4

SHSPEEDX:

	DW !s0
	DW !s0
	DW !sp
	DW !s0
	DW !sp
 
	DW !sp
	DW !sp
	DW !s0
	DW !s0
	DW !s0
 



SHSPEEDY:

	DW !s0
	DW !s0
	DW !s0
	DW !s0
	DW !sp
 
	DW !sp
	DW !sn
	DW !sn
	DW !sn
	DW !sp
 


;=================--------------=====================================

;____
;\   |
; \  |
;  \ |
;   \|

VERSLOPE:
	LDA $0C04,y	;direction
	AND #$FF00
	ORA SVDIRECTIONS,x
	STA $0C04,y	;new direction
	LDA SVSPEEDX,x	;new speed
	STA $0BDC,y
	LDA SVSPEEDY,x
	STA $0BF0,y
	RTS


SVDIRECTIONS:

	DW $0007	; U	0	1
	DW $0006	; UR	1	2
	DW $0004	; R	2	3
	DW $0010	; DR	3	2
	DW $0010	; D	4	1
	
	DW $0010	; D	5	4
	DW $0010	; DL	6	5
	DW $0010	; L	7	3
	DW $0007	; UL	8	5
	DW $0007	; U	9	4

SVSPEEDX:

	DW !sn
	DW !sn
	DW !s0
	DW !s0
	DW !s0
 
	DW !s0
	DW !s0
	DW !s0
	DW !sn
	DW !sn
 



SVSPEEDY:

	DW !s0
	DW !sp
	DW !sp
	DW !s0
	DW !s0
 
	DW !s0
	DW !s0
	DW !s0
	DW !s0
	DW !s0
 


;=================--------------=====================================


;_____
;|   /
;|  /o<---
;| / |
;|/  V
;    o

HORVERSLOPE:
	LDA $0C04,y	;direction
	AND #$FF00
	ORA SHVDIRECTIONS,x
	STA $0C04,y	;new direction
	LDA SHVSPEEDX,x	;new speed
	STA $0BDC,y
	LDA SHVSPEEDY,x
	STA $0BF0,y
	RTS

SHVDIRECTIONS:

	DW $0002	; U	0	1
	DW $0002	; UR	1	2
	DW $0010	; R	2	3
	DW $0010	; DR	3	2
	DW $0010	; D	4	1
	
	DW $0010	; D	5	4
	DW $0010	; DL	6	5
	DW $0005	; L	7	3
	DW $0003	; UL	8	5
	DW $0002	; U	9	4

SHVSPEEDX:
	DW !sp
	DW !sp
	DW !s0
	DW !s0
	DW !s0
 
	DW !s0
	DW !s0
	DW !s0
	DW !sp
	DW !sp
 


SHVSPEEDY:

	DW !s0
	DW !s0
	DW !s0
	DW !s0
	DW !s0
 
	DW !s0
	DW !s0
	DW !sp
	DW !sp
	DW !s0
 


;=================--------------=====================================


;\    | o  _o
;_\|  V ^  /|
;  o  o | /
;____________
;|__________|

FLOOR:			;when hitting the floor/ceiling
	LDA $0C04,y	;direction
	AND #$FF00
	ORA VDIRECTIONS,x
	STA $0C04,y	;new direction
	LDA VSPEEDX,x	;new speed
	STA $0BDC,y
	LDA VSPEEDY,x
	STA $0BF0,y
	RTS

VDIRECTIONS:

	DW $0004	; U	0	1
	DW $0003	; UR	1 	2
	DW $0007	; R	2	3
	DW $0001	; DR  	3	2
	DW $0000	; D	4	1

	DW $0009	; D	5	4
	DW $0008	; DL	6	5
	DW $0002	; L	7	3
	DW $0006	; UL	8	5
	DW $0005	; U	9	4

VSPEEDX:

	DW !s0
	DW !sp
	DW !s0
	DW !sp
	DW !s0
 
	DW !s0
	DW !sn
	DW !s0
	DW !sn
	DW !s0
 


VSPEEDY:

	DW !sp
	DW !sp
	DW !s0
	DW !sn
	DW !sn
 
	DW !sn
	DW !sn
	DW !s0
	DW !sp
	DW !sp
 


;=================--------------=====================================

; _
;| |
;| |
;| |
;| |
;| |
;|_|

WALL:		;when hitting a wall!
	LDA $0C04,y
	AND #$FF00
	ADC HDIRECTIONS,x
	STA $0C04,y
	
	LDA HSPEEDX,x
	STA $0BDC,y
	
	LDA HSPEEDY,x
	STA $0BF0,y
	RTS

HDIRECTIONS:

	DW $0004	; U	0	1
	DW $0008	; UR	1	2
	DW $0007	; R	2	3
	DW $0006	; DR	3	2
	DW $0000	; D	4	1
	
	DW $0009	; D	5	4
	DW $0003	; DL	6	5
	DW $0002	; L	7	3
	DW $0001	; UL	8	5
	DW $0005	; U	9	4

HSPEEDX:

	DW !s0	; Ur	0	1
	DW !sn	; UR	1	2
	DW !sn	; R	2	3
	DW !sn	; DR	3	2
	DW !s0	; Dr	4	1
	
	DW !s0	; Dl	5
	DW !sp	; DL	6
	DW !sp	; L	7
	DW !sp	; UL	8
	DW !s0	; Ul	9

HSPEEDY:

	DW !sp	; Ur	1
	DW !sn	; UR	2
	DW !s0	; R	3
	DW !sp	; DR	4
	DW !sn	; Dr	5
	
	DW !sn	; Dl	6
	DW !sp	; DL	7
	DW !s0	; L	8
	DW !sn	; UL	9
	DW !sp	; Ul	A

;=================--------------=====================================
