FishFunction:
	ld a, e
	push af
	call ClearFieldMoveBuffer
	pop af
	ld [wRodType], a
.loop
	ld hl, .FishTable
	call FieldMoveJumptable
	jr nc, .loop
	and $7f
	ld [wFieldMoveSucceeded], a
	ret

.FishTable
	dw .TryFish
	dw .FishNoBite
	dw .FishGotSomething
	dw .FailFish
	dw .FishNoFish

.TryFish
	ld a, [PlayerState]
	cp PLAYER_SURF
	jr z, .fail
	cp PLAYER_SURF_PIKA
	jr z, .fail
	call GetFacingTileCoord
	call GetTileCollision
	cp $1
	jr z, .facingwater
.fail
	ld a, $3
	ret

.facingwater
	call GetFishingGroup
	and a
	jr nz, .goodtofish
	ld a, $4
	ret

.goodtofish
	ld d, a
	ld a, [wRodType]
	ld e, a
	callba Fish
	ld a, d
	and a
	jr z, .nonibble
	ld [TempWildMonSpecies], a
	ld a, e
	ld [CurPartyLevel], a
	ld a, BATTLETYPE_FISH
	ld [wBattleType], a
	ld a, $2
	ret

.nonibble
	ld a, $1
	ret

.FailFish
	ld a, $80
	ret

.FishGotSomething
	ld a, $1
	ld [wFishResponse], a
	ld hl, Script_GotABite
	jr .queue_script_and_end

.FishNoBite
	ld a, $2
	jr .not_even_a_nibble

.FishNoFish
	xor a
.not_even_a_nibble
	ld [wFishResponse], a
	ld hl, Script_NotEvenANibble
.queue_script_and_end
	call QueueScript
	ld a, $81
	ret

Script_NotEvenANibble:
	scall Script_FishCastRod
	writetext Text_NotEvenANibble
	loademote EMOTE_SHADOW
	callasm PutTheRodAway
	closetext
	end

Script_GotABite:
	scall Script_FishCastRod
	callasm Fishing_CheckFacingUp
	sif true, then
		applymovement PLAYER, .Movement_FacingUp
	selse
		applymovement PLAYER, .Movement_NotFacingUp
	sendif
	pause 40
	applymovement PLAYER, .Movement_RestoreRod
	writetext Text_ABite
	callasm PutTheRodAway
	closetext
	randomwildmon
	startbattle
	reloadmapafterbattle
	end

.Movement_NotFacingUp
	fish_got_bite
	fish_got_bite
	fish_got_bite
	fish_got_bite
	show_emote
	step_end

.Movement_FacingUp
	fish_got_bite
	fish_got_bite
	fish_got_bite
	fish_got_bite
	step_sleep_1
	show_emote
	step_end

.Movement_RestoreRod
	hide_emote
	step_end

Fishing_CheckFacingUp:
	ld a, [PlayerDirection]
	and $c
	cp OW_UP
	ld a, $1
	jr z, .up
	xor a
.up
	ld [hScriptVar], a
	ret

Script_FishCastRod:
	reloadmappart
	loadvar hBGMapMode, $0
	special UpdateTimePals
	callasm LoadFishingGFX
	loademote EMOTE_SHOCK
	applymovement PLAYER, .Movement
	end

.Movement
	fish_cast_rod
	step_end

PutTheRodAway:
	xor a
	ld [hBGMapMode], a
	ld a, PERSON_ACTION_STAND
	ld [PlayerAction], a
	ld hl, ScriptFlags2
	res 0, [hl]
	call UpdateSprites
	jp ReplaceKrisSprite

Text_ABite:
	; Oh! A bite!
	text_jump Fishing_Bite

Text_NotEvenANibble:
	; Not even a nibble!
	text_jump Fishing_Nope

BikeFunction:
	call .TryBike
	and $7f
	ld [wFieldMoveSucceeded], a
	ret

.TryBike
	ld a, [wTileset]
	cp TILESET_SIDESCROLL
	jr z, .CannotUseBike
	call .CheckEnvironment
	jr c, .CannotUseBike
	ld a, [PlayerState]
	and a ; PLAYER_NORMAL
	jr z, .GetOnBike
	cp PLAYER_BIKE
	jr nz, .CannotUseBike

.GetOffBike
	ld hl, wBikeFlags
	bit 1, [hl]
	jr nz, .CantGetOffBike
	ld hl, Script_GetOffBike
	ld de, Script_GetOffBike_Register
	call .CheckIfRegistered
	ld a, BANK(Script_GetOffBike)
	jr .done

.CannotUseBike
	xor a
	ret

.GetOnBike
	ld hl, Script_GetOnBike
	ld de, Script_GetOnBike_Register
	call .CheckIfRegistered
	call QueueScript
	ld a, MUSIC_BICYCLE
	ld [wMapMusic], a
	ld [MusicFadeID], a
	xor a
	ld [MusicFadeID + 1], a
	ld a, $8
	ld [MusicFade], a
	ld a, $1
	ret

.CantGetOffBike
	ld hl, Script_CantGetOffBike
.done
	call QueueScript
	ld a, $1
	ret

.CheckIfRegistered
	ld a, [wUsingItemWithSelect]
	and a
	ret z
	ld h, d
	ld l, e
	ret

.CheckEnvironment
	call GetMapPermission
	call CheckOutdoorMap
	jr z, .ok
	cp CAVE
	jr z, .ok
	cp GATE
	jr nz, .nope
.ok
	call GetPlayerStandingTile
	and $f ; can't use our bike in a wall or on water
	ret z
.nope
	scf
	ret

Script_GetOnBike:
	reloadmappart
	special UpdateTimePals
	writecode VAR_MOVEMENT, PLAYER_BIKE
	writetext GotOnTheBikeText
	waitbutton
	closetext
	special ReplaceKrisSprite
	end

Script_GetOnBike_Register:
	writecode VAR_MOVEMENT, PLAYER_BIKE
	closetext
	special ReplaceKrisSprite
	end

Script_GetOffBike:
	reloadmappart
	special UpdateTimePals
	writecode VAR_MOVEMENT, PLAYER_NORMAL
	writetext GotOffTheBikeText
	waitbutton

FinishGettingOffBike:
	closetext
	special ReplaceKrisSprite
	fadetomapmusic $8
	end

Script_GetOffBike_Register:
	writecode VAR_MOVEMENT, PLAYER_NORMAL
	jump FinishGettingOffBike

Script_CantGetOffBike:
	jumptext .CantGetOffBikeText

.CantGetOffBikeText
	; You can't get off here!
	text_jump Bike_CantGetOff

GotOnTheBikeText:
	; got on the @ .
	text_jump Bike_GotOn

GotOffTheBikeText:
	; got off the @ .
	text_jump Bike_GotOff
