xxxxxxxxxx
200
// worker example: https://glitch.com/~p5js-web-worker
let cellsize;
let fontsize;
let rows, cols;
let gp_worker;
const DRUNKARDS_ITERATIONS = 20;
const DA_ITERATIONS = 1500;
const ITER_TIMEOUT = 50;
const CELLS = {
EMPTY: 0,
HARD_WALL: 1,
INNER_WALL: 2,
FLOOR: 3,
DOOR: 4,
};
const DIRS = [
[-1, -1],
[0, -1],
[1, -1],
[-1, 0],
[0, 0],
[1, 0],
[-1, 1],
[0, 1],
[1, 1],
];
function setup() {
createCanvas(800, 800);
background(0);
rows = 80;
cols = 80;
cellsize = int(width / cols);
gp_worker = new Worker("/gp_worker.js");
gp_worker.onmessage = handle_message;
let grid = [];
for (let r = 0; r < rows; r++) {
grid[r] = [];
for (let c = 0; c < cols; c++) {
if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
grid[r][c] = CELLS.HARD_WALL;
else grid[r][c] = CELLS.INNER_WALL;
}
}
grid = noise_map(grid);
grid = drunkards_walk(grid, DRUNKARDS_ITERATIONS);
// grid = diffusion_aggregation_lite(grid, DA_ITERATIONS);
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
drawCell(grid, c, r);
}
}
}
function draw() {
// background(220);
if (frameCount % 50 == 0) {
gp_worker.postMessage("getRandomColor");
}
}
function drawCell(g, c, r) {
let col = 0;
noStroke();
switch (g[r][c]) {
case CELLS.EMPTY:
col = 220;
break;
case CELLS.HARD_WALL:
col = 0;
break;
case CELLS.INNER_WALL:
col = 40;
break;
case CELLS.DOOR:
col = 80;
break;
case CELLS.FLOOR:
col = 180;
break;
default:
col = color(255, 0, 255);
break;
}
fill(col);
rect(c * cellsize, r * cellsize, cellsize, cellsize);
}
function cellular_automata(g, evols) {}
function bsp(g) {}
function noise_map(g) {
noiseDetail(8, 0.25);
const zoom = 0.1;
for (let r = 1; r < rows-1; r++) {
for (let c = 1; c < cols-1; c++) {
let n = noise(c*zoom, r*zoom);
if (n < 0.2) g[r][c] = CELLS.INNER_WALL;
else if (n < 0.3) g[r][c] = CELLS.FLOOR;
else if (n < 0.4) g[r][c] = CELLS.INNER_WALL;
else if (n < 0.5) g[r][c] = CELLS.HARD_WALL;
else g[r][c] = CELLS.FLOOR;
}
}
return g;
}
// based off of: https://www.noveltech.dev/unity-procgen-diffusion-aggregation
function diffusion_aggregation_lite(g, evols) {
// open center
let center_c = int(cols/2);
let center_r = int(rows/2);
for (let r = -5; r < 6; r++) {
for (let c = -5; c < 6; c++) {
g[center_r + r][center_c + c] = CELLS.FLOOR;
}
}
while (evols > 0) {
let done = false;
let sr = int(random(1, rows - 1));
let sc = int(random(1, cols - 1));
while (!done) {
g[sr][sc] = CELLS.FLOOR;
let d = random(DIRS);
let nsc = sc + d[0];
let nsr = sr + d[1];
if (isValid(g, nsc, nsr)) {
if (g[nsr][nsc] == CELLS.INNER_WALL) g[nsr][nsc] = CELLS.FLOOR;
else {
break;
}
} else {
done = true;
}
}
evols--;
}
return g;
}
// based off of: https://www.noveltech.dev/procgen-random-walk
function drunkards_walk(g, evols) {
while (evols > 0) {
let sr = int(random(1, rows - 1));
let sc = int(random(1, cols - 1));
let done = false;
// let timeout = ITER_TIMEOUT;
let life = random(20, ITER_TIMEOUT);
while (!done || life > 0) {
let d = random(DIRS);
sc += d[0];
sr += d[1];
if (isValid(g, sc, sr)) {
g[sr][sc] = CELLS.FLOOR;
} else {
done = true;
break;
}
life--;
}
evols--;
}
return g;
}
function flood_fill(g) { }
function isValid(g, c, r) {
return c > 0 && c < cols-1 && r > 0 && r < rows-1;
}
///// worker
function handle_message(msg) {
console.log(msg.data);
}