[mesecons_delayer] fix code style issues

This commit is contained in:
AFCMS 2021-05-23 11:36:30 +02:00
parent e6f72e0c60
commit 4fd0ea8a88
1 changed files with 218 additions and 226 deletions

View File

@ -4,7 +4,7 @@ local DELAYS = { 0.1, 0.2, 0.3, 0.4 }
local DEFAULT_DELAY = DELAYS[1] local DEFAULT_DELAY = DELAYS[1]
-- Function that get the input/output rules of the delayer -- Function that get the input/output rules of the delayer
local delayer_get_output_rules = function(node) local function delayer_get_output_rules(node)
local rules = {{x = -1, y = 0, z = 0, spread=true}} local rules = {{x = -1, y = 0, z = 0, spread=true}}
for i = 0, node.param2 do for i = 0, node.param2 do
rules = mesecon.rotate_rules_left(rules) rules = mesecon.rotate_rules_left(rules)
@ -12,7 +12,7 @@ local delayer_get_output_rules = function(node)
return rules return rules
end end
local delayer_get_input_rules = function(node) local function delayer_get_input_rules(node)
local rules = {{x = 1, y = 0, z = 0}} local rules = {{x = 1, y = 0, z = 0}}
for i = 0, node.param2 do for i = 0, node.param2 do
rules = mesecon.rotate_rules_left(rules) rules = mesecon.rotate_rules_left(rules)
@ -22,7 +22,7 @@ end
-- Return the sides of a delayer. -- Return the sides of a delayer.
-- Those are used to toggle the lock state. -- Those are used to toggle the lock state.
local delayer_get_sides = function(node) local function delayer_get_sides(node)
local rules = { local rules = {
{x = 0, y = 0, z = -1}, {x = 0, y = 0, z = -1},
{x = 0, y = 0, z = 1}, {x = 0, y = 0, z = 1},
@ -35,7 +35,7 @@ end
-- Make the repeater at pos try to lock any repeater it faces. -- Make the repeater at pos try to lock any repeater it faces.
-- Returns true if a repeater was locked. -- Returns true if a repeater was locked.
local check_lock_repeater = function(pos, node) local function check_lock_repeater(pos, node)
-- Check the repeater at pos and look if it faces -- Check the repeater at pos and look if it faces
-- a repeater placed sideways. -- a repeater placed sideways.
-- If yes, lock the second repeater. -- If yes, lock the second repeater.
@ -67,7 +67,7 @@ end
-- Make the repeater at pos try to unlock any repeater it faces. -- Make the repeater at pos try to unlock any repeater it faces.
-- Returns true if a repeater was unlocked. -- Returns true if a repeater was unlocked.
local check_unlock_repeater = function(pos, node) local function check_unlock_repeater(pos, node)
-- Check the repeater at pos and look if it faces -- Check the repeater at pos and look if it faces
-- a repeater placed sideways. -- a repeater placed sideways.
-- If yes, also check if the second repeater doesn't receive -- If yes, also check if the second repeater doesn't receive
@ -119,21 +119,19 @@ local check_unlock_repeater = function(pos, node)
end end
-- Functions that are called after the delay time -- Functions that are called after the delay time
local delayer_activate = function(pos, node) local function delayer_activate(pos, node)
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
local time = def.delayer_time local time = def.delayer_time
minetest.set_node(pos, {name=def.delayer_onstate, param2=node.param2}) minetest.set_node(pos, {name=def.delayer_onstate, param2=node.param2})
mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(node)}, time, nil) mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(node)}, time, nil)
check_lock_repeater(pos, node) check_lock_repeater(pos, node)
end end
local delayer_deactivate = function(pos, node) local function delayer_deactivate(pos, node)
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
local time = def.delayer_time local time = def.delayer_time
minetest.set_node(pos, {name=def.delayer_offstate, param2=node.param2}) minetest.set_node(pos, {name=def.delayer_offstate, param2=node.param2})
mesecon.queue:add_action(pos, "receptor_off", {delayer_get_output_rules(node)}, time, nil) mesecon.queue:add_action(pos, "receptor_off", {delayer_get_output_rules(node)}, time, nil)
check_unlock_repeater(pos, node) check_unlock_repeater(pos, node)
end end
@ -145,236 +143,230 @@ end
-- Register the 2 (states) x 4 (delay times) delayers -- Register the 2 (states) x 4 (delay times) delayers
for i = 1, 4 do for i = 1, 4 do
local groups local groups
if i == 1 then if i == 1 then
groups = {dig_immediate=3,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1,attached_node=1,redstone_repeater=i} groups = {dig_immediate=3,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1,attached_node=1,redstone_repeater=i}
else else
groups = {dig_immediate=3,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1,attached_node=1,redstone_repeater=i,not_in_creative_inventory=1} groups = {dig_immediate=3,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1,attached_node=1,redstone_repeater=i,not_in_creative_inventory=1}
end end
local delaytime = DELAYS[i] local delaytime = DELAYS[i]
local boxes local boxes
if i == 1 then if i == 1 then
boxes = { boxes = {
{ -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab
{ -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch { -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch
{ -1/16, -6/16, 0/16, 1/16, -1/16, 2/16}, -- moved torch { -1/16, -6/16, 0/16, 1/16, -1/16, 2/16}, -- moved torch
} }
elseif i == 2 then elseif i == 2 then
boxes = { boxes = {
{ -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab
{ -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch { -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch
{ -1/16, -6/16, -2/16, 1/16, -1/16, 0/16}, -- moved torch { -1/16, -6/16, -2/16, 1/16, -1/16, 0/16}, -- moved torch
} }
elseif i == 3 then elseif i == 3 then
boxes = { boxes = {
{ -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab
{ -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch { -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch
{ -1/16, -6/16, -4/16, 1/16, -1/16, -2/16}, -- moved torch { -1/16, -6/16, -4/16, 1/16, -1/16, -2/16}, -- moved torch
} }
elseif i == 4 then elseif i == 4 then
boxes = { boxes = {
{ -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, -- the main slab
{ -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch { -1/16, -6/16, 6/16, 1/16, -1/16, 4/16}, -- still torch
{ -1/16, -6/16, -6/16, 1/16, -1/16, -4/16}, -- moved torch { -1/16, -6/16, -6/16, 1/16, -1/16, -4/16}, -- moved torch
} }
end end
local help, tt, longdesc, usagehelp, icon, on_construct local help, tt, longdesc, usagehelp, icon, on_construct
if i == 1 then if i == 1 then
help = true help = true
tt = S("Transmits redstone power only in one direction").."\n".. tt = S("Transmits redstone power only in one direction").."\n"..
S("Delays signal").."\n".. S("Delays signal").."\n"..
S("Output locks when getting active redstone repeater signal from the side") S("Output locks when getting active redstone repeater signal from the side")
longdesc = S("Redstone repeaters are versatile redstone components with multiple purposes: 1. They only allow signals to travel in one direction. 2. They delay the signal. 3. Optionally, they can lock their output in one state.") longdesc = S("Redstone repeaters are versatile redstone components with multiple purposes: 1. They only allow signals to travel in one direction. 2. They delay the signal. 3. Optionally, they can lock their output in one state.")
usagehelp = S("To power a redstone repeater, send a signal in “arrow” direction (the input). The signal goes out on the opposite side (the output) with a delay. To change the delay, use the redstone repeater. The delay is between 0.1 and 0.4 seconds long and can be changed in steps of 0.1 seconds. It is indicated by the position of the moving redstone torch.").."\n".. usagehelp = S("To power a redstone repeater, send a signal in “arrow” direction (the input). The signal goes out on the opposite side (the output) with a delay. To change the delay, use the redstone repeater. The delay is between 0.1 and 0.4 seconds long and can be changed in steps of 0.1 seconds. It is indicated by the position of the moving redstone torch.").."\n"..
S("To lock a repeater, send a signal from an adjacent repeater into one of its sides. While locked, the moving redstone torch disappears, the output doesn't change and the input signal is ignored.") S("To lock a repeater, send a signal from an adjacent repeater into one of its sides. While locked, the moving redstone torch disappears, the output doesn't change and the input signal is ignored.")
icon = "mesecons_delayer_item.png" icon = "mesecons_delayer_item.png"
-- Check sides of constructed repeater and lock it, if required
-- Check sides of constructed repeater and lock it, if required on_construct = function(pos)
on_construct = function(pos) local node = minetest.get_node(pos)
local node = minetest.get_node(pos) local sides = delayer_get_sides(node)
local sides = delayer_get_sides(node) for s=1, #sides do
for s=1, #sides do local spos = vector.add(pos, sides[s])
local spos = vector.add(pos, sides[s]) local snode = minetest.get_node(spos)
local snode = minetest.get_node(spos) -- Is there a powered repeater at one of our sides?
-- Is there a powered repeater at one of our sides? local g = minetest.get_item_group(snode.name, "redstone_repeater")
local g = minetest.get_item_group(snode.name, "redstone_repeater") if g ~= 0 and mesecon.is_receptor_on(snode.name) then
if g ~= 0 and mesecon.is_receptor_on(snode.name) then -- The other repeater must also face towards the constructed node
-- The other repeater must also face towards the constructed node local sface = delayer_get_output_rules(snode)[1]
local sface = delayer_get_output_rules(snode)[1] local sface_pos = vector.add(spos, sface)
local sface_pos = vector.add(spos, sface) if vector.equals(sface_pos, pos) then
if vector.equals(sface_pos, pos) then -- Repeater is facing towards us! Now we just need to lock the costructed node
-- Repeater is facing towards us! Now we just need to lock the costructed node if mesecon.is_powered(pos, delayer_get_input_rules(node)[1]) ~= false then
if mesecon.is_powered(pos, delayer_get_input_rules(node)[1]) ~= false then local newnode = {name="mesecons_delayer:delayer_on_locked", param2 = node.param2}
local newnode = {name="mesecons_delayer:delayer_on_locked", param2 = node.param2} minetest.set_node(pos, newnode)
minetest.set_node(pos, newnode) mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(newnode)}, DEFAULT_DELAY, nil)
mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(newnode)}, DEFAULT_DELAY, nil) else
else minetest.set_node(pos, {name="mesecons_delayer:delayer_off_locked", param2 = node.param2})
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_locked", param2 = node.param2}) end
break
end end
break
end end
end end
end end
else
help = false
end end
else
help = false
end
local desc_off local desc_off
if i == 1 then if i == 1 then
desc_off = S("Redstone Repeater") desc_off = S("Redstone Repeater")
else else
desc_off = S("Redstone Repeater (Delay @1)", i) desc_off = S("Redstone Repeater (Delay @1)", i)
end end
minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), { minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), {
description = desc_off, description = desc_off,
inventory_image = icon, inventory_image = icon,
wield_image = icon, wield_image = icon,
_tt_help = tt, _tt_help = tt,
_doc_items_create_entry = help, _doc_items_create_entry = help,
_doc_items_longdesc = longdesc, _doc_items_longdesc = longdesc,
_doc_items_usagehelp = usagehelp, _doc_items_usagehelp = usagehelp,
drawtype = "nodebox", drawtype = "nodebox",
tiles = { tiles = {
"mesecons_delayer_off.png", "mesecons_delayer_off.png",
"mcl_stairs_stone_slab_top.png", "mcl_stairs_stone_slab_top.png",
"mesecons_delayer_sides_off.png", "mesecons_delayer_sides_off.png",
"mesecons_delayer_sides_off.png", "mesecons_delayer_sides_off.png",
"mesecons_delayer_ends_off.png", "mesecons_delayer_ends_off.png",
"mesecons_delayer_ends_off.png", "mesecons_delayer_ends_off.png",
},
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
--wield_image = "mesecons_delayer_off.png",
walkable = true,
selection_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
collision_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
node_box = {
type = "fixed",
fixed = boxes
},
groups = groups,
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = false,
is_ground_content = false,
drop = 'mesecons_delayer:delayer_off_1',
on_rightclick = function (pos, node, clicker)
local protname = clicker:get_player_name()
if minetest.is_protected(pos, protname) then
minetest.record_protection_violation(pos, protname)
return
end
if node.name=="mesecons_delayer:delayer_off_1" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_2", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_2" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_3", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_3" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_4", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_4" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_1", param2=node.param2})
end
end,
on_construct = on_construct,
delayer_time = delaytime,
delayer_onstate = "mesecons_delayer:delayer_on_"..tostring(i),
delayer_lockstate = "mesecons_delayer:delayer_off_locked",
sounds = mcl_sounds.node_sound_stone_defaults(),
mesecons = {
receptor =
{
state = mesecon.state.off,
rules = delayer_get_output_rules
}, },
effector = use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
{ --wield_image = "mesecons_delayer_off.png",
rules = delayer_get_input_rules, walkable = true,
action_on = delayer_activate selection_box = {
} type = "fixed",
}, fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
on_rotate = on_rotate,
})
minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), {
description = S("Redstone Repeater (Delay @1, Powered)", i),
_doc_items_create_entry = false,
drawtype = "nodebox",
tiles = {
"mesecons_delayer_on.png",
"mcl_stairs_stone_slab_top.png",
"mesecons_delayer_sides_on.png",
"mesecons_delayer_sides_on.png",
"mesecons_delayer_ends_on.png",
"mesecons_delayer_ends_on.png",
},
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
walkable = true,
selection_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
collision_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
node_box = {
type = "fixed",
fixed = boxes
},
groups = {dig_immediate = 3, dig_by_water=1,destroy_by_lava_flow=1, dig_by_piston=1, attached_node=1, redstone_repeater=i, not_in_creative_inventory = 1},
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = false,
is_ground_content = false,
drop = 'mesecons_delayer:delayer_off_1',
on_rightclick = function (pos, node, clicker)
local protname = clicker:get_player_name()
if minetest.is_protected(pos, protname) then
minetest.record_protection_violation(pos, protname)
return
end
if node.name=="mesecons_delayer:delayer_on_1" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_2",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_2" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_3",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_3" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_4",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_4" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_1",param2=node.param2})
end
end,
after_dig_node = function(pos, oldnode)
check_unlock_repeater(pos, oldnode)
end,
delayer_time = delaytime,
delayer_offstate = "mesecons_delayer:delayer_off_"..tostring(i),
delayer_lockstate = "mesecons_delayer:delayer_on_locked",
sounds = mcl_sounds.node_sound_stone_defaults(),
mesecons = {
receptor =
{
state = mesecon.state.on,
rules = delayer_get_output_rules
}, },
effector = collision_box = {
{ type = "fixed",
rules = delayer_get_input_rules, fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
action_off = delayer_deactivate },
} node_box = {
}, type = "fixed",
on_rotate = on_rotate, fixed = boxes
}) },
groups = groups,
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = false,
is_ground_content = false,
drop = 'mesecons_delayer:delayer_off_1',
on_rightclick = function (pos, node, clicker)
local protname = clicker:get_player_name()
if minetest.is_protected(pos, protname) then
minetest.record_protection_violation(pos, protname)
return
end
if node.name=="mesecons_delayer:delayer_off_1" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_2", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_2" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_3", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_3" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_4", param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_off_4" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_off_1", param2=node.param2})
end
end,
on_construct = on_construct,
delayer_time = delaytime,
delayer_onstate = "mesecons_delayer:delayer_on_"..tostring(i),
delayer_lockstate = "mesecons_delayer:delayer_off_locked",
sounds = mcl_sounds.node_sound_stone_defaults(),
mesecons = {
receptor = {
state = mesecon.state.off,
rules = delayer_get_output_rules,
},
effector = {
rules = delayer_get_input_rules,
action_on = delayer_activate,
},
},
on_rotate = on_rotate,
})
minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), {
description = S("Redstone Repeater (Delay @1, Powered)", i),
_doc_items_create_entry = false,
drawtype = "nodebox",
tiles = {
"mesecons_delayer_on.png",
"mcl_stairs_stone_slab_top.png",
"mesecons_delayer_sides_on.png",
"mesecons_delayer_sides_on.png",
"mesecons_delayer_ends_on.png",
"mesecons_delayer_ends_on.png",
},
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
walkable = true,
selection_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
collision_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 },
},
node_box = {
type = "fixed",
fixed = boxes
},
groups = {dig_immediate = 3, dig_by_water=1,destroy_by_lava_flow=1, dig_by_piston=1, attached_node=1, redstone_repeater=i, not_in_creative_inventory = 1},
paramtype = "light",
paramtype2 = "facedir",
sunlight_propagates = false,
is_ground_content = false,
drop = 'mesecons_delayer:delayer_off_1',
on_rightclick = function (pos, node, clicker)
local protname = clicker:get_player_name()
if minetest.is_protected(pos, protname) then
minetest.record_protection_violation(pos, protname)
return
end
--HACK! we already know the node name, so we should generate the function to avoid multiple checks
if node.name=="mesecons_delayer:delayer_on_1" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_2",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_2" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_3",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_3" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_4",param2=node.param2})
elseif node.name=="mesecons_delayer:delayer_on_4" then
minetest.set_node(pos, {name="mesecons_delayer:delayer_on_1",param2=node.param2})
end
end,
after_dig_node = function(pos, oldnode)
check_unlock_repeater(pos, oldnode)
end,
delayer_time = delaytime,
delayer_offstate = "mesecons_delayer:delayer_off_"..tostring(i),
delayer_lockstate = "mesecons_delayer:delayer_on_locked",
sounds = mcl_sounds.node_sound_stone_defaults(),
mesecons = {
receptor = {
state = mesecon.state.on,
rules = delayer_get_output_rules,
},
effector = {
rules = delayer_get_input_rules,
action_off = delayer_deactivate,
},
},
on_rotate = on_rotate,
})
end end