@zaisma/effects 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/partners.js CHANGED
@@ -23,10 +23,10 @@ const IMPACT_SPEED_M_PER_SEC = 1.5;
23
23
  const HEAVY_IMPACT_SPEED_M_PER_SEC = 9;
24
24
  // A pile settling knocks many times a second: one body's knocks are this far apart, and a frame plays this many.
25
25
  const IMPACT_GAP_SEC = 0.18;
26
- const MAXIMUM_IMPACTS_PER_FRAME = 4;
26
+ const MAXIMUM_IMPACT_COUNT_PER_FRAME = 4;
27
27
 
28
- const clamp = (value, low, high) => Math.min(Math.max(value, low), high);
29
- const isPoint = (value) => value !== null && typeof value === "object" && Number.isFinite(value.x) && Number.isFinite(value.y) && Number.isFinite(value.z);
28
+ const clamp = (amount, low, high) => Math.min(Math.max(amount, low), high);
29
+ const isPoint = (candidate) => candidate !== null && typeof candidate === "object" && Number.isFinite(candidate.x) && Number.isFinite(candidate.y) && Number.isFinite(candidate.z);
30
30
  const hookNamesOf = (piece) => new Set(typeof piece?.hooks?.list === "function" ? piece.hooks.list().map((hook) => hook.name) : []);
31
31
 
32
32
  /**
@@ -56,9 +56,9 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
56
56
  }
57
57
 
58
58
  /** Plays a free effect once its file is here; before that, nothing: the first footfalls of a game go without. */
59
- function playFree(effectName, at, reason) {
59
+ function playFree(effectName, playAt, reason) {
60
60
  if (!effects.has(effectName)) return;
61
- startPlay(effectName, at, reason);
61
+ startPlay(effectName, playAt, reason);
62
62
  }
63
63
 
64
64
  function loadFree(names) {
@@ -119,33 +119,33 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
119
119
  }
120
120
  const { x, y, z } = object.position;
121
121
  const acrossM = Math.hypot(x - seen.x, z - seen.z);
122
- const riseSpeed = (y - seen.y) / frameSec;
122
+ const riseSpeedMPerSec = (y - seen.y) / frameSec;
123
123
  // A first frame, or a jump of metres in one (a respawn): start again from here.
124
124
  if (!seen.hasPlace || acrossM > 3 || Math.abs(y - seen.y) > 3) {
125
125
  seen.hasPlace = true;
126
126
  seen.fallSpeed = 0;
127
127
  seen.groundSec = 0;
128
128
  seen.strideM = 0;
129
- } else if (riseSpeed < -3.5) {
130
- seen.fallSpeed = Math.max(seen.fallSpeed, -riseSpeed);
129
+ } else if (riseSpeedMPerSec < -3.5) {
130
+ seen.fallSpeed = Math.max(seen.fallSpeed, -riseSpeedMPerSec);
131
131
  seen.groundSec = 0;
132
- } else if (riseSpeed > 3.5) {
132
+ } else if (riseSpeedMPerSec > 3.5) {
133
133
  if (seen.groundSec > 0.1) tellWatched("jump", seen.x, seen.y, seen.z, 0, 0);
134
134
  seen.groundSec = 0;
135
- } else if (Math.abs(riseSpeed) < 3) {
135
+ } else if (Math.abs(riseSpeedMPerSec) < 3) {
136
136
  if (seen.fallSpeed > 0) tellWatched("land", x, y, z, 0, seen.fallSpeed);
137
137
  seen.fallSpeed = 0;
138
138
  seen.groundSec += frameSec;
139
- const speed = acrossM / frameSec;
140
- if (speed > 0.6) {
139
+ const speedMPerSec = acrossM / frameSec;
140
+ if (speedMPerSec > 0.6) {
141
141
  seen.strideM += acrossM;
142
142
  // A stride lengthens with speed: a walk's is short, a sprint's long.
143
- if (seen.strideM >= clamp(0.5 + speed * 0.13, 0.55, 1.5)) {
143
+ if (seen.strideM >= clamp(0.5 + speedMPerSec * 0.13, 0.55, 1.5)) {
144
144
  seen.strideM = 0;
145
145
  // The foot lands a little to one side of the way it's going.
146
- const side = (seen.isLeftNext ? 0.11 : -0.11) / Math.max(acrossM, 1e-6);
146
+ const sideNumber = (seen.isLeftNext ? 0.11 : -0.11) / Math.max(acrossM, 1e-6);
147
147
  seen.isLeftNext = !seen.isLeftNext;
148
- tellWatched("step", x - (z - seen.z) * side, y, z + (x - seen.x) * side, speed, 0);
148
+ tellWatched("step", x - (z - seen.z) * sideNumber, y, z + (x - seen.x) * sideNumber, speedMPerSec, 0);
149
149
  }
150
150
  } else seen.strideM = 0;
151
151
  }
@@ -173,38 +173,38 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
173
173
  // ---- Physics ----
174
174
  /** bodyId -> when its last knock played. */
175
175
  const knockedSec = new Map();
176
- let impactsThisFrame = 0;
176
+ let frameImpactCount = 0;
177
177
  const { physics } = given;
178
178
  if (physics && hookNamesOf(physics).has("bodyHit")) {
179
179
  stops.push(
180
180
  physics.hooks.on("bodyHit", (hit) => {
181
- if (!hit || !settings.get("impactEffectsEnabled") || impactsThisFrame >= MAXIMUM_IMPACTS_PER_FRAME) return;
181
+ if (!hit || !settings.get("impactEffectsEnabled") || frameImpactCount >= MAXIMUM_IMPACT_COUNT_PER_FRAME) return;
182
182
  const speed = hit.speedMPerSec;
183
183
  if (!(speed >= IMPACT_SPEED_M_PER_SEC)) return;
184
184
  const bodyId = hit.bodyId ?? hit.otherBodyId ?? null;
185
185
  if (bodyId !== null && nowSec - (knockedSec.get(bodyId) ?? -Infinity) < IMPACT_GAP_SEC) return;
186
- const at = {};
187
- if (isPoint(hit.positionM)) at.positionM = hit.positionM;
186
+ const playAt = {};
187
+ if (isPoint(hit.positionM)) playAt.positionM = hit.positionM;
188
188
  else {
189
189
  // A physics that tells no place: the body's own, dropped to what's under it.
190
190
  const middle = bodyId !== null && typeof physics.bodyState === "function" ? physics.bodyState(bodyId)?.positionM : null;
191
191
  if (!isPoint(middle)) return;
192
192
  if (findSurface(middle.x, middle.y, middle.z, 0, -1, 0, 2.5, surface)) {
193
- at.positionM = { x: surface.x, y: surface.y, z: surface.z };
194
- at.normal = { x: surface.normalX, y: surface.normalY, z: surface.normalZ };
195
- } else at.positionM = { x: middle.x, y: middle.y, z: middle.z };
193
+ playAt.positionM = { x: surface.x, y: surface.y, z: surface.z };
194
+ playAt.normal = { x: surface.normalX, y: surface.normalY, z: surface.normalZ };
195
+ } else playAt.positionM = { x: middle.x, y: middle.y, z: middle.z };
196
196
  }
197
- if (!isWithin(at.positionM, IMPACT_REACH_M)) return;
197
+ if (!isWithin(playAt.positionM, IMPACT_REACH_M)) return;
198
198
  // Heavy things land hard at a lower speed: a crate at 9 m/s, a car at 4.
199
199
  const massKg = Number.isFinite(hit.massKg) ? hit.massKg : 30;
200
- const force = speed * clamp(Math.cbrt(massKg / 30), 0.6, 2.5);
201
- const isHeavy = force >= HEAVY_IMPACT_SPEED_M_PER_SEC;
202
- at.scalePct = isHeavy ? clamp(50 + force * 6, 90, 240) : clamp(35 + force * 12, 50, 150);
200
+ const forceNumber = speed * clamp(Math.cbrt(massKg / 30), 0.6, 2.5);
201
+ const isHeavy = forceNumber >= HEAVY_IMPACT_SPEED_M_PER_SEC;
202
+ playAt.scalePct = isHeavy ? clamp(50 + forceNumber * 6, 90, 240) : clamp(35 + forceNumber * 12, 50, 150);
203
203
  // What it landed on says what flies up; a physics that tells neither is stone until the game says.
204
- at.surfaceKind = surfaceKindOf(hit.otherMaterialName ?? hit.materialName ?? surface.materialName ?? null, at.positionM, "body_hit", bodyId);
204
+ playAt.surfaceKind = surfaceKindOf(hit.otherMaterialName ?? hit.materialName ?? surface.materialName ?? null, playAt.positionM, "body_hit", bodyId);
205
205
  if (bodyId !== null) knockedSec.set(bodyId, nowSec);
206
- impactsThisFrame += 1;
207
- playFree(isHeavy ? "impact_heavy" : "impact", at, "body_hit");
206
+ frameImpactCount += 1;
207
+ playFree(isHeavy ? "impact_heavy" : "impact", playAt, "body_hit");
208
208
  }),
209
209
  );
210
210
  loadFree(PHYSICS_EFFECT_NAMES);
@@ -232,7 +232,7 @@ export function connectPartners({ effects, given, settings, startPlay, findSurfa
232
232
  /** Every frame, before the effects are drawn. camera: the three.js camera the game draws with. */
233
233
  update(sec, frameSec, camera) {
234
234
  nowSec = sec;
235
- impactsThisFrame = 0;
235
+ frameImpactCount = 0;
236
236
  if (camera?.isCamera) {
237
237
  const place = camera.matrixWorld.elements;
238
238
  cameraPlace.x = place[12];
package/piece.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "statement_version_number": 2,
3
3
  "name": "@zaisma/effects",
4
4
  "title": "Effects",
5
- "piece_version": "1.0.1",
5
+ "piece_version": "1.2.0",
6
6
  "kind": "foundation",
7
7
  "standard": "@zaisma/standard",
8
8
  "summary": "Spectacle that holds a phone's frame rate, for three.js: thousands of lit particles, hits, explosions, magic, trails, screen warps, marks on surfaces, debris that bounces and weather, in the look a game picks. An effect is a small file an AI writes; 92 come with it. Flashes light the scene, the character and physics bring effects free, and online every player sees the same effect at once.",
@@ -379,12 +379,12 @@
379
379
  {
380
380
  "name": "effect_started",
381
381
  "hook_kind": "event",
382
- "summary": "Told when an effect starts, with { playId, effectName, positionM, scalePct, strengthPct, surfaceKind, reason }: where a game plays the sound that goes with it."
382
+ "summary": "Told when an effect starts, with { playNumber, effectName, positionM, scalePct, strengthPct, surfaceKind, reason }: where a game plays the sound that goes with it."
383
383
  },
384
384
  {
385
385
  "name": "effect_ended",
386
386
  "hook_kind": "event",
387
- "summary": "Told when an effect's last particle is gone, or it was stopped, with { playId, effectName }."
387
+ "summary": "Told when an effect's last particle is gone, or it was stopped, with { playNumber, effectName }."
388
388
  },
389
389
  {
390
390
  "name": "surface_kind_at",
package/piece.json.js CHANGED
@@ -3,7 +3,7 @@ export default {
3
3
  "statement_version_number": 2,
4
4
  "name": "@zaisma/effects",
5
5
  "title": "Effects",
6
- "piece_version": "1.0.1",
6
+ "piece_version": "1.2.0",
7
7
  "kind": "foundation",
8
8
  "standard": "@zaisma/standard",
9
9
  "summary": "Spectacle that holds a phone's frame rate, for three.js: thousands of lit particles, hits, explosions, magic, trails, screen warps, marks on surfaces, debris that bounces and weather, in the look a game picks. An effect is a small file an AI writes; 92 come with it. Flashes light the scene, the character and physics bring effects free, and online every player sees the same effect at once.",
@@ -380,12 +380,12 @@ export default {
380
380
  {
381
381
  "name": "effect_started",
382
382
  "hook_kind": "event",
383
- "summary": "Told when an effect starts, with { playId, effectName, positionM, scalePct, strengthPct, surfaceKind, reason }: where a game plays the sound that goes with it."
383
+ "summary": "Told when an effect starts, with { playNumber, effectName, positionM, scalePct, strengthPct, surfaceKind, reason }: where a game plays the sound that goes with it."
384
384
  },
385
385
  {
386
386
  "name": "effect_ended",
387
387
  "hook_kind": "event",
388
- "summary": "Told when an effect's last particle is gone, or it was stopped, with { playId, effectName }."
388
+ "summary": "Told when an effect's last particle is gone, or it was stopped, with { playNumber, effectName }."
389
389
  },
390
390
  {
391
391
  "name": "surface_kind_at",
package/shapes.js CHANGED
@@ -10,9 +10,9 @@ export const ATLAS_CELL_COUNT = 8;
10
10
  export const FILM_FRAME_COUNT = 16;
11
11
  const FILM_COLUMN_COUNT = 4;
12
12
  const FILM_ORIGINS = { smoke_film: { column: 0, row: 4 }, fire_film: { column: 4, row: 4 } };
13
- const CELL = 1 / ATLAS_CELL_COUNT;
13
+ const CELL_SIZE_NUMBER = 1 / ATLAS_CELL_COUNT;
14
14
 
15
- const cellAt = (column, row) => ({ u: column * CELL, v: row * CELL, sizeNumber: CELL });
15
+ const cellAt = (column, row) => ({ u: column * CELL_SIZE_NUMBER, v: row * CELL_SIZE_NUMBER, sizeNumber: CELL_SIZE_NUMBER });
16
16
 
17
17
  // Shapes fill the first two rows, then the fourth: the third holds the marks.
18
18
  const shapeRow = (index) => (index < 16 ? Math.floor(index / ATLAS_CELL_COUNT) : 3);
@@ -260,10 +260,10 @@ export function createShapeAtlas(renderer, sizePx) {
260
260
  const depths = SHAPE_NAMES.map((shapeName, index) => {
261
261
  const fromX = (index % ATLAS_CELL_COUNT) * cellPx;
262
262
  const fromY = shapeRow(index) * cellPx;
263
- let deepest = 0;
263
+ let deepestByte = 0;
264
264
  // Every fourth point is enough to find the deepest.
265
- for (let y = fromY; y < fromY + cellPx; y += 4) for (let x = fromX; x < fromX + cellPx; x += 4) deepest = Math.max(deepest, drawn[(y * sizePx + x) * 4]);
266
- return Math.max(deepest / 255, 0.52);
265
+ for (let y = fromY; y < fromY + cellPx; y += 4) for (let x = fromX; x < fromX + cellPx; x += 4) deepestByte = Math.max(deepestByte, drawn[(y * sizePx + x) * 4]);
266
+ return Math.max(deepestByte / 255, 0.52);
267
267
  });
268
268
  return { texture: target.texture, depthOf: (shapeName) => depths[SHAPE_NAMES.indexOf(shapeName)] ?? 1, dispose: () => target.dispose() };
269
269
  }
package/sync.js CHANGED
@@ -14,14 +14,14 @@ import { EFFECT_NAME_PATTERN, MAXIMUM_EFFECT_NAME_CHARACTER_COUNT, SURFACE_KINDS
14
14
  const PIECE_NAME = "@zaisma/effects";
15
15
  const PLAY_CODE = 1;
16
16
  const STOP_CODE = 2;
17
- const HAS_DIRECTION = 1;
18
- const HAS_NORMAL = 2;
19
- const HAS_TO_POSITION = 4;
20
- const HAS_SCALE = 8;
21
- const HAS_STRENGTH = 16;
22
- const HAS_COLOR = 32;
23
- const HAS_SURFACE = 64;
24
- const IS_KEPT = 128;
17
+ const DIRECTION_FLAG_NUMBER = 1;
18
+ const NORMAL_FLAG_NUMBER = 2;
19
+ const TO_POSITION_FLAG_NUMBER = 4;
20
+ const SCALE_FLAG_NUMBER = 8;
21
+ const STRENGTH_FLAG_NUMBER = 16;
22
+ const COLOR_FLAG_NUMBER = 32;
23
+ const SURFACE_FLAG_NUMBER = 64;
24
+ const KEPT_FLAG_NUMBER = 128;
25
25
  const FIXED_BYTE_COUNT = 26;
26
26
  const MAXIMUM_PLAY_BYTE_COUNT = FIXED_BYTE_COUNT + 12 + 12 + 4 + 4 + 3 + 1 + 4 + 1 + MAXIMUM_EFFECT_NAME_CHARACTER_COUNT;
27
27
  /** How many effects that keep playing a room holds at once. */
@@ -46,9 +46,9 @@ const WEATHER_FIELDS = [
46
46
  const refuse = (code, message, field, fix) => {
47
47
  throw new PieceError(PIECE_NAME, code, { message, field, fix });
48
48
  };
49
- const isPoint = (value) => value !== null && typeof value === "object" && [value.x, value.y, value.z].every((part) => Number.isFinite(part) && Math.abs(part) <= MAXIMUM_PLACE_M);
50
- const isNumberIn = (value, low, high) => Number.isFinite(value) && value >= low && value <= high;
51
- const isName = (value) => typeof value === "string" && value.length <= MAXIMUM_EFFECT_NAME_CHARACTER_COUNT && EFFECT_NAME_PATTERN.test(value);
49
+ const isPoint = (candidate) => candidate !== null && typeof candidate === "object" && [candidate.x, candidate.y, candidate.z].every((part) => Number.isFinite(part) && Math.abs(part) <= MAXIMUM_PLACE_M);
50
+ const isNumberIn = (candidate, low, high) => Number.isFinite(candidate) && candidate >= low && candidate <= high;
51
+ const isName = (candidate) => typeof candidate === "string" && candidate.length <= MAXIMUM_EFFECT_NAME_CHARACTER_COUNT && EFFECT_NAME_PATTERN.test(candidate);
52
52
  const toHex = (bytes) => Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
53
53
  function fromHex(hex) {
54
54
  if (typeof hex !== "string" || hex.length % 2 !== 0 || hex.length > MAXIMUM_PLAY_BYTE_COUNT * 2 || !/^[0-9a-f]*$/.test(hex)) return null;
@@ -64,78 +64,78 @@ function fromHex(hex) {
64
64
  */
65
65
  export function encodePlay(play) {
66
66
  const way = play.normal ?? play.direction ?? null;
67
- let flags = 0;
67
+ let flagNumber = 0;
68
68
  let byteCount = FIXED_BYTE_COUNT + 1 + play.effectName.length;
69
69
  if (way) {
70
- flags |= play.normal ? HAS_NORMAL : HAS_DIRECTION;
70
+ flagNumber |= play.normal ? NORMAL_FLAG_NUMBER : DIRECTION_FLAG_NUMBER;
71
71
  byteCount += 12;
72
72
  }
73
73
  if (play.toPositionM) {
74
- flags |= HAS_TO_POSITION;
74
+ flagNumber |= TO_POSITION_FLAG_NUMBER;
75
75
  byteCount += 12;
76
76
  }
77
77
  if (play.scalePct !== null && play.scalePct !== undefined) {
78
- flags |= HAS_SCALE;
78
+ flagNumber |= SCALE_FLAG_NUMBER;
79
79
  byteCount += 4;
80
80
  }
81
81
  if (play.strengthPct !== null && play.strengthPct !== undefined) {
82
- flags |= HAS_STRENGTH;
82
+ flagNumber |= STRENGTH_FLAG_NUMBER;
83
83
  byteCount += 4;
84
84
  }
85
85
  if (play.color) {
86
- flags |= HAS_COLOR;
86
+ flagNumber |= COLOR_FLAG_NUMBER;
87
87
  byteCount += 3;
88
88
  }
89
89
  if (play.surfaceKind) {
90
- flags |= HAS_SURFACE;
90
+ flagNumber |= SURFACE_FLAG_NUMBER;
91
91
  byteCount += 1;
92
92
  }
93
93
  if (play.keptId) {
94
- flags |= IS_KEPT;
94
+ flagNumber |= KEPT_FLAG_NUMBER;
95
95
  byteCount += 4;
96
96
  }
97
97
  const bytes = new Uint8Array(byteCount);
98
98
  const view = new DataView(bytes.buffer);
99
- let at = 0;
100
- const point = (value) => {
101
- view.setFloat32(at, value.x, true);
102
- view.setFloat32(at + 4, value.y, true);
103
- view.setFloat32(at + 8, value.z, true);
104
- at += 12;
99
+ let byteIndex = 0;
100
+ const point = (vector) => {
101
+ view.setFloat32(byteIndex, vector.x, true);
102
+ view.setFloat32(byteIndex + 4, vector.y, true);
103
+ view.setFloat32(byteIndex + 8, vector.z, true);
104
+ byteIndex += 12;
105
105
  };
106
106
  view.setUint8(0, PLAY_CODE);
107
- view.setUint8(1, flags);
107
+ view.setUint8(1, flagNumber);
108
108
  view.setFloat64(2, play.startedAtMs, true);
109
109
  view.setUint32(10, play.seedNumber >>> 0, true);
110
- at = 14;
110
+ byteIndex = 14;
111
111
  point(play.positionM);
112
112
  if (way) point(way);
113
113
  if (play.toPositionM) point(play.toPositionM);
114
- if (flags & HAS_SCALE) {
115
- view.setFloat32(at, play.scalePct, true);
116
- at += 4;
114
+ if (flagNumber & SCALE_FLAG_NUMBER) {
115
+ view.setFloat32(byteIndex, play.scalePct, true);
116
+ byteIndex += 4;
117
117
  }
118
- if (flags & HAS_STRENGTH) {
119
- view.setFloat32(at, play.strengthPct, true);
120
- at += 4;
118
+ if (flagNumber & STRENGTH_FLAG_NUMBER) {
119
+ view.setFloat32(byteIndex, play.strengthPct, true);
120
+ byteIndex += 4;
121
121
  }
122
- if (flags & HAS_COLOR) {
122
+ if (flagNumber & COLOR_FLAG_NUMBER) {
123
123
  const colorNumber = Number.parseInt(play.color.slice(1), 16);
124
- view.setUint8(at, colorNumber >> 16);
125
- view.setUint8(at + 1, (colorNumber >> 8) & 255);
126
- view.setUint8(at + 2, colorNumber & 255);
127
- at += 3;
124
+ view.setUint8(byteIndex, colorNumber >> 16);
125
+ view.setUint8(byteIndex + 1, (colorNumber >> 8) & 255);
126
+ view.setUint8(byteIndex + 2, colorNumber & 255);
127
+ byteIndex += 3;
128
128
  }
129
- if (flags & HAS_SURFACE) {
130
- view.setUint8(at, SURFACE_KINDS.indexOf(play.surfaceKind));
131
- at += 1;
129
+ if (flagNumber & SURFACE_FLAG_NUMBER) {
130
+ view.setUint8(byteIndex, SURFACE_KINDS.indexOf(play.surfaceKind));
131
+ byteIndex += 1;
132
132
  }
133
- if (flags & IS_KEPT) {
134
- view.setUint32(at, play.keptId >>> 0, true);
135
- at += 4;
133
+ if (flagNumber & KEPT_FLAG_NUMBER) {
134
+ view.setUint32(byteIndex, play.keptId >>> 0, true);
135
+ byteIndex += 4;
136
136
  }
137
- view.setUint8(at, play.effectName.length);
138
- for (let index = 0; index < play.effectName.length; index += 1) view.setUint8(at + 1 + index, play.effectName.charCodeAt(index));
137
+ view.setUint8(byteIndex, play.effectName.length);
138
+ for (let index = 0; index < play.effectName.length; index += 1) view.setUint8(byteIndex + 1 + index, play.effectName.charCodeAt(index));
139
139
  return bytes;
140
140
  }
141
141
 
@@ -143,58 +143,58 @@ export function encodePlay(play) {
143
143
  export function decodePlay(bytes) {
144
144
  if (!(bytes instanceof Uint8Array) || bytes.length < FIXED_BYTE_COUNT + 2 || bytes.length > MAXIMUM_PLAY_BYTE_COUNT || bytes[0] !== PLAY_CODE) return null;
145
145
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
146
- const flags = view.getUint8(1);
147
- if (flags & HAS_DIRECTION && flags & HAS_NORMAL) return null;
148
- let at = 14;
146
+ const flagNumber = view.getUint8(1);
147
+ if (flagNumber & DIRECTION_FLAG_NUMBER && flagNumber & NORMAL_FLAG_NUMBER) return null;
148
+ let byteIndex = 14;
149
149
  let isShort = false;
150
150
  const has = (byteCount) => {
151
- if (at + byteCount > bytes.length) isShort = true;
151
+ if (byteIndex + byteCount > bytes.length) isShort = true;
152
152
  return !isShort;
153
153
  };
154
154
  const point = () => {
155
155
  if (!has(12)) return null;
156
- const value = { x: view.getFloat32(at, true), y: view.getFloat32(at + 4, true), z: view.getFloat32(at + 8, true) };
157
- at += 12;
158
- return value;
156
+ const decodedPoint = { x: view.getFloat32(byteIndex, true), y: view.getFloat32(byteIndex + 4, true), z: view.getFloat32(byteIndex + 8, true) };
157
+ byteIndex += 12;
158
+ return decodedPoint;
159
159
  };
160
160
  const play = { effectName: "", positionM: null, direction: null, normal: null, toPositionM: null, scalePct: null, strengthPct: null, color: null, surfaceKind: null, seedNumber: view.getUint32(10, true), startedAtMs: view.getFloat64(2, true), keptId: null };
161
161
  play.positionM = point();
162
- if (flags & HAS_DIRECTION) play.direction = point();
163
- if (flags & HAS_NORMAL) play.normal = point();
164
- if (flags & HAS_TO_POSITION) play.toPositionM = point();
165
- if (flags & HAS_SCALE && has(4)) {
166
- play.scalePct = view.getFloat32(at, true);
167
- at += 4;
162
+ if (flagNumber & DIRECTION_FLAG_NUMBER) play.direction = point();
163
+ if (flagNumber & NORMAL_FLAG_NUMBER) play.normal = point();
164
+ if (flagNumber & TO_POSITION_FLAG_NUMBER) play.toPositionM = point();
165
+ if (flagNumber & SCALE_FLAG_NUMBER && has(4)) {
166
+ play.scalePct = view.getFloat32(byteIndex, true);
167
+ byteIndex += 4;
168
168
  }
169
- if (flags & HAS_STRENGTH && has(4)) {
170
- play.strengthPct = view.getFloat32(at, true);
171
- at += 4;
169
+ if (flagNumber & STRENGTH_FLAG_NUMBER && has(4)) {
170
+ play.strengthPct = view.getFloat32(byteIndex, true);
171
+ byteIndex += 4;
172
172
  }
173
- if (flags & HAS_COLOR && has(3)) {
174
- play.color = `#${((view.getUint8(at) << 16) | (view.getUint8(at + 1) << 8) | view.getUint8(at + 2)).toString(16).padStart(6, "0")}`;
175
- at += 3;
173
+ if (flagNumber & COLOR_FLAG_NUMBER && has(3)) {
174
+ play.color = `#${((view.getUint8(byteIndex) << 16) | (view.getUint8(byteIndex + 1) << 8) | view.getUint8(byteIndex + 2)).toString(16).padStart(6, "0")}`;
175
+ byteIndex += 3;
176
176
  }
177
- if (flags & HAS_SURFACE && has(1)) {
178
- play.surfaceKind = SURFACE_KINDS[view.getUint8(at)] ?? "";
179
- at += 1;
177
+ if (flagNumber & SURFACE_FLAG_NUMBER && has(1)) {
178
+ play.surfaceKind = SURFACE_KINDS[view.getUint8(byteIndex)] ?? "";
179
+ byteIndex += 1;
180
180
  }
181
- if (flags & IS_KEPT && has(4)) {
182
- play.keptId = view.getUint32(at, true);
183
- at += 4;
181
+ if (flagNumber & KEPT_FLAG_NUMBER && has(4)) {
182
+ play.keptId = view.getUint32(byteIndex, true);
183
+ byteIndex += 4;
184
184
  }
185
185
  if (!has(1)) return null;
186
- const nameLength = view.getUint8(at);
187
- if (isShort || at + 1 + nameLength !== bytes.length) return null;
188
- play.effectName = String.fromCharCode(...bytes.subarray(at + 1));
186
+ const nameByteCount = view.getUint8(byteIndex);
187
+ if (isShort || byteIndex + 1 + nameByteCount !== bytes.length) return null;
188
+ play.effectName = String.fromCharCode(...bytes.subarray(byteIndex + 1));
189
189
  const isSound =
190
190
  isName(play.effectName) &&
191
191
  Number.isFinite(play.startedAtMs) &&
192
192
  isPoint(play.positionM) &&
193
- [play.direction, play.normal, play.toPositionM].every((value) => value === null || isPoint(value)) &&
193
+ [play.direction, play.normal, play.toPositionM].every((vector) => vector === null || isPoint(vector)) &&
194
194
  (play.scalePct === null || isNumberIn(play.scalePct, 0.01, 10000)) &&
195
195
  (play.strengthPct === null || isNumberIn(play.strengthPct, 0, 1000)) &&
196
196
  play.surfaceKind !== "" &&
197
- (!(flags & IS_KEPT) || play.keptId > 0);
197
+ (!(flagNumber & KEPT_FLAG_NUMBER) || play.keptId > 0);
198
198
  return isSound ? play : null;
199
199
  }
200
200
 
@@ -224,35 +224,35 @@ function createRateCap(perSec) {
224
224
  }
225
225
 
226
226
  /** What play and start take, checked as effects.play checks it, as a play ready to send. */
227
- function playOf(effectName, at, effectNames, callName) {
227
+ function playOf(effectName, playAt, effectNames, callName) {
228
228
  if (!isName(effectName)) refuse("bad_play", `${callName} takes an effect's name.`, "effectName", 'Give the name of an effect, like "explosion_large".');
229
229
  if (effectNames && !effectNames.includes(effectName)) refuse("unknown_effect", `"${effectName}" isn't one of this room's shared effects.`, "effectName", `Use one of: ${effectNames.join(", ")}; or add it to effectNames, in the game and on its game server.`);
230
- if (at === null || typeof at !== "object" || Array.isArray(at)) refuse("bad_play", `${callName} takes where to play as an object.`, "at", "Give { positionM: { x, y, z } }.");
231
- for (const field of Object.keys(at)) {
230
+ if (playAt === null || typeof playAt !== "object" || Array.isArray(playAt)) refuse("bad_play", `${callName} takes where to play as an object.`, "at", "Give { positionM: { x, y, z } }.");
231
+ for (const field of Object.keys(playAt)) {
232
232
  if (!AT_FIELDS.includes(field)) refuse("unknown_argument", `A shared play has no "${field}".`, field, `Give only: ${AT_FIELDS.join(", ")}. An effect that rides on an object (follow) is each game's own: play it with effects.play.`);
233
233
  }
234
- if (!isPoint(at.positionM)) refuse("bad_play", `${callName} needs where to play.`, "positionM", "Give positionM: { x, y, z } in metres.");
234
+ if (!isPoint(playAt.positionM)) refuse("bad_play", `${callName} needs where to play.`, "positionM", "Give positionM: { x, y, z } in metres.");
235
235
  for (const field of ["direction", "normal", "toPositionM"]) {
236
- if (at[field] !== undefined && at[field] !== null && !isPoint(at[field])) refuse("bad_play", `${field} isn't { x, y, z }.`, field, "Give { x, y, z }.");
236
+ if (playAt[field] !== undefined && playAt[field] !== null && !isPoint(playAt[field])) refuse("bad_play", `${field} isn't { x, y, z }.`, field, "Give { x, y, z }.");
237
237
  }
238
- if (at.direction && at.normal) refuse("bad_play", "A play takes a direction or a normal, never both.", "normal", "Give the one the effect goes by: normal for a surface it lands on, direction for the way it's thrown.");
239
- if (at.scalePct !== undefined && at.scalePct !== null && !isNumberIn(at.scalePct, 0.01, 10000)) refuse("bad_play", "scalePct isn't a number above 0, up to 10000.", "scalePct", "Give how big to play it against its file: 100 is as written.");
240
- if (at.strengthPct !== undefined && at.strengthPct !== null && !isNumberIn(at.strengthPct, 0, 1000)) refuse("bad_play", "strengthPct isn't a number from 0 to 1000.", "strengthPct", "Give how strong to play it: 100 is as written.");
241
- if (at.color !== undefined && at.color !== null && !(typeof at.color === "string" && HEX_COLOR_PATTERN.test(at.color))) refuse("bad_play", "color isn't a colour.", "color", 'Give a colour as six hex digits in lowercase, like "#ff7a1a", or leave it out.');
242
- if (at.surfaceKind !== undefined && at.surfaceKind !== null && !SURFACE_KINDS.includes(at.surfaceKind)) refuse("bad_play", `surfaceKind is ${JSON.stringify(at.surfaceKind)}, which isn't a surface kind.`, "surfaceKind", `Give one of: ${SURFACE_KINDS.join(", ")}.`);
243
- const copy = (value) => (value ? { x: value.x, y: value.y, z: value.z } : null);
244
- return { effectName, positionM: copy(at.positionM), direction: copy(at.direction), normal: copy(at.normal), toPositionM: copy(at.toPositionM), scalePct: at.scalePct ?? null, strengthPct: at.strengthPct ?? null, color: at.color ?? null, surfaceKind: at.surfaceKind ?? null, seedNumber: 0, startedAtMs: 0, keptId: null };
238
+ if (playAt.direction && playAt.normal) refuse("bad_play", "A play takes a direction or a normal, never both.", "normal", "Give the one the effect goes by: normal for a surface it lands on, direction for the way it's thrown.");
239
+ if (playAt.scalePct !== undefined && playAt.scalePct !== null && !isNumberIn(playAt.scalePct, 0.01, 10000)) refuse("bad_play", "scalePct isn't a number above 0, up to 10000.", "scalePct", "Give how big to play it against its file: 100 is as written.");
240
+ if (playAt.strengthPct !== undefined && playAt.strengthPct !== null && !isNumberIn(playAt.strengthPct, 0, 1000)) refuse("bad_play", "strengthPct isn't a number from 0 to 1000.", "strengthPct", "Give how strong to play it: 100 is as written.");
241
+ if (playAt.color !== undefined && playAt.color !== null && !(typeof playAt.color === "string" && HEX_COLOR_PATTERN.test(playAt.color))) refuse("bad_play", "color isn't a colour.", "color", 'Give a colour as six hex digits in lowercase, like "#ff7a1a", or leave it out.');
242
+ if (playAt.surfaceKind !== undefined && playAt.surfaceKind !== null && !SURFACE_KINDS.includes(playAt.surfaceKind)) refuse("bad_play", `surfaceKind is ${JSON.stringify(playAt.surfaceKind)}, which isn't a surface kind.`, "surfaceKind", `Give one of: ${SURFACE_KINDS.join(", ")}.`);
243
+ const copy = (vector) => (vector ? { x: vector.x, y: vector.y, z: vector.z } : null);
244
+ return { effectName, positionM: copy(playAt.positionM), direction: copy(playAt.direction), normal: copy(playAt.normal), toPositionM: copy(playAt.toPositionM), scalePct: playAt.scalePct ?? null, strengthPct: playAt.strengthPct ?? null, color: playAt.color ?? null, surfaceKind: playAt.surfaceKind ?? null, seedNumber: 0, startedAtMs: 0, keptId: null };
245
245
  }
246
246
 
247
247
  /** The weather as the wire carries it, from what setWeather was given: only its own fields, each in range. */
248
248
  function weatherOf(values, callName) {
249
249
  if (values === null || typeof values !== "object" || Array.isArray(values)) refuse("bad_weather", `${callName} takes the weather as an object.`, "weather", "Give { rainPct: 60, windSpeedMPerSec: 8 }: only what changes.");
250
250
  const stated = {};
251
- for (const [name, value] of Object.entries(values)) {
251
+ for (const [name, statedValue] of Object.entries(values)) {
252
252
  const field = WEATHER_FIELDS.find((candidate) => candidate.name === name);
253
253
  if (!field) refuse("unknown_argument", `The weather has no "${name}".`, name, `Give only: ${WEATHER_FIELDS.map((candidate) => candidate.name).join(", ")}.`);
254
- if (!isNumberIn(value, 0, field.maximumNumber)) refuse("bad_weather", `${name} isn't a number from 0 to ${field.maximumNumber}.`, name, `Give a number from 0 to ${field.maximumNumber}.`);
255
- stated[field.statedName] = value;
254
+ if (!isNumberIn(statedValue, 0, field.maximumNumber)) refuse("bad_weather", `${name} isn't a number from 0 to ${field.maximumNumber}.`, name, `Give a number from 0 to ${field.maximumNumber}.`);
255
+ stated[field.statedName] = statedValue;
256
256
  }
257
257
  return stated;
258
258
  }
@@ -308,9 +308,9 @@ export function effectsKindFrom(defineKind, given = {}) {
308
308
  function show(play, entry) {
309
309
  if (!effects) return null;
310
310
  const start = () => {
311
- const at = { positionM: play.positionM, seedNumber: play.seedNumber, startedAgoSec: Math.max(kindContext.serverTimeMs() - play.startedAtMs, 0) / 1000 };
312
- for (const field of ["direction", "normal", "toPositionM", "scalePct", "strengthPct", "color", "surfaceKind"]) if (play[field] !== null) at[field] = play[field];
313
- return effects.playShared(play.effectName, at);
311
+ const playAt = { positionM: play.positionM, seedNumber: play.seedNumber, startedAgoSec: Math.max(kindContext.serverTimeMs() - play.startedAtMs, 0) / 1000 };
312
+ for (const field of ["direction", "normal", "toPositionM", "scalePct", "strengthPct", "color", "surfaceKind"]) if (play[field] !== null) playAt[field] = play[field];
313
+ return effects.playShared(play.effectName, playAt);
314
314
  };
315
315
  if (effects.has(play.effectName)) return start();
316
316
  // An effect this game hasn't loaded yet: fetched, then played from where the room's clock has it by then.
@@ -341,7 +341,7 @@ export function effectsKindFrom(defineKind, given = {}) {
341
341
  const values = weatherSettings(stated);
342
342
  const restated = weatherOf(values, "setWeather");
343
343
  // The same weather again (everything kept, told to a newcomer) changes nothing.
344
- if (Object.entries(restated).every(([statedName, value]) => weather?.[statedName] === value)) return;
344
+ if (Object.entries(restated).every(([statedName, statedValue]) => weather?.[statedName] === statedValue)) return;
345
345
  weather = { ...(weather ?? {}), ...restated };
346
346
  effects?.settings.set(values);
347
347
  }
@@ -351,9 +351,9 @@ export function effectsKindFrom(defineKind, given = {}) {
351
351
  kindContext.onBytes((bytes, fromPlayerId) => {
352
352
  // With a game server, the server held the sender to its rate; on the relay, each game does.
353
353
  if (isOnHost && fromPlayerId !== null && !cap.take(fromPlayerId, kindContext.serverTimeMs())) return;
354
- const stoppedId = decodeStop(bytes);
355
- if (stoppedId) {
356
- drop(stoppedId);
354
+ const stoppedKeptNumber = decodeStop(bytes);
355
+ if (stoppedKeptNumber) {
356
+ drop(stoppedKeptNumber);
357
357
  return;
358
358
  }
359
359
  const play = decodePlay(bytes);
@@ -387,9 +387,9 @@ export function effectsKindFrom(defineKind, given = {}) {
387
387
  weather = null;
388
388
  });
389
389
 
390
- function share(effectName, at, isKept, callName) {
390
+ function share(effectName, playAt, isKept, callName) {
391
391
  if (!effects) refuse("effects_not_attached", `${callName} was called before the game's effects were handed over.`, null, "Call attach(effects) first, with what createEffects answered.");
392
- const play = playOf(effectName, at ?? {}, effectNames, callName);
392
+ const play = playOf(effectName, playAt ?? {}, effectNames, callName);
393
393
  play.seedNumber = newSeed();
394
394
  play.startedAtMs = kindContext.serverTimeMs();
395
395
  if (isKept) {
@@ -411,13 +411,13 @@ export function effectsKindFrom(defineKind, given = {}) {
411
411
  if (weather) effects.settings.set(weatherSettings(weather));
412
412
  },
413
413
  /** Plays an effect here and in every other player's game, the same in each. at: { positionM, direction, normal, toPositionM, scalePct, strengthPct, color, surfaceKind }. Answers this game's handle. */
414
- play(effectName, at) {
415
- const { play } = share(effectName, at, false, "play");
414
+ play(effectName, playAt) {
415
+ const { play } = share(effectName, playAt, false, "play");
416
416
  return show(play, null);
417
417
  },
418
418
  /** Starts an effect that keeps playing for everyone, a late joiner too, until its handle's stop(). */
419
- start(effectName, at) {
420
- const { play, bytes } = share(effectName, at, true, "start");
419
+ start(effectName, playAt) {
420
+ const { play, bytes } = share(effectName, playAt, true, "start");
421
421
  keep(play, bytes);
422
422
  const { keptId } = play;
423
423
  return {
@@ -460,10 +460,10 @@ export function effectsKindFrom(defineKind, given = {}) {
460
460
  onBytes(kindState, player, bytes, kindContext) {
461
461
  const nowMs = kindContext.serverTimeMs();
462
462
  if (!capOf(kindState).take(player.id, nowMs)) return;
463
- const stoppedId = decodeStop(bytes);
464
- if (stoppedId) {
465
- if (!(stoppedId in kindState.kept)) return;
466
- delete kindState.kept[stoppedId];
463
+ const stoppedKeptNumber = decodeStop(bytes);
464
+ if (stoppedKeptNumber) {
465
+ if (!(stoppedKeptNumber in kindState.kept)) return;
466
+ delete kindState.kept[stoppedKeptNumber];
467
467
  kindContext.broadcastBytes(bytes, { exceptPlayerId: player.id });
468
468
  return;
469
469
  }
@@ -483,8 +483,8 @@ export function effectsKindFrom(defineKind, given = {}) {
483
483
  kindContext.broadcastBytes(passed, { exceptPlayerId: player.id });
484
484
  },
485
485
  api: (kindState, kindContext) => {
486
- const send = (effectName, at, isKept, callName) => {
487
- const play = playOf(effectName, at ?? {}, effectNames, callName);
486
+ const send = (effectName, playAt, isKept, callName) => {
487
+ const play = playOf(effectName, playAt ?? {}, effectNames, callName);
488
488
  play.seedNumber = newSeed();
489
489
  play.startedAtMs = kindContext.serverTimeMs();
490
490
  if (isKept) {
@@ -499,11 +499,11 @@ export function effectsKindFrom(defineKind, given = {}) {
499
499
  };
500
500
  return {
501
501
  /** Plays an effect in every player's game: an explosion the server decided. */
502
- play(effectName, at) {
503
- send(effectName, at, false, "play");
502
+ play(effectName, playAt) {
503
+ send(effectName, playAt, false, "play");
504
504
  },
505
505
  /** Starts an effect that keeps playing for everyone, a late joiner too. Answers its keptId, for stop. */
506
- start: (effectName, at) => send(effectName, at, true, "start"),
506
+ start: (effectName, playAt) => send(effectName, playAt, true, "start"),
507
507
  /** Stops an effect that keeps playing. Answers whether it was playing. */
508
508
  stop(keptId) {
509
509
  if (!(keptId in kindState.kept)) return false;