feedback1 integrate: WO official diagonal routes (the floor preference no longer straightens diagonals)

# Conflicts:
#	data/stages.json
#	docs/DATA.md
#	server/sim/grid.js
#	test/sim/pathing-blockable.test.js
This commit is contained in:
sganggs committed 2026-10-03 06:49:11 +08:00
commit 075ba6428b
9 files changed
+551 -157

No files matched your search

+98 -50
View File
@@ -1,9 +1,12 @@
// Adversarial cross-check of grid.js against an independent port of the official client pathing (research 08 §3.4,
// `Torappu.Battle.SPFA`: FIFO SPFA from the destination over the WHOLE level map, UP/RIGHT/DOWN/LEFT, crates cost 1000,
// strict improvement; row-major in-place Bresenham smoothing with the diagonal corner rule), run on the raw level files
// in .cache/gamedata (skipped when absent), extended by our blockable-ground preference (grid.js header: equal-length
// ties go to the chain with the fewest non-blockable tiles; a smoothed line crosses non-blockable tiles only on its own
// raw chain):
// in .cache/gamedata (skipped when absent), extended by our blockable-ground preference (grid.js header: a second,
// preference field = 0.1.0's — equal-length ties to the chain with the fewest non-blockable tiles, a line of sight that
// covers non-blockable tiles, diagonal-step corners included, only on the own raw chain — whose pointer replaces the
// official one where its route crosses fewer non-blockable tiles, or as few while only skipping the official waypoint
// (on the straight line to it, leading there); "crosses" = positive-length intersection with the tile, found here by
// clipping the segment against each tile, independently of grid.js crossTiles):
// * the 8 active stages with their match-start devices: the smoothed chain from EVERY walkable tile of the normal,
// 联防 and boss rects to every goal equals the full-map reference one (the sim's rect limit changes nothing);
// * 400 random crate / block layouts: identical chains to the same algorithm limited to the rect;
@@ -26,9 +29,8 @@ const FOUR = [[1, 0], [0, 1], [-1, 0], [0, -1]];
/**
* The official algorithm on a raw level (non-hidden crates cost 1000; platforms / mounds blocked like the sim [ASSUMED]).
* `prefer` adds the blockable-ground preference (lexicographic (dist, non-blockable count) relaxation; smoothing lines
* cross non-blockable tiles only on the tile's own raw chain between its ends). Returns dest → (start → chain string),
* with `.dist(start)` / `.covered(start)` (tiles of the smoothed route incl. diagonal-step corners) on each dest.
* `prefer` adds the blockable-ground preference (see the header). Returns dest → (start → chain string), with
* `.dist(start)` / `.crossedNb(start)` (non-blockable tiles the route crosses, start excluded) on each dest.
*/
function official(sid, { crates: extraCrates = [], blocks: extraBlocks = [], rect = null, prefer = true } = {}) {
const lv = JSON.parse(readFileSync(file(sid), 'utf8'));
@@ -69,48 +71,95 @@ function official(sid, { crates: extraCrates = [], blocks: extraBlocks = [], rec
}
return clear ? out.every(([a, b]) => clear(a, b)) : out;
};
return (dest) => {
const D = dest.join();
const dist = new Map([[D, 0]]);
const pen = new Map([[D, 0]]);
const par = new Map();
const q = [dest];
const inq = new Set([D]);
while (q.length) {
const cur = q.shift();
const ck = cur.join();
inq.delete(ck);
for (const [dr, dc] of FOUR) {
const nb2 = [cur[0] + dr, cur[1] + dc];
const nk = nb2.join();
if (!passable(nb2[0], nb2[1])) continue;
const nd = dist.get(ck) + (crates.has(nk) ? 1000 : 1);
const np = pen.get(ck) + (nb(nb2[0], nb2[1]) ? 1 : 0);
const better = !dist.has(nk) || nd < dist.get(nk) || (prefer && nd === dist.get(nk) && np < pen.get(nk));
if (better) {
dist.set(nk, nd);
pen.set(nk, np);
par.set(nk, cur);
if (!inq.has(nk)) { q.push(nb2); inq.add(nk); }
// tiles the centre-to-centre segment a → b passes through with positive length (Liang–Barsky clip per tile)
const through = ([r0, c0], [r1, c1]) => {
const out = [];
for (let r = Math.min(r0, r1); r <= Math.max(r0, r1); r++) {
for (let c = Math.min(c0, c1); c <= Math.max(c0, c1); c++) {
let t0 = 0, t1 = 1;
for (const [pp, qq] of [[-(c1 - c0), c0 - (c - 0.5)], [c1 - c0, c + 0.5 - c0], [-(r1 - r0), r0 - (r - 0.5)], [r1 - r0, r + 0.5 - r0]]) {
if (pp === 0) { if (qq < 0) t1 = -1; continue; }
if (pp < 0) t0 = Math.max(t0, qq / pp); else t1 = Math.min(t1, qq / pp);
}
if (t1 - t0 > 1e-9) out.push([r, c]);
}
}
const nxt = new Map(par);
for (let r = 0; r < H; r++) {
for (let c = 0; c < W; c++) {
const k = `${r},${c}`;
if (!nxt.has(k)) continue;
const chain = new Set();
for (let x = [r, c]; x; x = par.get(x.join())) chain.add(x.join());
const los = (a) => {
const lo = dist.get(a.join());
return bres([r, c], a, (y, x) => passable(y, x) && !crates.has(`${y},${x}`)
&& (!prefer || !nb(y, x) || (chain.has(`${y},${x}`) && dist.get(`${y},${x}`) >= lo)));
};
let b = nxt.get(k);
while (nxt.has(b.join()) && los(nxt.get(b.join()))) b = nxt.get(b.join());
nxt.set(k, b);
return out;
};
const key = (p) => p.join();
const unkey = (k) => k.split(',').map(Number);
const losClear = (y, x) => passable(y, x) && !crates.has(`${y},${x}`);
return (dest) => {
const D = dest.join();
const spfa = (usePen) => {
const dist = new Map([[D, 0]]);
const pen = new Map([[D, 0]]);
const par = new Map();
const q = [dest];
const inq = new Set([D]);
while (q.length) {
const cur = q.shift();
const ck = cur.join();
inq.delete(ck);
for (const [dr, dc] of FOUR) {
const nb2 = [cur[0] + dr, cur[1] + dc];
const nk = nb2.join();
if (!passable(nb2[0], nb2[1])) continue;
const nd = dist.get(ck) + (crates.has(nk) ? 1000 : 1);
const np = pen.get(ck) + (usePen && nb(nb2[0], nb2[1]) ? 1 : 0);
if (!dist.has(nk) || nd < dist.get(nk) || (usePen && nd === dist.get(nk) && np < pen.get(nk))) {
dist.set(nk, nd);
pen.set(nk, np);
par.set(nk, cur);
if (!inq.has(nk)) { q.push(nb2); inq.add(nk); }
}
}
}
return { dist, par };
};
// row-major in-place smoothing; `guard` (0.1.0's preference smoothing): the line of sight toward ancestor a may
// also cover non-blockable tiles (Bresenham footprint, diagonal-step corners included) only on the tile's own raw
// chain between the tile and a — the jump stops at the first ancestor it cannot see so
const smooth = ({ dist, par }, guard) => {
const nxt = new Map(par);
for (let r = 0; r < H; r++) {
for (let c = 0; c < W; c++) {
const k = `${r},${c}`;
if (!nxt.has(k)) continue;
const chain = new Set();
if (guard) for (let x = [r, c]; x; x = par.get(x.join())) chain.add(x.join());
const sees = (a) => bres([r, c], a, (y, x) => losClear(y, x)
&& (!guard || !nb(y, x) || (chain.has(`${y},${x}`) && dist.get(`${y},${x}`) >= dist.get(a.join()))));
let b = nxt.get(k);
while (nxt.has(b.join()) && sees(nxt.get(b.join()))) b = nxt.get(b.join());
nxt.set(k, b);
}
}
return nxt;
};
const off = spfa(false);
const dist = off.dist;
const segNb = (a, b) => through(a, b).filter(([y, x]) => !(y === a[0] && x === a[1]) && nb(y, x)).length;
let nxt = smooth(off, false);
if (prefer) {
// per tile in increasing distance: the preference pointer when its route crosses fewer non-blockable tiles (or as
// few while it only skips the official waypoint: o strictly inside the segment a → p, and o's chosen pointer is p)
const nxtP = smooth(spfa(true), true);
const cost = new Map([[D, 0]]);
const chosen = new Map();
const skips = (a, o, p) => {
const t = through(a, p);
return key(o) !== key(a) && key(o) !== key(p) && (o[0] - a[0]) * (p[1] - a[1]) === (o[1] - a[1]) * (p[0] - a[0]) && t.some((x) => key(x) === key(o));
};
for (const k of [...dist.keys()].sort((a, b) => dist.get(a) - dist.get(b))) {
if (k === D) continue;
const a = unkey(k), o = nxt.get(k), p = nxtP.get(k);
const co = segNb(a, o) + cost.get(key(o)), cp = segNb(a, p) + cost.get(key(p));
const useP = cp < co || (cp === co && key(o) !== key(p) && key(chosen.get(key(o)) ?? []) === key(p) && skips(a, o, p));
chosen.set(k, useP ? p : o);
cost.set(k, useP ? cp : co);
}
nxt = chosen;
}
const chainOf = (start) => {
if (!dist.has(start.join())) return null;
@@ -121,13 +170,12 @@ function official(sid, { crates: extraCrates = [], blocks: extraBlocks = [], rec
};
const fn = (start) => { const ch = chainOf(start); return ch ? ch.map((p) => `(${p})`).join(' ') : null; };
fn.dist = (start) => dist.get(start.join()) ?? -1;
fn.coveredNb = (start) => {
fn.crossedNb = (start) => {
const ch = chainOf(start);
if (!ch) return null;
const s = new Set();
for (let i = 1; i < ch.length; i++) for (const [y, x] of bres(ch[i - 1], ch[i])) if (nb(y, x)) s.add(`${y},${x}`);
if (nb(...ch[0])) s.add(ch[0].join());
return s;
let n = 0;
for (let i = 1; i < ch.length; i++) n += segNb(ch[i - 1], ch[i]);
return n;
};
return fn;
};
@@ -203,7 +251,7 @@ test('vs the PURE official algorithm: same route length from every tile, never m
for (let c = rect.c0; c <= rect.c1; c++) {
if (!g.walkable(r, c) || p.dist([r, c]) < 0) continue;
assert.equal(f.dist[r * 21 + c], p.dist([r, c]), `${sid} (${r},${c}) → (${dest}): official length`);
const a = p.coveredNb([r, c]).size, b = o.coveredNb([r, c]).size;
const a = p.crossedNb([r, c]), b = o.crossedNb([r, c]);
assert.ok(b <= a, `${sid} (${r},${c}) → (${dest}): ${b} non-blockable tiles vs official ${a}`);
if (b < a) fewer++;
n++;