mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-22 15:33:00 +00:00
//maze, //maze3d: Allow non-number seeds
This uses a simple (non-crypto!) hash function to hash non-number strings to a number. Existing seeds are *not* affected - they will still work as before.
This commit is contained in:
parent
39103a046f
commit
fea89f2e3c
2 changed files with 20 additions and 4 deletions
|
@ -268,3 +268,22 @@ function worldeditadditions.human_time(ms)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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!).
|
||||||
|
-- @param {string} str The string to convert.
|
||||||
|
-- @source https://stackoverflow.com/a/2624210/1460422 The idea came from here
|
||||||
|
function worldeditadditions.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
|
||||||
|
|
|
@ -27,10 +27,7 @@ local function parse_params_maze(params_text, is_3d)
|
||||||
path_depth = tonumber(parts[4])
|
path_depth = tonumber(parts[4])
|
||||||
end
|
end
|
||||||
if #parts >= param_index_seed then
|
if #parts >= param_index_seed then
|
||||||
seed = tonumber(parts[param_index_seed])
|
seed = worldeditadditions.makeseed(parts[param_index_seed])
|
||||||
if not seed then
|
|
||||||
return false, "Error: Invalid seed value (seeds may only be integers)."
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
replace_node = worldedit.normalize_nodename(replace_node)
|
replace_node = worldedit.normalize_nodename(replace_node)
|
||||||
|
|
Loading…
Reference in a new issue