hexagons = {
regen;
const cells = [];
const alive = new Map();
const baseCells = h3.getRes0Cells();
for (const baseCell of baseCells) {
const childCount = h3.cellToChildrenSize(baseCell, h3Resolution);
for (let childPos = 0; childPos < childCount; childPos++) {
const cell = h3.childPosToCell(childPos, baseCell, h3Resolution);
cells.push(cell);
alive.set(cell, Math.random() < 0.3 ? 1 : 0);
}
}
function getState(cell) {
const isAlive = alive.get(cell);
const neighbors = h3.gridDisk(cell, 1);
let livingNeighbors = 0;
for (const neighbor of neighbors) {
if (neighbor !== cell && alive.get(neighbor)) livingNeighbors++;
}
if (isAlive) {
if (livingNeighbors <= underpop) return 0;
if (livingNeighbors >= overpop) return 0;
} else if (livingNeighbors > generation) {
return 1;
}
return isAlive;
}
while (true) {
await Promises.delay(200);
for (const cell of cells) {
alive.set(cell, getState(cell));
}
yield {cells, alive};
}
}