ruinsPit: macro
	dw \1 ; Flag
	db \2 ; Y
	db \3 ; X
	db \4 ; Permissions
	endm

RuinsF1_MapScriptHeader;trigger count
	db 1
	maptrigger .Trigger0

 ;callback count
	db 1
	dbw MAPCALLBACK_TILES, RuinsF1Blocks

.Trigger0
	callasm F1CheckJump
	callasm F1CheckPit
	end

F1GetFacingDirection:
	ld a, [PlayerDirection]
	and $c
	rrca
	rrca
	ret

F1CheckJump:
	ld a, [hJoyPressed]
	cp A_BUTTON
	ret nz
	call F1GetFacingDirection
	push af
	ld hl, YCoord
	ld a, [hli]
	ld e, a
	ld a, [hl]
	ld d, a
	pop af
	push de
	jumptable

.Jumptable
	dw .down
	dw .up
	dw .left
	dw .right

.down 
	pop de
	inc e
	jr .checkTile
.up 
	pop de
	dec e
	jr .checkTile
.left 
	pop de
	dec d
	jr .checkTile
.right 
	pop de
	inc d

.checkTile
	ld hl, RuinsF1PitTable
	call F1ReadPitTable
	ret nc

	ld a, [hli]
	ld e, a
	ld a, [hli]
	ld d, a
	ld b, CHECK_FLAG
	push hl
	ld hl, EventFlags
	predef EventFlagAction
	pop hl
	ret z

	inc hl
	inc hl ;Check facing permissions

	call F1GetFacingDirection
	ld b, a
	ld c, DOWN
	ld d, [hl]

.facingCheck
	ld a, b
	bit 3, d
	jr z, .facingOK
	cp c
	ret z
.facingOK
	sla d
	inc c
	ld a, c
	cp 4
	jr nz, .facingCheck

	ld a, BANK(Ruins_TryJump)
	ld de, Ruins_TryJump
	jp F1SetPriorityJump

F1SetPriorityJump:
	ld [wPriorityScriptBank], a
	ld hl, wPriorityScriptAddr
	ld a, e
	ld [hli], a
	ld a, d
	ld [hl], a
	ld hl, ScriptFlags
	set 3, [hl]
	ret

F1ReadPitTable: ;e: Y, d: X, set carry if it was found. Returns address of entry in hl
	inc hl
	inc hl
.loop
	ld a, [hli]
	cp -1
	jr z, .none

	ld b, a
	ld a, e
	cp b
	jr nz, .badCoord

	ld a, [hl]
	ld b, a
	ld a, d
	cp b
	jr nz, .badCoord 
	scf
rept 3
	dec hl
endr
	ret
.none
	and a
	ret

.badCoord
rept 4
	inc hl
endr
	jr .loop

F1CheckPit:
	ld hl, YCoord
	ld a, [hli]
	ld e, a
	ld d, [hl]
	ld hl, RuinsF1PitTable
	call F1ReadPitTable
	ret nc

	ld a, [hli]
	ld e, a
	ld a, [hl]
	ld d, a
	ld b, SET_FLAG
	ld hl, EventFlags
	predef EventFlagAction

	ld a, BANK(RuinsF1_TriggeredTrap)
	ld de, RuinsF1_TriggeredTrap
	jp F1SetPriorityJump

RuinsF1Blocks:
	clearevent EVENT_2
	jump RuinsTrapSet0

RuinsF1Trap21:
	checkevent EVENT_TELUM_SWITCH
	iffalse RuinsF1_24e5c9
	end

RuinsF1Trap22:
	disappear 5
	setevent EVENT_0
	end

RuinsF1MurumSwitch:
	opentext
	checkevent EVENT_MURUM_SWITCH
	sif true
		jumptext RuinsF1MurumSwitch_Text_SwitchIsOff
	writetext RuinsF1MurumSwitch_Text_SwitchIsOn
	yesorno
	sif false, then
		closetext
		end
	sendif
	playsound SFX_ENTER_DOOR
	setevent EVENT_MURUM_SWITCH
	jumptext RuinsF1MurumSwitch_Text_TurnedSwitchOff

RuinsF1_Item_1:
	db PP_UP, 1

RuinsF1_Item_2:
	db RED_JEWEL, 1

RuinsF1_PaletteYellow:
	trainer EVENT_RUINS_F1_TRAINER_1, PATROLLER, 5, RuinsF1_PaletteYellow_Text_BeforeBattle, RuinsF1_PaletteYellow_Text_BattleWon, $0000, .Script

.Script
	end_if_just_battled
	jumptext RuinsF1_PaletteYellow_Script_Text_AfterBattle

RuinsF1Arrow:
	jumptext RuinsF1Arrow_Text

RuinsF1_TriggeredTrap:
	setevent EVENT_1
	; fallthrough

RuinsTrapSet0:
	checkevent EVENT_RUINS_F1_TRAP_1
	iffalse RuinsF1_24e371
	checkevent EVENT_RUINS_F1_TRAP_2
	iffalse RuinsF1_24e382
	changeblock 4, 8, $43

RuinsTrapSet1:
	checkevent EVENT_RUINS_F1_TRAP_3
	iffalse RuinsF1_24e389
	checkevent EVENT_RUINS_F1_TRAP_4
	iffalse RuinsF1_24e39a
	changeblock 14, 6, $43

RuinsTrapSet2:
	checkevent EVENT_RUINS_F1_TRAP_5
	iffalse RuinsF1_24e3a1
	checkevent EVENT_RUINS_F1_TRAP_6
	iffalse RuinsF1_24e3b2
	changeblock 28, 10, $42

RuinsTrapSet3:
	checkevent EVENT_RUINS_F1_TRAP_7
	iffalse RuinsF1_24e355
	changeblock 36, 8, $34

RuinsTrapSet4:
	checkevent EVENT_RUINS_F1_TRAP_8
	iffalse RuinsF1_24e35c
	changeblock 36, 12, $33

RuinsTrapSet5:
	checkevent EVENT_RUINS_F1_TRAP_9
	iffalse RuinsF1_24e363
	changeblock 36, 16, $34

RuinsTrapSet6:
	checkevent EVENT_RUINS_F1_TRAP_10
	iffalse RuinsF1_24e36a
	changeblock 36, 20, $33

RuinsTrapSet7:
	checkevent EVENT_RUINS_F1_TRAP_11
	iffalse RuinsF1_24e3b9
	checkevent EVENT_RUINS_F1_TRAP_12
	iffalse RuinsF1_24e3ca
	changeblock 36, 28, $3b

RuinsTrapSet8:
	checkevent EVENT_RUINS_F1_TRAP_13
	iffalse RuinsF1_24e3d1
	checkevent EVENT_RUINS_F1_TRAP_14
	iffalse RuinsF1_24e3e2
	changeblock 8, 26, $38

RuinsTrapSet9:
	checkevent EVENT_RUINS_F1_TRAP_15
	iffalse RuinsF1_24e3e9
	changeblock 8, 24, $35

RuinsTrapSet10:
	checkevent EVENT_RUINS_F1_TRAP_16
	iffalse RuinsF1_24e3f0

RuinsTrapSet11:
	checkevent EVENT_RUINS_F1_TRAP_17
	iffalse RuinsF1_24e401
	changeblock 7, 24, $3c

RuinsTrapSet12:
	checkevent EVENT_RUINS_F1_TRAP_18
	iffalse RuinsF1_24e408
	checkevent EVENT_RUINS_F1_TRAP_19
	iffalse RuinsF1_24e423
	checkevent EVENT_RUINS_F1_TRAP_20
	iffalse RuinsF1_24e434
	changeblock 6, 26, $3f
	checkevent EVENT_2
	iffalse RuinsF1_24e351
	jump RuinsF1_24e210

RuinsF1_24e5c9:
	checkevent EVENT_0
	iftrue RuinsF1_24e5d2
	end

RuinsF1_24e371:
	changeblock 4, 8, $2b
	checkevent EVENT_RUINS_F1_TRAP_2
	iffalse RuinsTrapSet1
	changeblock 4, 8, $45
	jump RuinsTrapSet1

RuinsF1_24e382:
	changeblock 4, 8, $44
	jump RuinsTrapSet1

RuinsF1_24e389:
	changeblock 14, 6, $2b
	checkevent EVENT_RUINS_F1_TRAP_4
	iffalse RuinsTrapSet2
	changeblock 14, 6, $45
	jump RuinsTrapSet2

RuinsF1_24e39a:
	changeblock 14, 6, $44
	jump RuinsTrapSet2

RuinsF1_24e3a1:
	changeblock 28, 10, $2f
	checkevent EVENT_RUINS_F1_TRAP_6
	iffalse RuinsTrapSet3
	changeblock 28, 10, $46
	jump RuinsTrapSet3

RuinsF1_24e3b2:
	changeblock 28, 10, $47
	jump RuinsTrapSet3

RuinsF1_24e355:
	changeblock 36, 8, $1
	jump RuinsTrapSet4

RuinsF1_24e35c:
	changeblock 36, 12, $1
	jump RuinsTrapSet5

RuinsF1_24e363:
	changeblock 36, 16, $1
	jump RuinsTrapSet6

RuinsF1_24e36a:
	changeblock 36, 20, $1
	jump RuinsTrapSet7

RuinsF1_24e3b9:
	changeblock 36, 28, $1
	checkevent EVENT_RUINS_F1_TRAP_12
	iffalse RuinsTrapSet8
	changeblock 36, 28, $36
	jump RuinsTrapSet8

RuinsF1_24e3ca:
	changeblock 36, 28, $34
	jump RuinsTrapSet8

RuinsF1_24e3d1:
	changeblock 8, 26, $1
	checkevent EVENT_RUINS_F1_TRAP_14
	iffalse RuinsTrapSet9
	changeblock 8, 26, $33
	jump RuinsTrapSet9

RuinsF1_24e3e2:
	changeblock 8, 26, $35
	jump RuinsTrapSet9

RuinsF1_24e3e9:
	changeblock 8, 24, $1
	jump RuinsTrapSet10

RuinsF1_24e3f0:
	changeblock 6, 24, $1
	checkevent EVENT_RUINS_F1_TRAP_17
	iffalse RuinsTrapSet11
	changeblock 6, 24, $35
	jump RuinsTrapSet11

RuinsF1_24e401:
	changeblock 6, 24, $36
	jump RuinsTrapSet12

RuinsF1_24e408:
	changeblock 6, 26, $1
	checkevent EVENT_RUINS_F1_TRAP_19
	iffalse RuinsF1_24e43b
	changeblock 6, 26, $35
	checkevent EVENT_RUINS_F1_TRAP_20
	iffalse RuinsF1_24e348
	changeblock 6, 26, $3c
	jump RuinsF1_24e348

RuinsF1_24e423:
	changeblock 6, 26, $33
	checkevent EVENT_RUINS_F1_TRAP_20
	iffalse RuinsF1_24e348
	changeblock 6, 26, $39
	jump RuinsF1_24e348

RuinsF1_24e434:
	changeblock 6, 26, $38
	jump RuinsF1_24e348

RuinsF1_24e351:
	setevent EVENT_2
	return

RuinsF1_24e210:
	playsound SFX_ENTER_DOOR
	jump RuinsF1_1a9b10

RuinsF1_24e5d2:
	playsound SFX_VICEGRIP
	appear $5
	applymovement 5, .movement
	end
.movement
	fast_slide_step_left
	fast_slide_step_left
	step_end

Ruins_TryJump:
	checkevent EVENT_JUMPING_SHOES
	sif false
		end
	playsound SFX_JUMP_OVER_LEDGE
	checkcode VAR_FACING
	sif >, 3
		writebyte 0
	anonjumptable
	dw .facing_down
	dw .facing_up
	dw .facing_left
	dw .facing_right
.facing_down
	applymovement 0, .down_movement
	end
.facing_up
	applymovement 0, .up_movement
	end
.facing_left
	applymovement 0, .left_movement
	end
.facing_right
	applymovement 0, .right_movement
	end
.down_movement
	jump_step_down
	step_end
.up_movement
	jump_step_up
	step_end
.left_movement
	jump_step_left
	step_end
.right_movement
	jump_step_right
	step_end

RuinsF1_24e43b:
	checkevent EVENT_RUINS_F1_TRAP_20
	iffalse RuinsF1_24e348
	changeblock 6, 26, $36
	; fallthrough

RuinsF1_24e348:
	checkevent EVENT_2
	iffalse RuinsF1_24e351
	jump RuinsF1_24e210

RuinsF1_1a9b10:
	callasm AnchorBGMap
	callasm BGMapAnchorTopLeft

	showemote 0, 0, 32
	applymovement 0, .movement
	random 16
	anonjumptable
	dw RuinsF1_Warp0
	dw RuinsF1_Warp1
	dw RuinsF1_Warp2
	dw RuinsF1_Warp3
	dw RuinsF1_Warp4
	dw RuinsF1_Warp5
	dw RuinsF1_Warp6
	dw RuinsF1_Warp7
	dw RuinsF1_Warp8
	dw RuinsF1_Warp7
	dw RuinsF1_Warp8
	dw RuinsF1_Warp9
	dw RuinsF1_Warp10
	dw RuinsF1_Warp1
	dw RuinsF1_Warp2
	dw RuinsF1_Warp1
.movement
	hide_person
	step_end

RuinsF1_Warp0:
	warp RUINS_B1F, 8, 8
	jump RuinsF1_Warp

RuinsF1_Warp1:
	warp RUINS_B1F, 9, 8
	jump RuinsF1_Warp

RuinsF1_Warp2:
	warp RUINS_B1F, 8, 9
	jump RuinsF1_Warp

RuinsF1_Warp3:
	warp RUINS_B1F, 4, 10
	jump RuinsF1_Warp

RuinsF1_Warp4:
	warp RUINS_B1F, 5, 8
	jump RuinsF1_Warp

RuinsF1_Warp5:
	warp RUINS_B1F, 10, 5
	jump RuinsF1_Warp

RuinsF1_Warp6:
	warp RUINS_B1F, 8, 4
	jump RuinsF1_Warp

RuinsF1_Warp7:
	warp RUINS_B1F, 15, 10
	jump RuinsF1_Warp

RuinsF1_Warp8:
	warp RUINS_B1F, 14, 9
	jump RuinsF1_Warp

RuinsF1_Warp9:
	warp RUINS_B1F, 11, 15
	jump RuinsF1_Warp

RuinsF1_Warp10:
	warp RUINS_B1F, 8, 14
	; fallthrough

RuinsF1_Warp:
	playsound SFX_BALL_POOF
	applymovement 0, .movement
	playsound SFX_STRENGTH
	earthquake 24
	closetext
	end
.movement
	show_person
	skyfall
	step_end

RuinsF1PitTable:
	ruinsPit EVENT_RUINS_F1_TRAP_1, 8, 4, 0
	ruinsPit EVENT_RUINS_F1_TRAP_2, 8, 5, 0
	ruinsPit EVENT_RUINS_F1_TRAP_3, 6, 14, 0
	ruinsPit EVENT_RUINS_F1_TRAP_4, 6, 15, 0
	ruinsPit EVENT_RUINS_F1_TRAP_5, 11, 28, 0
	ruinsPit EVENT_RUINS_F1_TRAP_6, 10, 28, 0
	ruinsPit EVENT_RUINS_F1_TRAP_7, 8, 37, FACE_RIGHT
	ruinsPit EVENT_RUINS_F1_TRAP_8, 12, 36, FACE_LEFT
	ruinsPit EVENT_RUINS_F1_TRAP_9, 16, 37, FACE_RIGHT
	ruinsPit EVENT_RUINS_F1_TRAP_10, 20, 36, FACE_LEFT
	ruinsPit EVENT_RUINS_F1_TRAP_11, 28, 37, FACE_RIGHT
	ruinsPit EVENT_RUINS_F1_TRAP_12, 29, 37, FACE_DOWN | FACE_RIGHT
	ruinsPit EVENT_RUINS_F1_TRAP_13, 27, 8, 0
	ruinsPit EVENT_RUINS_F1_TRAP_14, 26, 8, 0
	ruinsPit EVENT_RUINS_F1_TRAP_15, 25, 8, 0
	ruinsPit EVENT_RUINS_F1_TRAP_16, 25, 7, 0
	ruinsPit EVENT_RUINS_F1_TRAP_17, 25, 6, 0
	ruinsPit EVENT_RUINS_F1_TRAP_18, 26, 6, 0
	ruinsPit EVENT_RUINS_F1_TRAP_19, 27, 6, 0
	ruinsPit EVENT_RUINS_F1_TRAP_20, 27, 7, 0
	db -1, -1, -1, -1

RuinsF1MurumSwitch_Text_SwitchIsOn:
	db TX_COMPRESSED, $29, $27, $11, $50, $ed, $94, $c1, $a2, $04
	db $8f, $69, $33, $3b, $aa, $cf, $a7
	db $95, $42, $ad, $2d, $a7

	db $fc, $00, $a6, $96, $48, $3b, $81, $c3, $0a
	db $5a, $68

RuinsF1_PaletteYellow_Text_BeforeBattle:
	db TX_COMPRESSED, $6e, $65, $c7, $32, $1a, $89, $2c, $32, $ea, $0f
	db $de, $56, $d5, $ca, $8c, $b8, $e1, $03
	db $6a, $b9, $08, $c9, $cc, $b0, $c4, $58, $44, $71

	db $00, $dc, $d8, $0e, $64, $c1, $8f, $a1, $10, $79
	db $9a, $f2, $2d, $61, $27, $30, $46, $ae, $c8, $6b, $42, $46

	db $20, $05, $a7, $6d, $c3, $d8, $fe, $a9, $1c, $7c, $e6, $93
	db $2f, $30, $96, $66, $48, $b1, $b2, $9e
	db $34, $d0

RuinsF1_PaletteYellow_Text_BattleWon:
	db TX_COMPRESSED, $29, $3b, $71, $09, $0b, $5d, $c9, $29, $91, $3a, $0c
	db $6f, $27, $44, $08, $32, $1e, $66, $f3, $a3, $25, $3c
	db $14, $d0

RuinsF1_PaletteYellow_Script_Text_AfterBattle:
	db TX_COMPRESSED, $e1, $e9, $ea, $0e, $22, $c2, $23

	db $c0, $1d, $61, $2a, $39, $80, $c9, $99, $69
	db $37, $91, $61, $21, $cb, $69, $77, $d3

	db $f8, $0a, $8e, $33, $a4, $88, $b3, $de, $c1, $da
	db $1e, $0a, $d5, $cd, $26, $62, $75, $64, $34
	db $21, $55, $a4, $e0, $56, $af, $14, $32, $1a, $f9, $05

	db $e0, $0d, $c7, $69, $96, $9d, $bf, $ca, $8e, $33, $a4
	db $79, $fd, $52, $39, $a4, $05, $01, $ca, $f8
	db $48, $a8, $c1, $8c, $91, $16, $b0, $93, $93, $5f
	db $1a, $68

RuinsF1Arrow_Text:
	db TX_COMPRESSED, $29, $23, $44, $d5, $ac, $35, $44, $3e, $7d, $ea
	db $1e, $10, $11, $a0, $24, $0b, $48

	db $f0, $01, $23, $bb, $35, $c2, $06, $e5, $85, $a6
	db $52, $f0, $ef
	db $d4, $d0

RuinsF1MurumSwitch_Text_SwitchIsOff:
	db TX_COMPRESSED, $29, $27, $11, $50, $ed, $94, $c1
	db $a1, $ed, $ea, $cf, $72, $0a, $07, $0c
	db $3d, $4d

RuinsF1MurumSwitch_Text_TurnedSwitchOff:
	db TX_COMPRESSED, $29, $a5, $90, $f3, $2d, $27, $11, $50, $ed, $94
	db $bc, $1c, $30
	db $f5, $34

RuinsF1_MapEventHeader ;filler
	db 0, 0

;warps
	db 4
	warp_def $1f, $12, 3, RUINS_OUTSIDE
	warp_def $1f, $13, 6, RUINS_OUTSIDE
	warp_def $1d, $3, 17, RUINS_B1F
	warp_def $9, $b, 1, RUINS_F2

	;xy triggers
	db 2
	xy_trigger 0, $6, $24, $0, RuinsF1Trap21, $0, $0
	xy_trigger 0, $b, $24, $0, RuinsF1Trap22, $0, $0

	;signposts
	db 1
	signpost 1, 31, SIGNPOST_READ, RuinsF1MurumSwitch

	;people-events
	db 5
	person_event SPRITE_P0, -3, -3, SPRITEMOVEDATA_ITEM_TREE, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_ITEMBALL, 1, RuinsF1_Item_1, EVENT_RUINS_F1_ITEM_1
	person_event SPRITE_POKE_BALL, 2, 7, SPRITEMOVEDATA_ITEM_TREE, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_ITEMBALL, 0, RuinsF1_Item_2, EVENT_RUINS_F1_ITEM_2
	person_event SPRITE_PALETTE_PATROLLER, 22, 7, SPRITEMOVEDATA_STANDING_RIGHT, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_TRAINER, 2, RuinsF1_PaletteYellow, EVENT_RUINS_F1_TRAINER_1
	person_event SPRITE_FAMICOM, 5, 38, SPRITEMOVEDATA_ITEM_TREE, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_SCRIPT, 0, RuinsF1Arrow, EVENT_0
	person_event SPRITE_POKE_BALL, 14, 25, SPRITEMOVEDATA_ITEM_TREE, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_TMHMBALL, TM_DRAGONBREATH, 0, EVENT_GET_TM24

