diff options
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/life/life.out | 321 | ||||
| -rw-r--r-- | addons/life/life.script | 24 | ||||
| -rw-r--r-- | addons/life/life.zig | 127 |
3 files changed, 472 insertions, 0 deletions
diff --git a/addons/life/life.out b/addons/life/life.out new file mode 100644 index 00000000..07ed2a49 --- /dev/null +++ b/addons/life/life.out @@ -0,0 +1,321 @@ +> seed 7 +> size 40 36 +window 38x16 +tag gen 0 alive 130 +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +|..#..##.#.......##.....#...#.... +> tick 100 +tag gen 0 alive 130 +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +|..#..##.#.......##.....#...#.... +> tick 100 +tag gen 1 alive 170 +|.#.#............##....##..#..... +|#.#......#.###.........#.#...... +|.#........#...###.......##.....# +|........###....#.#.#....##.....# +|...##..........#.#.#.....#...... +|...#.#..........###.....####.... +|...#.....#..#.............#.#... +|....#....###.##.....###......### +|..#.#......#####...#.####..##.## +|###.###......#..#...########.... +|..#.###.......###.........##...# +|.######.....#...#.#.......#...## +|#######..#.###....#......#....## +|...#.#...#....##..##.......#.#.# +|...#..##.#.#######........#.###. +|..####.......#.#####......##.... +> tick 100 +tag gen 2 alive 126 +|.#...........###......######.... +|#.#.......#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +|.....##......#...........##.##.. +> key x x +tag gen 2 alive 126 paused +|.#...........###......######.... +|#.#.......#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +> tick 300 +tag gen 2 alive 126 paused +|.#...........###......######.... +|#.#.......#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +> key lljx +tag gen 2 alive 125 paused +|.#...........###......######.... +|#.........#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +> key k +tag gen 2 alive 125 paused +|.#...........###......######.... +|#.........#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +> body +=.#...........###......######.... +=#.........#####..#....##.##..... +=.#......#...#####......#..#....# +=#........##......#........#..... +=...##....#.....#...#............ +=..##............###.....#..#.... +=...#.....#.###...#...#....#.###. +=....#....#.....#....#..........# +=..#.#......#...#...#.......##... +=#.#...#.........#...#.........#. +=.......#.....##.#....####.....## +=.......#...##.#.#........###.... +=..........######..#.......#..#.. +=.#.....#.#........##......##.... +=......#.#.#.#.............#..##. +=.....##......#...........##.##.. +> dump +dump 536 bytes +tag gen 2 alive 125 paused +|.#...........###......######.... +|#.........#####..#....##.##..... +|.#......#...#####......#..#....# +|#........##......#........#..... +|...##....#.....#...#............ +|..##............###.....#..#.... +|...#.....#.###...#...#....#.###. +|....#....#.....#....#..........# +|..#.#......#...#...#.......##... +|#.#...#.........#...#.........#. +|.......#.....##.#....####.....## +|.......#...##.#.#........###.... +|..........######..#.......#..#.. +|.#.....#.#........##......##.... +|......#.#.#.#.............#..##. +|.....##......#...........##.##.. +> key r +tag gen 0 alive 130 paused +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +> key h l +tag gen 0 alive 130 +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +|..#..##.#.......##.....#...#.... +> tick 100 +tag gen 4 alive 112 +|.#....#....#................#... +|#.#............#......#....#.... +|.#.......##.....#......#.......# +|####....#.#.....#......#........ +|.###....#........#..........#... +|.#........#.....#.#........#..#. +|..#.#.....#.##.#..##.......#..#. +|..#.##.....#....#.#.#.......#.## +|#.#.#.........#.##.#..#......#.# +|..#...#.......#....#.....#...#.. +|.............##.....#......#...# +|.......#.#.#.........#.....#.### +|.......##.#...##...#...#......#. +|......#...#...##.#.#......##..#. +|.....#...#...#............#....# +|.....#.##...#...............#.#. +> tick 100 +tag gen 5 alive 132 +|.#....##...................###.. +|#.#.......#..................... +|...#.....##....##.....##.......# +|...#....#.#.....##.............. +|...#............##.............. +|.#.......#.#....#.##.......###.. +|.##.##....#.#..##.#........##.#. +|#.#.##.....####.#...#.......#... +|#.#.#...........##.##.......##.# +|##.#..........#...###.......#..# +|.............##.....#.....#..#.# +|.......#.##..#.#....#.......##.# +|......###.##..###.#.#.....###... +|......###.#..#.##.#.......##..## +|.....#.###...##...........#..### +|.....#.##...#..............#.#.. +> blur +tag gen 5 alive 132 +|.#....##...................###.. +|#.#.......#..................... +|...#.....##....##.....##.......# +|...#....#.#.....##.............. +|...#............##.............. +|.#.......#.#....#.##.......###.. +|.##.##....#.#..##.#........##.#. +|#.#.##.....####.#...#.......#... +|#.#.#...........##.##.......##.# +|##.#..........#...###.......#..# +|.............##.....#.....#..#.# +|.......#.##..#.#....#.......##.# +|......###.##..###.#.#.....###... +|......###.#..#.##.#.......##..## +|.....#.###...##...........#..### +|.....#.##...#..............#.#.. +> tick 300 +tag gen 5 alive 132 +|.#....##...................###.. +|#.#.......#..................... +|...#.....##....##.....##.......# +|...#....#.#.....##.............. +|...#............##.............. +|.#.......#.#....#.##.......###.. +|.##.##....#.#..##.#........##.#. +|#.#.##.....####.#...#.......#... +|#.#.#...........##.##.......##.# +|##.#..........#...###.......#..# +|.............##.....#.....#..#.# +|.......#.##..#.#....#.......##.# +|......###.##..###.#.#.....###... +|......###.#..#.##.#.......##..## +|.....#.###...##...........#..### +|.....#.##...#..............#.#.. +> focus +tag gen 5 alive 132 +|.#....##...................###.. +|#.#.......#..................... +|...#.....##....##.....##.......# +|...#....#.#.....##.............. +|...#............##.............. +|.#.......#.#....#.##.......###.. +|.##.##....#.#..##.#........##.#. +|#.#.##.....####.#...#.......#... +|#.#.#...........##.##.......##.# +|##.#..........#...###.......#..# +|.............##.....#.....#..#.# +|.......#.##..#.#....#.......##.# +|......###.##..###.#.#.....###... +|......###.#..#.##.#.......##..## +|.....#.###...##...........#..### +|.....#.##...#..............#.#.. +> tick 200 +tag gen 6 alive 123 +|.#....###..................#.#.. +|###......##.................#... +|..##......##...###.............. +|..###.....#..................... +|..#......##....#............#... +|.#.##.....##......##.......#.#.. +|#.#.##....#...#.#.#............. +|#.#........####...#.#.........#. +|..#.##......#.#.##...#.....####. +|.###.........###.##..#.....##... +|.............#.#....##.....#.#.# +|......##.#####..#...##....#..#.. +|...........###............#....# +|.....#....####..#..#.....#.....# +|.....#......####..........#..#.# +|.....#...#...#............##.... diff --git a/addons/life/life.script b/addons/life/life.script new file mode 100644 index 00000000..fba9cd62 --- /dev/null +++ b/addons/life/life.script @@ -0,0 +1,24 @@ +# pardes --addon-test life.so addons/life/life.script | diff addons/life/life.out - +seed 7 +size 40 36 +tick 100 +tick 100 +tick 100 +# x, Space (pause), x: the cell under the cursor toggled twice +key x x +tick 300 +key lljx +key k +body +dump +key r +# h, Space (resume; a line's ends are trimmed), l. The tick after a key is +# handed the time of the last tick, from before the paused 300 ms, so the +# next tick 100 catches up four generations. +key h l +tick 100 +tick 100 +blur +tick 300 +focus +tick 200 diff --git a/addons/life/life.zig b/addons/life/life.zig new file mode 100644 index 00000000..5ea9c302 --- /dev/null +++ b/addons/life/life.zig @@ -0,0 +1,127 @@ +//! Conway's Game of Life in a pane: ten generations a second while the +//! pane has the keyboard. Space pauses, r reseeds, hjkl move the cursor, +//! x toggles the cell under it. +const std = @import("std"); +const addon = @import("addon"); + +export const pardes_addon = addon.ops(Life); + +const W = 32; +const H = 16; +const period = std.time.ns_per_s / 10; + +const Life = extern struct { + cells: [H][W]bool, + /// The pane's, kept: r after a restore reseeds as before it. + seed: u64, + gen: u32, + cx: u8, + cy: u8, + paused: bool, + next: addon.Deadline, + + pub const head: addon.Head = .{ .name = "life", .version = 1, .words = &.{.{ .name = "Life", .opens = true, .doc = "a pane of Conway's Game of Life" }} }; + + pub fn open(s: *Life, _: ?[]const u8) void { + s.seed = addon.seed(); + s.reseed(); + } + + pub fn tick(s: *Life, now: u64, flags: addon.Flags) u64 { + if (!flags.focused or s.paused) return s.next.stop(); + // A record's deadline is of another clock's origin. + if (s.next.at > now + period) s.next.at = 0; + if (!s.next.due(now)) return s.next.every(now, period); + s.step(); + // From the step's due time, not now: a tick comes on a frame, late. + s.next.at = if (now - s.next.at < period) s.next.at + period else now + period; + return s.next.at; + } + + pub fn key(s: *Life, k: addon.Key) bool { + switch (k.code) { + addon.char(' ') => s.paused = !s.paused, + addon.char('r') => s.reseed(), + addon.char('h') => s.cx = (s.cx + W - 1) % W, + addon.char('l') => s.cx = (s.cx + 1) % W, + addon.char('k') => s.cy = (s.cy + H - 1) % H, + addon.char('j') => s.cy = (s.cy + 1) % H, + addon.char('x') => s.cells[s.cy][s.cx] = !s.cells[s.cy][s.cx], + else => return false, + } + return true; + } + + pub fn paint(s: *Life, w: addon.Window, p: addon.Paint) void { + p.prefix("gen {d} alive {d}{s}", .{ s.gen, s.alive(), if (s.paused) " paused" else "" }); + for (s.cells, 0..) |row, y| for (row, 0..) |on, x| { + const cursor = w.focused and x == s.cx and y == s.cy; + w.put(x, y, if (on) "#" else ".", .{ .attrs = .{ .reverse = cursor } }); + }; + } + + pub fn body(s: *Life, out: *std.Io.Writer) !void { + for (s.cells) |row| { + for (row) |on| try out.writeByte(if (on) '#' else '.'); + try out.writeByte('\n'); + } + } + + pub const dump = addon.dumpBytes; + pub const load = addon.loadBytes; + + fn reseed(s: *Life) void { + var prng: std.Random.DefaultPrng = .init(s.seed); + const r = prng.random(); + for (&s.cells) |*row| for (row) |*on| { + on.* = r.uintLessThan(u8, 4) == 0; + }; + s.gen = 0; + } + + fn alive(s: *const Life) u32 { + var n: u32 = 0; + for (s.cells) |row| for (row) |on| { + n += @intFromBool(on); + }; + return n; + } + + fn step(s: *Life) void { + var next: [H][W]bool = undefined; + for (0..H) |y| for (0..W) |x| { + var n: u8 = 0; + for ([_]usize{ H - 1, 0, 1 }) |dy| for ([_]usize{ W - 1, 0, 1 }) |dx| { + if (dy == 0 and dx == 0) continue; + n += @intFromBool(s.cells[(y + dy) % H][(x + dx) % W]); + }; + next[y][x] = n == 3 or (n == 2 and s.cells[y][x]); + }; + s.cells = next; + s.gen += 1; + } +}; + +test "a blinker blinks, wrapping at the edges" { + var s = std.mem.zeroes(Life); + for ([_]usize{ W - 1, 0, 1 }) |x| s.cells[0][x] = true; + s.step(); + try std.testing.expectEqual(3, s.alive()); + for ([_]usize{ H - 1, 0, 1 }) |y| try std.testing.expect(s.cells[y][0]); + s.step(); + try std.testing.expect(s.cells[0][W - 1] and s.cells[0][1] and !s.cells[1][0]); + try std.testing.expectEqual(2, s.gen); +} + +test "a record loads back into a fresh pane" { + var a = std.mem.zeroes(Life); + a.seed = 42; + a.reseed(); + a.step(); + var buf: [@sizeOf(Life)]u8 = undefined; + try std.testing.expectEqual(buf.len, Life.dump(&a, &buf)); + var b = std.mem.zeroes(Life); + try std.testing.expect(Life.load(&b, &buf, Life.head.version)); + try std.testing.expectEqualDeep(a.cells, b.cells); + try std.testing.expect(!Life.load(&b, &buf, 2)); +} |
