scoreboard players operation @s up_down = global up_down
scoreboard players operation @s left_right = global left_right

scoreboard players set global validMove 1
scoreboard players set global validMoveXPOS 1
scoreboard players set global validMoveXNEG 1
scoreboard players set global validMoveZPOS 1
scoreboard players set global validMoveZNEG 1

#what move did we try to make?
execute if entity @s[tag=freecam,scores={left_right=1}] run tag @s add forward
execute if entity @s[tag=freecam,scores={left_right=-1}] run tag @s add reverse
execute if entity @s[tag=!freecam,scores={left_right=1,up_down=0}] run tag @s add forward
execute if entity @s[tag=!freecam,scores={left_right=-1,up_down=0}] run tag @s add reverse

execute if entity @s[tag=freecam,scores={up_down=1,left_right=0}] run tag @s add rotate_6
execute if entity @s[tag=freecam,scores={up_down=-1,left_right=0}] run tag @s add rotate_7
execute if entity @s[tag=!freecam,scores={up_down=1}] run tag @s add rotate_6
execute if entity @s[tag=!freecam,scores={up_down=-1}] run tag @s add rotate_7

#check if moves are valid
execute if entity @s[tag=forward] positioned ~-1 ~ ~ run function bd:movement/check_move/x_negative
execute if entity @s[tag=reverse] positioned ~1 ~ ~ run function bd:movement/check_move/x_positive

#execute if entity @s[tag=rotate_7] positioned ~-1 ~ ~-1 run function bd:movement/check_move/z_negative
#execute if entity @s[tag=rotate_7] positioned ~ ~ ~-1 run function bd:movement/check_move/x_positive

#execute if entity @s[tag=rotate_6] positioned ~-1 ~ ~1 run function bd:movement/check_move/z_positive
#execute if entity @s[tag=rotate_6] positioned ~ ~ ~1 run function bd:movement/check_move/x_positive

#execute if score global validMove matches 0 run say move failed

#don't freak out over #bd:player_only please
execute if entity @s[tag=forward] unless score global validMove matches 1 if block ~-1 ~ ~ #bd:player_only run scoreboard players set global validMove 1
execute if entity @s[tag=reverse] unless score global validMove matches 1 if block ~1 ~ ~ #bd:player_only run scoreboard players set global validMove 1

#move was tried and was valid? do it
execute if entity @s[tag=forward] if score global validMove matches 1 run function bd:movement/move_left_reverse
execute if entity @s[tag=reverse] if score global validMove matches 1 run function bd:movement/move_right_reverse
execute if entity @s[tag=rotate_7] if score global validMove matches 1 run function bd:movement/rotate_7
execute if entity @s[tag=rotate_6] if score global validMove matches 1 run function bd:movement/rotate_6

execute if entity @s[tag=forward,scores={ambientTime2=..5}] run scoreboard players set @s ambientTime2 20
execute if entity @s[tag=reverse] run function bd:ambient/backing_up

#clean up tags
tag @s remove forward
tag @s remove reverse
tag @s remove rotate_6
tag @s remove rotate_7