; Overworld SMWC Coin graphics - Patch by RPG Hacker

print ""
print "Overworld SMWC Coins"


header
lorom


!XPos = $12
!YPos = $29

!FrameTile1 = $4C
!FrameTile2 = $4D

!FilledTile = $6E
!EmptyTile = $6C



;;;;;;;;;;;;;;;;
;External files;
;;;;;;;;;;;;;;;;


incsrc CoinDefines.asm		; Coin defines



;;;;;;;;;
;Defines;
;;;;;;;;;


; Only touch when you know what you're doing
; Used for calculating translevel number and for timer

!owram = $0DC3		; 4 Bytes
!timer = !owram

!timerval = $10		; Timer for the blinking coin



;;;;;;;;;;;;;;;;;
;Routine hijacks;
;;;;;;;;;;;;;;;;;


org $049D95			; Level name display routine
	autoclean jml OWHack

org $03BB71			; The same for overworlds edited
	autoclean jml OWLMHack		; with Lunar Magic
	autoclean jml BackupHijack	; <- Backup of old code

org $048356			; OW Routine that runs each frame
	autoclean jml DisplayCoins
	nop



;;;;;;;;;;;;;
;PATCH START;
;;;;;;;;;;;;;

                                    
freecode



;;;;;;;;;;;;;;;;;;;;
;Level name routine;
;;;;;;;;;;;;;;;;;;;;


OWHack:
	iny
	plp
	bpl .loop

	jsr UpdateLevel
	jml $049D99|$800000

.loop
	jml $049D7F|$800000


OWLMHack:
	jsr UpdateLevel
	rtl

UpdateLevel:
	phy
	phx
	pha
	phb
	phk
	plb
	php
	sep #$30

	jsr GetTransLevel

	lda #$00
	sta !timer

.return
	plp
	plb
	pla
	plx
	ply
	rts	;jml $049D99


; Loads translevel number of current position on overworld

GetTransLevel:
	ldy #$00
	rep #$30
	ldx $0DD6
	lda $1F1F,x
	and #$000F
	sta !owram
	lda $1F21,x
	and #$000F
	asl
	asl
	asl
	asl
	sta !owram+2
	lda $1F1F,x
	and #$0010
	asl
	asl
	asl
	asl
	ora !owram
	sta !owram
	lda $1F21,x
	and #$0010
	asl
	asl
	asl
	asl
	asl
	ora !owram+2
	ora !owram
	tax
	lda $0DD6
	and #$00FF
	lsr
	lsr
	tay
	lda $1F11,y
	and #$000F
	beq .nosubmap
	txa
	clc
	adc #$0400
	tax

.nosubmap
	sep #$20
	lda $7ED000,x
	sta $13BF
	sep #$10

	rts


BackupHijack:
	lda $009CCB|$800000,x
	xba
	jml $009DB9|$800000



;;;;;;;;;;;;;;;;;
;Display routine;
;;;;;;;;;;;;;;;;;


DisplayCoins:
	phy
	phx
	pha
	phb
	phk
	plb
	php

	LDA $13D9	; only run when standing on a level tile and not pausing
	SEC : SBC #$03
	ORA $13D4
	BEQ +
	JMP .return
	+

.begin
	lda $13BF
	lsr
	lsr
	lsr
	tay
	lda $13BF
	and #$07
	tax
	lda leveltable,y
	and levelbittable,x
	cmp #$00
	bne .coinlevel
	jmp .nocoins


.coinlevel

	JSR .showFrame

	lda !timer	; Increase timer
	cmp.b #!timerval*2
	bcc .timerok
	lda #$FF

.timerok
	inc
	sta !timer



.coin1
	lda $13BF	; Is first coin collected?
	and #$07
	tax
	lda coinbittable,x
	sta.l !TempCounter+3

	lda $13BF
	lsr #3
	tax

	lda.l !ramspace,x
	and.l !TempCounter+3
	beq .1tempcheck
	bra .1filled

.1tempcheck
	lda $13BF	; Is first coin temporarily collected?
	cmp.l !TempCounter
	bne .1empty

	lda.l !TempCounter+1
	lsr #3
	eor.l !TempCounter+2
	and #$01
	beq .1empty

	lda !timer
	cmp.b #!timerval
	bcc .1empty
	bra .1filled	

.1filled
	JSR .firstFilled		;[PUT GRAPHICS CODE FOR FIRST FILLED COIN HERE]

	bra .coin2

.1empty
	JSR .firstEmpty			;[PUT GRAPHICS CODE FOR FIRST EMPTY COIN HERE]



.coin2
	lda $13BF	; Is second coin collected?
	and #$07
	tax
	lda coinbittable,x
	sta.l !TempCounter+3

	lda $13BF
	lsr #3
	tax

	lda.l !ramspace+$0B,x
	and.l !TempCounter+3
	beq .2tempcheck
	bra .2filled

.2tempcheck
	lda $13BF	; Is second coin temporarily collected?
	cmp.l !TempCounter
	bne .2empty

	lda.l !TempCounter+1
	lsr #3
	eor.l !TempCounter+2
	and #$02
	beq .2empty

	lda !timer
	cmp.b #!timerval
	bcc .2empty
	bra .2filled	

.2filled
	JSR .secondFilled		;[PUT GRAPHICS CODE FOR SECOND FILLED COIN HERE]

	bra .coin3

.2empty
	JSR .secondEmpty		;[PUT GRAPHICS CODE FOR SECOND EMPTY COIN HERE]



.coin3
	lda $13BF	; Is third coin collected?
	and #$07
	tax
	lda coinbittable,x
	sta.l !TempCounter+3

	lda $13BF
	lsr #3
	tax

	lda.l !ramspace+$16,x
	and !TempCounter+3
	beq .3tempcheck
	bra .3filled

.3tempcheck
	lda $13BF	; Is third coin temporarily collected?
	cmp.l !TempCounter
	bne .3empty

	lda.l !TempCounter+1
	lsr #3
	eor.l !TempCounter+2
	and #$04
	beq .3empty

	lda !timer
	cmp.b #!timerval
	bcc .3empty
	bra .3filled	

.3filled
	JSR .thirdFilled		;[PUT GRAPHICS CODE FOR THIRD FILLED COIN HERE]

	bra .return

.3empty
	JSR .thirdEmpty			;[PUT GRAPHICS CODE FOR THIRD EMPTY COIN HERE]

	bra .return



.nocoins
	;[PUT CODE FOR WHEN ON LEVEL WITHOUT ANY COINS]



.return
	plp
	plb
	pla
	plx
	ply

	lda $13D9               
	cmp #$03 
	jml $04835B|$800000



.showFrame

	PHY
	LDY #$02
	LDX #$4C
-
	LDA .frameXPos,y
	STA $0200,x
	LDA #!YPos
	STA $0201,x
	LDA .frameTilemap,y
	STA $0202,x
	LDA .frameProperties,y
	STA $0203,x
	PHX : TXA : LSR #2 : TAX
	LDA #$02
	STA $0420,x
	PLX

	INX #4
	DEY
	BPL -

	PLY
	RTS

.frameXPos
	db !XPos,!XPos+8,!XPos+18
.frameTilemap
	db !FrameTile1,!FrameTile2,!FrameTile1
.frameProperties
	db $30,$30,$70










.firstFilled
	LDX #$40
	LDA.b #!XPos+4
	JSR .shared1
	RTS

.firstEmpty
	LDX #$40
	LDA.b #!XPos+4
	JSR .shared2
	RTS



.secondFilled
	LDX #$44
	LDA.b #!XPos+13
	JSR .shared1
	RTS

.secondEmpty
	LDX #$44
	LDA.b #!XPos+13
	JSR .shared2
	RTS



.thirdFilled
	LDX #$48
	LDA.b #!XPos+22
	JSR .shared1
	RTS

.thirdEmpty
	LDX #$48
	LDA.b #!XPos+22
	JSR .shared2
	RTS



.emptyTilemap
	db !EmptyTile,!EmptyTile+1,!EmptyTile,!EmptyTile+1
.emptyProperties
	db $30,$30,$70,$B0




.shared1
	STA $0200,x
	LDA.b #!YPos+4
	STA $0201,x
	LDA #!FilledTile
	STA $0202,x
	LDA #$30
	BRA .shared3

.shared2
	STA $0200,x
	LDA.b #!YPos+4
	STA $0201,x
	LDA $13 : LSR #3 : AND #$03 : PHX : TAX
	LDA .emptyTilemap,x
	PLX
	STA $0202,x
	LDA $13 : LSR #3 : AND #$03 : PHX : TAX
	LDA .emptyProperties,x
	PLX
.shared3
	STA $0203,x
	TXA : LSR #2 : TAX
	STZ $0420,x
	RTS



;;;;;;;;;;;;;;
;Patch tables;
;;;;;;;;;;;;;;


levelbittable:
	db $80,$40,$20,$10,$08,$04,$02,$01

coinbittable:
	db $01,$02,$04,$08,$10,$20,$40,$80


; This table defines, which levels do and which don't have SMWC coins
; Order is from left to right

leveltable:
	db %00111111	; Level 000 - 007
	db %11111111	; Level 008 - 00F
	db %11111111	; Level 010 - 017
	db %11111111	; Level 018 - 01F
	db %11111111	; Level 020 - 024, Level 101 - 103
	db %11111111	; Level 104 - 10B
	db %11111111	; Level 10C - 113
	db %11111111	; Level 114 - 11B
	db %11111111	; Level 11C - 123
	db %11111111	; Level 124 - 12B
	db %11111100	; Level 12C - 133
	db %00000000	; Level 134 - 13B




print freespaceuse," bytes written at an address you don't need to care about."
print ""