execute if score @s controlState matches ..0 at @s run function bd:control/load_pos

scoreboard players set @s controlState 1
scoreboard players add @s[scores={controlAnimate=..999}] controlAnimate 1
effect clear @s blindness

#inventory buttons
function bd:control/inventory_controls

#hotbar buttons
function bd:control/hotbar_controls

###################################
#tag the entities that belong to us

#input_base
execute at @s as @e[tag=input_base,type=armor_stand,limit=1,sort=nearest] if score @s playerID = @a[limit=1,sort=nearest,tag=pl_self] playerID run tag @s add my_base
execute unless entity @e[tag=my_base,tag=input_base,type=armor_stand,limit=1,sort=nearest] as @e[tag=input_base,type=armor_stand] if score @s playerID = @a[limit=1,sort=nearest,tag=pl_self] playerID run tag @s add my_base

#player_char
execute as @e[tag=player_char] if score @s playerID = @a[limit=1,sort=nearest,tag=pl_self] playerID run tag @s add my_char
###################################

#reload if player char is missing
execute if entity @e[tag=my_char,limit=1] at @s run scoreboard players set @s charTimeout 0
execute unless entity @e[tag=my_char,limit=1] at @s run scoreboard players add @s charTimeout 1
execute if entity @s[scores={charTimeout=5..}] run function bd:control/load_pos

#keep input stack alive as long as we're near
scoreboard players set @e[tag=input,distance=..3] lifespan 20
scoreboard players set @e[tag=my_char,limit=1,sort=nearest] lifespan 10

#camera
function bd:control/camera/cam_main

#visualization
execute as @e[tag=my_char,limit=1,sort=nearest] at @s run function bd:control/char_get_model

#movement here
execute if entity @e[tag=my_char,tag=!noMove,limit=1,sort=nearest] run function bd:movement/_move_main

#clean up tags for next player that runs this
tag @e[tag=my_base] remove my_base
tag @e[tag=my_char] remove my_char
tag @e[tag=my_model] remove my_model