spread

by tjb0607

Game: SMW

Description: dtothefourth's "duplicating tiles" patch.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Duplicating tiles by dtothefourth
;
; Causes tiles to randomly copy themselves to adjacent tiles.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


!Rate = #$10 ; How many frames between spreading tiles
!Num  = #$01 ; How many tiles per spread
	;note that above 3 will cause flicker and 3 will cause flicker when also collecting coins and such

!AllowAir = 0 ; If not 0, air tiles will also be allowed to duplicate instead of just solid tiles
	;Probably want to turn the rate up a bit, as it will spend a fair amount of time duping air on top of air

!Retry = #$0020 ; How many times it will re-roll duplication targets if it chooses an invalid one
				; More consistant duping at the cost of potential lag, turn down for no lag

;FreeRAM
!XPos = $7FB600
!YPos = $7FB602
!Tile = $7FB604
!Prop = $7FB605
!Timer2 = $7FB606
nmi:
	LDA $00
	PHA
	LDA $9D
	ORA $13D4
	ORA $13FB
	ORA $1426
	ORA $1493
	ORA $0D9B
	BEQ +
	-
	PLA
	STA $00
	RTL
	+


	LDA $3E
	AND #$07
	CMP #$07
	BEQ -


	LDA !Timer2	; \ because there's no init function with CARL
	CMP #$55	; | snes initializes ram at #$55 i think
	BNE +		; | so this'll initialize it at #$00 instead
	LDA #$00	; |
	STA !Timer2	; |
	+		; /

	LDA !Timer2
	INC
	STA !Timer2
	CMP !Rate
	BEQ +
	PLA
	STA $00
	RTL
	+

	LDX !Num-1
	-
	PHX
	JSR Generate
	PLX
	DEX
	BPL -
			
	PLA
	STA $00
	RTL

Generate:
	LDA #$00
	STA !Timer2

	REP #$20
	LDA $94
	SEC
	SBC $7E
	CMP #$0010
	BPL +
	LDA #$0010
	+
	STA $9A
	STA !XPos
	
	LDA $96
	SEC
	SBC $80
	CMP #$0010
	BPL +
	LDA #$0010
	+
	STA $98
	STA !YPos

	LDA #$0000
	STA $1933
	
	STA !Prop

	-

	LDA !Prop
	INC
	STA !Prop
	CMP !Retry
	BNE +++++
	SEP #$20
	RTS
	+++++

	SEP #$20
	JSL $01ACF9
	REP #$20
	AND #$00F0
	CMP #$00F0
	BEQ -
	CLC
	ADC !XPos
	STA $9A

	--
	SEP #$20
	JSL $01ACF9
	REP #$20
	AND #$00F0
	CMP #$00F0
	BEQ --
	CMP #$00E0
	BEQ --
	CMP #$00D0
	BEQ --

	CLC
	ADC !YPos
	STA $98

	CMP #$01C0
	BPL -

	LDA $98
	SEC
	SBC $96
	BPL +++
	EOR #$FFFF
	INC
	+++
	STA $00

	LDA $9A
	SEC
	SBC $94
	BPL +++
	EOR #$FFFF
	INC
	+++
	CLC
	ADC $00
	CMP #$0041
	BMI -

	JSR GetMap16

	if !AllowAir == 0
	CMP #$0025
	BEQ -
	endif

	PHA

	JSR Offset

	PLA
	REP #$10
	TAX
	JSR change_map16
	SEP #$30


	+
	RTS

Offset:

	SEP #$20
	JSL $01ACF9
	AND #$03
	BNE +

	REP #$20
	LDA $98
	SEC
	SBC #$0010
	STA $98
	RTS
	+
	CMP #$01
	BNE +
	REP #$20
	LDA $9A
	CLC
	ADC #$0010
	STA $9A
	RTS
	+
	CMP #$02
	BNE +
	REP #$20
	LDA $98
	CLC
	ADC #$0010
	STA $98
	RTS
	+
	REP #$20
	LDA $9A
	SEC
	SBC #$0010
	STA $9A
	RTS


change_map16:
LDA $0F : PHA
	PHP
	PHY
	PHX
	LDY $98
	STY $0E
	LDY $9A
	STY $0C
	SEP #$30
	LDA $5B
	LDX $1933
	BEQ .Layer1
	LSR A
.Layer1
	STA $0A
	LSR A
	BCC .Horz
	LDA $9B
	LDY $99
	STY $9B
	STA $99
.Horz
	LDA $99
	CMP #$02
.check
	BCS .End
	LDA $9B
	CMP $5D
	BCC .NoEnd
.End
	REP #$10
	PLX
	PLY
	PLP
	JMP .returnasdf

.NoEnd
	STA $0B
	ASL A
	ADC $0B
	TAY
	REP #$20
	LDA $98
	AND.w #$FFF0
	STA $08
	AND.w #$00F0
	ASL #2			; 0000 00YY YY00 0000
	XBA			; YY00 0000 0000 00YY
	STA $06
	TXA
	SEP #$20
	ASL A
	TAX

	LDA $0D
	LSR A
	LDA $0F
	AND #$01		; 0000 000y
	ROL A			; 0000 00yx
	ASL #2			; 0000 yx00
	ORA.l .LayerData,x	; 001l yx00
	TSB $06			; $06 : 001l yxYY
	LDA $9A			; X LowByte
	AND #$F0		; XXXX 0000
	LSR #3			; 000X XXX0
	TSB $07			; $07 : YY0X XXX0
	LSR A
	TSB $08

	LDA $1925
	ASL A
	REP #$31
	ADC $00BEA8,x
	TAX
	TYA

		ADC $00,x
		TAX
		LDA $08
		ADC $00,x
	TAX
	PLA
	ASL A
	TAY
	LSR A
	SEP #$20
		STA $7E0000,x
		XBA
		STA $7F0000,x
	LSR $0A
	LDA $1933
	REP #$20
	BCS .Vert
	BNE .HorzL2
.HorzL1
	LDA $1A			;\
	SBC #$007F		; |$08 : Layer1X - 0x80
	STA $08			;/
	LDA $1C			;  $0A : Layer1Y
	BRA .Common
.HorzL2
	LDA $1E			;\ $08 : Layer2X
	STA $08			;/
	LDA $20			;\ $0A : Layer2Y - 0x80
	SBC #$007F		;/
	BRA .Common

.Vert
	BNE .VertL2
	LDA $1A			;\ $08 : Layer1X
	STA $08			;/
	LDA $1C			;\ $0A : Layer1Y - 0x80
	SBC #$0080		;/
	BRA .Common
.VertL2
	LDA $1E			;\
	SBC #$0080		; |$08 : Layer2X - 0x80
	STA $08			;/
	LDA $20			;  $0A : Layer2Y
.Common
	STA $0A
	PHB
	PHK
;	PER $0006
	PEA .return-1
	PEA $804C
	JML $00C0FB
.return
	PLB
	PLY
	PLP
.returnasdf
	PLA : STA $0F
	RTS

.LayerData	db $20,$00,$30

GetMap16:
	PHX
	PHP
	REP #$10
	PHB
	LDY $98
	STY $0E
	LDY $9A
	STY $0C
	SEP #$30
	LDA $5B
	LDX $1933
	BEQ .layer1
	LSR A
.layer1
	STA $0A
	LSR A
	BCC .horz
	LDA $9B
	LDY $99
	STY $9B
	STA $99
.horz
.verticalCheck
	LDA $99
	CMP #$02
.check
	BCC .noEnd
	PLB
	PLP
	PLX
	PLA
	STA $00
	LDA #$FF
	RTL
	
.noEnd
	LDA $9B
	STA $0B
	ASL A
	ADC $0B
	TAY
	REP #$20
	LDA $98
	AND.w #$FFF0
	STA $08
	AND.w #$00F0
	ASL #2			; 0000 00YY YY00 0000
	XBA			; YY00 0000 0000 00YY
	STA $06
	TXA
	SEP #$20
	ASL A
	TAX
	
	LDA $0D
	LSR A
	LDA $0F
	AND #$01		; 0000 000y
	ROL A			; 0000 00yx
	ASL #2			; 0000 yx00
	ORA #$20		; 0010 yx00
	CPX #$00
	BEQ .noAdd
	ORA #$10		; 001l yx00
.noAdd
	TSB $06			; $06 : 001l yxYY
	LDA $9A			; X LowByte
	AND #$F0		; XXXX 0000
	LSR #3			; 000X XXX0
	TSB $07			; $07 : YY0X XXX0
	LSR A
	TSB $08

	LDA $1925
	ASL A
	REP #$31
	ADC $00BEA8,x
	TAX
	TYA
    ADC $00,x
    TAX
    LDA $08
    ADC $00,x
	TAX
	SEP #$20
	LDA $7F0000,x
	XBA
	LDA $7E0000,x
	SEP #$30
	XBA
	TAY
	XBA

	PLB
	PLP
	PLX
	RTS
back to listings