techage/logic/repeater.lua

133 lines
3.6 KiB
Lua
Raw Normal View History

2019-08-24 00:35:37 +03:00
--[[
TechAge
=======
2020-01-31 21:55:10 +03:00
Copyright (C) 2017-2020 Joachim Stolberg
2019-08-24 00:35:37 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2019-08-24 00:35:37 +03:00
See LICENSE.txt for more information
Signal Repeater
2022-01-03 23:40:31 +03:00
2019-08-24 00:35:37 +03:00
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
local OVER_LOAD_MAX = 10
local CYCLE_TIME = 2
local function formspec(meta)
local numbers = meta:get_string("numbers") or ""
return "size[7.5,3]"..
"field[0.5,1;7,1;numbers;"..S("Insert destination node number(s)")..";"..numbers.."]" ..
"button_exit[2,2;3,1;exit;"..S("Save").."]"
end
2019-08-24 11:54:20 +03:00
minetest.register_node("techage:ta3_repeater", {
2019-08-24 00:35:37 +03:00
description = S("TA3 Repeater"),
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_repeater.png",
},
after_place_node = function(pos, placer)
local meta = M(pos)
2020-01-31 21:55:10 +03:00
local mem = techage.get_mem(pos)
2019-08-24 11:54:20 +03:00
logic.after_place_node(pos, placer, "techage:ta3_repeater", S("TA3 Repeater"))
2019-08-24 00:35:37 +03:00
logic.infotext(meta, S("TA3 Repeater"))
meta:set_string("formspec", formspec(meta))
mem.overload_cnt = 0
minetest.get_node_timer(pos):start(CYCLE_TIME)
end,
on_receive_fields = function(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2019-08-24 00:35:37 +03:00
local meta = M(pos)
if techage.check_numbers(fields.numbers, player:get_player_name()) then
meta:set_string("numbers", fields.numbers)
logic.infotext(M(pos), S("TA3 Repeater"))
meta:set_string("formspec", formspec(meta))
end
end,
2022-01-03 23:40:31 +03:00
2019-08-24 00:35:37 +03:00
on_timer = function(pos,elapsed)
2020-01-31 21:55:10 +03:00
local mem = techage.get_mem(pos)
2019-08-24 00:35:37 +03:00
mem.overload_cnt = 0
return true
end,
2022-01-03 23:40:31 +03:00
2019-08-24 00:35:37 +03:00
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Repeater"))
meta:set_string("formspec", formspec(meta))
return res
end,
2022-01-03 23:40:31 +03:00
2020-06-14 00:32:00 +03:00
after_dig_node = function(pos, oldnode, oldmetadata)
techage.remove_node(pos, oldnode, oldmetadata)
2020-01-31 21:55:10 +03:00
techage.del_mem(pos)
2019-08-24 00:35:37 +03:00
end,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
2019-08-24 00:35:37 +03:00
})
minetest.register_craft({
2019-08-24 11:54:20 +03:00
output = "techage:ta3_repeater",
2019-08-24 00:35:37 +03:00
recipe = {
{"", "group:wood", ""},
{"techage:vacuum_tube", "", "techage:vacuum_tube"},
{"", "group:wood", ""},
},
})
2019-08-24 11:54:20 +03:00
techage.register_node({"techage:ta3_repeater"}, {
on_recv_message = function(pos, src, topic, payload)
2020-01-31 21:55:10 +03:00
local mem = techage.get_mem(pos)
2019-08-24 00:35:37 +03:00
mem.overload_cnt = (mem.overload_cnt or 0) + 1
if mem.overload_cnt > OVER_LOAD_MAX then
logic.infotext(M(pos), S("TA3 Repeater"), "fault (overloaded)")
minetest.get_node_timer(pos):stop()
return false
else
local numbers = M(pos):get_string("numbers") or ""
techage.counting_start(M(pos):get_string("owner"))
techage.send_multi(src, numbers, topic, payload)
techage.counting_stop()
2019-08-24 00:35:37 +03:00
end
end,
2023-07-01 22:26:04 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
local mem = techage.get_mem(pos)
mem.overload_cnt = (mem.overload_cnt or 0) + 1
if mem.overload_cnt > OVER_LOAD_MAX then
logic.infotext(M(pos), S("TA3 Repeater"), "fault (overloaded)")
minetest.get_node_timer(pos):stop()
return 3
else
if topic == 1 then
local numbers = M(pos):get_string("numbers") or ""
techage.counting_start(M(pos):get_string("owner"))
techage.send_multi(src, numbers, payload[1] == 1 and "on" or "off")
techage.counting_stop()
return 0
end
end
return 2
end,
2019-08-24 00:35:37 +03:00
on_node_load = function(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end,
2022-01-03 23:40:31 +03:00
})