quill/val Public Public: anyone can read this; nobody can change it.

forked from benjivers/val

workbenchthis machine
Files 4990f87c0167ca7cfa9c9a4a67fa43bcfef26454
230 lines · 9.8 KB · ~2.9k tok Raw History
1
/**
2
 * Entity interpolation: a pure function of two samples and a time.
3
 *
4
 * What is checked here is what a browser cannot be asked about — that the
5
 * body walks between the pair at the server's own speed, that a missed
6
 * snapshot is carried exactly one interval and no further, that the turn is
7
 * taken the short way round, and that a time between the pair never draws
8
 * the body anywhere but between them.
9
 */
10
11
import { expect, test } from "bun:test";
12
import { arrive, bodyAt, delay, feed, GONE, LAG, LAG_MAX, LAG_MIN, makeTimeline, sample } from "../static/interp.js";
13
14
type Mark = { t: number; x: number; y: number; angle: number };
15
const pair = (a: Mark, b: Mark) => [a, b];
16
17
const walk = pair({ t: 10, x: 100, y: 200, angle: 0 }, { t: 10.1, x: 120, y: 200, angle: 1 });
18
19
test("between the pair it walks at the speed the server moved it", () => {
20
  expect(sample(walk, 10)).toMatchObject({ x: 100, y: 200 });
21
  expect(sample(walk, 10.05).x).toBeCloseTo(110, 6);
22
  expect(sample(walk, 10.1)).toMatchObject({ x: 120, y: 200 });
23
  // Constant speed: equal steps of time are equal steps of distance, which
24
  // is the whole difference from an exponential chase that never arrives.
25
  const a = sample(walk, 10.02).x;
26
  const b = sample(walk, 10.04).x;
27
  const c = sample(walk, 10.06).x;
28
  expect(b - a).toBeCloseTo(c - b, 6);
29
});
30
31
test("a time inside the pair is never a position outside it", () => {
32
  const diagonal = pair({ t: 1, x: -30, y: 80, angle: 2 }, { t: 1.07, x: 45, y: -10, angle: -2 });
33
  for (let when = 1; when <= 1.07; when += 0.002) {
34
    const at = sample(diagonal, when);
35
    expect(at.x).toBeGreaterThanOrEqual(-30);
36
    expect(at.x).toBeLessThanOrEqual(45);
37
    expect(at.y).toBeGreaterThanOrEqual(-10);
38
    expect(at.y).toBeLessThanOrEqual(80);
39
  }
40
});
41
42
test("before the older sample it holds rather than running backwards", () => {
43
  expect(sample(walk, 9.5)).toMatchObject({ x: 100, y: 200 });
44
  expect(sample(walk, 0)).toMatchObject({ x: 100, y: 200 });
45
});
46
47
test("a missed snapshot is carried one interval and then held", () => {
48
  // Half an interval late: half an interval of the pair's own velocity.
49
  expect(sample(walk, 10.15).x).toBeCloseTo(130, 6);
50
  // A whole one: exactly one more interval of it.
51
  expect(sample(walk, 10.2).x).toBeCloseTo(140, 6);
52
  // And never a step further, however long the silence lasts.
53
  expect(sample(walk, 10.3).x).toBeCloseTo(140, 6);
54
  expect(sample(walk, 30).x).toBeCloseTo(140, 6);
55
  expect(sample(walk, 30).angle).toBeCloseTo(2, 6);
56
});
57
58
test("two samples at one instant are one sample", () => {
59
  const still = pair({ t: 5, x: 7, y: 8, angle: 0.5 }, { t: 5, x: 7, y: 8, angle: 0.5 });
60
  expect(sample(still, 9)).toMatchObject({ x: 7, y: 8, angle: 0.5 });
61
});
62
63
test("the turn is taken the short way round", () => {
64
  const spin = pair({ t: 0, x: 0, y: 0, angle: 3.0 }, { t: 0.1, x: 0, y: 0, angle: -3.0 });
65
  // Not back through zero: onward past π, by a sixth of a radian or so.
66
  expect(sample(spin, 0.05).angle).toBeCloseTo(3.0 + 0.2831853 / 2, 6);
67
  const back = pair({ t: 0, x: 0, y: 0, angle: -3.0 }, { t: 0.1, x: 0, y: 0, angle: 3.0 });
68
  expect(sample(back, 0.05).angle).toBeCloseTo(-3.0 - 0.2831853 / 2, 6);
69
});
70
71
// ------------------------------------------------------------- the timeline
72
73
const body = (id: string, x: number, y = 0, angle = 0) => ({ id, x, y, angle });
74
75
test("a body is drawn where the server says until there are two of it", () => {
76
  const timeline = makeTimeline();
77
  const first = body("a", 50);
78
  expect(bodyAt(timeline, first, 10)).toBe(first);
79
  feed(timeline, "a", first, 10);
80
  expect(bodyAt(timeline, first, 10.2)).toMatchObject({ x: 50, y: 0 });
81
  feed(timeline, "a", body("a", 60), 10.1);
82
  expect(bodyAt(timeline, body("a", 60), 10.05).x).toBeCloseTo(55, 6);
83
});
84
85
test("each body keeps its own answer: one can be held while another is asked", () => {
86
  const timeline = makeTimeline();
87
  feed(timeline, "a", body("a", 0), 1);
88
  feed(timeline, "a", body("a", 10), 1.1);
89
  feed(timeline, "b", body("b", 100), 1);
90
  feed(timeline, "b", body("b", 110), 1.1);
91
  const a = bodyAt(timeline, body("a", 10), 1.05);
92
  const b = bodyAt(timeline, body("b", 110), 1.05);
93
  expect(a.x).toBeCloseTo(5, 6);
94
  expect(b.x).toBeCloseTo(105, 6);
95
});
96
97
test("a body that was out of sight starts again rather than sliding back", () => {
98
  const timeline = makeTimeline();
99
  feed(timeline, "a", body("a", 0), 1);
100
  feed(timeline, "a", body("a", 10), 1.1);
101
  feed(timeline, "a", body("a", 900), 1.1 + GONE + 0.1);
102
  expect(bodyAt(timeline, body("a", 900), 1.1 + GONE + 0.15).x).toBeCloseTo(900, 6);
103
});
104
105
test("a jump no one could have run is a respawn, not a walk", () => {
106
  const timeline = makeTimeline();
107
  feed(timeline, "a", body("a", 0), 1);
108
  feed(timeline, "a", body("a", 10), 1.1);
109
  // A third of a second's running is 80-odd units; 600 is a teleport.
110
  feed(timeline, "a", body("a", 610), 1.2);
111
  expect(bodyAt(timeline, body("a", 610), 1.25).x).toBeCloseTo(610, 6);
112
});
113
114
test("a sample that arrives out of order is dropped", () => {
115
  const timeline = makeTimeline();
116
  feed(timeline, "a", body("a", 0), 1);
117
  feed(timeline, "a", body("a", 10), 1.1);
118
  feed(timeline, "a", body("a", 999), 1.05);
119
  expect(bodyAt(timeline, body("a", 10), 1.1).x).toBeCloseTo(10, 6);
120
});
121
122
test("the delay follows the rate the snapshots actually arrive at", () => {
123
  const slow = makeTimeline();
124
  for (let i = 0; i < 60; i++) arrive(slow, 10 + i / 15);
125
  const fast = makeTimeline();
126
  for (let i = 0; i < 60; i++) arrive(fast, 10 + i / 30);
127
  expect(delay(slow)).toBeGreaterThan(delay(fast));
128
  expect(delay(slow)).toBeCloseTo((1 / 15) * LAG, 3);
129
  // Thirty a second wants less of a delay, and both stay inside the bounds.
130
  for (const d of [delay(slow), delay(fast)]) {
131
    expect(d).toBeGreaterThanOrEqual(LAG_MIN);
132
    expect(d).toBeLessThanOrEqual(LAG_MAX);
133
  }
134
  // A pause — a backgrounded tab, a reconnect — says nothing about the rate.
135
  arrive(fast, 40);
136
  expect(delay(fast)).toBeCloseTo(LAG_MIN, 3);
137
});
138
139
test("the moment being drawn is always bracketed by samples on hand", () => {
140
  // Thirty snapshots a second, a frame every 16ms: the moment being drawn
141
  // always falls between the two samples on hand, never before them.
142
  const timeline = makeTimeline();
143
  let x = 0;
144
  let last = { t: 0, x: 0 };
145
  for (let i = 0; i < 40; i++) {
146
    const at = 10 + i / 30;
147
    arrive(timeline, at);
148
    x += 8;
149
    feed(timeline, "a", body("a", x), at);
150
    last = { t: at, x };
151
    if (i < 3) continue;
152
    for (let f = 0; f < 2; f++) {
153
      const now = at + f * 0.016;
154
      const marks = timeline.bodies.get("a")!.marks as Mark[];
155
      const when = now - delay(timeline);
156
      expect(when).toBeGreaterThanOrEqual(marks[0]!.t - 1e-9);
157
      expect(when).toBeLessThanOrEqual(marks[marks.length - 1]!.t + 1e-9);
158
    }
159
  }
160
  // And late is what late means: behind the newest word by the delay, which
161
  // at eight units an interval is between one and two of them.
162
  const drawn = bodyAt(timeline, body("a", last.x), last.t - delay(timeline));
163
  expect(last.x - drawn.x).toBeGreaterThan(0);
164
  expect(last.x - drawn.x).toBeLessThan(20);
165
});
166
167
// --------------------------------------------- against the real simulation
168
169
/**
170
 * The match's own motion, sampled at the rate the server broadcasts it and
171
 * drawn at the rate a browser draws: what a bot walking across the map
172
 * actually looks like. The samples carry exactly what a snapshot carries —
173
 * a position rounded to a tenth, thirty times a second.
174
 *
175
 * The measure is the spread of the per-frame steps. A body drawn at the
176
 * server's speed takes even steps; the exponential chase this replaced took
177
 * a long step just after each snapshot and shorter ones after it, which is
178
 * choppiness with a number on it — twice this spread, measured the same way.
179
 */
180
test("the simulation's own motion is drawn at an even speed", async () => {
181
  const { runBots } = await import("./bots.ts");
182
  const { newMatch, tick } = await import("./match.ts");
183
  const round2 = (n: number) => Math.round(n * 10) / 10;
184
185
  // A match of its own: nothing here touches the arena everybody shares.
186
  const state = newMatch();
187
  const step = () => {
188
    runBots(state, 1 / 30);
189
    tick(state, 1 / 30);
190
  };
191
  for (let i = 0; i < 30 * 40 && state.phase !== "live"; i++) step();
192
  const stream: { t: number; bodies: { id: string; x: number; y: number; angle: number }[] }[] = [];
193
  for (let i = 0; i < 60; i++) {
194
    step();
195
    stream.push({
196
      t: i / 30,
197
      bodies: state.players.map((p) => ({ id: p.id, x: round2(p.x), y: round2(p.y), angle: round2(p.angle) })),
198
    });
199
  }
200
201
  const timeline = makeTimeline();
202
  const paths = new Map<string, { x: number; y: number }[]>();
203
  let next = 0;
204
  for (let f = 0; f / 60 < 1.8; f++) {
205
    const now = f / 60;
206
    while (next < stream.length && stream[next]!.t <= now) {
207
      arrive(timeline, stream[next]!.t);
208
      for (const p of stream[next]!.bodies) feed(timeline, p.id, p, stream[next]!.t);
209
      next += 1;
210
    }
211
    for (const p of stream[Math.max(0, next - 1)]!.bodies) {
212
      const at = bodyAt(timeline, p, now - delay(timeline));
213
      paths.set(p.id, [...(paths.get(p.id) ?? []), { x: at.x, y: at.y }]);
214
    }
215
  }
216
217
  // Whoever walked furthest in that second and a half.
218
  const walked = [...paths.values()]
219
    .filter((path) => path.length > 80)
220
    .map((path) => path.slice(1).map((at, i) => Math.hypot(at.x - path[i]!.x, at.y - path[i]!.y)))
221
    .map((steps) => steps.filter((step) => step > 0.001))
222
    .sort((a, b) => b.reduce((s, x) => s + x, 0) - a.reduce((s, x) => s + x, 0))[0]!;
223
  expect(walked.length).toBeGreaterThan(30);
224
  const mean = walked.reduce((s, x) => s + x, 0) / walked.length;
225
  const spread = Math.sqrt(walked.reduce((s, x) => s + (x - mean) ** 2, 0) / walked.length) / mean;
226
  // A body moving at the server's own speed: about four units every sixtieth
227
  // of a second, which is the run speed.
228
  expect(mean * 60).toBeGreaterThan(150);
229
  expect(spread).toBeLessThan(0.15);
230
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts