From b5bb5e8ccf71a5755dde95f04440ee9c6aaa18da Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 14:36:54 -0300 Subject: kinds #28, the Life dogfood's fixes: Deadline.every steps a period from when it was due while that is under a period behind and re-arms a period from now when it is further behind or more than one ahead (a restored record's), which Life's catch-up and guard lines were and snake's 150 ms now keep without drift; the tick after a key or a focus is told the clock's now, not the last stepped frame's, which lags it while the next wake is to come (the core's, Kind.step: Life's resume no longer jumps four generations); ops refuses at compile time a word that opens no pane, there being no ctl, and its doc names the functions it handles; reference.typ says how a script's key, tick and size work and that a pane a dump loads is a new pane with a new seed, as the header now does Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- addons/life/life.out | 372 +++++++++++++++++++++++------------------------ addons/life/life.script | 5 +- addons/life/life.zig | 9 +- addons/snake/snake.out | 22 +-- docs/typ/reference.typ | 11 ++ src/Kind.zig | 5 +- src/kinds/addon.zig | 18 ++- src/kinds/pardes_addon.h | 4 +- 8 files changed, 233 insertions(+), 213 deletions(-) diff --git a/addons/life/life.out b/addons/life/life.out index 07ed2a49..6e5b3f73 100644 --- a/addons/life/life.out +++ b/addons/life/life.out @@ -73,24 +73,164 @@ tag gen 2 alive 126 |......#.#.#.#.............#..##. |.....##......#...........##.##.. > key x x -tag gen 2 alive 126 paused -|.#...........###......######.... -|#.#.......#####..#....##.##..... -|.#......#...#####......#..#....# -|#........##......#........#..... -|...##....#.....#...#............ -|..##............###.....#..#.... -|...#.....#.###...#...#....#.###. -|....#....#.....#....#..........# -|..#.#......#...#...#.......##... -|#.#...#.........#...#.........#. -|.......#.....##.#....####.....## -|.......#...##.#.#........###.... -|..........######..#.......#..#.. -|.#.....#.#........##......##.... -|......#.#.#.#.............#..##. +tag gen 3 alive 131 paused +|.#.........#...#......#.....#... +|..#........#...................# +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. > tick 300 -tag gen 2 alive 126 paused +tag gen 3 alive 131 paused +|.#.........#...#......#.....#... +|..#........#...................# +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. +> key lljx +tag gen 3 alive 130 paused +|.#.........#...#......#.....#... +|...........#...................# +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. +> key k +tag gen 3 alive 130 paused +|.#.........#...#......#.....#... +|...........#...................# +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. +> body +=.#.........#...#......#.....#... +=...........#...................# +=.#.............###....###.##...# +=#.......###..#.................. +=..###....##..................... +=..#.......#.#...###........###.. +=..###.....#.#....##........####. +=....#......#..#.#...#.........#. +=.#...#.........##..##..........# +=.#.#..........#.#...####.....##. +=......##....###.##...######...## +=..........#.....##....######..#. +=........#.#...##.###........#... +=.......###....#...##.....######. +=.....##.##...................##. +=.....###....##.........#....###. +> dump +dump 536 bytes +tag gen 3 alive 130 paused +|.#.........#...#......#.....#... +|...........#...................# +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. +|.....###....##.........#....###. +> key r +tag gen 0 alive 130 paused +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +> key h l +tag gen 0 alive 130 +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +|..#..##.#.......##.....#...#.... +> tick 100 +tag gen 0 alive 130 +|.#.#.......##......#...#..#..... +|#.#......#......#......#.#...... +|.#.......##..##.....#...#...#..# +|.....#..##.....#.#.#......#....# +|#...#..........####....#.#...... +|..#.#.#....#.......#......###... +|........##..#............#.....# +|...#......##........#.......#... +|...#.#.....#.###...#.###....#.## +|##....#...#...#..#..#...##..#..# +|..#..#.........#.........##...#. +|.#..##.......#..#..#....#.#..... +|..#..#...#.##....#.#..........## +|..#...#..###..#...........#..#.# +|....#......##.##..#.#.....#.#.#. +|..#..##.#.......##.....#...#.... +> tick 100 +tag gen 2 alive 126 |.#...........###......######.... |#.#.......#####..#....##.##..... |.#......#...#####......#..#....# @@ -106,10 +246,11 @@ tag gen 2 alive 126 paused |..........######..#.......#..#.. |.#.....#.#........##......##.... |......#.#.#.#.............#..##. -> key lljx -tag gen 2 alive 125 paused +|.....##......#...........##.##.. +> blur +tag gen 2 alive 126 |.#...........###......######.... -|#.........#####..#....##.##..... +|#.#.......#####..#....##.##..... |.#......#...#####......#..#....# |#........##......#........#..... |...##....#.....#...#............ @@ -123,10 +264,11 @@ tag gen 2 alive 125 paused |..........######..#.......#..#.. |.#.....#.#........##......##.... |......#.#.#.#.............#..##. -> key k -tag gen 2 alive 125 paused +|.....##......#...........##.##.. +> tick 300 +tag gen 2 alive 126 |.#...........###......######.... -|#.........#####..#....##.##..... +|#.#.......#####..#....##.##..... |.#......#...#####......#..#....# |#........##......#........#..... |...##....#.....#...#............ @@ -140,28 +282,11 @@ tag gen 2 alive 125 paused |..........######..#.......#..#.. |.#.....#.#........##......##.... |......#.#.#.#.............#..##. -> body -=.#...........###......######.... -=#.........#####..#....##.##..... -=.#......#...#####......#..#....# -=#........##......#........#..... -=...##....#.....#...#............ -=..##............###.....#..#.... -=...#.....#.###...#...#....#.###. -=....#....#.....#....#..........# -=..#.#......#...#...#.......##... -=#.#...#.........#...#.........#. -=.......#.....##.#....####.....## -=.......#...##.#.#........###.... -=..........######..#.......#..#.. -=.#.....#.#........##......##.... -=......#.#.#.#.............#..##. -=.....##......#...........##.##.. -> dump -dump 536 bytes -tag gen 2 alive 125 paused +|.....##......#...........##.##.. +> focus +tag gen 2 alive 126 |.#...........###......######.... -|#.........#####..#....##.##..... +|#.#.......#####..#....##.##..... |.#......#...#####......#..#....# |#........##......#........#..... |...##....#.....#...#............ @@ -176,146 +301,21 @@ 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 -|.#....###..................#.#.. -|###......##.................#... -|..##......##...###.............. -|..###.....#..................... -|..#......##....#............#... -|.#.##.....##......##.......#.#.. -|#.#.##....#...#.#.#............. -|#.#........####...#.#.........#. -|..#.##......#.#.##...#.....####. -|.###.........###.##..#.....##... -|.............#.#....##.....#.#.# -|......##.#####..#...##....#..#.. -|...........###............#....# -|.....#....####..#..#.....#.....# -|.....#......####..........#..#.# -|.....#...#...#............##.... +tag gen 3 alive 131 +|.#.........#...#......#.....#... +|#.#........#.................... +|.#.............###....###.##...# +|#.......###..#.................. +|..###....##..................... +|..#.......#.#...###........###.. +|..###.....#.#....##........####. +|....#......#..#.#...#.........#. +|.#...#.........##..##..........# +|.#.#..........#.#...####.....##. +|......##....###.##...######...## +|..........#.....##....######..#. +|........#.#...##.###........#... +|.......###....#...##.....######. +|.....##.##...................##. +|.....###....##.........#....###. diff --git a/addons/life/life.script b/addons/life/life.script index fba9cd62..a97fcdb8 100644 --- a/addons/life/life.script +++ b/addons/life/life.script @@ -13,8 +13,9 @@ 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. +# told the clock's time (1.6 s), so the game goes on a period from there: +# none at the next frame, two generations in 200 ms (it caught up four +# when that tick was told the last stepped frame's, 1.24 s). key h l tick 100 tick 100 diff --git a/addons/life/life.zig b/addons/life/life.zig index 5ea9c302..2ef26c56 100644 --- a/addons/life/life.zig +++ b/addons/life/life.zig @@ -29,13 +29,8 @@ const Life = extern struct { 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; + if (s.next.due(now)) s.step(); + return s.next.every(now, period); } pub fn key(s: *Life, k: addon.Key) bool { diff --git a/addons/snake/snake.out b/addons/snake/snake.out index e53e72e0..79bc5f19 100644 --- a/addons/snake/snake.out +++ b/addons/snake/snake.out @@ -49,8 +49,8 @@ tag Snake 0 tag Snake 0 |###################### |# # -|# * # -|# oo@ # +|# @ * # +|# oo # |# # |# # |###################### @@ -79,8 +79,8 @@ tag Snake 0 tag Snake 0 |###################### |# # -|# @ * # -|# oo # +|# o@* # +|# o # |# # |# # |###################### @@ -124,8 +124,8 @@ tag Snake 1 tag Snake 1 |###################### |# # -|# oo@ # -|# o # +|# ooo@ # +|# # |# * # |# # |###################### @@ -154,8 +154,8 @@ tag Snake 1 tag Snake 1 |###################### |# # -|# ooo@ # -|# # +|# ooo # +|# @ # |# * # |# # |###################### @@ -310,15 +310,15 @@ tag Snake 1 | | > tick 150 -tag Snake 1 over +tag Snake 1 |###################### |# # |# o # |# o # |# * o # -|# x # +|# @ # |###################### -|game over: any key +| | | | diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index c66de5ad..4763f36c 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -1074,6 +1074,17 @@ and an older one runs as it did (2.1 added at the ends of its records: pane after each, with `window CxR` (the cells it paints) at the start and after `size`: an addon's test, compared with `diff`. `settle` ticks until the kind asks for no step, and fails for one that asks while idle. + `key` sends the rest of its line, its ends trimmed and a space within + sent as one, a character a key; ``, ``, ``, ``, + `` and `` `` `` `` name the others. `tick N` + moves the clock N ms, which the session steps in 16 ms frames: a + deadline is met at the first frame at or after it, and the tick after a + key or a focus is told the clock's time. The kind's pane shares its + column with the session's first, so after `size C R` its window is two + columns narrower than C and about half of R less its tag's rows (a tag + that wraps takes one more). A pane `dump` loads is a new pane, as a + Restore's is, with a new seed: a kind that reseeds alike keeps its seed + in its record. = The pardes command diff --git a/src/Kind.zig b/src/Kind.zig index 66e37852..9785a1a3 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -626,7 +626,10 @@ pub fn step(p: *Pardes) void { // counted by them. const flags = (if (focused) c.PA_FOCUSED else 0) | (if (slot.painted and caps(p, slot).v22) c.PA_FRAMED else 0); slot.painted = false; - if (slot.ops) |o| cl.ret = @bitCast(o.tick.?(slot.st, now, flags)); + // The clock's now for a tick after an event (a key, a focus): the + // stepped frame lags it while the next wake is still to come. + const at = if (slot.next_tick == 1) @max(now, p.clock.now_ns) else now; + if (slot.ops) |o| cl.ret = @bitCast(o.tick.?(slot.st, at, flags)); slot.next_tick = @bitCast(cl.end(prev).ret); p.needs_frame = true; repaint(p, slot, id); diff --git a/src/kinds/addon.zig b/src/kinds/addon.zig index afb59337..41b431ea 100644 --- a/src/kinds/addon.zig +++ b/src/kinds/addon.zig @@ -1,12 +1,16 @@ //! A kind of pane in Zig: what an addon imports beside the C ABI it is //! built to (pardes_addon.h, translated: `c`), which stays the contract. //! It imports nothing of the core. A kind is a struct whose `head` names -//! it and whose functions are whichever of pa_ops's it has, each over a -//! `*T` that `open` is handed zeroed (its defaults set) in the pane's arena: +//! it and whose functions are any of `open`, `paint` (required), `tick`, +//! `key`, `body`, `ctlRead`, `jobRun`, `jobDone`, `dump` and `load`, each +//! over a `*T` that `open` is handed zeroed (its defaults set) in the +//! pane's arena; pa_ops's others (mouse, ctl, address, where, search, +//! close) no kind here has needed yet: //! //! export const pardes_addon = addon.ops(Snake); //! -//! addons/snake/snake.zig and addons/image/image.zig are the examples. +//! addons/snake/snake.zig, addons/life/life.zig and addons/image/image.zig +//! are the examples. const std = @import("std"); pub const c = @import("pardes_addon"); const Allocator = std.mem.Allocator; @@ -119,9 +123,11 @@ pub const Deadline = extern struct { pub fn due(d: Deadline, now: u64) bool { return d.at != 0 and now >= d.at; } - /// Due `period` after now, unless it is still ahead: its time. + /// Due a period after it was (a tick comes on a frame, late), or a + /// period from now when that is more than a period behind or ahead (a + /// record's, of another clock); kept while ahead: its time. pub fn every(d: *Deadline, now: u64, period: u64) u64 { - if (d.at == 0 or now >= d.at) d.at = now + period; + if (d.at == 0 or now -| d.at >= period or d.at > now + period) d.at = now + period else if (now >= d.at) d.at += period; return d.at; } /// None: tick is next called when something happens to the pane. @@ -219,6 +225,8 @@ pub fn ops(comptime T: type) c.pa_ops { inline for (fns) |m| if (@hasDecl(T, m[0])) { @field(o, m[1]) = @field(F, m[1]); }; + // A word for its pane goes to `ctl`, which this file does not make. + for (T.head.words) |w| if (!w.opens) @compileError(@typeName(T) ++ "'s word " ++ w.name ++ " opens no pane, and addon.zig makes no ctl to take it"); // A public function of another name (a typo) would be left out unsaid. const names = [_][]const u8{ "open", "paint", "tick", "key", "body", "ctlRead", "jobRun", "jobDone", "dump", "load" }; inline for (@typeInfo(T).@"struct".decl_names) |d| if (comptime @typeInfo(@TypeOf(@field(T, d))) == .@"fn" and for (names) |n| { diff --git a/src/kinds/pardes_addon.h b/src/kinds/pardes_addon.h index 0870aa6f..a2b57155 100644 --- a/src/kinds/pardes_addon.h +++ b/src/kinds/pardes_addon.h @@ -196,7 +196,9 @@ typedef struct pa_api { /* A new text pane named `name` holding `bytes`; its serial, 0 if none. */ uint32_t (*text)(const char *name, uint32_t name_len, const char *bytes, uint32_t n); /* The pane's seed: the same pane, the same number, on every machine (a - * game's board), and what `pardes --addon-test`'s `seed N` sets. */ + * game's board), and what `pardes --addon-test`'s `seed N` sets. A pane + * restored from a dump is a new one: a kind keeps its seed in its record + * to reseed alike. */ uint64_t (*seed)(void); /* 2.1 (api->size >= 96): memory of pardes's that outlives a call, as * realloc's (n 0 frees it), on any thread at any time: what a pane's -- cgit v1.3