abm -> node timer

This commit is contained in:
h-v-smacker 2018-11-29 23:19:09 +03:00
parent df4e880d8b
commit ee4ddbfb9e
4 changed files with 180 additions and 128 deletions

View File

@ -74,11 +74,24 @@ minetest.register_node("mesecons_detector:object_detector_off", {
state = mesecon.state.off,
rules = mesecon.rules.pplate
}},
on_construct = object_detector_make_formspec,
on_construct = function(pos)
object_detector_make_formspec(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
if not object_detector_scan(pos) then return true end
local node = minetest.get_node(pos)
node.name = "mesecons_detector:object_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.pplate)
return true
end,
on_receive_fields = object_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = object_detector_digiline,
on_blast = mesecon.on_blastnode,
})
minetest.register_node("mesecons_detector:object_detector_on", {
@ -92,7 +105,19 @@ minetest.register_node("mesecons_detector:object_detector_on", {
state = mesecon.state.on,
rules = mesecon.rules.pplate
}},
on_construct = object_detector_make_formspec,
on_construct = function(pos)
object_detector_make_formspec(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
if object_detector_scan(pos) then return true end
local node = minetest.get_node(pos)
node.name = "mesecons_detector:object_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.pplate)
return true
end,
on_receive_fields = object_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = object_detector_digiline,
@ -117,32 +142,6 @@ minetest.register_craft({
}
})
minetest.register_abm({
nodenames = {"mesecons_detector:object_detector_off"},
interval = 1,
chance = 1,
action = function(pos, node)
if not object_detector_scan(pos) then return end
node.name = "mesecons_detector:object_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.pplate)
end,
})
minetest.register_abm({
nodenames = {"mesecons_detector:object_detector_on"},
interval = 1,
chance = 1,
action = function(pos, node)
if object_detector_scan(pos) then return end
node.name = "mesecons_detector:object_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.pplate)
end,
})
-- Node detector
-- Detects the node in front of it
@ -245,7 +244,19 @@ minetest.register_node("mesecons_detector:node_detector_off", {
mesecons = {receptor = {
state = mesecon.state.off
}},
on_construct = node_detector_make_formspec,
on_construct = function(pos)
node_detector_make_formspec(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
if not node_detector_scan(pos) then return true end
local node = minetest.get_node(pos)
node.name = "mesecons_detector:node_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos)
return true
end,
on_receive_fields = node_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline,
@ -263,7 +274,19 @@ minetest.register_node("mesecons_detector:node_detector_on", {
mesecons = {receptor = {
state = mesecon.state.on
}},
on_construct = node_detector_make_formspec,
on_construct = function(pos)
node_detector_make_formspec(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
if node_detector_scan(pos) then return true end
local node = minetest.get_node(pos)
node.name = "mesecons_detector:node_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos)
return true
end,
on_receive_fields = node_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline,
@ -288,28 +311,17 @@ minetest.register_craft({
}
})
minetest.register_abm({
nodenames = {"mesecons_detector:node_detector_off"},
interval = 1,
chance = 1,
action = function(pos, node)
if not node_detector_scan(pos) then return end
node.name = "mesecons_detector:node_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos)
end,
})
minetest.register_abm({
nodenames = {"mesecons_detector:node_detector_on"},
interval = 1,
chance = 1,
action = function(pos, node)
if node_detector_scan(pos) then return end
node.name = "mesecons_detector:node_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos)
-- LBM to start timers on existing, ABM-driven nodes
minetest.register_lbm({
name = "mesecons_detector:timer_init",
nodenames = {"mesecons_detector:object_detector_off",
"mesecons_detector:object_detector_on",
"mesecons_detector:node_detector_off",
"mesecons_detector:node_detector_on"},
run_at_every_load = false,
action = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
})

View File

@ -3,6 +3,13 @@
-- Active if flowing >water< above it
-- (does not work with other liquids)
local function is_flowing_water(pos)
local name = minetest.get_node(pos).name
local is_water = minetest.get_item_group(name, "water") > 0
local is_flowing = minetest.registered_items[name].liquidtype == "flowing"
return (is_water and is_flowing)
end
minetest.register_node("mesecons_hydroturbine:hydro_turbine_off", {
drawtype = "mesh",
mesh = "jeija_hydro_turbine_off.obj",
@ -27,6 +34,18 @@ minetest.register_node("mesecons_hydroturbine:hydro_turbine_off", {
state = mesecon.state.off
}},
on_blast = mesecon.on_blastnode,
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
local waterpos={x=pos.x, y=pos.y+1, z=pos.z}
if is_flowing_water(waterpos) then
minetest.set_node(pos, {name="mesecons_hydroturbine:hydro_turbine_on"})
mesecon.receptor_on(pos)
end
return true
end,
})
minetest.register_node("mesecons_hydroturbine:hydro_turbine_on", {
@ -56,39 +75,29 @@ minetest.register_node("mesecons_hydroturbine:hydro_turbine_on", {
state = mesecon.state.on
}},
on_blast = mesecon.on_blastnode,
})
local function is_flowing_water(pos)
local name = minetest.get_node(pos).name
local is_water = minetest.get_item_group(name, "water") > 0
local is_flowing = minetest.registered_items[name].liquidtype == "flowing"
return (is_water and is_flowing)
end
minetest.register_abm({
nodenames = {"mesecons_hydroturbine:hydro_turbine_off"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local waterpos={x=pos.x, y=pos.y+1, z=pos.z}
if is_flowing_water(waterpos) then
minetest.set_node(pos, {name="mesecons_hydroturbine:hydro_turbine_on"})
mesecon.receptor_on(pos)
end
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
})
minetest.register_abm({
nodenames = {"mesecons_hydroturbine:hydro_turbine_on"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
on_timer = function(pos)
local waterpos={x=pos.x, y=pos.y+1, z=pos.z}
if not is_flowing_water(waterpos) then
minetest.set_node(pos, {name="mesecons_hydroturbine:hydro_turbine_off"})
mesecon.receptor_off(pos)
end
return true
end,
})
-- LBM to start timers on existing, ABM-driven nodes
minetest.register_lbm({
name = "mesecons_hydroturbine:timer_init",
nodenames = {"mesecons_hydroturbine:hydro_turbine_off",
"mesecons_hydroturbine:hydro_turbine_on"},
run_at_every_load = false,
action = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
})

View File

@ -28,6 +28,20 @@ minetest.register_node("mesecons_solarpanel:solar_panel_on", {
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
local light = minetest.get_node_light(pos, nil)
local node = minetest.get_node(pos)
if light < 12 then
node.name = "mesecons_solarpanel:solar_panel_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.wallmounted_get(node))
end
return true
end,
})
-- Solar Panel
@ -60,6 +74,20 @@ minetest.register_node("mesecons_solarpanel:solar_panel_off", {
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = function(pos)
local light = minetest.get_node_light(pos, nil)
local node = minetest.get_node(pos)
if light >= 12 then
node.name = "mesecons_solarpanel:solar_panel_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.wallmounted_get(node))
end
return true
end,
})
minetest.register_craft({
@ -70,32 +98,15 @@ minetest.register_craft({
}
})
minetest.register_abm(
{nodenames = {"mesecons_solarpanel:solar_panel_off"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local light = minetest.get_node_light(pos, nil)
if light >= 12 then
node.name = "mesecons_solarpanel:solar_panel_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.wallmounted_get(node))
end
-- LBM to start timers on existing, ABM-driven nodes
minetest.register_lbm({
name = "mesecons_solarpanel:timer_init",
nodenames = {"mesecons_solarpanel:solar_panel_off",
"mesecons_solarpanel:solar_panel_on"},
run_at_every_load = false,
action = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
})
minetest.register_abm(
{nodenames = {"mesecons_solarpanel:solar_panel_on"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local light = minetest.get_node_light(pos, nil)
if light < 12 then
node.name = "mesecons_solarpanel:solar_panel_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.wallmounted_get(node))
end
end,
})

View File

@ -49,6 +49,28 @@ local torch_selectionbox =
wall_side = {-0.5, -0.1, -0.1, -0.5+0.6, 0.1, 0.1},
}
local torch_update = function(pos)
local node = minetest.get_node(pos)
local is_powered = false
for _, rule in ipairs(torch_get_input_rules(node)) do
local src = vector.add(pos, rule)
if mesecon.is_power_on(src) then
is_powered = true
end
end
if is_powered then
if node.name == "mesecons_torch:mesecon_torch_on" then
minetest.swap_node(pos, {name = "mesecons_torch:mesecon_torch_off", param2 = node.param2})
mesecon.receptor_off(pos, torch_get_output_rules(node))
end
elseif node.name == "mesecons_torch:mesecon_torch_off" then
minetest.swap_node(pos, {name = "mesecons_torch:mesecon_torch_on", param2 = node.param2})
mesecon.receptor_on(pos, torch_get_output_rules(node))
end
return true
end
minetest.register_node("mesecons_torch:mesecon_torch_off", {
drawtype = "torchlike",
tiles = {"jeija_torches_off.png", "jeija_torches_off_ceiling.png", "jeija_torches_off_side.png"},
@ -66,6 +88,11 @@ minetest.register_node("mesecons_torch:mesecon_torch_off", {
rules = torch_get_output_rules
}},
on_blast = mesecon.on_blastnode,
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = torch_update,
})
minetest.register_node("mesecons_torch:mesecon_torch_on", {
@ -88,33 +115,26 @@ minetest.register_node("mesecons_torch:mesecon_torch_on", {
rules = torch_get_output_rules
}},
on_blast = mesecon.on_blastnode,
on_construct = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
on_timer = torch_update,
})
minetest.register_abm({
nodenames = {"mesecons_torch:mesecon_torch_off","mesecons_torch:mesecon_torch_on"},
interval = 1,
chance = 1,
action = function(pos, node)
local is_powered = false
for _, rule in ipairs(torch_get_input_rules(node)) do
local src = vector.add(pos, rule)
if mesecon.is_power_on(src) then
is_powered = true
end
end
if is_powered then
if node.name == "mesecons_torch:mesecon_torch_on" then
minetest.swap_node(pos, {name = "mesecons_torch:mesecon_torch_off", param2 = node.param2})
mesecon.receptor_off(pos, torch_get_output_rules(node))
end
elseif node.name == "mesecons_torch:mesecon_torch_off" then
minetest.swap_node(pos, {name = "mesecons_torch:mesecon_torch_on", param2 = node.param2})
mesecon.receptor_on(pos, torch_get_output_rules(node))
end
end
-- LBM to start timers on existing, ABM-driven nodes
minetest.register_lbm({
name = "mesecons_torch:timer_init",
nodenames = {"mesecons_torch:mesecon_torch_off",
"mesecons_torch:mesecon_torch_on"},
run_at_every_load = false,
action = function(pos)
local timer = minetest.get_node_timer(pos)
timer:start(1)
end,
})
-- Param2 Table (Block Attached To)
-- 5 = z-1
-- 3 = x-1