mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-09-12 08:08:30 +02: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
@ -268,3 +268,22 @@ function worldeditadditions.human_time(ms)
|
||||
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])
|
||||
end
|
||||
if #parts >= param_index_seed then
|
||||
seed = tonumber(parts[param_index_seed])
|
||||
if not seed then
|
||||
return false, "Error: Invalid seed value (seeds may only be integers)."
|
||||
end
|
||||
seed = worldeditadditions.makeseed(parts[param_index_seed])
|
||||
end
|
||||
|
||||
replace_node = worldedit.normalize_nodename(replace_node)
|
||||
|
Loading…
Reference in New Issue
Block a user