From 29223a34558efded3375a273a104d90b817c3c54 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 3 May 2020 17:02:28 +0100 Subject: [PATCH] Patch maze3d to support the path dimennsion settings, but there's some weirdness going on --- worldeditadditions/maze3d.lua | 109 ++++++++++++------ worldeditadditions_commands/commands/maze.lua | 24 ++-- 2 files changed, 91 insertions(+), 42 deletions(-) diff --git a/worldeditadditions/maze3d.lua b/worldeditadditions/maze3d.lua index 73055dc..cbfcd63 100644 --- a/worldeditadditions/maze3d.lua +++ b/worldeditadditions/maze3d.lua @@ -21,15 +21,15 @@ end -- Initialise the world start_time = os.clock() -function generate_maze3d(seed, width, height, depth) - start_time = os.clock() +local function generate_maze3d(seed, width, height, depth, path_length, path_width, path_depth) - print("Generating maze "..width.."x"..height.."x"..depth) + if not path_length then path_length = 2 end + if not path_width then path_width = 1 end + if not path_depth then path_depth = 1 end + + -- print("Generating maze "..width.."x"..height.."x"..depth) math.randomseed(seed) -- seed the random number generator with the system clock - width = width - 1 - height = height - 1 - local world = {} for z = 0, depth, 1 do world[z] = {} @@ -52,22 +52,22 @@ function generate_maze3d(seed, width, height, depth) --print("Nodes left: " .. #nodes) local directions = "" -- the different directions we can move in - if cz - 2 > 0 and world[cz - 2][cy][cx] == "#" then + if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then directions = directions .. "-" end - if cz + 2 < depth and world[cz + 2][cy][cx] == "#" then + if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then directions = directions .. "+" end - if cy - 2 > 0 and world[cz][cy - 2][cx] == "#" then + if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then directions = directions .. "u" end - if cy + 2 < height and world[cz][cy + 2][cx] == "#" then + if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then directions = directions .. "d" end - if cx - 2 > 0 and world[cz][cy][cx - 2] == "#" then + if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then directions = directions .. "l" end - if cx + 2 < width and world[cz][cy][cx + 2] == "#" then + if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then directions = directions .. "r" end @@ -79,31 +79,74 @@ function generate_maze3d(seed, width, height, depth) -- we still have somewhere that we can go local curdirnum = math.random(1, #directions) local curdir = string.sub(directions, curdirnum, curdirnum) + if curdir == "+" then - world[cz + 1][cy][cx] = " " - world[cz + 2][cy][cx] = " " - cz = cz + 2 + for iz = cz,cz+path_length+(path_depth-1) do + for ix = cx,cx+(path_width-1) do + for iy = cy,cy+(path_width-1) do + world[iz][iy][ix] = " " + end + end + end + -- world[cz + 1][cy][cx] = " " + -- world[cz + 2][cy][cx] = " " + cz = cz + path_length elseif curdir == "-" then - world[cz - 1][cy][cx] = " " - world[cz - 2][cy][cx] = " " - cz = cz - 2 + for iz = cz,cz-path_length do + for ix = cx,cx+(path_width-1) do + for iy = cy,cy+(path_width-1) do + world[iz][iy][ix] = " " + end + end + end + -- world[cz - 1][cy][cx] = " " + -- world[cz - 2][cy][cx] = " " + cz = cz - path_length elseif curdir == "u" then - world[cz][cy - 1][cx] = " " - world[cz][cy - 2][cx] = " " - cy = cy - 2 + for iz = cz,cz+(path_depth-1) do + for ix = cx,cx+(path_width-1) do + for iy = cy-path_length,cy do + world[iz][iy][ix] = " " + end + end + end + -- world[cz][cy - 1][cx] = " " + -- world[cz][cy - 2][cx] = " " + cy = cy - path_length elseif curdir == "d" then - world[cz][cy + 1][cx] = " " - world[cz][cy + 2][cx] = " " - cy = cy + 2 + for iz = cz,cz+(path_depth-1) do + for ix = cx,cx+(path_width-1) do + for iy = cy,cy+path_length+(path_width-1) do + world[iz][iy][ix] = " " + end + end + end + -- world[cz][cy + 1][cx] = " " + -- world[cz][cy + 2][cx] = " " + cy = cy + path_length elseif curdir == "l" then - world[cz][cy][cx - 1] = " " - world[cz][cy][cx - 2] = " " - cx = cx - 2 + for iz = cz,cz+(path_depth-1) do + for iy = cy,cy+(path_width-1) do + for ix = cx-path_length,cx do + world[iz][iy][ix] = " " + end + end + end + -- world[cz][cy][cx - 1] = " " + -- world[cz][cy][cx - 2] = " " + cx = cx - path_length elseif curdir == "r" then - world[cz][cy][cx + 1] = " " - world[cz][cy][cx + 2] = " " - cx = cx + 2 + for iz = cz,cz+(path_depth-1) do + for iy = cy,cy+(path_width-1) do + for ix = cx,cx+path_length+(path_width-1) do + world[iz][iy][ix] = " " + end + end + end + -- world[cz][cy][cx + 1] = " " + -- world[cz][cy][cx + 2] = " " + cx = cx + path_length end table.insert(nodes, { x = cx, y = cy, z = cz }) @@ -122,14 +165,12 @@ function generate_maze3d(seed, width, height, depth) end end - end_time = os.clock() - return world end -- local world = maze(os.time(), width, height) -function worldedit.maze3d(pos1, pos2, target_node, seed) +function worldeditadditions.maze3d(pos1, pos2, target_node, seed, path_length, path_width, path_depth) pos1, pos2 = worldedit.sort_pos(pos1, pos2) -- pos2 will always have the highest co-ordinates now @@ -159,7 +200,7 @@ function worldedit.maze3d(pos1, pos2, target_node, seed) minetest.log("action", "Generating "..extent.x.."x"..extent.z.."x"..extent.z.." 3d maze from pos1 " .. worldeditadditions.vector.tostring(pos1).." to pos2 "..worldeditadditions.vector.tostring(pos2)) - local maze = generate_maze3d(seed, extent.z, extent.y, extent.x) -- x & need to be the opposite way around to how we index it + local maze = generate_maze3d(seed, extent.z, extent.y, extent.x, path_length, path_width, path_depth) -- x & z need to be the opposite way around to how we index it -- printspace3d(maze, extent.z, extent.y, extent.x) -- z y x is the preferred loop order, but that isn't really possible here diff --git a/worldeditadditions_commands/commands/maze.lua b/worldeditadditions_commands/commands/maze.lua index 9e3089a..d5d9ca0 100644 --- a/worldeditadditions_commands/commands/maze.lua +++ b/worldeditadditions_commands/commands/maze.lua @@ -6,7 +6,7 @@ local we_c = worldeditadditions_commands -- ██ ██ ██ ██ ██ ███ ██ -- ██ ██ ██ ██ ███████ ███████ -local function parse_params_maze(params_text) +local function parse_params_maze(params_text, is_3d) if not params_text then return nil, nil, nil, nil end @@ -17,6 +17,11 @@ local function parse_params_maze(params_text) local seed = os.time() local path_length = 2 local path_width = 1 + local path_depth = 1 + + local param_index_seed = 4 + if is_3d then param_index_seed = 5 end + if #parts >= 2 then path_length = tonumber(parts[2]) @@ -24,13 +29,16 @@ local function parse_params_maze(params_text) if #parts >= 3 then path_width = tonumber(parts[3]) end - if #parts >= 4 then - seed = tonumber(parts[4]) + if #parts >= 4 and is_3d then + path_depth = tonumber(parts[4]) + end + if #parts >= param_index_seed then + seed = tonumber(parts[param_index_seed]) end replace_node = worldedit.normalize_nodename(replace_node) - return replace_node, seed, path_length, path_width + return replace_node, seed, path_length, path_width, path_depth end minetest.register_chatcommand("/maze", { @@ -80,11 +88,11 @@ minetest.register_chatcommand("/maze", { -- ██ ██ ██ ██ ███████ ███████ ██████ ██████ minetest.register_chatcommand("/maze3d", { - params = " []", - description = "Generates a 3d maze covering the currently selected area (must be at least 3x3x3) with replace_node as the walls. Optionally takes a (integer) seed.", + params = " [ [ [ []]]]", + description = "Generates a 3d maze covering the currently selected area (must be at least 3x3x3) with replace_node as the walls. Optionally takes a (integer) seed and the path length, width, and depth (see the documentation in the worldeditadditions README for more information).", privs = { worldedit = true }, func = we_c.safe_region(function(name, params_text) - local replace_node, seed, has_seed = parse_params_maze(params_text) + local replace_node, seed, path_length, path_width, path_depth = parse_params_maze(params_text) if not replace_node then worldedit.player_notify(name, "Error: Invalid node name.") @@ -97,7 +105,7 @@ minetest.register_chatcommand("/maze3d", { if not seed then seed = os.time() end local start_time = os.clock() - local replaced = worldedit.maze3d(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed) + local replaced = worldeditadditions.maze3d(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed, path_length, path_width, path_depth) local time_taken = os.clock() - start_time worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s")