From 59b474df5153d90bf2addd9f5210df6af030cf66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20=C3=85str=C3=B6m?= Date: Fri, 25 Oct 2024 17:28:02 +0200 Subject: [PATCH] Add mcl_util.queue and use in mods --- mods/CORE/mcl_util/init.lua | 24 +++++++++++++++ mods/ENTITIES/mcl_minecarts/rails.lua | 27 ++--------------- mods/ITEMS/REDSTONE/mcl_redstone/logic.lua | 35 ++++------------------ mods/ITEMS/mcl_portals/portal_nether.lua | 25 +--------------- mods/ITEMS/mcl_trees/api.lua | 27 ++--------------- 5 files changed, 35 insertions(+), 103 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 87290d5bd..41482aaaf 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -1382,4 +1382,28 @@ function mcl_util.to_roman(number) return r end +function mcl_util.queue() + return { + front = 1, + back = 1, + queue = {}, + enqueue = function(self, value) + self.queue[self.back] = value + self.back = self.back + 1 + end, + dequeue = function(self) local value = self.queue[self.front] + if not value then + return + end + self.queue[self.front] = nil + self.front = self.front + 1 + return value + end, + size = function(self) + return self.back - self.front + end, + } +end + + dofile(minetest.get_modpath(minetest.get_current_modname()).."/compat.lua") diff --git a/mods/ENTITIES/mcl_minecarts/rails.lua b/mods/ENTITIES/mcl_minecarts/rails.lua index f046e43b1..ad06014f6 100644 --- a/mods/ENTITIES/mcl_minecarts/rails.lua +++ b/mods/ENTITIES/mcl_minecarts/rails.lua @@ -73,29 +73,6 @@ minetest.register_on_mods_loaded(function() end end) -local function queue() - return { - front = 1, - back = 1, - queue = {}, - enqueue = function(self, value) - self.queue[self.back] = value - self.back = self.back + 1 - end, - dequeue = function(self) local value = self.queue[self.front] - if not value then - return - end - self.queue[self.front] = nil - self.front = self.front + 1 - return value - end, - size = function(self) - return self.back - self.front - end, - } -end - local directions = { { rail = vector.new(1, 0, 0) }, { rail = vector.new(-1, 0, 0) }, @@ -135,8 +112,8 @@ local function propagate_golden_rail_power(pos, new_power, old_power, powered_on param2_data[ind] = power end - local clear_queue = queue() - local fill_queue = queue() + local clear_queue = mcl_util.queue() + local fill_queue = mcl_util.queue() if old_power then clear_queue:enqueue({ pos = pos, power = old_power }) end diff --git a/mods/ITEMS/REDSTONE/mcl_redstone/logic.lua b/mods/ITEMS/REDSTONE/mcl_redstone/logic.lua index fc404ecf3..36b608376 100644 --- a/mods/ITEMS/REDSTONE/mcl_redstone/logic.lua +++ b/mods/ITEMS/REDSTONE/mcl_redstone/logic.lua @@ -1,26 +1,3 @@ -local function queue() - return { - front = 1, - back = 1, - queue = {}, - enqueue = function(self, value) - self.queue[self.back] = value - self.back = self.back + 1 - end, - dequeue = function(self) local value = self.queue[self.front] - if not value then - return - end - self.queue[self.front] = nil - self.front = self.front + 1 - return value - end, - size = function(self) - return self.back - self.front - end, - } -end - -- Logical wireflags by contentid local lwireflag_tab = {} @@ -271,8 +248,8 @@ end -- Update neighbouring wires and components at pos. Oldnode is the previous -- node at the position. function update_neighbours(pos, oldnode) - local fill_queue = queue() - local clear_queue = queue() + local fill_queue = mcl_util.queue() + local clear_queue = mcl_util.queue() local node = minetest.get_node(pos) local ndef = minetest.registered_nodes[node.name] local oldndef = oldnode and minetest.registered_nodes[oldnode.name] @@ -326,8 +303,8 @@ function update_neighbours(pos, oldnode) end local function opaque_update_neighbours(pos, added) - local fill_queue = queue() - local clear_queue = queue() + local fill_queue = mcl_util.queue() + local clear_queue = mcl_util.queue() local function update_wire(pos) local oldpower = minetest.get_node(pos).param2 @@ -356,8 +333,8 @@ local function opaque_update_neighbours(pos, added) end local function update_wire(pos, oldnode) - local fill_queue = queue() - local clear_queue = queue() + local fill_queue = mcl_util.queue() + local clear_queue = mcl_util.queue() local node = minetest.get_node(pos) local power = get_node_power_2(pos) diff --git a/mods/ITEMS/mcl_portals/portal_nether.lua b/mods/ITEMS/mcl_portals/portal_nether.lua index bd571e9b3..6f93e3463 100644 --- a/mods/ITEMS/mcl_portals/portal_nether.lua +++ b/mods/ITEMS/mcl_portals/portal_nether.lua @@ -135,29 +135,6 @@ local function orient(pos, param2) return pos end -local function queue() - return { - front = 1, - back = 1, - queue = {}, - enqueue = function(self, value) - self.queue[self.back] = value - self.back = self.back + 1 - end, - dequeue = function(self) local value = self.queue[self.front] - if not value then - return - end - self.queue[self.front] = nil - self.front = self.front + 1 - return value - end, - size = function(self) - return self.back - self.front - end, - } -end - -- Check if node is replacable with a portal node. local function replacable_with_portal(name) return name == "air" or @@ -199,7 +176,7 @@ local function light_nether_portal(pos, param2) end local nodes = {} - local queue = queue() + local queue = mcl_util.queue() local checked = {} queue:enqueue(pos) diff --git a/mods/ITEMS/mcl_trees/api.lua b/mods/ITEMS/mcl_trees/api.lua index e8d96a828..1fdadaa77 100644 --- a/mods/ITEMS/mcl_trees/api.lua +++ b/mods/ITEMS/mcl_trees/api.lua @@ -9,29 +9,6 @@ local wood_groups = { local wood_sounds = mcl_sounds.node_sound_wood_defaults() -local function queue() - return { - front = 1, - back = 1, - queue = {}, - enqueue = function(self, value) - self.queue[self.back] = value - self.back = self.back + 1 - end, - dequeue = function(self) local value = self.queue[self.front] - if not value then - return - end - self.queue[self.front] = nil - self.front = self.front + 1 - return value - end, - size = function(self) - return self.back - self.front - end, - } -end - -- Make leaves which do not have a log within 6 nodes orphan. local function update_far_away_leaves(pos) local logs = minetest.find_nodes_in_area(pos:subtract(12), pos:add(12), "group:tree") @@ -116,8 +93,8 @@ local function update_leaves(pos, old_distance) param2_data[ind] = (distance + 1) * 32 + param2_data[ind] % 32 end - local clear_queue = queue() - local fill_queue = queue() + local clear_queue = mcl_util.queue() + local fill_queue = mcl_util.queue() if old_distance then clear_queue:enqueue({ pos = pos, distance = old_distance }) end