plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
bff4a1d
— Peter John
2026-11-16T12:23:13+05:30
one replay form, no version (user: no backwards compatibility): the first line is `plum-replay <seed high> <seed low> <width> <height>`, with no version number, and a tick line is what is held and the tick's events, the wheel among them; the version only told the old form from this one, so it went with the old form; removed: reading a `plum-replay 1` (its wheel column after the pointer, read as a `Wheel` event) in std/game/Replay (`Replay.Version`, the `v1` flag of `decodeTicks`/`parseLine`), the native host's `replay_header` and `recording`, the bridge's `replayHeader` and its recorder; an old file is refused as not a replay; tests: the version 1 tests of tests/port/game/replay.plum gone (round trip, bad lines) and the bad lines of the one form kept, a header with a version refused, tests/std/game/Input.plum's replay window, host_test's bad replays, the CLI's kept recording and folder run on the new header, web-test's recording check; docs: game.md (record and replay, the "Beside master" row); backlog: why std's delivery fold has no unique version, and what would give it one (the screen enters the fold shared, held by `Game.screens` and `Game.initial`, and the fresh screen the rules build reaches the next `reduce` only as the `state` of `act`'s `Next`, which other arms give as the shared screen itself: no static proof, only a check at run time in `actIn` or one copy of the screen as it enters the fold)
- compiler/cli/src/replay.rs +3 -3
- compiler/cli/tests/cli/run_folder_test.rs +1 -1
- compiler/game/src/host.rs +4 -5
- compiler/game/tests/host_test.rs +3 -3
- compiler/game/web-test/src/lib.rs +1 -1
- compiler/game/web/plum_bridge.js +3 -3
- docs/backlog.md +1 -1
- docs/game.md +2 -2
- std/game/Replay.plum +21 -32
- tests/port/game/replay.plum +12 -35
- tests/std/game/Input.plum +1 -1
compiler/cli/src/replay.rs
CHANGED
|
@@ -140,10 +140,10 @@ mod tests {
|
|
|
140
140
|
/// named by game and seed, with the seed line in the message.
|
|
141
141
|
#[test]
|
|
142
142
|
fn aFailedReplayCheckKeepsItsRecording() {
|
|
143
|
-
let text = "plum-replay
|
|
143
|
+
let text = "plum-replay 7 9 800 600\n- 0 0 0\n";
|
|
144
144
|
let said = keepRecording("kids/test_only.plum", text);
|
|
145
|
-
let file = plum_core::loader::builtRepoRoot().join("target/replay-failures/kids_test_only_plum-plum-
|
|
145
|
+
let file = plum_core::loader::builtRepoRoot().join("target/replay-failures/kids_test_only_plum-plum-replay_7_9_800_600.txt");
|
|
146
|
-
assert!(said.contains("plum-replay
|
|
146
|
+
assert!(said.contains("plum-replay 7 9 800 600") && said.contains(&file.display().to_string()), "{said}");
|
|
147
147
|
assert_eq!(std::fs::read_to_string(&file).unwrap(), text);
|
|
148
148
|
std::fs::remove_file(&file).unwrap();
|
|
149
149
|
assert_eq!(firstDifference(&["a".into(), "b".into()], &["a".into(), "c".into()]), "call 1: `b` vs `c`");
|
compiler/cli/tests/cli/run_folder_test.rs
CHANGED
|
@@ -42,7 +42,7 @@ fn runFolderWithNoMainOrTwoFails() {
|
|
|
42
42
|
fn runGameFolderIsItsMainFile() {
|
|
43
43
|
let dir = Scratch::new("run_folder_game");
|
|
44
44
|
let replay = dir.join("replay.txt");
|
|
45
|
-
std::fs::write(&replay, format!("plum-replay
|
|
45
|
+
std::fs::write(&replay, format!("plum-replay 7 9 800 600\n{}", "- 0 0 0\n".repeat(30))).unwrap();
|
|
46
46
|
let byFile = run(&["run", "examples/games/orbitrunner/OrbitRunner.plum", "--replay", replay.to_str().unwrap()]);
|
|
47
47
|
let byFolder = run(&["run", "examples/games/orbitrunner", "--replay", replay.to_str().unwrap()]);
|
|
48
48
|
assert!(byFile.0, "{}", byFile.2);
|
compiler/game/src/host.rs
CHANGED
|
@@ -41,18 +41,17 @@ const REPLAYING: i32 = 2;
|
|
|
41
41
|
const DEV: i32 = 3;
|
|
42
42
|
|
|
43
43
|
/// The seed halves and screen size from a replay's first line,
|
|
44
|
-
/// `plum-replay
|
|
44
|
+
/// `plum-replay <hi> <lo> <width> <height>`.
|
|
45
|
-
/// std/game/Replay reads too).
|
|
46
45
|
pub fn replay_header(text: &str) -> Result<([i32; 2], (i64, i64))> {
|
|
47
46
|
let first = text.lines().next().unwrap_or("");
|
|
48
47
|
let f: Vec<&str> = first.split_whitespace().collect();
|
|
49
48
|
let bad = || wasmtime::Error::msg(format!("not a plum replay: the first line is `{first}`"));
|
|
50
|
-
if f.len() !=
|
|
49
|
+
if f.len() != 5 || f[0] != "plum-replay" {
|
|
51
50
|
return Err(bad());
|
|
52
51
|
}
|
|
53
52
|
let n = |i: usize| f[i].parse::<i64>().map_err(|_| bad());
|
|
54
53
|
// The low half is written as the unsigned 32-bit value: keep its bits.
|
|
55
|
-
Ok(([n(
|
|
54
|
+
Ok(([n(1)? as i32, n(2)? as i32], (n(3)?, n(4)?)))
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
/// The `n`th `rawRandomHalf` (from 0, n >= 2) of a recorded or replayed run whose first two
|
|
@@ -332,7 +331,7 @@ impl<B: Backend> PlumGame<B> {
|
|
|
332
331
|
let rp = &self.store.data().replay;
|
|
333
332
|
let half = |i: usize| rp.halves.get(i).copied().unwrap_or(0);
|
|
334
333
|
// The low half as the unsigned value, as std/game/Replay's `header` writes it.
|
|
335
|
-
format!("plum-replay
|
|
334
|
+
format!("plum-replay {} {} {} {}\n{}", half(0), half(1) as u32, rp.screen.0, rp.screen.1, rp.text)
|
|
336
335
|
}
|
|
337
336
|
|
|
338
337
|
/// Replays `text` (a recording) from `main` on: call before `init`. `main` gets the
|
compiler/game/tests/host_test.rs
CHANGED
|
@@ -418,7 +418,7 @@ fn aRecordedRunReplaysWithItsSeedAndInput() {
|
|
|
418
418
|
g.draw().unwrap();
|
|
419
419
|
let recorded = lastText(&g);
|
|
420
420
|
let text = g.recording();
|
|
421
|
-
assert!(text.starts_with("plum-replay
|
|
421
|
+
assert!(text.starts_with("plum-replay ") && text.contains(" 1024 700\n"), "{text}");
|
|
422
422
|
assert_eq!(plum_game::host::replay_ticks(&text), 30);
|
|
423
423
|
assert!(recorded.contains("ticks 30 keys 20 "), "{recorded}");
|
|
424
424
|
|
|
@@ -494,8 +494,8 @@ fn everySeedOfARecordedRunReplays() {
|
|
|
494
494
|
fn aBadReplayIsRefused() {
|
|
495
495
|
let mut g = PlumGame::load(&seedGame("bad"), Recorder::new()).unwrap();
|
|
496
496
|
assert!(g.replay("hello\n").is_err());
|
|
497
|
-
assert!(g.replay("plum-replay 1
|
|
497
|
+
assert!(g.replay("plum-replay 1 2 3\n").is_err());
|
|
498
|
-
g.replay("plum-replay 1
|
|
498
|
+
g.replay("plum-replay 1 2 3 4\nnot a tick\n").unwrap();
|
|
499
499
|
g.init().unwrap();
|
|
500
500
|
let err = g.update(1.0 / 60.0).unwrap_err();
|
|
501
501
|
assert!(format!("{err:?}").contains("the replay can't be read: line 2"), "{err:?}");
|
compiler/game/web-test/src/lib.rs
CHANGED
|
@@ -987,7 +987,7 @@ async fn record_then_replay(browser: &playwright_rs::protocol::Browser, port: u1
|
|
|
987
987
|
if !errs.is_empty() {
|
|
988
988
|
return Err(format!("recording page: {errs:?}"));
|
|
989
989
|
}
|
|
990
|
-
if !text.starts_with("plum-replay
|
|
990
|
+
if !text.starts_with("plum-replay ") {
|
|
991
991
|
return Err(format!("plumReplay() gave no recording: {:?}", text.chars().take(80).collect::<String>()));
|
|
992
992
|
}
|
|
993
993
|
let ticks = text.lines().skip(1).filter(|l| !l.trim().is_empty()).count();
|
compiler/game/web/plum_bridge.js
CHANGED
|
@@ -1154,8 +1154,8 @@ fn shade(i: VOut) -> vec4f {
|
|
|
1154
1154
|
// The first line of a replay: its seed halves and screen size, or an error.
|
|
1155
1155
|
function replayHeader(text) {
|
|
1156
1156
|
const f = (text.split("\n", 1)[0] || "").trim().split(/\s+/);
|
|
1157
|
-
if (f.length !==
|
|
1157
|
+
if (f.length !== 5 || f[0] !== "plum-replay") throw new Error("not a plum replay");
|
|
1158
|
-
return { halves: [Number(f[
|
|
1158
|
+
return { halves: [Number(f[1]) | 0, Number(f[2]) | 0], size: [Number(f[3]) || 0, Number(f[4]) || 0] };
|
|
1159
1159
|
}
|
|
1160
1160
|
|
|
1161
1161
|
async function makeReplay(view) {
|
|
@@ -1195,7 +1195,7 @@ fn shade(i: VOut) -> vec4f {
|
|
|
1195
1195
|
const lines = [];
|
|
1196
1196
|
const size = [Math.round(view.w), Math.round(view.h)];
|
|
1197
1197
|
// The low half as the unsigned value, as std/game/Replay's `header` writes it.
|
|
1198
|
-
const recording = () => "plum-replay
|
|
1198
|
+
const recording = () => "plum-replay " + (halves[0] | 0) + " " + ((halves[1] | 0) >>> 0) + " " + size[0] + " " + size[1] + "\n" + lines.join("");
|
|
1199
1199
|
globalThis.plumReplay = recording;
|
|
1200
1200
|
const save = Object.assign(document.createElement("button"), {
|
|
1201
1201
|
textContent: "Save replay",
|
docs/backlog.md
CHANGED
|
@@ -8,7 +8,7 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
8
8
|
|
|
9
9
|
- **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
|
|
10
10
|
- **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); dead-code removal is reverted when it removes every type a downcast to a trait could match (it met `Container as c`, which master has since folded into `view` too).
|
|
11
|
-
- **No unique version of std's generic delivery fold**: Orbit Runner's `reduce$unique` and `newMine$unique` went when its rules became `then` actions (2026-11-16).
|
|
11
|
+
- **No unique version of std's generic delivery fold**: Orbit Runner's `reduce$unique` (3) and `newMine$unique` (1) went when its rules became `then` actions (2026-11-16). No static proof can bring them back as the code stands: the screen enters the fold shared (`Game.screens` and `Game.initial` hold it), and the fresh screen the rules build reaches the next `reduce` only as the `state` of the `Next` that `s.act(a)` returned in `Delivery.actIn`, where it meets the arms that return the shared screen as it is (`PlayAgain => self`, the default `reduce`, `followed`'s `Next(state: self, ..)`). So `next` in `actIn` is fresh or the shared `s` depending on the action, and a version of `actIn` has no unique caller to take. (Before, `collected.dispatch(Scored)` called `reduce` on a fresh local, on a receiver of known type.) Freshness through a returned field, `actIn`/`actAll` versioned as a pair and an `act$unique` for every implementer (the earlier plan) are needed but give nothing alone. What would: a check at run time in the shared `actIn` (`next` is not `s`, so it was built in `act`: hand it to the unique versions), or one copy of the screen when it enters the fold (a builtin, since `S` is generic; it makes every step of the tick unique). Either is a design decision; the cost today is the spreads of the rare `Scored`, `Hit` and `StartOver` ticks.
|
|
12
12
|
- **A program with no `Sized` or `Touch` view drops the dead code pass**: std's `Views.touchOf`, `sizedHit` and `missesArea` match `Touch as t` and `Sized as s` on a `View`, and once the pass has dropped every type that is one, the re-check calls each match impossible ("no type is both a View and a Touch"). Every game has such a view, so only a minimal test program meets it. A trait default's copy is told apart since 2026-11-16 (`defaultPositions` reads the trait's declarations), but these are statics.
|
|
13
13
|
- **Master's other games** (`clockwork`, `menu`) are not ported.
|
|
14
14
|
- **The view's cost**: each frame makes the view's list and items. `benchmarks/screen_views` (`orbit.plum`, `scale.plum`, master's drivers) times it; not yet run against master. Master's stepping walk makes the view once more a tick and each actor's view; this branch's `stepped` makes neither, so the two `update` timings are not the same work (`scale.plum`'s header says so).
|
docs/game.md
CHANGED
|
@@ -230,7 +230,7 @@ A run of a game is its seed and one `Input` per tick, so a recording is that and
|
|
|
230
230
|
|
|
231
231
|
While recording, the host remembers what `io.randomSeed()` returned in `main`, and the game's `Ticker` hands it the inputs of each frame's ticks. While replaying, `io.randomSeed()` returns the recorded seed and the screen size is the recorded one: each replayed tick's `Input.screen` is the header's. Only the first `io.randomSeed()` call is recorded; while recording or replaying, any later call returns a seed derived from that one, the same in the browser and natively, so it replays too. The screen size is the one at the start of the recording: resizing the window while recording isn't recorded.
|
|
232
232
|
|
|
233
|
-
The text is one line for the seed and screen size (`plum-replay
|
|
233
|
+
The text is one line for the seed and screen size (`plum-replay <seed high> <seed low> <width> <height>`), then one line per tick: what is held after the tick (the keys, the mouse buttons and the pointer) and the tick's events, a turn of the wheel among them (`W<dy>`). What is held is a fold of the events (`Held.after`), and each line keeps it all the same, so a replay doesn't depend on the fold. Floats are written exactly. There is one form, with no version number: a file in an older form is not read. The details are at the top of `std/game/Replay.plum`.
|
|
234
234
|
|
|
235
235
|
## Undo and time travel
|
|
236
236
|
|
|
@@ -469,7 +469,7 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
469
469
|
| `onKey(..) -> A?` and the other mappings | `-> Option<A>` | no `T?` in this dialect |
|
|
470
470
|
| `deliver(&self, d: Delivery)`, the delivery noting `acted` | `deliver(self, d: Delivery) -> Delivered` (the next screen, the tick's commands, the actions stepped, whether it was a user action), a fold with no queue | the delivery returns what it made |
|
|
471
471
|
| `Effects`, one queue of `Queued<A>` holding each action with its slice, set in one slot around a delivery, and `effect(a, game)` run for each after the tick | no `Effects` and no slot: a step returns its commands (`Next.cmds`), and the game does them after the tick | nothing in a tick is written in place or kept in a global, and a rewind skips the sounds by construction |
|
|
472
|
-
| `Input(held, events, pos, wheel)`, read through `isKeyDown`, `isKeyPressed`, `axis`, `isMouseDown`, `isMousePressed`, `isMouseReleased`; a slice's `held(self, input: Input)` | `Input(events, held, screen)`: the events, with the wheel among them (`Wheel(dy)`), and what is held after them (`Held(mouse, keys, buttons, pointer)`, `held.afterAll(events)`), read through `held.isKeyDown`, `axis`, `isButtonDown` and `pointer`; `held(self, h: Held)`; no `isKeyPressed`, `isMousePressed`, `isMouseReleased`, `pos` or `wheel`; a replay
|
|
472
|
+
| `Input(held, events, pos, wheel)`, read through `isKeyDown`, `isKeyPressed`, `axis`, `isMouseDown`, `isMousePressed`, `isMouseReleased`; a slice's `held(self, input: Input)` | `Input(events, held, screen)`: the events, with the wheel among them (`Wheel(dy)`), and what is held after them (`Held(mouse, keys, buttons, pointer)`, `held.afterAll(events)`), read through `held.isKeyDown`, `axis`, `isButtonDown` and `pointer`; `held(self, h: Held)`; no `isKeyPressed`, `isMousePressed`, `isMouseReleased`, `pos` or `wheel`; a replay's tick line has no wheel column, and there is one replay form, with no version number | the events are the one source of the input: what is held is their fold, and a press is its event, not a second reading of what is held |
|
|
473
473
|
| a press reaches `onPress(target, at)`, which tells the item by its identity or kind | the same, and by default `onPress` is what the item says a press on it means: `Presses<A>`'s `pressed()`, with `Button<A>(x, y, w, h, on: a) { .. }` ready-made; Balloon Pop's balloons are `Presses<PopAction>` (`Pop(id: self.id)`) | an item made in `view` is a new object each walk, so it says what it means rather than being compared with a field |
|
|
474
474
|
| `Viewport` updated in place (`v.update(w, h)`), `lookAt(x, y, zoom = 1.0)` | `Viewport.of(..).update(w, h) -> Viewport`, the same `lookAt` and `moved` | a value |
|
|
475
475
|
| the frame sets the host's camera (`setCamera`) for a camera with one scale, and puts a `Stretch` camera into the transform | every camera goes into the transform the view is drawn through, in window pixels, and culling maps its area through it | this branch's draw has always gone through the transform; the host sees no `setCamera`/`resetCamera` in a frame, so the recorder's calls differ (`compiler/game/tests/host_test.rs` checks the pixels) |
|
std/game/Replay.plum
CHANGED
|
@@ -25,7 +25,7 @@ import std/game/Input
|
|
|
25
25
|
#
|
|
26
26
|
# The text form, one line each:
|
|
27
27
|
#
|
|
28
|
-
# plum-replay
|
|
28
|
+
# plum-replay <seed, high 32 bits> <seed, low 32 bits> <screen width> <screen height>
|
|
29
29
|
# <held keys, comma separated, or -> <mouse buttons held> <pointer x> <pointer y> <events>
|
|
30
30
|
#
|
|
31
31
|
# with one tick line per tick, its events and what is held after them (`Input.held`:
|
|
@@ -37,12 +37,8 @@ import std/game/Input
|
|
|
37
37
|
# (ButtonUp) or `W<dy>` (Wheel). Floats are written exactly, as `m` or `m:e` for the
|
|
38
38
|
# integer m times 2^e, or as `-0`, `inf`, `-inf` or `nan` (`toStr` rounds to six digits).
|
|
39
39
|
# The screen size is the host's at the start of the recording, and the replaying host
|
|
40
|
-
# reports it to the game; 0 means not recorded.
|
|
40
|
+
# reports it to the game; 0 means not recorded. There is one form and no version: a
|
|
41
|
-
#
|
|
42
|
-
#
|
|
41
|
+
# file written in an older form is not read.
|
|
43
|
-
# have the wheel's turn after the pointer, read as a `Wheel` event after the tick's others
|
|
44
|
-
# when it is not 0 (and its mouse buttons held may be only 0 and 1, from before the other
|
|
45
|
-
# buttons).
|
|
46
42
|
|
|
47
43
|
primitive ReplayMode =
|
|
48
44
|
const Off = 0
|
|
@@ -55,9 +51,6 @@ primitive ReplayMode =
|
|
|
55
51
|
# A recorded run: the seed `io.randomSeed()` gave `main`, the screen size at the start,
|
|
56
52
|
# and the input of every tick in order.
|
|
57
53
|
struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
58
|
-
# The version of the text form a recording writes (`plum-replay 2`; a 1 is read too).
|
|
59
|
-
const Version = "2"
|
|
60
|
-
|
|
61
54
|
# Whether the host is recording or replaying this run (a `ReplayMode`, asked once a frame).
|
|
62
55
|
extern fun replayMode(io: Io) -> Int
|
|
63
56
|
# The text of the replay the host is replaying.
|
|
@@ -90,14 +83,14 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
90
83
|
fun decode(text: Str) -> Result<Replay, Str> =
|
|
91
84
|
lines := text.split("\n", 0)
|
|
92
85
|
head := lines.at(0).trim().split(" ", 0)
|
|
93
|
-
if head.length() !=
|
|
86
|
+
if head.length() != 5 || head.at(0) != "plum-replay"
|
|
94
|
-
Err("not a plum replay: the first line must be `plum-replay
|
|
87
|
+
Err("not a plum replay: the first line must be `plum-replay <seed high> <seed low> <width> <height>`")
|
|
95
88
|
else
|
|
96
|
-
hi := Replay.headNumber(head,
|
|
89
|
+
hi := Replay.headNumber(head, 1)?
|
|
97
|
-
lo := Replay.headNumber(head,
|
|
90
|
+
lo := Replay.headNumber(head, 2)?
|
|
98
|
-
width := Replay.headNumber(head,
|
|
91
|
+
width := Replay.headNumber(head, 3)?
|
|
99
|
-
height := Replay.headNumber(head,
|
|
92
|
+
height := Replay.headNumber(head, 4)?
|
|
100
|
-
ticks := Replay.decodeTicks(lines, 1,
|
|
93
|
+
ticks := Replay.decodeTicks(lines, 1, Slice<Input>())?
|
|
101
94
|
Ok(Replay(seed: {hi << 32} | {lo & 0xFFFFFFFF}, width: width, height: height, ticks: ticks).onScreen(width, height))
|
|
102
95
|
|
|
103
96
|
# The replay on a window `w` by `h` pixels, each tick's input in it (`Input.screen`): a
|
|
@@ -112,23 +105,23 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
112
105
|
fun headNumber(head: Slice<Str>, i: Int) -> Result<Int, Str> =
|
|
113
106
|
Int.parse(head.at(i)).mapErr(|e| "line 1: `{head.at(i)}` is not a whole number")
|
|
114
107
|
|
|
115
|
-
# The tick lines from `lines.at(i)` on, added to `out`
|
|
108
|
+
# The tick lines from `lines.at(i)` on, added to `out`.
|
|
116
|
-
fun decodeTicks(lines: Slice<Str>, i: Int,
|
|
109
|
+
fun decodeTicks(lines: Slice<Str>, i: Int, out: Slice<Input>) -> Result<Slice<Input>, Str> =
|
|
117
110
|
if i >= lines.length()
|
|
118
111
|
Ok(out)
|
|
119
112
|
else
|
|
120
113
|
l := lines.at(i).trim()
|
|
121
114
|
if l.length() == 0
|
|
122
|
-
Replay.decodeTicks(lines, i + 1,
|
|
115
|
+
Replay.decodeTicks(lines, i + 1, out)
|
|
123
116
|
else
|
|
124
|
-
match Replay.parseLine(l
|
|
117
|
+
match Replay.parseLine(l)
|
|
125
118
|
Some(input) =>
|
|
126
119
|
out.push(input)
|
|
127
|
-
Replay.decodeTicks(lines, i + 1,
|
|
120
|
+
Replay.decodeTicks(lines, i + 1, out)
|
|
128
121
|
None => Err("line {i + 1}: `{l}` is not a tick")
|
|
129
122
|
|
|
130
123
|
fun header(seed: Int, width: Int, height: Int) -> Str =
|
|
131
|
-
"plum-replay {
|
|
124
|
+
"plum-replay {seed >> 32} {seed & 0xFFFFFFFF} {width} {height}\n"
|
|
132
125
|
|
|
133
126
|
# One tick's line, ending in a newline.
|
|
134
127
|
fun line(input: Input) -> Str =
|
|
@@ -138,12 +131,10 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
138
131
|
events := input.events.fold("", |acc, e| "{acc} {Replay.eventCode(e)}")
|
|
139
132
|
"{keys} {mask} {Replay.floatCode(h.pointer.x)} {Replay.floatCode(h.pointer.y)}{events}\n"
|
|
140
133
|
|
|
141
|
-
# One tick's line read back (no newline), or None
|
|
134
|
+
# One tick's line read back (no newline), or None.
|
|
142
|
-
# with the wheel's turn after the pointer.
|
|
143
|
-
fun parseLine(l: Str
|
|
135
|
+
fun parseLine(l: Str) -> Option<Input> =
|
|
144
136
|
t := l.split(" ", 0)
|
|
145
|
-
first := if v1 then 5 else 4
|
|
146
|
-
mask := if t.length() <
|
|
137
|
+
mask := if t.length() < 4 then -1 else Int.parse(t.at(1)).ok().unwrapOr(-1)
|
|
147
138
|
if mask < 0 || mask > 7
|
|
148
139
|
None
|
|
149
140
|
else
|
|
@@ -151,11 +142,9 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
151
142
|
held := keys?
|
|
152
143
|
x := Replay.parseFloatCode(t.at(2))?
|
|
153
144
|
y := Replay.parseFloatCode(t.at(3))?
|
|
154
|
-
dy := if v1 then Replay.parseFloatCode(t.at(4)) else Some(0.0)
|
|
155
|
-
wheel := dy?
|
|
156
|
-
events := Replay.parseEvents(t,
|
|
145
|
+
events := Replay.parseEvents(t, 4)?
|
|
157
146
|
buttons := [if {mask & 2} != 0 then Mouse.Right, if {mask & 4} != 0 then Mouse.Middle]
|
|
158
|
-
Some(Input(events: events
|
|
147
|
+
Some(Input(events: events, held: Held(mouse: {mask & 1} != 0, keys: held, buttons: buttons, pointer: Vec2(x: x, y: y))))
|
|
159
148
|
|
|
160
149
|
fun parseKeys(ks: Slice<Str>, i: Int) -> Option<List<Int>> =
|
|
161
150
|
if i >= ks.length()
|
tests/port/game/replay.plum
CHANGED
|
@@ -169,7 +169,7 @@ primitive ReplayTest[UnitTest] =
|
|
|
169
169
|
assert Replay.parseFloatCode("-inf").unwrapOr(0.0) == -inf
|
|
170
170
|
assert Replay.parseFloatCode("nan").unwrapOr(0.0).isNaN()
|
|
171
171
|
odd := Input(events: [PointerMove(x: negZero, y: inf), Wheel(dy: 0.0 / 0.0)], held: Held(pointer: Vec2(x: negZero, y: Float.Min)))
|
|
172
|
-
if Some(back) := Replay.parseLine(Replay.line(odd).trim()
|
|
172
|
+
if Some(back) := Replay.parseLine(Replay.line(odd).trim())
|
|
173
173
|
assert 1.0 / back.held.pointer.x < 0.0
|
|
174
174
|
assert back.held.pointer.y == Float.Min
|
|
175
175
|
assert back.events.any(|e| match e
|
|
@@ -186,51 +186,28 @@ primitive ReplayTest[UnitTest] =
|
|
|
186
186
|
hi := -123456789
|
|
187
187
|
lo := -5
|
|
188
188
|
seed := {hi << 32} | {lo & 0xFFFFFFFF}
|
|
189
|
-
if Ok(r) := Replay.decode("plum-replay
|
|
189
|
+
if Ok(r) := Replay.decode("plum-replay {hi} {lo} 1024 768\n")
|
|
190
190
|
assert r.seed == seed
|
|
191
191
|
assert r.length() == 0
|
|
192
192
|
assert r.width == 1024
|
|
193
193
|
else
|
|
194
194
|
assert false
|
|
195
|
-
assert Replay.decode("plum-replay 2 {hi} {lo} 1024 768\n").unwrap().seed == seed
|
|
196
|
-
assert Replay.header(seed, 1, 2) == "plum-replay
|
|
195
|
+
assert Replay.header(seed, 1, 2) == "plum-replay -123456789 4294967291 1 2\n"
|
|
197
196
|
|
|
198
197
|
fun aBadReplaySaysWhatIsWrong() =
|
|
199
198
|
assert Replay.decode("").isErr()
|
|
200
|
-
assert Replay.decode("plum-replay
|
|
199
|
+
assert Replay.decode("plum-replay 2 0 0 0 0\n").isErr()
|
|
200
|
+
assert Replay.decode("plum-replay 0 0 x 0\n").isErr()
|
|
201
|
-
assert Replay.decode("plum-replay
|
|
201
|
+
assert Replay.decode("plum-replay 0 0 0 0\n- 0 0 0\n- 9 0 0\n").isErr()
|
|
202
|
-
assert Replay.decode("plum-replay
|
|
202
|
+
assert Replay.decode("plum-replay 0 0 0 0\n- 0 0 0 B1,2\n").isErr()
|
|
203
|
-
assert Replay.decode("plum-replay
|
|
203
|
+
assert Replay.decode("plum-replay 0 0 0 0\n- 0 0 0 Wx\n").isErr()
|
|
204
|
+
assert Replay.decode("plum-replay 0 0 0 0\n- 0 0 0 Q1\n").isErr()
|
|
204
|
-
assert Replay.decode("plum-replay
|
|
205
|
+
assert Replay.decode("plum-replay 0 0 0 0\n- 0 0 0 0\n").isErr()
|
|
205
|
-
assert Replay.decode("plum-replay
|
|
206
|
+
assert Replay.decode("plum-replay 0 0 0 0\r\n- 0 0 0 K32 W1\r\n").isOk()
|
|
206
|
-
assert Replay.decode("plum-replay 1 0 0 x 0\n").isErr()
|
|
207
|
-
assert Replay.decode("plum-replay 1 0 0 0 0\n- 0 0 0 0\n- 9 0 0 0\n").isErr()
|
|
208
|
-
assert Replay.decode("plum-replay 1 0 0 0 0\n- 0 0 0 0 B1,2\n").isErr()
|
|
209
|
-
assert Replay.decode("plum-replay 1 0 0 0 0\n- 0 0 0 0 Q1\n").isErr()
|
|
210
|
-
assert Replay.decode("plum-replay 1 0 0 0 0\r\n- 0 0 0 0 K32\r\n").isOk()
|
|
211
|
-
if Err(why) := Replay.decode("plum-replay
|
|
207
|
+
if Err(why) := Replay.decode("plum-replay 0 0 0 0\n- 0 0 0\n- 0 0\n")
|
|
212
208
|
assert why.startsWith("line 3:")
|
|
213
209
|
else
|
|
214
210
|
assert false
|
|
215
|
-
if Err(why) := Replay.decode("plum-replay 2 0 0 0 0\n- 0 0 0\n- 0 0\n")
|
|
216
|
-
assert why.startsWith("line 3:")
|
|
217
|
-
else
|
|
218
|
-
assert false
|
|
219
|
-
|
|
220
|
-
# a `plum-replay 1`, recorded before the other mouse buttons and before the wheel was an
|
|
221
|
-
# event, reads as it did: its wheel column is a `Wheel` after the tick's other events,
|
|
222
|
-
# and it is written again as a `plum-replay 2`
|
|
223
|
-
fun aReplayOfVersionOneReadsAsItDid() =
|
|
224
|
-
old := "plum-replay 1 0 7 800 600\n32,262 1 3 4 0 D3,4 K32\n- 0 1:-1 2 -1 U1,1 k32\n"
|
|
225
|
-
r := Replay.decode(old).unwrap()
|
|
226
|
-
assert r.length() == 2
|
|
227
|
-
assert r.at(0).held.mouse && r.at(0).held.buttons.isEmpty() && r.at(0).held.isKeyDown(Key.Space)
|
|
228
|
-
assert r.at(0).events.length() == 2
|
|
229
|
-
assert !r.at(1).held.mouse && r.at(1).held.pointer.x == 0.5
|
|
230
|
-
assert r.at(1).events.length() == 3 && r.at(1).events.at(2).isWheel()
|
|
231
|
-
now := "plum-replay 2 0 7 800 600\n32,262 1 3 4 D3,4 K32\n- 0 1:-1 2 U1,1 k32 W-1\n"
|
|
232
|
-
assert r.encode() == now
|
|
233
|
-
assert Replay.decode(now).unwrap().encode() == now
|
|
234
211
|
|
|
235
212
|
# std's maths is Plum code made of IEEE 754 double operations and wasm's own `f64.sqrt`,
|
|
236
213
|
# `f64.floor`, ..., which wasm defines exactly, so every browser and the native host compute
|
tests/std/game/Input.plum
CHANGED
|
@@ -43,7 +43,7 @@ primitive InputTest[UnitTest] =
|
|
|
43
43
|
|
|
44
44
|
# a replay's ticks are in the window of its header
|
|
45
45
|
fun aReplaysTicksAreInItsWindow() =
|
|
46
|
-
r := Replay.decode("plum-replay
|
|
46
|
+
r := Replay.decode("plum-replay 0 7 640 480\n- 0 0 0\n65 1 2 3 K65\n").unwrap()
|
|
47
47
|
assert r.width == 640 && r.ticks.length() == 2
|
|
48
48
|
assert r.ticks.all(|t| t.screen == Vec2(x: 640.0, y: 480.0))
|
|
49
49
|
assert r.onScreen(320, 200).ticks.at(1).screen == Vec2(x: 320.0, y: 200.0)
|