Fix bug causing some parts of mazes to be unexplored
Also add seed conversion function
This commit is contained in:
parent
5d26acf992
commit
f3d4d46c43
3 changed files with 28 additions and 10 deletions
10
main.lua
10
main.lua
|
@ -93,7 +93,7 @@ for i=1,#arg do
|
||||||
settings.depth = tonumber(arg[i])
|
settings.depth = tonumber(arg[i])
|
||||||
elseif arg[i] == "--seed" or arg[i] == "-s" then
|
elseif arg[i] == "--seed" or arg[i] == "-s" then
|
||||||
i = i + 1
|
i = i + 1
|
||||||
settings.seed = tonumber(arg[i])
|
settings.seed = strings.makeseed(arg[i])
|
||||||
elseif arg[i] == "--filename" or arg[i] == "-f" then
|
elseif arg[i] == "--filename" or arg[i] == "-f" then
|
||||||
i = i + 1
|
i = i + 1
|
||||||
settings.filename = arg[i]
|
settings.filename = arg[i]
|
||||||
|
@ -134,7 +134,7 @@ local function maze_text()
|
||||||
settings.path_width
|
settings.path_width
|
||||||
)
|
)
|
||||||
maze2d.printspace(world, settings.width, settings.height)
|
maze2d.printspace(world, settings.width, settings.height)
|
||||||
print("Generation completed in " .. time .. "ms.")
|
print("Generation completed in " .. time .. "ms with seed "..settings.seed..".")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function maze_svg()
|
local function maze_svg()
|
||||||
|
@ -159,7 +159,7 @@ local function maze_svg()
|
||||||
local out = io.open(settings.filename, "w")
|
local out = io.open(settings.filename, "w")
|
||||||
out:write(svg)
|
out:write(svg)
|
||||||
out:close()
|
out:close()
|
||||||
print("Generation completed in "..time.."ms.")
|
print("Generation completed in "..time.."ms with seed "..settings.seed..".")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ local function maze3d_text()
|
||||||
|
|
||||||
maze3d.printspace(world, settings.width, settings.height, settings.depth)
|
maze3d.printspace(world, settings.width, settings.height, settings.depth)
|
||||||
|
|
||||||
print("Generation completed in " .. time .. "ms.")
|
print("Generation completed in " .. time .. "ms with seed "..settings.seed..".")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function maze3d_openscad()
|
local function maze3d_openscad()
|
||||||
|
@ -191,7 +191,7 @@ local function maze3d_openscad()
|
||||||
)
|
)
|
||||||
|
|
||||||
if not settings.filename then
|
if not settings.filename then
|
||||||
print(a.fred(a.hicol("Error: No outupt filename specified.")))
|
print(a.fred(a.hicol("Error: No output filename specified.")))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
10
maze.lua
10
maze.lua
|
@ -59,11 +59,12 @@ end
|
||||||
|
|
||||||
-- Initialise the world
|
-- Initialise the world
|
||||||
|
|
||||||
function M.generate_maze(seed, width, height, path_length, path_width)
|
function M.generate_maze(seed, width, height, path_length, path_width, branching_factor)
|
||||||
start_time = os.clock()
|
start_time = os.clock()
|
||||||
|
|
||||||
if not path_length then path_length = 2 end
|
if not path_length then path_length = 2 end
|
||||||
if not path_width then path_width = 1 end
|
if not path_width then path_width = 1 end
|
||||||
|
if not branching_factor then branching_factor = 6 end
|
||||||
|
|
||||||
math.randomseed(seed) -- seed the random number generator with the system clock
|
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||||
|
|
||||||
|
@ -108,7 +109,7 @@ function M.generate_maze(seed, width, height, path_length, path_width)
|
||||||
end
|
end
|
||||||
-- print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
-- print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
||||||
|
|
||||||
local shift_attention = math.random(0, 9)
|
local shift_attention = math.random(0, branching_factor)
|
||||||
|
|
||||||
if #directions > 0 then
|
if #directions > 0 then
|
||||||
-- we still have somewhere that we can go
|
-- we still have somewhere that we can go
|
||||||
|
@ -156,9 +157,8 @@ function M.generate_maze(seed, width, height, path_length, path_width)
|
||||||
-- print("Now at ("..cx..", "..cy..")")
|
-- print("Now at ("..cx..", "..cy..")")
|
||||||
|
|
||||||
-- If the number of directions we could travel in was 1, then we've just travelled in the last remaining direction possible
|
-- If the number of directions we could travel in was 1, then we've just travelled in the last remaining direction possible
|
||||||
if #directions > 1 then
|
-- THIS ISN'T TRUE, because we're analysing the old position and then inserting the new one - so we have to insert regardless
|
||||||
table.insert(nodes, { x = cx, y = cy })
|
table.insert(nodes, { x = cx, y = cy })
|
||||||
end
|
|
||||||
else
|
else
|
||||||
--print("The node at " .. curnode .. " is a dead end.")
|
--print("The node at " .. curnode .. " is a dead end.")
|
||||||
table.remove(nodes, curnode)
|
table.remove(nodes, curnode)
|
||||||
|
|
|
@ -8,4 +8,22 @@ function M.ends_with(str, ending)
|
||||||
return ending == "" or str:sub(-#ending) == ending
|
return ending == "" or str:sub(-#ending) == ending
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Makes a seed from a string.
|
||||||
|
-- If the input is a number, it is returned as-is.
|
||||||
|
-- If the input is a string and can be converted to a number with tonumber(),
|
||||||
|
-- the output of tonumber() is returned.
|
||||||
|
-- Otherwise, the string is converted to a number via a simple hashing algorithm
|
||||||
|
-- (caution: certainlly NOT crypto-secure!).
|
||||||
|
|
||||||
|
-- @source https://stackoverflow.com/a/2624210/1460422
|
||||||
|
function M.makeseed(str)
|
||||||
|
if type(str) == "number" then return str end
|
||||||
|
if tonumber(str) ~= nil then return tonumber(str) end
|
||||||
|
local result = 0
|
||||||
|
for i = 1, #str do
|
||||||
|
result = (result*91) + (string.byte(str:sub(i, i)) * 31)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in a new issue