
# -> Set existence check
scoreboard players set #or.player_downed_exists OverlyRealistic.General.ArbitraryData 1

# -> Temp tag
tag @s add OverlyRealistic.Player.TempPlayer

# -> Operate crawl check time
scoreboard players set #or.player_is_crawling OverlyRealistic.General.ArbitraryData 1
scoreboard players add @s OverlyRealistic.Player.DownedCrawlCheckTime 1
execute if score @s OverlyRealistic.Player.DownedCrawlCheckTime matches 20 \
    anchored eyes \
    positioned ^ ^ ^ \
    anchored feet \
    unless entity @s[distance=..1.21] run \
        scoreboard players set #or.player_is_crawling OverlyRealistic.General.ArbitraryData 0
execute if score @s OverlyRealistic.Player.DownedCrawlCheckTime matches 20 run \
    scoreboard players set @s OverlyRealistic.Player.DownedCrawlCheckTime 0

# -> Operate on downed player parent ent
execute as @e[type=block_display,tag=OverlyRealistic.DownedPlayerParentEnt] \
    if score @s OverlyRealistic.Player.DeathID = @p[tag=OverlyRealistic.Player.TempPlayer,sort=arbitrary] OverlyRealistic.Player.DeathID \
    at @s run \
        function overly_realistic:general/downing/downing_parent_ent_operate

# -> Reset motion
scoreboard players add @s OverlyRealistic.Player.DownedMotionTime 1
execute if score @s OverlyRealistic.Player.DownedMotionTime matches 4.. \
    if predicate overly_realistic:player/actions/player_is_moving_horizontal \
    at @s run \
        tp @s @s
scoreboard players reset @s[scores={OverlyRealistic.Player.DownedMotionTime=4..}] OverlyRealistic.Player.DownedMotionTime

# -> If player has not gotten their attributes back, then restore them
execute unless entity @s[tag=OverlyRealistic.Player.RestoredModifiersPostDeath] run \
    function overly_realistic:general/downing/attribute_restoration/restoration_start

# -> Decrease cooldown timer
scoreboard players remove @s OverlyRealistic.Player.DeathTimer 1

# -> Actionbar display
title @s actionbar [{"bold":false,"color":"#A80000","text":"☠ "},{"bold":true,"text":"You are down! Dying in: "},{"bold":true,"score":{"name":"@s","objective":"OverlyRealistic.Player.DeathTimer"}},{"bold":false,"text":" ☠"}]

# -> If another player is near then operater
scoreboard players set #or.player_is_helping_downed_player OverlyRealistic.General.ArbitraryData 0
scoreboard players set #or.helper_has_healthcare_levels OverlyRealistic.General.ArbitraryData 0
execute as @a[tag=!OverlyRealistic.Player.TempPlayer,distance=..2,scores={OverlyRealistic.PredicateValue.IsSneaking=1}] run \
    function overly_realistic:general/downing/downed_player_get_help/player_help_downed_player

# -> If player is getting helped then operate
execute if score #or.player_is_helping_downed_player OverlyRealistic.General.ArbitraryData matches 1 run \
    function overly_realistic:general/downing/downed_player_get_help/player_getting_helped

# -> Untag player
tag @s remove OverlyRealistic.Player.TempPlayer

# -> Function tag
function #overly_realistic:mechanics/player_downed_operate

# -> Black screen operations
execute if score @s OverlyRealistic.Player.DeathTimer matches 151..170 run \
    title @s title " "
execute if score @s OverlyRealistic.Player.DeathTimer matches 150 run \
    function overly_realistic:general/downing/downing_final_screen

# -> If timer is insignificant then return
execute if score @s OverlyRealistic.Player.DeathTimer matches 1..2999 run \
    return fail

# -> If timer >= 1000 then save player
execute if score @s OverlyRealistic.Player.DeathTimer matches 3000.. run \
    return run \
        function overly_realistic:general/downing/downing_end_saved

# -> If timer <= 0 then kill off player
execute if score @s OverlyRealistic.Player.DeathTimer matches ..0 run \
    function overly_realistic:general/downing/downing_end_killed
