diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2022-03-27 19:43:58 +0200 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2022-03-27 19:43:58 +0200 |
| commit | ab750d05a2eb580067e51c26032ff24358834d0b (patch) | |
| tree | 6cf9a416f0b90dcb6bc0496d0862ee0b9e263591 /src/map.ts | |
| parent | f8c006414aec99c90558587dbe08cf1705ed44f7 (diff) | |
| download | boules-ab750d05a2eb580067e51c26032ff24358834d0b.tar.gz boules-ab750d05a2eb580067e51c26032ff24358834d0b.tar.zst boules-ab750d05a2eb580067e51c26032ff24358834d0b.zip | |
Start of next turn
Diffstat (limited to 'src/map.ts')
| -rw-r--r-- | src/map.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/map.ts b/src/map.ts new file mode 100644 index 0000000..44a970c --- /dev/null +++ b/src/map.ts @@ -0,0 +1,65 @@ +import Ball from "./Ball"; +import { MAP_HEIGHT, MAP_WIDTH } from "./consts"; +import { Graph } from "./pathfinder/Graph"; + +export type Map = (Ball | null)[][]; +export function genMap(balls: Array<Ball>): Map { + const map = <Map>[]; + for (let i = 0; i < MAP_HEIGHT; i++) { + map.push([]); + for (let j = 0; j < MAP_WIDTH; j++) { + map[i].push(null); + } + } + for (const b of balls) { + map[b.x][b.y] = b + } + return map; +} + +type Coord = { x: number; y: number }; +export function genGraph(map: Map, start: Coord, goal: Coord): Graph { + const graph: Graph = []; + map.forEach((row, i) => { + row.forEach((b, j) => { + graph.push({ + start: i === start.x && j === start.y, + goal: i === goal.x && j === goal.y, + walkable: b === null, + uuid: uuidv4(), + graph, + x: i, + y: j, + edges: [], + }); + }); + }); + map.forEach((row, i) => { + row.forEach((_, j) => { + const vertex = graph.find(v => v.x === i && v.y === j); + if (!vertex) return; + const n1 = graph.find(v => v.x === i - 1 && v.y === j); + const n2 = graph.find(v => v.x === i + 1 && v.y === j); + const n3 = graph.find(v => v.x === i && v.y === j - 1); + const n4 = graph.find(v => v.x === i && v.y === j + 1); + for (const n of [n1, n2, n3, n4]) { + if (n) { + vertex.edges.push({ + from: vertex, + to: n, + weight: 1, + }); + } + } + }); + }); + return graph; +} + +function uuidv4() { + // @ts-ignore + return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => + (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) + ); +} + |
