# First-join spawn safety
execute as @a unless score @s darkship_joined matches 1 run tp @s 85.5 140 37.0
execute as @a unless score @s darkship_joined matches 1 run gamemode adventure @s
execute as @a unless score @s darkship_joined matches 1 run spawnpoint @s 85 140 37
execute as @a unless score @s darkship_joined matches 1 run scoreboard players set @s darkship_joined 1

execute as @a store result score @s deck1_x run data get entity @s Pos[0] 100
execute as @a run scoreboard players operation @s deck1_dx = @s deck1_x
execute as @a run scoreboard players operation @s deck1_dx -= @s deck1_prevx

# entering the doorway from the east, moving west into Deck 1
execute as @a[tag=!deck1_zone,scores={deck1_dx=..-1}] if entity @s[x=106,y=145,z=19,dx=0,dy=0,dz=1] run function ship:deck1_enter

# entering the doorway from the west, moving east back out
execute as @a[tag=!deck1_zone,scores={deck1_dx=1..}] if entity @s[x=106,y=145,z=19,dx=0,dy=0,dz=1] run function ship:deck1_return

# remember who is currently in the doorway
execute as @a[tag=!deck1_zone] if entity @s[x=106,y=145,z=19,dx=0,dy=0,dz=1] run tag @s add deck1_zone
execute as @a[tag=deck1_zone] unless entity @s[x=106,y=145,z=19,dx=0,dy=0,dz=1] run tag @s remove deck1_zone

# save current X for next tick
execute as @a run scoreboard players operation @s deck1_prevx = @s deck1_x

# Remove iron bars
execute as @e[type=minecraft:item] if items entity @s contents minecraft:iron_bars run kill @s
execute as @e[type=minecraft:item] if items entity @s contents minecraft:cracked_stone_bricks run kill @s


# read lever state each tick
scoreboard players set #cur lever_8214621 0
execute if block 82 146 21 minecraft:lever[powered=true] run scoreboard players set #cur lever_8214621 1

# lever OFF -> make sure blocks exist
execute if score #cur lever_8214621 matches 0 unless block 86 145 16 minecraft:stripped_acacia_wood run setblock 86 145 16 minecraft:stripped_acacia_wood
execute if score #cur lever_8214621 matches 0 unless block 86 146 16 minecraft:prismarine_brick_slab[type=bottom] run setblock 86 146 16 minecraft:prismarine_brick_slab[type=bottom]
execute if score #cur lever_8214621 matches 0 unless block 86 145 15 minecraft:acacia_shelf[facing=north] run setblock 86 145 15 minecraft:acacia_shelf[facing=north]

# lever ON -> remove all three blocks
execute if score #cur lever_8214621 matches 1 unless block 86 145 16 minecraft:air run setblock 86 145 16 minecraft:air
execute if score #cur lever_8214621 matches 1 unless block 86 146 16 minecraft:air run setblock 86 146 16 minecraft:air
execute if score #cur lever_8214621 matches 1 unless block 86 145 15 minecraft:air run setblock 86 145 15 minecraft:air

# OFF transition only -> extend sound
execute if score #cur lever_8214621 matches 0 if score #prev lever_8214621 matches 1 run playsound minecraft:block.piston.extend master @a 86.5 145.5 16.0 8 0.8

# ON transition only -> retract sound
execute if score #cur lever_8214621 matches 1 if score #prev lever_8214621 matches 0 run playsound minecraft:block.piston.contract master @a 86.5 145.5 16.0 8 0.8

# store current state for next tick
scoreboard players operation #prev lever_8214621 = #cur lever_8214621


# Deck 0 trigger area: 44 140 17 to 44 141 15
# Area becomes x=44,y=140,z=15,dx=0,dy=1,dz=2

# entering Deck 0, moving west
execute as @a[tag=!deck0_zone,scores={deck1_dx=..-1}] if entity @s[x=44,y=140,z=15,dx=0,dy=1,dz=2] run function ship:deck0_enter

# trying to go back, moving east
execute as @a[tag=!deck0_zone,scores={deck1_dx=1..}] if entity @s[x=44,y=140,z=15,dx=0,dy=1,dz=2] run function ship:deck0_return

# remember who is currently in the Deck 0 trigger area
execute as @a[tag=!deck0_zone] if entity @s[x=44,y=140,z=15,dx=0,dy=1,dz=2] run tag @s add deck0_zone
execute as @a[tag=deck0_zone] unless entity @s[x=44,y=140,z=15,dx=0,dy=1,dz=2] run tag @s remove deck0_zone

# Deck -1 trigger area: -26 132 33 to -26 133 35
# Area becomes x=-26,y=132,z=33,dx=0,dy=1,dz=2

# entering Deck -1, moving west
execute as @a[tag=!deck_minus1_zone,scores={deck1_dx=..-1}] if entity @s[x=-26,y=132,z=33,dx=0,dy=1,dz=2] run function ship:deck_minus1_enter

# trying to go back, moving east
execute as @a[tag=!deck_minus1_zone,scores={deck1_dx=1..}] if entity @s[x=-26,y=132,z=33,dx=0,dy=1,dz=2] run function ship:deck_minus1_return

# remember who is currently in the Deck -1 trigger area
execute as @a[tag=!deck_minus1_zone] if entity @s[x=-26,y=132,z=33,dx=0,dy=1,dz=2] run tag @s add deck_minus1_zone
execute as @a[tag=deck_minus1_zone] unless entity @s[x=-26,y=132,z=33,dx=0,dy=1,dz=2] run tag @s remove deck_minus1_zone

function ship:check_map_solution

function ship:leverwall_check

