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+$24
    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 #$02
	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 #$02
	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 #$02
	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 #$02
	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_Frame0_Tiles:
	db $02,$00
MSMachineGunBullet_Frame4_Tiles:
	db $02,$00
MSMachineGunBullet_Frame5_Tiles:
	db $02,$00
MSMachineGunBullet_Frame7_Tiles:
	db $02,$00
MSMachineGunBullet_Frame8_Tiles:
	db $02,$00
MSMachineGunBullet_Frame9_Tiles:
	db $02,$00
MSMachineGunBullet_Frame10_Tiles:
	db $02,$00
MSMachineGunBullet_Frame11_Tiles:
	db $02,$00
XDisplacements:
MSGrenade_Frame0_XDisplacements:
	db $03,$04
MSMachineGunBullet_Frame4_XDisplacements:
	db $F8,$08
MSMachineGunBullet_Frame5_XDisplacements:
	db $F9,$09
MSMachineGunBullet_Frame7_XDisplacements:
	db $FB,$09
MSMachineGunBullet_Frame8_XDisplacements:
	db $FC,$08
MSMachineGunBullet_Frame9_XDisplacements:
	db $FE,$07
MSMachineGunBullet_Frame10_XDisplacements:
	db $00,$07
MSMachineGunBullet_Frame11_XDisplacements:
	db $01,$06
XDisplacementsFlip:
MSGrenade_Frame0_XDisplacementsFlip:
	db $FD,$FC
MSMachineGunBullet_Frame4_XDisplacementsFlip:
	db $08,$F8
MSMachineGunBullet_Frame5_XDisplacementsFlip:
	db $07,$F7
MSMachineGunBullet_Frame7_XDisplacementsFlip:
	db $05,$F7
MSMachineGunBullet_Frame8_XDisplacementsFlip:
	db $04,$F8
MSMachineGunBullet_Frame9_XDisplacementsFlip:
	db $02,$F9
MSMachineGunBullet_Frame10_XDisplacementsFlip:
	db $00,$F9
MSMachineGunBullet_Frame11_XDisplacementsFlip:
	db $FF,$FA
YDisplacements:
MSGrenade_Frame0_YDisplacements:
	db $03,$01
MSMachineGunBullet_Frame4_YDisplacements:
	db $05,$01
MSMachineGunBullet_Frame5_YDisplacements:
	db $04,$00
MSMachineGunBullet_Frame7_YDisplacements:
	db $03,$FE
MSMachineGunBullet_Frame8_YDisplacements:
	db $04,$FD
MSMachineGunBullet_Frame9_YDisplacements:
	db $05,$FC
MSMachineGunBullet_Frame10_YDisplacements:
	db $06,$FC
MSMachineGunBullet_Frame11_YDisplacements:
	db $07,$FB
YDisplacementsFlip:
MSGrenade_Frame0_YDisplacementsFlip:
	db $FF,$01
MSMachineGunBullet_Frame4_YDisplacementsFlip:
	db $FB,$FF
MSMachineGunBullet_Frame5_YDisplacementsFlip:
	db $FC,$00
MSMachineGunBullet_Frame7_YDisplacementsFlip:
	db $FD,$02
MSMachineGunBullet_Frame8_YDisplacementsFlip:
	db $FC,$03
MSMachineGunBullet_Frame9_YDisplacementsFlip:
	db $FB,$04
MSMachineGunBullet_Frame10_YDisplacementsFlip:
	db $FA,$04
MSMachineGunBullet_Frame11_YDisplacementsFlip:
	db $F9,$05


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