From 13e0337da7a31da565b9beb4cec1af45072c6f0e Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sat, 26 Nov 2022 11:06:37 -0500 Subject: [PATCH] Randomize digiline IDs --- digilines.lua | 13 ++++++------- lock.lua | 12 ++---------- misc.lua | 20 ++++++++++++++------ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/digilines.lua b/digilines.lua index 259c3a1..a625fb1 100644 --- a/digilines.lua +++ b/digilines.lua @@ -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 diff --git a/lock.lua b/lock.lua index f811441..866bccf 100644 --- a/lock.lua +++ b/lock.lua @@ -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 diff --git a/misc.lua b/misc.lua index 23a860a..85e626b 100644 --- a/misc.lua +++ b/misc.lua @@ -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