#this is probably the kingpin of all the movement mcfunctions in doze off
#all objects that move run this after finishing a movement animation
#we need to figure out if we fall down, slip on ice, or do any number of other silly things

execute if block ~ ~ ~ barrier run setblock ~ ~ ~ air

execute if entity @s[tag=moving_block,tag=can_fall] if block ~ ~-1 ~ #bd:not_solid run tag @s add fall
execute if entity @s[tag=player_char] if block ~ ~-1 ~ #bd:not_solid run tag @s add fall
execute if entity @s[tag=player_char] if block ~ ~-1 ~ #bd:player_only run tag @s add fall
execute if entity @s[tag=player_char] if block ~ ~-1 ~ light_gray_concrete_powder run tag @s add fall

execute if entity @s[tag=moving_block,tag=sticky] run tag @s add no_slip
execute if entity @s[tag=moving_block,tag=linked_block] run tag @s add no_slip
execute if block ~ ~-1 ~ #bd:slippery unless entity @s[tag=no_slip] unless entity @s[scores={moveCommit=9}] run tag @s add keepgoing
execute if entity @s[tag=moving_block,tag=slippery,tag=!no_slip,tag=!fall] run tag @s add keepgoing

#barrels keep going in the right direction
execute if entity @s[tag=moving_block,tag=barrel_z,tag=!no_slip,tag=!fall,scores={moveCommit=3}] positioned ~ ~1.5 ~ unless entity @e[tag=player_char,type=area_effect_cloud,distance=..1] run tag @s add keepgoing
execute if entity @s[tag=moving_block,tag=barrel_z,tag=!no_slip,tag=!fall,scores={moveCommit=7}] positioned ~ ~1.5 ~ unless entity @e[tag=player_char,type=area_effect_cloud,distance=..1] run tag @s add keepgoing
execute if entity @s[tag=moving_block,tag=barrel_x,tag=!no_slip,tag=!fall,scores={moveCommit=1}] positioned ~ ~1.5 ~ unless entity @e[tag=player_char,type=area_effect_cloud,distance=..1] run tag @s add keepgoing
execute if entity @s[tag=moving_block,tag=barrel_x,tag=!no_slip,tag=!fall,scores={moveCommit=5}] positioned ~ ~1.5 ~ unless entity @e[tag=player_char,type=area_effect_cloud,distance=..1] run tag @s add keepgoing

#ice makes paths over wet cement
execute if entity @s[tag=moving_block,tag=slippery] if block ~ ~-1 ~ light_gray_concrete_powder run setblock ~ ~-1 ~ light_gray_concrete

#arrow tiles (bulldozer only)
execute if entity @s[tag=player_char,scores={faceDirection=1}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=north] if block ~ ~ ~1 #bd:passable run tag @s add move_1
execute if entity @s[tag=player_char,scores={faceDirection=5}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=north] if block ~ ~ ~1 #bd:passable run tag @s add move_1
execute if entity @s[tag=player_char,scores={faceDirection=1}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=south] if block ~ ~ ~-1 #bd:passable run tag @s add move_5
execute if entity @s[tag=player_char,scores={faceDirection=5}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=south] if block ~ ~ ~-1 #bd:passable run tag @s add move_5

execute if entity @s[tag=player_char,scores={faceDirection=3}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=west] if block ~1 ~ ~ #bd:passable run tag @s add move_7
execute if entity @s[tag=player_char,scores={faceDirection=7}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=west] if block ~1 ~ ~ #bd:passable run tag @s add move_7
execute if entity @s[tag=player_char,scores={faceDirection=3}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=east] if block ~-1 ~ ~ #bd:passable run tag @s add move_3
execute if entity @s[tag=player_char,scores={faceDirection=7}] if block ~ ~-1 ~ magenta_glazed_terracotta[facing=east] if block ~-1 ~ ~ #bd:passable run tag @s add move_3

execute if entity @s[tag=player_char,tag=move_1,scores={faceDirection=1}] unless block ~ ~ ~2 #bd:passable run tag @s remove move_1
execute if entity @s[tag=player_char,tag=move_5,scores={faceDirection=5}] unless block ~ ~ ~-2 #bd:passable run tag @s remove move_5
execute if entity @s[tag=player_char,tag=move_7,scores={faceDirection=7}] unless block ~2 ~ ~ #bd:passable run tag @s remove move_7
execute if entity @s[tag=player_char,tag=move_3,scores={faceDirection=3}] unless block ~-2 ~ ~ #bd:passable run tag @s remove move_3

#blocks that push the player back from whence they came
execute if entity @s[tag=player_char] if block ~ ~-1 ~ #bd:reverse_move run playsound minecraft:entity.player.hurt_on_fire master @a ~ ~ ~ 2.5 1.3
execute if entity @s[tag=player_char,scores={moveCommit=1}] if block ~ ~-1 ~ #bd:reverse_move run tag @s add move_5
execute if entity @s[tag=player_char,scores={moveCommit=5}] if block ~ ~-1 ~ #bd:reverse_move run tag @s add move_1
execute if entity @s[tag=player_char,scores={moveCommit=3}] if block ~ ~-1 ~ #bd:reverse_move run tag @s add move_7
execute if entity @s[tag=player_char,scores={moveCommit=7}] if block ~ ~-1 ~ #bd:reverse_move run tag @s add move_3

#check movement here... ice be like
execute if entity @s[tag=keepgoing,scores={moveCommit=1}] unless block ~ ~ ~1 #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=keepgoing,scores={moveCommit=3}] unless block ~-1 ~ ~ #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=keepgoing,scores={moveCommit=5}] unless block ~ ~ ~-1 #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=keepgoing,scores={moveCommit=7}] unless block ~1 ~ ~ #bd:passable run tag @s remove keepgoing

#pesky head
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={moveCommit=1,faceDirection=1}] unless block ~ ~ ~2 #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={moveCommit=3,faceDirection=3}] unless block ~-2 ~ ~ #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={moveCommit=5,faceDirection=5}] unless block ~ ~ ~-2 #bd:passable run tag @s remove keepgoing
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={moveCommit=7,faceDirection=7}] unless block ~2 ~ ~ #bd:passable run tag @s remove keepgoing

#check for cone
execute if block ~ ~ ~ dark_oak_fence run scoreboard players set global hitCone 1
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={faceDirection=1}] if block ~ ~ ~1 dark_oak_fence run scoreboard players set global hitCone 1
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={faceDirection=3}] if block ~-1 ~ ~ dark_oak_fence run scoreboard players set global hitCone 1
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={faceDirection=5}] if block ~ ~ ~-1 dark_oak_fence run scoreboard players set global hitCone 1
execute if entity @s[tag=player_char,tag=!arm_disconnected,scores={faceDirection=7}] if block ~1 ~ ~ dark_oak_fence run scoreboard players set global hitCone 1
execute if block ~ ~ ~ dark_oak_fence run setblock ~ ~ ~ air destroy

#check for arm reconnect
execute if entity @s[tag=player_char,tag=arm_disconnected,tag=!fall,scores={faceDirection=1}] positioned ~ ~ ~1 if block ~ ~ ~ spruce_fence_gate run function bd:movement/object/arm_reconnect
execute if entity @s[tag=player_char,tag=arm_disconnected,tag=!fall,scores={faceDirection=3}] positioned ~-1 ~ ~ if block ~ ~ ~ birch_fence_gate run function bd:movement/object/arm_reconnect
execute if entity @s[tag=player_char,tag=arm_disconnected,tag=!fall,scores={faceDirection=5}] positioned ~ ~ ~-1 if block ~ ~ ~ jungle_fence_gate run function bd:movement/object/arm_reconnect
execute if entity @s[tag=player_char,tag=arm_disconnected,tag=!fall,scores={faceDirection=7}] positioned ~1 ~ ~ if block ~ ~ ~ acacia_fence_gate run function bd:movement/object/arm_reconnect

#check for arm disconnect
execute if entity @s[tag=player_char,tag=!arm_disconnected,tag=fall,scores={faceDirection=1}] positioned ~ ~ ~1 unless block ~ ~-1 ~ #bd:no_arm_break run function bd:movement/object/arm_disconnect_1
execute if entity @s[tag=player_char,tag=!arm_disconnected,tag=fall,scores={faceDirection=3}] positioned ~-1 ~ ~ unless block ~ ~-1 ~ #bd:no_arm_break run function bd:movement/object/arm_disconnect_3
execute if entity @s[tag=player_char,tag=!arm_disconnected,tag=fall,scores={faceDirection=5}] positioned ~ ~ ~-1 unless block ~ ~-1 ~ #bd:no_arm_break run function bd:movement/object/arm_disconnect_5
execute if entity @s[tag=player_char,tag=!arm_disconnected,tag=fall,scores={faceDirection=7}] positioned ~1 ~ ~ unless block ~ ~-1 ~ #bd:no_arm_break run function bd:movement/object/arm_disconnect_7

#blocks fast fall if player nearby
execute if entity @s[tag=moving_block,tag=can_fall,tag=fall] at @s if entity @e[tag=player_char,distance=..2] run tag @s add fastfall
tag @s[tag=fastfall] remove fall

#noise when bulldozer hits the ground
execute if entity @s[tag=player_char,tag=!fall,scores={moveCommitPrev=10}] run playsound minecraft:entity.player.small_fall master @a ~ ~ ~ 1.5 .7

#keep going or fall logic
execute if entity @s[tag=fall] run tag @s remove keepgoing
execute if entity @s[tag=fastfall] run tag @s remove keepgoing
execute if entity @s[tag=keepgoing] if entity @s[scores={moveCommitPrev=10}] run tag @s remove keepgoing
execute if entity @s[tag=keepgoing] if entity @s[scores={moveCommitPrev=11}] run tag @s remove keepgoing
execute if entity @s[tag=keepgoing] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME
execute if entity @s[tag=fall] run scoreboard players set @s moveCommit 10
execute if entity @s[tag=fall] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME
execute if entity @s[tag=fastfall] at @s run tp @s ~ ~-1 ~
execute if entity @s[tag=fastfall] run scoreboard players set @s moveCommit 11
execute if entity @s[tag=fastfall] run scoreboard players set @s moveCommitTime 1

execute if entity @s[tag=move_1] run scoreboard players set @s moveCommit 1
execute if entity @s[tag=move_1] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME
execute if entity @s[tag=move_3] run scoreboard players set @s moveCommit 3
execute if entity @s[tag=move_3] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME
execute if entity @s[tag=move_5] run scoreboard players set @s moveCommit 5
execute if entity @s[tag=move_5] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME
execute if entity @s[tag=move_7] run scoreboard players set @s moveCommit 7
execute if entity @s[tag=move_7] run scoreboard players operation @s moveCommitTime = global MOVECOMMITTIME

#convert moving blocks back to tiles
execute if entity @s[tag=moving_block,tag=!fall,tag=!keepgoing,tag=!fastfall] if block ~ ~ ~ #bd:passable run function bd:movement/object/convert_to_block

#piston raise thing
execute if entity @s[tag=player_char] if block ~ ~-1 ~ piston run function bd:movement/object/piston_raise

#if on ice or something, keep disallowing new move inputs
execute if entity @s[tag=player_char,scores={moveCommitTime=1..}] run scoreboard players set global moveCooldown 10

#check win?
execute if entity @s[tag=player_char] unless score global currentLevel matches 1 unless score global levelLoading matches 1.. run function bd:game/check_for_win

#level entry/exit
execute if entity @s[tag=player_char,scores={moveCommitTime=..0}] positioned ~ ~-.2 ~ if entity @e[type=armor_stand,tag=level_entry,distance=..1] positioned ~ ~.2 ~ run function bd:levels/try_enter
execute if entity @s[tag=player_char,scores={moveCommitTime=..0}] positioned ~ ~-.2 ~ if entity @e[type=armor_stand,tag=level_exit,distance=..1] positioned ~ ~.2 ~ run function bd:levels/try_exit

#update victoryState
execute if entity @s[tag=player_char] run scoreboard players set global checkForWin 2

tag @s remove fall
tag @s remove keepgoing
tag @s remove move_1
tag @s remove move_3
tag @s remove move_5
tag @s remove move_7
tag @s remove no_slip
tag @s remove fastfall