RuinsF3_MapScriptHeader;trigger count
	db 0
 ;callback count
	db 1
	dbw MAPCALLBACK_TILES, RuinsF3Blocks

RuinsF3Trap1:
	setevent EVENT_RUINS_F3_TRAP_1
	setevent EVENT_1
	jump RuinsF3_ApplyTraps

RuinsF3Trap2:
	setevent EVENT_RUINS_F3_TRAP_2
	setevent EVENT_2
	jump RuinsF3_ApplyTraps

RuinsF3Trap3:
	setevent EVENT_RUINS_F3_TRAP_3
	setevent EVENT_3
	jump RuinsF3_ApplyTraps

RuinsF3Trap4:
	setevent EVENT_RUINS_F3_TRAP_4
	setevent EVENT_4
	jump RuinsF3_ApplyTraps

RuinsF3Trap5:
	setevent EVENT_RUINS_F3_TRAP_5
	setevent EVENT_5
	jump RuinsF3_ApplyTraps

RuinsF3Trap6:
	setevent EVENT_RUINS_F3_TRAP_6
	setevent EVENT_6
	jump RuinsF3_ApplyTraps

RuinsF3Signpost1:
	checkevent EVENT_RUINS_F3_TRAP_5
	sif false
		end
	checkcode VAR_FACING
	sif =, UP
		end
	jump RuinsF3_JumpHole

RuinsF3Signpost2:
	checkevent EVENT_RUINS_F3_TRAP_6
	sif false
		end
	checkcode VAR_FACING
	sif =, DOWN
		end
	jump RuinsF3_JumpHole

RuinsF3Signpost3:
	checkcode VAR_FACING
	sif =, RIGHT
		end
	jump RuinsF3_JumpHole

RuinsF3_Item_1:
	db PEARL, 1

RuinsF3RandomBall3:
	disappear 4
	setevent EVENT_RUINS_OPENED_BALL_3
	checkevent EVENT_RUINS_RANDOM_BALL_IS_3
	jump RuinsF3_DoRandomBall

RuinsF3RandomBall2:
	disappear 5
	setevent EVENT_RUINS_OPENED_BALL_2
	checkevent EVENT_RUINS_RANDOM_BALL_IS_2
	jump RuinsF3_DoRandomBall

RuinsF3RandomBall1:
	disappear 6
	setevent EVENT_RUINS_OPENED_BALL_1
	checkevent EVENT_RUINS_RANDOM_BALL_IS_1
RuinsF3_DoRandomBall:
	sif true, then
		opentext
		giveitem BROWN_JEWEL, 1
		sif false
			jumptext .no_room_text
		writetext .got_brown_jewel_text
		playwaitsfx SFX_DEX_FANFARE_50_79
		closetext
		end
	sendif
	random 9
	anonjumptable
	dw .misdreavus
	dw .swablu
	dw .metang
	dw .golbat
	dw .golbat
	dw .golbat
	dw .yanma
	dw .yanma
	dw .yanma

.misdreavus
	writebyte MISDREAVUS
	jump .battle
.swablu
	writebyte SWABLU
	jump .battle
.metang
	writebyte METANG
	jump .battle
.golbat
	writebyte GOLBAT
	jump .battle
.yanma
	writebyte YANMA
.battle
	setlasttalked 255
	writecode VAR_BATTLETYPE, BATTLETYPE_TRAP
	loadwildmon 0, 40
	startbattle
	reloadmapafterbattle
	end

.no_room_text
	db TX_COMPRESSED, $07, $86, $2c, $21, $7e, $60, $15, $cb, $48, $d1
	db $c6, $9a

.got_brown_jewel_text
	db TX_COMPRESSED, $72, $85, $a9, $80, $5a, $26, $65, $a4
	db $de, $12, $58, $6a, $24, $13, $27, $51, $f7
	db $8d, $34

RuinsF3Blocks:
	checkevent EVENT_RUINS_RANDOM_BALL_PICKED
	sif false
		scall RuinsF3_SelectGoodRandomBall
	checkevent EVENT_MURUM_SWITCH
	sif true
		changeblock 18, 4, 1
RuinsF3_ApplyTraps:
	setevent EVENT_0
	checkevent EVENT_RUINS_F3_TRAP_5
	sif true, then
		changeblock 8, 2, $33
	selse
		changeblock 8, 2, $1
	sendif
	checkevent EVENT_RUINS_F3_TRAP_6
	sif true, then
		changeblock 10, 2, $35
	selse
		changeblock 10, 2, $1
	sendif
	checkevent EVENT_RUINS_F3_TRAP_1
	sif true, then
		checkevent EVENT_RUINS_F3_TRAP_2
		sif true, then
			changeblock 22, 14, $38
		selse
			changeblock 22, 14, $33
		sendif
	selse
		changeblock 22, 14, $1
		checkevent EVENT_RUINS_F3_TRAP_2
		sif true
			changeblock 22, 14, $35
	sendif
	changeblock 22, 16, $1
	checkevent EVENT_RUINS_F3_TRAP_4
	sif true, then
		checkevent EVENT_RUINS_F3_TRAP_3
		sif true, then
			changeblock 10, 18, $38
		selse
			changeblock 10, 18, $33
		sendif
	selse
		changeblock 10, 18, $1
		checkevent EVENT_RUINS_F3_TRAP_3
		sif true
			changeblock 10, 18, $35
	sendif
	checkevent EVENT_1
	iftrue .one
	checkevent EVENT_2
	iftrue .two
	checkevent EVENT_3
	iftrue .three
	checkevent EVENT_4
	iftrue .four
	checkevent EVENT_5
	iftrue .five
	checkevent EVENT_6
	iftrue .six
	return

.prepare_fall
	playsound SFX_ENTER_DOOR
	callasm AnchorBGMap
	callasm BGMapAnchorTopLeft
	showemote 0, 0, 32
	applymovement 0, .hide_falling_person
	end

.one
	clearevent EVENT_1
	scall .prepare_fall
	warp RUINS_F2, 19, 20
	jump .fall

.two
	clearevent EVENT_2
	scall .prepare_fall
	warp RUINS_F2, 19, 21
	jump .fall

.three
	clearevent EVENT_3
	scall .prepare_fall
	warp RUINS_F2, 9, 23
	jump .fall

.four
	clearevent EVENT_4
	scall .prepare_fall
	warp RUINS_F2, 9, 22
	jump .fall

.five
	clearevent EVENT_5
	scall .prepare_fall
	warp RUINS_F2, 10, 8
	jump .fall

.six
	clearevent EVENT_6
	scall .prepare_fall
	warp RUINS_F2, 12, 9
.fall
	playsound SFX_BALL_POOF
	applymovement 0, .show_falling_person
	playsound SFX_STRENGTH
	earthquake 24
	closetext
	end

.hide_falling_person
	hide_person
	step_end

.show_falling_person
	show_person
	skyfall
	step_end

RuinsF3Gap1:
	checkevent EVENT_RUINS_F3_TRAP_4
	iftrue RuinsF3_JumpHole
	end

RuinsF3Gap2:
	checkevent EVENT_RUINS_F3_TRAP_3
	iftrue RuinsF3_JumpHole
	end

RuinsF3Gap3:
	checkcode VAR_FACING
	sif =, LEFT
		end
	checkevent EVENT_RUINS_F3_TRAP_1
	sif false
		end
	checkevent EVENT_JUMPING_SHOES
	sif false
		end
	scall RuinsF3_JumpHole
	jump RuinsF3Trap2
	
RuinsF3Gap4:
	checkcode VAR_FACING
	sif =, LEFT
		end
	checkevent EVENT_RUINS_F3_TRAP_2
	sif false
		end
	checkevent EVENT_JUMPING_SHOES
	sif false
		end
	scall RuinsF3_JumpHole
	jump RuinsF3Trap1

RuinsF3_SelectGoodRandomBall:
	setevent EVENT_RUINS_RANDOM_BALL_PICKED
	random 3
	anonjumptable
	dw .one
	dw .two
	dw .three

.one
	setevent EVENT_RUINS_RANDOM_BALL_IS_1
	return

.two
	setevent EVENT_RUINS_RANDOM_BALL_IS_2
	return

.three
	setevent EVENT_RUINS_RANDOM_BALL_IS_3
	return

RuinsF3_JumpHole:
	checkevent EVENT_JUMPING_SHOES
	sif false
		end
	playsound SFX_JUMP_OVER_LEDGE
	checkcode VAR_FACING
	if_greater_than 3, .down
	anonjumptable
	dw .down
	dw .up
	dw .left
	dw .right

.down
	applymovement 0, .movement_jump_step_down
	end
.up
	applymovement 0, .movement_jump_step_up
	end
.left
	applymovement 0, .movement_jump_step_left
	end
.right
	applymovement 0, .movement_jump_step_right
	end

.movement_jump_step_down
	jump_step_down
	step_end
.movement_jump_step_up
	jump_step_up
	step_end
.movement_jump_step_left
	jump_step_left
	step_end
.movement_jump_step_right
	jump_step_right
	step_end

RuinsF3_MapEventHeader:: db 0, 0

.Warps: db 2
	warp_def 19, 3, 2, RUINS_F2
	warp_def 7, 19, 1, RUINS_F4

.CoordEvents: db 6
	xy_trigger 0, 14, 22, 0, RuinsF3Trap1, 0, 0
	xy_trigger 0, 15, 22, 0, RuinsF3Trap2, 0, 0
	xy_trigger 0, 19, 10, 0, RuinsF3Trap3, 0, 0
	xy_trigger 0, 18, 10, 0, RuinsF3Trap4, 0, 0
	xy_trigger 0, 2, 8, 0, RuinsF3Trap5, 0, 0
	xy_trigger 0, 3, 10, 0, RuinsF3Trap6, 0, 0

.BGEvents: db 6
	signpost 2, 8, SIGNPOST_READ, RuinsF3Signpost1
	signpost 3, 10, SIGNPOST_READ, RuinsF3Signpost2
	signpost 18, 10, SIGNPOST_READ, RuinsF3Gap1
	signpost 19, 10, SIGNPOST_READ, RuinsF3Gap2
	signpost 14, 22, SIGNPOST_READ, RuinsF3Gap3
	signpost 15, 22, SIGNPOST_READ, RuinsF3Gap4

.ObjectEvents: db 5
	person_event SPRITE_POKE_BALL, 19, 23, SPRITEMOVEDATA_00, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_TMHMBALL, TM_SANDSTORM, 0, EVENT_RUINS_F3_NPC_1
	person_event SPRITE_POKE_BALL, 18, 15, SPRITEMOVEDATA_00, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_ITEMBALL, 0, RuinsF3_Item_1, EVENT_RUINS_F3_ITEM_1
	person_event SPRITE_POKE_BALL, 2, 4, SPRITEMOVEDATA_00, 0, 0, -1, -1, PAL_OW_PURPLE, PERSONTYPE_SCRIPT, 0, RuinsF3RandomBall3, EVENT_RUINS_OPENED_BALL_1
	person_event SPRITE_POKE_BALL, 2, 3, SPRITEMOVEDATA_00, 0, 0, -1, -1, PAL_OW_PURPLE, PERSONTYPE_SCRIPT, 0, RuinsF3RandomBall2, EVENT_RUINS_OPENED_BALL_2
	person_event SPRITE_POKE_BALL, 2, 2, SPRITEMOVEDATA_00, 0, 0, -1, -1, PAL_OW_PURPLE, PERSONTYPE_SCRIPT, 0, RuinsF3RandomBall1, EVENT_RUINS_OPENED_BALL_3

