mirror of
https://github.com/minetest-mods/mesecons.git
synced 2025-07-06 01:20:23 +02:00
Use metatable to cache light update conductors
This commit is contained in:
@ -368,17 +368,9 @@ function mesecon.is_power_off(pos, rulename)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The set of conductor states which require light updates when they change.
|
-- Calculates conductors whose lighting characteristics change depending on their state.
|
||||||
local light_update_conductors
|
-- Puts into the given set the names of all such conducting nodes.
|
||||||
|
local function calculate_light_update_conductors(set)
|
||||||
-- Calculate the contents of the above set if they have not been calculated.
|
|
||||||
local function find_light_update_conductors()
|
|
||||||
-- The expensive calculation is only done the first time.
|
|
||||||
if light_update_conductors then return end
|
|
||||||
|
|
||||||
light_update_conductors = {}
|
|
||||||
|
|
||||||
-- Find conductors whose lighting characteristics change depending on their state.
|
|
||||||
local checked = {}
|
local checked = {}
|
||||||
for name, def in pairs(minetest.registered_nodes) do
|
for name, def in pairs(minetest.registered_nodes) do
|
||||||
local conductor = mesecon.get_conductor(name)
|
local conductor = mesecon.get_conductor(name)
|
||||||
@ -401,9 +393,9 @@ local function find_light_update_conductors()
|
|||||||
or def.light_source ~= other_def.light_source then
|
or def.light_source ~= other_def.light_source then
|
||||||
-- The light characteristics change depending on the state.
|
-- The light characteristics change depending on the state.
|
||||||
-- The states are added to the set.
|
-- The states are added to the set.
|
||||||
light_update_conductors[name] = true
|
set[name] = true
|
||||||
for _, other_state in ipairs(other_states) do
|
for _, other_state in ipairs(other_states) do
|
||||||
light_update_conductors[other_state] = true
|
set[other_state] = true
|
||||||
checked[other_state] = true
|
checked[other_state] = true
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
@ -414,13 +406,21 @@ local function find_light_update_conductors()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- The set of conductor states that require light updates when they change.
|
||||||
|
local light_update_conductors = setmetatable({}, {
|
||||||
|
__index = function(self, key)
|
||||||
|
-- Calculate the set. This only happens once.
|
||||||
|
setmetatable(self, nil)
|
||||||
|
calculate_light_update_conductors(self)
|
||||||
|
return self[key]
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
-- Turn off an equipotential section starting at `pos`, which outputs in the direction of `link`.
|
-- Turn off an equipotential section starting at `pos`, which outputs in the direction of `link`.
|
||||||
-- Breadth-first search. Map is abstracted away in a voxelmanip.
|
-- Breadth-first search. Map is abstracted away in a voxelmanip.
|
||||||
-- Follow all all conductor paths replacing conductors that were already
|
-- Follow all all conductor paths replacing conductors that were already
|
||||||
-- looked at, activating / changing all effectors along the way.
|
-- looked at, activating / changing all effectors along the way.
|
||||||
function mesecon.turnon(pos, link)
|
function mesecon.turnon(pos, link)
|
||||||
find_light_update_conductors()
|
|
||||||
|
|
||||||
local frontiers = fifo_queue.new()
|
local frontiers = fifo_queue.new()
|
||||||
frontiers:add({pos = pos, link = link})
|
frontiers:add({pos = pos, link = link})
|
||||||
local pos_can_be_skipped = {}
|
local pos_can_be_skipped = {}
|
||||||
@ -482,8 +482,6 @@ end
|
|||||||
-- depth = indicates order in which signals wire fired, higher is later
|
-- depth = indicates order in which signals wire fired, higher is later
|
||||||
-- }
|
-- }
|
||||||
function mesecon.turnoff(pos, link)
|
function mesecon.turnoff(pos, link)
|
||||||
find_light_update_conductors()
|
|
||||||
|
|
||||||
local frontiers = fifo_queue.new()
|
local frontiers = fifo_queue.new()
|
||||||
frontiers:add({pos = pos, link = link})
|
frontiers:add({pos = pos, link = link})
|
||||||
local signals = {}
|
local signals = {}
|
||||||
|
Reference in New Issue
Block a user