~ruther/NosSmooth.Local

c524044a5fa1e892114f0793dd3758a611a17f3e — Rutherther 3 years ago 164d42b
feat: use short position in walk commands and return tolerance
M src/Core/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs => src/Core/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs +2 -3
@@ 30,7 30,6 @@ public class PetManagerBinding
    public static Result<PetManagerBinding> Create
        (NosBindingManager bindingManager, PetManagerList petManagerList, PetManagerBindingOptions options)
    {
        var petManager = new PetManagerBinding(petManagerList);
        var petManager = new PetManagerBinding(bindingManager.Memory, petManagerList);
        var hookResult = bindingManager.CreateHookFromPattern<PetWalkDelegate>
        (


@@ 97,9 96,9 @@ public class PetManagerBinding
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    /// <returns>A result returned from NosTale or an error.</returns>
    public Result<bool> PetWalk(int selector, ushort x, ushort y)
    public Result<bool> PetWalk(int selector, short x, short y)
    {
        uint position = ((uint)y << 16) | x;
        uint position = Convert.ToUInt32(((ushort)y << 16) | (ushort)x);
        if (PetManagerList.Length < selector + 1)
        {
            return new NotFoundError("Could not find the pet using the given selector.");

M src/Core/NosSmooth.LocalBinding/Objects/PlayerManagerBinding.cs => src/Core/NosSmooth.LocalBinding/Objects/PlayerManagerBinding.cs +2 -2
@@ 187,9 187,9 @@ public class PlayerManagerBinding
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result<bool> Walk(ushort x, ushort y)
    public Result<bool> Walk(short x, short y)
    {
        int param = (y << 16) | x;
        int param = ((ushort)y << 16) | (ushort)x;
        try
        {
            return _originalWalk(PlayerManager.Address, param);

M src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/ControlCommandWalkHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/ControlCommandWalkHandler.cs +27 -13
@@ 7,9 7,9 @@
using NosSmooth.Core.Client;
using NosSmooth.Core.Commands.Control;
using NosSmooth.Core.Commands.Walking;
using NosSmooth.Core.Errors;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalBinding.Structs;
using NosSmooth.LocalClient.CommandHandlers.Walk.Errors;
using Remora.Results;

namespace NosSmooth.LocalClient.CommandHandlers.Walk;


@@ 20,12 20,13 @@ namespace NosSmooth.LocalClient.CommandHandlers.Walk;
internal class ControlCommandWalkHandler
{
    private readonly INostaleClient _nostaleClient;
    private readonly Func<ushort, ushort, Result<bool>> _walkFunction;
    private readonly Func<short, short, Result<bool>> _walkFunction;
    private readonly ControlManager _controlManager;
    private readonly WalkCommandHandlerOptions _options;

    private ushort _x;
    private ushort _y;
    private short _x;
    private short _y;
    private ushort _tolerance;

    /// <summary>
    /// Initializes a new instance of the <see cref="ControlCommandWalkHandler"/> class.


@@ 37,7 38,7 @@ internal class ControlCommandWalkHandler
    public ControlCommandWalkHandler
    (
        INostaleClient nostaleClient,
        Func<ushort, ushort, Result<bool>> walkFunction,
        Func<short, short, Result<bool>> walkFunction,
        ControlManager controlManager,
        WalkCommandHandlerOptions options
    )


@@ 53,12 54,22 @@ internal class ControlCommandWalkHandler
    /// </summary>
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    /// <param name="tolerance">The distance tolerance when to return success.</param>
    /// <param name="command">The take control command.</param>
    /// <param name="groupName">The name of the take control group.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
    public async Task<Result> HandleCommand(ushort x, ushort y, ITakeControlCommand command, string groupName, CancellationToken ct = default)
    public async Task<Result> HandleCommand
    (
        short x,
        short y,
        ushort tolerance,
        ITakeControlCommand command,
        string groupName,
        CancellationToken ct = default
    )
    {
        _tolerance = tolerance;
        _x = x;
        _y = y;



@@ 74,6 85,7 @@ internal class ControlCommandWalkHandler
                {
                    ControlCancelReason.AnotherTask => WalkUnfinishedReason.AnotherTask,
                    ControlCancelReason.UserAction => WalkUnfinishedReason.UserAction,
                    ControlCancelReason.MapChanged => WalkUnfinishedReason.MapChanged,
                    _ => WalkUnfinishedReason.Unknown
                };
                return Task.FromResult(Result.FromSuccess());


@@ 86,7 98,7 @@ internal class ControlCommandWalkHandler
            return commandResult;
        }

        if (reason is null && !IsAt(x, y))
        if (reason is null && !IsAt(x, y, _tolerance))
        {
            reason = WalkUnfinishedReason.PathNotFound;
        }


@@ 104,15 116,17 @@ internal class ControlCommandWalkHandler
        );
    }

    private bool IsAtTarget()
    private bool IsAtTarget(ushort tolerance)
    {
        return _controlManager.TargetX == _controlManager.Entity.X
            && _controlManager.TargetY == _controlManager.Entity.Y;
        return IsAt(_controlManager.TargetX, _controlManager.TargetY, tolerance);
    }

    private bool IsAt(ushort x, ushort y)
    private bool IsAt(int x, int y, ushort tolerance)
    {
        return _controlManager.Entity.X == x && _controlManager.Entity.Y == y;
        var xDiff = x - _controlManager.Entity.X;
        var yDiff = y - _controlManager.Entity.Y;

        return ((xDiff * xDiff) + (yDiff * yDiff)) < tolerance * tolerance;
    }

    private async Task<Result> WalkGrantedCallback(CancellationToken ct)


@@ 134,7 148,7 @@ internal class ControlCommandWalkHandler
                // ignored
            }

            if (IsAtTarget() || IsAt(_x, _y))
            if (IsAtTarget(_tolerance) || IsAt(_x, _y, _tolerance))
            {
                return Result.FromSuccess();
            }

M src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PetWalkCommandHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PetWalkCommandHandler.cs +1 -0
@@ 67,6 67,7 @@ public class PetWalkCommandHandler : ICommandHandler<PetWalkCommand>
        (
            command.TargetX,
            command.TargetY,
            command.ReturnDistanceTolerance,
            command,
            PetWalkControlGroup + "_" + command.PetSelector,
            ct

M src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PlayerWalkCommandHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Walk/PlayerWalkCommandHandler.cs +1 -1
@@ 11,7 11,6 @@ using NosSmooth.Core.Commands.Control;
using NosSmooth.Core.Commands.Walking;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalBinding.Objects;
using NosSmooth.LocalClient.CommandHandlers.Walk.Errors;
using Remora.Results;

namespace NosSmooth.LocalClient.CommandHandlers.Walk;


@@ 63,6 62,7 @@ public class PlayerWalkCommandHandler : ICommandHandler<PlayerWalkCommand>
        (
            command.TargetX,
            command.TargetY,
            command.ReturnDistanceTolerance,
            command,
            PlayerWalkControlGroup,
            ct

Do not follow this link