StartParkMinigameScript::
	; in: [hScriptVar]: selected minigame option
	; out: [hScriptVar]: 1 if the game started, 0 if the player had no funds
	scriptstartasm
	ld a, [hScriptVar]
	ld l, a
	xor a
	ld [hScriptVar], a
	ld h, a
	add hl, hl
	add hl, hl
	ld de, ParkMinigameGames
	add hl, de
	ld a, [hli]
	push hl
	call CheckParkMinigameMoney
	pop de
	ld hl, 0
	ret c
	ld a, [de]
	push de
	call InitializeParkMinigame
	pop hl
	inc hl
	inc hl
	ld a, [hld]
	cp -1
	jr nz, .not_time_of_day_based
	push hl
	callba GetTimeOfDay
	pop hl
	ld a, [TimeOfDay]
.not_time_of_day_based
	ld [wParkMinigameGameType], a
	ld a, [hl]
	ld [hScriptVar], a
	scriptstopasm
	waitsfx ;wait for the text's "click" sound to end
	special PlaceMoneyTopRight
	playwaitsfx SFX_TRANSACTION
	pushvar
	callasm BackupPlayerPokeballs
	writetext .keep_pokeballs_text
	sif true, then
		sif <, 7, then
			scall .one_mon_holding_ball
		selse
			writetext .mons_holding_balls_text
		sendif
	sendif
	popvar
	writetext .park_balls_given_text
	callasm GiveInitialParkBalls
	sif true
		writetext .stocked_balls_given_text
	writetext .good_luck_text
	waitsfx ;again, wait for the text's sound
	warpsound
	waitsfx
	warp PROVINCIAL_PARK_CONTEST, 19, 9
	spriteface PLAYER, DOWN
	writebyte 1
	callasm StartParkGameTimer
	end

.keep_pokeballs_text
	db TX_COMPRESSED, $74, $e1, $ec, $4e, $83, $1c, $b0, $c5, $23, $31
	db $0f, $02, $0b, $4b, $80, $e5, $8a, $f7, $bd
	db $40, $0a, $92, $3e, $9c, $04, $16, $a6, $ac, $e0
	db $c8, $f5, $a4, $e0, $8d, $5a, $97
	db $a9, $e2
.mons_holding_balls_text
	db TX_COMPRESSED, $74, $e1, $ec, $48, $7b, $98, $12, $58, $65, $a4
	db $de, $09, $0d, $12, $d3, $b7, $01, $05, $a5
	db $80, $07, $2b, $e1, $24, $d5
	db $9b, $c9, $0f, $72, $32, $73, $f9, $61, $0f
	db $a9, $e2
.one_mon_holding_ball
	scriptstartasm
	ld hl, wPartyMonNicknames - PKMN_NAME_LENGTH
	ld bc, PKMN_NAME_LENGTH
	ld a, [hScriptVar]
	rst AddNTimes
	ld d, h
	ld e, l
	call CopyName1
	scriptstopasm
	writetext .one_mon_holding_ball_text
	end
.one_mon_holding_ball_text
	db TX_COMPRESSED, $74, $e1, $ec, $48, $7b, $98, $12, $58, $65, $a4
	db $de, $09, $0e, $5a, $76, $e0, $20, $b4
	db $b0, $00, $e4, $c1
	db $a1, $e4, $87, $b9, $19, $39, $fc, $b0, $87
	db $d4, $f1, $00

.park_balls_given_text
	db TX_COMPRESSED, $62, $6b, $38, $08, $2d, $4e, $43
	db $f7, $9c, $c0
	deciram hScriptVar, 1, 3
	db TX_COMPRESSED, $9c, $3a, $b5, $28, $25, $bd, $ea
	db $3d, $4f, $10
.stocked_balls_given_text
	db TX_COMPRESSED, $ac, $99, $89, $35, $9c, $d5, $9c, $b4
	db $9b, $ca, $f7, $bd, $44, $c1, $61, $7e, $02, $0b, $4b
	db $54, $21, $66, $0c, $30, $b4, $88, $0c, $34, $0e, $d1
	db $ea, $78, $80

.good_luck_text
	db TX_COMPRESSED, $61, $04, $39, $9d, $69, $95, $2f, $40, $78, $6a
	db $78, $47, $4f, $50, $70, $e9, $d1, $dc, $d2
	db $65, $53, $3b, $6c, $a6, $03, $95, $f0, $93
	db $d4, $f1, $00

CheckParkMinigameMoney:
	; in: a: cost of the park minigame (x100)
	; out: carry if the player can't afford it, no carry (and money taken away) if the player can
	ld e, a
	ld d, 0
	ld bc, 100
	call Multiply16
	xor a
	ld hl, hMoneyTemp
	ld [hli], a
	ld a, d
	ld [hli], a
	ld [hl], e
	ld de, Money
	ld bc, hMoneyTemp
	callba CompareMoney
	ret c
	callba TakeMoney
	and a
	ret

InitializeParkMinigame:
	; in: a: duration of the minigame
	push af
	SetEngine ENGINE_PARK_MINIGAME
	pop af
	ld [wParkMinigameTotalTime], a
	push af
	lb bc, STOPWATCH_RESET, STOPWATCH_PARK_MINIGAME
	callba ReadStopwatch
	call CalculateRemainingParkMinigameTime
	xor a
	ld [wParkMinigameRemainingTime], a
	ld hl, wParkMinigameSpot1
	ld bc, wParkMinigameSpotsEnd - wParkMinigameSpot1
	call ByteFill
	ld hl, wParkMinigameSpot1Cooldown
	ld bc, wParkMinigameSpot2 - wParkMinigameSpot1
	ld d, PARK_MINIGAME_SPOT_COUNT
	pop af
.reset_cooldown_loop
	ld [hl], a
	add hl, bc
	dec d
	jr nz, .reset_cooldown_loop
	ld hl, wParkMinigameSavedHeldItems
	ld bc, MAX_BALLS * 2 + 8 ; size of wParkMinigameSavedHeldItems and wParkMinigameSavedBalls
	xor a
	jp ByteFill

CalculateRemainingParkMinigameTime:
	lb bc, STOPWATCH_READ_FORMATTED, STOPWATCH_PARK_MINIGAME
	callba ReadStopwatch
	ld a, b
	and a
	jr nz, .exceeded
	ld a, [wParkMinigameTotalTime]
	dec a
	sub c
	jr c, .exceeded
	ld c, a
	ld a, 59
	sub d
	ld d, a
	ld a, 100
	sub e
	ld e, a
	cp 100
	jr nz, .got_value
	ld e, 0
	inc d
	ld a, d
	cp 60
	jr nz, .got_value
	ld d, e ; e = 0 at this point
	inc c
.got_value
	ld hl, wParkMinigameRemainingTime + 1
	ld a, c
	ld [hli], a
	ld a, d
	ld [hli], a
	ld [hl], e
	or e
	or c
	ret
.exceeded
	xor a ; sets zero flag
	ld hl, wParkMinigameRemainingTime + 1
	ld [hli], a
	ld [hli], a
	ld [hl], a
	ret

BackupPlayerPokeballs:
	; this function will back up the player's balls in the balls pocket, and also the ones being held by the party
	; returns in [hScriptVar]: 0 if no mon was holding a ball, 1-6 if a specific mon was holding one, or something >6 if several mons were
	ld hl, NumBalls
	ld bc, BallsEnd - NumBalls
	ld de, wParkMinigameSavedBalls
	push hl
	rst CopyBytes
	pop hl
	xor a
	ld [hli], a
	ld [hl], $ff
	ld a, [wPartyCount]
	and a
	jr z, .done ;I have no idea of how this could ever happen.
	ld de, 0
	ld hl, PartyMon1Item
	ld bc, PARTYMON_STRUCT_LENGTH
.loop
	inc e
	ld a, [hl]
	and a
	jr z, .continue
	push de
	ld d, a
	call GetItemPocket
	cp BALL
	ld a, d
	pop de
	jr nz, .continue
	ld [hl], 0
	push hl
	push de
	ld hl, wParkMinigameSavedHeldItems - 1
	ld d, 0
	add hl, de
	pop de
	ld [hl], a
	pop hl
	ld a, d
	swap a
	add a, e
	ld d, a
.continue
	add hl, bc
	ld a, [wPartyCount]
	cp e
	jr nz, .loop
	ld a, d
.done
	ld [hScriptVar], a
	ret

GiveInitialParkBalls:
	; gives the player the initial stock of Park Balls (an amount indicated by [hScriptVar]), plus whatever stock of balls they had in store
	; returns in [hScriptVar], 0 if there were no balls in stock or non-zero if there were some
	; no bag overflow checking since the max amount of bag slots this will take up is 15
	ld de, 0
	ld c, e
.loop
	ld hl, ParkMinigameBallsList
	push de
	ld d, 0
	add hl, de
	add hl, de
	add hl, de
	pop de
	ld b, [hl]
	inc hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	inc b
	jr z, .done
	dec b
	ld a, [hl]
	and a
	jr z, .continue
	ld hl, Balls
	push bc
	ld b, 0
	add hl, bc
	add hl, bc
	pop bc
.inner_loop
	cp 100
	jr c, .not_over_99
	ld [hl], b
	inc hl
	ld [hl], 99
	inc hl
	inc c
	sub 99
	jr .inner_loop
.not_over_99
	ld [hl], b
	inc hl
	ld [hl], a
	inc c
	ld a, e
	and a
	jr z, .continue
	inc d
.continue
	inc e
	jr .loop
.done
	ld a, c
	ld [NumBalls], a
	add a, a
	add a, Balls & $ff
	ld l, a
	ld h, Balls >> 8
	jr nc, .no_pointer_carry
	inc h
.no_pointer_carry
	ld [hl], $ff
	ld a, d
	ld [hScriptVar], a
; since the park balls are given without the use of ReceiveItem
	ld hl, wItemsObtained + (PARK_BALL / 8)
	set (PARK_BALL % 8), [hl]
	ret

ParkMinigameBallsList:
	dbw PARK_BALL, hScriptVar
ParkMinigameSpecialBallsList:
	dbw POKE_BALL, wParkMinigamePokeBalls
	dbw GREAT_BALL, wParkMinigameGreatBalls
	dbw ULTRA_BALL, wParkMinigameUltraBalls
	dbw MASTER_BALL, wParkMinigameMasterBalls
	db -1

StoreAndRemoveParkBalls:
	; removes the player's balls (both from inventory and held items) and stores the "special" ones (i.e., non-Park balls) for later visits
	; returns in [hScriptVar]: 0 if no balls were stocked, 1 if some balls were stocked, or 2 if the player just had too many of some kind of ball (>255)
	ld a, [NumBalls]
	ld e, a
	ld d, 0
	ld bc, Balls
.pack_loop
	ld a, [bc]
	cp $ff
	jr z, .pack_done
	inc bc
	push bc
	push de
	ld hl, ParkMinigameSpecialBallsList
	ld e, 3
	call IsInArray
	pop de
	pop bc
	jr nc, .skip_pack_entry
	inc hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld a, [bc]
	and a
	jr z, .skip_pack_entry
	add a, [hl]
	jr nc, .pack_entry_did_not_overflow
	ld a, $ff
	set 1, d
.pack_entry_did_not_overflow
	ld [hl], a
	set 0, d
.skip_pack_entry
	inc bc
	dec e
	jr nz, .pack_loop
.pack_done
	ld a, [wPartyCount]
	and a
	jr z, .done ; ...if we somehow have an empty party
	ld e, a
	ld hl, PartyMon1Item
.party_loop
	ld a, [hl]
	and a
	jr z, .skip_party_member
	call GetItemPocket
	cp BALL
	jr nz, .skip_party_member
	ld a, [hl]
	ld [hl], 0
	push hl
	push de
	ld e, 3
	ld hl, ParkMinigameSpecialBallsList
	call IsInArray
	pop de
	jr nc, .done_party_member
	inc hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	set 0, d
	inc [hl]
	jr nz, .done_party_member
	set 1, d
	dec [hl]
.done_party_member
	pop hl
.skip_party_member
	ld bc, PARTYMON_STRUCT_LENGTH
	add hl, bc
	dec e
	jr nz, .party_loop
.done
	ld a, d
	cp 2
	jr c, .not_over_2
	ld a, 2
.not_over_2
	ld [hScriptVar], a
	ret

RestorePlayerPokeballs:
	; restores the player's balls that had been backed up at the beginning of the event
	; returns in [hScriptVar]: 0 if only the pack was restored, 1 if some previously held balls were restored, or 2 if the player had no room for some of those
	ld hl, wParkMinigameSavedBalls
	ld de, NumBalls
	ld bc, MAX_BALLS * 2 + 2
	rst CopyBytes
	lb de, 0, 6
	ld hl, wParkMinigameSavedHeldItems
.loop
	ld a, [hli]
	and a
	jr z, .continue
	ld [wCurItem], a
	ld a, 1
	ld [wItemQuantityChangeBuffer], a
	set 0, d
	push hl
	ld hl, NumItems
	call ReceiveItem
	pop hl
	jr c, .continue
	set 1, d
.continue
	dec e
	jr nz, .loop
	ld a, d
	cp 2
	jr c, .not_over_2
	ld a, 2
.not_over_2
	ld [hScriptVar], a
	ret

StartParkGameTimer:
	ld hl, wTimeEventCallback
	ld a, ParkGameOverworldLoop >> 8
	ld [hli], a
	ld a, ParkGameOverworldLoop & $ff
	ld [hli], a
	ld [hl], BANK(ParkGameOverworldLoop)
	ld hl, wStopwatchControl
	set STOPWATCH_PARK_MINIGAME, [hl]
	ret

ParkGameOverworldLoop:
	; if the minigame flag isn't set, the contest is over and we shouldn't be here
	CheckEngine ENGINE_PARK_MINIGAME
	jr z, .abort
	; if we somehow managed to exit the park, end the contest immediately
	; this prevents a lot of Safari Zone-like glitches
	ld hl, MapGroup
	ld a, [hli]
	cp GROUP_PROVINCIAL_PARK_CONTEST
	jr nz, .not_in_park
	ld a, [hl]
	cp MAP_PROVINCIAL_PARK_CONTEST
	jr z, .in_park
.not_in_park
	call EndParkMinigame
	ld hl, ParkGameOverScript
	jr RunParkMinigameScript
.in_park
	; error handlers were unnecessary -- check the time and see if the game is over
	call CalculateRemainingParkMinigameTime
	jr z, .game_over
	xor a
	ret
.abort
	call EndParkMinigame
	xor a
	ret
.game_over
	call EndParkMinigame
	ld hl, ParkMinigameTimeUpScript
	; fallthrough

RunParkMinigameScript:
	ld a, BANK(RunParkMinigameScript)
	jp CallScript

StopParkMinigameScript::
	callasm EndParkMinigame
	jump ParkGameOverScript

EndParkMinigame:
	xor a
	ld hl, wTimeEventCallback
	ld [hli], a
	ld [hli], a
	ld [hl], a
	ld hl, wStopwatchControl
	res STOPWATCH_PARK_MINIGAME, [hl]
	ret

ParkMinigameTimeUpScript:
	playmusic MUSIC_NONE
	playwaitsfx SFX_ELEVATOR_END
	showtext .time_is_up_text
	jump ParkGameOverScript
.time_is_up_text
	db TX_COMPRESSED, $e1, $ad, $c6, $70, $0c, $9c, $81, $c8, $49, $cf
	db $17, $9c, $a1, $69, $72, $c6, $f9, $c1, $a2, $34, $23
	db $8d, $3c, $40

GetParkSpotAddress:
	; returns in hl the address of the park spot a (1 to PARK_MINIGAME_SPOT_COUNT)
	push bc
	ld hl, wParkMinigameSpot1 - (wParkMinigameSpot2 - wParkMinigameSpot1)
	ld bc, wParkMinigameSpot2 - wParkMinigameSpot1
	rst AddNTimes
	pop bc
	ret

ResetParkSpot:
	; resets park spot a (1 to PARK_MINIGAME_SPOT_COUNT) to a random cooldown between 44 seconds and 1:15 minutes
	push hl
	push bc
	push de
	push af
	call CalculateRemainingParkMinigameTime
	pop af
	call GetParkSpotAddress
	xor a
	ld [hli], a
	ld bc, wParkMinigameRemainingTime + 1
	ld a, [bc]
	ld d, a
	inc bc
	ld a, [bc]
	ld e, a
	inc bc
	ld a, [bc]
	ld c, a
	call Random
	and 31
	add a, 44
	cp 60
	ld b, a
	jr c, .not_over_one_minute
	sub 60
	ld b, a
	ld a, d
	and a
	jr z, .remaining_time_too_low
	dec d
.not_over_one_minute
	ld a, e
	sub b
	ld e, a
	jr nc, .calculated
	add a, 60
	ld e, a
	ld a, d
	and a
	jr z, .remaining_time_too_low
	dec d
.calculated
	ld a, d
	ld [hli], a
	ld a, e
	ld [hli], a
	ld [hl], c
.done
	pop de
	pop bc
	pop hl
	ret
.remaining_time_too_low
	ld [hli], a
	ld [hli], a
	ld [hl], a
	jr .done

GetRandomPercentage:
	call Random
	cp 200
	jr nc, .not_in_range
	srl a
	ret
.not_in_range
	cp 250
	jr nc, GetRandomPercentage
	ld a, [hRandomAdd]
	rra
	ld a, [hRandomSub]
	rla
	sub 144 ;400 & $ff
	ret

GenerateParkSpot:
	; generates a park spot's contents; park spot passed in a (1 to PARK_MINIGAME_SPOT_COUNT)
	ld d, a
	ld bc, 5
	ld a, [wParkMinigameGameType]
	ld hl, ParkMinigameTables
	rst AddNTimes
	call GetRandomPercentage
	cp [hl]
	inc hl
	jr nc, .generate_item
	call .select
	ld b, [hl]
	ld a, d
	call GetParkSpotAddress
	ld a, b
	ld [hli], a
	call GetMaxPartyLevel
	push hl
	ld hl, hMultiplicand + 2
	ld [hld], a
	xor a
	ld [hld], a
	ld [hl], a
	pop hl
	call Random
	and 31
	add a, 50
	ld [hMultiplier], a
	predef Multiply
	ld a, 100
	ld [hDivisor], a
	ld b, 4
	predef Divide
	ld a, [hQuotient + 2]
	and a
	jr nz, .level_OK
	inc a
.level_OK
	ld [hli], a
	push hl
	call GenerateParkMinigameDVs
	pop hl
	ld a, b
	ld [hli], a
	ld [hl], c
	ret

.generate_item
	inc hl
	inc hl
	call .select
	ld a, [hl]
	push af
	and $f
	ld c, a
	pop af
	swap a
	and $f
	jr z, .empty_spot
	ld b, a
	; 1/32 chance of improving the item kind; 2/32 of increasing the amount given
	call Random
	and 31
	cp 3
	jr nc, .done_improving_item
	and a
	jr nz, .no_item_kind_improvement
	inc b
	ld c, 1 ; if we improve the kind of item, only give 1 of the improved item
.no_item_kind_improvement
	add a, c
	ld c, a
.done_improving_item
	dec b
	ld a, b
	cp .items_end - .items
	jr c, .valid_item
	ld a, (.items_end - .items) - 1
.valid_item
	add a, .items & $ff
	ld l, a
	ld h, .items >> 8
	jr nc, .no_items_overflow
	inc h
.no_items_overflow
	ld b, [hl]
	ld a, d
	call GetParkSpotAddress
	ld a, -1
	ld [hli], a
	ld a, b
	ld [hli], a
	ld [hl], c
	ret
.items
	db POKE_BALL
	db GREAT_BALL
	db ULTRA_BALL
	db MASTER_BALL
.items_end

.empty_spot
	ld a, d
	jp ResetParkSpot

.select
	ld a, [hli]
	ld h, [hl]
	ld l, a
	call GetRandomPercentage
.loop
	sub [hl]
	inc hl
	ret c
	inc hl
	jr .loop

GetMaxPartyLevel:
	ld a, [wPartyCount]
	and a
	ret z
	push hl
	push de
	push bc
	ld e, a
	xor a
	ld bc, PARTYMON_STRUCT_LENGTH
	ld hl, PartyMon1Level
.loop
	ld d, [hl]
	cp d
	jr nc, .not_higher
	ld a, d
.not_higher
	add hl, bc
	dec e
	jr nz, .loop
	pop bc
	pop de
	pop hl
	ret

GenerateParkMinigameDVs:
	; returns the generated DVs in bc
	; note that this results in overall "better" DVs, along with a 1/512 chance of a shiny (16x the usual)
	; the exact constraints imposed are that the HP DV is 7, 8, 11 or 15, and that the Defense, Speed and Special DVs are all 8 or higher
	call Random
	ld c, a
	ld a, [hRandomAdd]
	ld b, a
	set 3, b
	and $11
	jr z, .try_shiny
	ld a, c
	or $99
	ld c, a
	ret
.try_shiny
	set 4, b
	ld a, c
	or $88
	and $ee
	ld c, a
	ret

LoadCurrentParkSpot:
	; loads the spot indicated by [hScriptVar] (1 to PARK_MINIGAME_SPOT_COUNT) into wParkMinigameCurrentSpot
	ld a, [hScriptVar]
	ld [wParkMinigameCurrentSpotNumber], a
	call GetParkSpotAddress
	ld de, wParkMinigameCurrentSpot
	ld bc, wParkMinigameSpot2 - wParkMinigameSpot1
	rst CopyBytes
	ret

CheckCurrentParkSpotCooldownExpired:
	; checks if the cooldown of the current park spot has expired
	; returns in [hScriptVar]
	ld hl, wParkMinigameCurrentSpot
	ld a, [hli]
	and a
	jr nz, .expired
	ld a, [hli]
	ld c, a
	ld a, [hli]
	ld d, a
	ld e, [hl]
	ld hl, wParkMinigameRemainingTime + 1
	ld a, [hli]
	cp c
	jr c, .expired
	jr nz, .not_expired
	ld a, [hli]
	cp d
	jr c, .expired
	jr nz, .not_expired
	ld a, [hl]
	cp e
	jr c, .expired
	jr z, .expired
.not_expired
	xor a
	jr .done
.expired
	ld a, 1
.done
	ld [hScriptVar], a
	ret

ParkSpotScript::
	; assuming that [hScriptVar] already contains the spot's number
	callasm LoadCurrentParkSpot
	copybytetovar wParkMinigameCurrentSpotFlags
	iffalse .empty_spot
	if_equal -1, .item
	; wild mon
	pokenamemem 0, 0
	opentext
	writetext .pokemon_text
	yesorno
	sif false, then
		closetext
		end
	sendif
	randomwildmon
	scriptstartasm
	ld hl, wParkMinigameCurrentSpot
	ld a, [hli]
	ld [TempWildMonSpecies], a
	ld a, [hl]
	ld [CurPartyLevel], a
	scriptstopasm
	startbattle
	reloadmap
	if_equal 1, .defeated
	scriptstartasm
	ld a, [wParkMinigameCurrentSpotNumber]
	call ResetParkSpot
	ld hl, 0
	ret
.pokemon_text
	db TX_COMPRESSED, $50, $20, $37, $01, $e1, $f4, $73, $00
	deciram wParkMinigameCurrentSpotNumber, 1, 2
	db TX_COMPRESSED, $71
	db $78, $a4, $9a, $ce, $0d, $13
	db $40, $74, $1b, $d1, $cc, $00
	deciram wParkMinigameCurrentSpotLevel, 1, 3
	db TX_COMPRESSED, $94, $f2
	db $79, $26, $b3, $f4, $12, $db, $be, $9c, $1d, $d2
	db $d3, $40

.item
	copybytetovar wParkMinigameCurrentSpotItem
	itemtotext 0, 0
	opentext
	writetext .item_text
	yesorno
	sif false, then
		closetext
		end
	sendif
	scriptstartasm
	ld hl, wParkMinigameCurrentSpotItem
	ld a, [hli]
	ld d, [hl]
	ld e, a
	push de
	ld a, [wParkMinigameCurrentSpotNumber]
	call ResetParkSpot
	pop de
	ld a, e
	ld [wCurItem], a
	ld a, d
	ld [wItemQuantityChangeBuffer], a
	push de
	ld hl, NumItems
	call ReceiveItem
	ld hl, .no_room_for_items
	pop bc
	ret nc
	ld a, c
	ld [wNamedObjectIndexBuffer], a
	call GetItemName
	ld a, b
	ld [wItemQuantityChangeBuffer], a
	scriptstopasm
	writetext .got_item_text
	playwaitsfx SFX_ITEM
	waitbutton
	closetext
	end
.no_room_for_items
	jumptext .no_room_for_items_text
.item_text
	start_asm
	ld a, [wParkMinigameCurrentSpotQuantity]
	cp 2
	ld hl, .item_text_singular
	ret c
	ld hl, .item_text_plural
	ret
.item_text_singular
	db TX_COMPRESSED, $50, $20, $37, $01, $e1, $f4, $73, $00
	deciram wParkMinigameCurrentSpotNumber, 1, 2
	db TX_COMPRESSED, $71
	db $78, $a4, $8d, $11, $02, $03, $71, $90, $92, $f4, $64
	db $40, $01, $6a, $53, $cb
	db $cf, $70, $8d, $95, $28, $3b, $8d, $08, $a5
	db $a6, $80
.item_text_plural
	db TX_COMPRESSED, $50, $20, $37, $01, $e1, $f4, $73, $00
	deciram wParkMinigameCurrentSpotNumber, 1, 2
	db TX_COMPRESSED, $71
	db $78, $a4, $8d, $11, $02, $03, $71, $90, $92, $f4, $64
	db $40, $07, $30
	deciram wParkMinigameCurrentSpotQuantity, 1, 2
	db TX_COMPRESSED, $94, $f2, $47
	db $9e, $e1, $1b, $2a, $52, $d2, $6f, $c6, $84, $52
	db $d3, $40
.got_item_text
	start_asm
	ld a, [wItemQuantityChangeBuffer]
	dec a
	ld hl, .got_item_text_singular
	ret z
	ld hl, .got_item_text_plural
	ret
.got_item_text_singular
	db TX_COMPRESSED, $e1, $1b, $2a, $47, $98, $d0, $89, $69
	db $37, $81, $c8
	db $f5, $34
.got_item_text_plural
	db TX_COMPRESSED, $e1, $1b, $2a, $47, $98, $d0, $89, $69
	db $37, $9c, $c0
	deciram wItemQuantityChangeBuffer, 1, 3
	db TX_COMPRESSED, $80, $e4, $23
	db $d4, $d0
.no_room_for_items_text
	db TX_COMPRESSED, $72, $85, $a8, $9d, $cc, $20, $62, $c2, $17
	db $de, $c0, $2b, $96, $93, $83, $b3, $7d, $1f, $88
	db $0a, $ab, $49, $82, $95, $1a, $ce, $58, $50, $83, $cf
	db $a9, $a0

.empty_spot
	callasm CheckCurrentParkSpotCooldownExpired
	sif false
		jumptext .empty_spot_text
	scriptstartasm
	ld a, [wParkMinigameCurrentSpotNumber]
	ld [hScriptVar], a
	call GenerateParkSpot
	ld hl, ParkSpotScript
	ret

.empty_spot_text
	db TX_COMPRESSED, $50, $20, $37, $01, $e1, $f4, $73, $00
	deciram wParkMinigameCurrentSpotNumber, 1, 2
	db TX_COMPRESSED, $71
	db $78, $a4, $9c, $40, $80, $dc, $1a, $21, $be, $22, $c1, $53
	db $c1, $4d

.defeated
	callasm EndParkMinigame
	opentext
	writetext ParkMinigame_DefeatedText
	closetext
	special HealParty
	; fallthrough

ParkGameOverScript:
	warpsound
	special FadeOutPalettes
	waitsfx
	warp PROVINCIAL_PARK_GATE, 0, 4
	spriteface PLAYER, RIGHT
	applymovement PLAYER, .move_to_employee
	spriteface PLAYER, UP
	opentext
	writetext .welcome_back_text
	callasm StoreAndRemoveParkBalls
	writetext .returned_balls_text
	sif true, then
		writetext .stocked_balls_text
		sif >=, 2
			writetext .wasted_balls_text
	sendif
	callasm RestorePlayerPokeballs
	writetext .restored_balls_text
	sif =, 1
		writetext .restored_held_balls_text
	sif =, 2
		writetext .wasted_held_balls_text
	clearflag ENGINE_PARK_MINIGAME
	jumptext .final_text

.move_to_employee
	rept 6
		step_right
	endr
	step_end

.welcome_back_text
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $57, $ac, $a9
	db $7a, $9e, $20
.final_text
	db TX_COMPRESSED, $6d, $0b, $e7, $37, $9d, $19, $38
	db $d3, $c4

.returned_balls_text
	db TX_COMPRESSED, $72, $85, $a7, $0f, $62, $74, $18, $e5
	db $85, $e5, $9d, $b4, $b2, $4b, $49, $ce, $1d, $5a
	db $90, $01, $2d, $ef, $51, $2d, $3b, $70, $10, $5a
	db $7b, $23, $c9, $29, $91, $a4, $1f
	db $a9, $e2
.stocked_balls_text
	db TX_COMPRESSED, $74, $e1, $ec, $de, $a0, $31, $48, $cc, $44, $b4
	db $9b, $cb, $34, $5c, $0e, $12, $d2, $71, $5e, $f7, $a8
	db $01, $69, $db, $80, $82, $d4, $c0, $2d, $13, $30, $64
	db $7a, $d2, $70, $46, $ad, $4a, $60, $15, $c0, $41, $69
	db $6a, $88, $74, $ed, $c0, $61, $a0, $77
	db $ea, $78, $80
.wasted_balls_text
	db TX_COMPRESSED, $72, $85, $a9, $80, $5a, $26, $63, $e1, $59, $c0
	db $e0, $f5, $a4, $df, $96, $9d, $24, $75, $19, $d2
	db $00, $52, $33, $11, $f8, $80, $c7, $4e, $1e, $ca, $71, $a4
	db $5b, $d6, $95, $86, $a9, $b5, $68, $28, $80, $be, $7e
	db $a7, $88

.restored_balls_text
	db TX_COMPRESSED, $62, $6b, $39, $ab, $39, $69, $38, $0e
	db $57, $84, $b7, $bd, $44, $b4, $ed, $c7, $51, $48, $c4
	db $58, $0c, $02, $b8, $08, $2d, $4a, $92, $64, $80, $82
	db $d3, $c6, $4b, $35, $9e, $65, $a4, $e0, $8d, $5a, $97
	db $a9, $e2
.restored_held_balls_text
	db TX_COMPRESSED, $74, $46, $7a, $d6, $46, $4e, $65, $a4, $e0, $48, $68
	db $7a, $d3, $b7, $01, $05, $a5, $c0, $72, $be, $12
	db $2a, $a8, $d6, $71, $21, $ee, $46, $4e
	db $7d, $4f, $10
.wasted_held_balls_text
	db TX_COMPRESSED, $72, $85, $a9, $90, $92, $99, $13, $a0, $c7, $16, $10
	db $be, $f6, $01, $5c, $b4, $9c, $09, $0d, $12, $d3
	db $b6, $00, $10, $5a, $5c, $07, $2b, $e1, $24, $a8, $d6
	db $6f, $24, $3d, $c8, $c9, $cf, $e5, $a4, $2d, $72, $9f
	db $80, $40, $63, $a7, $0f, $65, $38, $d2, $2e, $5a, $56, $1a
	db $9e, $40, $5f, $38, $1c, $25, $a4, $df, $9b, $56, $82
	db $f5, $3c, $40

ParkMinigame_DefeatedText:
	db TX_COMPRESSED, $72, $85, $a9, $ab, $38, $16, $ae, $07
	db $07, $9a, $46, $ab, $e9, $c0, $72, $be, $12, $53
	db $c0, $01, $ca, $16, $a5, $43, $ef, $62, $b3, $97, $aa, $46
	db $47, $ac, $32, $d2, $70, $c9, $6b, $d2, $32, $7e
	db $a7, $88

