diff --git a/src/maze/_impl/_mz_square_cells_impl.scad b/src/maze/_impl/_mz_square_cells_impl.scad index 12ff91d4..40ad195c 100644 --- a/src/maze/_impl/_mz_square_cells_impl.scad +++ b/src/maze/_impl/_mz_square_cells_impl.scad @@ -1,9 +1,7 @@ use <_mz_square_comm.scad>; -function eqPos(x, y, cell) = cell.x == x && cell.y == y; - // is (x, y) visited? -function visited(x, y, cells, columns) = cells[y * columns + x][3]; +function visited(x, y, cells, columns) = cells[y][x][3]; // is (x, y) visitable? function visitable(x, y, cells, rows, columns) = @@ -12,10 +10,13 @@ function visitable(x, y, cells, rows, columns) = !visited(x, y, cells, columns); // unvisited // setting (x, y) as being visited -function set_visited(x, y, cells) = [ - for(cell = cells) - eqPos(x, y, cell) ? [x, y, get_type(cell), true] : cell -]; +function set_visited(x, y, cells) = + let(rowY = [for(cell = cells[y]) if(cell.x == x) [x, y, get_type(cell), true] else cell]) + [ + for(r = [0:len(cells) - 1]) + if(r == y) rowY + else cells[r] + ]; // 0(right), 1(top), 2(left), 3(bottom) _rand_dir_table = [ @@ -61,34 +62,68 @@ function next_y(y, dir, rows, wrapping) = wrapping ? (ny < 0 ? ny + rows : ny % rows) : ny; // go right and carve the right wall -function carve_right(x, y, cells) = [ - for(cell = cells) - !eqPos(x, y, cell) ? cell : - top_right_wall(cell) ? [x, y, 1, true] : [x, y, 0, true] -]; +function carve_right(x, y, cells) = + let( + rowY = [ + for(cell = cells[y]) + if(cell.x != x) cell + else (top_right_wall(cell) ? [x, y, 1, true] : [x, y, 0, true]) + ] + ) + [ + for(r = [0:len(cells) - 1]) + if(r == y) rowY + else cells[r] + ]; // go up and carve the top wall -function carve_top(x, y, cells) = [ - for(cell = cells) - !eqPos(x, y, cell) ? cell : - top_right_wall(cell) ? [x, y, 2, true] : [x, y, 0, true] -]; +function carve_top(x, y, cells) = + let( + rowY = [ + for(cell = cells[y]) + if(cell.x != x) cell + else (top_right_wall(cell) ? [x, y, 2, true] : [x, y, 0, true]) + ] + ) + [ + for(r = [0:len(cells) - 1]) + if(r == y) rowY + else cells[r] + ]; // go left and carve the right wall of the left cell function carve_left(x, y, cells, columns) = let( x_minus_one = x - 1, - nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one + nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one, + rowY = [ + for(cell = cells[y]) + if(cell.x != nx) cell + else [nx, y, 1, false] + ] ) - [for(cell = cells) eqPos(nx, y, cell) ? [nx, y, 1, 0] : cell]; + [ + for(r = [0:len(cells) - 1]) + if(r == y) rowY + else cells[r] + ]; // go down and carve the top wall of the bottom cell function carve_bottom(x, y, cells, rows) = let( y_minus_one = y - 1, - ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one + ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one, + rowNY = [ + for(cell = cells[ny]) + if(cell.x != x) cell + else [x, ny, 2, false] + ] ) - [for(cell = cells) eqPos(x, ny, cell) ? [x, ny, 2, 0] : cell]; + [ + for(r = [0:len(cells) - 1]) + if(r == ny) rowNY + else cells[r] + ]; // 0(right), 1(top), 2(left), 3(bottom) function carve(dir, x, y, cells, rows, columns) = @@ -97,7 +132,6 @@ function carve(dir, x, y, cells, rows, columns) = dir == 2 ? carve_left(x, y, cells, columns) : /*dir 3*/ carve_bottom(x, y, cells, rows); - // find out visitable dirs from (x, y) function visitable_dirs(r_dirs, x, y, cells, rows, columns, x_wrapping, y_wrapping) = [ for(dir = r_dirs) diff --git a/src/maze/_impl/_mz_square_initialize.scad b/src/maze/_impl/_mz_square_initialize.scad index 3ae89dd8..905b3314 100644 --- a/src/maze/_impl/_mz_square_initialize.scad +++ b/src/maze/_impl/_mz_square_initialize.scad @@ -2,14 +2,17 @@ use <_mz_square_comm.scad>; // create a starting maze for being visited later. function _rc_maze(rows, columns) = [ - for(y = [0:rows - 1], x = [0:columns - 1]) - cell( - x, y, - // all cells have top and right walls - 3, - // unvisited - false - ) + for(y = [0:rows - 1]) + [ + for(x = [0:columns - 1]) + cell( + x, y, + // all cells have top and right walls + 3, + // unvisited + false + ) + ] ]; function _mz_mask(mask) = @@ -18,7 +21,8 @@ function _mz_mask(mask) = columns = len(mask[0]) ) [ - for(y = [0:rows - 1], x = [0:columns - 1]) + for(y = [0:rows - 1]) [ + for(x = [0:columns - 1]) mask[rows - y - 1][x] == 0 ? cell( x, y, @@ -32,4 +36,5 @@ function _mz_mask(mask) = 3, // unvisited false ) + ] ]; \ No newline at end of file diff --git a/src/maze/mz_square_cells.scad b/src/maze/mz_square_cells.scad index 5e36322c..9524d8c4 100644 --- a/src/maze/mz_square_cells.scad +++ b/src/maze/mz_square_cells.scad @@ -16,15 +16,18 @@ function mz_square_cells(rows, columns, start = [0, 0], init_cells, x_wrapping = let( init_undef = is_undef(init_cells), mz = init_undef ? mz_square_initialize(rows, columns) : init_cells, - c = find_index(mz, function(cell) cell.y != 0), - r = len(mz) / c + r = len(mz), + c = len(mz[0]), + // c = find_index(mz, function(cell) cell.y != 0), + // r = len(mz) / c + generated = go_maze( + start.x, + start.y, + mz, + r, c, + x_wrapping, + y_wrapping, + seed + ) ) - go_maze( - start.x, - start.y, - mz, - r, c, - x_wrapping, - y_wrapping, - seed - ); + [for(row = generated) each row];