From cc1c2439854f6bc008f2d22968f40dfd8be8a7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 7 Jan 2023 15:13:56 +0100 Subject: [PATCH] feat: add pet positions to walk command Resolves #33 --- .../Commands/Walking/WalkCommand.cs | 5 ++-- .../Commands/Walking/WalkCommandHandler.cs | 25 ++++--------------- .../WalkManager.cs | 6 ++--- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs b/Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs index a05333d35cc1f22afc0dc2ede8c93d913e46452b..88280186d415f8974075910f8dc153eba2301f11 100644 --- a/Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs +++ b/Core/NosSmooth.Core/Commands/Walking/WalkCommand.cs @@ -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; /// /// The target x coordinate. /// The target y coordinate. -/// The pet indices. +/// The pet walk positions. /// The distance tolerance to the target when to return successful result. /// Whether the command may be cancelled by another task within the same group. /// Whether to wait for finish of the previous task @@ -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, diff --git a/Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs b/Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs index a4981412b64c94f3863631f6df36f223e10586a2..b780d2091ee45f39e21f4c58a13d53c948d45e99 100644 --- a/Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs +++ b/Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs @@ -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 public async Task HandleCommand(WalkCommand command, CancellationToken ct = default) { var tasks = new List>(); - 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 ( new PetWalkCommand ( - command.PetSelectors[i], + pet.PetSelector, (short)x, (short)y, command.ReturnDistanceTolerance, diff --git a/Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs b/Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs index 94581633542cdc341872b69b6510c4743b151819..03d494365f8ff1504a192df38722b457ded18022 100644 --- a/Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs +++ b/Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs @@ -44,9 +44,9 @@ public class WalkManager /// The target y coordinate. /// Whether to allow user actions during the walk operation. /// The cancellation token used for cancelling the operation. - /// The pet selectors to go with. + /// The positions to walk pets to. /// A result that may not succeed. - public async Task GoToAsync(short x, short y, bool allowUserActions = true, CancellationToken ct = default, params int[] petSelectors) + public async Task 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