From 18d3757079fb75eb878a36fa7771ae4fbcd755ef Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 27 Aug 2022 16:19:09 +0800 Subject: [PATCH] refactor for adding a directions function --- src/maze/_impl/_mz_square_cells_impl.scad | 19 ++++++++++--------- src/maze/mz_square.scad | 1 + src/maze/mz_square_cells.scad | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/maze/_impl/_mz_square_cells_impl.scad b/src/maze/_impl/_mz_square_cells_impl.scad index 975cba1c..7662b67f 100644 --- a/src/maze/_impl/_mz_square_cells_impl.scad +++ b/src/maze/_impl/_mz_square_cells_impl.scad @@ -43,8 +43,8 @@ _rand_dir_table = [ [3, 2, 0, 1], [3, 2, 1, 0] ]; -function rand_dirs(c, seed) = - let(r = is_undef(seed) ? rands(0, 23, 1) : rands(0, 23, 1, c + seed)) +function rand_dirs(x, y, cells, seed) = + let(r = is_undef(seed) ? rands(0, 23, 1) : rands(0, 23, 1, x + y * len(cells[0]) + seed)) _rand_dir_table[round(r[0])]; // get x value by dir @@ -91,17 +91,17 @@ function carve(dir, x, y, cells, rows, columns) = /*dir 3*/ carve_bottom(x, y, cells, rows); // go maze from (x, y) -function go_maze(x, y, cells, rows, columns, x_wrapping, y_wrapping, seed) = +function go_maze(x, y, cells, rows, columns, x_wrapping, y_wrapping, directions, seed) = let( - r_dirs = rand_dirs(x + y * columns, seed), + r_dirs = directions(x, y, cells, seed), nxcells0 = set_visited(x, y, cells), - nxcells1 = next_cells(x, y, r_dirs[0], nxcells0, rows, columns, x_wrapping, y_wrapping, seed), - nxcells2 = next_cells(x, y, r_dirs[1], nxcells1, rows, columns, x_wrapping, y_wrapping, seed), - nxcells3 = next_cells(x, y, r_dirs[2], nxcells2, rows, columns, x_wrapping, y_wrapping, seed) + nxcells1 = next_cells(x, y, r_dirs[0], nxcells0, rows, columns, x_wrapping, y_wrapping, directions, seed), + nxcells2 = next_cells(x, y, r_dirs[1], nxcells1, rows, columns, x_wrapping, y_wrapping, directions, seed), + nxcells3 = next_cells(x, y, r_dirs[2], nxcells2, rows, columns, x_wrapping, y_wrapping, directions, seed) ) - next_cells(x, y, r_dirs[3], nxcells3, rows, columns, x_wrapping, y_wrapping, seed); + next_cells(x, y, r_dirs[3], nxcells3, rows, columns, x_wrapping, y_wrapping, directions, seed); -function next_cells(x, y, dir, cells, rows, columns, x_wrapping, y_wrapping, seed) = +function next_cells(x, y, dir, cells, rows, columns, x_wrapping, y_wrapping, directions, seed) = let( nx = next_x(x, dir, columns, x_wrapping), ny = next_y(y, dir, rows, y_wrapping) @@ -113,5 +113,6 @@ function next_cells(x, y, dir, cells, rows, columns, x_wrapping, y_wrapping, see carve(dir, x, y, cells, rows, columns), rows, columns, x_wrapping, y_wrapping, + directions, seed ); \ No newline at end of file diff --git a/src/maze/mz_square.scad b/src/maze/mz_square.scad index 8cd4f8ca..32b941cf 100644 --- a/src/maze/mz_square.scad +++ b/src/maze/mz_square.scad @@ -24,5 +24,6 @@ function mz_square(rows, columns, start = [0, 0], init_cells, x_wrapping = false len(mz[0]), x_wrapping, y_wrapping, + function(x, y, cells, seed) rand_dirs(x, y, cells, seed), seed ); diff --git a/src/maze/mz_square_cells.scad b/src/maze/mz_square_cells.scad index 61f80a2e..5626de0b 100644 --- a/src/maze/mz_square_cells.scad +++ b/src/maze/mz_square_cells.scad @@ -24,6 +24,7 @@ function mz_square_cells(rows, columns, start = [0, 0], init_cells, x_wrapping = r, c, x_wrapping, y_wrapping, + function(x, y, cells, seed) rand_dirs(x, y, cells, seed), seed ) )