techage/logic/button.lua

209 lines
5.9 KiB
Lua
Raw Normal View History

2019-08-24 00:35:37 +03:00
--[[
TechAge
=======
Copyright (C) 2017-2019 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Logic button
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
local function switch_on(pos)
local cycle_time = M(pos):get_int("cycle_time")
2019-08-24 11:54:20 +03:00
logic.swap_node(pos, "techage:ta3_button_on")
2019-08-24 00:35:37 +03:00
logic.send_on(pos, M(pos), cycle_time)
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
end
local function switch_off(pos)
2019-08-24 11:54:20 +03:00
logic.swap_node(pos, "techage:ta3_button_off")
2019-08-24 00:35:37 +03:00
logic.send_off(pos, M(pos))
2019-08-25 22:16:42 +03:00
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
2019-08-24 00:35:37 +03:00
end
local function formspec(meta)
local numbers = meta:get_string("numbers") or ""
local idx = meta:get_int("cycle_idx") or 0
if idx == 0 then idx = 1 end
return "size[7.5,6]"..
"dropdown[0.2,0;3;type;switch,button 2s,button 4s,button 8s,button 16s,button 32s;"..idx.."]"..
"field[0.5,2;7,1;numbers;"..S("Insert destination node number(s)")..";"..numbers.."]" ..
"checkbox[1,3;public;public;false]"..
"button_exit[2,4;3,1;exit;"..S("Save").."]"
end
2019-08-24 11:54:20 +03:00
minetest.register_node("techage:ta3_button_off", {
2019-08-24 00:35:37 +03:00
description = S("TA3 Button/Switch"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_button.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_button.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_button_off.png",
},
after_place_node = function(pos, placer)
local meta = M(pos)
2019-08-24 11:54:20 +03:00
logic.after_place_node(pos, placer, "techage:ta3_button_off", S("TA3 Button/Switch"))
2019-08-24 00:35:37 +03:00
logic.infotext(meta, S("TA3 Button/Switch"))
meta:set_string("formspec", formspec(meta))
meta:set_string("public", "false")
meta:set_int("cycle_time", 0)
end,
on_receive_fields = function(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = M(pos)
if not techage.check_numbers(fields.numbers, player:get_player_name()) then
return
end
2019-08-25 22:16:42 +03:00
print(dump(fields))
2019-08-24 00:35:37 +03:00
meta:set_string("numbers", fields.numbers)
if fields.public then
meta:set_string("public", fields.public)
end
local cycle_time = nil
if fields.type == "switch" then
meta:set_int("cycle_idx", 1)
cycle_time = 0
elseif fields.type == "button 2s" then
meta:set_int("cycle_idx", 2)
cycle_time = 2
elseif fields.type == "button 4s" then
meta:set_int("cycle_idx", 3)
cycle_time = 4
elseif fields.type == "button 8s" then
meta:set_int("cycle_idx", 4)
cycle_time = 8
elseif fields.type == "button 16s" then
meta:set_int("cycle_idx", 5)
cycle_time = 16
elseif fields.type == "button 32s" then
meta:set_int("cycle_idx", 6)
cycle_time = 32
end
if cycle_time ~= nil then
meta:set_int("cycle_time", cycle_time)
end
logic.infotext(meta, S("TA3 Button/Switch"))
if fields.exit then
meta:set_string("formspec", nil)
2019-08-25 22:16:42 +03:00
meta:set_string("fixed" , "true")
2019-08-24 00:35:37 +03:00
else
meta:set_string("formspec", formspec(meta))
end
end,
on_rightclick = function(pos, node, clicker)
local meta = M(pos)
2019-08-25 22:16:42 +03:00
local fixed = meta:get_string("fixed")
if fixed == "true" then
2019-08-24 00:35:37 +03:00
if meta:get_string("public") == "true" or
clicker:get_player_name() == meta:get_string("owner") then
switch_on(pos)
end
end
end,
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Button/Switch"))
meta:set_string("formspec", formspec(meta))
return res
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
techage.remove_node(pos)
tubelib2.del_mem(pos)
2019-08-24 00:35:37 +03:00
end,
on_rotate = screwdriver.disallow,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2019-08-24 11:54:20 +03:00
minetest.register_node("techage:ta3_button_on", {
2019-08-24 00:35:37 +03:00
description = ("TA3 Button/Switch"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_button.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_button.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_button_on.png",
},
on_rightclick = function(pos, node, clicker)
local meta = M(pos)
local numbers = meta:get_string("numbers")
if numbers ~= "" and numbers ~= nil then
if meta:get_string("public") == "true" or
clicker:get_player_name() == meta:get_string("owner") then
switch_off(pos)
end
end
end,
on_timer = switch_off,
on_rotate = screwdriver.disallow,
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Button/Switch"))
meta:set_string("formspec", formspec(meta))
return res
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
techage.remove_node(pos)
tubelib2.del_mem(pos)
2019-08-24 00:35:37 +03:00
end,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
2019-08-24 11:54:20 +03:00
drop = "techage:ta3_button_off",
2019-08-24 00:35:37 +03:00
})
minetest.register_craft({
2019-08-24 11:54:20 +03:00
output = "techage:ta3_button_off",
2019-08-24 00:35:37 +03:00
recipe = {
{"", "group:wood", ""},
{"default:glass", "techage:vacuum_tube", ""},
{"", "group:wood", ""},
},
})
techage.register_entry_page("ta3l", "button",
S("TA3 Button/Switch"),
S("The Button/Switch is used to send on/off commands to machines/nodes.@n"..
"It can be configured as switch or as button with configurable cycle time from 2 to 32s)"),
2019-08-24 11:54:20 +03:00
"techage:ta3_button_on")