function aStar(origin, target, barriers) {
if (!target || barriers.has(target)) return [];
const frontier = new Heapify(256, [], [], Array);
frontier.push(origin, 0);
const cameFrom = {[origin]: null};
const costSoFar = {[origin]: 0};
let current;
while (frontier.length > 0) {
current = frontier.pop();
if (current === target) break;
const neighbors = h3.kRing(current, 1)
.filter(h3Index => h3Index !== current && !barriers.has(h3Index));
for (const neighbor of neighbors) {
const cost = costSoFar[current] + 1;
if (!costSoFar[neighbor] || cost < costSoFar[neighbor]) {
costSoFar[neighbor] = cost;
const priority = cost + h3.h3Distance(neighbor, target);
try {
frontier.push(neighbor, priority);
} catch (e) {
return [];
}
cameFrom[neighbor] = current;
}
}
}
const path = [];
current = target;
while (current !== origin) {
path.push(current);
current = cameFrom[current];
}
path.reverse();
return path;
}