| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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/engine/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 |
|
| 24 |
|
| 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 |
|
| 49 |
expect(sample(walk, 10.15).x).toBeCloseTo(130, 6); |
| 50 |
|
| 51 |
expect(sample(walk, 10.2).x).toBeCloseTo(140, 6); |
| 52 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 130 |
for (const d of [delay(slow), delay(fast)]) { |
| 131 |
expect(d).toBeGreaterThanOrEqual(LAG_MIN); |
| 132 |
expect(d).toBeLessThanOrEqual(LAG_MAX); |
| 133 |
} |
| 134 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 161 |
|
| 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 |
|