Skip to content
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
247 changes: 213 additions & 34 deletions src/nav/controller.zig

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions tests/nav/controller_v4_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,56 @@ test "tick fires onPathInvalidated when the goal dies and no reroute exists" {
try std.testing.expect(!pf.isNavigating(9));
}

test "a one-waypoint route walks the rest of a segment and arrives (#60)" {
const Pf = PathfinderWith(u64, struct {});
var pf = Pf.init(std.testing.allocator, test_config);
defer pf.deinit();

const a = try pf.addNode(.{ .x = 100, .y = 100 }, true);
_ = try pf.addNode(.{ .x = 100, .y = 200 }, true);

var ctx = CallbackCtx.init(std.testing.allocator);
defer ctx.deinit();
// 40 px short of `a`, as a re-issue finds a walker on its last leg.
try ctx.positions.put(7, .{ .x = 100, .y = 140 });

_ = try pf.navigate(7, a, a, 60.0);
var i: usize = 0;
while (i < 120 and pf.isNavigating(7)) : (i += 1) {
const before = ctx.positions.get(7).?;
pf.tick(&ctx, 0.016);
// Walks, never jumps: no step beyond the engine's 2 px waypoint
// snap (speed·dt is ~1 px here; the jump was the whole 40 px).
try std.testing.expect(before.y - ctx.positions.get(7).?.y <= 2.0 + 0.001);
}

try std.testing.expectEqual(@as(usize, 1), ctx.arrived.items.len);
try std.testing.expectEqual(a, ctx.arrived.items[0].goal_node);
try std.testing.expectEqual(@as(f32, 100), ctx.positions.get(7).?.y);
}

test "a one-waypoint route is invalidated when its node goes away (#60)" {
const Pf = PathfinderWith(u64, struct {});
var pf = Pf.init(std.testing.allocator, test_config);
defer pf.deinit();

const a = try pf.addNode(.{ .x = 100, .y = 100 }, true);
_ = try pf.addNode(.{ .x = 900, .y = 900 }, false);

var ctx = CallbackCtx.init(std.testing.allocator);
defer ctx.deinit();
try ctx.positions.put(9, .{ .x = 100, .y = 140 });

_ = try pf.navigate(9, a, a, 10.0);
pf.tick(&ctx, 0.016);
pf.removeNode(a);
pf.tick(&ctx, 0.016);

try std.testing.expectEqual(@as(usize, 0), ctx.arrived.items.len);
try std.testing.expectEqual(@as(usize, 1), ctx.invalidated.items.len);
try std.testing.expect(!pf.isNavigating(9));
}

test "plain ctx without settle callbacks still compiles and ticks" {
const Pf = PathfinderWith(u64, struct {});
var pf = Pf.init(std.testing.allocator, test_config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Static library for linking
const lib = b.addLibrary(.{
.linkage = .static,
.name = "zig_utils",
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
}),
});
b.installArtifact(lib);

// zspec dependency
const zspec = b.dependency("zspec", .{
.target = target,
.optimize = optimize,
});

// zig_utils module for tests
const zig_utils_module = b.addModule("zig_utils", .{
.root_source_file = b.path("src/root.zig"),
});

// Tests with zspec runner
const lib_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("tests/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zspec", .module = zspec.module("zspec") },
.{ .name = "zig_utils", .module = zig_utils_module },
},
}),
.test_runner = .{ .path = zspec.path("src/runner.zig"), .mode = .simple },
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.{
.name = .zig_utils,
.version = "0.7.3",
.fingerprint = 0x6dc482caf73c4a75,
.minimum_zig_version = "0.16.0",

.dependencies = .{
.zspec = .{
.url = "https://github.com/apotema/zspec/archive/v0.9.2.tar.gz",
.hash = "zspec-0.9.1-jaKLbXgMBACFwbNjflhbMyP113leoYjboxn-1UOP-FGw",
},
},

.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
Loading
Loading