;marine pop torpedo projectile
;by smkdan
;GFX from Mattrizzle
;thanks mior for locating the block bounce routine

;$019138: grab map16# of block sprite is touching
;$028663: shatter block
;$00BEB0: generate tile

incsrc subroutinedefs_trasm.asm

;enemy hit sound
ENEMYHITSOUND = $25
;map16 block (0-0x200)
TURNBLOCKMAP16 = $132

DIR = $03
PROPRAM = $04
TEMP = $09

dcb "INIT"
	LDA #TILEMAP		;tilemap into stunned goomba tile
	STA $15EA,x
	RTL

dcb "MAIN"
	PHB
	PHK
	PLB
	JSR Run
	PLB
	RTL

Return_I
	RTS

Run
	JSL SubOffscreenX0
	JSL GetDrawInfo
	JSR GFX			;draw sprite

	LDA $14C8,x
	CMP #$08          	 
	BNE Return_I           
	LDA $9D			;locked sprites?
	BNE Return_I
	LDA $15D0,x		;yoshi eating?
	BNE Return_I

	JSL $818022		;update xpos no gravity
	STZ $AA,x		;no yspd

;block interaction
	LDA $15A0,x		;test something
	ORA $186C,x
	BNE CheckWall

	JSL $819138		;map 16 'query'
	LDA $1862		;high byte
	XBA
	LDA $1860		;lowbyte
	REP #$20
	CMP.w #TURNBLOCKMAP16	;11E = turn block
	SEP #$20
	BNE CheckWall

	PHB			;preserve current bank
	LDA #$02		;push 02
	PHA
	PLB			;bank = 02
	LDA #$00		;default shatter
	JSL $828663		;shatter block
	PLB			;restore bank

	LDA #$02		;block to generate
	STA $9C
	JSL $80BEB0		;generate

	JSR SUB_SMOKE		;smoke
	STZ $14C8,x		;kill sprite, already hit something

	RTS 			;return here

CheckWall			;not turn block
	LDA $1588,x		;test wall
	AND #$03
	BEQ CheckSprite

;touchingwall	
	PHK			;push current program bank
	PEA ReturnAdd		;push return address -1
	PHB			;preserve bank
	LDA #$01		;bank 01
	PHA		
	PLB			;new data bank
	PEA $801F		;push return address containing PLB + RTL
	JML $01999E		;long jump to local routine

ReturnAdd
	dcb $FF			;because of broken math system

	JSR SUB_SMOKE		;smoke
	STZ $14C8,x		;erase sprite

CheckSprite
	STX TEMP		;store self to TEMP
	LDY #$0B		;12 entries
CheckLoop
	CPY TEMP		;skip self
	BEQ NextSprite

	LDA $14C8,y		;check status
	BEQ NextSprite
	
	LDA $1686,Y		;skip sprites that don't interact with others
	AND #$08
	BNE NextSprite

	JSL $83B69F		;clipA
	PHX
	TYX
	JSL $83B6E5		;clipB
	PLX
	JSL $83B72B		;check contact between A and B
	BCC NextSprite		;no contact = don't set

	LDA #$00
	STA $14C8,y		;erase sprite
	STZ $14C8,x		;also erase self

	JSR SUB_SMOKE		;and smoke

	LDA #ENEMYHITSOUND	;and sound
	STA $1DFC

	RTS

NextSprite
	DEY			;next sprite
	BPL CheckLoop
	RTS
       
			
;=====

TILEMAP = $E8
GFX
	LDA $157C,x	;direction...
	STA DIR

	LDA $15F6,x	;properties...
	STA PROPRAM

	LDA $00		;xpos
	STA $0300,y
	LDA $01
	STA $0301,y	;ypos
	LDA #TILEMAP
	STA $0302,y	;chr
	LDA $15F6,x
	ORA $64
	STA $0303,y	;prop

	LDY #$02	;16x16
	LDA #$00	;1 tile
	JSL $81B7B3	;reserve

	RTS
	
;=================
;BORROWED ROUTINES
;=================

;A smoke routine
;===============

SUB_SMOKE           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 $D8,x               ; \ smoke y position = generator y position
                    STA $17C4,y             ; /
                    LDA #$18                ; \ set time to show smoke
                    STA $17CC,y             ; /
                    LDA $E4,x               ; \ load generator x position and store it for later
                    STA $17C8,y             ; /
                    LDX $15E9
             
                    RTS
