Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement walk_to() #2204

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DMCompiler/DMStandard/_Standard.dm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ proc/viewers(Depth, Center = usr) as /list
proc/walk(Ref, Dir, Lag = 0, Speed = 0)
proc/walk_rand(Ref,Lag = 0,Speed = 0)
proc/walk_to(Ref, Trg, Min = 0, Lag = 0, Speed = 0)
set opendream_unimplemented = 1
proc/walk_towards(Ref,Trg,Lag=0,Speed=0)
proc/winclone(player, window_name, clone_name)
proc/winexists(player, control_id) as text
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_rand);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_towards);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_to);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winclone);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winexists);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winget);
Expand Down
23 changes: 23 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,29 @@ public static DreamValue NativeProc_walk_towards(NativeProc.Bundle bundle, Dream
return DreamValue.Null;
}

[DreamProc("walk_to")]
[DreamProcParameter("Ref", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("Trg", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("Min", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Lag", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Speed", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
public static DreamValue NativeProc_walk_to(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (!bundle.GetArgument(0, "Ref").TryGetValueAsDreamObject<DreamObjectMovable>(out var refAtom))
return DreamValue.Null;

if (!bundle.GetArgument(1, "Trg").TryGetValueAsDreamObject<DreamObjectAtom>(out var trgAtom)) {
bundle.WalkManager.StopWalks(refAtom);
return DreamValue.Null;
}

bundle.GetArgument(2, "Min").TryGetValueAsInteger(out var min);
bundle.GetArgument(3, "Lag").TryGetValueAsInteger(out var lag);
bundle.GetArgument(4, "Speed").TryGetValueAsInteger(out var speed);

bundle.WalkManager.StartWalkTo(refAtom, trgAtom, min, lag, speed);
return DreamValue.Null;
}

[DreamProc("winclone")]
[DreamProcParameter("player", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("window_name", Type = DreamValueTypeFlag.String)]
Expand Down
36 changes: 36 additions & 0 deletions OpenDreamRuntime/WalkManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;

Check warning

Code scanning / InspectCode

Redundant using directive Warning

Using directive is not required by the code and can be safely removed
using System.Threading;
using OpenDreamRuntime.Map;
using OpenDreamRuntime.Objects.Types;
Expand Down Expand Up @@ -111,4 +112,39 @@
return DreamValue.Null;
});
}

/// <summary>
/// Walk towards the target with pathfinding taken into account
/// </summary>
public void StartWalkTo(DreamObjectMovable movable, DreamObjectAtom target, int min, int lag, int speed) { // TODO: Implement speed. Speed=0 uses Ref.step_size
StopWalks(movable);

lag = Math.Max(lag, 1); // Minimum of 1 tick lag

CancellationTokenSource cancelSource = new();
_walkTasks[movable] = cancelSource;

DreamThread.Run($"walk_to {movable}", async state => {
var moveProc = movable.GetProc("Move");

while (true) {
await _scheduler.CreateDelayTicks(lag);
if (cancelSource.IsCancellationRequested)
break;

var currentLoc = _atomManager.GetAtomPosition(movable);
var targetLoc = _atomManager.GetAtomPosition(target);
var steps = _dreamMapManager.CalculateSteps(currentLoc, targetLoc, min);
using var enumerator = steps.GetEnumerator();
if (!enumerator.MoveNext()) // No more steps to take
break;

var dir = enumerator.Current;
var newLoc = DreamProcNativeHelpers.GetStep(_atomManager, _dreamMapManager, movable, dir);
await state.Call(moveProc, movable, null, new(newLoc), new((int)dir));
}

return DreamValue.Null;
});
}
}
Loading