Script_CombosBook:
	ldtx hl, CombosBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text CombosBookText
	end_dialog
	end_script
	ret

Script_EnergyTransBook:
	ldtx hl, EnergyTransBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text EnergyTransBookText
	end_dialog
	end_script
	ret

Script_ToxicGasBook:
	ldtx hl, ToxicGasBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text ToxicGasBookText
	end_dialog
	end_script
	ret

Script_RainDanceBook:
	ldtx hl, RainDanceBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text RainDanceBookText
	end_dialog
	end_script
	ret

Script_SelfdestructBook:
	ldtx hl, SelfdestructBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SelfdestructBookText
	end_dialog
	end_script
	ret

Script_DamageSwapBook:
	ldtx hl, DamageSwapBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text DamageSwapBookText
	end_dialog
	end_script
	ret

Script_RemoveEnergiesBook:
	ldtx hl, RemoveEnergiesBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text RemoveEnergiesBookText
	end_dialog
	end_script
	ret

Script_PrehistoricPowerBook:
	ldtx hl, PrehistoricPowerBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PrehistoricPowerBookText
	end_dialog
	end_script
	ret

Script_WeaknessResistanceBook:
	ldtx hl, WeaknessResistanceBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text WeaknessResistanceBookText
	end_dialog
	end_script
	ret

Script_TeamGRBook:
	ldtx hl, TeamGRBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text TeamGRBookText
	end_dialog
	end_script
	ret

Script_GameCenterBook:
	ldtx hl, GameCenterBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GameCenterBookText
	end_dialog
	end_script
	ret

Script_SamePokemonBook:
	ldtx hl, SamePokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SamePokemonBookText
	end_dialog
	end_script
	ret

Script_RockGroundPokemonBook:
	ldtx hl, RockGroundPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text RockGroundPokemonBookText
	end_dialog
	end_script
	ret

Script_FightingPokemonSurefireMethodBook:
	ldtx hl, FightingPokemonSurefireMethodBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FightingPokemonSurefireMethodBookText
	end_dialog
	end_script
	ret

Script_FightingPokemonFormidableOpponentsBook:
	ldtx hl, FightingPokemonFormidableOpponentsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FightingPokemonFormidableOpponentsBookText
	end_dialog
	end_script
	ret

Script_FightingPokemonBook:
	ldtx hl, FightingPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FightingPokemonBookText
	end_dialog
	end_script
	ret

Script_FightingPokemonCombosBook:
	ldtx hl, FightingPokemonCombosBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FightingPokemonCombosBookText
	end_dialog
	end_script
	ret

Script_FightingPokemonDeckBuildingBook:
	ldtx hl, FightingPokemonDeckBuildingBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FightingPokemonDeckBuildingBookText
	end_dialog
	end_script
	ret

Script_PlantlikePokemonBook:
	ldtx hl, PlantlikePokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PlantlikePokemonBookText
	end_dialog
	end_script
	ret

Script_GrassPokemonBreederBook:
	ldtx hl, GrassPokemonBreederBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GrassPokemonBreederBookText
	end_dialog
	end_script
	ret

Script_GrassPokemonDeckBuildingBook:
	ldtx hl, GrassPokemonDeckBuildingBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GrassPokemonDeckBuildingBookText
	end_dialog
	end_script
	ret

Script_SciencePokemonBook:
	ldtx hl, SciencePokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SciencePokemonBookText
	end_dialog
	end_script
	ret

Script_SciencePokemonSurefireMethodBook:
	ldtx hl, SciencePokemonSurefireMethodBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SciencePokemonSurefireMethodBookText
	end_dialog
	end_script
	ret

Script_SciencePokemonDeckBuildingBook:
	ldtx hl, SciencePokemonDeckBuildingBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SciencePokemonDeckBuildingBookText
	end_dialog
	end_script
	ret

Script_WaterPokemonBook:
	ldtx hl, WaterPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text WaterPokemonBookText
	end_dialog
	end_script
	ret

Script_WaterPokemonAttacksBook:
	ldtx hl, WaterPokemonAttacksBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text WaterPokemonAttacksBookText
	end_dialog
	end_script
	ret

Script_PsyduckGolduckBook:
	ldtx hl, PsyduckGolduckBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PsyduckGolduckBookText
	end_dialog
	end_script
	ret

Script_FirePokemonBook:
	ldtx hl, FirePokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FirePokemonBookText
	end_dialog
	end_script
	ret

Script_FirePokemonAttacksBook:
	ldtx hl, FirePokemonAttacksBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FirePokemonAttacksBookText
	end_dialog
	end_script
	ret

Script_FirePokemonDeckBuildingBook:
	ldtx hl, FirePokemonDeckBuildingBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text FirePokemonDeckBuildingBookText
	end_dialog
	end_script
	ret

Script_LightningPokemonBook:
	ldtx hl, LightningPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LightningPokemonBookText
	end_dialog
	end_script
	ret

Script_LightningPokemonDeckBuildingBook:
	ldtx hl, LightningPokemonDeckBuildingBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LightningPokemonDeckBuildingBookText
	end_dialog
	end_script
	ret

Script_BirdPokemonBook:
	ldtx hl, BirdPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text BirdPokemonBookText
	end_dialog
	end_script
	ret

Script_PsychicPokemonBook:
	ldtx hl, PsychicPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PsychicPokemonBookText
	end_dialog
	end_script
	ret

Script_PsychicPokemonPokemonPowerBook:
	ldtx hl, PsychicPokemonPokemonPowerBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PsychicPokemonPokemonPowerBookText
	end_dialog
	end_script
	ret

Script_UsefulButtonsBook:
	ldtx hl, UsefulButtonsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text UsefulButtonsBookText
	end_dialog
	end_script
	ret

Script_ColorlessPokemonBook:
	ldtx hl, ColorlessPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text ColorlessPokemonBookText
	end_dialog
	end_script
	ret

Script_CardPopBook:
	ldtx hl, CardPopBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text CardPopBookText
	end_dialog
	end_script
	ret

Script_EnergyCardColorsBook:
	ldtx hl, EnergyCardColorsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text EnergyCardColorsBookText
	end_dialog
	end_script
	ret

Script_LegendaryPokemonCardsVol1Book:
	ldtx hl, LegendaryPokemonCardsVol1BookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LegendaryPokemonCardsVol1BookText
	end_dialog
	end_script
	ret

Script_LegendaryPokemonCardsVol2Book:
	ldtx hl, LegendaryPokemonCardsVol2BookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LegendaryPokemonCardsVol2BookText
	end_dialog
	end_script
	ret

Script_LegendaryPokemonCardsVol3Book:
	ldtx hl, LegendaryPokemonCardsVol3BookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LegendaryPokemonCardsVol3BookText
	end_dialog
	end_script
	ret

Script_LegendaryPokemonCardsVol4Book:
	ldtx hl, LegendaryPokemonCardsVol4BookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text LegendaryPokemonCardsVol4BookText
	end_dialog
	end_script
	ret

Script_GrandMastersBook:
	ldtx hl, GrandMastersBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GrandMastersBookText
	end_dialog
	end_script
	ret

Script_CoinsBook:
	ldtx hl, CoinsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text CoinsBookText
	end_dialog
	end_script
	ret

Script_PhantomCardsBook:
	ldtx hl, PhantomCardsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text PhantomCardsBookText
	end_dialog
	end_script
	ret

Script_DarkPokemonBook:
	ldtx hl, DarkPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text DarkPokemonBookText
	end_dialog
	end_script
	ret

Script_AugmentBasicPokemonBook:
	ldtx hl, AugmentBasicPokemonBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text AugmentBasicPokemonBookText
	end_dialog
	end_script
	ret

Script_GRKingCardsBook:
	ldtx hl, GRKingCardsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GRKingCardsBookText
	end_dialog
	end_script
	ret

Script_SpecialEnergyBook:
	ldtx hl, SpecialEnergyBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text SpecialEnergyBookText
	end_dialog
	end_script
	ret

Script_GetCoinsBook:
	ldtx hl, GetCoinsBookName
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GetCoinsBookText
	end_dialog
	end_script
	ret

OverworldTcg_MapHeader:
	db OVERWORLD_MAP_GFX_TCG
	dba OverworldTcg_MapScripts
	db MUSIC_OVERWORLD

OverworldTcg_MapScripts:
	dbw OWMODE_MUSIC_PRELOAD, Func_40474
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_4048a
	dbw OWMODE_WARP_FADE_OUT_PRELOAD, Func_4053b
	dbw OWMODE_WARP_END_SFX, Func_4053e
	db $ff ; end

Func_40474:
	ld a, [wPrevMap]
	cp MAP_OVERHEAD_ISLANDS
	jr z, .asm_40482
	cp MAP_TCG_AIRPORT
	jr z, .asm_40482
	scf
	ccf
	ret

.asm_40482
	ld a, MUSIC_GR_BLIMP
	ld [wNextMusic], a
	scf
	ccf
	ret

Func_4048a:
	xor a
	farcall InitOWObjects
	lb de,  1, 1
	lb bc, 11, 1
	farcall FillBoxInBGMapWithZero
	ld a, [wCurOWLocation]
	ld [wPlayerOWLocation], a

	ld a, NPC_VOLCANO_SMOKE_TCG
	lb de, 120, 0
	ld b, NORTH
	farcall LoadOWObject

	ld a, [wCurOWLocation]
	call PrintTCGIslandLocationName

	ld a, [wPrevMap]
	cp MAP_TCG_AIRPORT
	jr z, .gr_blimp_cutscene
	cp MAP_OVERHEAD_ISLANDS
	jr z, .gr_blimp_cutscene

	; this is the case where player is
	; on OW map and navigating
	ld a, [wPlayerOWObject]
	lb de, 0, 0
	ld b, SOUTH
	farcall LoadOWObject
	ld a, [wCurOWLocation]
	call PlacePlayerInTCGIslandLocation

	ld a, NPC_CURSOR_TCG
	lb de, 0, 0
	ld b, NORTH
	farcall LoadOWObject
	ld a, [wCurOWLocation]
	call UpdateTCGIslandCursorPosition

	ld a, OWMODE_SCRIPT
	ld [wOverworldMode], a
	ld a, BANK(HandleTCGIslandInput)
	ld [wOverworldScriptBank], a
	ld hl, HandleTCGIslandInput
	ld a, l
	ld [wOverworldScriptPointer + 0], a
	ld a, h
	ld [wOverworldScriptPointer + 1], a
	scf
	ret

.gr_blimp_cutscene
	ld bc, TILEMAP_001
	lb de, 0, 0
	farcall Func_12c0ce

	ld a, OWMODE_SCRIPT
	ld [wOverworldMode], a
	ld a, BANK(DoGRBlimpMovement_TCGIsland)
	ld [wOverworldScriptBank], a
	ld hl, DoGRBlimpMovement_TCGIsland
	ld a, l
	ld [wOverworldScriptPointer + 0], a
	ld a, h
	ld [wOverworldScriptPointer + 1], a

	ld a, [wPrevMap]
	cp MAP_OVERHEAD_ISLANDS
	jr z, .asm_40528
	ld a, NPC_GR_BLIMP
	lb de, 80, 120
	ld b, EAST
	farcall LoadOWObject
	scf
	ret
.asm_40528
	ld a, NPC_GR_BLIMP
	lb de, 144, 96
	ld b, WEST
	farcall LoadOWObject
	ld a, $00
	call Func_338f
	scf
	ccf
	ret

Func_4053b:
	scf
	ccf
	ret

Func_4053e:
	ld a, [wTempPrevMap]
	cp MAP_TCG_AIRPORT
	jr z, .asm_4054b
	cp MAP_OVERHEAD_ISLANDS
	jr z, .asm_40569
	scf
	ret

.asm_4054b
	ld a, 10
	ld c, 7
	farcall InitMusicFadeOut
	farcall MusicFadeOut
	xor a
	call PlaySong
	ld a, 7
	call SetVolume
	ld a, SFX_GR_BLIMP_HATCH_OPEN
	call PlaySFX
	farcall WaitForSFXToFinish
.asm_40569
	scf
	ccf
	ret

HandleTCGIslandInput:
	farcall Func_d683
	farcall DeliverMailFromQueue
	call WaitPalFading
.loop_input
	call DoFrame
	call UpdateRNGSources
	call HandleTCGIslandDirectionalInput
	ldh a, [hKeysPressed]
	bit B_PAD_A, a
	jr z, .loop_input
	call Func_406d1
	xor a
	call PlaySFX
	call Func_40682
	ret

PlacePlayerInTCGIslandLocation:
	sla a ; *2
	ld hl, TCGIslandLocationPositions
	add_hl_a
	ld a, [hli]
	ld e, [hl]
	ld d, a
	ld a, [wPlayerOWObject]
	farcall SetOWObjectPosition
	ret

UpdateTCGIslandCursorPosition:
	sla a ; *2
	ld hl, TCGIslandLocationPositions
	add_hl_a
	ld a, [hli]
	ld d, a
	ld a, [hl]
	sub 12
	ld e, a
	ld a, NPC_CURSOR_TCG
	farcall SetOWObjectPosition
	ret

HandleTCGIslandDirectionalInput:
	lb bc, 4, 0
	ldh a, [hKeysPressed]
.loop_shift
	sla a
	jr c, .got_key
	inc c
	dec b
	jr nz, .loop_shift
	ret

.got_key
	ld a, SFX_CURSOR
	call PlaySFX
	ld a, [wCurOWLocation]
	ld b, a
	sla a
	sla a ; *4
	ld hl, .LocationConnections
	add_hl_a
	ld a, c
	add_hl_a
	ld a, [hl]
	cp b
	jr z, .done
	ld [wCurOWLocation], a
	call UpdateTCGIslandCursorPosition
	ld a, [wCurOWLocation]
	call PrintTCGIslandLocationName
.done
	ret

.LocationConnections:
	; OWMAP_MASON_LABORATORY
	db OWMAP_MASON_LABORATORY ; down
	db OWMAP_LIGHTNING_CLUB ; up
	db OWMAP_MASON_LABORATORY ; left
	db OWMAP_FIGHTING_CLUB ; right

	; OWMAP_ISHIHARAS_HOUSE
	db OWMAP_ROCK_CLUB ; down
	db OWMAP_ISHIHARAS_HOUSE ; up
	db OWMAP_ISHIHARAS_HOUSE ; left
	db OWMAP_TCG_CHALLENGE_HALL ; right

	; OWMAP_LIGHTNING_CLUB
	db OWMAP_FIGHTING_CLUB ; down
	db OWMAP_ROCK_CLUB ; up
	db OWMAP_MASON_LABORATORY ; left
	db OWMAP_POKEMON_DOME ; right

	; OWMAP_PSYCHIC_CLUB
	db OWMAP_GRASS_CLUB ; down
	db OWMAP_FIRE_CLUB ; up
	db OWMAP_POKEMON_DOME ; left
	db OWMAP_SCIENCE_CLUB ; right

	; OWMAP_ROCK_CLUB
	db OWMAP_LIGHTNING_CLUB ; down
	db OWMAP_ISHIHARAS_HOUSE ; up
	db OWMAP_ROCK_CLUB ; left
	db OWMAP_POKEMON_DOME ; right

	; OWMAP_FIGHTING_CLUB
	db OWMAP_TCG_AIRPORT ; down
	db OWMAP_LIGHTNING_CLUB ; up
	db OWMAP_MASON_LABORATORY ; left
	db OWMAP_TCG_AIRPORT ; right

	; OWMAP_GRASS_CLUB
	db OWMAP_WATER_CLUB ; down
	db OWMAP_SCIENCE_CLUB ; up
	db OWMAP_PSYCHIC_CLUB ; left
	db OWMAP_GRASS_CLUB ; right

	; OWMAP_SCIENCE_CLUB
	db OWMAP_GRASS_CLUB ; down
	db OWMAP_FIRE_CLUB ; up
	db OWMAP_PSYCHIC_CLUB ; left
	db OWMAP_SCIENCE_CLUB ; right

	; OWMAP_WATER_CLUB
	db OWMAP_TCG_AIRPORT ; down
	db OWMAP_GRASS_CLUB ; up
	db OWMAP_TCG_AIRPORT ; left
	db OWMAP_WATER_CLUB ; right

	; OWMAP_FIRE_CLUB
	db OWMAP_SCIENCE_CLUB ; down
	db OWMAP_FIRE_CLUB ; up
	db OWMAP_PSYCHIC_CLUB ; left
	db OWMAP_SCIENCE_CLUB ; right

	; OWMAP_TCG_AIRPORT
	db OWMAP_TCG_AIRPORT ; down
	db OWMAP_POKEMON_DOME ; up
	db OWMAP_FIGHTING_CLUB ; left
	db OWMAP_WATER_CLUB ; right

	; OWMAP_TCG_CHALLENGE_HALL
	db OWMAP_POKEMON_DOME ; down
	db OWMAP_TCG_CHALLENGE_HALL ; up
	db OWMAP_ISHIHARAS_HOUSE ; left
	db OWMAP_PSYCHIC_CLUB ; right

	; OWMAP_POKEMON_DOME
	db OWMAP_FIGHTING_CLUB ; down
	db OWMAP_TCG_CHALLENGE_HALL ; up
	db OWMAP_ROCK_CLUB ; left
	db OWMAP_PSYCHIC_CLUB ; right

PrintTCGIslandLocationName:
	lb de, 1, 1
	ldtx hl, EmptyLocationNameText
	call InitTextPrinting_ProcessTextFromIDVRAM0

	ld a, [wCurOWLocation]
	sla a
	sla a ; *4
	ld hl, .LocationTitleTextItems
	add_hl_a
	ld a, [hli]
	ld e, a
	ld a, [hli]
	ld d, a
	ld a, [hli]
	ld h, [hl]
	ld l, a
	call InitTextPrinting_ProcessTextFromIDVRAM0
	ret

.LocationTitleTextItems:
	textitem 1, 3, MapMasonLabText         ; OWMAP_MASON_LABORATORY
	textitem 1, 2, MapIshiharasHouseText   ; OWMAP_ISHIHARAS_HOUSE
	textitem 1, 3, MapLightningClubText    ; OWMAP_LIGHTNING_CLUB
	textitem 1, 3, MapPsychicClubText      ; OWMAP_PSYCHIC_CLUB
	textitem 1, 3, MapRockClubText         ; OWMAP_ROCK_CLUB
	textitem 1, 3, MapFightingClubText     ; OWMAP_FIGHTING_CLUB
	textitem 1, 3, MapGrassClubText        ; OWMAP_GRASS_CLUB
	textitem 1, 2, MapScienceClubText      ; OWMAP_SCIENCE_CLUB
	textitem 1, 3, MapWaterClubText        ; OWMAP_WATER_CLUB
	textitem 1, 3, MapFireClubText         ; OWMAP_FIRE_CLUB
	textitem 1, 4, MapTCGAirportText       ; OWMAP_TCG_AIRPORT
	textitem 1, 2, MapTCGChallengeHallText ; OWMAP_TCG_CHALLENGE_HALL
	textitem 1, 3, MapPokemonDomeText      ; OWMAP_POKEMON_DOME
	; no sentinels

Func_40682:
	ld a, [wCurOWLocation]
	sla a
	sla a ; *4
	ld hl, .data
	add_hl_a
	ld a, [hl] ; map
	inc hl
	ld d, [hl] ; x
	inc hl
	ld e, [hl] ; y
	inc hl
	ld b, [hl] ; direction
	farcall SetWarpData
	ret

.data
	db MAP_MASON_LABORATORY_MAIN,       6, 13, NORTH ; OWMAP_MASON_LABORATORY
	db MAP_ISHIHARAS_HOUSE,             4, 11, NORTH ; OWMAP_ISHIHARAS_HOUSE
	db MAP_LIGHTNING_CLUB_ENTRANCE,     4,  7, NORTH ; OWMAP_LIGHTNING_CLUB
	db MAP_PSYCHIC_CLUB_ENTRANCE,       4,  7, NORTH ; OWMAP_PSYCHIC_CLUB
	db MAP_ROCK_CLUB_ENTRANCE,          4,  7, NORTH ; OWMAP_ROCK_CLUB
	db MAP_FIGHTING_CLUB_ENTRANCE,      4,  7, NORTH ; OWMAP_FIGHTING_CLUB
	db MAP_GRASS_CLUB_ENTRANCE,         4,  7, NORTH ; OWMAP_GRASS_CLUB
	db MAP_SCIENCE_CLUB_ENTRANCE,       4,  7, NORTH ; OWMAP_SCIENCE_CLUB
	db MAP_WATER_CLUB_ENTRANCE,         4,  7, NORTH ; OWMAP_WATER_CLUB
	db MAP_FIRE_CLUB_ENTRANCE,          4,  7, NORTH ; OWMAP_FIRE_CLUB
	db MAP_TCG_AIRPORT_ENTRANCE,        5, 11, NORTH ; OWMAP_TCG_AIRPORT
	db MAP_TCG_CHALLENGE_HALL_ENTRANCE, 4,  7, NORTH ; OWMAP_TCG_CHALLENGE_HALL
	db MAP_POKEMON_DOME_ENTRANCE,       7,  7, NORTH ; OWMAP_POKEMON_DOME

Func_406d1:
	ld a, [wPlayerOWObject]
	ld b, $01
	farcall _SetOWObjectAnimStruct1Flag2

	ld a, [wPlayerOWLocation]
	sla a ; *2
	ld hl, TCGIslandPlayerPaths
	add_hl_a
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld a, [wCurOWLocation]
	sla a ; *2
	add_hl_a
	ld a, [hli]
	ld h, [hl]
	ld l, a
	or h
	jr z, .finish_movement ; is null
	ld a, SFX_PLAYER_WALK_MAP
	call PlaySFX

.loop_commands
	ld a, [hli]
	ld e, [hl]
	inc hl
	ld d, a
	or e
	jr z, .straight_line
	cp $ff
	jr z, .finish_movement

; set target position
	push hl
	ld a, [wPlayerOWObject]
	farcall SetOWObjectTargetPosition
	jr .loop_wait_movement

.straight_line
	push hl
	ld a, [wCurOWLocation]
	sla a
	ld hl, TCGIslandLocationPositions
	add_hl_a
	ld a, [hli]
	ld e, [hl]
	ld d, a
	ld a, [wPlayerOWObject]
	farcall SetOWObjectTargetPosition

.loop_wait_movement
	call DoFrame
	ld hl, wOverworldTransition
	bit 2, [hl]
	jr nz, .asm_40746
	ldh a, [hKeysPressed]
	bit B_PAD_B, a
	jr z, .asm_40746
	set 2, [hl]
	ld a, $01
	ld b, $00
	farcall StartPalFadeToBlackOrWhite
.asm_40746
	farcall MoveOWObjectToTargetPosition
	jr c, .asm_4074f
	pop hl
	jr .loop_commands
.asm_4074f
	ld a, [wOverworldTransition]
	bit 2, a
	jr z, .loop_wait_movement
	farcall CheckPalFading
	jr nz, .loop_wait_movement
	pop hl
	jr .wait_fade

.finish_movement
	ld a, $01
	ld b, $00
	farcall StartPalFadeToBlackOrWhite
.wait_fade
	call WaitPalFading
	farcall Func_110a8
	ret

TCGIslandLocationPositions:
	; x, y
	db $0c, $68 ; OWMAP_MASON_LABORATORY
	db $04, $18 ; OWMAP_ISHIHARAS_HOUSE
	db $24, $50 ; OWMAP_LIGHTNING_CLUB
	db $5c, $2c ; OWMAP_PSYCHIC_CLUB
	db $14, $38 ; OWMAP_ROCK_CLUB
	db $34, $68 ; OWMAP_FIGHTING_CLUB
	db $7c, $40 ; OWMAP_GRASS_CLUB
	db $7c, $20 ; OWMAP_SCIENCE_CLUB
	db $6c, $64 ; OWMAP_WATER_CLUB
	db $6c, $10 ; OWMAP_FIRE_CLUB
	db $50, $78 ; OWMAP_TCG_AIRPORT
	db $3c, $20 ; OWMAP_TCG_CHALLENGE_HALL
	db $44, $44 ; OWMAP_POKEMON_DOME

INCLUDE "data/tcg_island_paths.asm"

DoGRBlimpMovement_TCGIsland:
	ld a, [wPrevMap]
	cp MAP_TCG_AIRPORT
	jr z, .asm_40cfa

	ld a, MAP_TCG_AIRPORT
	lb de, 0, 0
	ld b, NORTH
	farcall SetWarpData
	ld hl, .movement_3
	jr .start_movement
.asm_40cfa
	ld a, MAP_OVERHEAD_ISLANDS
	lb de, 0, 0
	ld b, NORTH
	farcall SetWarpData
	ld hl, .movement_2
	ld a, EVENT_SHORT_GR_ISLAND_FLYOVER_SEQUENCE
	farcall GetEventValue
	jr nz, .start_movement
	ld hl, wOverworldTransition
	set 3, [hl]
	ld hl, .movement_1

.start_movement
	ld a, [hli] ; coordinates
	ld e, [hl]  ;
	inc hl
	ld d, a
	cp $ff
	jr z, .done_movement
	ld a, [hli] ; direction
	ld b, a
	push hl
	push bc
	ld a, NPC_GR_BLIMP
	farcall SetOWObjectTargetPosition
	ld a, NPC_GR_BLIMP
	pop bc
	farcall _SetOWObjectDirection

; does movement every 4 frames
; can be skipped with B button
.loop_movement
	ld c, 4
.loop_wait
	push bc
	call DoFrame
	ld hl, wOverworldTransition
	bit 3, [hl]
	jr nz, .skip_fade_out
	bit 2, [hl]
	jr nz, .skip_fade_out
	ldh a, [hKeysPressed]
	bit B_PAD_B, a
	jr z, .skip_fade_out
	set 2, [hl]
	call .FadeOut
.skip_fade_out
	pop bc
	dec c
	jr nz, .loop_wait
	farcall MoveOWObjectToTargetPosition
	jr c, .still_moving
	pop hl
	jr .start_movement
.still_moving
	ld a, [wOverworldTransition]
	bit 2, a
	jr z, .loop_movement
	farcall CheckPalFading
	jr nz, .loop_movement
	pop hl
	jr .finish

.done_movement
	ld a, [wTempPrevMap]
	cp MAP_OVERHEAD_ISLANDS
	jr z, .asm_40d71 ; unnecessary cp
.asm_40d71
	call .FadeOut
.finish
	call WaitPalFading
	farcall Func_110a8
	ret

; fades out to white or black
; dependent on wTempPrevMap
.FadeOut:
	ld a, [wTempPrevMap]
	cp MAP_OVERHEAD_ISLANDS
	jr nz, .to_black
; to white
	ld a, $0
	ld b, $00
	farcall StartPalFadeToBlackOrWhite
	ret
.to_black
	ld a, $1
	ld b, $00
	farcall StartPalFadeToBlackOrWhite
	ret

.movement_1
	; goes up and east (high)
	db $50, $58, EAST
	db $90, $40, EAST
	db $a0, $40, EAST
	db $ff, $ff ; end

.movement_2
	; goes up and east (low)
	db $50, $58, EAST
	db $90, $60, EAST
	db $a0, $60, EAST
	db $ff, $ff ; end

.movement_3
	; goes down
	db $50, $58, WEST
	db $50, $78, EAST
	db $ff, $ff ; end

MasonLaboratoryMain_MapHeader:
	db MAP_GFX_MASON_LABORATORY_MAIN
	dba MasonLaboratoryMain_MapScripts
	db MUSIC_OVERWORLD

MasonLaboratoryMain_StepEvents:
	_ow_coordinate_function 6, 14, 0, 1, 7, 2, Func_40fff
	_ow_coordinate_function 7, 14, 0, 1, 7, 2, Func_40fff
	map_exit 0, 5, MAP_MASON_LABORATORY_TRAINING_ROOM, 12, 11, WEST
	map_exit 0, 6, MAP_MASON_LABORATORY_TRAINING_ROOM, 12, 12, WEST
	map_exit 13, 5, MAP_MASON_LABORATORY_COMPUTER_ROOM, 1, 5, EAST
	map_exit 13, 6, MAP_MASON_LABORATORY_COMPUTER_ROOM, 1, 6, EAST
	db $ff

MasonLaboratoryMain_NPCs:
	npc NPC_DR_MASON, 7, 5, SOUTH, NULL
	npc NPC_SAM, 2, 7, EAST, NULL
	npc NPC_LAB_TECH_PC_GUIDE, 3, 2, SOUTH, NULL
	npc NPC_LAB_TECH_CLUB_GUIDE, 11, 8, SOUTH, NULL
	npc NPC_LAB_TECH_BOOSTER_GUIDE, 9, 10, WEST, NULL
	npc NPC_LAB_TECH_ROOM_GUIDE, 10, 4, WEST, NULL
	npc NPC_RONALD, 3, 6, SOUTH, Func_4121d
	db $ff

MasonLaboratoryMain_NPCInteractions:
	npc_script NPC_DR_MASON, Func_41188
	npc_script NPC_SAM, Func_41233
	npc_script NPC_LAB_TECH_PC_GUIDE, Func_41370
	npc_script NPC_LAB_TECH_CLUB_GUIDE, Func_41396
	npc_script NPC_LAB_TECH_BOOSTER_GUIDE, Func_413bc
	npc_script NPC_LAB_TECH_ROOM_GUIDE, Func_413e2
	npc_script NPC_RONALD, Func_411f2
	db $ff

MasonLaboratoryMain_OWInteractions:
	ow_script 1, 2, PCMenu
	ow_script 2, 2, PCMenu
	ow_script 6, 3, Script_TCGChallengeMachine
	ow_script 7, 3, Script_TCGChallengeMachine
	db $ff

MasonLaboratoryMain_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_40e83
	dbw OWMODE_INTERACT, Func_40f04
	dbw OWMODE_AFTER_DUEL, Func_40f21
	dbw OWMODE_AFTER_DUEL_PRELOAD, Func_40f14
	dbw OWMODE_NPC_POSITION, Func_40e8a
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_40e91
	dbw OWMODE_MUSIC_PRELOAD, Func_40e72
	db $ff

Func_40e72:
	ld a, VAR_TIMES_MET_RONALD
	farcall GetVarValue
	or a
	jr nz, .asm_40e80
	ld a, MUSIC_RONALD
	ld [wNextMusic], a
.asm_40e80
	scf
	ccf
	ret

Func_40e83:
	ld hl, MasonLaboratoryMain_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_40e8a:
	ld hl, MasonLaboratoryMain_NPCs
	call LoadNPCs
	ret

Func_40e91:
	ld a, VAR_TIMES_MET_RONALD
	farcall GetVarValue
	or a
	jr z, .asm_40ead
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall GetEventValue
	jr nz, .asm_40ee8
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	farcall GetEventValue
	call nz, Func_40ef9
	scf
	ret
.asm_40ead
	ld a, NPC_RONALD
	lb de, 6, 5
	ld b, EAST
	farcall SetOWObjectTilePositionAndDirection
	ld a, [wPlayerOWObject]
	lb de, 6, 15
	farcall SetOWObjectTilePosition
	ld a, NPC_DR_MASON
	ld b, WEST
	farcall SetOWObjectDirection
	ld de, $408
	farcall CalcOWScroll
	ld a, OWMODE_SCRIPT
	ld [wOverworldMode], a
	ld a, BANK(Func_40f46)
	ld [wOverworldScriptBank], a
	ld hl, Func_40f46
	ld a, l
	ld [wOverworldScriptPointer], a
	ld a, h
	ld [wOverworldScriptPointer + 1], a
	scf
	ret
.asm_40ee8
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall ZeroOutEventValue
	call Func_40ef9
	ld a, $00
	call Func_338f
	scf
	ccf
	ret

Func_40ef9:
	ld bc, TILEMAP_006
	lb de, 5, 0
	farcall Func_12c0ce
	ret

Func_40f04:
	ld hl, MasonLaboratoryMain_NPCInteractions
	call Func_328c
	jr nc, .asm_40f12
	ld hl, MasonLaboratoryMain_OWInteractions
	call Func_32bf
.asm_40f12
	scf
	ret

Func_40f14:
	ld a, EVENT_EB
	farcall GetEventValue
	jr nz, .asm_40f1e
	scf
	ret
.asm_40f1e
	scf
	ccf
	ret

Func_40f21:
	ld a, EVENT_EB
	farcall GetEventValue
	jr nz, .asm_40f34
	ld hl, MasonLaboratoryMain_AfterDuelScripts
	ld a, [wScriptNPC]
	call ExecuteNPCScript
	scf
	ret
.asm_40f34
	farcall LoadChallengeMachineSave
	call Func_41074
	scf
	ret

MasonLaboratoryMain_AfterDuelScripts:
	npc_script NPC_DR_MASON, Func_4133e
	npc_script NPC_SAM, Func_412c0
	db $ff

Func_40f46:
	xor a
	start_script
	script_callfar Script_34000
	move_player .NPCMovement_40f80, TRUE
	wait_for_player_animation
	set_active_npc NPC_DR_MASON, DialogDrMasonText
	start_dialog
	send_mail $03
	send_mail $04
	send_mail $08
	set_event EVENT_GOT_CHANSEY_COIN
	script_call .ows_40f83
	script_call .ows_40fac
	script_call .ows_40f9b
	quit_script
	farcall _SaveGame
	ld a, $01
	start_script
	play_sfx SFX_SAVE_GAME
	end_dialog
	end_script
	ld a, OWMODE_IDLE
	ld [wOverworldMode], a
	ret
.NPCMovement_40f80:
	db NORTH, MOVE_2
	db $ff
.ows_40f83
	set_event EVENT_SET_UNTIL_MAP_RELOAD_1
	set_active_npc_direction SOUTH
	print_npc_text Text0efb
.ows_40f8a
	ask_question Text0efc, TRUE
	script_jump_if_b0nz .ows_40f97
	print_npc_text Text0efd
	script_jump .ows_40f8a
.ows_40f97
	print_npc_text Text0efe
	script_ret
.ows_40f9b
	print_npc_text Text0eff
	play_song MUSIC_BOOSTER_PACK
	print_text_wide_textbox Text0f00
	wait_song
	do_frames 60
	wait_input
	resume_song
	print_npc_text Text0f01
	script_ret
.ows_40fac
	print_npc_text Text0f02
	play_song MUSIC_BOOSTER_PACK
	print_text_wide_textbox Text0f03
	wait_song
	do_frames 60
	wait_input
	resume_song
	give_deck STARTER_DECK_ID
	script_ret

Func_40fbc:
	ld a, NPC_DR_MASON
	ld [wScriptNPC], a
	ldtx hl, DialogDrMasonText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_active_npc_direction SOUTH
	print_npc_text Text0f04
	end_dialog
	get_player_x_position
	compare_loaded_var $07
	script_jump_if_b0nz .ows_40fe4
	move_player .NPCMovement_40ff5, TRUE
	script_jump .ows_40fe8
.ows_40fe4
	move_player .NPCMovement_40ffc, TRUE
.ows_40fe8
	wait_for_player_animation
	start_dialog
	print_npc_text Text0f05
	end_dialog
	end_script
	ld a, OWMODE_IDLE
	ld [wOverworldMode], a
	ret
.NPCMovement_40ff5:
	db NORTH, MOVE_4
	db EAST, MOVE_1
	db NORTH, MOVE_4
	db $ff
.NPCMovement_40ffc:
	db NORTH, MOVE_8
	db $ff

Func_40fff:
	ld a, EVENT_SET_UNTIL_MAP_RELOAD_1
	farcall GetEventValue
	jr nz, .asm_41013
	ld a, OVERWORLD_MAP_TCG
	lb de, 1, 7
	ld b, SOUTH
	farcall SetWarpData
	ret
.asm_41013
	ld a, EVENT_SET_UNTIL_MAP_RELOAD_1
	farcall ZeroOutEventValue
	call Func_40fbc
	ret

Script_TCGChallengeMachine:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	farcall GetEventValue
	ret z
	farcall ValidateChallengeMachineSaveData
	jr nc, .no_error
	farcall InitChallengeMachine
	xor a
	start_script
	start_dialog
	print_text ChallengeMachineWinStreakRecordCorruptedAndResetText
	end_dialog
	end_script
.no_error
	farcall LoadChallengeMachineSave
	xor a
	farcall SetChallengeMachineOpponents
	farcall LoadChallengeMachineOpponentTitlesAndNames
	xor a
	lb bc, 0, 0
	farcall ChallengeMachine
	jr c, .quit
	xor a
	start_script
	set_event EVENT_EB
	set_var VAR_CHALLENGEMACHINE_CURRENT_ROUND, 1
	send_mail $0d
	end_script
	farcall SetChallengeMachineDuelParams
	jr .done
.quit
	or a
	jr z, .done
	ld bc, 0
	ld a, c
	ld [wTCGChallengeMachineCurWinStreak], a
	ld a, b
	ld [wTCGChallengeMachineCurWinStreak + 1], a
	farcall SaveChallengeMachine
.done
	ret

Func_41074:
	ld a, EVENT_SET_UNTIL_MAP_RELOAD_2
	farcall GetEventValue
	jp z, .quit
	ld a, [wTCGChallengeMachineCurWinStreak]
	ld c, a
	ld a, [wTCGChallengeMachineCurWinStreak + 1]
	ld b, a
	inc bc
	cp16bc_long MAX_NUM_CHALLENGE_MACHINE_WIN_RECORD + 1
	jr nc, .skip_incr_wins
	ld a, c
	ld [wTCGChallengeMachineCurWinStreak], a
	ld a, b
	ld [wTCGChallengeMachineCurWinStreak + 1], a
.skip_incr_wins
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	farcall GetVarValue
	cp NUM_CHALLENGE_MACHINE_ROUNDS_PER_SET
	jr z, .finished_set
	ld b, a
	xor a ; TCG_ISLAND
	ld c, CHALLENGE_MACHINE_DUEL_WIN
	farcall ChallengeMachine
	jp c, .withdrew
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	ld d, a
	farcall GetVarValue
	inc a
	ld c, a
	ld a, d
	farcall SetVarValue
	farcall SetChallengeMachineDuelParams
	jp .done

.finished_set
	ld a, [wTCGChallengeMachineSetsWonRecord]
	ld c, a
	ld a, [wTCGChallengeMachineSetsWonRecord + 1]
	ld b, a
	inc bc
	cp16bc_long MAX_NUM_CHALLENGE_MACHINE_WIN_RECORD + 1
	jr nc, .skip_incr_record
	ld a, c
	ld [wTCGChallengeMachineSetsWonRecord], a
	ld a, b
	ld [wTCGChallengeMachineSetsWonRecord + 1], a
.skip_incr_record
	call .CheckWinStreakRecord
	xor a ; TCG_ISLAND
	lb bc, NUM_CHALLENGE_MACHINE_ROUNDS_PER_SET, CHALLENGE_MACHINE_DUEL_WIN
	farcall ChallengeMachine
	jr .check_50winstreak_prize

.quit
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	farcall GetVarValue
	ld b, a
	xor a ; TCG_ISLAND
	ld c, CHALLENGE_MACHINE_DUEL_LOSS_OR_QUIT
	farcall ChallengeMachine
.withdrew
	call .CheckWinStreakRecord
	ld bc, 0
	ld a, c
	ld [wTCGChallengeMachineCurWinStreak], a
	ld a, b
	ld [wTCGChallengeMachineCurWinStreak + 1], a
.exit
	ld a, EVENT_EB
	farcall ZeroOutEventValue
	farcall SaveChallengeMachine
.done
	ret

.check_50winstreak_prize
	ld a, [wTCGChallengeMachineCurWinStreak]
	ld e, a
	ld a, [wTCGChallengeMachineCurWinStreak + 1]
	ld d, a
	cp16_long NUM_CHALLENGE_MACHINE_WINS_FOR_COIN_PRIZE
	jr nz, .exit
	call Script_DrMason_50WinStreak
	jr .exit

.CheckWinStreakRecord:
	ld a, [wTCGChallengeMachineCurWinStreak]
	ld c, a
	ld a, [wTCGChallengeMachineCurWinStreak + 1]
	ld b, a
	ld a, [wTCGChallengeMachineWinStreakRecord]
	ld e, a
	ld a, [wTCGChallengeMachineWinStreakRecord + 1]
	ld d, a
	cp16bc_long de
	jr nc, .new_record
	scf
	ret
.new_record
	ld a, c
	ld [wTCGChallengeMachineWinStreakRecord], a
	ld a, b
	ld [wTCGChallengeMachineWinStreakRecord + 1], a
	call EnableSRAM
	ld hl, sPlayerName
	ld de, wTCGChallengeMachinePlayerName
	ld bc, NAME_BUFFER_LENGTH
	call CopyDataHLtoDE_SaveRegisters
	call DisableSRAM
	scf
	ccf
	ret

Script_DrMason_50WinStreak:
	xor a
	start_script
	check_event EVENT_GOT_DUGTRIO_COIN
	script_jump_if_b0z .done
	set_event EVENT_GOT_DUGTRIO_COIN
	set_active_npc NPC_DR_MASON, DialogDrMasonText
	set_active_npc_direction NORTH
	set_player_direction SOUTH
	start_dialog
	print_npc_text DrMasonTCGChallengeMachine50WinStreakRewards1Text
	give_coin COIN_DUGTRIO
	print_npc_text DrMasonTCGChallengeMachine50WinStreakRewards2Text
	end_dialog
	set_active_npc_direction SOUTH
.done
	end_script
	ret

Func_41188:
	ld a, NPC_DR_MASON
	ld [wScriptNPC], a
	ldtx hl, DialogDrMasonText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_411df
	check_event EVENT_GOT_MACHAMP_COIN
	script_jump_if_b0z .ows_411d9
	check_event EVENT_GOT_MAGNEMITE_COIN
	script_jump_if_b0z .ows_411d3
	check_event EVENT_GOT_GR_COIN
	script_jump_if_b0z .ows_411cd
	get_var VAR_TIMES_MET_RONALD
	compare_loaded_var $02
	script_jump_if_b0nz .ows_411c1
	script_jump_if_b1z .ows_411c7
	print_npc_text Text0f09
	script_jump .ows_411ef
.ows_411c1
	print_npc_text Text0f0a
	script_jump .ows_411ef
.ows_411c7
	print_npc_text Text0f0b
	script_jump .ows_411ef
.ows_411cd
	print_npc_text Text0f0c
	script_jump .ows_411ef
.ows_411d3
	print_npc_text Text0f0d
	script_jump .ows_411ef
.ows_411d9
	print_npc_text Text0f0e
	script_jump .ows_411ef
.ows_411df
	check_event EVENT_92
	script_jump_if_b0z .ows_411ec
	set_event EVENT_92
	print_npc_text Text0f0f
	script_jump .ows_411ef
.ows_411ec
	print_npc_text Text0f10
.ows_411ef
	end_dialog
	end_script
	ret

Func_411f2:
	ld a, NPC_RONALD
	ld [wScriptNPC], a
	ldtx hl, DialogRonaldText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	get_var VAR_TIMES_MET_RONALD
	compare_loaded_var $09
	script_jump_if_b1z .ows_41217
	set_var VAR_TIMES_MET_RONALD, $09
	print_npc_text Text0f11
	script_jump .ows_4121a
.ows_41217
	print_npc_text Text0f12
.ows_4121a
	end_dialog
	end_script
	ret

Func_4121d:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	farcall GetEventValue
	jr nz, .asm_41230
	ld a, VAR_TIMES_MET_RONALD
	farcall GetVarValue
	or a
	jr z, .asm_41230
	scf
	ret
.asm_41230
	scf
	ccf
	ret

Func_41233:
	ld a, NPC_SAM
	ld [wScriptNPC], a
	ldtx hl, DialogSamText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_TALKED_TO_SAM
	script_jump_if_b0nz Script_412f8
	print_npc_text Text0f13
	end_script
	ld a, $01
	ld b, $00
	farcall HandlePopupMenu
	jr c, .asm_41264
	or a
	jr z, .asm_4126b
	dec a
	jr z, .asm_4128d
	dec a
	jr z, .asm_412b2
.asm_41264
	xor a
	start_script
	script_jump .ows_412bd
.asm_4126b
	xor a
	start_script
	print_npc_text Text0f14
	ask_question Text0f15, TRUE
	script_jump_if_b0z .ows_41287
	end_dialog
	set_active_npc_direction EAST
	script_call Script_41499
	set_event EVENT_EF
	start_duel SAMS_PRACTICE_DECK_ID, MUSIC_MATCH_START_MEMBER
	end_script
	ret
.ows_41287
	print_npc_text Text0f16
	script_jump .ows_412bd
.asm_4128d
	xor a
	start_script
	print_npc_text Text0f17
	ask_question Text0f15, TRUE
	script_jump_if_b0z .ows_412ac
	end_dialog
	set_active_npc_direction EAST
	script_call Script_4151b
	start_dialog
	print_npc_text Text0f18
	end_dialog
	start_duel UNUSED_SAMS_PRACTICE_DECK_ID, MUSIC_MATCH_START_MEMBER
	end_script
	ret
.ows_412ac
	print_npc_text Text0f19
	script_jump .ows_412bd
.asm_412b2
	xor a
	start_script
	script_call Script_41408
	print_npc_text Text0f16
.ows_412bd
	end_dialog
	end_script
	ret

Func_412c0:
	xor a
	start_script
	start_dialog
	check_event EVENT_EF
	script_jump_if_b0z .ows_412e4
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_412db
	print_npc_text Text0f1a
	give_booster_packs BoosterList_cc9b
	print_npc_text Text0f1b
	script_jump .ows_412e1
.ows_412db
	print_npc_text Text0f1c
	script_jump .ows_412e1
.ows_412e1
	end_dialog
	end_script
	ret
.ows_412e4
	reset_event EVENT_EF
	print_npc_text Text0f1d
	end_dialog
	move_npc NPC_DR_MASON, .NPCMovement_412f1
	wait_for_player_animation
	end_script
	ret
.NPCMovement_412f1:
	db NORTH, MOVE_1
	db EAST, MOVE_3
	db SOUTH, MOVE_0
	db $ff

Script_412f8:
	set_event EVENT_TALKED_TO_SAM
	print_npc_text Text0f1e
	ask_question Text0f1f, TRUE
	script_jump_if_b0nz .ows_4130a
	print_npc_text Text0f20
	script_jump .ows_4133b
.ows_4130a
	print_npc_text Text0f21
	end_dialog
	set_active_npc_direction EAST
	script_call Script_41499
	start_dialog
	set_active_npc NPC_DR_MASON, DialogDrMasonText
	print_npc_text Text0f22
	set_active_npc NPC_SAM, DialogSamText
	print_npc_text Text0f23
.ows_41323
	script_call Script_41408
	npc_ask_question Text0f24, TRUE
	script_jump_if_b0z .ows_41323
	set_active_npc NPC_DR_MASON, DialogDrMasonText
	print_npc_text Text0f25
	end_dialog
	start_duel SAMS_PRACTICE_DECK_ID, MUSIC_MATCH_START_MEMBER
	end_script
	ret
.ows_4133b
	end_dialog
	end_script
	ret

Func_4133e:
	xor a
	start_script
	start_dialog
	print_npc_text Text0f26
	ask_question Text0f27, TRUE
	script_jump_if_b0z .ows_41356
	print_npc_text Text0f28
	end_dialog
	start_duel SAMS_PRACTICE_DECK_ID, MUSIC_MATCH_START_MEMBER
	end_script
	ret
.ows_41356
	print_npc_text Text0f29
	end_dialog
	move_active_npc .NPCMovement_41369
	wait_for_player_animation
	start_dialog
	set_active_npc NPC_SAM, DialogSamText
	print_npc_text Text0f2a
	end_dialog
	end_script
	ret
.NPCMovement_41369:
	db NORTH, MOVE_1
	db EAST, MOVE_3
	db SOUTH, MOVE_0
	db $ff

Func_41370:
	ld a, NPC_LAB_TECH_PC_GUIDE
	ld [wScriptNPC], a
	ldtx hl, DialogTechText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_41390
	print_npc_text Text0f2b
	script_jump .ows_41393
.ows_41390
	print_npc_text Text0f2c
.ows_41393
	end_dialog
	end_script
	ret

Func_41396:
	ld a, NPC_LAB_TECH_CLUB_GUIDE
	ld [wScriptNPC], a
	ldtx hl, DialogTechText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_413b6
	print_npc_text Text0f2d
	script_jump .ows_413b9
.ows_413b6
	print_npc_text Text0f2e
.ows_413b9
	end_dialog
	end_script
	ret

Func_413bc:
	ld a, NPC_LAB_TECH_BOOSTER_GUIDE
	ld [wScriptNPC], a
	ldtx hl, DialogTechText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_413dc
	print_npc_text Text0f2f
	script_jump .ows_413df
.ows_413dc
	print_npc_text Text0f30
.ows_413df
	end_dialog
	end_script
	ret

Func_413e2:
	ld a, NPC_LAB_TECH_ROOM_GUIDE
	ld [wScriptNPC], a
	ldtx hl, DialogTechText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_41402
	print_npc_text Text0f31
	script_jump .ows_41405
.ows_41402
	print_npc_text Text0f32
.ows_41405
	end_dialog
	end_script
	ret

Script_41408:
	end_dialog
	quit_script
	ld b, $00
.asm_4140c
	xor a
	farcall HandlePopupMenu
	jr c, .asm_41428
	or a
	jr z, .asm_4142f
	dec a
	jr z, .asm_4143e
	dec a
	jr z, .asm_4144d
	dec a
	jr z, .asm_4145c
	dec a
	jr z, .asm_4146b
	dec a
	jr z, .asm_4147a
	dec a
	jr z, .asm_41489
.asm_41428
	ld a, $01
	start_script
	start_dialog
	script_ret
.asm_4142f
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f33
	end_dialog
	quit_script
	ld b, $00
	jr .asm_4140c
.asm_4143e
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f34
	end_dialog
	quit_script
	ld b, $01
	jr .asm_4140c
.asm_4144d
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f35
	end_dialog
	quit_script
	ld b, $02
	jr .asm_4140c
.asm_4145c
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f36
	end_dialog
	quit_script
	ld b, $03
	jr .asm_4140c
.asm_4146b
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f37
	end_dialog
	quit_script
	ld b, $04
	jr .asm_4140c
.asm_4147a
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f38
	end_dialog
	quit_script
	ld b, $05
	jr .asm_4140c
.asm_41489
	ld a, $01
	start_script
	start_dialog
	print_npc_text Text0f39
	end_dialog
	quit_script
	ld b, $06
	jp .asm_4140c

Script_41499:
	get_player_direction
	compare_loaded_var NORTH
	script_jump_if_b0nz .ows_414b8
	compare_loaded_var EAST
	script_jump_if_b0nz .ows_414ac
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_414c4
	script_jump .ows_414d9
.ows_414ac
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_414cb
	move_player .NPCMovement_414f0, TRUE
	script_jump .ows_414d9
.ows_414b8
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_414d2
	move_player .NPCMovement_414f5, TRUE
	script_jump .ows_414de
.ows_414c4
	move_player .NPCMovement_414fe, TRUE
	script_jump .ows_414de
.ows_414cb
	move_player .NPCMovement_41509, TRUE
	script_jump .ows_414de
.ows_414d2
	move_player .NPCMovement_41512, TRUE
	script_jump .ows_414de
.ows_414d9
	wait_for_player_animation
	move_player .NPCMovement_414e9, TRUE
.ows_414de
	move_npc NPC_DR_MASON, .NPCMovement_414e4
	wait_for_player_animation
	script_ret
.NPCMovement_414e4:
	db WEST, MOVE_3
	db SOUTH, MOVE_1
	db $ff
.NPCMovement_414e9:
	db EAST, MOVE_3
	db SOUTH, MOVE_1
	db WEST, MOVE_0
	db $ff
.NPCMovement_414f0:
	db NORTH, MOVE_1
	db EAST, MOVE_1
	db $ff
.NPCMovement_414f5:
	db SOUTH, MOVE_1
	db EAST, MOVE_3
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_414fe:
	db WEST, MOVE_1
	db SOUTH, MOVE_3
	db EAST, MOVE_4
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_41509:
	db SOUTH, MOVE_2
	db EAST, MOVE_4
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_41512:
	db SOUTH, MOVE_1
	db EAST, MOVE_3
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff

Script_4151b:
	get_player_direction
	compare_loaded_var NORTH
	script_jump_if_b0nz .ows_4153e
	compare_loaded_var EAST
	script_jump_if_b0nz .ows_41532
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_4154a
	move_player .NPCMovement_4155e, TRUE
	script_jump .ows_4155c
.ows_41532
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_41551
	move_player .NPCMovement_41565, TRUE
	script_jump .ows_4155c
.ows_4153e
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_41558
	move_player .NPCMovement_4156e, TRUE
	script_jump .ows_4155c
.ows_4154a
	move_player .NPCMovement_41577, TRUE
	script_jump .ows_4155c
.ows_41551
	move_player .NPCMovement_41580, TRUE
	script_jump .ows_4155c
.ows_41558
	move_player .NPCMovement_41589, TRUE
.ows_4155c
	wait_for_player_animation
	script_ret
.NPCMovement_4155e:
	db EAST, MOVE_3
	db SOUTH, MOVE_1
	db WEST, MOVE_0
	db $ff
.NPCMovement_41565:
	db NORTH, MOVE_1
	db EAST, MOVE_4
	db SOUTH, MOVE_1
	db WEST, MOVE_0
	db $ff
.NPCMovement_4156e:
	db SOUTH, MOVE_1
	db EAST, MOVE_3
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_41577:
	db NORTH, MOVE_1
	db EAST, MOVE_3
	db SOUTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_41580:
	db SOUTH, MOVE_2
	db EAST, MOVE_4
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff
.NPCMovement_41589:
	db SOUTH, MOVE_1
	db EAST, MOVE_3
	db NORTH, MOVE_2
	db WEST, MOVE_0
	db $ff

TcgChallengeHall_MapHeader:
	db MAP_GFX_TCG_CHALLENGE_HALL
	dba TcgChallengeHall_MapScripts
	db MUSIC_OVERWORLD

TcgChallengeHall_StepEvents:
	map_exit 7, 15, MAP_TCG_CHALLENGE_HALL_ENTRANCE, 4, 1, SOUTH
	map_exit 8, 15, MAP_TCG_CHALLENGE_HALL_ENTRANCE, 5, 1, SOUTH
	db $ff

TcgChallengeHall_NPCs:
	npc NPC_CUP_HOST, 7, 2, SOUTH, TcgChallengeHall_AppearDuringTCGCups
	npc NPC_TCG_CUP_CLERK_RIGHT, 10, 9, SOUTH, TcgChallengeHall_AppearDuringTCGCups
	npc NPC_TCG_CUP_CLERK_LEFT, 5, 9, SOUTH, TcgChallengeHall_AppearDuringTCGCups
	npc NPC_TCG_CHALLENGE_HALL_MAN, 7, 10, SOUTH, TcgChallengeHall_DisappearDuringTCGCups
	db $ff

TcgChallengeHall_NPCInteractions:
	npc_script NPC_TCG_CUP_CLERK_LEFT, Script_TCGCupClerkLeft
	npc_script NPC_TCG_CUP_CLERK_RIGHT, Script_TCGCupClerkRight
	npc_script NPC_TCG_CHALLENGE_HALL_MAN, Script_TCGChallengeHallMan
	db $ff

TcgChallengeHall_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_415f3
	dbw OWMODE_INTERACT, Func_41622
	dbw OWMODE_AFTER_DUEL, Func_4162a
	dbw OWMODE_NPC_POSITION, Func_415fa
	dbw OWMODE_MUSIC_PRELOAD, Func_415e6
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_41603
	dbw OWMODE_CONTINUE_OW, Func_4163b
	db $ff

Func_415e6:
	call CheckIfTCGCupIsActive
	jr c, .inactive
	ld a, MUSIC_CHALLENGE_HALL
	ld [wNextMusic], a
.inactive
	scf
	ccf
	ret

Func_415f3:
	ld hl, TcgChallengeHall_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_415fa:
	ld hl, TcgChallengeHall_NPCs
	call LoadNPCs
	scf
	ccf
	ret

Func_41603:
	call CheckIfTCGCupIsActive
	jr c, .inactive
	xor a ; TCG_ISLAND
	farcall SetChallengeCupOpponents
	ld a, VAR_CHALLENGECUP_ROUND1_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	lb de, 9, 4
	ld b, WEST
	farcall LoadOWObjectInMap
.inactive
	scf
	ret

Func_41622:
	ld hl, TcgChallengeHall_NPCInteractions
	call Func_328c
	scf
	ret

Func_4162a:
	ld a, VAR_CHALLENGECUP_CURRENT_ROUND
	farcall GetVarValue
	cp 2
	jp c, Script_TCGCupRound1AfterDuel
	jp z, Script_TCGCupRound2AfterDuel
	jp Script_TCGCupRound3AfterDuel

Func_4163b:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall GetEventValue
	jr nz, .asm_41645
	scf
	ret
.asm_41645
	farcall OverworldResumeWithCurSong
	call Func_4164f
	scf
	ccf
	ret

Func_4164f:
	xor a
	start_script
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	set_active_npc_position 9, 2
	get_var VAR_CHALLENGECUP_CURRENT_ROUND
	compare_loaded_var 2
	script_jump_if_b0nz Script_TCGCupRound2Start
	script_jump Script_TCGCupRound3Start

Script_TCGCupClerkLeft:
	ld a, NPC_TCG_CUP_CLERK_LEFT
	ld [wScriptNPC], a
	ldtx hl, DialogReceptionistText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_RESULT
	compare_loaded_var CHALLENGE_CUP_RESULT_WON
	script_jump_if_b0nz .won
	script_jump_if_b1z .lost
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_3_START
	script_jump_if_b0nz .cup3
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .cup2
; cup1
	print_npc_text TCGCupClerkCup1ActiveText
	script_jump .enter_prompt
.cup2
	print_npc_text TCGCupClerkCup2ActiveText
	script_jump .enter_prompt
.cup3
	quit_script
	farcall GetTCGChallengeCupPrizeCardName
	call LoadTxRam2
	ld a, $01
	start_script
	print_npc_text TCGCupClerkCup3ActiveText
	script_jump .enter_prompt
.won
	print_npc_text TCGCupClerkPlayerWonText
	script_jump .done
.lost
	print_npc_text TCGCupClerkPlayerLostText
	script_jump .done
.enter_prompt
	ask_question TCGCupClerkEnterPromptText, TRUE
	script_jump_if_b0z .declined
	print_npc_text TCGCupClerkEnterAcceptedText
	end_dialog
	get_player_direction
	compare_loaded_var NORTH
	script_jump_if_b0nz .walk_straight
; has to turn
	move_player .NPCMovement_416f8, TRUE
	script_jump .enter_stage
.walk_straight
	move_player .NPCMovement_416fd, TRUE
	script_jump .enter_stage
.declined
	print_npc_text TCGCupClerkEnterDeclinedText
.done
	end_dialog
	end_script
	ret
.enter_stage
	move_active_npc .NPCMovement_416ec
	wait_for_player_animation
	move_player .NPCMovement_41700, TRUE
	move_active_npc .NPCMovement_416f3
	wait_for_player_animation
	script_jump Script_TCGCupRound1Start
.NPCMovement_416ec:
	db NORTH, MOVE_2
	db EAST, MOVE_1
	db WEST, MOVE_0
	db $ff
.NPCMovement_416f3:
	db WEST, MOVE_1
	db SOUTH, MOVE_2
	db $ff
.NPCMovement_416f8:
	db EAST, MOVE_1
	db NORTH, MOVE_3
	db $ff
.NPCMovement_416fd:
	db NORTH, MOVE_4
	db $ff
.NPCMovement_41700:
	db NORTH, MOVE_2
	db EAST, MOVE_1
	db $ff

Script_TCGCupClerkRight:
	ld a, NPC_TCG_CUP_CLERK_RIGHT
	ld [wScriptNPC], a
	ldtx hl, DialogReceptionistText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text TCGCupClerkRightNoEntryText
	end_dialog
	end_script
	ret

Script_TCGChallengeHallMan:
	ld a, NPC_TCG_CHALLENGE_HALL_MAN
	ld [wScriptNPC], a
	ldtx hl, DialogManText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .cup3_unlocked
	check_event EVENT_GOT_GR_COIN
	script_jump_if_b0z .cup1_unlocked
; cups dead
	print_npc_text TCGChallengeHallManNoCardsText
	script_jump .done
.cup1_unlocked
	print_npc_text TCGChallengeHallManGoodLuckText
	script_jump .done
.cup3_unlocked
	print_npc_text TCGChallengeHallManTCGCup3Text
.done
	end_dialog
	end_script
	ret

TcgChallengeHall_DisappearDuringTCGCups:
	ld a, VAR_TCG_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .disappear
	cp CHALLENGE_CUP_2_START
	jr z, .disappear
	cp CHALLENGE_CUP_3_START
	jr z, .disappear
	scf
	ccf
	ret
.disappear
	scf
	ret

TcgChallengeHall_AppearDuringTCGCups:
	ld a, VAR_TCG_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .appear
	cp CHALLENGE_CUP_2_START
	jr z, .appear
	cp CHALLENGE_CUP_3_START
	jr z, .appear
	scf
	ret
.appear
	scf
	ccf
	ret

Script_TCGCupRound1Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 0
	script_jump .round1
.round1
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 1
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	set_active_npc NPC_CUP_HOST, DialogCupHostText
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .cup2
	script_jump_if_b1z .cup3
; cup1
	print_npc_text CupHostTCGCup1IntroText
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCup1Round1ChallengerText
	script_jump .start_duel
.cup2
	print_npc_text CupHostTCGCup2IntroText
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCup2Round1ChallengerText
	script_jump .start_duel
.cup3
	print_npc_text CupHostTCGCup3IntroText
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCup3Round1ChallengerText
.start_duel
	end_dialog
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCupRound1DuelStartText
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret

Script_TCGCupRound1AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round1
; won round1
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round1
	script_jump_if_b1z .won_cup3_round1
; won cup1 round1
	print_npc_text CupHostTCGCup1Or2Round1PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup1Round1NextChallengerText
	script_jump .proceed
.won_cup2_round1
	print_npc_text CupHostTCGCup1Or2Round1PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup2Round1NextChallengerText
	script_jump .proceed
.won_cup3_round1
	print_npc_text CupHostTCGCup3Round1PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup3Round1NextChallengerText
	script_jump .proceed
.lost_round1
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .lost_cup2_round1
	script_jump_if_b1z .lost_cup3_round1
; lost cup1 round1
	print_npc_text CupHostTCGCup1Round1PlayerLostText
	script_jump Script_TCGCupPlayerLost
.lost_cup2_round1
	print_npc_text CupHostTCGCup2Round1PlayerLostText
	script_jump Script_TCGCupPlayerLost
.lost_cup3_round1
	print_npc_text CupHostTCGCup3Round1PlayerLostText
	script_jump Script_TCGCupPlayerLost
.proceed
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND2_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	ld b, NORTH
	lb de, 10, 9
	farcall LoadOWObjectInMap
	ld b, BANK(.NPCMovement_418a1)
	ld hl, .NPCMovement_418a1
	farcall MoveNPC
	call Func_3340
	ld a, $01
	start_script
	start_dialog
	script_jump Script_TCGCupRound2Start
.opponent_eliminated
	end_dialog
	animate_active_npc_movement $01, $01
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCupPlayerWonOpponentEliminatedText
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND1_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	push af
	ld b, BANK(.NPCMovement_4189c)
	ld hl, .NPCMovement_4189c
	farcall MoveNPC
	call Func_3340
	pop af
	farcall ClearOWObject
	ld a, $01
	start_script
	start_dialog
	script_ret
.NPCMovement_4189c:
	db EAST, MOVE_1
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_418a1:
	db NORTH, MOVE_5
	db WEST, MOVE_1
	db $ff

Script_TCGCupRound2Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 2
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	start_dialog
	print_npc_text CupHostTCGCupRound2ChallengerText
	end_dialog
	move_active_npc .NPCMovement_418e8
	wait_for_player_animation
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_3_START
	script_jump_if_b0z .loop_prep
	print_npc_text CupHostTCGCupRound2The4PrizesMatchText
.loop_prep
	print_npc_text CupHostTCGCupAreYourDecksReadyText
	ask_question DuelPrepPromptText, FALSE
	script_jump_if_b0z .start_duel
	print_npc_text CupHostTCGCupMakeYourPreparationsText
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	script_jump .loop_prep
.start_duel
	print_npc_text CupHostTCGCupRound2DuelStartText
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret
.NPCMovement_418e8:
	db WEST, MOVE_2
	db SOUTH, MOVE_0
	db $ff

Script_TCGCupRound2AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round2
; won round2
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round2
	script_jump_if_b1z .won_cup3_round2
; won cup1 round2
	print_npc_text CupHostTCGCupRound2PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup1Round2NextChallengerText
	script_jump .proceed
.won_cup2_round2
	print_npc_text CupHostTCGCupRound2PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup2Round2NextChallengerText
	script_jump .proceed
.won_cup3_round2
	print_npc_text CupHostTCGCupRound2PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup3Round2NextChallengerText
	script_jump .proceed
.lost_round2
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_3_START
	script_jump_if_b0nz .lost_cup3_round2
; lost cup1/2 round2
	print_npc_text CupHostTCGCup1Or2Round2PlayerLostText
	script_jump Script_TCGCupPlayerLost
.lost_cup3_round2
	print_npc_text CupHostTCGCup3Round2PlayerLostText
	script_jump Script_TCGCupPlayerLost
.proceed
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND3_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	ld b, NORTH
	lb de, 10, 9
	farcall LoadOWObjectInMap
	ld b, BANK(.NPCMovement_419a6)
	ld hl, .NPCMovement_419a6
	farcall MoveNPC
	call Func_3340
	ld a, $01
	start_script
	start_dialog
	script_jump Script_TCGCupRound3Start
.opponent_eliminated
	end_dialog
	animate_active_npc_movement $01, $01
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCupPlayerWonOpponentEliminatedText
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND2_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	push af
	ld b, BANK(.NPCMovement_419a1)
	ld hl, .NPCMovement_419a1
	farcall MoveNPC
	call Func_3340
	pop af
	farcall ClearOWObject
	ld a, $01
	start_script
	start_dialog
	script_ret
.NPCMovement_419a1:
	db EAST, MOVE_1
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_419a6:
	db NORTH, MOVE_5
	db WEST, MOVE_1
	db $ff

Script_TCGCupRound3Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 3
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .cup2
	script_jump_if_b1z .cup3
; cup1
	print_npc_text CupHostTCGCup1Round3ChallengerText
	script_jump .prep
.cup2
	print_npc_text CupHostTCGCup2Round3ChallengerText
	script_jump .prep
.cup3
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND3_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	cp NPC_RONALD
	jr z, .ronald
	cp NPC_GR_1
	jr z, .gr
	cp NPC_GR_2
	jr z, .gr
	cp NPC_GR_3
	jr z, .gr
	cp NPC_GR_4
	jr z, .gr
; other (club master)
	ld a, $01
	start_script
	print_npc_text CupHostTCGCup3Round3ChallengerClubMasterText
	script_jump .prep
.gr
	ld a, $01
	start_script
	print_npc_text CupHostTCGCup3Round3ChallengerGRText
	script_jump .prep
.ronald
	ld a, $01
	start_script
	print_npc_text CupHostTCGCup3Round3ChallengerRonaldText
	set_active_npc NPC_RONALD, DialogRonaldText
	print_npc_text RonaldTCGCup3ChallengerText
	set_active_npc NPC_CUP_HOST, DialogCupHostText
.prep
	end_dialog
	move_active_npc .NPCMovement_41a48
	wait_for_player_animation
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0z .loop_prep
	print_npc_text CupHostTCGCupRound3The6PrizesMatchText
.loop_prep
	print_npc_text CupHostTCGCupAreYourDecksReadyText
	ask_question DuelPrepPromptText, FALSE
	script_jump_if_b0z .start_duel
	print_npc_text CupHostTCGCupMakeYourPreparationsText
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	script_jump .loop_prep
.start_duel
	print_npc_text CupHostTCGCupRound3DuelStartText
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret
.NPCMovement_41a48:
	db WEST, MOVE_2
	db SOUTH, MOVE_0
	db $ff

Script_TCGCupRound3AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round3
; won round3
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round3
	script_jump_if_b1z .won_cup3_round3
; won cup1 round3, identical to cup2
	print_npc_text CupHostTCGCupRound3PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup1Or2PlayerChampionText
	script_jump .proceed_prize
.won_cup2_round3
	print_npc_text CupHostTCGCupRound3PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup1Or2PlayerChampionText
	script_jump .proceed_prize
.won_cup3_round3
	print_npc_text CupHostTCGCupRound3PlayerWonText
	script_call .opponent_eliminated
	print_npc_text CupHostTCGCup3PlayerChampionText
	script_jump .proceed_prize
.lost_round3
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .lost_cup2_round3
	script_jump_if_b1z .lost_cup3_round3
; lost cup1 round3
	print_npc_text CupHostTCGCup1Round3PlayerLostText
	script_jump Script_TCGCupPlayerLost
.lost_cup2_round3
	print_npc_text CupHostTCGCup2Round3PlayerLostText
	script_jump Script_TCGCupPlayerLost
.lost_cup3_round3
	print_npc_text CupHostTCGCup3Round3PlayerLostText
	script_jump Script_TCGCupPlayerLost
.opponent_eliminated
	end_dialog
	animate_active_npc_movement $01, $01
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCupPlayerWonOpponentEliminatedText
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_ROUND3_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	push af
	ld b, BANK(.NPCMovement_41ae0)
	ld hl, .NPCMovement_41ae0
	farcall MoveNPC
	call Func_3340
	pop af
	farcall ClearOWObject
	ld a, $01
	start_script
	start_dialog
	script_ret
.NPCMovement_41ae0:
	db EAST, MOVE_1
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_41ae5:
	db NORTH, MOVE_5
	db WEST, MOVE_1
	db $ff
.proceed_prize
	set_var VAR_TCG_CHALLENGE_CUP_RESULT, CHALLENGE_CUP_RESULT_WON
	get_var VAR_TIMES_WON_TCG_CHALLENGE_CUP
	compare_loaded_var NUM_CHALLENGE_CUP_WINS_FOR_COIN_PRIZE + 1
	script_jump_if_b1z .skip_incr_win_count
	inc_var VAR_TIMES_WON_TCG_CHALLENGE_CUP
.skip_incr_win_count
	end_dialog
	move_active_npc .NPCMovement_41b86
	wait_for_player_animation
	set_player_direction NORTH
	start_dialog
	get_var VAR_TCG_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .prize_cup2
	script_jump_if_b1z .prize_cup3
; prize cup1
	print_npc_text CupHostTCGCup1ChampionPrizeText
	give_card MEWTWO_LV30
	show_card_received_screen MEWTWO_LV30
	script_jump .check_win_count ; redundant, jumping to .skip_10times_prize should suffice
.prize_cup2
	print_npc_text CupHostTCGCup2ChampionPrizeText
	give_card PIKACHU_LV13
	show_card_received_screen PIKACHU_LV13
	script_jump .check_win_count ; redundant, jumping to .skip_10times_prize should suffice
.prize_cup3
	quit_script
	ld a, VAR_TCG_CHALLENGE_CUP_PRIZE_INDEX
	farcall GetVarValue
	farcall GetTCGChallengeCupPrizeCardName
	call LoadTxRam2
	ld a, $01
	start_script
	print_npc_text CupHostTCGCup3ChampionPrizeText
	quit_script
	ld a, VAR_TCG_CHALLENGE_CUP_PRIZE_INDEX
	farcall GetVarValue
	farcall GetTCGChallengeCupPrizeCardID
	ld e, c
	ld d, b
	farcall Func_1022a
	farcall Func_c646
	farcall Func_10252
	call WaitPalFading
	ld a, $01
	start_script
.check_win_count
	get_var VAR_TIMES_WON_TCG_CHALLENGE_CUP
	compare_loaded_var NUM_CHALLENGE_CUP_WINS_FOR_COIN_PRIZE
	script_jump_if_b0z .skip_10times_prize
	print_npc_text CupHostTCGCup10TimesChampionRewards1Text
	set_event EVENT_GOT_PONYTA_COIN
	give_coin COIN_PONYTA
	print_npc_text CupHostTCGCup10TimesChampionRewards2Text
	script_jump .exit
.skip_10times_prize
	print_npc_text CupHostTCGCupPlayerChampionCongratsText
.exit
	end_dialog
	move_player .NPCMovement_41b8b, TRUE
	wait_for_player_animation
	move_player .NPCMovement_41b90, TRUE
	move_npc NPC_TCG_CUP_CLERK_LEFT, .NPCMovement_41b93
	wait_for_player_animation
	animate_npc_movement NPC_TCG_CUP_CLERK_LEFT, $01, $01
	set_npc_direction NPC_TCG_CUP_CLERK_LEFT, SOUTH
	end_script
	ret
.NPCMovement_41b86:
	db WEST, MOVE_3
	db SOUTH, MOVE_1
	db $ff
.NPCMovement_41b8b:
	db WEST, MOVE_1
	db SOUTH, MOVE_3
	db $ff
.NPCMovement_41b90:
	db SOUTH, MOVE_3
	db $ff
.NPCMovement_41b93:
	db WEST, MOVE_1
	db EAST, MOVE_0
	db $ff

Script_TCGCupPlayerLost:
	set_var VAR_TCG_CHALLENGE_CUP_RESULT, CHALLENGE_CUP_RESULT_LOST
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text CupHostTCGCupPlayerLostEliminatedText
	end_dialog
	move_active_npc .NPCMovement_41bc0
	move_player .NPCMovement_41bc5, TRUE
	wait_for_player_animation
	move_player .NPCMovement_41bca, TRUE
	move_npc NPC_TCG_CUP_CLERK_LEFT, .NPCMovement_41bcd
	wait_for_player_animation
	animate_npc_movement NPC_TCG_CUP_CLERK_LEFT, $01, $01
	set_npc_direction NPC_TCG_CUP_CLERK_LEFT, SOUTH
	end_script
	ret
.NPCMovement_41bc0:
	db EAST, MOVE_1
	db SOUTH, MOVE_0
	db $ff
.NPCMovement_41bc5:
	db WEST, MOVE_1
	db SOUTH, MOVE_3
	db $ff
.NPCMovement_41bca:
	db SOUTH, MOVE_3
	db $ff
.NPCMovement_41bcd:
	db WEST, MOVE_1
	db EAST, MOVE_0
	db $ff

; clear carry if active
; set carry if inactive
CheckIfTCGCupIsActive:
	ld a, VAR_TCG_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .active
	cp CHALLENGE_CUP_2_START
	jr z, .active
	cp CHALLENGE_CUP_3_START
	jr z, .active
	scf
	ret
.active
	scf
	ccf
	ret

GrAirport_MapHeader:
	db MAP_GFX_GR_AIRPORT
	dba GrAirport_MapScripts
	db MUSIC_GR_OVERWORLD

GrAirport_StepEvents:
	map_exit 13, 9, MAP_GR_AIRPORT_ENTRANCE, 1, 6, EAST
	map_exit 13, 10, MAP_GR_AIRPORT_ENTRANCE, 1, 7, EAST
	_ow_coordinate_function 5, 8, 0, 0, 0, 3, Func_41dac
	ow_script 4, 9, Func_41dac
	ow_script 3, 9, Func_41dac
	_ow_coordinate_function 2, 8, 0, 0, 0, 1, Func_41dac
	db $ff

GrAirport_NPCs:
	npc NPC_GR_5, 4, 8, SOUTH, NULL
	db $ff

GrAirport_NPCInteractions:
	npc_script NPC_GR_5, Func_41cd3
	db $ff

GrAirport_MapScripts:
	dbw OWMODE_IDLE, Func_41c44
	dbw OWMODE_STEP_EVENT, Func_41c50
	dbw OWMODE_INTERACT, Func_41ccb
	dbw OWMODE_NPC_POSITION, Func_41c57
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_41c60
	dbw OWMODE_WARP_END_SFX, Func_41ca1
	db $ff

Func_41c44:
	call DoFrame
	call Func_41db4
	call HandleOverworldPlayerInput
	scf
	ccf
	ret

Func_41c50:
	ld hl, GrAirport_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_41c57:
	ld hl, GrAirport_NPCs
	call LoadNPCs
	scf
	ccf
	ret

Func_41c60:
	farcall ClearwD986
	ld a, [wPrevMap]
	cp OVERWORLD_MAP_GR
	jr nz, .asm_41c9f
	ld a, OWMODE_SCRIPT
	ld [wOverworldMode], a
	ld a, BANK(Script_GR5_GRAirportLanded)
	ld [wOverworldScriptBank], a
	ld hl, Script_GR5_GRAirportLanded
	ld a, l
	ld [wOverworldScriptPointer], a
	ld a, h
	ld [wOverworldScriptPointer + 1], a
	ld a, NPC_GR_5
	lb de, 5, 5
	farcall SetOWObjectTilePosition
	ld b, WEST
	farcall SetOWObjectDirection
	ld a, [wPlayerOWObject]
	lb de, 5, 5
	farcall SetOWObjectTilePosition
	ld b, WEST
	farcall SetOWObjectDirection
.asm_41c9f
	scf
	ret

Func_41ca1:
	ld a, [wTempPrevMap]
	cp OVERWORLD_MAP_GR
	jr z, .asm_41caa
	scf
	ret
.asm_41caa
	ld a, $0a
	ld c, $07
	farcall InitMusicFadeOut
	farcall MusicFadeOut
	xor a
	call PlaySong
	ld a, $07
	call SetVolume
	ld a, SFX_GR_BLIMP_HATCH_CLOSE
	call PlaySFX
	farcall WaitForSFXToFinish
	scf
	ccf
	ret

Func_41ccb:
	ld hl, GrAirport_NPCInteractions
	call Func_328c
	scf
	ret

Func_41cd3:
	ld a, NPC_GR_5
	ld [wScriptNPC], a
	ldtx hl, DialogGR5Text
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	npc_ask_question GRAirportGR5FlightPromptText, TRUE
	script_jump_if_b0nz .start_flight
	print_npc_text GRAirportGR5DeclinedFlightText
	end_dialog
	end_script
	ret
.start_flight
	print_npc_text GRAirportGR5StartFlightText
	end_dialog
	get_player_direction
	compare_loaded_var WEST
	script_jump_if_b0nz .ows_41d0d
	compare_loaded_var EAST
	script_jump_if_b0nz .ows_41d2b
	get_player_x_position
	compare_loaded_var $04
	script_jump_if_b0nz .ows_41d17
	script_jump .ows_41d21
.ows_41d0d
	move_active_npc .NPCMovement_41d42
	move_player .NPCMovement_41d4c, TRUE
	script_jump .ows_41d32
.ows_41d17
	move_active_npc .NPCMovement_41d42
	move_player .NPCMovement_41d53, TRUE
	script_jump .ows_41d32
.ows_41d21
	move_active_npc .NPCMovement_41d47
	move_player .NPCMovement_41d58, TRUE
	script_jump .ows_41d32
.ows_41d2b
	move_active_npc .NPCMovement_41d47
	move_player .NPCMovement_41d5d, TRUE
.ows_41d32
	wait_for_player_animation
	do_frames 60
	end_script
	ld a, OVERWORLD_MAP_GR
	lb de, 0, 0
	ld b, NORTH
	farcall SetWarpData
	ret
.NPCMovement_41d42:
	db NORTH, MOVE_3
	db EAST, MOVE_2
	db $ff
.NPCMovement_41d47:
	db NORTH, MOVE_3
	db EAST, MOVE_3
	db $ff
.NPCMovement_41d4c:
	db WEST, MOVE_1
	db NORTH, MOVE_3
	db EAST, MOVE_1
	db $ff
.NPCMovement_41d53:
	db NORTH, MOVE_4
	db EAST, MOVE_1
	db $ff
.NPCMovement_41d58:
	db NORTH, MOVE_4
	db EAST, MOVE_2
	db $ff
.NPCMovement_41d5d:
	db EAST, MOVE_1
	db NORTH, MOVE_3
	db EAST, MOVE_2
	db $ff

Script_GR5_GRAirportLanded:
	xor a
	start_script
	do_frames 60
	move_player .NPCMovement_41da5, TRUE
	wait_for_player_animation
	move_npc NPC_GR_5, .NPCMovement_41da0
	wait_for_player_animation
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .postgame
	check_event EVENT_SHORT_GR_ISLAND_FLYOVER_SEQUENCE
	script_jump_if_b0z .repeat
	set_event EVENT_SHORT_GR_ISLAND_FLYOVER_SEQUENCE
	start_dialog
	print_npc_text GRAirportGR5LandedInitialText
	end_dialog
	script_jump .done
.repeat
	set_active_npc NPC_GR_5, DialogGR5Text
	start_dialog
	print_npc_text GRAirportGR5LandedRepeatText
	end_dialog
	script_jump .done
.postgame
	send_mail $0e
	script_jump .repeat
.done
	end_script
	ld a, OWMODE_IDLE
	ld [wOverworldMode], a
	ret
.NPCMovement_41da0:
	db WEST, MOVE_1
	db SOUTH, MOVE_3
	db $ff
.NPCMovement_41da5:
	db WEST, MOVE_1
	db SOUTH, MOVE_4
	db NORTH, MOVE_0
	db $ff

Func_41dac:
	call Func_41db4
	farcall OverworldResumeAndHandlePlayerMoveInput
	ret

Func_41db4:
	ld a, [wPlayerOWObject]
	farcall GetOWObjectTilePosition
	cpcoord 5, 8
	jr z, .asm_41ded
	cpcoord 4, 9
	jr z, .asm_41e09
	cpcoord 3, 9
	jr z, .asm_41e25
	cpcoord 2, 8
	jr z, .asm_41e41
	jr .asm_41e5b
.asm_41ded
	ldh a, [hKeysHeld]
	bit B_PAD_LEFT, a
	jr z, .asm_41e5b
	ld a, [wPlayerOWObject]
	ld b, WEST
	farcall SetOWObjectDirection
	ld a, NPC_GR_5
	ld b, EAST
	farcall SetOWObjectDirection
	call Func_41e75
	jr .asm_41e5b
.asm_41e09
	ldh a, [hKeysHeld]
	bit B_PAD_UP, a
	jr z, .asm_41e5b
	ld a, [wPlayerOWObject]
	ld b, NORTH
	farcall SetOWObjectDirection
	ld a, NPC_GR_5
	ld b, SOUTH
	farcall SetOWObjectDirection
	call Func_41e75
	jr .asm_41e5b
.asm_41e25
	ldh a, [hKeysHeld]
	bit B_PAD_UP, a
	jr z, .asm_41e5b
	ld a, [wPlayerOWObject]
	ld b, NORTH
	farcall SetOWObjectDirection
	ld a, NPC_GR_5
	ld b, SOUTH
	farcall SetOWObjectDirection
	call Func_41e5c
	jr .asm_41e5b
.asm_41e41
	ldh a, [hKeysHeld]
	bit B_PAD_RIGHT, a
	jr z, .asm_41e5b
	ld a, [wPlayerOWObject]
	ld b, EAST
	farcall SetOWObjectDirection
	ld a, NPC_GR_5
	ld b, WEST
	farcall SetOWObjectDirection
	call Func_41e5c
.asm_41e5b
	ret

Func_41e5c:
	ld a, NPC_GR_5
	farcall GetOWObjectTilePosition
	ld a, $03
	cp d
	ret z
	ld a, NPC_GR_5
	lb bc, WEST | MOVE_BACKWARDS, MOVE_SPEED_WALK
	farcall Func_10e3c
	ld a, NPC_GR_5
	call Func_336d
	ret

Func_41e75:
	ld a, NPC_GR_5
	farcall GetOWObjectTilePosition
	ld a, $04
	cp d
	ret z
	ld a, NPC_GR_5
	lb bc, EAST | MOVE_BACKWARDS, MOVE_SPEED_WALK
	farcall Func_10e3c
	ld a, NPC_GR_5
	call Func_336d
	ret

SealedFort_MapHeader:
	db MAP_GFX_SEALED_FORT
	dba SealedFort_MapScripts
	db MUSIC_FORT_4

SealedFort_StepEvents:
	map_exit 5, 12, MAP_SEALED_FORT_ENTRANCE, 4, 1, SOUTH
	map_exit 6, 12, MAP_SEALED_FORT_ENTRANCE, 5, 1, SOUTH
	map_exit 7, 12, MAP_SEALED_FORT_ENTRANCE, 6, 1, SOUTH
	ow_script 6, 6, Func_41f66
	db $ff

SealedFort_OWInteractions:
	ow_script 6, 2, Func_41fe1
	ow_script 2, 2, Func_4207e
	ow_script 3, 2, Func_42119
	ow_script 4, 2, Func_421b4
	ow_script 5, 2, Func_4224f
	ow_script 9, 2, Func_422ec
	ow_script 7, 2, Func_42387
	ow_script 8, 2, Func_42424
	ow_script 10, 2, Func_424bf
	db $ff

SealedFort_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_41f14
	dbw OWMODE_INTERACT, Func_41f1b
	dbw OWMODE_AFTER_DUEL, Func_41f36
	db $ff

Func_41f14:
	ld hl, SealedFort_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_41f1b:
	ld a, [wPlayerOWObject]
	farcall GetOWObjectAnimStruct1Flag0And1
	ld a, b
	cp $00
	ld a, [wPlayerOWObject]
	jr nz, .asm_41f34
	farcall GetOWObjectTilePosition
	ld hl, SealedFort_OWInteractions
	call ExecuteCoordScript
.asm_41f34
	scf
	ret

Func_41f36:
	ld hl, SealedFort_AfterDuelScripts
	ld a, [wScriptNPC]
	call ExecuteNPCScript
	scf
	ret

SealedFort_AfterDuelScripts:
	npc_script NPC_TOBICHAN, Func_42050
	npc_script NPC_EIJI, Func_420eb
	npc_script NPC_MAGICIAN, Func_42186
	npc_script NPC_TOSHIRON, Func_42221
	npc_script NPC_PIERROT, Func_422be
	npc_script NPC_ANNA, Func_42359
	npc_script NPC_DEE, Func_423f6
	npc_script NPC_MASQUERADE, Func_42491
	npc_script NPC_YUI, Func_4252c
	db $ff

Func_41f66:
	ld a, NPC_TOBICHAN
	ld [wScriptNPC], a
	ldtx hl, DialogTobichanText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	ld a, EVENT_SET_UNTIL_MAP_RELOAD_1
	farcall GetEventValue
	jr nz, .asm_41fd9
	xor a
	start_script
	set_player_direction NORTH
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0z .ows_41f8e
	start_dialog
	print_npc_text Text1088
	end_dialog
.ows_41f8e
	set_event EVENT_SET_UNTIL_MAP_RELOAD_1
	scroll_to_position $02, $00
	load_npc NPC_TOBICHAN, 6, 2, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $56
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	move_active_npc .NPCMovement_41fde
	scroll_to_position $02, $02
	wait_for_player_animation
	scroll_to_player
	start_dialog
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0z .ows_41fc2
	set_event EVENT_GHOST_MASTER_STATUES_STATE
	print_npc_text Text1089
	script_jump .ows_41fc5
.ows_41fc2
	print_npc_text Text108a
.ows_41fc5
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $56
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_TOBICHAN
	end_script
.asm_41fd9
	farcall OverworldResumeAndHandlePlayerMoveInput
	ret
.NPCMovement_41fde:
	db SOUTH, MOVE_1
	db $ff

Func_41fe1:
	ld a, NPC_TOBICHAN
	ld [wScriptNPC], a
	ldtx hl, DialogTobichanText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogTobichanText
	set_text_ram2b Text108b
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_42044
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_42044
	print_npc_text Text108e
	end_dialog
	move_player .NPCMovement_42047, TRUE
	wait_for_player_animation
	load_npc NPC_TOBICHAN, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $56
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_TOBICHAN
	script_jump_if_b0z .ows_4203b
	set_event EVENT_BATTLED_TOBICHAN
	print_npc_text Text108f
	script_jump .ows_4203e
.ows_4203b
	print_npc_text Text1090
.ows_4203e
	end_dialog
	start_duel POISON_STORM_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_42044
	end_dialog
	end_script
	ret
.NPCMovement_42047:
	db WEST, MOVE_2
	db SOUTH, MOVE_4
	db EAST, MOVE_2
	db NORTH, MOVE_0
	db $ff

Func_42050:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_42066
	print_npc_text Text1091
	give_booster_packs BoosterList_cdbe
	print_npc_text Text1092
	script_jump .ows_42069
.ows_42066
	print_npc_text Text1093
.ows_42069
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $56
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_TOBICHAN
	end_script
	ret

Func_4207e:
	ld a, NPC_EIJI
	ld [wScriptNPC], a
	ldtx hl, DialogEijiText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogEijiText
	set_text_ram2b Text1094
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_420e1
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_420e1
	print_npc_text Text1095
	end_dialog
	move_player .NPCMovement_420e4, TRUE
	wait_for_player_animation
	load_npc NPC_EIJI, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $57
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_EIJI
	script_jump_if_b0z .ows_420d8
	set_event EVENT_BATTLED_EIJI
	print_npc_text Text1096
	script_jump .ows_420db
.ows_420d8
	print_npc_text Text1097
.ows_420db
	end_dialog
	start_duel EVERYBODYS_FRIEND_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_420e1
	end_dialog
	end_script
	ret
.NPCMovement_420e4:
	db SOUTH, MOVE_4
	db EAST, MOVE_4
	db NORTH, MOVE_0
	db $ff

Func_420eb:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_42101
	print_npc_text Text1098
	give_booster_packs BoosterList_cdbe
	print_npc_text Text1099
	script_jump .ows_42104
.ows_42101
	print_npc_text Text109a
.ows_42104
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $57
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_EIJI
	end_script
	ret

Func_42119:
	ld a, NPC_MAGICIAN
	ld [wScriptNPC], a
	ldtx hl, DialogMagicianText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogMagicianText
	set_text_ram2b Text109b
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_4217c
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_4217c
	print_npc_text Text109c
	end_dialog
	move_player .NPCMovement_4217f, TRUE
	wait_for_player_animation
	load_npc NPC_MAGICIAN, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $58
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_MAGICIAN
	script_jump_if_b0z .ows_42173
	set_event EVENT_BATTLED_MAGICIAN
	print_npc_text Text109d
	script_jump .ows_42176
.ows_42173
	print_npc_text Text109e
.ows_42176
	end_dialog
	start_duel IMMORTAL_POKEMON_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_4217c
	end_dialog
	end_script
	ret
.NPCMovement_4217f:
	db SOUTH, MOVE_4
	db EAST, MOVE_3
	db NORTH, MOVE_0
	db $ff

Func_42186:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_4219c
	print_npc_text Text109f
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10a0
	script_jump .ows_4219f
.ows_4219c
	print_npc_text Text10a1
.ows_4219f
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $58
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_MAGICIAN
	end_script
	ret

Func_421b4:
	ld a, NPC_TOSHIRON
	ld [wScriptNPC], a
	ldtx hl, DialogToshironText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogToshironText
	set_text_ram2b Text10a2
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_42217
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_42217
	print_npc_text Text10a3
	end_dialog
	move_player .NPCMovement_4221a, TRUE
	wait_for_player_animation
	load_npc NPC_TOSHIRON, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $59
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_TOSHIRON
	script_jump_if_b0z .ows_4220e
	set_event EVENT_BATTLED_TOSHIRON
	print_npc_text Text10a4
	script_jump .ows_42211
.ows_4220e
	print_npc_text Text10a5
.ows_42211
	end_dialog
	start_duel TRAINER_IMPRISON_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_42217
	end_dialog
	end_script
	ret
.NPCMovement_4221a:
	db SOUTH, MOVE_4
	db EAST, MOVE_2
	db NORTH, MOVE_0
	db $ff

Func_42221:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_42237
	print_npc_text Text10a6
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10a7
	script_jump .ows_4223a
.ows_42237
	print_npc_text Text10a8
.ows_4223a
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $59
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_TOSHIRON
	end_script
	ret

Func_4224f:
	ld a, NPC_PIERROT
	ld [wScriptNPC], a
	ldtx hl, DialogPierrotText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogPierrotText
	set_text_ram2b Text10a9
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_422b2
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_422b2
	print_npc_text Text10aa
	end_dialog
	move_player .NPCMovement_422b5, TRUE
	wait_for_player_animation
	load_npc NPC_PIERROT, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $5a
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_PIERROT
	script_jump_if_b0z .ows_422a9
	set_event EVENT_BATTLED_PIERROT
	print_npc_text Text10ab
	script_jump .ows_422ac
.ows_422a9
	print_npc_text Text10ac
.ows_422ac
	end_dialog
	start_duel BLAZING_FLAME_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_422b2
	end_dialog
	end_script
	ret
.NPCMovement_422b5:
	db WEST, MOVE_1
	db SOUTH, MOVE_4
	db EAST, MOVE_2
	db NORTH, MOVE_0
	db $ff

Func_422be:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_422d4
	print_npc_text Text10ad
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10ae
	script_jump .ows_422d7
.ows_422d4
	print_npc_text Text10af
.ows_422d7
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $5a
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_PIERROT
	end_script
	ret

Func_422ec:
	ld a, NPC_ANNA
	ld [wScriptNPC], a
	ldtx hl, DialogAnnaText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogAnnaText
	set_text_ram2b Text10b0
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_4234f
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_4234f
	print_npc_text Text10b1
	end_dialog
	move_player .NPCMovement_42352, TRUE
	wait_for_player_animation
	load_npc NPC_ANNA, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $5b
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_ANNA
	script_jump_if_b0z .ows_42346
	set_event EVENT_BATTLED_ANNA
	print_npc_text Text10b2
	script_jump .ows_42349
.ows_42346
	print_npc_text Text10b3
.ows_42349
	end_dialog
	start_duel DAMAGE_CHAOS_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_4234f
	end_dialog
	end_script
	ret
.NPCMovement_42352:
	db SOUTH, MOVE_4
	db WEST, MOVE_3
	db NORTH, MOVE_0
	db $ff

Func_42359:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_4236f
	print_npc_text Text10b4
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10b5
	script_jump .ows_42372
.ows_4236f
	print_npc_text Text10b6
.ows_42372
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $5b
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_ANNA
	end_script
	ret

Func_42387:
	ld a, NPC_DEE
	ld [wScriptNPC], a
	ldtx hl, DialogDeeText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogDeeText
	set_text_ram2b Text10b7
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_423ea
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_423ea
	print_npc_text Text10b8
	end_dialog
	move_player .NPCMovement_423ed, TRUE
	wait_for_player_animation
	load_npc NPC_DEE, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $5c
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_DEE
	script_jump_if_b0z .ows_423e1
	set_event EVENT_BATTLED_DEE
	print_npc_text Text10b9
	script_jump .ows_423e4
.ows_423e1
	print_npc_text Text10ba
.ows_423e4
	end_dialog
	start_duel BIG_THUNDER_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_423ea
	end_dialog
	end_script
	ret
.NPCMovement_423ed:
	db EAST, MOVE_1
	db SOUTH, MOVE_4
	db WEST, MOVE_2
	db NORTH, MOVE_0
	db $ff

Func_423f6:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_4240c
	print_npc_text Text10bb
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10bc
	script_jump .ows_4240f
.ows_4240c
	print_npc_text Text10bd
.ows_4240f
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $5c
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_DEE
	end_script
	ret

Func_42424:
	ld a, NPC_MASQUERADE
	ld [wScriptNPC], a
	ldtx hl, DialogMasqueradeText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogMasqueradeText
	set_text_ram2b Text10be
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_42487
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_42487
	print_npc_text Text10bf
	end_dialog
	move_player .NPCMovement_4248a, TRUE
	wait_for_player_animation
	load_npc NPC_MASQUERADE, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $5d
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_MASQUERADE
	script_jump_if_b0z .ows_4247e
	set_event EVENT_BATTLED_MASQUERADE
	print_npc_text Text10c0
	script_jump .ows_42481
.ows_4247e
	print_npc_text Text10c1
.ows_42481
	end_dialog
	start_duel POWER_OF_DARKNESS_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_42487
	end_dialog
	end_script
	ret
.NPCMovement_4248a:
	db SOUTH, MOVE_4
	db WEST, MOVE_2
	db NORTH, MOVE_0
	db $ff

Func_42491:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_424a7
	print_npc_text Text10c2
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10c3
	script_jump .ows_424aa
.ows_424a7
	print_npc_text Text10c4
.ows_424aa
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $5d
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_MASQUERADE
	end_script
	ret

Func_424bf:
	ld a, NPC_YUI
	ld [wScriptNPC], a
	ldtx hl, DialogYuiText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_text_ram2 DialogYuiText
	set_text_ram2b Text10c5
	print_text Text108c
	check_event EVENT_GHOST_MASTER_STATUES_STATE
	script_jump_if_b0nz .ows_42522
	ask_question Text108d, TRUE
	script_jump_if_b0z .ows_42522
	print_npc_text Text10c6
	end_dialog
	move_player .NPCMovement_42525, TRUE
	wait_for_player_animation
	load_npc NPC_YUI, 6, 3, SOUTH
	play_sfx SFX_GHOST_MASTER_APPEAR
	quit_script
	ld a, $b0
	ld hl, Data_4257f
	call Func_4255a
	farcall SetOWObjectSpriteAnimFlag6
	ld a, $01
	start_script
	start_dialog
	check_event EVENT_BATTLED_YUI
	script_jump_if_b0z .ows_42519
	set_event EVENT_BATTLED_YUI
	print_npc_text Text10c7
	script_jump .ows_4251c
.ows_42519
	print_npc_text Text10c8
.ows_4251c
	end_dialog
	start_duel TORRENTIAL_FLOOD_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.ows_42522
	end_dialog
	end_script
	ret
.NPCMovement_42525:
	db SOUTH, MOVE_4
	db WEST, MOVE_4
	db NORTH, MOVE_0
	db $ff

Func_4252c:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_42542
	print_npc_text Text10c9
	give_booster_packs BoosterList_cdbe
	print_npc_text Text10ca
	script_jump .ows_42545
.ows_42542
	print_npc_text Text10cb
.ows_42545
	end_dialog
	play_sfx SFX_GHOST_MASTER_DISAPPEAR
	quit_script
	ld a, $b0
	ld hl, Data_425ae
	call Func_4255a
	ld a, $01
	start_script
	unload_npc NPC_YUI
	end_script
	ret

Func_4255a:
	push af
	push bc
	push hl
	ld c, a
.asm_4255e
	ld a, [hl]
	cp $ff
	jr z, .asm_4257b
	bit 7, a
	jr z, .asm_4256e
	ld a, c
	farcall SetOWObjectSpriteAnimFlag6
	jr .asm_42573
.asm_4256e
	ld a, c
	farcall ResetOWObjectSpriteAnimFlag6
.asm_42573
	ld a, [hli]
	res 7, a
	call WaitAFrames
	jr .asm_4255e
.asm_4257b
	pop hl
	pop bc
	pop af
	ret

Data_4257f:
	db $8a, $0a, $89, $09, $88, $08, $87, $07, $86, $06
	db $85, $05, $84, $04, $83, $02, $82, $02, $82, $02
	db $82, $02, $82, $02, $81, $01, $81, $01, $81, $01
	db $81, $01, $81, $01, $81, $01, $81, $01, $81, $01
	db $81, $01, $81, $01, $81, $01, $ff

Data_425ae:
	db $88, $08, $87, $07, $86, $06, $85, $05, $84, $04
	db $83, $02, $82, $02, $82, $02, $81, $01, $81, $01
	db $81, $01, $81, $01, $81, $01, $81, $01, $81, $01
	db $81, $01, $ff

GrChallengeHall_MapHeader:
	db MAP_GFX_GR_CHALLENGE_HALL
	dba GrChallengeHall_MapScripts
	db MUSIC_GR_OVERWORLD

GrChallengeHall_StepEvents:
	map_exit 7, 15, MAP_GR_CHALLENGE_HALL_ENTRANCE, 4, 1, SOUTH
	map_exit 8, 15, MAP_GR_CHALLENGE_HALL_ENTRANCE, 5, 1, SOUTH
	db $ff

GrChallengeHall_NPCs:
	npc NPC_CUP_HOST, 7, 2, SOUTH, GrChallengeHall_AppearDuringGRCups
	npc NPC_GR_CUP_CLERK_LEFT, 4, 8, SOUTH, GrChallengeHall_AppearDuringGRCups
	npc NPC_GR_CUP_CLERK_RIGHT, 11, 8, SOUTH, GrChallengeHall_AppearDuringGRCups
	npc NPC_GR_STAFF, 8, 6, NORTH, GrChallengeHall_DisappearDuringGRCups
	db $ff

GrChallengeHall_NPCInteractions:
	npc_script NPC_GR_CUP_CLERK_LEFT, Script_GRCupClerkLeft
	npc_script NPC_GR_CUP_CLERK_RIGHT, Script_GRCupClerkRight
	npc_script NPC_GR_STAFF, Script_GRChallengeMachineStaff
	db $ff

GrChallengeHall_OWInteractions:
	ow_script 7, 3, Script_GRChallengeMachine
	ow_script 8, 3, Script_GRChallengeMachine
	db $ff

GrChallengeHall_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_42639
	dbw OWMODE_INTERACT, Func_426b9
	dbw OWMODE_AFTER_DUEL, Func_426d6
	dbw OWMODE_AFTER_DUEL_PRELOAD, Func_426c9
	dbw OWMODE_NPC_POSITION, Func_42640
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_4266e
	dbw OWMODE_MUSIC_PRELOAD, Func_42649
	dbw OWMODE_CONTINUE_OW, Func_426f8
	db $ff

Func_42639:
	ld hl, GrChallengeHall_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_42640:
	ld hl, GrChallengeHall_NPCs
	call LoadNPCs
	scf
	ccf
	ret

Func_42649:
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .active
	cp CHALLENGE_CUP_2_START
	jr z, .active
	cp CHALLENGE_CUP_3_UNLOCKED
	jr z, .inactive_has_machine
	jr nc, .active
	scf
	ret
.active
	ld a, MUSIC_GR_CHALLENGE_CUP
	ld [wNextMusic], a
	jr .done
.inactive_has_machine
	ld a, MAP_GFX_GR_CHALLENGE_HALL_2
	ld [wNextMapGfx], a
.done
	scf
	ccf
	ret

Func_4266e:
	call CheckIfGRCupIsActive
	jr nc, .active
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_3_UNLOCKED
	jr nz, .done
; has machine and staff
	ld a, NPC_GR_STAFF
	lb de, 9, 3
	ld b, SOUTH
	farcall SetOWObjectTilePositionAndDirection
	jr .done
.active
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_3_START
	jr nz, .set_opponents_draw_round1
	ld bc, TILEMAP_057
	lb de, 5, 0
	farcall Func_12c0ce
.set_opponents_draw_round1
	ld a, GR_ISLAND
	farcall SetChallengeCupOpponents
	ld a, VAR_CHALLENGECUP_ROUND1_OPPONENT_DECK_ID
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	lb de, 9, 4
	ld b, WEST
	farcall LoadOWObjectInMap
.done
	scf
	ret

Func_426b9:
	ld hl, GrChallengeHall_NPCInteractions
	call Func_328c
	jr nc, .asm_426c7
	ld hl, GrChallengeHall_OWInteractions
	call Func_32bf
.asm_426c7
	scf
	ret

Func_426c9:
	ld a, EVENT_EB
	farcall GetEventValue
	jr nz, .asm_426d3
	scf
	ret
.asm_426d3
	scf
	ccf
	ret

Func_426d6:
	ld a, EVENT_EB
	farcall GetEventValue
	jr nz, .asm_426ef
	ld a, VAR_CHALLENGECUP_CURRENT_ROUND
	farcall GetVarValue
	cp 2
	jp c, Script_GRCupRound1AfterDuel
	jp z, Script_GRCupRound2AfterDuel
	jp Script_GRCupRound3AfterDuel
.asm_426ef
	farcall LoadChallengeMachineSave
	call Func_42776
	scf
	ret

Func_426f8:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall GetEventValue
	jr nz, .asm_42702
	scf
	ret
.asm_42702
	farcall OverworldResumeWithCurSong
	call Func_4270c
	scf
	ccf
	ret

Func_4270c:
	xor a
	start_script
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	set_active_npc_position 9, 2
	get_var VAR_CHALLENGECUP_CURRENT_ROUND
	compare_loaded_var 2
	script_jump_if_b0nz Script_GRCupRound2Start
	script_jump Script_GRCupRound3Start

Script_GRChallengeMachine:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	farcall GetEventValue
	ret z
	farcall ValidateChallengeMachineSaveData
	jr nc, .no_error
	farcall InitChallengeMachine
	xor a
	start_script
	start_dialog
	print_text ChallengeMachineWinStreakRecordCorruptedAndResetText
	end_dialog
	end_script
.no_error
	farcall LoadChallengeMachineSave
	ld a, GR_ISLAND
	farcall SetChallengeMachineOpponents
	farcall LoadChallengeMachineOpponentTitlesAndNames
	ld a, GR_ISLAND
	lb bc, 0, 0
	farcall ChallengeMachine
	jr c, .quit
	xor a
	start_script
	set_event EVENT_EB
	set_var VAR_CHALLENGEMACHINE_CURRENT_ROUND, 1
	end_script
	farcall SetChallengeMachineDuelParams
	jr .done
.quit
	or a
	jr z, .done
	ld bc, 0
	ld a, c
	ld [wGRChallengeMachineCurWinStreak], a
	ld a, b
	ld [wGRChallengeMachineCurWinStreak + 1], a
	farcall SaveChallengeMachine
.done
	ret

Func_42776:
	ld a, EVENT_SET_UNTIL_MAP_RELOAD_2
	farcall GetEventValue
	jp z, .quit
	ld a, [wGRChallengeMachineCurWinStreak]
	ld c, a
	ld a, [wGRChallengeMachineCurWinStreak + 1]
	ld b, a
	inc bc
	cp16bc_long MAX_NUM_CHALLENGE_MACHINE_WIN_RECORD + 1
	jr nc, .skip_incr_wins
	ld a, c
	ld [wGRChallengeMachineCurWinStreak], a
	ld a, b
	ld [wGRChallengeMachineCurWinStreak + 1], a
.skip_incr_wins
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	farcall GetVarValue
	cp NUM_CHALLENGE_MACHINE_ROUNDS_PER_SET
	jr z, .finished_set
	ld b, a
	ld a, GR_ISLAND
	ld c, CHALLENGE_MACHINE_DUEL_WIN
	farcall ChallengeMachine
	jp c, .withdrew
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	ld d, a
	farcall GetVarValue
	inc a
	ld c, a
	ld a, d
	farcall SetVarValue
	farcall SetChallengeMachineDuelParams
	jp .done

.finished_set
	ld a, [wGRChallengeMachineSetsWonRecord]
	ld c, a
	ld a, [wGRChallengeMachineSetsWonRecord + 1]
	ld b, a
	inc bc
	cp16bc_long MAX_NUM_CHALLENGE_MACHINE_WIN_RECORD + 1
	jr nc, .skip_incr_record
	ld a, c
	ld [wGRChallengeMachineSetsWonRecord], a
	ld a, b
	ld [wGRChallengeMachineSetsWonRecord + 1], a
.skip_incr_record
	call .CheckWinStreakRecord
	ld a, GR_ISLAND
	lb bc, NUM_CHALLENGE_MACHINE_ROUNDS_PER_SET, CHALLENGE_MACHINE_DUEL_WIN
	farcall ChallengeMachine
	jr .check_50winstreak_prize

.quit
	ld a, VAR_CHALLENGEMACHINE_CURRENT_ROUND
	farcall GetVarValue
	ld b, a
	ld a, GR_ISLAND
	ld c, CHALLENGE_MACHINE_DUEL_LOSS_OR_QUIT
	farcall ChallengeMachine
.withdrew
	call .CheckWinStreakRecord
	ld bc, 0
	ld a, c
	ld [wGRChallengeMachineCurWinStreak], a
	ld a, b
	ld [wGRChallengeMachineCurWinStreak + 1], a
.exit
	ld a, EVENT_EB
	farcall ZeroOutEventValue
	farcall SaveChallengeMachine
.done
	ret

.check_50winstreak_prize
	ld a, [wGRChallengeMachineCurWinStreak]
	ld e, a
	ld a, [wGRChallengeMachineCurWinStreak + 1]
	ld d, a
	cp16_long NUM_CHALLENGE_MACHINE_WINS_FOR_COIN_PRIZE
	jr nz, .exit
	call Script_GRChallengeMachineStaff_50WinStreak
	jr .exit

.CheckWinStreakRecord:
	ld a, [wGRChallengeMachineCurWinStreak]
	ld c, a
	ld a, [wGRChallengeMachineCurWinStreak + 1]
	ld b, a
	ld a, [wGRChallengeMachineWinStreakRecord]
	ld e, a
	ld a, [wGRChallengeMachineWinStreakRecord + 1]
	ld d, a
	cp16bc_long de
	jr nc, .new_record
	scf
	ret
.new_record
	ld a, c
	ld [wGRChallengeMachineWinStreakRecord], a
	ld a, b
	ld [wGRChallengeMachineWinStreakRecord + 1], a
	call EnableSRAM
	ld hl, sPlayerName
	ld de, wGRChallengeMachinePlayerName
	ld bc, NAME_BUFFER_LENGTH
	call CopyDataHLtoDE_SaveRegisters
	call DisableSRAM
	scf
	ccf
	ret

Script_GRChallengeMachineStaff_50WinStreak:
	xor a
	start_script
	check_event EVENT_GOT_GENGAR_COIN
	script_jump_if_b0z .done
	set_event EVENT_GOT_GENGAR_COIN
	set_active_npc NPC_GR_STAFF, DialogStaffText
	set_active_npc_direction WEST
	set_player_direction EAST
	start_dialog
	print_npc_text GRChallengeMachineStaff50WinStreakRewards1Text
	give_coin COIN_GENGAR
	print_npc_text GRChallengeMachineStaff50WinStreakRewards2Text
	end_dialog
	set_active_npc_direction SOUTH
.done
	end_script
	ret

Script_GRCupClerkLeft:
	ld a, NPC_GR_CUP_CLERK_LEFT
	ld [wScriptNPC], a
	ldtx hl, DialogReceptionistText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	get_var VAR_GR_CHALLENGE_CUP_RESULT
	compare_loaded_var CHALLENGE_CUP_RESULT_WON
	script_jump_if_b0nz .won
	script_jump_if_b1z .lost
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_3_START
	script_jump_if_b0nz .cup3
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .cup2
; cup1
	print_npc_text GRCupClerkCup1ActiveText
	script_jump .enter_prompt
.cup2
	print_npc_text GRCupClerkCup2ActiveText
	script_jump .enter_prompt
.cup3
	quit_script
	farcall GetGRChallengeCupPrizeCardName
	call LoadTxRam2
	ld a, $01
	start_script
	print_npc_text GRCupClerkCup3ActiveText
	script_jump .enter_prompt
.won
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b1z .won_cup3
	script_jump_if_b0nz .won_cup2
; won cup1
	print_npc_text GRCupClerkCup1PlayerWonText
	script_jump .done
.won_cup2
	print_npc_text GRCupClerkCup2PlayerWonText
	script_jump .done
.won_cup3
	print_npc_text GRCupClerkCup3PlayerWonText
	script_jump .done
.lost
	print_npc_text GRCupClerkPlayerLostText
	script_jump .done
.enter_prompt
	ask_question GRCupClerkEnterPromptText, TRUE
	script_jump_if_b0z .declined
	print_npc_text GRCupClerkEnterAcceptedText
	end_dialog
	move_active_npc .NPCMovement_4291d
	move_player .NPCMovement_42927, TRUE
	wait_for_player_animation
	move_active_npc .NPCMovement_42922
	move_player .NPCMovement_4292a, TRUE
	wait_for_player_animation
	script_jump Script_GRCupRound1Start
.declined
	print_npc_text GRCupClerkEnterDeclinedText
.done
	end_dialog
	end_script
	ret
.NPCMovement_4291d:
	db EAST, MOVE_1
	db WEST, MOVE_0
	db $ff
.NPCMovement_42922:
	db WEST, MOVE_1
	db SOUTH, MOVE_0
	db $ff
.NPCMovement_42927:
	db NORTH, MOVE_2
	db $ff
.NPCMovement_4292a:
	db NORTH, MOVE_3
	db EAST, MOVE_2
	db $ff

Script_GRCupClerkRight:
	ld a, NPC_GR_CUP_CLERK_RIGHT
	ld [wScriptNPC], a
	ldtx hl, DialogReceptionistText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	print_npc_text GRCupClerkRightNoEntryText
	end_dialog
	end_script
	ret

GrChallengeHall_AppearDuringGRCups:
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .appear
	cp CHALLENGE_CUP_2_START
	jr z, .appear
	cp CHALLENGE_CUP_3_START
	jr z, .appear
	scf
	ret
.appear
	scf
	ccf
	ret

Script_GRChallengeMachineStaff:
	ld a, NPC_GR_STAFF
	ld [wScriptNPC], a
	ldtx hl, DialogStaffText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	check_event EVENT_GOT_GENGAR_COIN
	script_jump_if_b0z .has_gengar_coin
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .has_machine
	check_event EVENT_TALKED_TO_GR_CHALLENGE_MACHINE_STAFF
	script_jump_if_b0z .no_machine_repeat
; no machine initial
	set_event EVENT_TALKED_TO_GR_CHALLENGE_MACHINE_STAFF
	print_npc_text GRChallengeMachineStaffUnderConstructionInitialText
	script_jump .done
.no_machine_repeat
	print_npc_text GRChallengeMachineStaffUnderConstructionRepeatText
	script_jump .done
.has_machine
	print_npc_text GRChallengeMachineStaffWelcomeText
	script_jump .done
.has_gengar_coin
	print_npc_text GRChallengeMachineStaffWelcome50WinStreakDoneText
.done
	end_dialog
	end_script
	ret

GrChallengeHall_DisappearDuringGRCups:
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .disappear
	cp CHALLENGE_CUP_2_START
	jr z, .disappear
	cp CHALLENGE_CUP_3_START
	jr z, .disappear
	scf
	ccf
	ret
.disappear
	scf
	ret

Script_GRCupRound1Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 0
	script_jump .round1
.round1
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 1
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	set_active_npc NPC_CUP_HOST, DialogCupHostText
	start_dialog
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .cup2
	script_jump_if_b1z .cup3
; cup1
	print_npc_text CupHostGRCup1IntroText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Round1ChallengerText
	script_call Script_42d2d
	print_npc_text CupHostGRCup1Round1DuelStartText
	script_jump .start_duel
.cup2
	print_npc_text CupHostGRCup2IntroText
	script_call Script_42d1a
	print_npc_text CupHostGRCup2Round1ChallengerText
	script_call Script_42d2d
	print_npc_text CupHostGRCup1Round1DuelStartText
	script_jump .start_duel
.cup3
	print_npc_text CupHostGRCup3IntroText
	script_call Script_42d1a
	print_npc_text CupHostGRCup3Round1ChallengerText
	script_call Script_42d2d
	print_npc_text CupHostGRCup3Round1DuelStartText
	script_jump .start_duel
.start_duel
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret

Script_GRCupRound1AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round1
; won round1
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round1
	script_jump_if_b1z .won_cup3_round1
; won cup1 round1, identical to cup2
	print_npc_text CupHostGRCup1Or2Round1PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCup1Or2Round1PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round1NextChallengerText
	script_jump .proceed
.won_cup2_round1
	print_npc_text CupHostGRCup1Or2Round1PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCup1Or2Round1PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round1NextChallengerText
	script_jump .proceed
.won_cup3_round1
	print_npc_text CupHostGRCup3Round1PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCup3Round1PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup3Round1NextChallengerText
	script_jump .proceed
.lost_round1
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .lost_cup2_round1
	script_jump_if_b1z .lost_cup3_round1
; lost cup1 round1, identical to cup2
	print_npc_text CupHostGRCup1Or2Round1PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round1PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup2_round1
	print_npc_text CupHostGRCup1Or2Round1PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round1PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup3_round1
	print_npc_text CupHostGRCup3Round1PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup3Round1PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.proceed
	script_call Script_GRCupNewChallenger
	end_dialog
	script_jump Script_GRCupRound2Start

Script_GRCupRound2Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 2
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	start_dialog
	print_npc_text CupHostGRCupRound2ChallengerText
	script_call Script_42d35
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_3_START
	script_jump_if_b0z .loop_prep
	print_npc_text CupHostGRCupRound2The4PrizesMatchText
.loop_prep
	print_npc_text CupHostGRCupAreYourDecksReadyText
	ask_question DuelPrepPromptText, FALSE
	script_jump_if_b0z .start_duel
	print_npc_text CupHostGRCupMakeYourPreparationsText
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	script_jump .loop_prep
.start_duel
	print_npc_text CupHostGRCupRound2DuelStartText
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret

Script_GRCupRound2AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round2
; won round2
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round2
	script_jump_if_b1z .won_cup3_round2
; won cup1 round2
	print_npc_text CupHostGRCup1Round2PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round2NextChallengerText
	script_jump .proceed
.won_cup2_round2
	print_npc_text CupHostGRCup2Round2PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round2NextChallengerText
	script_jump .proceed
.won_cup3_round2
	print_npc_text CupHostGRCup3Round2PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup3Round2NextChallengerText
	script_jump .proceed
.lost_round2
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .lost_cup2_round2
	script_jump_if_b1z .lost_cup3_round2
; lost cup1 round2, identical to cup2
	print_npc_text CupHostGRCup1Or2Round2PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round2PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup2_round2
	print_npc_text CupHostGRCup1Or2Round2PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round2PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup3_round2
	print_npc_text CupHostGRCup3Round2PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup3Round2PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.proceed
	script_call Script_GRCupNewChallenger
	end_dialog
	script_jump Script_GRCupRound3Start

Script_GRCupRound3Start:
	set_var VAR_CHALLENGECUP_CURRENT_ROUND, 3
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	start_dialog
	print_npc_text CupHostGRCupRound3ChallengerText
	script_call Script_42d35
.loop_prep
	print_npc_text CupHostGRCupAreYourDecksReadyText
	ask_question DuelPrepPromptText, FALSE
	script_jump_if_b0z .start_duel
	print_npc_text CupHostGRCupMakeYourPreparationsText
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	script_jump .loop_prep
.start_duel
	print_npc_text CupHostGRCupRound3DuelStartText
	end_dialog
	end_script
	farcall SetChallengeCupDuelParams
	ret

Script_GRCupRound3AfterDuel:
	xor a
	start_script
	start_dialog
	quit_script
	farcall LoadChallengeCupOpponentName
	ld a, $01
	start_script
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .lost_round3
; won round3
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .won_cup2_round3
	script_jump_if_b1z .won_cup3_round3
; won cup1 round3, identical to cup2
	print_npc_text CupHostGRCup1Or2Round3PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round3PlayerChampionText
	script_jump .proceed_prize
.won_cup2_round3
	print_npc_text CupHostGRCup1Or2Round3PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup1Or2Round3PlayerChampionText
	script_jump .proceed_prize
.won_cup3_round3
	print_npc_text CupHostGRCup3Round3PlayerWonText
	script_call Script_42d22
	print_npc_text CupHostGRCupRound2Or3PlayerWonOpponentEliminatedText
	script_call Script_GRCupOpponentEliminated
	print_npc_text CupHostGRCup3Round3PlayerChampionText
	script_jump .proceed_prize
.lost_round3
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .lost_cup2_round3
	script_jump_if_b1z .lost_cup3_round3
; lost cup1 round3, identical to cup2
	print_npc_text CupHostGRCup1Or2Round3PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round3PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup2_round3
	print_npc_text CupHostGRCup1Or2Round3PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup1Or2Round3PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.lost_cup3_round3
	print_npc_text CupHostGRCup3Round3PlayerLostText
	script_call Script_42d1a
	print_npc_text CupHostGRCup3Round3PlayerLostEliminatedText
	script_jump Script_GRCupPlayerLost
.proceed_prize
	set_var VAR_GR_CHALLENGE_CUP_RESULT, CHALLENGE_CUP_RESULT_WON
	end_dialog
	move_active_npc .NPCMovement_42cd8
	wait_for_player_animation
	set_player_direction NORTH
	start_dialog
	get_var VAR_GR_CHALLENGE_CUP_STATE
	compare_loaded_var CHALLENGE_CUP_2_START
	script_jump_if_b0nz .prize_cup2
	script_jump_if_b1z .prize_cup3
; prize cup1
	print_npc_text CupHostGRCup1ChampionPrizeText
	receive_card DARK_PERSIAN_ALT_LV28
	script_jump .skip_10times_prize
.prize_cup2
	print_npc_text CupHostGRCup2ChampionPrizeText
	receive_card BILLS_COMPUTER
	script_jump .check_win_count ; redundant, jumping to .skip_10times_prize should suffice
.prize_cup3
	get_var VAR_TIMES_WON_GR_CHALLENGE_CUP
	compare_loaded_var NUM_CHALLENGE_CUP_WINS_FOR_COIN_PRIZE + 1
	script_jump_if_b1z .skip_incr_win_count
	inc_var VAR_TIMES_WON_GR_CHALLENGE_CUP
.skip_incr_win_count
	quit_script
	ld a, VAR_GR_CHALLENGE_CUP_PRIZE_INDEX
	farcall GetVarValue
	farcall GetGRChallengeCupPrizeCardName
	call LoadTxRam2
	ld a, $01
	start_script
	print_npc_text CupHostGRCup3ChampionPrizeText
	quit_script
	ld a, VAR_GR_CHALLENGE_CUP_PRIZE_INDEX
	farcall GetVarValue
	farcall GetGRChallengeCupPrizeCardID
	ld e, c
	ld d, b
	farcall Func_1022a
	farcall Func_c646
	farcall Func_10252
	call WaitPalFading
	ld a, $01
	start_script
.check_win_count
	get_var VAR_TIMES_WON_GR_CHALLENGE_CUP
	compare_loaded_var NUM_CHALLENGE_CUP_WINS_FOR_COIN_PRIZE
	script_jump_if_b0z .skip_10times_prize
	print_npc_text CupHostGRCup10TimesChampionRewards1Text
	set_event EVENT_GOT_HORSEA_COIN
	give_coin COIN_HORSEA
	print_npc_text CupHostGRCup10TimesChampionRewards2Text
	script_jump .exit
.skip_10times_prize
	print_npc_text CupHostGRCupPlayerChampionCongratsText
.exit
	end_dialog
	move_player .NPCMovement_42cdd, TRUE
	wait_for_player_animation
	move_player .NPCMovement_42ce2, TRUE
	move_npc NPC_GR_CUP_CLERK_LEFT, .NPCMovement_42ce5
	wait_for_player_animation
	animate_npc_movement NPC_GR_CUP_CLERK_LEFT, $03, $01
	set_npc_direction NPC_GR_CUP_CLERK_LEFT, SOUTH
	end_script
	ret
.NPCMovement_42cd8:
	db WEST, MOVE_3
	db SOUTH, MOVE_1
	db $ff
.NPCMovement_42cdd:
	db WEST, MOVE_2
	db SOUTH, MOVE_2
	db $ff
.NPCMovement_42ce2:
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_42ce5:
	db EAST, MOVE_1
	db WEST, MOVE_0
	db $ff

Script_GRCupPlayerLost:
	set_var VAR_GR_CHALLENGE_CUP_RESULT, CHALLENGE_CUP_RESULT_LOST
	end_dialog
	move_active_npc .NPCMovement_42d08
	move_player .NPCMovement_42d0d, TRUE
	wait_for_player_animation
	move_player .NPCMovement_42d12, TRUE
	move_npc NPC_GR_CUP_CLERK_LEFT, .NPCMovement_42d15
	wait_for_player_animation
	animate_npc_movement NPC_GR_CUP_CLERK_LEFT, $03, $01
	set_npc_direction NPC_GR_CUP_CLERK_LEFT, SOUTH
	end_script
	ret
.NPCMovement_42d08:
	db EAST, MOVE_1
	db SOUTH, MOVE_0
	db $ff
.NPCMovement_42d0d:
	db WEST, MOVE_2
	db SOUTH, MOVE_2
	db $ff
.NPCMovement_42d12:
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_42d15:
	db EAST, MOVE_1
	db WEST, MOVE_0
	db $ff

Script_42d1a:
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	script_ret

Script_42d22:
	end_dialog
	animate_active_npc_movement $01, $01
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	script_ret

Script_42d2d:
	end_dialog
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	script_ret

Script_42d35:
	end_dialog
	animate_active_npc_movement $03, $01
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	script_ret

Script_GRCupOpponentEliminated:
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_CURRENT_ROUND
	farcall GetVarValue
	ld c, VAR_CHALLENGECUP_ROUND1_OPPONENT_DECK_ID
	cp 2
	jr c, .got_opponent
	ld c, VAR_CHALLENGECUP_ROUND2_OPPONENT_DECK_ID
	jr z, .got_opponent
	ld c, VAR_CHALLENGECUP_ROUND3_OPPONENT_DECK_ID
.got_opponent
	ld a, c
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	push af
	ld b, BANK(.NPCMovement_42d9b)
	ld hl, .NPCMovement_42d9b
	farcall MoveNPC
	call Func_3340
	pop af
	push af
	ld b, BANK(.NPCMovement_42da0)
	ASSERT BANK(.NPCMovement_42da0) == BANK(.NPCMovement_42da3)
	ld hl, .NPCMovement_42da0
	farcall MoveNPC
	ld a, NPC_GR_CUP_CLERK_RIGHT
	ld hl, .NPCMovement_42da3
	farcall MoveNPC
	call Func_3340
	pop af
	farcall ClearOWObject
	ld a, NPC_GR_CUP_CLERK_RIGHT
	ld b, BANK(.NPCMovement_42da8)
	ld hl, .NPCMovement_42da8
	farcall MoveNPC
	call Func_3340
	ld a, $01
	start_script
	start_dialog
	script_ret
.NPCMovement_42d9b:
	db EAST, MOVE_2
	db SOUTH, MOVE_2
	db $ff
.NPCMovement_42da0:
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_42da3:
	db WEST, MOVE_1
	db EAST, MOVE_0
	db $ff
.NPCMovement_42da8:
	db EAST, MOVE_1
	db SOUTH, MOVE_0
	db $ff

Script_GRCupNewChallenger:
	end_dialog
	quit_script
	ld a, VAR_CHALLENGECUP_CURRENT_ROUND
	farcall GetVarValue
	ld c, VAR_CHALLENGECUP_ROUND2_OPPONENT_DECK_ID
	cp 2
	jr c, .got_opponent
	ld c, VAR_CHALLENGECUP_ROUND3_OPPONENT_DECK_ID
.got_opponent
	ld a, c
	farcall GetVarValue
	farcall GetNPCByDeck_AdjustAmy
	push af
	ld b, NORTH
	lb de, 11, 10
	farcall LoadOWObjectInMap
	ld b, BANK(.NPCMovement_42e02)
	ASSERT BANK(.NPCMovement_42e02) == BANK(.NPCMovement_42e0a)
	ld hl, .NPCMovement_42e02
	farcall MoveNPC
	ld a, NPC_GR_CUP_CLERK_RIGHT
	ld hl, .NPCMovement_42e0a
	farcall MoveNPC
	call Func_3340
	pop af
	ld b, BANK(.NPCMovement_42e05)
	ASSERT BANK(.NPCMovement_42e05) == BANK(.NPCMovement_42e0f)
	ld hl, .NPCMovement_42e05
	farcall MoveNPC
	ld a, NPC_GR_CUP_CLERK_RIGHT
	ld hl, .NPCMovement_42e0f
	farcall MoveNPC
	call Func_3340
	ld a, $01
	start_script
	start_dialog
	script_ret
.NPCMovement_42e02:
	db NORTH, MOVE_3
	db $ff
.NPCMovement_42e05:
	db NORTH, MOVE_3
	db WEST, MOVE_2
	db $ff
.NPCMovement_42e0a:
	db WEST, MOVE_1
	db SOUTH, MOVE_0
	db $ff
.NPCMovement_42e0f:
	db EAST, MOVE_1
	db SOUTH, MOVE_0
	db $ff

; clear carry if active
; set carry if inactive
CheckIfGRCupIsActive:
	ld a, VAR_GR_CHALLENGE_CUP_STATE
	farcall GetVarValue
	cp CHALLENGE_CUP_1_START
	jr z, .active
	cp CHALLENGE_CUP_2_START
	jr z, .active
	cp CHALLENGE_CUP_3_START
	jr z, .active
	scf
	ret
.active
	scf
	ccf
	ret

FightingFortMaze1_MapHeader:
	db MAP_GFX_FIGHTING_FORT_MAZE_1
	dba FightingFortMaze1_MapScripts
	db MUSIC_FORT_3

FightingFortMaze1_StepEvents:
	map_exit 4, 8, MAP_FIGHTING_FORT, 3, 2, SOUTH
	map_exit 5, 8, MAP_FIGHTING_FORT, 3, 2, SOUTH
	map_exit 4, 0, MAP_FIGHTING_FORT_MAZE_6, 4, 7, NORTH
	map_exit 5, 0, MAP_FIGHTING_FORT_MAZE_6, 5, 7, NORTH
	map_exit 9, 3, MAP_FIGHTING_FORT_MAZE_2, 1, 3, EAST
	map_exit 9, 4, MAP_FIGHTING_FORT_MAZE_2, 1, 4, EAST
	db $ff

FightingFortMaze1_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_42e6e
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_42e75
	db $ff

Func_42e6e:
	ld hl, FightingFortMaze1_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_42e75:
	ld bc, $53
	ld a, $05
	farcall SetwD896
	scf
	ret

FightingFortMaze21_MapHeader:
	db MAP_GFX_FIGHTING_FORT_MAZE_21
	dba FightingFortMaze21_MapScripts
	db MUSIC_FORT_3

FightingFortMaze21_StepEvents:
	map_exit 4, 8, MAP_FIGHTING_FORT_MAZE_17, 4, 1, SOUTH
	map_exit 5, 8, MAP_FIGHTING_FORT_MAZE_17, 5, 1, SOUTH
	map_exit 0, 3, MAP_FIGHTING_FORT_MAZE_20, 8, 3, WEST
	map_exit 0, 4, MAP_FIGHTING_FORT_MAZE_20, 8, 4, WEST
	map_exit 9, 3, MAP_FIGHTING_FORT_MAZE_22, 1, 3, EAST
	map_exit 9, 4, MAP_FIGHTING_FORT_MAZE_22, 1, 4, EAST
	db $ff

FightingFortMaze21_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_42ec3
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_42eca
	db $ff

Func_42ec3:
	ld hl, FightingFortMaze21_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_42eca:
	ld bc, $53
	ld a, $05
	farcall SetwD896
	scf
	ret

GrCastleBiruritchi_MapHeader:
	db MAP_GFX_GR_CASTLE_BIRURITCHI
	dba GrCastleBiruritchi_MapScripts
	db MUSIC_GR_CASTLE

GrCastleBiruritchi_StepEvents:
	map_exit 6, 15, MAP_GR_CASTLE, 6, 1, SOUTH
	map_exit 7, 15, MAP_GR_CASTLE, 7, 1, SOUTH
	map_exit 8, 15, MAP_GR_CASTLE, 8, 1, SOUTH
	db $ff

GrCastleBiruritchi_NPCs:
	npc NPC_BIRURITCHI, 7, 5, SOUTH, NULL
	db $ff

GrCastleBiruritchi_NPCInteractions:
	npc_script NPC_BIRURITCHI, Func_43136
	db $ff

GrCastleBiruritchi_OWInteractions:
	ow_script 4, 12, Func_431bf
	ow_script 10, 12, Func_431bf
	db $ff

GrCastleBiruritchi_MapScripts:
	dbw OWMODE_STEP_EVENT, Func_42f2e
	dbw OWMODE_INTERACT, Func_42fad
	dbw OWMODE_AFTER_DUEL, Func_42fbd
	dbw OWMODE_NPC_POSITION, Func_42f35
	dbw OWMODE_WARP_FADE_IN_PRELOAD, Func_42f3e
	dbw OWMODE_CONTINUE_OW, Func_42fcd
	dbw OWMODE_WARP_FADE_OUT_PRELOAD, Func_42f86
	dbw OWMODE_WARP_END_SFX, Func_42fa1
	db $ff

Func_42f2e:
	ld hl, GrCastleBiruritchi_StepEvents
	call ExecutePlayerCoordScript
	ret

Func_42f35:
	ld hl, GrCastleBiruritchi_NPCs
	call LoadNPCs
	scf
	ccf
	ret

Func_42f3e:
	ld a, EVENT_GR_CASTLE_STAIRS_RUI_ROADBLOCK
	farcall GetEventValue
	jr nz, .asm_42f84
	ld a, OWMODE_SCRIPT
	ld [wOverworldMode], a
	ld a, BANK(Func_43050)
	ld [wOverworldScriptBank], a
	ld hl, Func_43050
	ld a, l
	ld [wOverworldScriptPointer], a
	ld a, h
	ld [wOverworldScriptPointer + 1], a
	ld a, NPC_BIRURITCHI
	lb de, 7, 2
	farcall SetOWObjectTilePosition
	ld a, NPC_KANZAKI
	lb de, 8, 14
	ld b, NORTH
	farcall LoadOWObjectInMap
	ld a, NPC_RUI
	lb de, 6, 14
	ld b, NORTH
	farcall LoadOWObjectInMap
	ld a, [wPlayerOWObject]
	lb de, 7, 15
	farcall SetOWObjectTilePosition
.asm_42f84
	scf
	ret

Func_42f86:
	ld a, [wNextGameEvent]
	cp GAME_EVENT_CREDITS
	jr z, .asm_42f8f
	scf
	ret
.asm_42f8f
	ld a, $00
	ld b, $04
	farcall StartPalFadeToBlackOrWhite
	call WaitPalFading
	farcall Func_110a8
	scf
	ccf
	ret

Func_42fa1:
	ld a, [wNextGameEvent]
	cp GAME_EVENT_CREDITS
	jr z, .asm_42faa
	scf
	ret
.asm_42faa
	scf
	ccf
	ret

Func_42fad:
	ld hl, GrCastleBiruritchi_NPCInteractions
	call Func_328c
	jr nc, .asm_42fbb
	ld hl, GrCastleBiruritchi_OWInteractions
	call Func_32bf
.asm_42fbb
	scf
	ret

Func_42fbd:
	ld hl, GrCastleBiruritchi_AfterDuelScripts
	ld a, [wScriptNPC]
	call ExecuteNPCScript
	scf
	ret

GrCastleBiruritchi_AfterDuelScripts:
	npc_script NPC_BIRURITCHI, Func_431a6
	db $ff

Func_42fcd:
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall GetEventValue
	jr nz, .asm_42fd7
	scf
	ret
.asm_42fd7
	farcall OverworldResumeWithCurSong
	call Func_42fe1
	scf
	ccf
	ret

Func_42fe1:
	ld a, NPC_BIRURITCHI
	ld [wScriptNPC], a
	ldtx hl, DialogBiruritchiText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0nz Script_4324b
	script_jump Script_432a0

Script_43000:
	send_mail $0c
	play_song_next MUSIC_HALL_OF_HONOR
	end_dialog
	print_npc_text Text0927
	set_event EVENT_GOT_TOGEPI_COIN
	give_coin COIN_TOGEPI
	print_npc_text Text0928
	receive_card GRS_MEWTWO
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	set_event EVENT_WON_FINAL_CUP
	reset_event EVENT_TALKED_TO_MITCH
	reset_event EVENT_TALKED_TO_AMY
	reset_event EVENT_TALKED_TO_KEN
	reset_event EVENT_TALKED_TO_MURRAY
	reset_event EVENT_TALKED_TO_MORINO
	reset_event EVENT_TALKED_TO_HIDERO
	reset_event EVENT_TALKED_TO_KANZAKI
	reset_event EVENT_TALKED_TO_ISHIHARA
	reset_event EVENT_TALKED_TO_COURTNEY_POKEMON_DOME
	reset_event EVENT_TALKED_TO_STEVE_POKEMON_DOME
	reset_event EVENT_TALKED_TO_JACK_POKEMON_DOME
	reset_event EVENT_ENTERED_GRAND_MASTER_CUP
	set_var VAR_0B, $01
	var_add VAR_27, $05
	set_var VAR_TCG_CHALLENGE_CUP_STATE, CHALLENGE_CUP_3_UNLOCKED
	set_var VAR_GR_CHALLENGE_CUP_STATE, CHALLENGE_CUP_3_UNLOCKED
	script_call Script_43333
	print_npc_text Text0929
	script_jump Script_43044

Script_43044:
	end_script
	ld hl, wOverworldTransition
	set 0, [hl]
	ld a, GAME_EVENT_CREDITS
	ld [wNextGameEvent], a
	ret

Func_43050:
	xor a
	start_script
	set_event EVENT_GR_CASTLE_STAIRS_RUI_ROADBLOCK
	move_npc NPC_KANZAKI, .NPCMovement_430ff
	move_npc NPC_RUI, .NPCMovement_4310a
	move_player .NPCMovement_430f4, TRUE
	wait_for_player_animation
	move_npc NPC_RUI, .NPCMovement_43113
	wait_for_player_animation
	do_frames 30
	start_dialog
	set_active_npc NPC_KANZAKI, DialogKanzakiText
	print_npc_text Text092a
	end_dialog
	animate_active_npc_movement $82, $01
	start_dialog
	set_active_npc NPC_RUI, DialogRuiText
	print_npc_text Text092b
	end_dialog
	animate_active_npc_movement $82, $01
	start_dialog
	set_active_npc NPC_BIRURITCHI, DialogBiruritchiText
	print_npc_text Text092c
	end_dialog
	set_active_npc NPC_KANZAKI, DialogKanzakiText
	animate_active_npc_movement $00, $01
	set_active_npc_direction WEST
	start_dialog
	print_npc_text Text092d
	end_dialog
	move_active_npc .NPCMovement_4311a
	wait_for_player_animation
	set_active_npc NPC_RUI, DialogRuiText
	animate_active_npc_movement $00, $01
	set_active_npc_direction EAST
	start_dialog
	print_npc_text Text092e
	end_dialog
	move_active_npc .NPCMovement_4311f
	wait_for_player_animation
	unload_npc NPC_KANZAKI
	unload_npc NPC_RUI
	set_active_npc NPC_BIRURITCHI, DialogBiruritchiText
	animate_active_npc_movement $02, $01
	start_dialog
	print_npc_text Text092f
	end_dialog
	animate_active_npc_movement $01, $01
	start_dialog
	print_npc_text Text0930
	end_dialog
	animate_active_npc_movement $03, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text Text0931
	end_dialog
	animate_active_npc_movement $03, $01
	start_dialog
	print_npc_text Text0932
	end_dialog
	animate_active_npc_movement $01, $01
	set_active_npc_direction SOUTH
	start_dialog
	print_npc_text Text0933
	end_dialog
	move_active_npc .NPCMovement_4312d
	move_player .NPCMovement_43124, TRUE
	wait_for_player_animation
	end_script
	ld a, OWMODE_IDLE
	ld [wOverworldMode], a
	jp Func_43136
.NPCMovement_430f4:
	db NORTH, MOVE_6
	db WEST, MOVE_2
	db NORTH, MOVE_5
	db EAST, MOVE_2
	db NORTH, MOVE_0
	db $ff
.NPCMovement_430ff:
	db NORTH, MOVE_5
	db EAST, MOVE_1
	db NORTH, MOVE_5
	db WEST, MOVE_1
	db NORTH, MOVE_0
	db $ff
.NPCMovement_4310a:
	db NORTH, MOVE_5
	db WEST, MOVE_1
	db NORTH, MOVE_6
	db SOUTH, MOVE_0
	db $ff
.NPCMovement_43113:
	db SOUTH, MOVE_1
	db EAST, MOVE_1
	db NORTH, MOVE_0
	db $ff
.NPCMovement_4311a:
	db EAST, MOVE_1
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_4311f:
	db WEST, MOVE_1
	db SOUTH, MOVE_5
	db $ff
.NPCMovement_43124:
	db EAST, MOVE_2
	db SOUTH, MOVE_4
	db WEST, MOVE_2
	db NORTH, MOVE_0
	db $ff
.NPCMovement_4312d:
	db EAST, MOVE_1
	db SOUTH, MOVE_2
	db WEST, MOVE_1
	db SOUTH, MOVE_0
	db $ff

Func_43136:
	ld a, NPC_BIRURITCHI
	ld [wScriptNPC], a
	ldtx hl, DialogBiruritchiText
	ld a, l
	ld [wScriptNPCName], a
	ld a, h
	ld [wScriptNPCName + 1], a
	xor a
	start_script
	start_dialog
	set_var VAR_08, $00
	set_var VAR_09, $00
	set_var VAR_0A, $00
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0z .ows_43181
	check_event EVENT_TALKED_TO_BIRURITCHI
	script_jump_if_b0z .ows_43166
	set_event EVENT_TALKED_TO_BIRURITCHI
	print_npc_text Text0934
	script_jump .ows_43169
.ows_43166
	print_npc_text Text0935
.ows_43169
	ask_question Text0936, TRUE
	script_jump_if_b0z .ows_4317b
	script_call Script_4336b
	print_npc_text Text0937
	end_dialog
	script_jump Script_431ca
.ows_4317b
	print_npc_text Text0938
	end_dialog
	end_script
	ret
.ows_43181
	print_npc_text Text0939
	ask_question Text0936, TRUE
	script_jump_if_b0z .ows_43196
	script_call Script_4336b
	print_npc_text Text093a
	end_dialog
	script_jump Script_431ca
.ows_43196
	print_npc_text Text093b
	get_var VAR_0B
	compare_loaded_var $01
	script_jump_if_b0z .ows_431a3
	print_npc_text Text093c
.ows_431a3
	end_dialog
	end_script
	ret

Func_431a6:
	xor a
	start_script
	start_dialog
	check_event EVENT_SET_UNTIL_MAP_RELOAD_2
	script_jump_if_b0nz .ows_431b5
	inc_var VAR_0A
	script_jump .ows_431b7
.ows_431b5
	get_var VAR_09
.ows_431b7
	check_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE
	script_jump_if_b0nz Script_4324b
	script_jump Script_432a0

Func_431bf:
	xor a
	start_script
	start_dialog
	print_text BiruritchiStatueCaptionText
	end_dialog
	end_script
	ret

Script_431ca:
	inc_var VAR_09
	end_script
.asm_431cd
	ld a, $04
	call Random
	dec a
	jr z, .asm_431f7
	dec a
	jr z, .asm_43213
	dec a
	jr z, .asm_4322f
	ld a, VAR_08
	farcall GetVarValue
	bit 0, a
	jr nz, .asm_431cd
	set 0, a
	ld c, a
	ld a, VAR_08
	farcall SetVarValue
	xor a
	start_script
	start_duel STOP_LIFE_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.asm_431f7
	ld a, VAR_08
	farcall GetVarValue
	bit 1, a
	jr nz, .asm_431cd
	set 1, a
	ld c, a
	ld a, VAR_08
	farcall SetVarValue
	xor a
	start_script
	start_duel SCORCHER_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.asm_43213
	ld a, VAR_08
	farcall GetVarValue
	bit 2, a
	jr nz, .asm_431cd
	set 2, a
	ld c, a
	ld a, VAR_08
	farcall SetVarValue
	xor a
	start_script
	start_duel TSUNAMI_STARTER_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret
.asm_4322f
	ld a, VAR_08
	farcall GetVarValue
	bit 3, a
	jr nz, .asm_431cd
	set 3, a
	ld c, a
	ld a, VAR_08
	farcall SetVarValue
	xor a
	start_script
	start_duel SMASH_TO_MINCEMEAT_DECK_ID, MUSIC_MATCH_START_GR_EXECS
	end_script
	ret

Script_4324b:
	get_var VAR_09
	compare_loaded_var $03
	script_jump_if_b0nz .ows_4327b
	compare_loaded_var $02
	script_jump_if_b0nz .ows_43266
	get_var VAR_0A
	compare_loaded_var $01
	print_variable_npc_text Text093d, Text093e
	set_text_ram3 $0002
	script_jump .ows_43285
.ows_43266
	get_var VAR_0A
	compare_loaded_var $02
	script_jump_if_b0nz Script_43000
	compare_loaded_var $00
	script_jump_if_b0nz Script_432f5
	print_npc_text Text093f
	set_text_ram3 $0003
	script_jump .ows_43285
.ows_4327b
	get_var VAR_0A
	compare_loaded_var $02
	script_jump_if_b0nz Script_43000
	script_jump Script_432f5
.ows_43285
	ask_question ChangeDeckPromptText, FALSE
	script_jump_if_b0z .ows_43299
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	print_npc_text Text0941
	script_jump .ows_43285
.ows_43299
	print_npc_text Text0942
	end_dialog
	script_jump Script_431ca

Script_432a0:
	get_var VAR_09
	compare_loaded_var $03
	script_jump_if_b0nz .ows_432d0
	compare_loaded_var $02
	script_jump_if_b0nz .ows_432bb
	get_var VAR_0A
	compare_loaded_var $01
	print_variable_npc_text Text0943, Text0944
	set_text_ram3 $0002
	script_jump .ows_432da
.ows_432bb
	get_var VAR_0A
	compare_loaded_var $02
	script_jump_if_b0nz Script_432fb
	compare_loaded_var $00
	script_jump_if_b0nz Script_4332d
	print_npc_text Text0945
	set_text_ram3 $0003
	script_jump .ows_432da
.ows_432d0
	get_var VAR_0A
	compare_loaded_var $02
	script_jump_if_b0nz Script_432fb
	script_jump Script_4332d
.ows_432da
	ask_question ChangeDeckPromptText, FALSE
	script_jump_if_b0z .ows_432ee
	end_dialog
	set_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	open_menu
	reset_event EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	start_dialog
	print_npc_text Text0946
	script_jump .ows_432da
.ows_432ee
	print_npc_text Text0947
	end_dialog
	script_jump Script_431ca

Script_432f5:
	print_npc_text Text0948
	end_dialog
	end_script
	ret

Script_432fb:
	play_song_next MUSIC_HALL_OF_HONOR
	end_dialog
	get_var VAR_0B
	compare_loaded_var $01
	script_jump_if_b0z .ows_4331a
	set_var VAR_0B, $02
	set_event EVENT_SEALED_FORT_DOOR_STATE
	print_npc_text Text0949
	receive_card GRS_MEWTWO
	script_call Script_43333
	print_npc_text Text094a
	script_jump Script_43044
.ows_4331a
	set_var VAR_0B, $03
	print_npc_text Text094b
	give_booster_packs BoosterList_cdba
	script_call Script_43333
	print_npc_text Text094c
	script_jump Script_43044

Script_4332d:
	print_npc_text Text094d
	end_dialog
	end_script
	ret

Script_43333:
	quit_script
	call EnableSRAM
	ld a, $01
	ld [sClearedGame], a
	call DisableSRAM
	ld hl, wOverworldTransition
	set 6, [hl]
	ld a, MAP_MASON_LABORATORY_MAIN
	lb de, 1, 2
	ld b, NORTH
	farcall SetWarpData
	ld a, OWMAP_MASON_LABORATORY
	ld [wCurOWLocation], a
	ld a, TCG_ISLAND
	ld [wCurIsland], a
	ld a, EVENT_MASONS_LAB_CHALLENGE_MACHINE_STATE_DUMMY
	farcall MaxOutEventValue
	farcall _SaveGame
	ld a, $01
	start_script
	play_sfx SFX_SAVE_GAME
	script_ret

Script_4336b:
	get_player_y_position
	compare_loaded_var $08
	script_jump_if_b0nz .ows_43393
	end_dialog
	set_active_npc_direction SOUTH
	get_player_direction
	compare_loaded_var EAST
	script_jump_if_b0nz .ows_4338d
	compare_loaded_var SOUTH
	script_jump_if_b0nz .ows_43386
	move_player .NPCMovement_43394, TRUE
	script_jump .ows_43391
.ows_43386
	move_player .NPCMovement_4339d, TRUE
	script_jump .ows_43391
.ows_4338d
	move_player .NPCMovement_433a6, TRUE
.ows_43391
	wait_for_player_animation
	start_dialog
.ows_43393
	script_ret
.NPCMovement_43394:
	db EAST, MOVE_1
	db SOUTH, MOVE_3
	db WEST, MOVE_2
	db NORTH, MOVE_0
	db $ff
.NPCMovement_4339d:
	db EAST, MOVE_2
	db SOUTH, MOVE_4
	db WEST, MOVE_2
	db NORTH, MOVE_0
	db $ff
.NPCMovement_433a6:
	db WEST, MOVE_1
	db SOUTH, MOVE_3
	db EAST, MOVE_2
	db NORTH, MOVE_0
	db $ff
