## Activated ability for vanilla. Adjust animation length (cooldown) in endmo-bp/animations/player.json > animation.player.morph_vanilla_activated

## Add the Raycast objective that will count how many times the raycast function is run in `morph/vanilla_raycast`
scoreboard objectives add Raycast dummy

## From the player's eye height, check one block in front to see if the path is clear (end_rod also included for the escaping jail sequence)
execute positioned ~ ~1.575 ~ if block ^ ^ ^1 air if block ^ ^ ^0.1 air if block ~ ~ ~ air run tag @s add ClearPath
execute positioned ~ ~1.575 ~ if block ^ ^ ^1 end_rod if block ~ ~ ~ air run tag @s add ClearPath
execute positioned ~ ~1.575 ~ if block ^ ^ ^1 end_rod if block ~ ~ ~ light_block run tag @s add ClearPath
## If the path is clear, summon an enderman_teleport raycaster one block in front and rotated `~ ~` to match the player
execute positioned ~ ~1.575 ~ if entity @s[tag=ClearPath] run summon ldz:raycaster ^ ^ ^1 ~ ~ enderman_teleport
## This makes the first call to `morph/vanilla_raycast`, which will be called recursively until its path is no longer clear
execute as @e[type=ldz:raycaster,family=enderman_teleport,c=1] at @s run function morph/vanilla_raycast

## Sends a debug message to anyone with tag=dev indicating how many iterations the raycast just took
execute as @e[type=ldz:raycaster,family=enderman_teleport,c=1] run tellraw @a[tag=dev] {"rawtext":[{"text":"Raycast Iterations: "},{ "score": {"name": "@s", "objective": "Raycast" }}]}

## Before teleporting the player to the final location of the raycaster, we play a particle and sound effect at their previous location
execute as @s[tag=ClearPath] run playsound player.morph.vanilla.teleport @a ~ ~ ~ 0.75
execute as @s[tag=ClearPath] run particle ldz:vanilla_teleport ~ ~ ~

## If the raycaster didn't indicate that it made a normal collision, then we know it went out of simulation distance.
## In this case, we bring the player to the ground so they don't wind up high in the air
execute as @e[type=ldz:raycaster,family=enderman_teleport,c=1,tag=!Collision] at @s run function bring_to_ground

## Make sure the raycaster is not in the void before telporting
execute as @s[tag=!InEnd] as @e[type=ldz:raycaster,family=enderman_teleport,c=1] at @s as @s[y=-64,dy=1] run tag @s add InVoid
execute as @s[tag=InEnd] as @e[type=ldz:raycaster,family=enderman_teleport,c=1] at @s as @s[y=0,dy=1] run tag @s add InVoid
execute if entity @e[tag=InVoid,type=ldz:raycaster,family=enderman_teleport,c=1] run tag @s remove ClearPath

## Now that the raycaster is at its final destination, we teleport the player positioned and rotated as it 0.5 blocks backwards so they don't end up in a block
execute positioned as @e[tag=!InVoid,type=ldz:raycaster,family=enderman_teleport,c=1] run function morph/vanilla_tp_p1

## If the player is inside a block or high off the ground, we normalize their height
execute as @s[tag=ClearPath] if block ~ ~-1.575 ~ air if block ~ ~-1.0 ~ air if block ~ ~-0.5 ~ air run tp ~ ~-1.575 ~
execute as @s[tag=ClearPath] unless block ~ ~1.25 ~ air run tp ~ ~-1.25 ~

## Finally we despawn the raycaster entity and remove the ClearPath tag from the player
event entity @e[type=ldz:raycaster,family=enderman_teleport,c=1] despawn
tag @s remove ClearPath