scoreboard players operation @s up_down = global up_down
scoreboard players operation @s left_right = global left_right
tag @s remove freecam
execute if score global freecam matches 1 run tag @s add freecam

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={up_down=1}] run tag @s add forward
execute if entity @s[tag=freecam,scores={up_down=-1}] run tag @s add reverse
execute if entity @s[tag=!freecam,scores={up_down=1,left_right=0}] run tag @s add forward
execute if entity @s[tag=!freecam,scores={up_down=-1,left_right=0}] run tag @s add reverse

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

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

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

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

#revalidate movement if only #bd:breakable or #bd:player_only stopped us
execute if entity @s[tag=forward] unless score global validMove matches 1 if block ~ ~ ~2 #bd:breakable run scoreboard players set global validMove 1
execute if entity @s[tag=rotate_5] unless score global validMove matches 1 if block ~-1 ~ ~1 #bd:break_ok if block ~-1 ~ ~ #bd:break_ok run scoreboard players set global validMove 1
execute if entity @s[tag=rotate_4] unless score global validMove matches 1 if block ~1 ~ ~1 #bd:break_ok if block ~1 ~ ~ #bd:break_ok run scoreboard players set global validMove 1

execute if entity @s[tag=forward] unless score global validMove matches 1 if block ~ ~ ~2 #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
execute if entity @s[tag=rotate_5] unless score global validMove matches 1 if score global validMoveXNEG matches 1 if block ~-1 ~ ~ #bd:player_only run scoreboard players set global validMove 1
execute if entity @s[tag=rotate_5] unless score global validMove matches 1 if score global validMoveZNEG matches 1 if block ~-1 ~ ~1 #bd:player_only run scoreboard players set global validMove 1
execute if entity @s[tag=rotate_4] unless score global validMove matches 1 if score global validMoveXPOS matches 1 if block ~1 ~ ~ #bd:player_only run scoreboard players set global validMove 1
execute if entity @s[tag=rotate_4] unless score global validMove matches 1 if score global validMoveZNEG matches 1 if block ~1 ~ ~1 #bd:player_only run scoreboard players set global validMove 1

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

#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_down_forward
execute if entity @s[tag=reverse] if score global validMove matches 1 run function bd:movement/move_up_reverse
execute if entity @s[tag=rotate_5] if score global validMove matches 1 run function bd:movement/rotate_5
execute if entity @s[tag=rotate_4] if score global validMove matches 1 run function bd:movement/rotate_4

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_4
tag @s remove rotate_5