FieldVerticalTransition: ; 0xe674
	push af
	ld a, [wPinballIsVisible]
	push af
	xor a
	ld [wPinballIsVisible], a
	ld [wRumblePattern], a
	callba DrawSpritesForStage
	call CleanOAMBuffer
	pop af
	ld [wPinballIsVisible], a
	pop af
	ld [wCurrentStage], a
	xor a
	ld [hBGP], a
	ld [hOBP0], a
	ld [hOBP1], a
	rst AdvanceFrame
	call ToggleAudioEngineUpdateMethod
	call DisableLCD
	call ClearOAMBuffer
	call Func_1129
	call LoadStageCollisionAttributes
	call LoadStageData
	call ToggleAudioEngineUpdateMethod
	call EnableLCD
	ld a, $e4
	ld [hBGP], a
	ld a, $e1
	ld [hOBP0], a
	ld a, $e4
	ld [hOBP1], a
	ret

LoadStageData: ; 0xe6c2
; Loads all stage data, such as graphics, collision attributes, etc.
	ld a, [wCurrentStage]
	bit 0, a
	ld a, $86
	jr z, .gotWindowYPos
	ld a, [wBottomTextEnabled]
	and a
	ld a, $86
	jr nz, .gotWindowYPos
	ld a, $90
.gotWindowYPos
	ld [hWY], a
	ld hl, StageGfxPointers_GameBoyColor
	ld a, [wCurrentStage]
	call LoadVideoData
	xor a
	ld [wd7f2], a
	callba _LoadStageData
	ret

INCLUDE "data/stage_base_gfx.asm"

CheckStageTransition: ; 0xece9
	call ScrollScreenToShowPinball
	ld a, [wBallYPos + 1]
	add $10
	cp $18
	jr c, .moving_up
	cp $b8
	ret c
	ld a, [wCurrentStage]
	ld c, a
	ld b, $0
	ld hl, BallMovingDownStageTransitions
	add hl, bc
	ld a, [hl]
	cp $ff
	jr z, .youLose
	call FieldVerticalTransition
	ld a, [wBallYPos + 1]
	sub $88
	ld [wBallYPos + 1], a
	ret

.moving_up
	ld a, [wCurrentStage]
	ld c, a
	ld b, $0
	ld hl, BallMovingUpStageTransitions
	add hl, bc
	ld a, [hl]
	cp $ff
	jr z, .youLose
	call FieldVerticalTransition
	ld a, [wBallYPos + 1]
	add $88
	ld [wBallYPos + 1], a
	ret

.youLose
	ld a, $1
	ld [wMoveToNextScreenState], a
	callba HandleBallLoss
	ret

BallMovingUpStageTransitions: ; 0xed3e
; Maps the relationship between stages when
; the ball moves out of the screen upward.
	db $FF                      ; STAGE_RED_FIELD_TOP
	db STAGE_RED_FIELD_TOP      ; STAGE_RED_FIELD_BOTTOM
	db $FF                      ; STAGE_BLUE_FIELD_TOP
	db STAGE_BLUE_FIELD_TOP     ; STAGE_BLUE_FIELD_BOTTOM
	db $FF                      ; STAGE_GOLD_FIELD_TOP
	db STAGE_GOLD_FIELD_TOP     ; STAGE_GOLD_FIELD_BOTTOM
	db $FF                      ; STAGE_SILVER_FIELD_TOP
	db STAGE_SILVER_FIELD_TOP   ; STAGE_SILVER_FIELD_BOTTOM
	db $FF                      ; STAGE_RUBY_FIELD_TOP
	db STAGE_RUBY_FIELD_TOP     ; STAGE_RUBY_FIELD_BOTTOM
	db $FF                      ; STAGE_SAPPHIRE_FIELD_TOP
	db STAGE_SAPPHIRE_FIELD_TOP ; STAGE_SAPPHIRE_FIELD_BOTTOM
	db $FF                      ; STAGE_GENGAR_BONUS
	db $FF                      ; STAGE_GENGAR_BONUS
	db $FF                      ; STAGE_MEWTWO_BONUS
	db $FF                      ; STAGE_MEWTWO_BONUS
	db $FF                      ; STAGE_MEOWTH_BONUS
	db $FF                      ; STAGE_MEOWTH_BONUS
	db $FF                      ; STAGE_DIGLETT_BONUS
	db $FF                      ; STAGE_DIGLETT_BONUS
	db $FF                      ; STAGE_SEEL_BONUS
	db $FF                      ; STAGE_SEEL_BONUS

BallMovingDownStageTransitions: ; 0xed4e
; Maps the relationship between stages when
; the ball moves out of the screen downward.
	db STAGE_RED_FIELD_BOTTOM      ; STAGE_RED_FIELD_TOP
	db $FF                         ; STAGE_RED_FIELD_BOTTOM
	db STAGE_BLUE_FIELD_BOTTOM     ; STAGE_BLUE_FIELD_TOP
	db $FF                         ; STAGE_BLUE_FIELD_BOTTOM
	db STAGE_GOLD_FIELD_BOTTOM     ; STAGE_GOLD_FIELD_TOP
	db $FF                         ; STAGE_GOLD_FIELD_BOTTOM
	db STAGE_SILVER_FIELD_BOTTOM   ; STAGE_SILVER_FIELD_TOP
	db $FF                         ; STAGE_SILVER_FIELD_BOTTOM
	db STAGE_RUBY_FIELD_BOTTOM     ; STAGE_RUBY_FIELD_TOP
	db $FF                         ; STAGE_RUBY_FIELD_BOTTOM
	db STAGE_SAPPHIRE_FIELD_BOTTOM ; STAGE_SAPPHIRE_FIELD_TOP
	db $FF                         ; STAGE_SAPPHIRE_FIELD_BOTTOM
	db $FF                         ; STAGE_GENGAR_BONUS
	db $FF                         ; STAGE_GENGAR_BONUS
	db $FF                         ; STAGE_MEWTWO_BONUS
	db $FF                         ; STAGE_MEWTWO_BONUS
	db $FF                         ; STAGE_MEOWTH_BONUS
	db $FF                         ; STAGE_MEOWTH_BONUS
	db $FF                         ; STAGE_DIGLETT_BONUS
	db $FF                         ; STAGE_DIGLETT_BONUS
	db $FF                         ; STAGE_SEEL_BONUS
	db $FF                         ; STAGE_SEEL_BONUS

ScrollScreenToShowPinball: ; 0xed5e
; When the ball is launched on the Blue and Red Fields, the screen starts off scrolled to the right.
; However, when the balls rolls in on Bonus Stages, the screen does NOT scroll.
	ld hl, wSCX
	ld a, [wDisableHorizontalScrollForBallStart]
	and a
	jr nz, .modify_scx_and_scy
	ld a, [wBallXPos + 1]
	cp $9a
	ld a,  2
	jr nc, .okay1
	ld a, -2
.okay1
	ld [wUnused_d7aa], a ; This is not used
	add [hl]
	cp $22
	jr z, .modify_scx_and_scy
	bit 7, a
	jr nz, .modify_scx_and_scy
	ld [hl], a
.modify_scx_and_scy
	ld a, [hl]
	ld hl, wLeftAndRightTiltPixelsOffset
	sub [hl]
	ld [hSCX], a
	xor a
	ld hl, wUpperTiltPixelsOffset
	sub [hl]
	ld [hSCY], a
	ret
