@zaisma/effects 1.0.0 → 1.1.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/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.0",
5
+ "piece_version": "1.1.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.0",
6
+ "piece_version": "1.1.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;
package/tables.js CHANGED
@@ -20,7 +20,7 @@ export function createTable(texelCount, rowCount) {
20
20
  texture.generateMipmaps = false;
21
21
  texture.needsUpdate = true;
22
22
  const freeRows = [];
23
- for (let row = rowCount - 1; row >= 0; row -= 1) freeRows.push(row);
23
+ for (let rowIndex = rowCount - 1; rowIndex >= 0; rowIndex -= 1) freeRows.push(rowIndex);
24
24
 
25
25
  return {
26
26
  texture,
@@ -37,11 +37,11 @@ export function createTable(texelCount, rowCount) {
37
37
  },
38
38
  /** Writes one texel of a row. */
39
39
  write(row, texelIndex, first, second, third, fourth) {
40
- const at = (row * texelCount + texelIndex) * 4;
41
- values[at] = first;
42
- values[at + 1] = second;
43
- values[at + 2] = third;
44
- values[at + 3] = fourth;
40
+ const floatIndex = (row * texelCount + texelIndex) * 4;
41
+ values[floatIndex] = first;
42
+ values[floatIndex + 1] = second;
43
+ values[floatIndex + 2] = third;
44
+ values[floatIndex + 3] = fourth;
45
45
  texture.needsUpdate = true;
46
46
  },
47
47
  dispose() {
@@ -66,32 +66,32 @@ export function createSlotPool(slotCount) {
66
66
  return freeCount;
67
67
  },
68
68
  /** The start of a free run of `count` slots (the first that fits), or -1. */
69
- take(count) {
69
+ take(wantedCount) {
70
70
  for (let index = 0; index < freeRuns.length; index += 1) {
71
71
  const run = freeRuns[index];
72
- if (run.count < count) continue;
73
- const start = run.start;
74
- run.start += count;
75
- run.count -= count;
72
+ if (run.count < wantedCount) continue;
73
+ const startIndex = run.start;
74
+ run.start += wantedCount;
75
+ run.count -= wantedCount;
76
76
  if (run.count === 0) freeRuns.splice(index, 1);
77
- freeCount -= count;
78
- return start;
77
+ freeCount -= wantedCount;
78
+ return startIndex;
79
79
  }
80
80
  return -1;
81
81
  },
82
82
  /** The longest free run's length: the most one emitter can still take. */
83
83
  longestFreeCount() {
84
- let longest = 0;
85
- for (const run of freeRuns) if (run.count > longest) longest = run.count;
86
- return longest;
84
+ let longestCount = 0;
85
+ for (const run of freeRuns) if (run.count > longestCount) longestCount = run.count;
86
+ return longestCount;
87
87
  },
88
- give(start, count) {
88
+ give(start, givenCount) {
89
89
  let index = 0;
90
90
  while (index < freeRuns.length && freeRuns[index].start < start) index += 1;
91
- freeRuns.splice(index, 0, { start, count });
92
- freeCount += count;
91
+ freeRuns.splice(index, 0, { start, count: givenCount });
92
+ freeCount += givenCount;
93
93
  const next = freeRuns[index + 1];
94
- if (next && start + count === next.start) {
94
+ if (next && start + givenCount === next.start) {
95
95
  freeRuns[index].count += next.count;
96
96
  freeRuns.splice(index + 1, 1);
97
97
  }
package/weather.js CHANGED
@@ -91,7 +91,7 @@ void main() {
91
91
  }`;
92
92
 
93
93
  const easeToward = (current, target, share, snap) => (Math.abs(target - current) <= snap ? target : current + (target - current) * share);
94
- const wrapInto = (value, size) => ((value % size) + size) % size;
94
+ const wrapInto = (positionM, sizeM) => ((positionM % sizeM) + sizeM) % sizeM;
95
95
 
96
96
  /**
97
97
  * Makes the weather. Its layers are elements like any effect's: index.js gives each a row and writes it in the look
@@ -212,16 +212,16 @@ export function createWeather({ sprites, sharedUniforms, scene, renderer, playRo
212
212
  */
213
213
  function update({ nowSec, frameSec, camera, target, changeDurationSec, groundHeightM, hasShelter }) {
214
214
  // What's set as the effects are made is the weather at once; later changes are eased to.
215
- const share = hasStarted && changeDurationSec > 0 ? Math.min((frameSec * 3) / changeDurationSec, 1) : 1;
215
+ const shareNumber = hasStarted && changeDurationSec > 0 ? Math.min((frameSec * 3) / changeDurationSec, 1) : 1;
216
216
  hasStarted = true;
217
- now.rainPct = easeToward(now.rainPct, target.rainPct, share, 0.5);
218
- now.snowPct = easeToward(now.snowPct, target.snowPct, share, 0.5);
219
- now.fogPct = easeToward(now.fogPct, target.fogPct, share, 0.5);
220
- now.windSpeedMPerSec = easeToward(now.windSpeedMPerSec, target.windSpeedMPerSec, share, 0.05);
221
- now.windGustPct = easeToward(now.windGustPct, target.windGustPct, share, 0.5);
217
+ now.rainPct = easeToward(now.rainPct, target.rainPct, shareNumber, 0.5);
218
+ now.snowPct = easeToward(now.snowPct, target.snowPct, shareNumber, 0.5);
219
+ now.fogPct = easeToward(now.fogPct, target.fogPct, shareNumber, 0.5);
220
+ now.windSpeedMPerSec = easeToward(now.windSpeedMPerSec, target.windSpeedMPerSec, shareNumber, 0.05);
221
+ now.windGustPct = easeToward(now.windGustPct, target.windGustPct, shareNumber, 0.5);
222
222
  // The wind turns the short way round.
223
223
  const turnDeg = ((((target.windDirectionDeg - now.windDirectionDeg) % 360) + 540) % 360) - 180;
224
- now.windDirectionDeg = Math.abs(turnDeg) <= 0.5 ? target.windDirectionDeg : wrapInto(now.windDirectionDeg + turnDeg * share, 360);
224
+ now.windDirectionDeg = Math.abs(turnDeg) <= 0.5 ? target.windDirectionDeg : wrapInto(now.windDirectionDeg + turnDeg * shareNumber, 360);
225
225
  now.isSettled = now.rainPct === target.rainPct && now.snowPct === target.snowPct && now.fogPct === target.fogPct && now.windSpeedMPerSec === target.windSpeedMPerSec && now.windGustPct === target.windGustPct && now.windDirectionDeg === target.windDirectionDeg;
226
226
 
227
227
  windAt(now, nowSec, windM);
@@ -231,29 +231,29 @@ export function createWeather({ sprites, sharedUniforms, scene, renderer, playRo
231
231
  camera.getWorldPosition(cameraPlace);
232
232
  camera.getWorldDirection(cameraWay);
233
233
  for (const layer of layers) {
234
- const layerShare = shareOf(layer);
235
- if (layerShare <= 0) {
234
+ const layerShareNumber = shareOf(layer);
235
+ if (layerShareNumber <= 0) {
236
236
  dropLayer(layer);
237
237
  continue;
238
238
  }
239
239
  fillLayer(layer);
240
- const [width, height, depth] = layer.boxM;
240
+ const [widthM, heightM, depthM] = layer.boxM;
241
241
  const index = layer.layerIndex;
242
- sharedUniforms.uWrapShare.value[index] = layerShare;
243
- sharedUniforms.uWrapBox.value[index].set(width, height, depth);
242
+ sharedUniforms.uWrapShare.value[index] = layerShareNumber;
243
+ sharedUniforms.uWrapBox.value[index].set(widthM, heightM, depthM);
244
244
  if (index === LANDING_LAYER_INDEX) {
245
- sharedUniforms.uWrapCenter.value[index].set(cameraPlace.x + cameraWay.x * width * 0.3, cameraPlace.y, cameraPlace.z + cameraWay.z * depth * 0.3);
245
+ sharedUniforms.uWrapCenter.value[index].set(cameraPlace.x + cameraWay.x * widthM * 0.3, cameraPlace.y, cameraPlace.z + cameraWay.z * depthM * 0.3);
246
246
  sharedUniforms.uWrapVelocity.value[index].set(LANDINGS_PER_SEC, 0, 0);
247
247
  continue;
248
248
  }
249
249
  const velocityX = windM.x * layer.windShare;
250
250
  const velocityZ = windM.z * layer.windShare;
251
- layer.shift.set(wrapInto(layer.shift.x + velocityX * frameSec, width), wrapInto(layer.shift.y - layer.fallMPerSec * frameSec, height), wrapInto(layer.shift.z + velocityZ * frameSec, depth));
251
+ layer.shift.set(wrapInto(layer.shift.x + velocityX * frameSec, widthM), wrapInto(layer.shift.y - layer.fallMPerSec * frameSec, heightM), wrapInto(layer.shift.z + velocityZ * frameSec, depthM));
252
252
  sharedUniforms.uWrapShift.value[index].copy(layer.shift);
253
253
  sharedUniforms.uWrapVelocity.value[index].set(velocityX, -layer.fallMPerSec, velocityZ);
254
254
  // The box sits a little ahead of the camera, so more of it is in view; fog lies on the ground.
255
- const centerY = layer.layerName === "fog" ? groundHeightM + height * 0.5 : cameraPlace.y + height * 0.2;
256
- sharedUniforms.uWrapCenter.value[index].set(cameraPlace.x + cameraWay.x * width * 0.25, centerY, cameraPlace.z + cameraWay.z * depth * 0.25);
255
+ const centerY = layer.layerName === "fog" ? groundHeightM + heightM * 0.5 : cameraPlace.y + heightM * 0.2;
256
+ sharedUniforms.uWrapCenter.value[index].set(cameraPlace.x + cameraWay.x * widthM * 0.25, centerY, cameraPlace.z + cameraWay.z * depthM * 0.25);
257
257
  }
258
258
 
259
259
  const isFalling = now.rainPct > 0 || now.snowPct > 0;