summaryrefslogtreecommitdiff
path: root/mesecons
diff options
context:
space:
mode:
Diffstat (limited to 'mesecons')
-rw-r--r--mesecons/actionqueue.lua6
-rw-r--r--mesecons/init.lua16
-rw-r--r--mesecons/internal.lua216
-rw-r--r--mesecons/legacy.lua38
-rw-r--r--mesecons/util.lua145
5 files changed, 203 insertions, 218 deletions
diff --git a/mesecons/actionqueue.lua b/mesecons/actionqueue.lua
index e300ea7..f3479ce 100644
--- a/mesecons/actionqueue.lua
+++ b/mesecons/actionqueue.lua
@@ -87,7 +87,11 @@ minetest.register_globalstep(function (dtime)
end)
function mesecon.queue:execute(action)
- mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
+ -- ignore if action queue function name doesn't exist,
+ -- (e.g. in case the action queue savegame was written by an old mesecons version)
+ if mesecon.queue.funcs[action.func] then
+ mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
+ end
end
diff --git a/mesecons/init.lua b/mesecons/init.lua
index 6b36e69..7da3583 100644
--- a/mesecons/init.lua
+++ b/mesecons/init.lua
@@ -70,6 +70,8 @@ dofile(minetest.get_modpath("mesecons").."/internal.lua");
-- these are the only functions you need to remember
mesecon.queue:add_function("receptor_on", function (pos, rules)
+ mesecon.vm_begin()
+
rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later
@@ -90,6 +92,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon.turnon(np, rulename)
end
end
+
+ mesecon.vm_commit()
end)
function mesecon.receptor_on(pos, rules)
@@ -112,10 +116,16 @@ mesecon.queue:add_function("receptor_off", function (pos, rules)
local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do
- if not mesecon.connected_to_receptor(np, mesecon.invertRule(rule)) then
- mesecon.turnoff(np, rulename)
+ mesecon.vm_begin()
+ mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
+
+ -- Turnoff returns true if turnoff process was successful, no onstate receptor
+ -- was found along the way. Commit changes that were made in voxelmanip. If turnoff
+ -- returns true, an onstate receptor was found, abort voxelmanip transaction.
+ if (mesecon.turnoff(np, rulename)) then
+ mesecon.vm_commit()
else
- mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
+ mesecon.vm_abort()
end
end
end
diff --git a/mesecons/internal.lua b/mesecons/internal.lua
index 4ef2b18..e5ba91e 100644
--- a/mesecons/internal.lua
+++ b/mesecons/internal.lua
@@ -37,11 +37,6 @@
-- HIGH-LEVEL Internals
-- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way
-- mesecon.is_power_off(pos) --> Returns true if pos does not emit power in any way
--- mesecon.turnon(pos, link) --> link is the input rule that caused calling turnon, turns on every connected node, iterative
--- mesecon.turnoff(pos, link) --> link is the input rule that caused calling turnoff, turns off every connected node, iterative
--- mesecon.connected_to_receptor(pos, link) --> Returns true if pos is connected to a receptor directly or via conductors, iterative
--- mesecon.rules_link(output, input, dug_outputrules) --> Returns true if outputposition + outputrules = inputposition and inputposition + inputrules = outputposition (if the two positions connect)
--- mesecon.rules_link_anydir(outp., inp., d_outpr.) --> Same as rules mesecon.rules_link but also returns true if output and input are swapped
-- mesecon.is_powered(pos) --> Returns true if pos is powered by a receptor or a conductor
-- RULES ROTATION helpers
@@ -371,52 +366,32 @@ function mesecon.is_power_off(pos, rulename)
return false
end
+-- 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.
+-- Follow all all conductor paths replacing conductors that were already
+-- looked at, activating / changing all effectors along the way.
function mesecon.turnon(pos, link)
local frontiers = {{pos = pos, link = link}}
local depth = 1
- while frontiers[depth] do
- local f = frontiers[depth]
+ while frontiers[1] do
+ local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos)
- -- area not loaded, postpone action
if not node then
- mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
+ -- Area does not exist; do nothing
elseif mesecon.is_conductor_off(node, f.link) then
local rules = mesecon.conductor_get_rules(node)
- -- Success: If false, at least one neighboring node is unloaded,
- -- postpone turning on action
- local success = true
- local neighborlinks = {}
-
- -- call turnon on neighbors
+ -- Call turnon on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
-
- -- Neighboring node not loaded, postpone turning on current node
- -- since we can't even know if neighboring node has matching rules
- if not mesecon.get_node_force(np) then
- success = false
- break
- else
- neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
+ for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
+ table.insert(frontiers, {pos = np, link = l})
end
end
- if success then
- minetest.swap_node(f.pos, {name = mesecon.get_conductor_on(node, f.link),
- param2 = node.param2})
-
- for npos, links in pairs(neighborlinks) do
- -- links = all links to node, l = each single link
- for _, l in ipairs(links) do
- table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
- end
- end
- else
- mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
- end
+ mesecon.swap_node_force(f.pos, mesecon.get_conductor_on(node, f.link))
elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth)
if mesecon.is_effector_off(node.name) then
@@ -427,151 +402,77 @@ function mesecon.turnon(pos, link)
end
end
-mesecon.queue:add_function("turnon", function(pos, rulename, recdepth)
- mesecon.turnon(pos, rulename, recdepth)
-end)
-
+-- Turn on an equipotential section starting at `pos`, which outputs in the direction of `link`.
+-- Breadth-first search. Map is abstracted away in a voxelmanip.
+-- Follow all all conductor paths replacing conductors that were already
+-- looked at, deactivating / changing all effectors along the way.
+-- In case an onstate receptor is discovered, abort the process by returning false, which will
+-- cause `receptor_off` to discard all changes made in the voxelmanip.
+-- Contrary to turnon, turnoff has to cache all change and deactivate signals so that they will only
+-- be called in the very end when we can be sure that no conductor was found along the path.
+--
+-- Signal table entry structure:
+-- {
+-- pos = position of effector,
+-- node = node descriptor (name, param1 and param2),
+-- link = link the effector is connected to,
+-- depth = indicates order in which signals wire fired, higher is later
+-- }
function mesecon.turnoff(pos, link)
local frontiers = {{pos = pos, link = link}}
+ local signals = {}
local depth = 1
- while frontiers[depth] do
- local f = frontiers[depth]
+ while frontiers[1] do
+ local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos)
- -- area not loaded, postpone action
if not node then
- mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
+ -- Area does not exist; do nothing
elseif mesecon.is_conductor_on(node, f.link) then
local rules = mesecon.conductor_get_rules(node)
-
- -- Success: If false, at least one neighboring node is unloaded,
- -- postpone turning on action
- local success = true
- local neighborlinks = {}
-
- -- call turnoff on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
- -- Neighboring node not loaded, postpone turning off current node
- -- since we can't even know if neighboring node has matching rules
- if not mesecon.get_node_force(np) then
- success = false
- break
- else
- neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
- end
- end
-
- if success then
- minetest.swap_node(f.pos, {name = mesecon.get_conductor_off(node, f.link),
- param2 = node.param2})
-
- for npos, links in pairs(neighborlinks) do
- -- links = all links to node, l = each single link
- for _, l in ipairs(links) do
- table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
+ -- Check if an onstate receptor is connected. If that is the case,
+ -- abort this turnoff process by returning false. `receptor_off` will
+ -- discard all the changes that we made in the voxelmanip:
+ for _, l in ipairs(mesecon.rules_link_rule_all_inverted(f.pos, r)) do
+ if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then
+ return false
end
end
- else
- mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
- end
- elseif mesecon.is_effector(node.name) then
- mesecon.changesignal(f.pos, node, f.link, mesecon.state.off, depth)
- if mesecon.is_effector_on(node.name) and not mesecon.is_powered(f.pos) then
- mesecon.deactivate(f.pos, node, f.link, depth)
- end
- end
- depth = depth + 1
- end
-end
-
-mesecon.queue:add_function("turnoff", function(pos, rulename, recdepth)
- mesecon.turnoff(pos, rulename, recdepth)
-end)
-
-
-function mesecon.connected_to_receptor(pos, link)
- local node = mesecon.get_node_force(pos)
- if not node then return false end
-
- -- Check if conductors around are connected
- local rules = mesecon.get_any_inputrules(node)
- if not rules then return false end
-
- for _, rule in ipairs(mesecon.rule2meta(link, rules)) do
- local links = mesecon.rules_link_rule_all_inverted(pos, rule)
- for _, l in ipairs(links) do
- local np = vector.add(pos, l)
- if mesecon.find_receptor_on(np, mesecon.invertRule(l)) then
- return true
- end
- end
- end
-
- return false
-end
-
-function mesecon.find_receptor_on(pos, link)
- local frontiers = {{pos = pos, link = link}}
- local checked = {}
-
- -- List of positions that have been searched for onstate receptors
- local depth = 1
- while frontiers[depth] do
- local f = frontiers[depth]
- local node = mesecon.get_node_force(f.pos)
-
- if not node then return false end
- if mesecon.is_receptor_on(node.name) then return true end
- if mesecon.is_conductor_on(node, f.link) then
- local rules = mesecon.conductor_get_rules(node)
-
- -- call turnoff on neighbors: normal rules
- for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
- local np = vector.add(f.pos, r)
- local links = mesecon.rules_link_rule_all_inverted(f.pos, r)
- for _, l in ipairs(links) do
- local checkedstring = np.x..np.y..np.z..l.x..l.y..l.z
- if not checked[checkedstring] then
- table.insert(frontiers, {pos = np, link = l})
- checked[checkedstring] = true
- end
+ -- Call turnoff on neighbors
+ for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
+ table.insert(frontiers, {pos = np, link = l})
end
end
+ mesecon.swap_node_force(f.pos, mesecon.get_conductor_off(node, f.link))
+ elseif mesecon.is_effector(node.name) then
+ table.insert(signals, {
+ pos = f.pos,
+ node = node,
+ link = f.link,
+ depth = depth
+ })
end
depth = depth + 1
end
-end
-
-function mesecon.rules_link(output, input, dug_outputrules) --output/input are positions (outputrules optional, used if node has been dug), second return value: the name of the affected input rule
- local outputnode = mesecon.get_node_force(output)
- local inputnode = mesecon.get_node_force(input)
-
- local outputrules = dug_outputrules or mesecon.get_any_outputrules(outputnode)
- local inputrules = mesecon.get_any_inputrules(inputnode)
- if not outputrules or not inputrules then
- return
- end
- for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do
- -- Check if output sends to input
- if vector.equals(vector.add(output, outputrule), input) then
- for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do
- -- Check if input accepts from output
- if vector.equals(vector.add(input, inputrule), output) then
- return true, inputrule
- end
- end
+ for _, sig in ipairs(signals) do
+ mesecon.changesignal(sig.pos, sig.node, sig.link, mesecon.state.off, sig.depth)
+ if mesecon.is_effector_on(sig.node.name) and not mesecon.is_powered(sig.pos) then
+ mesecon.deactivate(sig.pos, sig.node, sig.link, sig.depth)
end
end
- return false
+ return true
end
+-- Get all linking inputrules of inputnode (effector or conductor) that is connected to
+-- outputnode (receptor or conductor) at position `output` and has an output in direction `rule`
function mesecon.rules_link_rule_all(output, rule)
local input = vector.add(output, rule)
local inputnode = mesecon.get_node_force(input)
@@ -591,8 +492,9 @@ function mesecon.rules_link_rule_all(output, rule)
return rules
end
+-- Get all linking outputnodes of outputnode (receptor or conductor) that is connected to
+-- inputnode (effector or conductor) at position `input` and has an input in direction `rule`
function mesecon.rules_link_rule_all_inverted(input, rule)
- --local irule = mesecon.invertRule(rule)
local output = vector.add(input, rule)
local outputnode = mesecon.get_node_force(output)
local outputrules = mesecon.get_any_outputrules(outputnode)
@@ -609,10 +511,6 @@ function mesecon.rules_link_rule_all_inverted(input, rule)
return rules
end
-function mesecon.rules_link_anydir(pos1, pos2)
- return mesecon.rules_link(pos1, pos2) or mesecon.rules_link(pos2, pos1)
-end
-
function mesecon.is_powered(pos, rule)
local node = mesecon.get_node_force(pos)
local rules = mesecon.get_any_inputrules(node)
diff --git a/mesecons/legacy.lua b/mesecons/legacy.lua
index 6d8ccca..ad7093a 100644
--- a/mesecons/legacy.lua
+++ b/mesecons/legacy.lua
@@ -1,30 +1,14 @@
--- Ugly hack to prevent breaking compatibility with other mods
--- Just remove the following two functions to delete the hack, to be done when other mods have updated
-function mesecon.receptor_on(self, pos, rules)
- if (self.receptor_on) then
- print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_on.")
- print("[Mesecons] If you are the programmer of this mod, please update it ")
- print("[Mesecons] to use mesecon.receptor_on instead. mesecon:* is deprecated")
- print("[Mesecons] Otherwise, please make sure you're running the latest version")
- print("[Mesecons] of that mod and inform the mod creator.")
- else
- rules = pos
- pos = self
- end
- mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
-end
+-- Un-forceload any forceloaded mapblocks from older versions of Mesecons which
+-- used forceloading instead of VoxelManipulators.
+local BLOCKSIZE = 16
-function mesecon.receptor_off(self, pos, rules)
- if (self.receptor_off) then
- print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_off.")
- print("[Mesecons] If you are the programmer of this mod, please update it ")
- print("[Mesecons] to use mesecon.receptor_off instead. mesecon:* is deprecated")
- print("[Mesecons] Otherwise, please make sure you're running the latest version")
- print("[Mesecons] of that mod and inform the mod creator.")
- else
- rules = pos
- pos = self
- end
- mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
+-- convert block hash --> node position
+local function unhash_blockpos(hash)
+ return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
end
+local old_forceloaded_blocks = mesecon.file2table("mesecon_forceloaded")
+for hash, _ in pairs(old_forceloaded_blocks) do
+ minetest.forceload_free_block(unhash_blockpos(hash))
+end
+os.remove(minetest.get_worldpath()..DIR_DELIM.."mesecon_forceloaded")
diff --git a/mesecons/util.lua b/mesecons/util.lua
index d95f216..c9ba7fc 100644
--- a/mesecons/util.lua
+++ b/mesecons/util.lua
@@ -219,7 +219,7 @@ function mesecon.table2file(filename, table)
f:close()
end
--- Forceloading: Force server to load area if node is nil
+-- Block position "hashing" (convert to integer) functions for voxelmanip cache
local BLOCKSIZE = 16
-- convert node position --> block hash
@@ -236,43 +236,132 @@ local function unhash_blockpos(hash)
return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
end
-mesecon.forceloaded_blocks = {}
+-- Maps from a hashed mapblock position (as returned by hash_blockpos) to a
+-- table.
+--
+-- Contents of the table are:
+-- “vm” → the VoxelManipulator
+-- “va” → the VoxelArea
+-- “data” → the data array
+-- “param1” → the param1 array
+-- “param2” → the param2 array
+-- “dirty” → true if data has been modified
+--
+-- Nil if no VM-based transaction is in progress.
+local vm_cache = nil
+
+-- Starts a VoxelManipulator-based transaction.
+--
+-- During a VM transaction, calls to vm_get_node and vm_swap_node operate on a
+-- cached copy of the world loaded via VoxelManipulators. That cache can later
+-- be committed to the real map by means of vm_commit or discarded by means of
+-- vm_abort.
+function mesecon.vm_begin()
+ vm_cache = {}
+end
--- get node and force-load area
-function mesecon.get_node_force(pos)
+-- Finishes a VoxelManipulator-based transaction, freeing the VMs and map data
+-- and writing back any modified areas.
+function mesecon.vm_commit()
+ for hash, tbl in pairs(vm_cache) do
+ if tbl.dirty then
+ local vm = tbl.vm
+ vm:set_data(tbl.data)
+ vm:write_to_map()
+ vm:update_map()
+ end
+ end
+ vm_cache = nil
+end
+
+-- Finishes a VoxelManipulator-based transaction, freeing the VMs and throwing
+-- away any modified areas.
+function mesecon.vm_abort()
+ vm_cache = nil
+end
+
+-- Gets the cache entry covering a position, populating it if necessary.
+local function vm_get_or_create_entry(pos)
local hash = hash_blockpos(pos)
+ local tbl = vm_cache[hash]
+ if not tbl then
+ local vm = minetest.get_voxel_manip(pos, pos)
+ local min_pos, max_pos = vm:get_emerged_area()
+ local va = VoxelArea:new{MinEdge = min_pos, MaxEdge = max_pos}
+ tbl = {vm = vm, va = va, data = vm:get_data(), param1 = vm:get_light_data(), param2 = vm:get_param2_data(), dirty = false}
+ vm_cache[hash] = tbl
+ end
+ return tbl
+end
- if mesecon.forceloaded_blocks[hash] == nil then
- -- if no more forceload spaces are available, try again next time
- if minetest.forceload_block(pos) then
- mesecon.forceloaded_blocks[hash] = 0
- end
+-- Gets the node at a given position during a VoxelManipulator-based
+-- transaction.
+function mesecon.vm_get_node(pos)
+ local tbl = vm_get_or_create_entry(pos)
+ local index = tbl.va:indexp(pos)
+ local node_value = tbl.data[index]
+ if node_value == core.CONTENT_IGNORE then
+ return nil
else
- mesecon.forceloaded_blocks[hash] = 0
+ local node_param1 = tbl.param1[index]
+ local node_param2 = tbl.param2[index]
+ return {name = minetest.get_name_from_content_id(node_value), param1 = node_param1, param2 = node_param2}
end
+end
- return minetest.get_node_or_nil(pos)
+-- Sets a node’s name during a VoxelManipulator-based transaction.
+--
+-- Existing param1, param2, and metadata are left alone.
+function mesecon.vm_swap_node(pos, name)
+ local tbl = vm_get_or_create_entry(pos)
+ local index = tbl.va:indexp(pos)
+ tbl.data[index] = minetest.get_content_id(name)
+ tbl.dirty = true
end
-minetest.register_globalstep(function (dtime)
- for hash, time in pairs(mesecon.forceloaded_blocks) do
- -- unload forceloaded blocks after 10 minutes without usage
- if (time > mesecon.setting("forceload_timeout", 600)) then
- minetest.forceload_free_block(unhash_blockpos(hash))
- mesecon.forceloaded_blocks[hash] = nil
- else
- mesecon.forceloaded_blocks[hash] = time + dtime
+-- Gets the node at a given position, regardless of whether it is loaded or
+-- not, respecting a transaction if one is in progress.
+--
+-- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
+-- the server’s main map data cache and then accessed from there.
+--
+-- Inside a VM transaction, the transaction’s VM cache is used.
+function mesecon.get_node_force(pos)
+ if vm_cache then
+ return mesecon.vm_get_node(pos)
+ else
+ local node = minetest.get_node_or_nil(pos)
+ if node == nil then
+ -- Node is not currently loaded; use a VoxelManipulator to prime
+ -- the mapblock cache and try again.
+ minetest.get_voxel_manip(pos, pos)
+ node = minetest.get_node_or_nil(pos)
end
+ return node
end
-end)
-
--- Store and read the forceloaded blocks to / from a file
--- so that those blocks are remembered when the game
--- is restarted
-mesecon.forceloaded_blocks = mesecon.file2table("mesecon_forceloaded")
-minetest.register_on_shutdown(function()
- mesecon.table2file("mesecon_forceloaded", mesecon.forceloaded_blocks)
-end)
+end
+
+-- Swaps the node at a given position, regardless of whether it is loaded or
+-- not, respecting a transaction if one is in progress.
+--
+-- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
+-- the server’s main map data cache and then accessed from there.
+--
+-- Inside a VM transaction, the transaction’s VM cache is used.
+--
+-- This function can only be used to change the node’s name, not its parameters
+-- or metadata.
+function mesecon.swap_node_force(pos, name)
+ if vm_cache then
+ return mesecon.vm_swap_node(pos, name)
+ else
+ -- This serves to both ensure the mapblock is loaded and also hand us
+ -- the old node table so we can preserve param2.
+ local node = mesecon.get_node_force(pos)
+ node.name = name
+ minetest.swap_node(pos, node)
+ end
+end
-- Autoconnect Hooks
-- Nodes like conductors may change their appearance and their connection rules