Add files via upload
This commit is contained in:
149
digiswitch.lua
149
digiswitch.lua
@@ -13,7 +13,7 @@ local function get_mesecon_rule_for_side (side)
|
||||
if side == "white" then
|
||||
return { { x = 0, y = 1, z = 0 } }
|
||||
elseif side == "black" then
|
||||
return { { x = 0, y = -1, z = 0 } } -- down doesn't work
|
||||
return { { x = 0, y = -1, z = 0 } }
|
||||
elseif side == "red" then
|
||||
return { { x = -1, y = 0, z = 0 } }
|
||||
elseif side == "green" then
|
||||
@@ -32,13 +32,106 @@ local function get_mesecon_rule_for_side (side)
|
||||
{ x = 0, y = 0, z = 1 },
|
||||
{ x = 0, y = 0, z = -1 },
|
||||
{ x = 0, y = 1, z = 0 },
|
||||
{ x = 0, y = -1, z = 0 }, -- down doesn't work
|
||||
{ x = 0, y = -1, z = 0 },
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local side_bits =
|
||||
{
|
||||
["white"] = 0,
|
||||
["black"] = 1,
|
||||
["red"] = 2,
|
||||
["green"] = 3,
|
||||
["blue"] = 4,
|
||||
["yellow"] = 5
|
||||
}
|
||||
|
||||
|
||||
|
||||
local function get_side_bit (side)
|
||||
if side then
|
||||
if side == "switch" then
|
||||
return 64
|
||||
end
|
||||
|
||||
local bit = side_bits[side]
|
||||
|
||||
if bit then
|
||||
return math.pow (2, bit)
|
||||
end
|
||||
end
|
||||
|
||||
return 63
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function is_side_on (bits, side)
|
||||
local bit = get_side_bit (side)
|
||||
|
||||
for i = 0, 6, 1 do
|
||||
if (bit % 2) == 1 and (bits % 2) ~= 1 then
|
||||
return false
|
||||
end
|
||||
|
||||
bit = math.floor (bit / 2)
|
||||
bits = math.floor (bits / 2)
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function set_side_bit (bits, side, on)
|
||||
local bit = get_side_bit (side)
|
||||
local result = 0
|
||||
|
||||
for i = 0, 6, 1 do
|
||||
if (bit % 2) == 1 then
|
||||
if on then
|
||||
result = result + math.pow (2, i)
|
||||
end
|
||||
elseif (bits % 2) == 1 then
|
||||
result = result + math.pow (2, i)
|
||||
end
|
||||
|
||||
bit = math.floor (bit / 2)
|
||||
bits = math.floor (bits / 2)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function switch_on (pos, side)
|
||||
utils.mesecon_receptor_on (pos, get_mesecon_rule_for_side (side))
|
||||
|
||||
local node = utils.get_far_node (pos)
|
||||
if node then
|
||||
node.param1 = set_side_bit (node.param1, side, true)
|
||||
minetest.swap_node (pos, node)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function switch_off (pos, side)
|
||||
utils.mesecon_receptor_off (pos, get_mesecon_rule_for_side (side))
|
||||
|
||||
local node = utils.get_far_node (pos)
|
||||
if node then
|
||||
node.param1 = set_side_bit (node.param1, side, false)
|
||||
minetest.swap_node (pos, node)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function digilines_support ()
|
||||
return
|
||||
{
|
||||
@@ -80,9 +173,9 @@ local function digilines_support ()
|
||||
end
|
||||
|
||||
if words[1] == "on" then
|
||||
utils.mesecon_receptor_on (pos, get_mesecon_rule_for_side (words[2]))
|
||||
switch_on (pos, words[2])
|
||||
elseif words[1] == "off" then
|
||||
utils.mesecon_receptor_off (pos, get_mesecon_rule_for_side (words[2]))
|
||||
switch_off (pos, words[2])
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -99,16 +192,32 @@ local function mesecon_support ()
|
||||
{
|
||||
receptor =
|
||||
{
|
||||
state = mesecon.state.off,
|
||||
rules =
|
||||
{
|
||||
{ x = 1, y = 0, z = 0 },
|
||||
{ x = -1, y = 0, z = 0 },
|
||||
{ x = 0, y = 0, z = 1 },
|
||||
{ x = 0, y = 0, z = -1 },
|
||||
{ x = 0, y = 1, z = 0 },
|
||||
{ x = 0, y = -1, z = 0 }, -- down doesn't work
|
||||
}
|
||||
state = mesecon.state.on,
|
||||
|
||||
rules = function (node)
|
||||
if is_side_on (node.param1, "switch") then
|
||||
return utils.mesecon_default_rules
|
||||
end
|
||||
|
||||
local r = { }
|
||||
local sides =
|
||||
{
|
||||
"white",
|
||||
"black",
|
||||
"red",
|
||||
"green",
|
||||
"blue",
|
||||
"yellow",
|
||||
}
|
||||
|
||||
for _, side in ipairs (sides) do
|
||||
if is_side_on (node.param1, side) then
|
||||
r[#r + 1] = get_mesecon_rule_for_side (side)[1]
|
||||
end
|
||||
end
|
||||
|
||||
return r
|
||||
end
|
||||
},
|
||||
}
|
||||
end
|
||||
@@ -124,7 +233,7 @@ local function on_construct (pos)
|
||||
local formspec =
|
||||
"formspec_version[3]\n"..
|
||||
"size[6.0,4.0]\n"..
|
||||
"field[1.0,0.8;4.0,1.0;channel;Channel;]\n"..
|
||||
"field[1.0,0.8;4.0,1.0;channel;Channel;${channel}]\n"..
|
||||
"button_exit[2.0,2.5;2.0,1.0;set;Set]\n"
|
||||
|
||||
meta:set_string ("formspec", formspec)
|
||||
@@ -145,14 +254,6 @@ local function on_receive_fields (pos, formname, fields, sender)
|
||||
|
||||
if meta then
|
||||
meta:set_string ("channel", fields.channel or "")
|
||||
|
||||
local formspec =
|
||||
"formspec_version[3]\n"..
|
||||
"size[6.0,4.0]\n"..
|
||||
"field[1.0,0.8;4.0,1.0;channel;Channel;"..minetest.formspec_escape (meta:get_string ("channel")).."]\n"..
|
||||
"button_exit[2.0,2.5;2.0,1.0;set;Set]\n"
|
||||
|
||||
meta:set_string ("formspec", formspec)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -172,6 +273,8 @@ minetest.register_node ("lwcomponents:digiswitch", {
|
||||
{-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
|
||||
}
|
||||
},
|
||||
paramtype = "none",
|
||||
param1 = 0,
|
||||
groups = { cracky = 2, oddly_breakable_by_hand = 2 },
|
||||
sounds = default.node_sound_stone_defaults (),
|
||||
mesecons = mesecon_support (),
|
||||
|
Reference in New Issue
Block a user