~ruther/NosSmooth

cc1c2439854f6bc008f2d22968f40dfd8be8a7b1 — František Boháček 2 years ago e67bb2b
feat: add pet positions to walk command

Resolves #33
M Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs => Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs +3 -2
@@ 4,6 4,7 @@
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using NosSmooth.Core.Commands.Control;

namespace NosSmooth.Core.Commands.Walking;


@@ 13,7 14,7 @@ namespace NosSmooth.Core.Commands.Walking;
/// </summary>
/// <param name="TargetX">The target x coordinate.</param>
/// <param name="TargetY">The target y coordinate.</param>
/// <param name="PetSelectors">The pet indices.</param>
/// <param name="Pets">The pet walk positions.</param>
/// <param name="ReturnDistanceTolerance">The distance tolerance to the target when to return successful result.</param>
/// <param name="CanBeCancelledByAnother">Whether the command may be cancelled by another task within the same group.</param>
/// <param name="WaitForCancellation">Whether to wait for finish of the previous task</param>


@@ 22,7 23,7 @@ public record WalkCommand
(
    short TargetX,
    short TargetY,
    int[] PetSelectors,
    IReadOnlyList<(int PetSelector, short TargetX, short TargetY)>? Pets,
    ushort ReturnDistanceTolerance,
    bool CanBeCancelledByAnother = true,
    bool WaitForCancellation = true,

M Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs => Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs +5 -20
@@ 4,6 4,7 @@
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;


@@ 35,26 36,10 @@ internal class WalkCommandHandler : ICommandHandler<WalkCommand>
    public async Task<Result> HandleCommand(WalkCommand command, CancellationToken ct = default)
    {
        var tasks = new List<Task<Result>>();
        for (var i = 0; i < command.PetSelectors.Length; i++)
        foreach (var pet in command.Pets ?? Array.Empty<(int, short, short)>())
        {
            short xOffset = (short)(-1 + (i % 3));
            short yOffset = (short)(-1 + ((i / 3) % 5));
            if (xOffset == 0 && yOffset == 0)
            {
                yOffset += 2;
            }

            int x = command.TargetX;
            int y = command.TargetY;

            if (x + xOffset > 0)
            {
                x += xOffset;
            }
            if (y + yOffset > 0)
            {
                y += yOffset;
            }
            int x = pet.TargetX;
            int y = pet.TargetY;

            tasks.Add
            (


@@ 62,7 47,7 @@ internal class WalkCommandHandler : ICommandHandler<WalkCommand>
                (
                    new PetWalkCommand
                    (
                        command.PetSelectors[i],
                        pet.PetSelector,
                        (short)x,
                        (short)y,
                        command.ReturnDistanceTolerance,

M Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs => Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs +3 -3
@@ 44,9 44,9 @@ public class WalkManager
    /// <param name="y">The target y coordinate.</param>
    /// <param name="allowUserActions">Whether to allow user actions during the walk operation.</param>
    /// <param name="ct">The cancellation token used for cancelling the operation.</param>
    /// <param name="petSelectors">The pet selectors to go with.</param>
    /// <param name="pets">The positions to walk pets to.</param>
    /// <returns>A result that may not succeed.</returns>
    public async Task<Result> GoToAsync(short x, short y, bool allowUserActions = true, CancellationToken ct = default, params int[] petSelectors)
    public async Task<Result> GoToAsync(short x, short y, bool allowUserActions = true, CancellationToken ct = default, params (int Selector, short TargetX, short TargetY)[] pets)
    {
        var pathResult = _pathfinder.FindPathFromCurrent(x, y);
        if (!pathResult.IsSuccess)


@@ 68,7 68,7 @@ public class WalkManager
            }

            var next = path.TakeForwardPath();
            var walkResult = await _client.SendCommandAsync(new WalkCommand(next.X, next.Y, petSelectors, 2, AllowUserCancel: allowUserActions), ct);
            var walkResult = await _client.SendCommandAsync(new WalkCommand(next.X, next.Y, pets, 2, AllowUserCancel: allowUserActions), ct);
            if (!walkResult.IsSuccess)
            {
                if (path.ReachedEnd && walkResult.Error is WalkNotFinishedError walkNotFinishedError

Do not follow this link