Randomize digiline IDs

This commit is contained in:
Jude Melton-Houghton 2022-11-26 11:06:37 -05:00
parent ea77c99b7e
commit 13e0337da7
3 changed files with 22 additions and 23 deletions

View File

@ -30,9 +30,9 @@
]]
local use = ...
local storage, get_node_maybe_load,
local get_random_id, get_node_maybe_load,
CONTAINER_NAME_PREFIX, DIGILINE_OFFSET = use("misc", {
"storage", "get_node_maybe_load",
"get_random_id", "get_node_maybe_load",
"CONTAINER_NAME_PREFIX", "DIGILINE_OFFSET"
})
local get_related_container, get_related_inside,
@ -65,15 +65,14 @@ local DIGILINE_NODE_RULES = {
-- is not a container, nil is returned.
local function get_digiline_id(container_pos)
local meta = minetest.get_meta(container_pos)
local id = tonumber((meta:get("area_containers:digiline_id")))
local id = meta:get("area_containers:digiline_id")
if not id then
local node = get_node_maybe_load(container_pos)
local prefix = string.sub(node.name, 1, #CONTAINER_NAME_PREFIX)
if prefix == CONTAINER_NAME_PREFIX then
-- It's a container; allocate the ID for the first use.
id = storage:get_int("next_digiline_id")
storage:set_int("next_digiline_id", id + 1)
meta:set_int("area_containers:digiline_id", id)
-- It's a container; get the ID for the first use.
id = get_random_id()
meta:set_string("area_containers:digiline_id", id)
end
end
return id

View File

@ -31,18 +31,10 @@
]]
local use = ...
local rng = use("misc", {"rng"})
local get_random_id = use("misc", {"get_random_id"})
local exports = {}
-- Returns a unique lock ID.
local function get_next_lock_id()
-- Generate 64 random bits and encode them in hexadecimal:
return string.format("%08x%08x",
rng:next() + 2147483648,
rng:next(0, 131071) * 32768 + math.random(0, 32767))
end
-- Returns whether the named player is an admin of the node owned as given.
-- A nil owner means that the node is unowned.
local function user_can_use(player_name, owner)
@ -83,7 +75,7 @@ function exports.set_lock(pos, user)
end
meta:set_string("area_containers:lock",
meta:get("area_containers:lock_inactive") or get_next_lock_id())
meta:get("area_containers:lock_inactive") or get_random_id())
meta:set_string("area_containers:lock_inactive", "")
-- Take ownership if it's unowned:
if not owner then meta:set_string("owner", player_name) end

View File

@ -44,14 +44,14 @@ local MAPGEN_LIMIT =
local CHUNKSIZE = tonumber(minetest.get_mapgen_setting("chunksize")) or 5
exports.storage = minetest.get_mod_storage()
-- A randomly seeded instance of PcgRandom. I'm just trying to mix up a few
-- entropy sources here.
exports.rng = PcgRandom(math.random(0, 32767))
exports.rng = PcgRandom(math.random(0, 32767) + exports.rng:next())
exports.rng = PcgRandom(os.time() + exports.rng:next())
exports.rng = PcgRandom(os.clock() + exports.rng:next())
local rng = PcgRandom(math.random(0, 32767))
rng = PcgRandom(math.random(0, 32767) + rng:next())
rng = PcgRandom(os.time() + rng:next())
rng = PcgRandom(os.clock() + rng:next())
exports.storage = minetest.get_mod_storage()
exports.translate = minetest.get_translator("area_containers")
@ -75,6 +75,14 @@ end
-- Does and returns nothing.
function exports.null_func() end
-- Returns a (most likely) unique random ID string.
function exports.get_random_id()
-- Generate 64 random bits and encode them in hexadecimal:
return string.format("%08x%08x",
rng:next() + 2147483648,
rng:next(0, 131071) * 32768 + math.random(0, 32767))
end
-- Rounds the number down to the nearest multiple of the blocksize.
function exports.floor_blocksize(a)
return math.floor(a / 16) * 16