MagnetTrainScript::
	writehalfword MagnetTrainWarpDataArray
	jump MagnetTrainOrMinecartScript

MinecartScript::
	writehalfword MinecartWarpDataArray

MagnetTrainOrMinecartScript::
	writebyte 0
	loadarray -1, 0
	readarrayhalfword 0
	isinarray MapGroup, -1, 2, 4, 4
	sif =, $ff
		end
	scriptstartasm
	ld a, [hScriptVar]
	ld [wScriptBuffer], a
	ld b, a
	ld c, 0
	ld hl, wMagnetTrainOrMinecartMenuItems
	ld a, 4
	ld [hli], a
.loop
	ld a, c
	cp b
	jr z, .skip
	ld [hli], a
.skip
	inc a
	ld c, a
	cp 5
	jr c, .loop
	ld [hl], $ff
	xor a
	ld [wWhichIndexSet], a
	scriptstopasm
	opentext
	readarrayhalfword 2
	writetext -1
	readarrayhalfword 4
	loadmenudata -1
	verticalmenu
	closewindow
	sif >, 3, then
		closetext
		end
	sendif
	readarrayhalfword 6
	pushvar
	scall -1
	sif false
		end
	readarrayhalfword 8
	playmusic -1
	pullvar
	comparevartobyte wScriptBuffer ; var = destination, byte = source
	copyvartobyte wScriptBuffer
	readarray 10
	readarrayhalfword 11
	callasm -1, -1
	wait 14
	popvar
	callasm ForceMapMusicRestart
	pusharray
	readarrayhalfword 0
	loadarray -1, 4
	cmdwitharrayargs
	db warp_command, %111, 0, 2, 3
	endcmdwitharrayargs
	poparray
	readarrayhalfword 13
	jump -1

mapwarpcoords: MACRO
	map \1
	db \2, \3
ENDM

MagnetTrainWarpDataArray:
	dw .MagnetTrainMapWarpsArray
	dw .magnet_train_greetings_text
	dw .MagnetTrainMenuDataHeader
	dw .MagnetTrainBeforeWarpCallback
	dw MUSIC_MAGNET_TRAIN
	dba MagnetTrainAnimation
	dw .MagnetTrainAfterWarpCallback

.MagnetTrainMapWarpsArray:
	mapwarpcoords TORENIA_MAGNET_TRAIN, 11, 6
.MagnetTrainMapWarpsArrayEntrySizeEnd:
	mapwarpcoords BOTAN_MAGNET_TRAIN, 11, 6
	mapwarpcoords GOLDENROD_MAGNET_TRAIN, 11, 6
	mapwarpcoords SAFFRON_MAGNET_TRAIN, 11, 6

.MagnetTrainMenuDataHeader:
	db $40 ; flags
	db 01, 10 ; start coords
	db 11, 19 ; end coords
	dw .MenuData2
	db 1 ; default option

.MenuData2:
	db $80
	db 0
	dw wMagnetTrainOrMinecartMenuItems
	dw PlaceMenuStrings
	dw .Strings

.Strings:
	db "Naljo@"
	db "Rijon@"
	db "Johto@"
	db "Kanto@"
	db "Cancel@"

.MagnetTrainBeforeWarpCallback:
	sif >, 1, then
		checkitem MAGNET_PASS
		sif false, then
			writetext .need_pass_text
			closetext
			writebyte 0
			end
		sendif
	sendif
	writetext .enter_train_text
	closetext
	applymovement 2, .employee_enter_train
	applymovement 0, .player_enter_train
	writebyte 1
	end

.MagnetTrainAfterWarpCallback:
	applymovement 2, .employee_exit_train
	applymovement 0, .player_exit_train
	applymovement 2, .employee_walk_back
	mapnametotext 0
	getnthstring .Strings, 0
	jumptext .arrived_in_city_text

.employee_enter_train
	step_up
	step_up
	step_left
	step_end

.player_enter_train
	step_up
	step_up
	step_up
	step_up
	step_left
	step_left
	step_left
	step_up
	step_end

.employee_exit_train
	step_down
	step_left
	step_end

.player_exit_train
	step_left
	step_left
	step_down
	step_down
	step_down
	step_down
	step_down
	turn_head_up
	step_end

.employee_walk_back
	step_right
	step_up
	turn_head_down
	step_end

.magnet_train_greetings_text
	db TX_COMPRESSED, $62, $7d, $e8

	db $7c, $01, $ba, $4d, $67, $32, $18, $08, $2d, $4a, $b4
	db $96, $f5, $86, $72, $1a
	db $50, $73

.enter_train_text
	db TX_COMPRESSED, $76, $26, $3f, $04, $74, $f5, $07, $0c, $96, $6b
	db $7a, $d2, $71, $fd, $e4, $87, $72, $d7, $a3, $27
	db $a9, $e2

.need_pass_text
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $cd, $82, $bb, $c9, $0e
	db $de, $e1, $d4, $22, $58, $67, $21, $96, $93, $59
	db $fa, $9e, $20

.arrived_in_city_text
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0
	db $bd, $4f, $2f
	db $aa, $07, $23
	db $d4, $d0

MinecartWarpDataArray:
	dw .MinecartMapWarpsArray
	dw .MinecartSeemsStableText
	dw .MinecartMenuDataHeader
	dw .MinecartBeforeWarpCallback
	dw MUSIC_NONE
	dba .TempMinecartAnimation
	dw .MinecartAfterWarpCallback

.MinecartMapWarpsArray:
	mapwarpcoords ACQUA_START, 28, 36
.MinecartMapWarpsArrayEntrySizeEnd:
	mapwarpcoords MOUND_B3F, 8, 58
	mapwarpcoords CLATHRITE_1F, 20, 38
	mapwarpcoords FIRELIGHT_MINECART, 12, 14

.MinecartSeemsStableText:
	db TX_COMPRESSED, $29, $23, $44, $7c, $64, $38, $ce, $ae
	db $de, $41, $9b, $e8, $88, $bd, $57, $d3
	db $e2, $01, $ba, $4d, $67, $32, $18, $08, $2d, $4a, $b4
	db $96, $f5, $86, $72, $1a
	db $50, $73

.MinecartMenuDataHeader:
	db $40 ; flags
	db 02, 00 ; start coords
	db 11, 19 ; end coords
	dw .MenuData2
	db 1 ; default option

.MenuData2:
	db $80
	db 0
	dw wMagnetTrainOrMinecartMenuItems
	dw PlaceMenuStrings
	dw .Strings

.Strings:
	db "Acqua Mines@"
	db "Mound Cave@"
	db "Clathrite Tunnel@"
	db "Firelight Caverns@"
	db "Cancel@"

.MinecartBeforeWarpCallback:
	closetext
	applymovement 0, .player_moves_down
	writebyte 1
	end

.player_moves_down
	step_down
	step_end
	
.TempMinecartAnimation:
	xor a
	ld [hMapAnims], a
	call ClearPalettes
	call DelayFrame
	ld de, SFX_BALL_POOF
	call PlaySFX
	ld c, 8
	ld de, SFX_SANDSTORM
.loop
	call PlayWaitSFX
	dec c
	jr nz, .loop
	ld de, SFX_EMBER
	call PlayWaitSFX
	ld de, SFX_STOMP
	jp PlayWaitSFX

.MinecartAfterWarpCallback:
	applymovement 0, .player_moves_up
	end

.player_moves_up
	step_up
	step_end
