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+$03
    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 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:
DKCContactEffect_Frame2_Tiles:
	db $02,$05,$04,$00
DKCContactEffect_Frame3_Tiles:
	db $04,$02,$00,$06
DKCContactEffect_Frame5_Tiles:
	db $09,$06,$08,$04,$02,$00
DKCContactEffect_Frame6_Tiles:
	db $0B,$08,$06,$04,$02,$00,$0A
DKCContactEffect_Frame7_Tiles:
	db $08,$0B,$06,$04,$02,$0A,$00
DKCContactEffect_Frame10_Tiles:
	db $0C,$08,$0B,$06,$0A,$04,$02,$00
DKCContactEffect_Frame11_Tiles:
	db $08,$0C,$06,$0B,$04,$0A,$02,$00
MSExplosionRight_Frame0_Tiles:
	db $23,$22,$0C,$02,$06,$08,$0E,$00,$04,$0A,$20
MSExplosionRight_Frame1_Tiles:
	db $23,$04,$08,$0C,$02,$06,$0A,$0E,$00,$20,$21,$30,$31,$22,$32
MSExplosionRight_Frame2_Tiles:
	db $22,$06,$0C,$02,$0E,$04,$08,$26,$00,$0A,$20,$24,$25,$34,$35
MSExplosionRight_Frame3_Tiles:
	db $08,$0C,$20,$02,$06,$24,$0E,$22,$0A,$00,$04,$25,$34,$35
MSExplosionRight_Frame4_Tiles:
	db $24,$25,$0C,$20,$06,$08,$02,$34,$22,$04,$0A,$0E,$00,$35
MSExplosionRight_Frame5_Tiles:
	db $08,$0C,$20,$02,$06,$24,$0E,$22,$0A,$00,$04,$25,$34,$35
MSExplosionRight_Frame6_Tiles:
	db $0C,$22,$04,$27,$00,$08,$0E,$24,$26,$02,$06,$0A,$20,$36,$37
MSExplosionRight_Frame7_Tiles:
	db $0C,$22,$06,$2A,$02,$0A,$20,$26,$04,$00,$08,$0E,$24,$38,$39,$28
	db $29
MSExplosionRight_Frame8_Tiles:
	db $0A,$36,$06,$02,$37,$0C,$20,$08,$04,$00,$0E,$22,$26,$27,$34,$35
	db $24,$25
MSExplosionRight_Frame9_Tiles:
	db $08,$0E,$22,$37,$02,$27,$04,$0A,$20,$24,$26,$00,$06,$0C,$36,$28
	db $29,$38
MSExplosionRight_Frame10_Tiles:
	db $08,$0C,$20,$02,$22,$06,$0A,$0E,$00,$04,$24,$26,$27,$36,$37,$28
MSExplosionRight_Frame11_Tiles:
	db $08,$0C,$36,$20,$02,$06,$26,$0E,$22,$00,$04,$0A,$24,$27,$37,$28
MSExplosionRight_Frame12_Tiles:
	db $06,$0A,$35,$0E,$02,$22,$08,$0C,$26,$00,$04,$20,$36,$24,$25,$34
	db $27
MSExplosionRight_Frame13_Tiles:
	db $06,$0C,$20,$02,$08,$0E,$22,$24,$00,$04,$0A,$25,$34,$35,$26
MSExplosionRight_Frame14_Tiles:
	db $04,$08,$0C,$0E,$02,$06,$0A,$00,$20,$22,$23,$32,$33,$24,$25
MSExplosionRight_Frame15_Tiles:
	db $04,$08,$33,$0E,$0A,$02,$32,$00,$0C,$20,$06,$22,$23
MSExplosionRight_Frame17_Tiles:
	db $08,$20,$0C,$04,$00,$06,$0A,$0E,$02
MSExplosionRight_Frame18_Tiles:
	db $0F,$04,$08,$0E,$00,$02,$06,$0A,$0C
MSExplosionRight_Frame20_Tiles:
	db $04,$08,$0A,$00,$02,$06,$0C,$0E,$0F
MSExplosionRight_Frame21_Tiles:
	db $04,$06,$0A,$0C,$0E,$02,$08,$00
MSExplosionRight_Frame23_Tiles:
	db $09,$08,$18,$06,$04,$00,$02
MSExplosionRight_Frame25_Tiles:
	db $03,$02,$00
XDisplacements:
DKCContactEffect_Frame2_XDisplacements:
	db $00,$10,$00,$01
DKCContactEffect_Frame3_XDisplacements:
	db $FD,$0D,$FE,$0C
DKCContactEffect_Frame5_XDisplacements:
	db $F6,$FC,$06,$0C,$FA,$07
DKCContactEffect_Frame6_XDisplacements:
	db $F5,$FB,$FD,$0D,$F8,$07,$08
DKCContactEffect_Frame7_XDisplacements:
	db $F5,$FB,$05,$0F,$F9,$09,$09
DKCContactEffect_Frame10_XDisplacements:
	db $F1,$F6,$06,$0A,$1A,$F2,$FF,$0F
DKCContactEffect_Frame11_XDisplacements:
	db $F6,$06,$0D,$21,$F1,$FE,$06,$11
MSExplosionRight_Frame0_XDisplacements:
	db $E4,$E6,$E6,$E8,$E8,$E9,$EE,$F8,$F8,$F9,$FE
MSExplosionRight_Frame1_XDisplacements:
	db $E2,$E4,$E4,$E4,$ED,$F4,$F4,$F4,$FD,$04,$04,$04,$04,$04,$04
MSExplosionRight_Frame2_XDisplacements:
	db $E0,$E2,$E2,$E5,$F0,$F2,$F2,$F2,$F5,$FA,$FA,$02,$02,$02,$02
MSExplosionRight_Frame3_XDisplacements:
	db $E2,$E2,$E2,$EB,$EB,$F2,$F2,$F2,$FA,$FB,$FB,$02,$02,$02
MSExplosionRight_Frame4_XDisplacements:
	db $E1,$E2,$E2,$E2,$E9,$EA,$EB,$F2,$F2,$F9,$FA,$FA,$FB,$02
MSExplosionRight_Frame5_XDisplacements:
	db $E0,$E0,$E3,$E9,$E9,$F0,$F0,$F0,$F8,$F9,$F9,$00,$00,$00
MSExplosionRight_Frame6_XDisplacements:
	db $E2,$E2,$E4,$E4,$E8,$EC,$EC,$F2,$F4,$F8,$FC,$FC,$FC,$02,$02
MSExplosionRight_Frame7_XDisplacements:
	db $E2,$E2,$E4,$E4,$E7,$EC,$EC,$F2,$F4,$F7,$FC,$FC,$FC,$04,$04,$07
	db $07
MSExplosionRight_Frame8_XDisplacements:
	db $E2,$E2,$E3,$E6,$E7,$EA,$EA,$F2,$F3,$F6,$FA,$FA,$02,$02,$03,$03
	db $06,$06
MSExplosionRight_Frame9_XDisplacements:
	db $E2,$E2,$E2,$E3,$E6,$E6,$EE,$EE,$F2,$F2,$F6,$FE,$FE,$FE,$02,$02
	db $02,$02
MSExplosionRight_Frame10_XDisplacements:
	db $E2,$E2,$E2,$E9,$EF,$F2,$F2,$F2,$F9,$F9,$FF,$02,$02,$02,$02,$02
MSExplosionRight_Frame11_XDisplacements:
	db $E2,$E2,$E2,$E2,$E9,$E9,$F2,$F2,$F2,$F9,$F9,$F9,$FD,$02,$02,$02
MSExplosionRight_Frame12_XDisplacements:
	db $E3,$E3,$E4,$E4,$E9,$F0,$F3,$F3,$F3,$F8,$F8,$FB,$02,$03,$03,$03
	db $03
MSExplosionRight_Frame13_XDisplacements:
	db $E4,$E4,$E5,$EA,$EA,$F4,$F4,$F7,$F8,$FA,$FA,$04,$04,$04,$04
MSExplosionRight_Frame14_XDisplacements:
	db $E6,$E6,$E6,$EE,$F6,$F6,$F6,$F8,$FE,$06,$06,$06,$06,$06,$06
MSExplosionRight_Frame15_XDisplacements:
	db $E7,$E7,$E7,$E8,$EF,$F7,$F7,$F8,$F8,$F8,$FF,$07,$07
MSExplosionRight_Frame17_XDisplacements:
	db $E9,$E9,$E9,$EC,$F8,$F9,$F9,$F9,$FC
MSExplosionRight_Frame18_XDisplacements:
	db $EA,$EA,$EA,$F2,$F9,$FA,$FA,$FA,$FE
MSExplosionRight_Frame20_XDisplacements:
	db $EB,$EB,$EB,$FA,$FB,$FB,$FB,$FF,$07
MSExplosionRight_Frame21_XDisplacements:
	db $EB,$EC,$EC,$F8,$FA,$FB,$FC,$FF
MSExplosionRight_Frame23_XDisplacements:
	db $EE,$F1,$F6,$F8,$F9,$FB,$FB
MSExplosionRight_Frame25_XDisplacements:
	db $F5,$FB,$FD
YDisplacements:
DKCContactEffect_Frame2_YDisplacements:
	db $08,$02,$00,$FF
DKCContactEffect_Frame3_YDisplacements:
	db $07,$01,$FD,$01
DKCContactEffect_Frame5_YDisplacements:
	db $06,$06,$10,$04,$FA,$F6
DKCContactEffect_Frame6_YDisplacements:
	db $06,$0A,$04,$05,$FA,$FD,$F5
DKCContactEffect_Frame7_YDisplacements:
	db $06,$10,$09,$06,$F9,$F4,$FC
DKCContactEffect_Frame10_YDisplacements:
	db $07,$0C,$16,$09,$02,$F7,$EE,$F9
DKCContactEffect_Frame11_YDisplacements:
	db $0B,$16,$08,$00,$F6,$F2,$F2,$FB
MSExplosionRight_Frame0_YDisplacements:
	db $1E,$0E,$16,$ED,$FD,$07,$17,$EB,$FB,$0B,$1B
MSExplosionRight_Frame1_YDisplacements:
	db $20,$F8,$08,$18,$EB,$F9,$09,$19,$E9,$F9,$01,$09,$11,$19,$21
MSExplosionRight_Frame2_YDisplacements:
	db $1B,$FB,$0B,$EB,$1A,$F9,$09,$19,$E9,$0A,$1A,$EA,$F2,$FA,$02
MSExplosionRight_Frame3_YDisplacements:
	db $FB,$0B,$1B,$EA,$FA,$0A,$0B,$1B,$09,$E9,$F9,$19,$21,$29
MSExplosionRight_Frame4_YDisplacements:
	db $03,$0A,$0C,$1C,$FA,$04,$EA,$14,$1C,$F9,$09,$19,$E9,$29
MSExplosionRight_Frame5_YDisplacements:
	db $FE,$0E,$1E,$E9,$F9,$09,$0E,$1E,$07,$E7,$F7,$17,$1F,$27
MSExplosionRight_Frame6_YDisplacements:
	db $0D,$1D,$F7,$07,$E7,$FF,$0F,$1F,$F7,$E7,$F7,$07,$17,$27,$2F
MSExplosionRight_Frame7_YDisplacements:
	db $0E,$1E,$F8,$08,$E8,$00,$10,$20,$F7,$E7,$FF,$0F,$1F,$EF,$F7,$E6
	db $EE
MSExplosionRight_Frame8_YDisplacements:
	db $08,$16,$F8,$E8,$1F,$0C,$1C,$03,$F3,$E3,$10,$20,$00,$08,$F0,$F8
	db $E2,$EA
MSExplosionRight_Frame9_YDisplacements:
	db $FD,$0D,$1D,$15,$EA,$FA,$F0,$00,$10,$20,$EA,$E0,$F0,$00,$10,$18
	db $20,$28
MSExplosionRight_Frame10_YDisplacements:
	db $FA,$0A,$1A,$EB,$20,$F8,$08,$18,$DF,$EF,$20,$FF,$07,$0F,$17,$1F
MSExplosionRight_Frame11_YDisplacements:
	db $FE,$0E,$16,$1E,$EC,$FC,$06,$0E,$1E,$DF,$EF,$FF,$21,$0F,$17,$1F
MSExplosionRight_Frame12_YDisplacements:
	db $FA,$0A,$12,$1A,$ED,$1F,$FD,$0D,$17,$E0,$F0,$1D,$2E,$00,$08,$10
	db $18
MSExplosionRight_Frame13_YDisplacements:
	db $FA,$0A,$1A,$ED,$FD,$0D,$1D,$E2,$E1,$F1,$01,$11,$19,$21,$29
MSExplosionRight_Frame14_YDisplacements:
	db $F1,$01,$11,$19,$F0,$00,$10,$E0,$1C,$F0,$F8,$00,$08,$10,$18
MSExplosionRight_Frame15_YDisplacements:
	db $F2,$02,$12,$18,$08,$F0,$00,$E0,$0D,$1D,$FD,$F0,$F8
MSExplosionRight_Frame17_YDisplacements:
	db $FF,$07,$0F,$F2,$E0,$FE,$0E,$1E,$F0
MSExplosionRight_Frame18_YDisplacements:
	db $F9,$FD,$0D,$F7,$E2,$F2,$02,$12,$22
MSExplosionRight_Frame20_YDisplacements:
	db $F9,$08,$0F,$E5,$F5,$05,$15,$25,$25
MSExplosionRight_Frame21_YDisplacements:
	db $FA,$FC,$0C,$18,$E7,$F8,$08,$E8
MSExplosionRight_Frame23_YDisplacements:
	db $0B,$05,$0B,$13,$03,$EB,$FC
MSExplosionRight_Frame25_YDisplacements:
	db $10,$07,$0A
Sizes:
DKCContactEffect_Frame2_Sizes:
	db $02,$00,$00,$02
DKCContactEffect_Frame3_Sizes:
	db $02,$02,$02,$00
DKCContactEffect_Frame5_Sizes:
	db $00,$02,$00,$02,$02,$02
DKCContactEffect_Frame6_Sizes:
	db $00,$02,$02,$02,$02,$02,$00
DKCContactEffect_Frame7_Sizes:
	db $02,$00,$02,$02,$02,$00,$02
DKCContactEffect_Frame10_Sizes:
	db $00,$02,$00,$02,$00,$02,$02,$02
DKCContactEffect_Frame11_Sizes:
	db $02,$00,$02,$00,$02,$00,$02,$02
MSExplosionRight_Frame0_Sizes:
	db $00,$00,$02,$02,$02,$02,$02,$02,$02,$02,$02
MSExplosionRight_Frame1_Sizes:
	db $00,$02,$02,$02,$02,$02,$02,$02,$02,$00,$00,$00,$00,$00,$00
MSExplosionRight_Frame2_Sizes:
	db $02,$02,$02,$02,$02,$02,$02,$00,$02,$02,$02,$00,$00,$00,$00
MSExplosionRight_Frame3_Sizes:
	db $02,$02,$02,$02,$02,$00,$02,$02,$02,$02,$02,$00,$00,$00
MSExplosionRight_Frame4_Sizes:
	db $00,$00,$02,$02,$02,$02,$02,$00,$02,$02,$02,$02,$02,$00
MSExplosionRight_Frame5_Sizes:
	db $02,$02,$02,$02,$02,$00,$02,$02,$02,$02,$02,$00,$00,$00
MSExplosionRight_Frame6_Sizes:
	db $02,$02,$02,$00,$02,$02,$02,$02,$00,$02,$02,$02,$02,$00,$00
MSExplosionRight_Frame7_Sizes:
	db $02,$02,$02,$00,$02,$02,$02,$02,$02,$02,$02,$02,$02,$00,$00,$00
	db $00
MSExplosionRight_Frame8_Sizes:
	db $02,$00,$02,$02,$00,$02,$02,$02,$02,$02,$02,$02,$00,$00,$00,$00
	db $00,$00
MSExplosionRight_Frame9_Sizes:
	db $02,$02,$02,$00,$02,$00,$02,$02,$02,$02,$00,$02,$02,$02,$00,$00
	db $00,$00
MSExplosionRight_Frame10_Sizes:
	db $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$00,$00,$00,$00,$00
MSExplosionRight_Frame11_Sizes:
	db $02,$02,$00,$02,$02,$02,$00,$02,$02,$02,$02,$02,$02,$00,$00,$00
MSExplosionRight_Frame12_Sizes:
	db $02,$02,$00,$02,$02,$02,$02,$02,$00,$02,$02,$02,$00,$00,$00,$00
	db $00
MSExplosionRight_Frame13_Sizes:
	db $02,$02,$02,$02,$02,$02,$02,$00,$02,$02,$02,$00,$00,$00,$00
MSExplosionRight_Frame14_Sizes:
	db $02,$02,$02,$02,$02,$02,$02,$02,$02,$00,$00,$00,$00,$00,$00
MSExplosionRight_Frame15_Sizes:
	db $02,$02,$00,$02,$02,$02,$00,$02,$02,$02,$02,$00,$00
MSExplosionRight_Frame17_Sizes:
	db $02,$00,$02,$02,$02,$02,$02,$02,$02
MSExplosionRight_Frame18_Sizes:
	db $00,$02,$02,$00,$02,$02,$02,$02,$02
MSExplosionRight_Frame20_Sizes:
	db $02,$02,$02,$02,$02,$02,$02,$00,$00
MSExplosionRight_Frame21_Sizes:
	db $02,$02,$02,$02,$00,$02,$02,$02
MSExplosionRight_Frame23_Sizes:
	db $00,$00,$00,$02,$02,$02,$02
MSExplosionRight_Frame25_Sizes:
	db $00,$00,$02


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