
incsrc subroutinedefs_trasm.asm

dcb "INIT"
STZ $1510,x
LDA #$40
STA $163E,x
STZ $157C,x
LDA $7FAB10,x
AND #$04
BEQ LEFT_FACING
INC $157C,x
LEFT_FACING
RTL

dcb "MAIN"
PHB
PHK
PLB
JSR SPRITE_SWITCH
PLB
RTL

SPRITE_SWITCH
		LDA $1510,x
		BEQ JSHOOT
		BRA JWAVE
JSHOOT
		JSR SHOOT
		RTS
JWAVE
		JSR WAVE
		RTS
RETURNSHOOT
		RTS
SHOOT
       
	   LDA $14C8,x ;\
       CMP #$08   ; | If the sprite is dead or not it it's normal state, return.
       BNE RETURNSHOOT ;/
       LDA $9D    ;\
       BNE RETURNSHOOT ;/ If sprites are locked, return.
       
	   STZ $AA,x
	   STZ $B6,x
	   JSL $81802A
	   JSL SubOffscreenX0
	   JSR ASK_SHOOT
	   RTS
ASK_SHOOT
		STZ $15A0,x
		LDA $E4,x               ; \
        CMP $1A                 ;  | set horizontal offscreen if necessary
        LDA $14E0,x             ;  |
        SBC $1B                 ;  |
        BEQ ON_SCR        ;  |
        INC $15A0,x             ; /
ON_SCR
        LDA $15A0,x
		BNE NO_SHOOT
		LDA $163E,x
		BEQ YES_SHOOT
NO_SHOOT
		RTS
XOFF_GEN: dcb $10,$F0
XOFF_FIX: dcb $00,$FF
SLOTS_FULL
		RTS
YES_SHOOT
		LDA #$80
		STA $163E,x
		LDA $157C,x
		TAY
		LDA XOFF_GEN,y
		STA $00
		LDA XOFF_FIX,y
		STA $01
		JSL $82A9DE
		BMI SLOTS_FULL ; If sprite slots are full, don't generate.
		;After this, the sprite to generate is in the Y register.
		
		PHX ; NOTE: We need to use the X register for the generated sprite.
		TYX ; So we preserve the current sprites data.
		LDA #$98
		STA $7FAB9E,x ; The sprite number is stored to the custom sprite RAM Address. STA $7FAB9E,y doesn't exist, that's why we transfered X to Y.
		TXY ; Transfer sprite to spawn back to Y.
		PLX ; Restore X.

		LDA #$08
		STA $14C8,y ; Run sprite's INIT code first.z
		LDA $00E4,x
		CLC
		ADC $00
		STA $00E4,y ; X position.
		LDA $14E0,x
		ADC $01
		STA $14E0,y ; X position high.
		LDA $D8,x
		SEC
		SBC #$0F
		STA $00D8,y ; Y position.
		LDA $14D4,x
		ADC #$FF
		STA $14D4,y ; Y position high.
		STZ $01
		LDA $7FAB10,x
		AND #$04
		BNE LEFT_NOADDGEN
		LDA #$04
		STA $01
LEFT_NOADDGEN
		PHX ; Push current sprite.
		TYX ; Get sprite to generate into X.
		JSL $87F7D2 ; Reset sprite tables, to generate this one.
		JSL $8187A7 ; Reset custom sprite tables, too.
		LDA #$08
		ORA $01
		STA $7FAB10,x ; Setting this to 08 means we are generating a custom sprite.
		LDA #$01
		STA $1510,x
		PLX ; Restore previous sprite.
		LDA $157C,x
		STA $157C,y
		LDA #$02
		STA $163E,y
		LDA #$02
		STA $1DFC
		RTS 
WALL
		;LDA $1504,x
		;BNE INVTIME
		STZ $14C8,x
		JSR DRAW_SMOKE
		RTS
RETURNWAVE
		RTS
		
XSPEED: dcb $18,$E8

WAVE
		JSR GRAFICS
		LDA $14C8,x ;\
        CMP #$08   ; | If the sprite is dead or not it it's normal state, return.
        BNE RETURNWAVE ;/
        LDA $9D    ;\
        BNE RETURNWAVE ;/ If sprites are locked, return.
        JSL SubOffscreenX0
        LDA $1588,x
        AND #$03
        BNE WALL
		STZ $1504,x
INVTIME
		LDY $157C,x ; Load the direction in Y.
        LDA XSPEED,y ; Speed based on direction.
        STA $B6,x ; Store to sprite's X speed.
        LDA #$01
        STZ $AA,x
        JSL $81802A ;Save new Sprite positions
        JSL $818032 ;Sprite to Sprite interaction
		JSL $81A7DC ;Sprite to Mario interaction
        BCS CONTACT
        RTS
PUSH_SP: dcb $38,$E4,$1C,$C8

CONTACT    
		LDA $163E,x
		BNE INVTIME2
		LDA #$10
		STA $163E,x
		LDA $0E		; is mario above the sprite?
		CMP #$E8	; cause if he is.
		BMI NOHORZ_PUSH	; sprite should not push him horz.
		CMP #$0C
		BPL NOHORZ_PUSH
		JSR SubHorizPos2
		LDA $157C,x
		BEQ NOADDFLIP
		INY
		INY
NOADDFLIP
		LDA PUSH_SP,y
        STA $7B ;Mario X speed
NOHORZ_PUSH
		LDA $0E		; is mario above the sprite?
		CMP #$E8	; cause if he is.
		BMI DOWN_CONTACT	; sprite should not push upwards (down contact)
		CMP #$00
		BPL UP_CONTACT
CONTINUE_CONTACT
        LDA #$01
        STA $1DF9 ;Sound
CONTINUE_CONTACT2		
		;JSR DRAW_SMOKE
INVTIME2
        RTS
DOWN_CONTACT
		LDA $77
		AND #$04
		BNE CONTINUE_CONTACT2
		LDA #$B0
		STA $7D
		LDA $75
		BNE WATER_CONTACT
		LDA $15
		AND #$80
		BNE EXTRA_BOOST
		BRA CONTINUE_CONTACT
WATER_CONTACT
		LDA #$D0
		STA $7D
		BRA CONTINUE_CONTACT
EXTRA_BOOST
		LDA #$A3
		STA $7D
		BRA CONTINUE_CONTACT
UP_CONTACT
		LDA #$09
		STA $7D
		BRA CONTINUE_CONTACT

;;;;;;;;;;;;;;;;;;;;;;;;;;
; GRAFICS   routine      ;
;;;;;;;;;;;;;;;;;;;;;;;;;;

TILES:  dcb $86,$88,$A6,$A8
		dcb $80,$C0,$A4,$AA
XOFF: 	dcb $00,$10,$00,$10
		dcb $10,$00,$10,$00
YOFF:   dcb $00,$00,$10,$10
		dcb $00,$00,$10,$10
FLIP:   dcb $40,$00

GRAFICS JSL GetDrawInfo

        STZ $07
		LDA $157C,x
		BNE NOFLIP
		LDA #$40
		STA $07
NOFLIP
        PHX
		LDX #$03
LOOP
		PHX
		LDA $07
		BEQ NOADD_1
		INX
		INX
		INX
		INX
NOADD_1
		LDA $00
		CLC
		ADC XOFF,x
        STA $0300,y
        LDA $01
		CLC
		ADC YOFF,x
        STA $0301,y
		PLX
        PHX
		LDA $14
        LSR
        LSR
		LSR
        AND #$01
        BEQ NOADD
		INX
		INX
		INX
		INX
NOADD
        LDA TILES,x
        STA $0302,y
        PLX
		
        LDA #%0000111
        ORA $07
        ORA $64
        STA $0303,y
		
		INY
        INY
        INY
        INY
		
		DEX
		BPL LOOP
		PLX
        
        LDY #$02 
        LDA #$03 
        JSL $81B7B3
        RTS


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; variant of SubHorizPos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SubHorizPos2:
	LDA $157C,x
	BEQ RightSub
	LDY #$00
	LDA $E4,x
	CLC
	ADC #$10
	STA $00
	LDA $14E0,x
	ADC #$00
	STA $01
	LDA $94
	SEC
	SBC $00
	STA $0F
	STA $0F
	LDA $95
	SBC $01
	BPL Spr2L16
	INY
Spr2L16:
	RTS
RightSub:
;	LDA #$14
;	STA $1DFC
	LDY #$00
	LDA $E4,x
	STA $00
	LDA $14E0,x
	STA $01
	REP #$20
	LDA $94
	SEC
	SBC $00
	BPL Spr2RL16
	INY
Spr2RL16:
	SEP #$20
	RTS

;==================================================================
;Draw Smoke Routine
;==================================================================
NODRAW_SMOKE
					RTS
DRAW_SMOKE:         LDA $15A0,x
					ORA $186C,x
					BNE NODRAW_SMOKE  ;No draw if offscreen horz/vert.
					LDY #$03                ; \ find a free slot to display effect
FINDFREE:           LDA $17C0,y             ;  |
                    BEQ FOUNDONE            ;  |
                    DEY                     ;  |
                    BPL FINDFREE            ;  |
                    RTS                     ; / return if no slots open

FOUNDONE:           LDA #$01                ; \ set effect graphic to smoke graphic
                    STA $17C0,y             ; /
                    LDA #$1B                ; \ set time to show smoke
                    STA $17CC,y             ; /
                    LDA $D8,x               ; \ smoke y position = generator y position
                    STA $17C4,y             ; /
                    LDA $E4,x               ; \ load generator x position and store it for later
                    STA $17C8,y             ; /
                    RTS
