17 Commits

Author SHA1 Message Date
95fedc88bc Never postpone action_on or action_off
There is no more need for that since VoxelManip caching makes sure we
always get an answer to get_node_force.
2016-09-04 09:50:30 +02:00
433778c2ec Add Hawk777 to contributor list, update developer documentation URL 2016-09-04 09:32:41 +02:00
54efc64558 Duplicate unhash_blockpos code to legacy.lua
Duplicating that small piece of code seems like a better idea than
putting the undo-forceloading code back into util.lua. This way, it is
easier to remember to remove that unneccesary code after a couple of
months / years, when people have transitioned. This also means we can
make changes to the code in util.lua without breaking old code.
2016-08-31 08:46:11 +02:00
dfeb070a20 Remove some long unused legacy code
The change that required this compatibility layer happened in
2014, so other mods have had a lot of time to update.
2016-08-30 20:54:20 +02:00
6dacdaee1f Small cleanups in internal.lua, move forceloading reversal to legacy 2016-08-30 19:37:43 +02:00
e561be7fa3 Greatly improve performance by making use of VoxelManips in turnoff
Instead of seperately looking for onstate receptors along equipotential
sections of the circuit before turning off, do that while already
modifying the VoxelManip. In case an onstate receptor is found, discard
the VoxelManip cache, otherwise commit it after turnoff is completed.
2016-08-30 19:12:09 +02:00
89153f6909 Further simplification of turnon/turnoff 2016-08-30 18:24:08 +02:00
8e6536ca2e Simplify turnon/turnoff.
It is no longer possible for get_node_force to return nil if the target
location does, in fact, exist, because a VM will always be able to load
it (whereas a forceload might not, due to exhaustion of forceload
resources). So it is no longer necessary to handle get_node_force
returning nil by deferring processing.
2016-08-24 00:42:40 -07:00
bc9d4c2d5a Use VM transactions for conductor state changes.
VoxelManipulator-based transactions are used to hopefully speed up
scanning and replacing of networks of conductors when receptors start or
stop driving signals into them.
2016-08-24 00:42:40 -07:00
6d79272ed4 Implement VoxelManipulator-based transactions.
Rather than calling out through the Lua-to-C API for each node that
needs to be read or written, a group of map reads (and optionally
writes) can be grouped into a transaction. Access to map data within the
transaction is provided at high speed by means of VoxelManipulators.
Once the reads and writes are finished, the transaction can be committed
or aborted.
2016-08-24 00:42:38 -07:00
564cee346a Use VoxelManipulators for get_node_force.
A VoxelManipulator, when asked to read a mapblock, in addition to making
that mapblock available to the caller, also pulls it into the server’s
map cache, thus making get_node calls in the immediate future succeed.

This has the dual advantages that not every mapblock containing a
Mesecons circuit need remain loaded at all times (rather mapblocks can
be loaded on demand as signals are sent), and that the server need not
bother running ABMs and ticking entities within those mapblocks that are
loaded due to Mesecons signalling.
2016-08-24 00:41:47 -07:00
912f17f335 Split wires into their own mod, introduce autoconnect hooks
The update_autoconnect function had to be abstracted away from the
default wires, any kind of wire can now register autoconnect hooks,
which should make having multiple different wire types much easier.

mesecons_mvps, mesecons_receiver and mesecons_random made use of
update_autoconnect, their code was also adapted. This also fixes a
receiver bug: If a receiver was placed with a onstate receptor next
to it (but not the wall lever / button that caused the receiver to
appear) the receiver didn't turn on in the past.

Also move documentation for mesecon wire into mesecons_wire.
2016-08-23 11:08:53 +02:00
4816dee396 Move shared textures into base mod (fixes a few LuaC issues) 2016-08-23 11:02:12 +02:00
a3042b44fc Merge pull request #282 from Hawk777/luacontroller-comment
Remove an obsolete comment.
2016-08-16 12:14:33 +02:00
53eaf2af11 Remove an obsolete comment. 2016-08-16 00:16:24 -07:00
778ee427f4 Fix bug introduced in previous commit that broke all logic blocks
turnon / turnoff were calling activate / deactivate on nodes even though
their rules didn't link

Fixes #278, thanks to @darkfeels
2016-07-31 22:26:33 +02:00
7c7595fd7d Fix #276: Fix bugs in block forceloading in turnon / turnoff
Thanks to @Hawk777 for reporting this problem
2016-07-25 10:01:43 +02:00
21 changed files with 264 additions and 252 deletions

View File

@ -53,6 +53,7 @@ These awesome people made Mesecons possible!
| Contributor | Contribution | | Contributor | Contribution |
| --------------- | -------------------------------- | | --------------- | -------------------------------- |
| Hawk777 | Code for VoxelManip caching |
| Jat15 | Various tweaks. | | Jat15 | Various tweaks. |
| Jeija | **Main developer! Everything.** | | Jeija | **Main developer! Everything.** |
| Jordach | Noteblock sounds. | | Jordach | Noteblock sounds. |

View File

@ -1,6 +1,6 @@
{ {
"Conductors" : { "Conductors" : {
"Mesecon" : "mesecons/doc/mesecon", "Mesecon" : "mesecons_wires/doc/mesecon",
"Insulated Wire" : "mesecons_insulated/doc/insulated", "Insulated Wire" : "mesecons_insulated/doc/insulated",
"T-Junction" : "mesecons_extrawires/doc/tjunction", "T-Junction" : "mesecons_extrawires/doc/tjunction",
"Crossing" : "mesecons_extrawires/doc/crossing", "Crossing" : "mesecons_extrawires/doc/crossing",

View File

@ -87,7 +87,11 @@ minetest.register_globalstep(function (dtime)
end) end)
function mesecon.queue:execute(action) 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 end

View File

@ -1 +0,0 @@
Mesecons are the wires, use them to connect effectors with receptors.

View File

@ -3,7 +3,7 @@
-- | \/ | |___ ____ |___ | | | | \ | |____ -- | \/ | |___ ____ |___ | | | | \ | |____
-- | | | | | | | | | \ | | -- | | | | | | | | | \ | |
-- | | |___ ____| |___ |____ |____| | \| ____| -- | | |___ ____| |___ |____ |____| | \| ____|
-- by Jeija, Uberi (Temperest), sfan5, VanessaE -- by Jeija, Uberi (Temperest), sfan5, VanessaE, Hawk777 and contributors
-- --
-- --
-- --
@ -11,7 +11,7 @@
-- See the documentation on the forum for additional information, especially about crafting -- See the documentation on the forum for additional information, especially about crafting
-- --
-- --
-- For developer documentation see the Developers' section on mesecons.TK -- For basic development resources, see http://mesecons.net/developers.html
-- --
-- --
-- --
@ -70,19 +70,11 @@ dofile(minetest.get_modpath("mesecons").."/internal.lua");
-- these are the only functions you need to remember -- these are the only functions you need to remember
mesecon.queue:add_function("receptor_on", function (pos, rules) mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon.vm_begin()
rules = rules or mesecon.rules.default rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later -- Call turnon on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule)
-- if area is not loaded, keep trying
if minetest.get_node_or_nil(np) == nil then
mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
return
end
end
-- execute action
for _, rule in ipairs(mesecon.flattenrules(rules)) do for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule) local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule) local rulenames = mesecon.rules_link_rule_all(pos, rule)
@ -90,6 +82,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon.turnon(np, rulename) mesecon.turnon(np, rulename)
end end
end end
mesecon.vm_commit()
end) end)
function mesecon.receptor_on(pos, rules) function mesecon.receptor_on(pos, rules)
@ -99,23 +93,21 @@ end
mesecon.queue:add_function("receptor_off", function (pos, rules) mesecon.queue:add_function("receptor_off", function (pos, rules)
rules = rules or mesecon.rules.default rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later -- Call turnoff on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule)
if minetest.get_node_or_nil(np) == nil then
mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
return
end
end
for _, rule in ipairs(mesecon.flattenrules(rules)) do for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule) local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule) local rulenames = mesecon.rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do for _, rulename in ipairs(rulenames) do
if not mesecon.connected_to_receptor(np, mesecon.invertRule(rule)) then mesecon.vm_begin()
mesecon.turnoff(np, rulename) 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 else
mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2) mesecon.vm_abort()
end end
end end
end end
@ -132,8 +124,5 @@ print("[OK] Mesecons")
-- To be removed in future releases -- To be removed in future releases
dofile(minetest.get_modpath("mesecons").."/legacy.lua"); dofile(minetest.get_modpath("mesecons").."/legacy.lua");
--The actual wires
dofile(minetest.get_modpath("mesecons").."/wires.lua");
--Services like turnoff receptor on dignode and so on --Services like turnoff receptor on dignode and so on
dofile(minetest.get_modpath("mesecons").."/services.lua"); dofile(minetest.get_modpath("mesecons").."/services.lua");

View File

@ -37,11 +37,6 @@
-- HIGH-LEVEL Internals -- HIGH-LEVEL Internals
-- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way -- 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.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 -- mesecon.is_powered(pos) --> Returns true if pos is powered by a receptor or a conductor
-- RULES ROTATION helpers -- RULES ROTATION helpers
@ -76,7 +71,7 @@ function mesecon.get_conductor(nodename)
end end
end end
function mesecon.get_any_outputrules (node) function mesecon.get_any_outputrules(node)
if not node then return nil end if not node then return nil end
if mesecon.is_conductor(node.name) then if mesecon.is_conductor(node.name) then
@ -86,7 +81,7 @@ function mesecon.get_any_outputrules (node)
end end
end end
function mesecon.get_any_inputrules (node) function mesecon.get_any_inputrules(node)
if not node then return nil end if not node then return nil end
if mesecon.is_conductor(node.name) then if mesecon.is_conductor(node.name) then
@ -96,7 +91,7 @@ function mesecon.get_any_inputrules (node)
end end
end end
function mesecon.get_any_rules (node) function mesecon.get_any_rules(node)
return mesecon.mergetable(mesecon.get_any_inputrules(node) or {}, return mesecon.mergetable(mesecon.get_any_inputrules(node) or {},
mesecon.get_any_outputrules(node) or {}) mesecon.get_any_outputrules(node) or {})
end end
@ -371,38 +366,32 @@ function mesecon.is_power_off(pos, rulename)
return false return false
end 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) function mesecon.turnon(pos, link)
local frontiers = {{pos = pos, link = link}} local frontiers = {{pos = pos, link = link}}
local depth = 1 local depth = 1
while frontiers[depth] do while frontiers[1] do
local f = frontiers[depth] local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos) local node = mesecon.get_node_force(f.pos)
-- area not loaded, postpone action
if not node then if not node then
mesecon.queue:add_action(f.pos, "turnon", {link}, nil, true) -- Area does not exist; do nothing
elseif mesecon.is_conductor_off(node, f.link) then elseif mesecon.is_conductor_off(node, f.link) then
local rules = mesecon.conductor_get_rules(node) local rules = mesecon.conductor_get_rules(node)
minetest.swap_node(f.pos, {name = mesecon.get_conductor_on(node, f.link), -- Call turnon on neighbors
param2 = node.param2})
-- call turnon on neighbors: normal rules
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r) local np = vector.add(f.pos, r)
for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
-- area not loaded, postpone action table.insert(frontiers, {pos = np, link = l})
if not mesecon.get_node_force(np) then
mesecon.queue:add_action(np, "turnon", {rulename},
nil, true)
else
local links = mesecon.rules_link_rule_all(f.pos, r)
for _, l in ipairs(links) do
table.insert(frontiers, {pos = np, link = l})
end
end end
end end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_on(node, f.link))
elseif mesecon.is_effector(node.name) then elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth) mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth)
if mesecon.is_effector_off(node.name) then if mesecon.is_effector_off(node.name) then
@ -413,141 +402,81 @@ function mesecon.turnon(pos, link)
end end
end end
mesecon.queue:add_function("turnon", function (pos, rulename, recdepth) -- Turn on an equipotential section starting at `pos`, which outputs in the direction of `link`.
mesecon.turnon(pos, rulename, recdepth) -- Breadth-first search. Map is abstracted away in a voxelmanip.
end) -- 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) function mesecon.turnoff(pos, link)
local frontiers = {{pos = pos, link = link}} local frontiers = {{pos = pos, link = link}}
local signals = {}
local depth = 1 local depth = 1
while frontiers[depth] do while frontiers[1] do
local f = frontiers[depth] local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos) local node = mesecon.get_node_force(f.pos)
-- area not loaded, postpone action
if not node then if not node then
mesecon.queue:add_action(f.pos, "turnoff", {link}, nil, true) -- Area does not exist; do nothing
elseif mesecon.is_conductor_on(node, f.link) then elseif mesecon.is_conductor_on(node, f.link) then
local rules = mesecon.conductor_get_rules(node) local rules = mesecon.conductor_get_rules(node)
minetest.swap_node(f.pos, {name = mesecon.get_conductor_off(node, f.link),
param2 = node.param2})
-- call turnoff on neighbors: normal rules
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r) local np = vector.add(f.pos, r)
-- area not loaded, postpone action -- Check if an onstate receptor is connected. If that is the case,
if not mesecon.get_node_force(np) then -- abort this turnoff process by returning false. `receptor_off` will
mesecon.queue:add_action(np, "turnoff", {rulename}, -- discard all the changes that we made in the voxelmanip:
nil, true) for _, l in ipairs(mesecon.rules_link_rule_all_inverted(f.pos, r)) do
else if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then
local links = mesecon.rules_link_rule_all(f.pos, r) return false
for _, l in ipairs(links) do
table.insert(frontiers, {pos = np, link = l})
end end
end 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 end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_off(node, f.link))
elseif mesecon.is_effector(node.name) then elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.off, depth) table.insert(signals, {
if mesecon.is_effector_on(node.name) and not mesecon.is_powered(f.pos) then pos = f.pos,
mesecon.deactivate(f.pos, node, f.link, depth) node = node,
end link = f.link,
depth = depth
})
end end
depth = depth + 1 depth = depth + 1
end end
end
mesecon.queue:add_function("turnoff", function (pos, rulename, recdepth) for _, sig in ipairs(signals) do
mesecon.turnoff(pos, rulename, recdepth) mesecon.changesignal(sig.pos, sig.node, sig.link, mesecon.state.off, sig.depth)
end) 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)
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
end end
return false return true
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
end
end
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
end
end
return false
end 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) function mesecon.rules_link_rule_all(output, rule)
local input = vector.add(output, rule) local input = vector.add(output, rule)
local inputnode = mesecon.get_node_force(input) local inputnode = mesecon.get_node_force(input)
local inputrules = mesecon.get_any_inputrules (inputnode) local inputrules = mesecon.get_any_inputrules(inputnode)
if not inputrules then if not inputrules then
return {} return {}
end end
@ -563,11 +492,12 @@ function mesecon.rules_link_rule_all(output, rule)
return rules return rules
end 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) function mesecon.rules_link_rule_all_inverted(input, rule)
--local irule = mesecon.invertRule(rule)
local output = vector.add(input, rule) local output = vector.add(input, rule)
local outputnode = mesecon.get_node_force(output) local outputnode = mesecon.get_node_force(output)
local outputrules = mesecon.get_any_outputrules (outputnode) local outputrules = mesecon.get_any_outputrules(outputnode)
if not outputrules then if not outputrules then
return {} return {}
end end
@ -581,10 +511,6 @@ function mesecon.rules_link_rule_all_inverted(input, rule)
return rules return rules
end 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) function mesecon.is_powered(pos, rule)
local node = mesecon.get_node_force(pos) local node = mesecon.get_node_force(pos)
local rules = mesecon.get_any_inputrules(node) local rules = mesecon.get_any_inputrules(node)

View File

@ -1,30 +1,14 @@
-- Ugly hack to prevent breaking compatibility with other mods -- Un-forceload any forceloaded mapblocks from older versions of Mesecons which
-- Just remove the following two functions to delete the hack, to be done when other mods have updated -- used forceloading instead of VoxelManipulators.
function mesecon.receptor_on(self, pos, rules) local BLOCKSIZE = 16
if (self.receptor_on) then
print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_on.") -- convert block hash --> node position
print("[Mesecons] If you are the programmer of this mod, please update it ") local function unhash_blockpos(hash)
print("[Mesecons] to use mesecon.receptor_on instead. mesecon:* is deprecated") return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
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 end
function mesecon.receptor_off(self, pos, rules) local old_forceloaded_blocks = mesecon.file2table("mesecon_forceloaded")
if (self.receptor_off) then for hash, _ in pairs(old_forceloaded_blocks) do
print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_off.") minetest.forceload_free_block(unhash_blockpos(hash))
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)
end end
os.remove(minetest.get_worldpath()..DIR_DELIM.."mesecon_forceloaded")

View File

@ -1,7 +1,7 @@
-- Dig and place services -- Dig and place services
mesecon.on_placenode = function (pos, node) mesecon.on_placenode = function(pos, node)
mesecon.update_autoconnect(pos, node) mesecon.execute_autoconnect_hooks_now(pos, node)
-- Receptors: Send on signal when active -- Receptors: Send on signal when active
if mesecon.is_receptor_on(node.name) then if mesecon.is_receptor_on(node.name) then
@ -52,16 +52,15 @@ mesecon.on_placenode = function (pos, node)
end end
end end
mesecon.on_dignode = function (pos, node) mesecon.on_dignode = function(pos, node)
if mesecon.is_conductor_on(node) then if mesecon.is_conductor_on(node) then
mesecon.receptor_off(pos, mesecon.conductor_get_rules(node)) mesecon.receptor_off(pos, mesecon.conductor_get_rules(node))
elseif mesecon.is_receptor_on(node.name) then elseif mesecon.is_receptor_on(node.name) then
mesecon.receptor_off(pos, mesecon.receptor_get_rules(node)) mesecon.receptor_off(pos, mesecon.receptor_get_rules(node))
end end
mesecon.queue:add_action(pos, "update_autoconnect", {node})
end
mesecon.queue:add_function("update_autoconnect", mesecon.update_autoconnect) mesecon.execute_autoconnect_hooks_queue(pos, node)
end
minetest.register_on_placenode(mesecon.on_placenode) minetest.register_on_placenode(mesecon.on_placenode)
minetest.register_on_dignode(mesecon.on_dignode) minetest.register_on_dignode(mesecon.on_dignode)

View File

Before

Width:  |  Height:  |  Size: 323 B

After

Width:  |  Height:  |  Size: 323 B

View File

Before

Width:  |  Height:  |  Size: 550 B

After

Width:  |  Height:  |  Size: 550 B

View File

Before

Width:  |  Height:  |  Size: 613 B

After

Width:  |  Height:  |  Size: 613 B

View File

@ -219,7 +219,7 @@ function mesecon.table2file(filename, table)
f:close() f:close()
end end
-- Forceloading: Force server to load area if node is nil -- Block position "hashing" (convert to integer) functions for voxelmanip cache
local BLOCKSIZE = 16 local BLOCKSIZE = 16
-- convert node position --> block hash -- convert node position --> block hash
@ -236,40 +236,160 @@ local function unhash_blockpos(hash)
return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE) return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
end 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
-- get node and force-load area -- Starts a VoxelManipulator-based transaction.
function mesecon.get_node_force(pos) --
local hash = hash_blockpos(pos) -- 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
if mesecon.forceloaded_blocks[hash] == nil then -- be committed to the real map by means of vm_commit or discarded by means of
-- if no more forceload spaces are available, try again next time -- vm_abort.
if minetest.forceload_block(pos) then function mesecon.vm_begin()
mesecon.forceloaded_blocks[hash] = 0 vm_cache = {}
end
else
mesecon.forceloaded_blocks[hash] = 0
end
return minetest.get_node_or_nil(pos)
end end
minetest.register_globalstep(function (dtime) -- Finishes a VoxelManipulator-based transaction, freeing the VMs and map data
for hash, time in pairs(mesecon.forceloaded_blocks) do -- and writing back any modified areas.
-- unload forceloaded blocks after 10 minutes without usage function mesecon.vm_commit()
if (time > mesecon.setting("forceload_timeout", 600)) then for hash, tbl in pairs(vm_cache) do
minetest.forceload_free_block(unhash_blockpos(hash)) if tbl.dirty then
mesecon.forceloaded_blocks[hash] = nil local vm = tbl.vm
else vm:set_data(tbl.data)
mesecon.forceloaded_blocks[hash] = time + dtime vm:write_to_map()
vm:update_map()
end end
end end
end) vm_cache = nil
end
-- Store and read the forceloaded blocks to / from a file -- Finishes a VoxelManipulator-based transaction, freeing the VMs and throwing
-- so that those blocks are remembered when the game -- away any modified areas.
-- is restarted function mesecon.vm_abort()
mesecon.forceloaded_blocks = mesecon.file2table("mesecon_forceloaded") vm_cache = nil
minetest.register_on_shutdown(function() end
mesecon.table2file("mesecon_forceloaded", mesecon.forceloaded_blocks)
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
-- 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
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
-- Sets a nodes 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
-- 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 servers main map data cache and then accessed from there.
--
-- Inside a VM transaction, the transactions 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
-- 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 servers main map data cache and then accessed from there.
--
-- Inside a VM transaction, the transactions VM cache is used.
--
-- This function can only be used to change the nodes 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
-- right after being placed or after being dug, e.g. the default wires use this
-- to automatically connect to linking nodes after placement.
-- After placement, the update function will be executed immediately so that the
-- possibly changed rules can be taken into account when recalculating the circuit.
-- After digging, the update function will be queued and executed after
-- recalculating the circuit. The update function must take care of updating the
-- node at the given position itself, but also all of the other nodes the given
-- position may have (had) a linking connection to.
mesecon.autoconnect_hooks = {}
-- name: A unique name for the hook, e.g. "foowire". Used to name the actionqueue function.
-- fct: The update function with parameters function(pos, node)
function mesecon.register_autoconnect_hook(name, fct)
mesecon.autoconnect_hooks[name] = fct
mesecon.queue:add_function("autoconnect_hook_"..name, fct)
end
function mesecon.execute_autoconnect_hooks_now(pos, node)
for _, fct in pairs(mesecon.autoconnect_hooks) do
fct(pos, node)
end
end
function mesecon.execute_autoconnect_hooks_queue(pos, node)
for name in pairs(mesecon.autoconnect_hooks) do
mesecon.queue:add_action(pos, "autoconnect_hook_"..name, {node})
end
end

View File

@ -20,8 +20,6 @@
-- The Sandbox -- The Sandbox
-- The whole code of the controller runs in a sandbox, -- The whole code of the controller runs in a sandbox,
-- a very restricted environment. -- a very restricted environment.
-- However, as this does not prevent you from using e.g. loops,
-- we need to check for these prohibited commands first.
-- Actually the only way to damage the server is to -- Actually the only way to damage the server is to
-- use too much memory from the sandbox. -- use too much memory from the sandbox.
-- You can add more functions to the environment -- You can add more functions to the environment

View File

@ -200,7 +200,6 @@ end
mesecon.register_on_mvps_move(function(moved_nodes) mesecon.register_on_mvps_move(function(moved_nodes)
for _, n in ipairs(moved_nodes) do for _, n in ipairs(moved_nodes) do
mesecon.on_placenode(n.pos, n.node) mesecon.on_placenode(n.pos, n.node)
mesecon.update_autoconnect(n.pos)
end end
end) end)

View File

@ -9,7 +9,7 @@ minetest.register_node("mesecons_random:removestone", {
mesecons = {effector = { mesecons = {effector = {
action_on = function (pos, node) action_on = function (pos, node)
minetest.remove_node(pos) minetest.remove_node(pos)
mesecon.update_autoconnect(pos) mesecon.on_dignode(pos, node)
end end
}} }}
}) })

View File

@ -104,13 +104,8 @@ function mesecon.receiver_place(rcpt_pos)
if string.find(nn.name, "mesecons:wire_") ~= nil then if string.find(nn.name, "mesecons:wire_") ~= nil then
minetest.dig_node(pos) minetest.dig_node(pos)
if mesecon.is_power_on(rcpt_pos) then minetest.set_node(pos, {name = "mesecons_receiver:receiver_off", param2 = node.param2})
minetest.set_node(pos, {name = "mesecons_receiver:receiver_on", param2 = node.param2}) mesecon.on_placenode(pos, nn)
mesecon.receptor_on(pos, receiver_get_rules(node))
else
minetest.set_node(pos, {name = "mesecons_receiver:receiver_off", param2 = node.param2})
end
mesecon.update_autoconnect(pos)
end end
end end
@ -121,7 +116,6 @@ function mesecon.receiver_remove(rcpt_pos, dugnode)
minetest.dig_node(pos) minetest.dig_node(pos)
local node = {name = "mesecons:wire_00000000_off"} local node = {name = "mesecons:wire_00000000_off"}
minetest.set_node(pos, node) minetest.set_node(pos, node)
mesecon.update_autoconnect(pos)
mesecon.on_placenode(pos, node) mesecon.on_placenode(pos, node)
end end
end end

View File

@ -0,0 +1 @@
mesecons

View File

@ -0,0 +1 @@
Mesecons are the wires, use them to connect effectors with receptors.

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

@ -91,10 +91,7 @@ local update_on_place_dig = function (pos, node)
end end
end end
function mesecon.update_autoconnect(pos, node) mesecon.register_autoconnect_hook("wire", update_on_place_dig)
if (not node) then node = minetest.get_node(pos) end
update_on_place_dig(pos, node)
end
-- ############################ -- ############################
-- ## Wire node registration ## -- ## Wire node registration ##
@ -204,7 +201,7 @@ register_wires = function()
groups_off["not_in_creative_inventory"] = 1 groups_off["not_in_creative_inventory"] = 1
end end
mesecon.register_node("mesecons:wire_"..nodeid, { mesecon.register_node(":mesecons:wire_"..nodeid, {
description = "Mesecon", description = "Mesecon",
drawtype = "nodebox", drawtype = "nodebox",
inventory_image = "mesecons_wire_inv.png", inventory_image = "mesecons_wire_inv.png",