WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 97f7415

Browse files
committed
feat: add random obstacle generation to fragmented A* pathfinding
1 parent 24ec1e0 commit 97f7415

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/pages/fragmented-a-star/index.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,30 @@ export default function FragmentedAStar() {
3939
const maxCost = 2 ** (GridType.BYTES_PER_ELEMENT * 8) - 1
4040
const grid = new GridType(gridBuffer).fill(0)
4141

42+
{
43+
// random obstacles
44+
const count = Math.random() * SIDE / 2 + SIDE / 2
45+
for (let i = 0; i < count; i++) {
46+
const largeSide = Math.random() > 0.5
47+
const x1 = Math.floor(Math.random() * SIDE)
48+
const x2 = Math.floor(Math.random() * SIDE / (largeSide ? 30 : 8)) + x1
49+
const y1 = Math.floor(Math.random() * SIDE)
50+
const y2 = Math.floor(Math.random() * SIDE / (largeSide ? 8 : 30)) + y1
51+
for (let y = y1; y < y2; y++) {
52+
const row = y * SIDE
53+
for (let x = x1; x < x2; x++) {
54+
grid[row + x] = maxCost
55+
}
56+
}
57+
}
58+
}
59+
4260
const goal = { x: Math.round(SIDE * 1 / 8), y: Math.round(SIDE * 1 / 8) }
43-
const from = { x: Math.round(SIDE * 7 / 8), y: Math.round(SIDE * 7 / 8) }
61+
const from = { x: 0, y: 0 }
62+
do {
63+
from.x = Math.floor(Math.random() * SIDE)
64+
from.y = Math.floor(Math.random() * SIDE)
65+
} while (grid[from.y * SIDE + from.x] === maxCost)
4466

4567
const graph: Map<number, { islands: Set<Island>, tiles: Map<number, Island> }> = new Map()
4668
const path: Island[] = []

src/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const ROUTES = {
188188
title: '[WIP] Fragmented A*',
189189
},
190190
git: {
191-
lastModified: 1740872879000,
191+
lastModified: 1740912450000,
192192
firstAdded: 1727995709000
193193
},
194194
},

0 commit comments

Comments
 (0)