if read1($00FFD5) == $23
    fullsa1rom
else
    lorom
endif

!rom = $800000
if read1($00FFD5) == $23
    !rom = $000000
endif

!Routines #= (read3($00821F))|!rom

!PoseWasLoaded = read3(!Routines+$00)
!TakeDynamicRequest = read3(!Routines+$03)
!Draw = read3(!Routines+$06)
!IsValid = read3(!Routines+$09)
!RemapOamTile = read3(!Routines+$0C)
!XIsValid = read3(!Routines+$0F)
!YIsValid = read3(!Routines+$12)
!Draw_Return = read3(!Routines+$15)
!ResourceTable = read3(!Routines+$18)
!GraphicRoutinesTable = read3(!Routines+$1B)

org !GraphicRoutinesTable+$1E
    dl snestopc(GraphicRoutine)+$1F8

reset freespaceuse
freecode cleaned

!XOffSet = $00
!YOffSet = $02
!Property = $04
!Tile = $05
!PoseID = $06
!PoseOffset = $08
!MaxTilePriority = $0A

!GraphicRoutine = $45
!Iterator = !GraphicRoutine+3
!maxtile_pointer = !Iterator+2

!maxtile_pointer_max        = $6180       ; 16 bytes
!maxtile_pointer_high       = $6190       ; 16 bytes
!maxtile_pointer_normal     = $61A0       ; 16 bytes
!maxtile_pointer_low        = $61B0       ; 16 bytes

!TileX = $0B
!TileY = $0D
!TileSize = $0F

GraphicRoutine:
    PHB
    PHK
    PLB

    
    

    .NoFlip
..Loop
	LDX !maxtile_pointer+0
	CPX !maxtile_pointer+4
	BEQ ..End

    LDA XDisplacements,y
	STA !TileX
	LDA YDisplacements,y
	STA !TileY
	LDA #$02
	STA !TileSize
	JSL !IsValid
	BCC ..next
	LDA !TileX
	STA $400000,x
	LDA !TileY
	STA $400001,x

    LDA Tiles,y
	JSL !RemapOamTile
	ORA 	!Property
EOR Properties,y
	STA $400003,x
	LDA $8A
	STA $400002,x

    DEX #4
    STX !maxtile_pointer+0

    LDX !maxtile_pointer+2
    LDA !TileSize
    STA $400000,x
    DEX
    STX !maxtile_pointer+2

..next
    DEY

    DEC !Iterator
    BPL ..Loop

..End
    PLB
    JML !Draw_Return

Tiles:
MSExplosion_Frame3_Tiles:
	db $04,$00,$06,$02
MSExplosion_Frame4_Tiles:
	db $04,$00,$06,$02
MSExplosion_Frame5_Tiles:
	db $04,$02,$06,$00
MSExplosion_Frame6_Tiles:
	db $04,$02,$06,$00
MSExplosion_Frame7_Tiles:
	db $04,$02,$06,$00
MSExplosion_Frame13_Tiles:
	db $04,$08,$00,$02,$06
MSExplosion_Frame14_Tiles:
	db $02,$06,$00,$04,$08
MSExplosion_Frame16_Tiles:
	db $00,$04,$02,$06
MSExplosion_Frame36_Tiles:
	db $06,$0C,$04,$0A,$20,$00,$08,$0E,$02
MSExplosion_Frame37_Tiles:
	db $06,$0C,$04,$0A,$20,$00,$08,$0E,$02
MSExplosion_Frame38_Tiles:
	db $08,$0E,$04,$0A,$20,$00,$06,$0C,$02
MSExplosion_Frame52_Tiles:
	db $04,$00,$06,$02
MSExplosion_Frame54_Tiles:
	db $00,$02
MSExplosion_Frame55_Tiles:
	db $00,$02
MSExplosion_Frame56_Tiles:
	db $00,$02,$06,$04,$08,$00,$02,$04,$06,$08,$00,$02,$04,$06,$08,$00
	db $02,$04,$06,$08,$04,$04,$04,$04,$04,$04,$04,$04
MSExplosion_Frame57_Tiles:
	db $00,$02,$06,$04,$08,$00,$02,$04,$06,$08,$00,$02,$04,$06,$08,$00
	db $02,$04,$06,$08,$04,$04,$04,$04,$04,$04,$04,$04
MSExplosion_Frame76_Tiles:
	db $00,$04,$08,$0C,$20,$02,$06,$0A,$0E
MSExplosion_Frame77_Tiles:
	db $00,$04,$08,$0C,$20,$02,$06,$0A,$0E
MSExplosion_Frame83_Tiles:
	db $00,$02
Properties:
MSExplosion_Frame3_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame4_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame5_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame6_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame7_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame13_Properties:
	db $00,$00,$00,$00,$00
MSExplosion_Frame14_Properties:
	db $00,$00,$00,$00,$00
MSExplosion_Frame16_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame36_Properties:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame37_Properties:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame38_Properties:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame52_Properties:
	db $00,$00,$00,$00
MSExplosion_Frame54_Properties:
	db $00,$00
MSExplosion_Frame55_Properties:
	db $00,$00
MSExplosion_Frame56_Properties:
	db $00,$00,$00,$00,$00,$80,$80,$80,$80,$80,$C0,$C0,$C0,$C0,$C0,$40
	db $40,$40,$40,$40,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame57_Properties:
	db $00,$00,$00,$00,$00,$80,$80,$80,$80,$80,$C0,$C0,$C0,$C0,$C0,$40
	db $40,$40,$40,$40,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame76_Properties:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame77_Properties:
	db $00,$00,$00,$00,$00,$00,$00,$00,$00
MSExplosion_Frame83_Properties:
	db $00,$00
XDisplacements:
MSExplosion_Frame3_XDisplacements:
	db $FA,$FC,$0A,$0C
MSExplosion_Frame4_XDisplacements:
	db $F8,$FB,$08,$0B
MSExplosion_Frame5_XDisplacements:
	db $F9,$FC,$09,$0C
MSExplosion_Frame6_XDisplacements:
	db $F9,$FB,$09,$0B
MSExplosion_Frame7_XDisplacements:
	db $F9,$FA,$09,$0A
MSExplosion_Frame13_XDisplacements:
	db $F9,$F9,$02,$09,$09
MSExplosion_Frame14_XDisplacements:
	db $F9,$F9,$02,$09,$09
MSExplosion_Frame16_XDisplacements:
	db $FA,$FA,$0A,$0A
MSExplosion_Frame36_XDisplacements:
	db $F0,$F0,$F5,$00,$00,$05,$10,$10,$15
MSExplosion_Frame37_XDisplacements:
	db $F2,$F2,$F5,$02,$02,$05,$12,$12,$15
MSExplosion_Frame38_XDisplacements:
	db $F2,$F2,$F5,$02,$02,$05,$12,$12,$15
MSExplosion_Frame52_XDisplacements:
	db $F5,$FC,$05,$0C
MSExplosion_Frame54_XDisplacements:
	db $F9,$09
MSExplosion_Frame55_XDisplacements:
	db $F8,$08
MSExplosion_Frame56_XDisplacements:
	db $E0,$E4,$E9,$F4,$F9,$E0,$E4,$F4,$E9,$F9,$20,$1C,$0C,$17,$07,$20
	db $1C,$0C,$17,$07,$F0,$14,$FC,$08,$00,$00,$0C,$0C
MSExplosion_Frame57_XDisplacements:
	db $E0,$E4,$E9,$F4,$F9,$E0,$E4,$F4,$E9,$F9,$20,$1C,$0C,$17,$07,$20
	db $1C,$0C,$17,$07,$F0,$14,$FC,$08,$00,$00,$04,$04
MSExplosion_Frame76_XDisplacements:
	db $08,$08,$08,$08,$08,$18,$18,$18,$18
MSExplosion_Frame77_XDisplacements:
	db $08,$08,$08,$08,$08,$18,$18,$18,$18
MSExplosion_Frame83_XDisplacements:
	db $08,$08
YDisplacements:
MSExplosion_Frame3_YDisplacements:
	db $07,$FC,$07,$FD
MSExplosion_Frame4_YDisplacements:
	db $08,$FC,$08,$FC
MSExplosion_Frame5_YDisplacements:
	db $08,$FC,$08,$FB
MSExplosion_Frame6_YDisplacements:
	db $08,$FB,$08,$F9
MSExplosion_Frame7_YDisplacements:
	db $09,$FB,$09,$F9
MSExplosion_Frame13_YDisplacements:
	db $FC,$0C,$F8,$FB,$0B
MSExplosion_Frame14_YDisplacements:
	db $FB,$0B,$F8,$FB,$0B
MSExplosion_Frame16_YDisplacements:
	db $F9,$09,$F9,$09
MSExplosion_Frame36_YDisplacements:
	db $FE,$0E,$F5,$FF,$0F,$F3,$FE,$0E,$F3
MSExplosion_Frame37_YDisplacements:
	db $FE,$0E,$F5,$FF,$0F,$F3,$FE,$0E,$F4
MSExplosion_Frame38_YDisplacements:
	db $FF,$0F,$F6,$00,$10,$F2,$FD,$0D,$F4
MSExplosion_Frame52_YDisplacements:
	db $03,$F4,$03,$F4
MSExplosion_Frame54_YDisplacements:
	db $FF,$07
MSExplosion_Frame55_YDisplacements:
	db $02,$06
MSExplosion_Frame56_YDisplacements:
	db $09,$11,$1E,$11,$21,$F9,$F1,$F1,$E4,$E1,$F9,$F1,$F1,$E4,$E1,$09
	db $11,$11,$1E,$21,$01,$01,$01,$01,$11,$F1,$F1,$11
MSExplosion_Frame57_YDisplacements:
	db $09,$11,$1E,$11,$21,$F9,$F1,$F1,$E4,$E1,$F9,$F1,$F1,$E4,$E1,$09
	db $11,$11,$1E,$21,$01,$01,$01,$01,$F1,$11,$F1,$11
MSExplosion_Frame76_YDisplacements:
	db $E0,$F0,$00,$10,$20,$E3,$F3,$03,$13
MSExplosion_Frame77_YDisplacements:
	db $E1,$EC,$FC,$0C,$1C,$E4,$F4,$04,$14
MSExplosion_Frame83_YDisplacements:
	db $06,$16


End:
print dec(snestopc(GraphicRoutine)-8)
print freespaceuse