aboutsummaryrefslogtreecommitdiff
path: root/src/field.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-12-04 05:24:23 +0100
committerJulien Dessaux2022-12-04 05:24:23 +0100
commit2517e448996842978bc19457065fbc5f6e606595 (patch)
treef982304e608c2bc9a8ff968fcf92149e9a30fe5d /src/field.zig
parentImproved tui debugger (diff)
downloadzigfunge98-2517e448996842978bc19457065fbc5f6e606595.tar.gz
zigfunge98-2517e448996842978bc19457065fbc5f6e606595.tar.bz2
zigfunge98-2517e448996842978bc19457065fbc5f6e606595.zip
Ensure the pointer always ends up on an executable instruction for tui consistency
Diffstat (limited to 'src/field.zig')
-rw-r--r--src/field.zig46
1 files changed, 30 insertions, 16 deletions
diff --git a/src/field.zig b/src/field.zig
index 0130848..02bf544 100644
--- a/src/field.zig
+++ b/src/field.zig
@@ -393,35 +393,49 @@ pub const Field = struct {
try std.testing.expectEqual(f.get(8, 2), '2');
try std.testing.expectEqual(f.get(9, 2), ' ');
}
- pub fn step(f: *Field, x: i64, y: i64, dx: i64, dy: i64) struct { x: i64, y: i64 } {
+ pub fn step(f: *Field, x: i64, y: i64, dx: i64, dy: i64, smartAdvance: bool, jumping: bool) struct { x: i64, y: i64 } {
var a = x + dx;
var b = y + dy;
- if (f.isIn(a, b)) return .{ .x = a, .y = b };
- // # We are stepping outside, we need to wrap the Lahey-space
- a = x;
- b = y;
- while (true) {
- var c = a - dx;
- var d = b - dy;
- if (!f.isIn(c, d)) return .{ .x = a, .y = b };
- a = c;
- b = d;
+ if (!f.isIn(a, b)) {
+ // # We are stepping outside, we need to wrap the Lahey-space
+ a = x;
+ b = y;
+ while (true) {
+ var c = a - dx;
+ var d = b - dy;
+ if (!f.isIn(c, d)) break;
+ a = c;
+ b = d;
+ }
+ }
+ if (smartAdvance) {
+ const v = f.get(a, b);
+ if (jumping) {
+ return f.step(a, b, dx, dy, true, v != ';');
+ }
+ if (v == ' ') {
+ return f.step(a, b, dx, dy, true, false);
+ }
+ if (v == ';') {
+ return f.step(a, b, dx, dy, true, true);
+ }
}
+ return .{ .x = a, .y = b };
}
test "step" {
var minimal = std.io.fixedBufferStream("@");
var f = try Field.init(std.testing.allocator);
defer f.deinit();
try f.load(minimal.reader());
- try std.testing.expectEqual(f.step(0, 0, 0, 0), .{ .x = 0, .y = 0 });
- try std.testing.expectEqual(f.step(0, 0, 1, 0), .{ .x = 0, .y = 0 });
+ try std.testing.expectEqual(f.step(0, 0, 0, 0, false, false), .{ .x = 0, .y = 0 });
+ try std.testing.expectEqual(f.step(0, 0, 1, 0, false, false), .{ .x = 0, .y = 0 });
var hello = std.io.fixedBufferStream("64+\"!dlroW ,olleH\">:#,_@\n");
var fHello = try Field.init(std.testing.allocator);
defer fHello.deinit();
try fHello.load(hello.reader());
- try std.testing.expectEqual(fHello.step(3, 0, 0, 0), .{ .x = 3, .y = 0 });
- try std.testing.expectEqual(fHello.step(3, 0, 1, 0), .{ .x = 4, .y = 0 });
- try std.testing.expectEqual(fHello.step(0, 0, -1, 0), .{ .x = 23, .y = 0 });
+ try std.testing.expectEqual(fHello.step(3, 0, 0, 0, false, false), .{ .x = 3, .y = 0 });
+ try std.testing.expectEqual(fHello.step(3, 0, 1, 0, false, false), .{ .x = 4, .y = 0 });
+ try std.testing.expectEqual(fHello.step(0, 0, -1, 0, false, false), .{ .x = 23, .y = 0 });
}
};