🚀 go back to first position
This commit is contained in:
@@ -4,6 +4,7 @@ import { getBestRoute } from "@snk/compute/getBestRoute";
|
||||
import { createGif } from "@snk/gif-creator";
|
||||
import { createSvg } from "../svg-creator";
|
||||
import { snake4 } from "@snk/types/__fixtures__/snake";
|
||||
import { getPathToPose } from "@snk/compute/getPathToPose";
|
||||
|
||||
export const generateContributionSnake = async (
|
||||
userName: string,
|
||||
@@ -30,6 +31,7 @@ export const generateContributionSnake = async (
|
||||
|
||||
console.log("📡 computing best route");
|
||||
const chain = getBestRoute(grid, snake)!;
|
||||
chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
|
||||
|
||||
const output: Record<string, Buffer | string> = {};
|
||||
|
||||
|
||||
69
packages/compute/getPathToPose.ts
Normal file
69
packages/compute/getPathToPose.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
getHeadX,
|
||||
getHeadY,
|
||||
nextSnake,
|
||||
snakeEquals,
|
||||
snakeToCells,
|
||||
snakeWillSelfCollide,
|
||||
} from "@snk/types/snake";
|
||||
import type { Snake } from "@snk/types/snake";
|
||||
import { getColor, Grid, isEmpty, isInside } from "@snk/types/grid";
|
||||
import { getTunnelPath } from "./tunnel";
|
||||
import { around4 } from "@snk/types/point";
|
||||
import { sortPush } from "./utils/sortPush";
|
||||
|
||||
const isEmptySafe = (grid: Grid, x: number, y: number) =>
|
||||
!isInside(grid, x, y) || isEmpty(getColor(grid, x, y));
|
||||
|
||||
type M = { snake: Snake; parent: M | null; w: number; f: number };
|
||||
export const getPathToPose = (snake0: Snake, target: Snake, grid?: Grid) => {
|
||||
const targetCells = snakeToCells(target).reverse();
|
||||
|
||||
const [t0] = targetCells;
|
||||
|
||||
const openList: M[] = [{ snake: snake0, w: 0 } as any];
|
||||
const closeList: Snake[] = [];
|
||||
|
||||
while (openList.length) {
|
||||
const o = openList.shift()!;
|
||||
|
||||
const x = getHeadX(o.snake);
|
||||
const y = getHeadY(o.snake);
|
||||
|
||||
if (x === t0.x && y === t0.y) {
|
||||
const path: Snake[] = [];
|
||||
let e: M["parent"] = o;
|
||||
while (e) {
|
||||
path.push(e.snake);
|
||||
e = e.parent;
|
||||
}
|
||||
path.unshift(...getTunnelPath(path[0], targetCells));
|
||||
path.pop();
|
||||
path.reverse();
|
||||
return path;
|
||||
}
|
||||
|
||||
for (let i = 0; i < around4.length; i++) {
|
||||
const { x: dx, y: dy } = around4[i];
|
||||
|
||||
const nx = x + dx;
|
||||
const ny = y + dy;
|
||||
|
||||
if (
|
||||
!snakeWillSelfCollide(o.snake, dx, dy) &&
|
||||
(!grid || isEmptySafe(grid, nx, ny))
|
||||
) {
|
||||
const snake = nextSnake(o.snake, dx, dy);
|
||||
|
||||
if (!closeList.some((s) => snakeEquals(snake, s))) {
|
||||
const w = o.w + 1;
|
||||
const h = Math.abs(nx - x) + Math.abs(ny - y);
|
||||
const f = w + h;
|
||||
|
||||
sortPush(openList, { f, w, snake, parent: o }, (a, b) => a.f - b.f);
|
||||
closeList.push(snake);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from "@snk/draw/drawWorld";
|
||||
import { userContributionToGrid } from "../action/userContributionToGrid";
|
||||
import { snake4 as snake } from "@snk/types/__fixtures__/snake";
|
||||
import { getPathToPose } from "@snk/compute/getPathToPose";
|
||||
|
||||
const createForm = ({
|
||||
onSubmit,
|
||||
@@ -206,6 +207,7 @@ const onSubmit = async (userName: string) => {
|
||||
|
||||
const grid = userContributionToGrid(cells);
|
||||
const chain = getBestRoute(grid, snake)!;
|
||||
chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
|
||||
dispose();
|
||||
|
||||
createViewer({ grid0: grid, chain, drawOptions });
|
||||
|
||||
@@ -3,8 +3,10 @@ import { getBestRoute } from "@snk/compute/getBestRoute";
|
||||
import { createSvg } from "../svg-creator";
|
||||
import { grid, snake } from "./sample";
|
||||
import { drawOptions } from "./canvas";
|
||||
import { getPathToPose } from "@snk/compute/getPathToPose";
|
||||
|
||||
const chain = getBestRoute(grid, snake);
|
||||
chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
|
||||
const svg = createSvg(grid, chain, drawOptions, { frameDuration: 200 });
|
||||
|
||||
const container = document.createElement("div");
|
||||
|
||||
Reference in New Issue
Block a user