tag @a remove mapsound.scary_zone
tag @a remove mapsound.scary_zone2
tag @a remove mapsound.corpse_zone

# store player X coordinate
execute as @a store result score @s mapsound_x run data get entity @s Pos[0] 1

execute as @a[x=80,y=139,z=25,dx=20,dy=4,dz=5] run tag @s add mapsound.scary_zone
execute as @a[x=97,y=139,z=18,dx=4,dy=4,dz=18] run tag @s add mapsound.scary_zone
execute as @a[x=70,y=145,z=9,dx=13,dy=3,dz=3] run tag @s add mapsound.scary_zone

# Deck 0 scary ambient area: 44 139 8 to 75 148 32
execute as @a[x=44,y=139,z=0,dx=31,dy=9,dz=32] run tag @s add mapsound.scary_zone

# Deck -1 scary ambient area: -26 131 4 to 19 143 37
execute as @a[x=-26,y=131,z=4,dx=45,dy=12,dz=33] run tag @s add mapsound.scary_zone

# scary ambient 2 plays whenever player X <= -25, even outside scary zone
execute as @a[scores={mapsound_x=..-26}] run tag @s add mapsound.scary_zone2

# remove safe area from creepy sound zone: 43 139 -1 to 52 143 9
execute as @a[x=43,y=139,z=-1,dx=9,dy=4,dz=10] run tag @s remove mapsound.scary_zone

# corpse trigger area: 84 151 23 to 92 155 40
execute as @a[x=84,y=151,z=23,dx=8,dy=4,dz=17] run tag @s add mapsound.corpse_zone

# entering corpse area: play once, loud
execute as @a[tag=mapsound.corpse_zone,tag=!mapsound.was_corpse_zone] at @s run playsound mymap:corpse master @s ~ ~ ~ 4 1

# scary ambient 2 overrides both ship and scary ambient 1
execute as @a[tag=mapsound.scary_zone2,scores={ship_cd=1..}] run stopsound @s ambient mymap:ship
execute as @a[tag=mapsound.scary_zone2,scores={scary_cd=1..}] run stopsound @s ambient mymap:scary_ambient
scoreboard players set @a[tag=mapsound.scary_zone2] ship_cd 0
scoreboard players set @a[tag=mapsound.scary_zone2] scary_cd 0

# leaving scary ambient 2 area
execute as @a[tag=!mapsound.scary_zone2,scores={scary2_cd=1..}] run stopsound @s ambient mymap:scary_ambient2
scoreboard players set @a[tag=!mapsound.scary_zone2] scary2_cd 0

# entering normal scary area: stop ship loop
execute as @a[tag=mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={ship_cd=1..}] run stopsound @s ambient mymap:ship
scoreboard players set @a[tag=mapsound.scary_zone,tag=!mapsound.scary_zone2] ship_cd 0

# outside normal scary area: stop scary loop
execute as @a[tag=!mapsound.scary_zone,scores={scary_cd=1..}] run stopsound @s ambient mymap:scary_ambient
scoreboard players set @a[tag=!mapsound.scary_zone] scary_cd 0

# scary loop 1 while inside normal scary zones, but not scary zone 2
scoreboard players remove @a[tag=mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={scary_cd=1..}] scary_cd 1
execute as @a[tag=mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={scary_cd=0}] at @s run playsound mymap:scary_ambient ambient @s ~ ~ ~ 0.4 1
execute as @a[tag=mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={scary_cd=0}] run scoreboard players set @s scary_cd 1200

# scary loop 2 whenever X <= -26
scoreboard players remove @a[tag=mapsound.scary_zone2,scores={scary2_cd=1..}] scary2_cd 1
execute as @a[tag=mapsound.scary_zone2,scores={scary2_cd=0}] at @s run playsound mymap:scary_ambient2 ambient @s ~ ~ ~ 0.4 1
execute as @a[tag=mapsound.scary_zone2,scores={scary2_cd=0}] run scoreboard players set @s scary2_cd 1200

# ship loop while outside scary zones and outside scary zone 2
scoreboard players remove @a[tag=!mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={ship_cd=1..}] ship_cd 1
execute as @a[tag=!mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={ship_cd=0}] at @s run playsound mymap:ship ambient @s ~ ~ ~ 0.2 1
execute as @a[tag=!mapsound.scary_zone,tag=!mapsound.scary_zone2,scores={ship_cd=0}] run scoreboard players set @s ship_cd 1200

# remember corpse zone state for next tick
tag @a remove mapsound.was_corpse_zone
tag @a[tag=mapsound.corpse_zone] add mapsound.was_corpse_zone