!FreeRamForMode2 = $7F9D00	;Change if it conflicts with other patches (about $40 bytes)

BlimpBattlefield:		; Needed to get the graphics correct.
incbin Blimp_battlefield.bin
BlimpBackground:
incbin Background.bin

level6E:
LDA $1493
CMP #$F8
BNE +
LDA #$09
STA $3E
+
STZ $1B96
LDA $9D
BEQ +
RTS
+
LDA $1697
CMP #$07
BNE +
LDA #$06
STA $1697
+

LDA $15
CMP #$10
BCS .NoRNGManipulation1
JSL $01ACF9

.NoRNGManipulation1
LDA $15
CMP #$40
BNE .NoRNGManipulation2
JSL $01ACF9

.NoRNGManipulation2
LDA $15
CMP #$80
BNE .NoRNGManipulation3
JSL $01ACF9

.NoRNGManipulation3

.NoRNGManipulation4

	LDA $1B97
	BEQ .noWaveStuffJump
	LDX #$00
	BRA .waveLoop

.noWaveStuffJump
JMP .noWaveStuff
.waveLoop    

	LDA $1B97
	REP #$20
	AND #$00FF
	STA $03
	ASL
	ASL
	ASL
	STA $01
	LDA $03
	SEC
	SBC #$00FF
	SEP #$20
	STA $00
	JSR SIN
	STZ $0A		; $0A indicates if this number was negative or not.
	REP #$20
	LDA $03
	BPL +
	EOR #$FFFF
	INC A
	INC $0A
+
	LSR

	STA $00
	TXA
	AND #$00FF
	CLC
	ADC $1B98
	ASL #4
	CLC
	ADC #$0080
	STA $01
	SEP #$20
	JSR SIN

	REP #$20
	LDA $03	
	LDY $0A
	BNE .isNegative
	REP #$20
	CLC
	ADC #$2065
	BRA +
.isNegative
	LDA #$2065
	SEC
	SBC $03

+	STA $00


	PHX		; Preserve x; we need it to index a table.
	TXA
	ASL
	CLC
	ADC #$0040
	TAX
	LDA $00
	CMP #$2000
	BCS +
	LDA #$2000
+
	STA !FreeRamForMode2,x
	PLX
	INX
	CPX #$20
	BNE .waveLoop
	;LDA $14
	;AND #$01
	;BEQ +
	SEP #$30
	LDA $13D4
	BNE .noDecreaseTimer
	DEC $1B97
.noDecreaseTimer
STA $7FFFE0
	LDA $0F6B	; Don't affect the player if he's flying.
	BEQ +
	RTS
	+
	REP #$30
	LDA $D3
	SEC
	SBC #$008B
	STA $04		; $04 contains the player's y coord (feet-based) "relative" to the blimp's position
	
	LDA $D1
	LSR #3
	ASL
	TAX
	LDA !FreeRamForMode2+$40,x
	AND #$0FFF
	STA $00
	LDA !FreeRamForMode2+$42,x
	AND #$0FFF
	CMP $00
	BCS +
	LDA $00
	+

	SEC
	SBC #$0065	; We have to flip this over 65; tiles are stored upside down (a large y is higher up on the screen than a lower y)
	EOR #$FFFF
	INC A
	CLC
	ADC #$0065
	
	STA $00		; $00 holds the y position of the highest blimp "piece" that the player is touching / over.
	CMP $04
	SEP #$30
	BEQ .return
	BPL .belowPlayer
			; The highest piece is above the player.  Launch the player upwards.
	REP #$20
	LDA $00	
	CLC
	ADC #$008B
	STA $96
	LDA $00
	SEC
	SBC $04
	AND #$00FF
	SEP #$20
	ASL #4
	ORA #$80

	CMP $7D
	BPL +
	STA $7D
	+
	RTS
	
	
.belowPlayer
			; The highest piece is below the player.  Make the player fall.
	LDA #$01
	STA $185C
	
;+
.return
	RTS			; Skip the code where we level the playing field.


.noWaveStuff
	STZ $185C
	
	REP #$20	; \
	LDA $96		; |
	BMI +		; |
	CMP #$00F0	; |
	BCC +		; | Don't let the player fall off the screen.
	LDA #$00F0	; |
	STA $96		; |
	+		; /
	
	LDX #$40

.loop2    
	CPX #$80
	BEQ .done2
	LDA #$2065
	STA !FreeRamForMode2,x
	INX
	INX
	BRA .loop2
.done2


	
SEP #$20
LDA $77
AND #$04
BEQ .sprites

REP #$30
LDA $D1

LSR
LSR
DEC A
DEC A
DEC A
AND #$FFFE
TAX


LDA !FreeRamForMode2+$40,x
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
DEC A
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
SEC
SBC #$0004
STA !FreeRamForMode2+$40,x
INX 
INX

LDA !FreeRamForMode2+$40,x
SEC
SBC #$0004
STA !FreeRamForMode2+$40,x
INX 
INX

LDA !FreeRamForMode2+$40,x
DEC A
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
DEC A
STA !FreeRamForMode2+$40,x

;;INC A
;;STA !FreeRamForMode2+$40

.sprites
SEP #$30


LDY #$FF
.loop
INY
CPY #$0C
BEQ .noadjust

LDA $14C8,y
BEQ .loop
LDA $1588,y
AND #$04
BEQ .loop

LDA $14E0,y
XBA
LDA $00E4,y
REP #$30
;SEC
;SBC #$0008
LSR
LSR
DEC A
DEC A
DEC A
AND #$FFFE
TAX


LDA !FreeRamForMode2+$40,x
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
DEC A
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
SEC
SBC #$0004
STA !FreeRamForMode2+$40,x
INX 
INX

LDA !FreeRamForMode2+$40,x
SEC
SBC #$0004
STA !FreeRamForMode2+$40,x
INX 
INX

LDA !FreeRamForMode2+$40,x
DEC A
DEC A
STA !FreeRamForMode2+$40,x
INX
INX

LDA !FreeRamForMode2+$40,x
DEC A
STA !FreeRamForMode2+$40,x
SEP #$30
BRA .loop




.noadjust
	SEP #$10
	REP #$20
	LDX #$40

.loop3 
	CPX #$80
	BEQ .done3
	LDA !FreeRamForMode2,x
	CMP #$2061
	BCS $07
	LDA #$2061
	STA !FreeRamForMode2,x
	INX #2
	BRA .loop3
.done3

SEP #$30

	RTS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;サインJSL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;$00 = radius
;$01 - $02 = angle
;$03 - $04 = result


SIN:		PHP
		PHX

		REP #$30		;16bitモード
		LDA $01
		AND #$00FF
		ASL A
		TAX				
		LDA $07F7DB,x
		STA $03			

		SEP #$30		;8bitモード	
		LDA $02			;$02を保存
		PHA
		LDA $03			;|sin|を
		STA $4202		;「かけられる数」とする。
		LDA $00			;半径を呼ぶ
		LDX $04			;|sin| = 1.00 だったら計算不要（Rsin = 半径）
		BNE IF1_SIN
		STA $4203		;半径を「かける数」とする。
		ASL $4216		;出た答えの小数点以下を四捨五入
		LDA $4217		
		ADC #$00
IF1_SIN:	LSR $02			;絶対値を外す
		BCC IF_SIN_PLUS

		EOR #$FF
		INC A
		STA $03
		BEQ IF0_SIN
		LDA #$FF
		STA $04
		BRA END_SIN

IF_SIN_PLUS:	STA $03
IF0_SIN:	STZ $04

END_SIN:	PLA
		STA $02			;$02を復元
		PLX
		PLP
		RTS