objectdef MrNavigation
{
    variable string autorun="num lock"
    variable string forward=w
    variable string backward=s
    variable string strafeleft=q
    variable string straferight=e
    variable string turnleft=a
    variable string turnright=d

    method FollowPath(... path)
    {
        variable int index
        for (index:Set[1]; ${index} < ${path.Size}; index:Inc)
        {
            variable point3f targetLocation
            targetLocation:Set[${path[index]:Token[1,","]},${path[index]:Token[2,","]},${path[index]:Token[3,","]}]
            echo ${targetLocation}
            call This.MoveTo path[index]
        }
    }


    function MoveTo(float x, float z, float percision)
    {
        if !${percision}
        {
            percision:Set[5]
        }

        variable float angle
        echo "Moving to ${x}, ${z}"
        press "${autorun}"
        do
        {
            echo "${Math.Distance[${Me.X},${Me.Z}, x, z]} units away from target"
            face ${x} ${z}
            
            wait 5

        } 
        while ${Math.Distance[${Me.X},${Me.Z}, locatoin.X, location.Z]} > ${percision}
        press "${autorun}"
    }

    function Follow(int targetId)
    {
        echo "Following ${Actor[${targetId}].Name}"
        if !${percision}
        {
            percision:Set[5]
        }

        variable float angle
        variable point3f targetLocation
        do
        {
            targetLocation:Set[${Actor[${targetId}].Loc}]
            wait 5
            if ${Actor[${targetId}].Distance} <= 3
            {
                if ${Me.IsMoving}
                {
                    echo "Target is close enough"
                    press "${autorun}"
                }
                
                continue
            }
            else
            {
                if !${Me.IsMoving}
                {
                    echo "Target is too far away"
                    press "${autorun}"
                }
            }


            echo " ${Actor[${targetId}].Distance} units away from target"
            face ${targetLocation.X} ${targetLocation.Z}
        } 
        while TRUE
    }
}

function main()
{
    variable int followTargetId = ${Me.Target.ID}
    variable MrNavigation nav

    call nav.Follow ${followTargetId}
}