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

forked from benjivers/val

workbenchthis machine
Files 4fd585251d7e85f906bd34583a5e32b0a1736c1b
1
/**
2
 * Coming into sight and going out of it, as arithmetic. The case that
3
 * matters is a bot crossing a doorway: it should arrive over a tenth of a
4
 * second from the side it came from, and leave a mark where it went.
5
 */
6
7
import { expect, test } from "bun:test";
8
import {
9
  ENTRY,
10
  FADE,
11
  forget,
12
  GHOST,
13
  ghosts,
14
  makePresence,
15
  REACH,
16
  shown,
17
  sighted,
18
  slide,
19
  SMALL,
20
  swell,
21
} from "../../static/engine/presence.js";
22
23
const body = (over: Record<string, unknown> = {}) => ({
24
  id: "them",
25
  x: 400,
26
  y: 200,
27
  angle: 0,
28
  alive: true,
29
  mate: false,
30
  ...over,
31
});
32
33
const here = (x: number, y: number) => ({ x, y, angle: 0 });
34
35
test("a body just in sight is drawn in over a tenth of a second", () => {
36
  const p = makePresence();
37
  sighted(p, [body()], 10);
38
  expect(shown(p, "them", 10)).toBe(0);
39
  expect(shown(p, "them", 10 + FADE / 2)).toBeCloseTo(0.5, 5);
40
  expect(shown(p, "them", 10 + FADE * 1.01)).toBe(1);
41
  expect(shown(p, "them", 30)).toBe(1);
42
  // It starts small and reaches its own size, never past it.
43
  expect(swell(0)).toBe(SMALL);
44
  expect(swell(1)).toBe(1);
45
  expect(swell(4)).toBe(1);
46
});
47
48
test("a body already in sight is not faded in again by the next snapshot", () => {
49
  const p = makePresence();
50
  sighted(p, [body()], 10);
51
  sighted(p, [body({ x: 420 })], 10.03);
52
  sighted(p, [body({ x: 440 })], 10.06);
53
  expect(shown(p, "them", 10.06)).toBeCloseTo(0.5, 5);
54
  expect(shown(p, "them", 10.2)).toBe(1);
55
});
56
57
test("somebody nobody has ever seen is solid, not invisible", () => {
58
  const p = makePresence();
59
  expect(shown(p, "nobody", 10)).toBe(1);
60
  expect(slide(p, "nobody", here(1, 2), 10, here(0, 0))).toEqual(here(1, 2));
61
});
62
63
test("a body that steps back into view slides in from the way it came", () => {
64
  const p = makePresence();
65
  sighted(p, [body({ x: 400, y: 200 })], 10); // seen at the door
66
  sighted(p, [], 10.1); // and gone behind the wall
67
  sighted(p, [body({ x: 500, y: 200 })], 11); // back, further along
68
69
  const out = here(0, 0);
70
  // It begins a stride back down the line it walked, not at its destination.
71
  const start = slide(p, "them", here(500, 200), 11, out);
72
  expect(start.x).toBeCloseTo(500 - ENTRY, 5);
73
  expect(start.y).toBeCloseTo(200, 5);
74
  // Half way through the fade it is half way in.
75
  const half = slide(p, "them", here(500, 200), 11 + FADE / 2, out);
76
  expect(half.x).toBeCloseTo(500 - ENTRY / 2, 5);
77
  // And once it is in, the drawing is the interpolation's, untouched.
78
  const done = slide(p, "them", here(500, 200), 11 + FADE * 1.01, out);
79
  expect(done).toEqual(here(500, 200));
80
});
81
82
test("a body that turns up across the map does not slide across it", () => {
83
  const p = makePresence();
84
  sighted(p, [body({ x: 100, y: 100 })], 10);
85
  sighted(p, [], 10.1);
86
  sighted(p, [body({ x: 100 + REACH + 10, y: 100 })], 12);
87
  const out = here(0, 0);
88
  const at = here(100 + REACH + 10, 100);
89
  expect(slide(p, "them", at, 12, out)).toEqual(at);
90
  // It still fades in: an arrival, just not a walk from anywhere.
91
  expect(shown(p, "them", 12)).toBe(0);
92
});
93
94
test("a body that has not moved since has no direction to come from", () => {
95
  const p = makePresence();
96
  sighted(p, [body({ x: 300, y: 300 })], 10);
97
  sighted(p, [], 10.1);
98
  sighted(p, [body({ x: 300.5, y: 300 })], 11);
99
  const at = here(300.5, 300);
100
  expect(slide(p, "them", at, 11, here(0, 0))).toEqual(at);
101
});
102
103
test("where somebody was last seen is marked, and the mark dims away", () => {
104
  const p = makePresence();
105
  sighted(p, [body({ x: 640, y: 480 })], 10);
106
  expect(ghosts(p, 10)).toEqual([]);
107
108
  sighted(p, [], 10.5);
109
  const fresh = ghosts(p, 10.5);
110
  expect(fresh.length).toBe(1);
111
  expect(fresh[0]).toMatchObject({ id: "them", x: 640, y: 480, mate: false, fade: 1 });
112
  expect(ghosts(p, 10.5 + GHOST / 2)[0]!.fade).toBeCloseTo(0.5, 5);
113
  expect(ghosts(p, 10.5 + GHOST)).toEqual([]);
114
  // And it is forgotten, not kept for ever.
115
  expect(p.last.size).toBe(0);
116
});
117
118
test("a body last seen dead leaves no mark: a corpse is not a question", () => {
119
  const p = makePresence();
120
  sighted(p, [body({ alive: false })], 10);
121
  sighted(p, [], 10.2);
122
  expect(ghosts(p, 10.2)).toEqual([]);
123
});
124
125
test("coming back into sight takes the mark away at once", () => {
126
  const p = makePresence();
127
  sighted(p, [body({ x: 400 })], 10);
128
  sighted(p, [], 10.2);
129
  expect(ghosts(p, 10.3).length).toBe(1);
130
  sighted(p, [body({ x: 430 })], 10.4);
131
  expect(ghosts(p, 10.4)).toEqual([]);
132
});
133
134
test("a body in sight the whole time is never marked and never re-faded", () => {
135
  const p = makePresence();
136
  for (let i = 0; i < 20; i++) sighted(p, [body({ x: 400 + i * 5 })], 10 + i * 0.033);
137
  expect(ghosts(p, 10.7)).toEqual([]);
138
  expect(shown(p, "them", 10.7)).toBe(1);
139
});
140
141
test("a new round forgets everybody", () => {
142
  const p = makePresence();
143
  sighted(p, [body()], 10);
144
  sighted(p, [], 10.2);
145
  forget(p);
146
  expect(ghosts(p, 10.3)).toEqual([]);
147
  expect(p.live.size).toBe(0);
148
  // And the next sighting is a first sighting, with nothing to slide from.
149
  sighted(p, [body({ x: 900 })], 11);
150
  expect(shown(p, "them", 11)).toBe(0);
151
  const at = here(900, 200);
152
  expect(slide(p, "them", at, 11, here(0, 0))).toEqual(at);
153
});
154
155
test("two bodies are tracked apart", () => {
156
  const p = makePresence();
157
  sighted(p, [body({ id: "a" }), body({ id: "b", x: 800 })], 10);
158
  sighted(p, [body({ id: "a" })], 10.2);
159
  expect(shown(p, "a", 10.2)).toBe(1);
160
  const marks = ghosts(p, 10.2);
161
  expect(marks.map((m) => m.id)).toEqual(["b"]);
162
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts