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+$27
    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

    
    

    LDA !Property
	BIT #$C0
	BNE .Flip
.NoFlip
..Loop
	LDX !maxtile_pointer+0
	CPX !maxtile_pointer+4
	BEQ ..End

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

    LDA Tiles,y
	JSL !RemapOamTile
	ORA !Property
	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
.Flip
	BIT #$80
	BNE .WithFlipY
.FlipX
..Loop
	LDX !maxtile_pointer+0
	CPX !maxtile_pointer+4
	BEQ ..End

    LDA XDisplacementsFlip,y
	STA !TileX
	LDA YDisplacements,y
	STA !TileY
	LDA Sizes,y
	STA !TileSize
	JSL !IsValid
	BCC ..next
	LDA !TileX
	STA $400000,x
	LDA !TileY
	STA $400001,x

    LDA Tiles,y
	JSL !RemapOamTile
	ORA !Property
	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
.WithFlipY
	BIT #$40
	BEQ .FlipY
..Loop
	LDX !maxtile_pointer+0
	CPX !maxtile_pointer+4
	BEQ ..End

    LDA XDisplacementsFlip,y
	STA !TileX
	LDA YDisplacementsFlip,y
	STA !TileY
	LDA Sizes,y
	STA !TileSize
	JSL !IsValid
	BCC ..next
	LDA !TileX
	STA $400000,x
	LDA !TileY
	STA $400001,x

    LDA Tiles,y
	JSL !RemapOamTile
	ORA !Property
	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
.FlipY
..Loop
	LDX !maxtile_pointer+0
	CPX !maxtile_pointer+4
	BEQ ..End

    LDA XDisplacements,y
	STA !TileX
	LDA YDisplacementsFlip,y
	STA !TileY
	LDA Sizes,y
	STA !TileSize
	JSL !IsValid
	BCC ..next
	LDA !TileX
	STA $400000,x
	LDA !TileY
	STA $400001,x

    LDA Tiles,y
	JSL !RemapOamTile
	ORA !Property
	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:
MSGrenade_Frame1_Tiles:
	db $00,$02
MSGrenade_Frame2_Tiles:
	db $02,$00
MSGrenade_Frame3_Tiles:
	db $02,$00
MSGrenade_Frame4_Tiles:
	db $02,$00
MSGrenade_Frame5_Tiles:
	db $02,$00
MSGrenade_Frame6_Tiles:
	db $02,$00
MSGrenade_Frame7_Tiles:
	db $02,$00
MSGrenade_Frame8_Tiles:
	db $02,$00
MSGrenade_Frame9_Tiles:
	db $02,$00
MSGrenade_Frame10_Tiles:
	db $02,$00
MSGrenade_Frame11_Tiles:
	db $02,$00
MSGrenade_Frame12_Tiles:
	db $02,$00
MSGrenade_Frame13_Tiles:
	db $02,$00
MSGrenade_Frame14_Tiles:
	db $02,$00
MSGrenade_Frame15_Tiles:
	db $02,$00
MSMachineGunBullet_Frame6_Tiles:
	db $02,$00
XDisplacements:
MSGrenade_Frame1_XDisplacements:
	db $02,$05
MSGrenade_Frame2_XDisplacements:
	db $02,$03
MSGrenade_Frame3_XDisplacements:
	db $01,$04
MSGrenade_Frame4_XDisplacements:
	db $00,$04
MSGrenade_Frame5_XDisplacements:
	db $FF,$05
MSGrenade_Frame6_XDisplacements:
	db $FE,$06
MSGrenade_Frame7_XDisplacements:
	db $FE,$06
MSGrenade_Frame8_XDisplacements:
	db $FE,$06
MSGrenade_Frame9_XDisplacements:
	db $FD,$05
MSGrenade_Frame10_XDisplacements:
	db $FE,$05
MSGrenade_Frame11_XDisplacements:
	db $FF,$04
MSGrenade_Frame12_XDisplacements:
	db $FF,$04
MSGrenade_Frame13_XDisplacements:
	db $00,$03
MSGrenade_Frame14_XDisplacements:
	db $01,$02
MSGrenade_Frame15_XDisplacements:
	db $02,$02
MSMachineGunBullet_Frame6_XDisplacements:
	db $FA,$02
XDisplacementsFlip:
MSGrenade_Frame1_XDisplacementsFlip:
	db $FE,$03
MSGrenade_Frame2_XDisplacementsFlip:
	db $06,$FD
MSGrenade_Frame3_XDisplacementsFlip:
	db $07,$FC
MSGrenade_Frame4_XDisplacementsFlip:
	db $08,$FC
MSGrenade_Frame5_XDisplacementsFlip:
	db $09,$FB
MSGrenade_Frame6_XDisplacementsFlip:
	db $0A,$FA
MSGrenade_Frame7_XDisplacementsFlip:
	db $0A,$FA
MSGrenade_Frame8_XDisplacementsFlip:
	db $0A,$FA
MSGrenade_Frame9_XDisplacementsFlip:
	db $0B,$FB
MSGrenade_Frame10_XDisplacementsFlip:
	db $0A,$FB
MSGrenade_Frame11_XDisplacementsFlip:
	db $09,$FC
MSGrenade_Frame12_XDisplacementsFlip:
	db $09,$FC
MSGrenade_Frame13_XDisplacementsFlip:
	db $08,$FD
MSGrenade_Frame14_XDisplacementsFlip:
	db $07,$FE
MSGrenade_Frame15_XDisplacementsFlip:
	db $06,$FE
MSMachineGunBullet_Frame6_XDisplacementsFlip:
	db $0E,$FE
YDisplacements:
MSGrenade_Frame1_YDisplacements:
	db $03,$00
MSGrenade_Frame2_YDisplacements:
	db $0B,$00
MSGrenade_Frame3_YDisplacements:
	db $0A,$01
MSGrenade_Frame4_YDisplacements:
	db $09,$01
MSGrenade_Frame5_YDisplacements:
	db $08,$02
MSGrenade_Frame6_YDisplacements:
	db $08,$03
MSGrenade_Frame7_YDisplacements:
	db $08,$04
MSGrenade_Frame8_YDisplacements:
	db $07,$04
MSGrenade_Frame9_YDisplacements:
	db $05,$04
MSGrenade_Frame10_YDisplacements:
	db $03,$04
MSGrenade_Frame11_YDisplacements:
	db $01,$05
MSGrenade_Frame12_YDisplacements:
	db $00,$05
MSGrenade_Frame13_YDisplacements:
	db $FF,$06
MSGrenade_Frame14_YDisplacements:
	db $FF,$07
MSGrenade_Frame15_YDisplacements:
	db $FF,$07
MSMachineGunBullet_Frame6_YDisplacements:
	db $09,$FF
YDisplacementsFlip:
MSGrenade_Frame1_YDisplacementsFlip:
	db $FF,$0A
MSGrenade_Frame2_YDisplacementsFlip:
	db $FF,$02
MSGrenade_Frame3_YDisplacementsFlip:
	db $00,$01
MSGrenade_Frame4_YDisplacementsFlip:
	db $01,$01
MSGrenade_Frame5_YDisplacementsFlip:
	db $02,$00
MSGrenade_Frame6_YDisplacementsFlip:
	db $02,$FF
MSGrenade_Frame7_YDisplacementsFlip:
	db $02,$FE
MSGrenade_Frame8_YDisplacementsFlip:
	db $03,$FE
MSGrenade_Frame9_YDisplacementsFlip:
	db $05,$FE
MSGrenade_Frame10_YDisplacementsFlip:
	db $07,$FE
MSGrenade_Frame11_YDisplacementsFlip:
	db $09,$FD
MSGrenade_Frame12_YDisplacementsFlip:
	db $0A,$FD
MSGrenade_Frame13_YDisplacementsFlip:
	db $0B,$FC
MSGrenade_Frame14_YDisplacementsFlip:
	db $0B,$FB
MSGrenade_Frame15_YDisplacementsFlip:
	db $0B,$FB
MSMachineGunBullet_Frame6_YDisplacementsFlip:
	db $FF,$01
Sizes:
MSGrenade_Frame1_Sizes:
	db $02,$00
MSGrenade_Frame2_Sizes:
	db $00,$02
MSGrenade_Frame3_Sizes:
	db $00,$02
MSGrenade_Frame4_Sizes:
	db $00,$02
MSGrenade_Frame5_Sizes:
	db $00,$02
MSGrenade_Frame6_Sizes:
	db $00,$02
MSGrenade_Frame7_Sizes:
	db $00,$02
MSGrenade_Frame8_Sizes:
	db $00,$02
MSGrenade_Frame9_Sizes:
	db $00,$02
MSGrenade_Frame10_Sizes:
	db $00,$02
MSGrenade_Frame11_Sizes:
	db $00,$02
MSGrenade_Frame12_Sizes:
	db $00,$02
MSGrenade_Frame13_Sizes:
	db $00,$02
MSGrenade_Frame14_Sizes:
	db $00,$02
MSGrenade_Frame15_Sizes:
	db $00,$02
MSMachineGunBullet_Frame6_Sizes:
	db $00,$02


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