1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-06 14:56:47 +02:00

add seed parameter

This commit is contained in:
Justin Lin
2020-08-26 20:41:41 +08:00
parent 5ac9581f42
commit f25663c092
2 changed files with 16 additions and 12 deletions

View File

@@ -63,8 +63,9 @@ _rand_dir_table = [
[3, 2, 0, 1], [3, 2, 0, 1],
[3, 2, 1, 0] [3, 2, 1, 0]
]; ];
function rand_dirs() = function rand_dirs(seed) =
_rand_dir_table[round(rands(0, 24, 1)[0])]; let(r = is_undef(seed) ? rands(0, 24, 1) : rands(0, 24, 1, seed))
_rand_dir_table[round(r[0])];
// get x value by dir // get x value by dir
_next_x_table = [1, 0, -1, 0]; _next_x_table = [1, 0, -1, 0];
@@ -152,32 +153,34 @@ function visitable_dirs_from(x, y, maze, rows, columns, x_circular, y_circular)
]; ];
// go maze from (x, y) // go maze from (x, y)
function go_maze(x, y, maze, rows, columns, x_circular = false, y_circular = false) = function go_maze(x, y, maze, rows, columns, x_circular = false, y_circular = false, seed) =
// have visitable dirs? // have visitable dirs?
len(visitable_dirs_from(x, y, maze, rows, columns, x_circular, y_circular)) == 0 ? len(visitable_dirs_from(x, y, maze, rows, columns, x_circular, y_circular)) == 0 ?
set_visited(x, y, maze) // road closed set_visited(x, y, maze) // road closed
: walk_around_from( : walk_around_from(
x, y, x, y,
rand_dirs(), rand_dirs(x + y + seed),
set_visited(x, y, maze), set_visited(x, y, maze),
rows, columns, rows, columns,
x_circular, y_circular x_circular, y_circular,
seed = seed
); );
// try four directions // try four directions
function walk_around_from(x, y, dirs, maze, rows, columns, x_circular, y_circular, i = 4) = function walk_around_from(x, y, dirs, maze, rows, columns, x_circular, y_circular, i = 4, seed) =
// all done? // all done?
i > 0 ? i > 0 ?
// not yet // not yet
walk_around_from(x, y, dirs, walk_around_from(x, y, dirs,
// try one direction // try one direction
try_routes_from(x, y, dirs[4 - i], maze, rows, columns, x_circular, y_circular), try_routes_from(x, y, dirs[4 - i], maze, rows, columns, x_circular, y_circular, seed),
rows, columns, rows, columns,
x_circular, y_circular, x_circular, y_circular,
i - 1) i - 1,
seed)
: maze; : maze;
function try_routes_from(x, y, dir, maze, rows, columns, x_circular, y_circular) = function try_routes_from(x, y, dir, maze, rows, columns, x_circular, y_circular, seed) =
// is the dir visitable? // is the dir visitable?
visitable(next_x(x, dir, columns, x_circular), next_y(y, dir, rows, y_circular), maze, rows, columns) ? visitable(next_x(x, dir, columns, x_circular), next_y(y, dir, rows, y_circular), maze, rows, columns) ?
// try the block // try the block
@@ -185,7 +188,8 @@ function try_routes_from(x, y, dir, maze, rows, columns, x_circular, y_circular)
next_x(x, dir, columns, x_circular), next_y(y, dir, rows, y_circular), next_x(x, dir, columns, x_circular), next_y(y, dir, rows, y_circular),
try_block(dir, x, y, maze, rows, columns), try_block(dir, x, y, maze, rows, columns),
rows, columns, rows, columns,
x_circular, y_circular x_circular, y_circular,
seed
) )
// road closed so return maze directly // road closed so return maze directly
: maze; : maze;

View File

@@ -1,8 +1,8 @@
use <experimental/_impl/_mz_blocks_impl.scad>; use <experimental/_impl/_mz_blocks_impl.scad>;
function mz_blocks(start, rows, columns, maze, x_circular = false, y_circular = false) = function mz_blocks(start, rows, columns, maze, x_circular = false, y_circular = false, seed) =
go_maze( go_maze(
start[0], start[1], // starting point start[0], start[1], // starting point
is_undef(maze) ? starting_maze(rows, columns) : maze, is_undef(maze) ? starting_maze(rows, columns) : maze,
rows, columns, x_circular, y_circular rows, columns, x_circular, y_circular, seed
); );