Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pathfinding and flood traversals #119

Open
justinkwaugh opened this issue Sep 12, 2024 · 1 comment
Open

Pathfinding and flood traversals #119

justinkwaugh opened this issue Sep 12, 2024 · 1 comment

Comments

@justinkwaugh
Copy link

justinkwaugh commented Sep 12, 2024

The current set of traversers are not sufficient for pathfinding or flooding to a range w/ obstacles. There are sort of two things needed that would make such things more easily doable.

  1. A traverser which will perform breadth-first traversal
  2. Which accepts a predicate closure to determine traversability

I created something that works for me, but it doesn't handle the cursor param appropriately as I did not need it

export function flood<T extends Hex>(options: FloodOptions): Traverser<T> {
    return function floodTraverser(createHex, _cursor) {
        const visitedHexes = new Map<number, T>()

        const startHex = createHex(options.start)
        const directions = startHex.isPointy ? POINTY_NEIGHBORS : FLAT_NEIGHBORS

        let depth = 0
        const queue: T[] = [startHex]
        visitedHexes.set(axialCoordinatesToNumber(startHex), startHex)

        while (queue.length > 0 && depth < options.range) {
            const numAtCurrentDepth = queue.length
            for (let i = 0; i < numAtCurrentDepth; i++) {
                const currentHex = queue.shift()!
                for (const direction of directions) {
                    const neighbor = createHex(neighborOf(currentHex, direction))
                    if (options.canTraverse && !options.canTraverse(neighbor)) {
                        continue
                    }
                    const neighborKey = axialCoordinatesToNumber(neighbor)
                    if (!visitedHexes.has(neighborKey)) {
                        visitedHexes.set(neighborKey, neighbor)
                        queue.push(neighbor)
                    }
                }
            }
            depth++
        }
        return [...visitedHexes.values()]
    }
}

/**
 * @category Traverser
 */
export interface FloodOptions {
    start: HexCoordinates
    range: number
    canTraverse?: (hex: Hex) => boolean
}

const POINTY_NEIGHBORS = [
    Direction.E,
    Direction.SE,
    Direction.SW,
    Direction.W,
    Direction.NW,
    Direction.NE
]
const FLAT_NEIGHBORS = [
    Direction.N,
    Direction.NE,
    Direction.SE,
    Direction.S,
    Direction.SW,
    Direction.NW
]
@flauwekeul
Copy link
Owner

Thanks for opening a ticket. I'll try to look into it in the coming weeks, but to be honest, Honeycomb has low priority for me...

It looks like you delimited the code with a single back tick, did you try delimiting with 3 back ticks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants