🔨 fix getPathToPose
This commit is contained in:
22
packages/action/__tests__/generateContributionSnake.spec.ts
Normal file
22
packages/action/__tests__/generateContributionSnake.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { generateContributionSnake } from "../generateContributionSnake";
|
||||
|
||||
it("should generate contribution snake", async () => {
|
||||
const outputSvg = path.join(__dirname, "__snapshots__/out.svg");
|
||||
const outputGif = path.join(__dirname, "__snapshots__/out.gif");
|
||||
|
||||
const buffer = await generateContributionSnake("platane", {
|
||||
svg: true,
|
||||
gif: true,
|
||||
});
|
||||
|
||||
expect(buffer.svg).toBeDefined();
|
||||
expect(buffer.gif).toBeDefined();
|
||||
|
||||
console.log("💾 writing to", outputSvg);
|
||||
fs.writeFileSync(outputSvg, buffer.svg);
|
||||
|
||||
console.log("💾 writing to", outputGif);
|
||||
fs.writeFileSync(outputGif, buffer.gif);
|
||||
});
|
||||
@@ -1,13 +1,20 @@
|
||||
import {
|
||||
getHeadX,
|
||||
getHeadY,
|
||||
getSnakeLength,
|
||||
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 {
|
||||
getColor,
|
||||
Grid,
|
||||
isEmpty,
|
||||
isInside,
|
||||
isInsideLarge,
|
||||
} from "@snk/types/grid";
|
||||
import { getTunnelPath } from "./tunnel";
|
||||
import { around4 } from "@snk/types/point";
|
||||
import { sortPush } from "./utils/sortPush";
|
||||
@@ -19,6 +26,18 @@ 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 snakeN = getSnakeLength(snake0);
|
||||
const box = {
|
||||
min: {
|
||||
x: Math.min(getHeadX(snake0), getHeadX(target)) - snakeN - 1,
|
||||
y: Math.min(getHeadY(snake0), getHeadY(target)) - snakeN - 1,
|
||||
},
|
||||
max: {
|
||||
x: Math.max(getHeadX(snake0), getHeadX(target)) + snakeN + 1,
|
||||
y: Math.max(getHeadY(snake0), getHeadY(target)) + snakeN + 1,
|
||||
},
|
||||
};
|
||||
|
||||
const [t0] = targetCells;
|
||||
|
||||
const openList: M[] = [{ snake: snake0, w: 0 } as any];
|
||||
@@ -51,7 +70,13 @@ export const getPathToPose = (snake0: Snake, target: Snake, grid?: Grid) => {
|
||||
|
||||
if (
|
||||
!snakeWillSelfCollide(o.snake, dx, dy) &&
|
||||
(!grid || isEmptySafe(grid, nx, ny))
|
||||
(!grid || isEmptySafe(grid, nx, ny)) &&
|
||||
(grid
|
||||
? isInsideLarge(grid, 2, nx, ny)
|
||||
: box.min.x <= nx &&
|
||||
nx <= box.max.x &&
|
||||
box.min.y <= ny &&
|
||||
ny <= box.max.y)
|
||||
) {
|
||||
const snake = nextSnake(o.snake, dx, dy);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user