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+$00
    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
	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:
DKCContactEffect_Frame0_Tiles:
	db $02,$00
DKCContactEffect_Frame1_Tiles:
	db $02,$00
DKCContactEffect_Frame4_Tiles:
	db $06,$04,$02,$00
DKCContactEffect_Frame8_Tiles:
	db $0C,$0A,$08,$06,$04,$02,$00
DKCContactEffect_Frame9_Tiles:
	db $0C,$0A,$08,$06,$04,$02,$00
MSExplosionRight_Frame16_Tiles:
	db $04,$08,$0A,$00,$02,$06,$0C,$0E
MSExplosionRight_Frame19_Tiles:
	db $04,$08,$0A,$00,$02,$06,$0C,$0E
MSExplosionRight_Frame22_Tiles:
	db $08,$06,$02,$0C,$00,$04,$0A
MSExplosionRight_Frame24_Tiles:
	db $02,$00,$04
XDisplacements:
DKCContactEffect_Frame0_XDisplacements:
	db $03,$FD
DKCContactEffect_Frame1_XDisplacements:
	db $01,$FF
DKCContactEffect_Frame4_XDisplacements:
	db $F9,$09,$FC,$06
DKCContactEffect_Frame8_XDisplacements:
	db $F2,$F9,$09,$10,$F7,$07,$0A
DKCContactEffect_Frame9_XDisplacements:
	db $F2,$F9,$09,$13,$F6,$06,$0C
MSExplosionRight_Frame16_XDisplacements:
	db $E8,$E8,$E8,$F8,$F8,$F8,$F8,$F9
MSExplosionRight_Frame19_XDisplacements:
	db $EB,$EB,$EB,$FA,$FA,$FB,$FB,$FF
MSExplosionRight_Frame22_XDisplacements:
	db $EB,$EC,$F7,$F7,$FB,$FC,$FC
MSExplosionRight_Frame24_XDisplacements:
	db $EE,$FB,$FE
YDisplacements:
DKCContactEffect_Frame0_YDisplacements:
	db $FD,$05
DKCContactEffect_Frame1_YDisplacements:
	db $08,$01
DKCContactEffect_Frame4_YDisplacements:
	db $07,$07,$FB,$FB
DKCContactEffect_Frame8_YDisplacements:
	db $06,$0B,$0B,$06,$F8,$F3,$FB
DKCContactEffect_Frame9_YDisplacements:
	db $06,$13,$0B,$06,$F7,$F3,$FB
MSExplosionRight_Frame16_YDisplacements:
	db $F3,$03,$12,$E2,$F2,$02,$12,$22
MSExplosionRight_Frame19_YDisplacements:
	db $F8,$07,$0E,$E4,$F4,$04,$14,$24
MSExplosionRight_Frame22_YDisplacements:
	db $08,$03,$F6,$11,$E9,$00,$10
MSExplosionRight_Frame24_YDisplacements:
	db $06,$00,$10


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