ravensight-playtest 0.13.3 → 0.16.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/README.md +67 -1
- package/addons/ravensight_driver/driver.gd +472 -28
- package/docs/godot-driver.md +66 -2
- package/package.json +4 -4
- package/src/api/README.md +8 -1
- package/src/api/client.js +24 -1
- package/src/cli.js +14 -0
- package/src/commands/check.js +5 -2
- package/src/commands/run.js +14 -0
- package/src/commands/setup.js +176 -0
- package/src/packs/index.js +37 -5
- package/src/packs/keys.json +11 -0
- package/src/packs/pack-signing-hashes.json +7 -0
- package/src/packs/pack-signing-vectors.json +119 -0
- package/src/packs/signature.js +186 -0
- package/src/run/anygame/gamenotes.js +74 -12
- package/src/run/anygame/index.js +97 -34
- package/src/run/anygame/language.js +100 -10
- package/src/run/anygame/memory.js +70 -13
- package/src/run/anygame/shared-hashes.json +7 -7
- package/src/run/anygame/sound.js +59 -16
- package/src/run/args.js +2 -0
- package/src/run/claimwords.js +43 -7
- package/src/run/drivers/godot-cursor.js +96 -0
- package/src/run/drivers/godot-observation.js +29 -5
- package/src/run/drivers/godot.js +60 -5
- package/src/run/drivers/web.js +111 -8
- package/src/run/eventclaims.js +102 -6
- package/src/run/events.js +64 -6
- package/src/run/gameMemory.js +14 -5
- package/src/run/honesty.js +49 -2
- package/src/run/judge.js +22 -10
- package/src/run/liveFeed.js +254 -0
- package/src/run/personaLoop.js +70 -20
- package/src/run/pixels.js +491 -2
- package/src/run/replay.js +11 -0
- package/src/run/tools.js +2 -2
package/src/run/pixels.js
CHANGED
|
@@ -128,14 +128,17 @@ export function pngFingerprint(png) {
|
|
|
128
128
|
* pixels and the changed blocks' bounding box, or null when the two cannot be
|
|
129
129
|
* compared (different sizes).
|
|
130
130
|
*/
|
|
131
|
-
export function screenDelta(a, b, tolerance = BLOCK_TOLERANCE) {
|
|
131
|
+
export function screenDelta(a, b, tolerance = BLOCK_TOLERANCE, ignore = null) {
|
|
132
132
|
if (!a || !b || a.cols !== b.cols || a.rows !== b.rows || a.block !== b.block) return null;
|
|
133
|
+
// `ignore`: blocks left out of the comparison (what moves by itself, motionBlocks below).
|
|
134
|
+
const skip = ignore && ignore.length === a.cols * a.rows ? ignore : null;
|
|
133
135
|
let n = 0;
|
|
134
136
|
let x0 = Infinity;
|
|
135
137
|
let y0 = Infinity;
|
|
136
138
|
let x1 = -1;
|
|
137
139
|
let y1 = -1;
|
|
138
140
|
for (let c = 0; c < a.cols * a.rows; c += 1) {
|
|
141
|
+
if (skip && skip[c]) continue;
|
|
139
142
|
const i = c * 3;
|
|
140
143
|
if (Math.abs(a.rgb[i] - b.rgb[i]) <= tolerance && Math.abs(a.rgb[i + 1] - b.rgb[i + 1]) <= tolerance && Math.abs(a.rgb[i + 2] - b.rgb[i + 2]) <= tolerance) continue;
|
|
141
144
|
n += 1;
|
|
@@ -156,6 +159,492 @@ export function significant(delta) {
|
|
|
156
159
|
return delta.area >= MIN_CHANGE_AREA_PX || (delta.width >= MIN_CHANGE_SIDE_PX && delta.height >= MIN_CHANGE_SIDE_PX);
|
|
157
160
|
}
|
|
158
161
|
|
|
162
|
+
// ------------------------------------------------------------ the bracket
|
|
163
|
+
//
|
|
164
|
+
// Ported from the cloud runner (the platform repo, infrastructure/hosted-playtest/direct-runner/lib/pixels.mjs, and
|
|
165
|
+
// docs/playtest/MOVING_SCREENS.md there). Something that moves by itself shows in a before and after comparison only
|
|
166
|
+
// where it was at the start and where it is at the end, so what to leave out is what moves by itself at the before
|
|
167
|
+
// frame (the previous input's after sample: game time stands still between inputs) and at the after frame (a short
|
|
168
|
+
// sample with no input once the input has played).
|
|
169
|
+
|
|
170
|
+
/** Game time the after sample runs with no input. */
|
|
171
|
+
export const BRACKET_MS = 120;
|
|
172
|
+
/** A solid shape that moves a little changes only at its two ends: gaps between moved blocks up to this are filled. */
|
|
173
|
+
export const MOTION_GAP_BLOCKS = 32;
|
|
174
|
+
/** A mask that would hide more than this share of the screen is not used: too little would be left to judge by. */
|
|
175
|
+
export const MASK_MAX_SHARE = 0.45;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The blocks that moved between two frames taken with no input between them, gaps on a row or a column filled up to
|
|
179
|
+
* `gap` blocks, grown by `grow`: an all-zero mask when nothing moved, null when the frames cannot be compared or
|
|
180
|
+
* more than half the screen moved (a full scroll).
|
|
181
|
+
*
|
|
182
|
+
* @returns {Uint8Array|null}
|
|
183
|
+
*/
|
|
184
|
+
export function motionBlocks(a, b, { tolerance = BLOCK_TOLERANCE, grow = 2, gap = MOTION_GAP_BLOCKS } = {}) {
|
|
185
|
+
if (!a || !b || a.cols !== b.cols || a.rows !== b.rows || a.block !== b.block) return null;
|
|
186
|
+
const { cols, rows } = a;
|
|
187
|
+
const moved = movedBlocks(a, b, tolerance);
|
|
188
|
+
if (moved.any > 0.5 * cols * rows) return null;
|
|
189
|
+
if (!moved.any) return moved.mask;
|
|
190
|
+
return growMask(fillGaps(moved.mask, cols, rows, gap), cols, rows, grow);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The blocks that differ between two fingerprints of one size: `{ mask, any }`. */
|
|
194
|
+
function movedBlocks(a, b, tolerance) {
|
|
195
|
+
const n = a.cols * a.rows, mask = new Uint8Array(n);
|
|
196
|
+
let any = 0;
|
|
197
|
+
for (let c = 0; c < n; c++) {
|
|
198
|
+
const i = c * 3;
|
|
199
|
+
if (Math.abs(a.rgb[i] - b.rgb[i]) > tolerance || Math.abs(a.rgb[i + 1] - b.rgb[i + 1]) > tolerance || Math.abs(a.rgb[i + 2] - b.rgb[i + 2]) > tolerance) { mask[c] = 1; any++; }
|
|
200
|
+
}
|
|
201
|
+
return { mask, any };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A block mask with the gaps between set blocks on one row or column (`axis` 'row' or 'col': only those) filled, up to `gap` blocks. */
|
|
205
|
+
function fillGaps(moved, cols, rows, gap, axis = 'both') {
|
|
206
|
+
const filled = new Uint8Array(moved);
|
|
207
|
+
const fill = (n, at) => {
|
|
208
|
+
let last = -1;
|
|
209
|
+
for (let i = 0; i < n; i++) {
|
|
210
|
+
if (!moved[at(i)]) continue;
|
|
211
|
+
if (last >= 0 && i - last > 1 && i - last - 1 <= gap) for (let k = last + 1; k < i; k++) filled[at(k)] = 1;
|
|
212
|
+
last = i;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
if (axis !== 'col') for (let y = 0; y < rows; y++) fill(cols, i => y * cols + i);
|
|
216
|
+
if (axis !== 'row') for (let x = 0; x < cols; x++) fill(rows, i => i * cols + x);
|
|
217
|
+
return filled;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** A block mask grown by `grow` blocks each way. */
|
|
221
|
+
function growMask(filled, cols, rows, grow) {
|
|
222
|
+
const mask = new Uint8Array(cols * rows);
|
|
223
|
+
for (let c = 0; c < cols * rows; c++) {
|
|
224
|
+
if (!filled[c]) continue;
|
|
225
|
+
const x = c % cols, y = (c / cols) | 0;
|
|
226
|
+
for (let dy = -grow; dy <= grow; dy++) for (let dx = -grow; dx <= grow; dx++) {
|
|
227
|
+
const nx = x + dx, ny = y + dy;
|
|
228
|
+
if (nx >= 0 && ny >= 0 && nx < cols && ny < rows) mask[ny * cols + nx] = 1;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return mask;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The mask a bracket judges by: every mask given together (null ones left out), with `outside` (what a scroll brought
|
|
236
|
+
* into view) masked but not counted. Null when there is nothing to judge by: no mask, masks of another size, or
|
|
237
|
+
* together more than MASK_MAX_SHARE of the rest of the screen.
|
|
238
|
+
*
|
|
239
|
+
* @returns {Uint8Array|null}
|
|
240
|
+
*/
|
|
241
|
+
export function bracketMask(masks, { outside = null } = {}) {
|
|
242
|
+
const list = (masks || []).filter(Boolean);
|
|
243
|
+
if (list.length === 0 || list.some(m => m.length !== list[0].length)) return null;
|
|
244
|
+
const out = new Uint8Array(list[0].length);
|
|
245
|
+
for (const m of list) for (let i = 0; i < out.length; i += 1) out[i] |= m[i];
|
|
246
|
+
// `outside`: blocks with nothing to judge by (what a scroll brought into view), masked and left out of the share.
|
|
247
|
+
if (outside && outside.length !== out.length) return null;
|
|
248
|
+
let n = 0;
|
|
249
|
+
let total = out.length;
|
|
250
|
+
for (let i = 0; i < out.length; i += 1) {
|
|
251
|
+
if (outside && outside[i]) {
|
|
252
|
+
out[i] = 1;
|
|
253
|
+
total -= 1;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
n += out[i];
|
|
257
|
+
}
|
|
258
|
+
return total > 0 && n <= MASK_MAX_SHARE * total ? out : null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The verdict on one input from the plain comparison (`old`) and the bracket's (`bracket`, null when there was
|
|
263
|
+
* none), as the cloud runner gives it. The bracket may call an input a change the old check missed; it may call one
|
|
264
|
+
* nothing only when the player was found on both frames (`player`), which this runner never knows, since a player
|
|
265
|
+
* that bobs or drifts by itself is masked too. Otherwise the input stays a change and `selfOnly` says only what moves
|
|
266
|
+
* by itself changed.
|
|
267
|
+
*
|
|
268
|
+
* @returns {{changed: boolean, flipped: 'changed'|'none'|null, selfOnly?: boolean}}
|
|
269
|
+
*/
|
|
270
|
+
export function bracketVerdict(old, bracket, { player = false } = {}) {
|
|
271
|
+
if (bracket === null || bracket === undefined || Boolean(old) === Boolean(bracket)) return { changed: Boolean(old), flipped: null };
|
|
272
|
+
if (bracket) return { changed: true, flipped: 'changed' };
|
|
273
|
+
return player ? { changed: false, flipped: 'none' } : { changed: true, flipped: null, selfOnly: true };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ------------------------------------------------------------ shapes that leave the screen
|
|
277
|
+
//
|
|
278
|
+
// A shape sliding off the edge during an input changes, in the sample before it, only at its trailing end: its middle
|
|
279
|
+
// is the same colour on both frames and the gap fill has no far end to fill to (the far end is off the screen). Its
|
|
280
|
+
// middle at the before frame then reads as the input's change when the shape has gone by the after frame. So each
|
|
281
|
+
// moving region's motion vector is found, and a region the vector carries to the edge during the input is swept along
|
|
282
|
+
// it to the edge (the platform repo's docs/playtest/MOVING_SCREENS.md).
|
|
283
|
+
|
|
284
|
+
// How far a region may move between the two frames of one sample, in blocks either way (12 blocks in 120 ms: 400 px a second).
|
|
285
|
+
export const MOVE_SEARCH_BLOCKS = 12;
|
|
286
|
+
// At most this many regions get a vector, the largest first.
|
|
287
|
+
export const MOVE_MAX_REGIONS = 24;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The moving regions between two frames taken with no input between them, each with its motion vector:
|
|
291
|
+
* `{ cols, rows, ms, list: [{ x0, y0, x1, y1, vx, vy }] }`, the box in blocks and the vector in blocks a millisecond
|
|
292
|
+
* over `ms`. A region whose move cannot be told from flicker (no shift matches it much better than none) has no entry.
|
|
293
|
+
*/
|
|
294
|
+
export function motionMoves(a, b, { ms = BRACKET_MS, tolerance = BLOCK_TOLERANCE, gap = MOTION_GAP_BLOCKS, search = MOVE_SEARCH_BLOCKS } = {}) {
|
|
295
|
+
if (!a || !b || a.cols !== b.cols || a.rows !== b.rows || a.block !== b.block || !(ms > 0)) return null;
|
|
296
|
+
const { cols, rows } = a;
|
|
297
|
+
const moved = movedBlocks(a, b, tolerance);
|
|
298
|
+
if (!moved.any || moved.any > 0.5 * cols * rows) return { cols, rows, ms, list: [] };
|
|
299
|
+
const filled = fillGaps(moved.mask, cols, rows, gap);
|
|
300
|
+
const seen = new Uint8Array(cols * rows), regions = [];
|
|
301
|
+
for (let c = 0; c < cols * rows; c++) {
|
|
302
|
+
if (!filled[c] || seen[c]) continue;
|
|
303
|
+
const r = { x0: cols, y0: rows, x1: -1, y1: -1, n: 0 }, stack = [c];
|
|
304
|
+
seen[c] = 1;
|
|
305
|
+
while (stack.length) {
|
|
306
|
+
const k = stack.pop(), x = k % cols, y = (k / cols) | 0;
|
|
307
|
+
r.n++; if (x < r.x0) r.x0 = x; if (x > r.x1) r.x1 = x; if (y < r.y0) r.y0 = y; if (y > r.y1) r.y1 = y;
|
|
308
|
+
for (const [nx, ny] of [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]) {
|
|
309
|
+
if (nx < 0 || ny < 0 || nx >= cols || ny >= rows) continue;
|
|
310
|
+
const j = ny * cols + nx;
|
|
311
|
+
if (filled[j] && !seen[j]) { seen[j] = 1; stack.push(j); }
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (r.n >= 2) regions.push(r);
|
|
315
|
+
}
|
|
316
|
+
regions.sort((p, q) => q.n - p.n);
|
|
317
|
+
const list = [];
|
|
318
|
+
for (const r of regions.slice(0, MOVE_MAX_REGIONS)) {
|
|
319
|
+
// The region's box with a margin, matched against the second frame at each shift: the shift that matches best is its move.
|
|
320
|
+
const bx0 = Math.max(0, r.x0 - 2), by0 = Math.max(0, r.y0 - 2), bx1 = Math.min(cols - 1, r.x1 + 2), by1 = Math.min(rows - 1, r.y1 + 2);
|
|
321
|
+
const area = (bx1 - bx0 + 1) * (by1 - by0 + 1);
|
|
322
|
+
const cost = (dx, dy) => {
|
|
323
|
+
let sum = 0, n = 0;
|
|
324
|
+
for (let y = by0; y <= by1; y++) {
|
|
325
|
+
const ty = y + dy;
|
|
326
|
+
if (ty < 0 || ty >= rows) continue;
|
|
327
|
+
for (let x = bx0; x <= bx1; x++) {
|
|
328
|
+
const tx = x + dx;
|
|
329
|
+
if (tx < 0 || tx >= cols) continue;
|
|
330
|
+
const i = (y * cols + x) * 3, j = (ty * cols + tx) * 3;
|
|
331
|
+
sum += Math.abs(a.rgb[i] - b.rgb[j]) + Math.abs(a.rgb[i + 1] - b.rgb[j + 1]) + Math.abs(a.rgb[i + 2] - b.rgb[j + 2]);
|
|
332
|
+
n++;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return n >= 0.5 * area ? sum / n : Infinity;
|
|
336
|
+
};
|
|
337
|
+
const still = cost(0, 0);
|
|
338
|
+
let best = null;
|
|
339
|
+
for (let dy = -search; dy <= search; dy++) for (let dx = -search; dx <= search; dx++) {
|
|
340
|
+
if (!dx && !dy) continue;
|
|
341
|
+
const v = cost(dx, dy);
|
|
342
|
+
if (!best || v < best.v || (v === best.v && Math.abs(dx) + Math.abs(dy) < Math.abs(best.dx) + Math.abs(best.dy))) best = { v, dx, dy };
|
|
343
|
+
}
|
|
344
|
+
if (best && best.v < 0.5 * still) list.push({ x0: r.x0, y0: r.y0, x1: r.x1, y1: r.y1, vx: best.dx / ms, vy: best.dy / ms });
|
|
345
|
+
}
|
|
346
|
+
return { cols, rows, ms, list };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* A mask with every moving region (`moves`, from motionMoves) that its vector carries to the edge of the screen within
|
|
351
|
+
* `ms` swept along that vector to the edge, `dir` 1 forward (a shape seen at the before frame, leaving) or -1 back (a
|
|
352
|
+
* shape seen at the after frame, that came in), grown by `grow`. Regions that stay on the screen are left as they are.
|
|
353
|
+
* Answers `mask` itself when nothing is swept.
|
|
354
|
+
*/
|
|
355
|
+
export function sweepMask(mask, moves, ms, { dir = 1, grow = 2 } = {}) {
|
|
356
|
+
if (!mask || !moves || !moves.list?.length || !(ms > 0) || mask.length !== moves.cols * moves.rows) return mask;
|
|
357
|
+
const { cols, rows } = moves;
|
|
358
|
+
let out = null;
|
|
359
|
+
for (const m of moves.list) {
|
|
360
|
+
const tx = m.vx * ms * dir, ty = m.vy * ms * dir;
|
|
361
|
+
const reaches = m.x0 + tx <= 0 || m.x1 + tx >= cols - 1 || m.y0 + ty <= 0 || m.y1 + ty >= rows - 1;
|
|
362
|
+
if (!reaches) continue;
|
|
363
|
+
if (!out) out = new Uint8Array(mask);
|
|
364
|
+
const steps = Math.ceil(Math.max(Math.abs(tx), Math.abs(ty)));
|
|
365
|
+
for (let k = 0; k <= steps; k++) {
|
|
366
|
+
const ox = Math.round(tx * k / steps), oy = Math.round(ty * k / steps);
|
|
367
|
+
const x0 = Math.max(0, m.x0 + ox - grow), x1 = Math.min(cols - 1, m.x1 + ox + grow), y0 = Math.max(0, m.y0 + oy - grow), y1 = Math.min(rows - 1, m.y1 + oy + grow);
|
|
368
|
+
for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) out[y * cols + x] = 1;
|
|
369
|
+
// Off the screen already: nothing further to mark.
|
|
370
|
+
if (m.x1 + ox < 0 || m.x0 + ox >= cols || m.y1 + oy < 0 || m.y0 + oy >= rows) break;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return out || mask;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ------------------------------------------------------------ whole-screen scroll
|
|
377
|
+
//
|
|
378
|
+
// A side scroller's camera, an auto-runner, a scrolling background: more than half the screen moves by itself, so
|
|
379
|
+
// motionBlocks gives no mask and every input read as a change. The scroll offset between two frames is found by a cheap
|
|
380
|
+
// global alignment (column and row projections of a downscaled grey frame, then a check at full size), and the frames
|
|
381
|
+
// are compared in two layers: a pixel is unchanged when it matches the earlier frame either where it is (the parts
|
|
382
|
+
// fixed on the screen: the player of an auto-runner, the score) or shifted by the scroll (the world). What is left (the
|
|
383
|
+
// residual) is what moved against both: a jump, a bird, the input's own effect (the platform repo's docs/playtest/MOVING_SCREENS.md).
|
|
384
|
+
|
|
385
|
+
// The most a scroll may move between two frames, as a share of the screen's side.
|
|
386
|
+
export const SCROLL_MAX_SHARE = 0.34;
|
|
387
|
+
// Of the pixels both frames show, at least this share must match once shifted; unshifted, at least SCROLL_MIN_MOVED must
|
|
388
|
+
// not match, and the shift must leave at most SCROLL_LEFT of those unmatched (a scene of close shades matches a lot
|
|
389
|
+
// unshifted too, so a fixed gain would refuse it).
|
|
390
|
+
export const SCROLL_MIN_MATCH = 0.7;
|
|
391
|
+
export const SCROLL_MIN_MOVED = 0.05;
|
|
392
|
+
export const SCROLL_LEFT = 0.25;
|
|
393
|
+
// Per channel, how far two pixels may differ and still be the same.
|
|
394
|
+
export const PIXEL_TOLERANCE = 24;
|
|
395
|
+
// A block of the residual counts when at least this many of its pixels are left (of 16).
|
|
396
|
+
export const RESIDUAL_MIN_PIXELS = 3;
|
|
397
|
+
// The scroll during an input must be what the screen's own scroll rate predicts, within this share or this many px.
|
|
398
|
+
export const SCROLL_RATE_SLACK = 0.35;
|
|
399
|
+
export const SCROLL_RATE_SLACK_PX = 16;
|
|
400
|
+
|
|
401
|
+
const SCROLL_FACTOR = 4, SCROLL_SAMPLE = 3;
|
|
402
|
+
|
|
403
|
+
function greyScaled(img, f) {
|
|
404
|
+
const w = Math.floor(img.width / f), h = Math.floor(img.height / f), g = new Float32Array(w * h);
|
|
405
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
|
406
|
+
let sum = 0;
|
|
407
|
+
for (let yy = 0; yy < f; yy++) for (let xx = 0; xx < f; xx++) {
|
|
408
|
+
const i = (((y * f + yy) * img.width) + x * f + xx) * img.channels;
|
|
409
|
+
sum += 0.299 * img.data[i] + 0.587 * img.data[i + 1] + 0.114 * img.data[i + 2];
|
|
410
|
+
}
|
|
411
|
+
g[y * w + x] = sum / (f * f);
|
|
412
|
+
}
|
|
413
|
+
return { w, h, g };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The shifts of `pb` against `pa` (pb[i] about pa[i - s]) that match best, up to `max` either way, best first. */
|
|
417
|
+
function projectionShifts(pa, pb, max, keep = 3) {
|
|
418
|
+
const n = pa.length, out = [];
|
|
419
|
+
for (let s = -max; s <= max; s++) {
|
|
420
|
+
let sum = 0, k = 0;
|
|
421
|
+
for (let i = Math.max(0, s); i < Math.min(n, n + s); i++) { sum += Math.abs(pb[i] - pa[i - s]); k++; }
|
|
422
|
+
if (k) out.push({ s, v: sum / k });
|
|
423
|
+
}
|
|
424
|
+
const minima = out.filter((o, i) => (i === 0 || o.v <= out[i - 1].v) && (i === out.length - 1 || o.v <= out[i + 1].v));
|
|
425
|
+
return minima.sort((p, q) => p.v - q.v).slice(0, keep).map(o => o.s);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const near3 = (d, i, e, j, tol) => Math.abs(d[i] - e[j]) <= tol && Math.abs(d[i + 1] - e[j + 1]) <= tol && Math.abs(d[i + 2] - e[j + 2]) <= tol;
|
|
429
|
+
|
|
430
|
+
/** Of the pixels sampled that `b` shows and `a` shows shifted by (dx, dy), the share that match. */
|
|
431
|
+
function shiftMatch(a, b, dx, dy, step = SCROLL_SAMPLE, tol = PIXEL_TOLERANCE) {
|
|
432
|
+
let n = 0, hit = 0;
|
|
433
|
+
for (let y = Math.max(0, dy); y < Math.min(b.height, a.height + dy); y += step) {
|
|
434
|
+
for (let x = Math.max(0, dx); x < Math.min(b.width, a.width + dx); x += step) {
|
|
435
|
+
n++;
|
|
436
|
+
if (near3(b.data, (y * b.width + x) * b.channels, a.data, ((y - dy) * a.width + x - dx) * a.channels, tol)) hit++;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return n ? hit / n : 0;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The scroll between two decoded frames of the same size: `{ dx, dy, match, still }` in pixels, `b` at (x, y) showing
|
|
444
|
+
* what `a` showed at (x - dx, y - dy), or null when nothing scrolled or no shift explains the frames well enough.
|
|
445
|
+
*/
|
|
446
|
+
export function scrollShift(a, b, { maxShare = SCROLL_MAX_SHARE } = {}) {
|
|
447
|
+
if (!a || !b || a.width !== b.width || a.height !== b.height || a.width < 64 || a.height < 64) return null;
|
|
448
|
+
const f = SCROLL_FACTOR, ga = greyScaled(a, f), gb = greyScaled(b, f);
|
|
449
|
+
const proj = (g, cols) => {
|
|
450
|
+
const out = new Float32Array(cols ? g.w : g.h);
|
|
451
|
+
for (let y = 0; y < g.h; y++) for (let x = 0; x < g.w; x++) out[cols ? x : y] += g.g[y * g.w + x];
|
|
452
|
+
for (let i = 0; i < out.length; i++) out[i] /= cols ? g.h : g.w;
|
|
453
|
+
return out;
|
|
454
|
+
};
|
|
455
|
+
const maxX = Math.floor(ga.w * maxShare), maxY = Math.floor(ga.h * maxShare);
|
|
456
|
+
const xs = projectionShifts(proj(ga, true), proj(gb, true), maxX), ys = projectionShifts(proj(ga, false), proj(gb, false), maxY);
|
|
457
|
+
// Each pair of candidates checked on the small grey frames, then the best one refined at full size.
|
|
458
|
+
const coarse = (dx, dy) => {
|
|
459
|
+
let sum = 0, n = 0;
|
|
460
|
+
for (let y = Math.max(0, dy); y < Math.min(gb.h, ga.h + dy); y++) for (let x = Math.max(0, dx); x < Math.min(gb.w, ga.w + dx); x++) { sum += Math.abs(gb.g[y * gb.w + x] - ga.g[(y - dy) * ga.w + x - dx]); n++; }
|
|
461
|
+
return n ? sum / n : Infinity;
|
|
462
|
+
};
|
|
463
|
+
let pick = null;
|
|
464
|
+
for (const dx of xs.length ? xs : [0]) for (const dy of ys.length ? ys : [0]) { const v = coarse(dx, dy); if (!pick || v < pick.v) pick = { dx, dy, v }; }
|
|
465
|
+
if (!pick) return null;
|
|
466
|
+
let best = null;
|
|
467
|
+
for (let ry = -f + 1; ry < f; ry++) for (let rx = -f + 1; rx < f; rx++) {
|
|
468
|
+
const dx = pick.dx * f + rx, dy = pick.dy * f + ry;
|
|
469
|
+
if (Math.abs(dx) > a.width * maxShare || Math.abs(dy) > a.height * maxShare) continue;
|
|
470
|
+
const m = shiftMatch(a, b, dx, dy);
|
|
471
|
+
if (!best || m > best.match) best = { dx, dy, match: m };
|
|
472
|
+
}
|
|
473
|
+
if (!best || (!best.dx && !best.dy)) return null;
|
|
474
|
+
const still = shiftMatch(a, b, 0, 0);
|
|
475
|
+
if (best.match < SCROLL_MIN_MATCH || 1 - still < SCROLL_MIN_MOVED || 1 - best.match > SCROLL_LEFT * (1 - still)) return null;
|
|
476
|
+
return { dx: best.dx, dy: best.dy, match: Math.round(best.match * 1000) / 1000, still: Math.round(still * 1000) / 1000 };
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* `b` against `a` scrolled by `shift`, on the block grid of `fp` (a fingerprint of these frames): `changed`, the blocks
|
|
481
|
+
* where enough pixels match `a` neither where they are nor shifted; `exposed`, the blocks that came into view with the
|
|
482
|
+
* scroll (nothing in `a` to compare them with); `fixed`, the blocks of `b` that stayed where they are on the screen
|
|
483
|
+
* while the world moved past (an auto-runner's player, the score): the world behind them is not seen, so in a later
|
|
484
|
+
* comparison their copy carried with the world is a ghost, not a change. Only a fixed shape's ends are found this way
|
|
485
|
+
* when it is one colour over more than the shift (its middle matches itself shifted); `still`, the blocks where every
|
|
486
|
+
* pixel matches where it is, lets scrollMotionBlocks carry the ends across the middle. (A world pixel of one colour
|
|
487
|
+
* over more than the shift, next to a fixed shape, is found fixed too: what is fixed is only ever masked, never judged.)
|
|
488
|
+
*/
|
|
489
|
+
export function scrollResidual(a, b, shift, fp, { tolerance = PIXEL_TOLERANCE, minPixels = RESIDUAL_MIN_PIXELS } = {}) {
|
|
490
|
+
if (!a || !b || !shift || !fp || a.width !== b.width || a.height !== b.height) return null;
|
|
491
|
+
const { cols, rows } = fp, bw = b.width / cols, bh = b.height / rows;
|
|
492
|
+
const changedN = new Uint16Array(cols * rows), exposedN = new Uint16Array(cols * rows), fixedN = new Uint16Array(cols * rows);
|
|
493
|
+
const hereN = new Uint16Array(cols * rows), totalN = new Uint16Array(cols * rows);
|
|
494
|
+
const { dx, dy } = shift;
|
|
495
|
+
for (let y = 0; y < b.height; y++) {
|
|
496
|
+
const r = Math.min(rows - 1, (y / bh) | 0), sy = y - dy, inY = sy >= 0 && sy < a.height;
|
|
497
|
+
for (let x = 0; x < b.width; x++) {
|
|
498
|
+
const c = r * cols + Math.min(cols - 1, (x / bw) | 0), i = (y * b.width + x) * b.channels, sx = x - dx;
|
|
499
|
+
const here = near3(b.data, i, a.data, (y * a.width + x) * a.channels, tolerance);
|
|
500
|
+
const inside = inY && sx >= 0 && sx < a.width;
|
|
501
|
+
const moved = inside && near3(b.data, i, a.data, (sy * a.width + sx) * a.channels, tolerance);
|
|
502
|
+
if (!inside && !here) exposedN[c]++;
|
|
503
|
+
else if (!here && !moved) changedN[c]++;
|
|
504
|
+
else if (here && !moved && inside) fixedN[c]++;
|
|
505
|
+
if (here) hereN[c]++;
|
|
506
|
+
totalN[c]++;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
const changed = new Uint8Array(cols * rows), exposed = new Uint8Array(cols * rows), fixed = new Uint8Array(cols * rows), still = new Uint8Array(cols * rows);
|
|
510
|
+
for (let c = 0; c < cols * rows; c++) {
|
|
511
|
+
if (hereN[c] === totalN[c]) still[c] = 1;
|
|
512
|
+
if (exposedN[c]) { exposed[c] = 1; continue; }
|
|
513
|
+
if (changedN[c] >= minPixels) changed[c] = 1;
|
|
514
|
+
if (fixedN[c] >= minPixels) fixed[c] = 1;
|
|
515
|
+
}
|
|
516
|
+
return { changed, exposed, fixed, still };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** A copy of a block mask moved by `shift` (pixels), rounded both ways so no block is lost. */
|
|
520
|
+
export function shiftMask(mask, shift, fp, { width, height } = {}) {
|
|
521
|
+
if (!mask || !shift || !fp || mask.length !== fp.cols * fp.rows) return mask || null;
|
|
522
|
+
const { cols, rows } = fp, unit = width ? width / cols : fp.block, unitY = height ? height / rows : fp.block;
|
|
523
|
+
const fx = shift.dx / unit, fy = shift.dy / unitY;
|
|
524
|
+
const xs = [...new Set([Math.floor(fx), Math.ceil(fx)])], ys = [...new Set([Math.floor(fy), Math.ceil(fy)])];
|
|
525
|
+
const out = new Uint8Array(mask.length);
|
|
526
|
+
for (let c = 0; c < mask.length; c++) {
|
|
527
|
+
if (!mask[c]) continue;
|
|
528
|
+
const x = c % cols, y = (c / cols) | 0;
|
|
529
|
+
for (const oy of ys) for (const ox of xs) { const nx = x + ox, ny = y + oy; if (nx >= 0 && ny >= 0 && nx < cols && ny < rows) out[ny * cols + nx] = 1; }
|
|
530
|
+
}
|
|
531
|
+
return out;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* What moves by itself on a scrolling screen, from two decoded frames taken `ms` apart with no input (the after sample):
|
|
536
|
+
* the residual once the scroll is taken out, what arrived and what left, at both frames' places, gaps filled and grown as motionBlocks does, with
|
|
537
|
+
* `scroll` ({ dx, dy, ms }) and `fixed` (the blocks fixed on the screen, grown by one) on it. Null when no scroll is found
|
|
538
|
+
* or the residual is more than half the screen (not a scroll this can follow).
|
|
539
|
+
*/
|
|
540
|
+
export function scrollMotionBlocks(a, b, fp, { ms = BRACKET_MS, grow = 2, gap = MOTION_GAP_BLOCKS } = {}) {
|
|
541
|
+
const shift = scrollShift(a, b);
|
|
542
|
+
if (!shift || !fp) return null;
|
|
543
|
+
const res = scrollResidual(a, b, shift, fp);
|
|
544
|
+
if (!res) return null;
|
|
545
|
+
const { cols, rows } = fp, dims = { width: a.width, height: a.height }, unshift = { dx: -shift.dx, dy: -shift.dy };
|
|
546
|
+
let n = 0;
|
|
547
|
+
for (let i = 0; i < res.changed.length; i++) n += res.changed[i];
|
|
548
|
+
if (n > 0.5 * cols * rows) return null;
|
|
549
|
+
const fixed = growMask(spreadFixed(res.fixed, res.still, fp, gap, Math.abs(shift.dx) >= Math.abs(shift.dy)), cols, rows, 1);
|
|
550
|
+
// The residual is at the later frame's places (what arrived); what left is found by the same comparison the other
|
|
551
|
+
// way, at the earlier frame's places, with the strip the scroll carried off the screen (a bird that flies off the
|
|
552
|
+
// edge during the sample arrives nowhere). The copies of what is fixed on the screen carried on with the world are
|
|
553
|
+
// ghosts, left out of what arrived: with them a jump would be masked by its own ghosts filled across it. What left is
|
|
554
|
+
// kept whole: world of one colour that a bird covered in one frame is found fixed too, and its ghost is the bird.
|
|
555
|
+
const left = scrollResidual(b, a, unshift, fp);
|
|
556
|
+
const ghostOn = shiftMask(fixed, shift, fp, dims);
|
|
557
|
+
const arrived = new Uint8Array(res.changed), gone = left ? left.changed : new Uint8Array(res.changed.length);
|
|
558
|
+
for (let i = 0; i < arrived.length; i++) if (ghostOn[i]) arrived[i] = 0;
|
|
559
|
+
const back = shiftMask(arrived, unshift, fp, dims);
|
|
560
|
+
const both = new Uint8Array(arrived);
|
|
561
|
+
for (let i = 0; i < both.length; i++) both[i] |= back[i] | gone[i] | (left ? left.exposed[i] : 0);
|
|
562
|
+
const mask = growMask(fillGaps(both, cols, rows, gap), cols, rows, grow);
|
|
563
|
+
mask.scroll = { dx: shift.dx, dy: shift.dy, ms };
|
|
564
|
+
mask.fixed = fixed;
|
|
565
|
+
return mask;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* A fixed shape's ends carried across its middle: from each block found fixed, along the scroll's axis (`rowwise` for a
|
|
570
|
+
* sideways scroll), over still blocks of about its colour, up to `gap` blocks. The middle of a one-colour shape matches
|
|
571
|
+
* itself shifted, so only its ends were found; the world next to it is not of its colour, so the spread stops there.
|
|
572
|
+
*/
|
|
573
|
+
function spreadFixed(fixed, still, fp, gap, rowwise) {
|
|
574
|
+
const { cols, rows } = fp, out = new Uint8Array(fixed);
|
|
575
|
+
const like = (c, d) => Math.abs(fp.rgb[c * 3] - fp.rgb[d * 3]) <= BLOCK_TOLERANCE && Math.abs(fp.rgb[c * 3 + 1] - fp.rgb[d * 3 + 1]) <= BLOCK_TOLERANCE && Math.abs(fp.rgb[c * 3 + 2] - fp.rgb[d * 3 + 2]) <= BLOCK_TOLERANCE;
|
|
576
|
+
for (let c = 0; c < cols * rows; c++) {
|
|
577
|
+
if (!fixed[c]) continue;
|
|
578
|
+
const x = c % cols, y = (c / cols) | 0;
|
|
579
|
+
for (const dir of [-1, 1]) {
|
|
580
|
+
for (let k = 1; k <= gap; k++) {
|
|
581
|
+
const nx = rowwise ? x + dir * k : x, ny = rowwise ? y : y + dir * k;
|
|
582
|
+
if (nx < 0 || ny < 0 || nx >= cols || ny >= rows) break;
|
|
583
|
+
const d = ny * cols + nx;
|
|
584
|
+
if (!still[d] || !like(c, d)) break;
|
|
585
|
+
out[d] = 1;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return out;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Whether the scroll during an input (`shift`, over `ms` of game time) is the screen's own: the screen scrolled by
|
|
594
|
+
* itself at both ends of the input (`rates`: the samples' scrolls, { dx, dy, ms } each), at rates that agree, and the
|
|
595
|
+
* input's scroll is what that rate predicts. A camera that follows the player scrolls because of the input, and its
|
|
596
|
+
* scroll must not be taken out of what the input did.
|
|
597
|
+
*/
|
|
598
|
+
export function steadyScroll(shift, ms, rates) {
|
|
599
|
+
const list = (rates || []).filter(r => r && r.ms > 0);
|
|
600
|
+
if (!shift || !(ms > 0) || list.length < 2) return false;
|
|
601
|
+
const per = list.map(r => ({ x: r.dx / r.ms, y: r.dy / r.ms }));
|
|
602
|
+
const within = (got, want) => Math.abs(got - want) <= Math.max(SCROLL_RATE_SLACK_PX, SCROLL_RATE_SLACK * Math.abs(want));
|
|
603
|
+
for (const axis of ['x', 'y']) {
|
|
604
|
+
const vals = per.map(p => p[axis]), mean = vals.reduce((s, v) => s + v, 0) / vals.length;
|
|
605
|
+
// The two samples agree: over the input's time their predictions are close.
|
|
606
|
+
if (vals.some(v => !within(v * ms, mean * ms))) return false;
|
|
607
|
+
if (!within(axis === 'x' ? shift.dx : shift.dy, mean * ms)) return false;
|
|
608
|
+
}
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* One input on a scrolling screen, judged both ways once the scroll (`shift`, from scrollShift) is taken out: what
|
|
614
|
+
* arrived at the after frame that the before frame does not explain, and what left the before frame that the after
|
|
615
|
+
* frame does not show (a jump leaves the world where the player stood, and the world matches itself shifted, so only
|
|
616
|
+
* the player's own pixels, gone from the before frame, tell it). `prev` and `sample` are the scroll samples at the
|
|
617
|
+
* before and the after frame (scrollMotionBlocks); what moved by itself in either, where it is on the screen and where
|
|
618
|
+
* the world carried it, and the ghosts of what is fixed on the screen are masked; `extra` masks are added on both
|
|
619
|
+
* sides, `keep` boxes left in (bracketMask). `{ mask, seen }`, or null when a side has nothing to judge by.
|
|
620
|
+
*/
|
|
621
|
+
export function scrollJudge(before, after, shift, fp, { prev, sample, extra = [], keep = [], width, height } = {}) {
|
|
622
|
+
if (!before || !after || !shift || !fp || !prev || !sample) return null;
|
|
623
|
+
const dims = { width, height }, back = { dx: -shift.dx, dy: -shift.dy };
|
|
624
|
+
const arrived = scrollResidual(before, after, shift, fp), gone = scrollResidual(after, before, back, fp);
|
|
625
|
+
if (!arrived || !gone) return null;
|
|
626
|
+
const opts = { fp, keep, ...dims };
|
|
627
|
+
const atAfter = bracketMask([prev, shiftMask(prev, shift, fp, dims), shiftMask(prev.fixed, shift, fp, dims), sample, ...extra], { ...opts, outside: arrived.exposed });
|
|
628
|
+
const atBefore = bracketMask([prev, sample, shiftMask(sample, back, fp, dims), shiftMask(sample.fixed, back, fp, dims), ...extra], { ...opts, outside: gone.exposed });
|
|
629
|
+
if (!atAfter || !atBefore) return null;
|
|
630
|
+
return { mask: atAfter, seen: significant(residualDelta(arrived.changed, atAfter, fp)) || significant(residualDelta(gone.changed, atBefore, fp)) };
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** The changed blocks of a residual not under `ignore`, as screenDelta answers: area and box in pixels. */
|
|
634
|
+
export function residualDelta(changed, ignore, fp) {
|
|
635
|
+
if (!changed || !fp || changed.length !== fp.cols * fp.rows) return null;
|
|
636
|
+
if (ignore && ignore.length !== changed.length) ignore = null;
|
|
637
|
+
let n = 0, x0 = Infinity, y0 = Infinity, x1 = -1, y1 = -1;
|
|
638
|
+
for (let c = 0; c < changed.length; c++) {
|
|
639
|
+
if (!changed[c] || (ignore && ignore[c])) continue;
|
|
640
|
+
n++;
|
|
641
|
+
const x = c % fp.cols, y = (c / fp.cols) | 0;
|
|
642
|
+
if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y;
|
|
643
|
+
}
|
|
644
|
+
const s = fp.block;
|
|
645
|
+
return n ? { area: n * s * s, width: (x1 - x0 + 1) * s, height: (y1 - y0 + 1) * s, x: x0 * s, y: y0 * s } : { area: 0, width: 0, height: 0, x: 0, y: 0 };
|
|
646
|
+
}
|
|
647
|
+
|
|
159
648
|
/**
|
|
160
649
|
* Whether two saved screenshots are the same screen to a player.
|
|
161
650
|
*
|
|
@@ -212,4 +701,4 @@ export function countDistinct(pngs) {
|
|
|
212
701
|
return distinct.count;
|
|
213
702
|
}
|
|
214
703
|
|
|
215
|
-
export default { decodePng, fingerprint, pngFingerprint, screenDelta, significant, sameScreen, createDistinct, countDistinct };
|
|
704
|
+
export default { decodePng, fingerprint, pngFingerprint, screenDelta, significant, sameScreen, createDistinct, countDistinct, motionBlocks, bracketMask, bracketVerdict, motionMoves, sweepMask, scrollShift, scrollResidual, shiftMask, scrollMotionBlocks, steadyScroll, residualDelta, scrollJudge };
|
package/src/run/replay.js
CHANGED
|
@@ -5,6 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import { promisify } from 'node:util';
|
|
6
6
|
|
|
7
7
|
import { DriverRpcError } from './drivers/godot-rpc.js';
|
|
8
|
+
import { CURSOR_NOTE, cleanCursorReads } from './drivers/godot-cursor.js';
|
|
8
9
|
import { allowlistedEnv, GodotDriver } from './drivers/godot.js';
|
|
9
10
|
import { detectGodotBinary, importProjectCopy } from './drivers/godot-project.js';
|
|
10
11
|
import { parseTrace } from './trace.js';
|
|
@@ -166,6 +167,16 @@ export async function replayTrace({ text, projectDir, godotPath, headless, runDi
|
|
|
166
167
|
// A held key the desktop dropped and the driver put back: this replay may be a
|
|
167
168
|
// tick off for a reason that has nothing to do with the game.
|
|
168
169
|
result.inputs_reasserted = driver.reasserted ?? 0;
|
|
170
|
+
// Desktop mouse events that reached the game's engine and that the addon put
|
|
171
|
+
// back (addon 1.7.0), and where the game reads the cursor itself: those reads
|
|
172
|
+
// see the desktop's pointer, so a state that depends on them can differ.
|
|
173
|
+
result.desktop_pointer_events = driver.desktopPointer ?? 0;
|
|
174
|
+
// The trace is a file anyone could have changed: only well formed reads are echoed.
|
|
175
|
+
const reads = cleanCursorReads(header.cursor_reads);
|
|
176
|
+
if (reads.length > 0) {
|
|
177
|
+
result.cursor_reads = reads;
|
|
178
|
+
result.cursor_note = CURSOR_NOTE;
|
|
179
|
+
}
|
|
169
180
|
if (result.matched) {
|
|
170
181
|
result.matched_prefix = true;
|
|
171
182
|
result.fully_replayed = !trace.truncated && !result.stopped;
|
package/src/run/tools.js
CHANGED
|
@@ -210,14 +210,14 @@ export const SCROLL_TOOL = {
|
|
|
210
210
|
|
|
211
211
|
export const SELECT_TOOL = {
|
|
212
212
|
name: 'select',
|
|
213
|
-
description: 'playwright_web only. Choose an option in a dropdown, or set a slider or other range control to a value, the way a player does with the mouse. Spends one action. Name the control by its ref or like a click target. For a dropdown pass option: the text of the option as the screen shows it. For a slider pass value: a number between its ends.',
|
|
213
|
+
description: 'playwright_web only. Choose an option in a dropdown, or set a slider or other range control to a value, the way a player does with the mouse. Spends one action. Name the control by its ref or like a click target. For a dropdown pass option: the text of the option as the screen shows it; in a menu whose item opens a further menu, the whole path, such as "Export > PNG". For a slider pass value: a number between its ends.',
|
|
214
214
|
input_schema: {
|
|
215
215
|
type: 'object',
|
|
216
216
|
additionalProperties: false,
|
|
217
217
|
properties: {
|
|
218
218
|
ref: { type: 'string', description: 'The ref shown on the control, such as e12.' },
|
|
219
219
|
target: { type: 'string', description: 'The control, named like a click target, such as combobox "Difficulty".' },
|
|
220
|
-
option: { type: 'string', description: 'A dropdown: the option to choose, by its visible text (or its value).' },
|
|
220
|
+
option: { type: 'string', description: 'A dropdown: the option to choose, by its visible text (or its value). In a menu with further menus, the path of items, such as "Export > PNG".' },
|
|
221
221
|
value: { type: 'number', description: 'A slider: the value to set it to.' },
|
|
222
222
|
expect,
|
|
223
223
|
mood,
|